Skip to content

Commit

Permalink
changing the builder to a tx.signer
Browse files Browse the repository at this point in the history
  • Loading branch information
otherview committed Sep 24, 2024
1 parent e267e13 commit fb481d2
Show file tree
Hide file tree
Showing 15 changed files with 184 additions and 131 deletions.
13 changes: 6 additions & 7 deletions api/accounts/accounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"github.com/ethereum/go-ethereum/common/math"
"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
ABI "github.com/vechain/thor/v2/abi"
"github.com/vechain/thor/v2/api/accounts"
"github.com/vechain/thor/v2/block"
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,9 +292,9 @@ func buildTxWithClauses(t *testing.T, chaiTag byte, clauses ...*tx.Clause) *tx.T
builder.Clause(c)
}

transaction, err := builder.BuildAndSign(genesis.DevAccounts()[0].PrivateKey)
require.NoError(t, err)
return transaction
trx := builder.Build()

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

func packTx(repo *chain.Repository, stater *state.Stater, transaction *tx.Transaction, t *testing.T) {
Expand Down
24 changes: 13 additions & 11 deletions api/blocks/blocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,24 +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, err := new(tx.Builder).
ChainTag(repo.ChainTag()).
GasPriceCoef(1).
Expiration(10).
Gas(21000).
Nonce(1).
Clause(cla).
BlockRef(tx.NewBlockRef(0)).
BuildAndSign(genesis.DevAccounts()[0].PrivateKey)
require.NoError(t, err)
trx := tx.MustSignTx(
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,
)

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: 6 additions & 7 deletions api/debug/debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"github.com/ethereum/go-ethereum/common/math"
"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/vechain/thor/v2/block"
"github.com/vechain/thor/v2/builtin"
"github.com/vechain/thor/v2/chain"
Expand Down Expand Up @@ -527,16 +526,16 @@ func initDebugServer(t *testing.T) {

// Adding an empty clause transaction to the block to cover the case of
// scanning multiple txs before getting the right one
noClausesTx, err := new(tx.Builder).
noClausesTx := new(tx.Builder).
ChainTag(repo.ChainTag()).
Expiration(10).
Gas(21000).
BuildAndSign(genesis.DevAccounts()[0].PrivateKey)
require.NoError(t, err)
Build()
noClausesTx = tx.MustSignTx(noClausesTx, genesis.DevAccounts()[0].PrivateKey)

cla := tx.NewClause(&addr).WithValue(big.NewInt(10000))
cla2 := tx.NewClause(&addr).WithValue(big.NewInt(10000))
transaction, err = new(tx.Builder).
transaction = new(tx.Builder).
ChainTag(repo.ChainTag()).
GasPriceCoef(1).
Expiration(10).
Expand All @@ -545,8 +544,8 @@ func initDebugServer(t *testing.T) {
Clause(cla).
Clause(cla2).
BlockRef(tx.NewBlockRef(0)).
BuildAndSign(genesis.DevAccounts()[0].PrivateKey)
require.NoError(t, err)
Build()
transaction = tx.MustSignTx(transaction, 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
7 changes: 3 additions & 4 deletions api/subscriptions/block_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/vechain/thor/v2/block"
"github.com/vechain/thor/v2/chain"
"github.com/vechain/thor/v2/genesis"
Expand Down Expand Up @@ -77,16 +76,16 @@ func initChain(t *testing.T) (*chain.Repository, []*block.Block, *txpool.TxPool)

addr := thor.BytesToAddress([]byte("to"))
cla := tx.NewClause(&addr).WithValue(big.NewInt(10000))
tr, err := new(tx.Builder).
tr := new(tx.Builder).
ChainTag(repo.ChainTag()).
GasPriceCoef(1).
Expiration(10).
Gas(21000).
Nonce(1).
Clause(cla).
BlockRef(tx.NewBlockRef(0)).
BuildAndSign(genesis.DevAccounts()[0].PrivateKey)
require.NoError(t, err)
Build()
tr = tx.MustSignTx(tr, 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
32 changes: 16 additions & 16 deletions api/subscriptions/pending_tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/vechain/thor/v2/block"
"github.com/vechain/thor/v2/chain"
"github.com/vechain/thor/v2/genesis"
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,19 +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, err := new(tx.Builder).
ChainTag(repo.ChainTag()).
GasPriceCoef(1).
Expiration(10).
Gas(21000).
Nonce(1).
Clause(cla).
BlockRef(tx.NewBlockRef(0)).
BuildAndSign(genesis.DevAccounts()[addressNumber].PrivateKey)
require.NoError(t, err)

return tx

return tx.MustSignTx(
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,
)
}
27 changes: 14 additions & 13 deletions api/subscriptions/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/crypto"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/vechain/thor/v2/block"
"github.com/vechain/thor/v2/chain"
"github.com/vechain/thor/v2/genesis"
Expand Down Expand Up @@ -96,15 +95,15 @@ func TestConvertTransfer(t *testing.T) {
repo, _ := chain.NewRepository(db, b)

// New tx
transaction, err := new(tx.Builder).
transaction := new(tx.Builder).
ChainTag(repo.ChainTag()).
GasPriceCoef(1).
Expiration(10).
Gas(21000).
Nonce(1).
BlockRef(tx.NewBlockRef(0)).
BuildAndSign(genesis.DevAccounts()[0].PrivateKey)
require.NoError(t, err)
Build()
transaction = tx.MustSignTx(transaction, genesis.DevAccounts()[0].PrivateKey)

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

// New tx
transaction, err := new(tx.Builder).
ChainTag(repo.ChainTag()).
GasPriceCoef(1).
Expiration(10).
Gas(21000).
Nonce(1).
BlockRef(tx.NewBlockRef(0)).
BuildAndSign(genesis.DevAccounts()[0].PrivateKey)
require.NoError(t, err)
transaction := tx.MustSignTx(
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
35 changes: 18 additions & 17 deletions api/transactions/transactions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"github.com/ethereum/go-ethereum/rlp"
"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/vechain/thor/v2/api/transactions"
"github.com/vechain/thor/v2/chain"
"github.com/vechain/thor/v2/genesis"
Expand Down Expand Up @@ -112,15 +111,17 @@ func sendTx(t *testing.T) {
var expiration = uint32(10)
var gas = uint64(21000)

tx, err := new(tx.Builder).
BlockRef(blockRef).
ChainTag(chainTag).
Expiration(expiration).
Gas(gas).
BuildAndSign(genesis.DevAccounts()[0].PrivateKey)
require.NoError(t, err)

rlpTx, err := rlp.EncodeToBytes(tx)
trx := tx.MustSignTx(
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 @@ -130,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 @@ -284,24 +285,24 @@ func initTransactionServer(t *testing.T) {
repo, _ = chain.NewRepository(db, b)
addr := thor.BytesToAddress([]byte("to"))
cla := tx.NewClause(&addr).WithValue(big.NewInt(10000))
transaction, err = new(tx.Builder).
transaction = new(tx.Builder).
ChainTag(repo.ChainTag()).
GasPriceCoef(1).
Expiration(10).
Gas(21000).
Nonce(1).
Clause(cla).
BlockRef(tx.NewBlockRef(0)).
BuildAndSign(genesis.DevAccounts()[0].PrivateKey)
require.NoError(t, err)
Build()
transaction = tx.MustSignTx(transaction, genesis.DevAccounts()[0].PrivateKey)

mempoolTx, err = new(tx.Builder).
mempoolTx = new(tx.Builder).
ChainTag(repo.ChainTag()).
Expiration(10).
Gas(21000).
Nonce(1).
BuildAndSign(genesis.DevAccounts()[0].PrivateKey)
require.NoError(t, err)
Build()
mempoolTx = tx.MustSignTx(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
7 changes: 5 additions & 2 deletions cmd/thor/node/tx_stash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ import (
)

func newTx() *tx.Transaction {
tx, _ := new(tx.Builder).Nonce(rand.Uint64()).BuildAndSign(genesis.DevAccounts()[0].PrivateKey) // nolint:gosec
return tx
return tx.MustSignTx(
new(tx.Builder).
Nonce(rand.Uint64()).Build(), // nolint:gosec,
genesis.DevAccounts()[0].PrivateKey,
)
}

func TestTxStash(t *testing.T) {
Expand Down
5 changes: 3 additions & 2 deletions cmd/thor/solo/solo.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,11 @@ func (s *Solo) newTx(clauses []*tx.Clause, from genesis.DevAccount) (*tx.Transac
builder.Clause(c)
}

return builder.BlockRef(tx.NewBlockRef(0)).
trx := builder.BlockRef(tx.NewBlockRef(0)).
Expiration(math.MaxUint32).
Nonce(rand.Uint64()). // #nosec
DependsOn(nil).
Gas(1_000_000).
BuildAndSign(from.PrivateKey)
Build()
return tx.SignTx(trx, from.PrivateKey)
}
7 changes: 3 additions & 4 deletions consensus/consensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (

"github.com/ethereum/go-ethereum/crypto"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/vechain/thor/v2/block"
"github.com/vechain/thor/v2/builtin"
"github.com/vechain/thor/v2/chain"
Expand Down Expand Up @@ -578,11 +577,11 @@ func TestValidateBlockBody(t *testing.T) {
{
"TxOriginBlocked", func(t *testing.T) {
thor.MockBlocklist([]string{genesis.DevAccounts()[9].Address.String()})
tx, err := txBuilder(tc.tag).BuildAndSign(genesis.DevAccounts()[9].PrivateKey)
require.NoError(t, err)
trx := txBuilder(tc.tag).Build()
trx = tx.MustSignTx(trx, genesis.DevAccounts()[9].PrivateKey)

blk, err := tc.sign(
tc.builder(tc.original.Header()).Transaction(tx),
tc.builder(tc.original.Header()).Transaction(trx),
)
if err != nil {
t.Fatal(err)
Expand Down
Loading

0 comments on commit fb481d2

Please sign in to comment.