Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add transaction signer #850

Merged
merged 11 commits into from
Oct 9, 2024
16 changes: 6 additions & 10 deletions api/accounts/accounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/crypto"
"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
ABI "github.com/vechain/thor/v2/abi"
Expand Down Expand Up @@ -262,7 +261,7 @@ func initAccountServer(t *testing.T) {
repo, _ := chain.NewRepository(db, b)
claTransfer := tx.NewClause(&addr).WithValue(value)
claDeploy := tx.NewClause(nil).WithData(bytecode)
transaction := buildTxWithClauses(t, repo.ChainTag(), claTransfer, claDeploy)
transaction := buildTxWithClauses(repo.ChainTag(), claTransfer, claDeploy)
contractAddr = thor.CreateContractAddress(transaction.ID(), 1, 0)
packTx(repo, stater, transaction, t)

Expand All @@ -274,7 +273,7 @@ func initAccountServer(t *testing.T) {
t.Fatal(err)
}
claCall := tx.NewClause(&contractAddr).WithData(input)
transactionCall := buildTxWithClauses(t, repo.ChainTag(), claCall)
transactionCall := buildTxWithClauses(repo.ChainTag(), claCall)
packTx(repo, stater, transactionCall, t)

router := mux.NewRouter()
Expand All @@ -284,7 +283,7 @@ func initAccountServer(t *testing.T) {
ts = httptest.NewServer(router)
}

func buildTxWithClauses(t *testing.T, chaiTag byte, clauses ...*tx.Clause) *tx.Transaction {
func buildTxWithClauses(chaiTag byte, clauses ...*tx.Clause) *tx.Transaction {
builder := new(tx.Builder).
ChainTag(chaiTag).
Expiration(10).
Expand All @@ -293,12 +292,9 @@ func buildTxWithClauses(t *testing.T, chaiTag byte, clauses ...*tx.Clause) *tx.T
builder.Clause(c)
}

transaction := builder.Build()
sig, err := crypto.Sign(transaction.SigningHash().Bytes(), genesis.DevAccounts()[0].PrivateKey)
if err != nil {
t.Fatal(err)
}
return transaction.WithSignature(sig)
trx := builder.Build()

return tx.MustSign(trx, genesis.DevAccounts()[0].PrivateKey)
}

func packTx(repo *chain.Repository, stater *state.Stater, transaction *tx.Transaction, t *testing.T) {
Expand Down
29 changes: 13 additions & 16 deletions api/blocks/blocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"testing"
"time"

"github.com/ethereum/go-ethereum/crypto"
"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -171,28 +170,26 @@ func initBlockServer(t *testing.T) {
repo, _ := chain.NewRepository(db, b)
addr := thor.BytesToAddress([]byte("to"))
cla := tx.NewClause(&addr).WithValue(big.NewInt(10000))
tx := new(tx.Builder).
ChainTag(repo.ChainTag()).
GasPriceCoef(1).
Expiration(10).
Gas(21000).
Nonce(1).
Clause(cla).
BlockRef(tx.NewBlockRef(0)).
Build()
trx := tx.MustSign(
new(tx.Builder).
ChainTag(repo.ChainTag()).
GasPriceCoef(1).
Expiration(10).
Gas(21000).
Nonce(1).
Clause(cla).
BlockRef(tx.NewBlockRef(0)).
Build(),
genesis.DevAccounts()[0].PrivateKey,
)

sig, err := crypto.Sign(tx.SigningHash().Bytes(), genesis.DevAccounts()[0].PrivateKey)
if err != nil {
t.Fatal(err)
}
tx = tx.WithSignature(sig)
packer := packer.New(repo, stater, genesis.DevAccounts()[0].Address, &genesis.DevAccounts()[0].Address, thor.NoFork)
sum, _ := repo.GetBlockSummary(b.Header().ID())
flow, err := packer.Schedule(sum, uint64(time.Now().Unix()))
if err != nil {
t.Fatal(err)
}
err = flow.Adopt(tx)
err = flow.Adopt(trx)
if err != nil {
t.Fatal(err)
}
Expand Down
13 changes: 2 additions & 11 deletions api/debug/debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (

"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/crypto"
"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
"github.com/vechain/thor/v2/block"
Expand Down Expand Up @@ -532,11 +531,7 @@ func initDebugServer(t *testing.T) {
Expiration(10).
Gas(21000).
Build()
sig, err := crypto.Sign(noClausesTx.SigningHash().Bytes(), genesis.DevAccounts()[0].PrivateKey)
if err != nil {
t.Fatal(err)
}
noClausesTx = noClausesTx.WithSignature(sig)
noClausesTx = tx.MustSign(noClausesTx, genesis.DevAccounts()[0].PrivateKey)

cla := tx.NewClause(&addr).WithValue(big.NewInt(10000))
cla2 := tx.NewClause(&addr).WithValue(big.NewInt(10000))
Expand All @@ -550,12 +545,8 @@ func initDebugServer(t *testing.T) {
Clause(cla2).
BlockRef(tx.NewBlockRef(0)).
Build()
transaction = tx.MustSign(transaction, genesis.DevAccounts()[0].PrivateKey)

sig, err = crypto.Sign(transaction.SigningHash().Bytes(), genesis.DevAccounts()[0].PrivateKey)
if err != nil {
t.Fatal(err)
}
transaction = transaction.WithSignature(sig)
packer := packer.New(repo, stater, genesis.DevAccounts()[0].Address, &genesis.DevAccounts()[0].Address, thor.NoFork)
sum, _ := repo.GetBlockSummary(b.Header().ID())
flow, err := packer.Schedule(sum, uint64(time.Now().Unix()))
Expand Down
7 changes: 1 addition & 6 deletions api/subscriptions/block_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"testing"
"time"

"github.com/ethereum/go-ethereum/crypto"
"github.com/stretchr/testify/assert"
"github.com/vechain/thor/v2/block"
"github.com/vechain/thor/v2/chain"
Expand Down Expand Up @@ -86,12 +85,8 @@ func initChain(t *testing.T) (*chain.Repository, []*block.Block, *txpool.TxPool)
Clause(cla).
BlockRef(tx.NewBlockRef(0)).
Build()
tr = tx.MustSign(tr, genesis.DevAccounts()[0].PrivateKey)

sig, err := crypto.Sign(tr.SigningHash().Bytes(), genesis.DevAccounts()[0].PrivateKey)
if err != nil {
t.Fatal(err)
}
tr = tr.WithSignature(sig)
packer := packer.New(repo, stater, genesis.DevAccounts()[0].Address, &genesis.DevAccounts()[0].Address, thor.NoFork)
sum, _ := repo.GetBlockSummary(b.Header().ID())
flow, err := packer.Schedule(sum, uint64(time.Now().Unix()))
Expand Down
34 changes: 15 additions & 19 deletions api/subscriptions/pending_tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"testing"
"time"

"github.com/ethereum/go-ethereum/crypto"
"github.com/stretchr/testify/assert"
"github.com/vechain/thor/v2/block"
"github.com/vechain/thor/v2/chain"
Expand Down Expand Up @@ -77,7 +76,7 @@ func TestPendingTx_DispatchLoop(t *testing.T) {
p.Subscribe(txCh)

// Add a new tx to the mempool
transaction := createTx(t, repo, 0)
transaction := createTx(repo, 0)
txPool.AddLocal(transaction)

// Start the dispatch loop
Expand All @@ -95,7 +94,7 @@ func TestPendingTx_DispatchLoop(t *testing.T) {
p.Unsubscribe(txCh)

// Add another tx to the mempool
tx2 := createTx(t, repo, 1)
tx2 := createTx(repo, 1)
txPool.AddLocal(tx2)

// Assert that the channel did not receive the second transaction
Expand Down Expand Up @@ -129,23 +128,20 @@ func addNewBlock(repo *chain.Repository, stater *state.Stater, b0 *block.Block,
}
}

func createTx(t *testing.T, repo *chain.Repository, addressNumber uint) *tx.Transaction {
func createTx(repo *chain.Repository, addressNumber uint) *tx.Transaction {
addr := thor.BytesToAddress([]byte("to"))
cla := tx.NewClause(&addr).WithValue(big.NewInt(10000))
tx := new(tx.Builder).
ChainTag(repo.ChainTag()).
GasPriceCoef(1).
Expiration(10).
Gas(21000).
Nonce(1).
Clause(cla).
BlockRef(tx.NewBlockRef(0)).
Build()
sig, err := crypto.Sign(tx.SigningHash().Bytes(), genesis.DevAccounts()[addressNumber].PrivateKey)
if err != nil {
t.Fatal(err)
}
tx = tx.WithSignature(sig)

return tx
return tx.MustSign(
new(tx.Builder).
ChainTag(repo.ChainTag()).
GasPriceCoef(1).
Expiration(10).
Gas(21000).
Nonce(1).
Clause(cla).
BlockRef(tx.NewBlockRef(0)).
Build(),
genesis.DevAccounts()[addressNumber].PrivateKey,
)
}
32 changes: 12 additions & 20 deletions api/subscriptions/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,7 @@ func TestConvertTransfer(t *testing.T) {
Nonce(1).
BlockRef(tx.NewBlockRef(0)).
Build()

sig, err := crypto.Sign(transaction.SigningHash().Bytes(), genesis.DevAccounts()[0].PrivateKey)
if err != nil {
t.Fatal(err)
}
transaction = transaction.WithSignature(sig)
transaction = tx.MustSign(transaction, genesis.DevAccounts()[0].PrivateKey)

// New block
blk := new(block.Builder).
Expand Down Expand Up @@ -187,20 +182,17 @@ func TestConvertEvent(t *testing.T) {
repo, _ := chain.NewRepository(db, b)

// New tx
transaction := new(tx.Builder).
ChainTag(repo.ChainTag()).
GasPriceCoef(1).
Expiration(10).
Gas(21000).
Nonce(1).
BlockRef(tx.NewBlockRef(0)).
Build()

sig, err := crypto.Sign(transaction.SigningHash().Bytes(), genesis.DevAccounts()[0].PrivateKey)
if err != nil {
t.Fatal(err)
}
transaction = transaction.WithSignature(sig)
transaction := tx.MustSign(
new(tx.Builder).
ChainTag(repo.ChainTag()).
GasPriceCoef(1).
Expiration(10).
Gas(21000).
Nonce(1).
BlockRef(tx.NewBlockRef(0)).
Build(),
genesis.DevAccounts()[0].PrivateKey,
)

// New block
blk := new(block.Builder).
Expand Down
41 changes: 14 additions & 27 deletions api/transactions/transactions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"time"

"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/rlp"
"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -112,18 +111,17 @@ func sendTx(t *testing.T) {
var expiration = uint32(10)
var gas = uint64(21000)

tx := new(tx.Builder).
BlockRef(blockRef).
ChainTag(chainTag).
Expiration(expiration).
Gas(gas).
Build()
sig, err := crypto.Sign(tx.SigningHash().Bytes(), genesis.DevAccounts()[0].PrivateKey)
if err != nil {
t.Fatal(err)
}
tx = tx.WithSignature(sig)
rlpTx, err := rlp.EncodeToBytes(tx)
trx := tx.MustSign(
new(tx.Builder).
BlockRef(blockRef).
ChainTag(chainTag).
Expiration(expiration).
Gas(gas).
Build(),
genesis.DevAccounts()[0].PrivateKey,
)

rlpTx, err := rlp.EncodeToBytes(trx)
if err != nil {
t.Fatal(err)
}
Expand All @@ -133,7 +131,7 @@ func sendTx(t *testing.T) {
if err = json.Unmarshal(res, &txObj); err != nil {
t.Fatal(err)
}
assert.Equal(t, tx.ID().String(), txObj["id"], "should be the same transaction id")
assert.Equal(t, trx.ID().String(), txObj["id"], "should be the same transaction id")
}

func getTxWithBadID(t *testing.T) {
Expand Down Expand Up @@ -296,26 +294,15 @@ func initTransactionServer(t *testing.T) {
Clause(cla).
BlockRef(tx.NewBlockRef(0)).
Build()
transaction = tx.MustSign(transaction, genesis.DevAccounts()[0].PrivateKey)

mempoolTx = new(tx.Builder).
ChainTag(repo.ChainTag()).
Expiration(10).
Gas(21000).
Nonce(1).
Build()

sig, err := crypto.Sign(transaction.SigningHash().Bytes(), genesis.DevAccounts()[0].PrivateKey)
if err != nil {
t.Fatal(err)
}

sig2, err := crypto.Sign(mempoolTx.SigningHash().Bytes(), genesis.DevAccounts()[0].PrivateKey)
if err != nil {
t.Fatal(err)
}

transaction = transaction.WithSignature(sig)
mempoolTx = mempoolTx.WithSignature(sig2)
mempoolTx = tx.MustSign(mempoolTx, genesis.DevAccounts()[0].PrivateKey)

packer := packer.New(repo, stater, genesis.DevAccounts()[0].Address, &genesis.DevAccounts()[0].Address, thor.NoFork)
sum, _ := repo.GetBlockSummary(b.Header().ID())
Expand Down
9 changes: 5 additions & 4 deletions cmd/thor/node/tx_stash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"sort"
"testing"

"github.com/ethereum/go-ethereum/crypto"
"github.com/stretchr/testify/assert"
"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/storage"
Expand All @@ -20,9 +19,11 @@ import (
)

func newTx() *tx.Transaction {
tx := new(tx.Builder).Nonce(rand.Uint64()).Build() // nolint:gosec
sig, _ := crypto.Sign(tx.SigningHash().Bytes(), genesis.DevAccounts()[0].PrivateKey)
return tx.WithSignature(sig)
return tx.MustSign(
new(tx.Builder).
Nonce(rand.Uint64()).Build(), // nolint:gosec,
genesis.DevAccounts()[0].PrivateKey,
)
}

func TestTxStash(t *testing.T) {
Expand Down
8 changes: 2 additions & 6 deletions cmd/thor/solo/solo.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/common/mclock"
"github.com/ethereum/go-ethereum/crypto"
"github.com/pkg/errors"
"github.com/vechain/thor/v2/block"
"github.com/vechain/thor/v2/builtin"
Expand Down Expand Up @@ -261,14 +260,11 @@ func (s *Solo) newTx(clauses []*tx.Clause, from genesis.DevAccount) (*tx.Transac
builder.Clause(c)
}

newTx := builder.BlockRef(tx.NewBlockRef(0)).
trx := builder.BlockRef(tx.NewBlockRef(0)).
Expiration(math.MaxUint32).
Nonce(rand.Uint64()). // #nosec
DependsOn(nil).
Gas(1_000_000).
Build()

sig, err := crypto.Sign(newTx.SigningHash().Bytes(), from.PrivateKey)

return newTx.WithSignature(sig), err
return tx.Sign(trx, from.PrivateKey)
}
Loading
Loading