Skip to content

Commit

Permalink
refactor: merge tx builder into one builder
Browse files Browse the repository at this point in the history
  • Loading branch information
paologalligit committed Jan 15, 2025
1 parent 39133c2 commit b797b10
Show file tree
Hide file tree
Showing 35 changed files with 569 additions and 738 deletions.
27 changes: 6 additions & 21 deletions api/accounts/accounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,27 +313,12 @@ func initAccountServer(t *testing.T, enabledDeprecated bool) {
}

func buildTxWithClauses(txType int, chainTag byte, clauses ...*tx.Clause) *tx.Transaction {
var trx *tx.Transaction
switch txType {
case tx.LegacyTxType:
builder := new(tx.LegacyBuilder).
ChainTag(chainTag).
Expiration(10).
Gas(1000000)
for _, c := range clauses {
builder.Clause(c)
}
trx = builder.Build()
case tx.DynamicFeeTxType:
builder := new(tx.DynFeeBuilder).
ChainTag(chainTag).
Expiration(10).
Gas(1000000)
for _, c := range clauses {
builder.Clause(c)
}
trx = builder.Build()
}
trx, _ := tx.NewTxBuilder(txType).
ChainTag(chainTag).
Expiration(10).
Gas(1000000).
Clauses(clauses).
Build()
return tx.MustSign(trx, genesis.DevAccounts()[0].PrivateKey)
}

Expand Down
47 changes: 22 additions & 25 deletions api/blocks/blocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,31 +230,28 @@ func initBlockServer(t *testing.T) {

addr := thor.BytesToAddress([]byte("to"))
cla := tx.NewClause(&addr).WithValue(big.NewInt(10000))
legacyTx := tx.MustSign(
new(tx.LegacyBuilder).
ChainTag(thorChain.Repo().ChainTag()).
GasPriceCoef(1).
Expiration(10).
Gas(21000).
Nonce(1).
Clause(cla).
BlockRef(tx.NewBlockRef(0)).
Build(),
genesis.DevAccounts()[0].PrivateKey,
)
dynFeeTx := tx.MustSign(
new(tx.DynFeeBuilder).
ChainTag(thorChain.Repo().ChainTag()).
MaxFeePerGas(big.NewInt(1000)).
MaxPriorityFeePerGas(big.NewInt(100000)).
Expiration(10).
Gas(21000).
Nonce(2).
Clause(cla).
BlockRef(tx.NewBlockRef(0)).
Build(),
genesis.DevAccounts()[0].PrivateKey,
)
legacyTx, _ := tx.NewTxBuilder(tx.LegacyTxType).
ChainTag(thorChain.Repo().ChainTag()).
GasPriceCoef(1).
Expiration(10).
Gas(21000).
Nonce(1).
Clause(cla).
BlockRef(tx.NewBlockRef(0)).
Build()
legacyTx = tx.MustSign(legacyTx, genesis.DevAccounts()[0].PrivateKey)

dynFeeTx, _ := tx.NewTxBuilder(tx.DynamicFeeTxType).
ChainTag(thorChain.Repo().ChainTag()).
MaxFeePerGas(big.NewInt(100000)).
MaxPriorityFeePerGas(big.NewInt(100)).
Expiration(10).
Gas(21000).
Nonce(2).
Clause(cla).
BlockRef(tx.NewBlockRef(0)).
Build()
dynFeeTx = tx.MustSign(dynFeeTx, genesis.DevAccounts()[0].PrivateKey)

require.NoError(t, thorChain.MintTransactions(genesis.DevAccounts()[0], legacyTx, dynFeeTx))

Expand Down
27 changes: 14 additions & 13 deletions api/debug/debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ 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 := new(tx.LegacyBuilder).
noClausesTx, _ := tx.NewTxBuilder(tx.LegacyTxType).
ChainTag(thorChain.Repo().ChainTag()).
Expiration(10).
Gas(21000).
Expand All @@ -550,7 +550,7 @@ func initDebugServer(t *testing.T) {

cla := tx.NewClause(&addr).WithValue(big.NewInt(10000))
cla2 := tx.NewClause(&addr).WithValue(big.NewInt(10000))
transaction = new(tx.LegacyBuilder).
transaction, _ = tx.NewTxBuilder(tx.LegacyTxType).
ChainTag(thorChain.Repo().ChainTag()).
GasPriceCoef(1).
Expiration(10).
Expand All @@ -562,17 +562,18 @@ func initDebugServer(t *testing.T) {
Build()
transaction = tx.MustSign(transaction, genesis.DevAccounts()[0].PrivateKey)

dynFeeTx := tx.MustSign(
new(tx.DynFeeBuilder).
ChainTag(thorChain.Repo().ChainTag()).
Expiration(10).
Gas(21000).
MaxFeePerGas(big.NewInt(1000)).
MaxPriorityFeePerGas(big.NewInt(100000)).
Nonce(1).
Clause(cla).
BlockRef(tx.NewBlockRef(0)).
Build(),
dynFeeTx, _ := tx.NewTxBuilder(tx.DynamicFeeTxType).
ChainTag(thorChain.Repo().ChainTag()).
Expiration(10).
Gas(21000).
MaxFeePerGas(big.NewInt(1000)).
MaxPriorityFeePerGas(big.NewInt(100000)).
Nonce(1).
Clause(cla).
BlockRef(tx.NewBlockRef(0)).
Build()
dynFeeTx = tx.MustSign(
dynFeeTx,
genesis.DevAccounts()[0].PrivateKey,
)

Expand Down
4 changes: 2 additions & 2 deletions api/subscriptions/block_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func initChain(t *testing.T) *testchain.Chain {

addr := thor.BytesToAddress([]byte("to"))
cla := tx.NewClause(&addr).WithValue(big.NewInt(10000))
tr := new(tx.LegacyBuilder).
tr, _ := tx.NewTxBuilder(tx.LegacyTxType).
ChainTag(thorChain.Repo().ChainTag()).
GasPriceCoef(1).
Expiration(10).
Expand All @@ -75,7 +75,7 @@ func initChain(t *testing.T) *testchain.Chain {
Build()
tr = tx.MustSign(tr, genesis.DevAccounts()[0].PrivateKey)

txDeploy := new(tx.DynFeeBuilder).
txDeploy, _ := tx.NewTxBuilder(tx.DynamicFeeTxType).
ChainTag(thorChain.Repo().ChainTag()).
MaxFeePerGas(big.NewInt(1)).
Expiration(100).
Expand Down
47 changes: 15 additions & 32 deletions api/subscriptions/pending_tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func TestPendingTx_DispatchLoop(t *testing.T) {
p.Subscribe(txCh)

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

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

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

// Assert that the channel did not receive the second transaction
Expand Down Expand Up @@ -165,7 +165,7 @@ func TestPendingTx_NoWriteAfterUnsubscribe(t *testing.T) {

done := make(chan struct{})
// Attempt to write a new transaction
trx := createLegacyTx(thorChain.Repo(), 0)
trx := createTx(thorChain.Repo(), 0, tx.LegacyTxType)
assert.NotPanics(t, func() {
p.dispatch(trx, done) // dispatch should not panic after unsubscribe
}, "Dispatching after unsubscribe should not panic")
Expand Down Expand Up @@ -203,7 +203,7 @@ func TestPendingTx_UnsubscribeOnWebSocketClose(t *testing.T) {
defer ws.Close()

// Add a transaction
trx := createLegacyTx(thorChain.Repo(), 0)
trx := createTx(thorChain.Repo(), 0, tx.LegacyTxType)
txPool.AddLocal(trx)

// Wait to receive transaction
Expand All @@ -225,38 +225,21 @@ func TestPendingTx_UnsubscribeOnWebSocketClose(t *testing.T) {
sub.pendingTx.mu.Unlock()
}

func createLegacyTx(repo *chain.Repository, addressNumber uint) *tx.Transaction {
func createTx(repo *chain.Repository, addressNumber uint, txType int) *tx.Transaction {
addr := thor.BytesToAddress([]byte("to"))
cla := tx.NewClause(&addr).WithValue(big.NewInt(10000))

trx, _ := tx.NewTxBuilder(txType).
ChainTag(repo.ChainTag()).
GasPriceCoef(1).
Expiration(1000).
Gas(21000).
Nonce(uint64(datagen.RandInt())).
Clause(cla).
BlockRef(tx.NewBlockRef(0)).
Build()
return tx.MustSign(
new(tx.LegacyBuilder).
ChainTag(repo.ChainTag()).
GasPriceCoef(1).
Expiration(1000).
Gas(21000).
Nonce(uint64(datagen.RandInt())).
Clause(cla).
BlockRef(tx.NewBlockRef(0)).
Build(),
genesis.DevAccounts()[addressNumber].PrivateKey,
)
}

func createDynFeeTx(repo *chain.Repository, addressNumber uint) *tx.Transaction {
addr := thor.BytesToAddress([]byte("to"))
cla := tx.NewClause(&addr).WithValue(big.NewInt(10000))

return tx.MustSign(
new(tx.DynFeeBuilder).
ChainTag(repo.ChainTag()).
MaxFeePerGas(big.NewInt(1)).
Expiration(10).
Gas(21000).
Nonce(1).
Clause(cla).
BlockRef(tx.NewBlockRef(0)).
Build(),
trx,
genesis.DevAccounts()[addressNumber].PrivateKey,
)
}
8 changes: 4 additions & 4 deletions api/subscriptions/subscriptions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ func initSubscriptionsServer(t *testing.T, enabledDeprecated bool) {

addr := thor.BytesToAddress([]byte("to"))
cla := tx.NewClause(&addr).WithValue(big.NewInt(10000))
tr := new(tx.DynFeeBuilder).
tr, _ := tx.NewTxBuilder(tx.DynamicFeeTxType).
ChainTag(thorChain.Repo().ChainTag()).
MaxFeePerGas(big.NewInt(1)).
Expiration(10).
Expand All @@ -255,7 +255,7 @@ func initSubscriptionsServer(t *testing.T, enabledDeprecated bool) {
}
tr = tr.WithSignature(sig)

txDeploy := new(tx.LegacyBuilder).
txDeploy, _ := tx.NewTxBuilder(tx.LegacyTxType).
ChainTag(thorChain.Repo().ChainTag()).
GasPriceCoef(1).
Expiration(100).
Expand Down Expand Up @@ -291,7 +291,7 @@ func TestSubscriptionsBacktrace(t *testing.T) {

addr := thor.BytesToAddress([]byte("to"))
cla := tx.NewClause(&addr).WithValue(big.NewInt(10000))
tr := new(tx.LegacyBuilder).
tr, _ := tx.NewTxBuilder(tx.LegacyTxType).
ChainTag(thorChain.Repo().ChainTag()).
GasPriceCoef(1).
Expiration(10).
Expand All @@ -307,7 +307,7 @@ func TestSubscriptionsBacktrace(t *testing.T) {
}
tr = tr.WithSignature(sig)

txDeploy := new(tx.LegacyBuilder).
txDeploy, _ := tx.NewTxBuilder(tx.LegacyTxType).
ChainTag(thorChain.Repo().ChainTag()).
GasPriceCoef(1).
Expiration(100).
Expand Down
28 changes: 15 additions & 13 deletions api/subscriptions/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func TestConvertTransfer(t *testing.T) {
repo, _ := chain.NewRepository(db, b)

// New tx
legacyTx := new(tx.LegacyBuilder).
legacyTx, _ := tx.NewTxBuilder(tx.LegacyTxType).
ChainTag(repo.ChainTag()).
GasPriceCoef(1).
Expiration(10).
Expand All @@ -105,7 +105,7 @@ func TestConvertTransfer(t *testing.T) {
Build()
legacyTx = tx.MustSign(legacyTx, genesis.DevAccounts()[0].PrivateKey)

dynFeeTx := new(tx.DynFeeBuilder).
dynFeeTx, _ := tx.NewTxBuilder(tx.DynamicFeeTxType).
ChainTag(repo.ChainTag()).
MaxFeePerGas(big.NewInt(1)).
Expiration(10).
Expand Down Expand Up @@ -175,14 +175,15 @@ func TestConvertEventWithBadSignature(t *testing.T) {
badSig := bytes.Repeat([]byte{0xf}, 65)

// New tx
transaction := new(tx.LegacyBuilder).
transaction, _ := tx.NewTxBuilder(tx.LegacyTxType).
ChainTag(1).
GasPriceCoef(1).
Expiration(10).
Gas(21000).
Nonce(1).
BlockRef(tx.NewBlockRef(0)).
Build().
Build()
transaction = transaction.
WithSignature(badSig[:])

// New block
Expand Down Expand Up @@ -214,15 +215,16 @@ func TestConvertEvent(t *testing.T) {
repo, _ := chain.NewRepository(db, b)

// New tx
transaction := tx.MustSign(
new(tx.LegacyBuilder).
ChainTag(repo.ChainTag()).
GasPriceCoef(1).
Expiration(10).
Gas(21000).
Nonce(1).
BlockRef(tx.NewBlockRef(0)).
Build(),
transaction, _ := tx.NewTxBuilder(tx.LegacyTxType).
ChainTag(repo.ChainTag()).
GasPriceCoef(1).
Expiration(10).
Gas(21000).
Nonce(1).
BlockRef(tx.NewBlockRef(0)).
Build()
transaction = tx.MustSign(
transaction,
genesis.DevAccounts()[0].PrivateKey,
)

Expand Down
6 changes: 3 additions & 3 deletions api/transactions/transactions_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ func createOneClausePerTx(signerPK *ecdsa.PrivateKey, thorChain *testchain.Chain
for gasUsed < 9_500_000 {
toAddr := datagen.RandAddress()
cla := tx.NewClause(&toAddr).WithValue(big.NewInt(10000))
transaction := new(tx.LegacyBuilder).
transaction, _ := tx.NewTxBuilder(tx.LegacyTxType).
ChainTag(thorChain.Repo().ChainTag()).
GasPriceCoef(1).
Expiration(math.MaxUint32 - 1).
Expand All @@ -260,7 +260,7 @@ func createManyClausesPerTx(signerPK *ecdsa.PrivateKey, thorChain *testchain.Cha
gasUsed := uint64(0)
txGas := uint64(42_000)

transactionBuilder := new(tx.LegacyBuilder).
transactionBuilder := tx.NewTxBuilder(tx.LegacyTxType).
ChainTag(thorChain.Repo().ChainTag()).
GasPriceCoef(1).
Expiration(math.MaxUint32 - 1).
Expand All @@ -272,7 +272,7 @@ func createManyClausesPerTx(signerPK *ecdsa.PrivateKey, thorChain *testchain.Cha
transactionBuilder.Clause(tx.NewClause(&toAddr).WithValue(big.NewInt(10000)))
}

transaction := transactionBuilder.Gas(gasUsed).Build()
transaction, _ := transactionBuilder.Gas(gasUsed).Build()

sig, err := crypto.Sign(transaction.SigningHash().Bytes(), signerPK)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions api/transactions/transactions_coverter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestConvertLegacyTransaction_Success(t *testing.T) {
cla := tx.NewClause(&addr).WithValue(big.NewInt(10000))
cla2 := tx.NewClause(&addr).WithValue(big.NewInt(10000))
br := tx.NewBlockRef(0)
transaction := new(tx.LegacyBuilder).
transaction, _ := tx.NewTxBuilder(tx.LegacyTxType).
ChainTag(123).
GasPriceCoef(1).
Expiration(10).
Expand Down Expand Up @@ -61,7 +61,7 @@ func TestConvertDynTransaction_Success(t *testing.T) {
br := tx.NewBlockRef(0)
maxFeePerGas := big.NewInt(25000)
maxPriorityFeePerGas := big.NewInt(100)
transaction := new(tx.DynFeeBuilder).
transaction, _ := tx.NewTxBuilder(tx.DynamicFeeTxType).
ChainTag(123).
MaxFeePerGas(maxFeePerGas).
MaxPriorityFeePerGas(maxPriorityFeePerGas).
Expand Down
Loading

0 comments on commit b797b10

Please sign in to comment.