diff --git a/api/accounts/accounts_test.go b/api/accounts/accounts_test.go index e0f9e98cc..833c60f2f 100644 --- a/api/accounts/accounts_test.go +++ b/api/accounts/accounts_test.go @@ -286,7 +286,7 @@ func initAccountServer(t *testing.T, enabledDeprecated bool) { genesisBlock = thorChain.GenesisBlock() claTransfer := tx.NewClause(&addr).WithValue(value) claDeploy := tx.NewClause(nil).WithData(bytecode) - transaction := buildTxWithClauses(tx.LegacyTxType, thorChain.Repo().ChainTag(), claTransfer, claDeploy) + transaction := buildTxWithClauses(tx.TypeLegacy, thorChain.Repo().ChainTag(), claTransfer, claDeploy) contractAddr = thor.CreateContractAddress(transaction.ID(), 1, 0) method := "set" abi, _ := ABI.New([]byte(abiJSON)) @@ -296,7 +296,7 @@ func initAccountServer(t *testing.T, enabledDeprecated bool) { t.Fatal(err) } claCall := tx.NewClause(&contractAddr).WithData(input) - transactionCall := buildTxWithClauses(tx.LegacyTxType, thorChain.Repo().ChainTag(), claCall) + transactionCall := buildTxWithClauses(tx.TypeLegacy, thorChain.Repo().ChainTag(), claCall) require.NoError(t, thorChain.MintTransactions( genesis.DevAccounts()[0], @@ -312,7 +312,7 @@ func initAccountServer(t *testing.T, enabledDeprecated bool) { ts = httptest.NewServer(router) } -func buildTxWithClauses(txType int, chainTag byte, clauses ...*tx.Clause) *tx.Transaction { +func buildTxWithClauses(txType tx.TxType, chainTag byte, clauses ...*tx.Clause) *tx.Transaction { trx := tx.NewTxBuilder(txType). ChainTag(chainTag). Expiration(10). diff --git a/api/blocks/blocks_test.go b/api/blocks/blocks_test.go index acde024be..7d2f0ba24 100644 --- a/api/blocks/blocks_test.go +++ b/api/blocks/blocks_test.go @@ -236,7 +236,7 @@ func initBlockServer(t *testing.T) { addr := thor.BytesToAddress([]byte("to")) cla := tx.NewClause(&addr).WithValue(big.NewInt(10000)) - legacyTx := tx.NewTxBuilder(tx.LegacyTxType). + legacyTx := tx.NewTxBuilder(tx.TypeLegacy). ChainTag(thorChain.Repo().ChainTag()). GasPriceCoef(1). Expiration(10). @@ -248,7 +248,7 @@ func initBlockServer(t *testing.T) { legacyTx = tx.MustSign(legacyTx, genesis.DevAccounts()[0].PrivateKey) require.NoError(t, thorChain.MintTransactions(genesis.DevAccounts()[0], legacyTx)) - dynFeeTx := tx.NewTxBuilder(tx.DynamicFeeTxType). + dynFeeTx := tx.NewTxBuilder(tx.TypeDynamicFee). ChainTag(thorChain.Repo().ChainTag()). MaxFeePerGas(big.NewInt(thor.InitialBaseFee)). MaxPriorityFeePerGas(big.NewInt(100)). diff --git a/api/blocks/types.go b/api/blocks/types.go index 09291d331..265f28da1 100644 --- a/api/blocks/types.go +++ b/api/blocks/types.go @@ -196,7 +196,7 @@ func buildJSONEmbeddedTxs(txs tx.Transactions, receipts tx.Receipts) []*JSONEmbe Reverted: receipt.Reverted, Outputs: jos, } - if trx.Type() == tx.LegacyTxType { + if trx.Type() == tx.TypeLegacy { embedTx.GasPriceCoef = trx.GasPriceCoef() } else { embedTx.MaxFeePerGas = trx.MaxFeePerGas() diff --git a/api/debug/debug_test.go b/api/debug/debug_test.go index 8a78fdd80..27fe717c4 100644 --- a/api/debug/debug_test.go +++ b/api/debug/debug_test.go @@ -546,7 +546,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 := tx.NewTxBuilder(tx.LegacyTxType). + noClausesTx := tx.NewTxBuilder(tx.TypeLegacy). ChainTag(thorChain.Repo().ChainTag()). Expiration(10). Gas(21000). @@ -555,7 +555,7 @@ func initDebugServer(t *testing.T) { cla := tx.NewClause(&addr).WithValue(big.NewInt(10000)) cla2 := tx.NewClause(&addr).WithValue(big.NewInt(10000)) - transaction = tx.NewTxBuilder(tx.LegacyTxType). + transaction = tx.NewTxBuilder(tx.TypeLegacy). ChainTag(thorChain.Repo().ChainTag()). GasPriceCoef(1). Expiration(10). @@ -568,7 +568,7 @@ func initDebugServer(t *testing.T) { transaction = tx.MustSign(transaction, genesis.DevAccounts()[0].PrivateKey) require.NoError(t, thorChain.MintTransactions(genesis.DevAccounts()[0], transaction, noClausesTx)) - dynFeeTx := tx.NewTxBuilder(tx.DynamicFeeTxType). + dynFeeTx := tx.NewTxBuilder(tx.TypeDynamicFee). ChainTag(thorChain.Repo().ChainTag()). Expiration(10). Gas(21000). diff --git a/api/fees/fees_test.go b/api/fees/fees_test.go index 823563812..a54de5d7f 100644 --- a/api/fees/fees_test.go +++ b/api/fees/fees_test.go @@ -96,7 +96,7 @@ func initFeesServer(t *testing.T, backtraceLimit uint32, fixedCacheSize uint32, var dynFeeTx *tx.Transaction for i := 0; i < numberOfBlocks-1; i++ { - dynFeeTx = tx.NewTxBuilder(tx.DynamicFeeTxType). + dynFeeTx = tx.NewTxBuilder(tx.TypeDynamicFee). ChainTag(thorChain.Repo().ChainTag()). MaxFeePerGas(big.NewInt(250_000_000_000_000)). MaxPriorityFeePerGas(big.NewInt(100)). diff --git a/api/subscriptions/block_reader_test.go b/api/subscriptions/block_reader_test.go index 4ce428a72..044b99911 100644 --- a/api/subscriptions/block_reader_test.go +++ b/api/subscriptions/block_reader_test.go @@ -77,7 +77,7 @@ func initChain(t *testing.T) *testchain.Chain { addr := thor.BytesToAddress([]byte("to")) cla := tx.NewClause(&addr).WithValue(big.NewInt(10000)) - tr := tx.NewTxBuilder(tx.LegacyTxType). + tr := tx.NewTxBuilder(tx.TypeLegacy). ChainTag(thorChain.Repo().ChainTag()). GasPriceCoef(1). Expiration(10). @@ -89,7 +89,7 @@ func initChain(t *testing.T) *testchain.Chain { tr = tx.MustSign(tr, genesis.DevAccounts()[0].PrivateKey) require.NoError(t, thorChain.MintTransactions(genesis.DevAccounts()[0], tr)) - txDeploy := tx.NewTxBuilder(tx.DynamicFeeTxType). + txDeploy := tx.NewTxBuilder(tx.TypeDynamicFee). ChainTag(thorChain.Repo().ChainTag()). MaxFeePerGas(big.NewInt(thor.InitialBaseFee)). Expiration(100). diff --git a/api/subscriptions/pending_tx_test.go b/api/subscriptions/pending_tx_test.go index ef9dc195d..3cc16dda9 100644 --- a/api/subscriptions/pending_tx_test.go +++ b/api/subscriptions/pending_tx_test.go @@ -95,7 +95,7 @@ func TestPendingTx_DispatchLoop(t *testing.T) { p.Subscribe(txCh) // Add a new tx to the mempool - transaction := createTx(repo, 0, tx.LegacyTxType) + transaction := createTx(repo, 0, tx.TypeLegacy) txPool.AddLocal(transaction) // Start the dispatch loop @@ -113,7 +113,7 @@ func TestPendingTx_DispatchLoop(t *testing.T) { p.Unsubscribe(txCh) // Add another tx to the mempool - tx2 := createTx(repo, 1, tx.DynamicFeeTxType) + tx2 := createTx(repo, 1, tx.TypeDynamicFee) txPool.AddLocal(tx2) // Assert that the channel did not receive the second transaction @@ -162,7 +162,7 @@ func TestPendingTx_NoWriteAfterUnsubscribe(t *testing.T) { done := make(chan struct{}) // Attempt to write a new transaction - trx := createTx(thorChain.Repo(), 0, tx.LegacyTxType) + trx := createTx(thorChain.Repo(), 0, tx.TypeLegacy) assert.NotPanics(t, func() { p.dispatch(trx, done) // dispatch should not panic after unsubscribe }, "Dispatching after unsubscribe should not panic") @@ -200,7 +200,7 @@ func TestPendingTx_UnsubscribeOnWebSocketClose(t *testing.T) { defer ws.Close() // Add a transaction - trx := createTx(thorChain.Repo(), 0, tx.LegacyTxType) + trx := createTx(thorChain.Repo(), 0, tx.TypeLegacy) txPool.AddLocal(trx) // Wait to receive transaction @@ -222,7 +222,7 @@ func TestPendingTx_UnsubscribeOnWebSocketClose(t *testing.T) { sub.pendingTx.mu.Unlock() } -func createTx(repo *chain.Repository, addressNumber uint, txType int) *tx.Transaction { +func createTx(repo *chain.Repository, addressNumber uint, txType tx.TxType) *tx.Transaction { addr := thor.BytesToAddress([]byte("to")) cla := tx.NewClause(&addr).WithValue(big.NewInt(10000)) diff --git a/api/subscriptions/subscriptions_test.go b/api/subscriptions/subscriptions_test.go index 87dcae97f..45614448e 100644 --- a/api/subscriptions/subscriptions_test.go +++ b/api/subscriptions/subscriptions_test.go @@ -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 := tx.NewTxBuilder(tx.LegacyTxType). + tr := tx.NewTxBuilder(tx.TypeLegacy). ChainTag(thorChain.Repo().ChainTag()). GasPriceCoef(1). Expiration(10). @@ -255,7 +255,7 @@ func initSubscriptionsServer(t *testing.T, enabledDeprecated bool) { } tr = tr.WithSignature(sig) - txDeploy := tx.NewTxBuilder(tx.LegacyTxType). + txDeploy := tx.NewTxBuilder(tx.TypeLegacy). ChainTag(thorChain.Repo().ChainTag()). GasPriceCoef(1). Expiration(100). @@ -291,7 +291,7 @@ func TestSubscriptionsBacktrace(t *testing.T) { addr := thor.BytesToAddress([]byte("to")) cla := tx.NewClause(&addr).WithValue(big.NewInt(10000)) - tr := tx.NewTxBuilder(tx.LegacyTxType). + tr := tx.NewTxBuilder(tx.TypeLegacy). ChainTag(thorChain.Repo().ChainTag()). GasPriceCoef(1). Expiration(10). @@ -307,7 +307,7 @@ func TestSubscriptionsBacktrace(t *testing.T) { } tr = tr.WithSignature(sig) - txDeploy := tx.NewTxBuilder(tx.LegacyTxType). + txDeploy := tx.NewTxBuilder(tx.TypeLegacy). ChainTag(thorChain.Repo().ChainTag()). GasPriceCoef(1). Expiration(100). diff --git a/api/subscriptions/types_test.go b/api/subscriptions/types_test.go index c9028213b..45366125d 100644 --- a/api/subscriptions/types_test.go +++ b/api/subscriptions/types_test.go @@ -95,7 +95,7 @@ func TestConvertTransfer(t *testing.T) { repo, _ := chain.NewRepository(db, b) // New tx - legacyTx := tx.NewTxBuilder(tx.LegacyTxType). + legacyTx := tx.NewTxBuilder(tx.TypeLegacy). ChainTag(repo.ChainTag()). GasPriceCoef(1). Expiration(10). @@ -105,7 +105,7 @@ func TestConvertTransfer(t *testing.T) { MustBuild() legacyTx = tx.MustSign(legacyTx, genesis.DevAccounts()[0].PrivateKey) - dynFeeTx := tx.NewTxBuilder(tx.DynamicFeeTxType). + dynFeeTx := tx.NewTxBuilder(tx.TypeDynamicFee). ChainTag(repo.ChainTag()). MaxFeePerGas(big.NewInt(1)). Expiration(10). @@ -175,7 +175,7 @@ func TestConvertEventWithBadSignature(t *testing.T) { badSig := bytes.Repeat([]byte{0xf}, 65) // New tx - transaction := tx.NewTxBuilder(tx.LegacyTxType). + transaction := tx.NewTxBuilder(tx.TypeLegacy). ChainTag(1). GasPriceCoef(1). Expiration(10). @@ -215,7 +215,7 @@ func TestConvertEvent(t *testing.T) { repo, _ := chain.NewRepository(db, b) // New tx - transaction := tx.NewTxBuilder(tx.LegacyTxType). + transaction := tx.NewTxBuilder(tx.TypeLegacy). ChainTag(repo.ChainTag()). GasPriceCoef(1). Expiration(10). diff --git a/api/transactions/transactions_benchmark_test.go b/api/transactions/transactions_benchmark_test.go index 749d18fb5..9abb72089 100644 --- a/api/transactions/transactions_benchmark_test.go +++ b/api/transactions/transactions_benchmark_test.go @@ -238,9 +238,9 @@ func createOneClausePerTx(signerPK *ecdsa.PrivateKey, thorChain *testchain.Chain for gasUsed < 9_500_000 { toAddr := datagen.RandAddress() cla := tx.NewClause(&toAddr).WithValue(big.NewInt(10000)) - b := tx.NewTxBuilder(tx.LegacyTxType) + b := tx.NewTxBuilder(tx.TypeLegacy) if gasUsed%2 == 0 { - b = tx.NewTxBuilder(tx.DynamicFeeTxType) + b = tx.NewTxBuilder(tx.TypeDynamicFee) } transaction := b. ChainTag(thorChain.Repo().ChainTag()). @@ -271,7 +271,7 @@ func createManyClausesPerTxLegacy(signerPK *ecdsa.PrivateKey, thorChain *testcha gasUsed := uint64(0) txGas := uint64(42_000) - transactionBuilder := tx.NewTxBuilder(tx.LegacyTxType). + transactionBuilder := tx.NewTxBuilder(tx.TypeLegacy). ChainTag(thorChain.Repo().ChainTag()). GasPriceCoef(1). Expiration(math.MaxUint32 - 1). @@ -301,7 +301,7 @@ func createManyClausesPerTxDynFee(signerPK *ecdsa.PrivateKey, thorChain *testcha gasUsed := uint64(0) txGas := uint64(42_000) - transactionBuilder := tx.NewTxBuilder(tx.DynamicFeeTxType). + transactionBuilder := tx.NewTxBuilder(tx.TypeDynamicFee). ChainTag(thorChain.Repo().ChainTag()). MaxFeePerGas(big.NewInt(1000000)). MaxPriorityFeePerGas(big.NewInt(100)). diff --git a/api/transactions/transactions_converter.go b/api/transactions/transactions_converter.go index 86a118d02..b62a252a1 100644 --- a/api/transactions/transactions_converter.go +++ b/api/transactions/transactions_converter.go @@ -61,7 +61,7 @@ func convertTransaction(trx *tx.Transaction, header *block.Header) *Transaction } switch trx.Type() { - case tx.LegacyTxType: + case tx.TypeLegacy: t.GasPriceCoef = trx.GasPriceCoef() default: t.MaxFeePerGas = trx.MaxFeePerGas() diff --git a/api/transactions/transactions_coverter_test.go b/api/transactions/transactions_coverter_test.go index 7ed0ab97a..72af4ae75 100644 --- a/api/transactions/transactions_coverter_test.go +++ b/api/transactions/transactions_coverter_test.go @@ -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 := tx.NewTxBuilder(tx.LegacyTxType). + transaction := tx.NewTxBuilder(tx.TypeLegacy). ChainTag(123). GasPriceCoef(1). Expiration(10). @@ -61,7 +61,7 @@ func TestConvertDynTransaction_Success(t *testing.T) { br := tx.NewBlockRef(0) maxFeePerGas := big.NewInt(25000) maxPriorityFeePerGas := big.NewInt(100) - transaction := tx.NewTxBuilder(tx.DynamicFeeTxType). + transaction := tx.NewTxBuilder(tx.TypeDynamicFee). ChainTag(123). MaxFeePerGas(maxFeePerGas). MaxPriorityFeePerGas(maxPriorityFeePerGas). diff --git a/api/transactions/transactions_test.go b/api/transactions/transactions_test.go index a4c9121f5..e071a583d 100644 --- a/api/transactions/transactions_test.go +++ b/api/transactions/transactions_test.go @@ -127,12 +127,14 @@ func getTxReceipt(t *testing.T) { t.Fatal(err) } assert.Equal(t, receipt.GasUsed, legacyTx.Gas(), "receipt gas used not equal to transaction gas") + assert.Equal(t, receipt.Type, legacyTx.Type()) r = httpGetAndCheckResponseStatus(t, "/transactions/"+dynFeeTx.ID().String()+"/receipt", 200) if err := json.Unmarshal(r, &receipt); err != nil { t.Fatal(err) } assert.Equal(t, receipt.GasUsed, legacyTx.Gas(), "receipt gas used not equal to transaction gas") + assert.Equal(t, receipt.Type, dynFeeTx.Type()) } func sendLegacyTx(t *testing.T) { @@ -140,7 +142,7 @@ func sendLegacyTx(t *testing.T) { var expiration = uint32(10) var gas = uint64(21000) - trx := tx.NewTxBuilder(tx.LegacyTxType). + trx := tx.NewTxBuilder(tx.TypeLegacy). BlockRef(blockRef). ChainTag(chainTag). Expiration(expiration). @@ -169,7 +171,7 @@ func sendDynamicFeeTx(t *testing.T) { var expiration = uint32(10) var gas = uint64(21000) - trx := tx.NewTxBuilder(tx.DynamicFeeTxType). + trx := tx.NewTxBuilder(tx.TypeDynamicFee). BlockRef(blockRef). ChainTag(chainTag). Expiration(expiration). @@ -287,7 +289,7 @@ func sendTxWithBadFormat(t *testing.T) { } func sendTxThatCannotBeAcceptedInLocalMempool(t *testing.T) { - tx := tx.NewTxBuilder(tx.LegacyTxType).MustBuild() + tx := tx.NewTxBuilder(tx.TypeLegacy).MustBuild() rlpTx, err := tx.MarshalBinary() if err != nil { t.Fatal(err) @@ -341,7 +343,7 @@ func initTransactionServer(t *testing.T) { // Creating first block with legacy tx addr := thor.BytesToAddress([]byte("to")) cla := tx.NewClause(&addr).WithValue(big.NewInt(10000)) - legacyTx = tx.NewTxBuilder(tx.LegacyTxType). + legacyTx = tx.NewTxBuilder(tx.TypeLegacy). ChainTag(chainTag). GasPriceCoef(1). Expiration(10). @@ -353,7 +355,7 @@ func initTransactionServer(t *testing.T) { legacyTx = tx.MustSign(legacyTx, genesis.DevAccounts()[0].PrivateKey) require.NoError(t, thorChain.MintTransactions(genesis.DevAccounts()[0], legacyTx)) - dynFeeTx = tx.NewTxBuilder(tx.DynamicFeeTxType). + dynFeeTx = tx.NewTxBuilder(tx.TypeDynamicFee). ChainTag(chainTag). MaxFeePerGas(big.NewInt(thor.InitialBaseFee * 10)). MaxPriorityFeePerGas(big.NewInt(10)). @@ -369,7 +371,7 @@ func initTransactionServer(t *testing.T) { mempool := txpool.New(thorChain.Repo(), thorChain.Stater(), txpool.Options{Limit: 10000, LimitPerAccount: 16, MaxLifetime: 10 * time.Minute}, &forkConfig) - mempoolTx = tx.NewTxBuilder(tx.DynamicFeeTxType). + mempoolTx = tx.NewTxBuilder(tx.TypeDynamicFee). ChainTag(chainTag). Expiration(10). Gas(21000). @@ -405,11 +407,11 @@ func checkMatchingTx(t *testing.T, expectedTx *tx.Transaction, actualTx *transac assert.Equal(t, c.To(), actualTx.Clauses[i].To) } switch expectedTx.Type() { - case tx.LegacyTxType: + case tx.TypeLegacy: assert.Equal(t, expectedTx.GasPriceCoef(), actualTx.GasPriceCoef) assert.Empty(t, actualTx.MaxFeePerGas) assert.Empty(t, actualTx.MaxPriorityFeePerGas) - case tx.DynamicFeeTxType: + case tx.TypeDynamicFee: assert.Empty(t, actualTx.GasPriceCoef) assert.Equal(t, expectedTx.MaxFeePerGas(), actualTx.MaxFeePerGas) assert.Equal(t, expectedTx.MaxPriorityFeePerGas(), actualTx.MaxPriorityFeePerGas) diff --git a/api/transactions/types.go b/api/transactions/types.go index 3640fd5d6..62a0a66ca 100644 --- a/api/transactions/types.go +++ b/api/transactions/types.go @@ -82,6 +82,7 @@ type ReceiptMeta struct { // Receipt for json marshal type Receipt struct { + Type uint8 `json:"type,omitempty"` GasUsed uint64 `json:"gasUsed"` GasPayer thor.Address `json:"gasPayer"` Paid *math.HexOrDecimal256 `json:"paid"` @@ -121,6 +122,7 @@ func convertReceipt(txReceipt *tx.Receipt, header *block.Header, tx *tx.Transact return nil, err } receipt := &Receipt{ + Type: txReceipt.Type, GasUsed: txReceipt.GasUsed, GasPayer: txReceipt.GasPayer, Paid: &paid, diff --git a/api/transactions/types_test.go b/api/transactions/types_test.go index 413e24fa3..52f535023 100644 --- a/api/transactions/types_test.go +++ b/api/transactions/types_test.go @@ -21,14 +21,16 @@ import ( ) func TestErrorWhileRetrievingTxOriginInConvertReceipt(t *testing.T) { - txTypes := []int{tx.LegacyTxType, tx.DynamicFeeTxType} + txTypes := []tx.TxType{tx.TypeLegacy, tx.TypeDynamicFee} for _, txType := range txTypes { tr := tx.NewTxBuilder(txType).MustBuild() header := &block.Header{} receipt := &tx.Receipt{ - Reward: big.NewInt(100), - Paid: big.NewInt(10), + ReceiptBody: tx.ReceiptBody{ + Reward: big.NewInt(100), + Paid: big.NewInt(10), + }, } convRec, err := convertReceipt(receipt, header, tr) @@ -42,8 +44,8 @@ func TestErrorWhileRetrievingTxOriginInConvertReceipt(t *testing.T) { func TestConvertReceiptWhenTxHasNoClauseTo(t *testing.T) { value := big.NewInt(100) txs := []*tx.Transaction{ - newLegacyTx(tx.NewClause(nil).WithValue(value)), - newDynFeeTx(tx.NewClause(nil).WithValue(value)), + newTx(tx.NewClause(nil).WithValue(value), tx.TypeLegacy), + newTx(tx.NewClause(nil).WithValue(value), tx.TypeDynamicFee), } for _, tr := range txs { b := new(block.Builder).Build() @@ -64,8 +66,8 @@ func TestConvertReceipt(t *testing.T) { addr := randAddress() txs := []*tx.Transaction{ - newLegacyTx(tx.NewClause(&addr).WithValue(value)), - newDynFeeTx(tx.NewClause(&addr).WithValue(value)), + newTx(tx.NewClause(&addr).WithValue(value), tx.TypeLegacy), + newTx(tx.NewClause(&addr).WithValue(value), tx.TypeDynamicFee), } for _, tr := range txs { b := new(block.Builder).Build() @@ -75,6 +77,7 @@ func TestConvertReceipt(t *testing.T) { convRec, err := convertReceipt(receipt, header, tr) assert.NoError(t, err) + assert.Equal(t, receipt.Type, convRec.Type) assert.Equal(t, 1, len(convRec.Outputs)) assert.Equal(t, 1, len(convRec.Outputs[0].Events)) assert.Equal(t, 1, len(convRec.Outputs[0].Transfers)) @@ -95,36 +98,29 @@ func randAddress() (addr thor.Address) { func newReceipt() *tx.Receipt { return &tx.Receipt{ - Outputs: []*tx.Output{ - { - Events: tx.Events{{ - Address: randAddress(), - Topics: []thor.Bytes32{randomBytes32()}, - Data: randomBytes32().Bytes(), - }}, - Transfers: tx.Transfers{{ - Sender: randAddress(), - Recipient: randAddress(), - Amount: new(big.Int).SetBytes(randAddress().Bytes()), - }}, + ReceiptBody: tx.ReceiptBody{ + Outputs: []*tx.Output{ + { + Events: tx.Events{{ + Address: randAddress(), + Topics: []thor.Bytes32{randomBytes32()}, + Data: randomBytes32().Bytes(), + }}, + Transfers: tx.Transfers{{ + Sender: randAddress(), + Recipient: randAddress(), + Amount: new(big.Int).SetBytes(randAddress().Bytes()), + }}, + }, }, + Reward: big.NewInt(100), + Paid: big.NewInt(10), }, - Reward: big.NewInt(100), - Paid: big.NewInt(10), } } -func newLegacyTx(clause *tx.Clause) *tx.Transaction { - tx := tx.NewTxBuilder(tx.LegacyTxType). - Clause(clause). - MustBuild() - pk, _ := crypto.GenerateKey() - sig, _ := crypto.Sign(tx.SigningHash().Bytes(), pk) - return tx.WithSignature(sig) -} - -func newDynFeeTx(clause *tx.Clause) *tx.Transaction { - tx := tx.NewTxBuilder(tx.DynamicFeeTxType). +func newTx(clause *tx.Clause, txType tx.TxType) *tx.Transaction { + tx := tx.NewTxBuilder(txType). Clause(clause). MustBuild() pk, _ := crypto.GenerateKey() diff --git a/api/transfers/transfers_test.go b/api/transfers/transfers_test.go index 3afcf3098..f72538ea4 100644 --- a/api/transfers/transfers_test.go +++ b/api/transfers/transfers_test.go @@ -242,13 +242,15 @@ func createDb(t *testing.T) *logdb.LogDB { // Utilities functions func newReceipt() *tx.Receipt { return &tx.Receipt{ - Outputs: []*tx.Output{ - { - Transfers: tx.Transfers{{ - Sender: datagen.RandAddress(), - Recipient: datagen.RandAddress(), - Amount: new(big.Int).SetBytes(datagen.RandAddress().Bytes()), - }}, + ReceiptBody: tx.ReceiptBody{ + Outputs: []*tx.Output{ + { + Transfers: tx.Transfers{{ + Sender: datagen.RandAddress(), + Recipient: datagen.RandAddress(), + Amount: new(big.Int).SetBytes(datagen.RandAddress().Bytes()), + }}, + }, }, }, } diff --git a/block/block_test.go b/block/block_test.go index 84581abe0..e4c3e7759 100644 --- a/block/block_test.go +++ b/block/block_test.go @@ -18,8 +18,8 @@ import ( ) func TestBlock(t *testing.T) { - tx1 := tx.NewTxBuilder(tx.LegacyTxType).Clause(tx.NewClause(&thor.Address{})).Clause(tx.NewClause(&thor.Address{})).MustBuild() - tx2 := tx.NewTxBuilder(tx.DynamicFeeTxType).Clause(tx.NewClause(nil)).MustBuild() + tx1 := tx.NewTxBuilder(tx.TypeLegacy).Clause(tx.NewClause(&thor.Address{})).Clause(tx.NewClause(&thor.Address{})).MustBuild() + tx2 := tx.NewTxBuilder(tx.TypeDynamicFee).Clause(tx.NewClause(nil)).MustBuild() privKey := string("dce1443bd2ef0c2631adc1c67e5c93f13dc23a41c18b536effbbdcbcdb96fb65") diff --git a/block/builder_test.go b/block/builder_test.go index c27e22a72..7fa2798c5 100644 --- a/block/builder_test.go +++ b/block/builder_test.go @@ -39,7 +39,7 @@ func TestBuilder_Build(t *testing.T) { builder.StateRoot(hash) builder.ReceiptsRoot(hash) - tr := tx.NewTxBuilder(tx.LegacyTxType).MustBuild() + tr := tx.NewTxBuilder(tx.TypeLegacy).MustBuild() builder.Transaction(tr) features := tx.Features(0x01) diff --git a/chain/chain_test.go b/chain/chain_test.go index 0b570dd90..f57e37713 100644 --- a/chain/chain_test.go +++ b/chain/chain_test.go @@ -15,7 +15,7 @@ import ( "github.com/vechain/thor/v2/tx" ) -func newTx(txType int) *tx.Transaction { +func newTx(txType tx.TxType) *tx.Transaction { tx := tx.NewTxBuilder(txType).MustBuild() pk, _ := crypto.GenerateKey() sig, _ := crypto.Sign(tx.SigningHash().Bytes(), pk) @@ -24,7 +24,7 @@ func newTx(txType int) *tx.Transaction { func TestChain(t *testing.T) { _, repo := newTestRepo() - txTypes := []int{tx.LegacyTxType, tx.DynamicFeeTxType} + txTypes := []tx.TxType{tx.TypeLegacy, tx.TypeDynamicFee} for _, txType := range txTypes { tr := newTx(txType) diff --git a/chain/repository_test.go b/chain/repository_test.go index 8432356e5..4160d38ad 100644 --- a/chain/repository_test.go +++ b/chain/repository_test.go @@ -60,7 +60,7 @@ func TestRepositoryFunc(t *testing.T) { assert.Equal(t, b0summary, repo1.BestBlockSummary()) assert.Equal(t, repo1.GenesisBlock().Header().ID()[31], repo1.ChainTag()) - tx1 := tx.NewTxBuilder(tx.LegacyTxType).MustBuild() + tx1 := tx.NewTxBuilder(tx.TypeLegacy).MustBuild() receipt1 := &tx.Receipt{} b1 := newBlock(repo1.GenesisBlock(), 10, tx1) diff --git a/cmd/thor/node/node_benchmark_test.go b/cmd/thor/node/node_benchmark_test.go index 72f0c8f66..7bfffc323 100644 --- a/cmd/thor/node/node_benchmark_test.go +++ b/cmd/thor/node/node_benchmark_test.go @@ -262,7 +262,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 := tx.NewTxBuilder(tx.LegacyTxType). + transaction := tx.NewTxBuilder(tx.TypeLegacy). ChainTag(thorChain.Repo().ChainTag()). GasPriceCoef(1). Expiration(math.MaxUint32 - 1). @@ -289,7 +289,7 @@ func createManyClausesPerTx(signerPK *ecdsa.PrivateKey, thorChain *testchain.Cha gasUsed := uint64(0) txGas := uint64(42_000) - transactionBuilder := tx.NewTxBuilder(tx.LegacyTxType). + transactionBuilder := tx.NewTxBuilder(tx.TypeLegacy). ChainTag(thorChain.Repo().ChainTag()). GasPriceCoef(1). Expiration(math.MaxUint32 - 1). diff --git a/cmd/thor/node/tx_stash_test.go b/cmd/thor/node/tx_stash_test.go index 03b5215ac..336522308 100644 --- a/cmd/thor/node/tx_stash_test.go +++ b/cmd/thor/node/tx_stash_test.go @@ -18,7 +18,7 @@ import ( "github.com/vechain/thor/v2/tx" ) -func newTx(txType int) *tx.Transaction { +func newTx(txType tx.TxType) *tx.Transaction { return tx.MustSign( tx.NewTxBuilder(txType).Nonce(rand.Uint64()).MustBuild(), //#nosec genesis.DevAccounts()[0].PrivateKey, @@ -33,13 +33,13 @@ func TestTxStash(t *testing.T) { var saved tx.Transactions for i := 0; i < 11; i++ { - tx := newTx(tx.LegacyTxType) + tx := newTx(tx.TypeLegacy) assert.Nil(t, stash.Save(tx)) saved = append(saved, tx) } for i := 0; i < 11; i++ { - tx := newTx(tx.DynamicFeeTxType) + tx := newTx(tx.TypeDynamicFee) assert.Nil(t, stash.Save(tx)) saved = append(saved, tx) } diff --git a/cmd/thor/solo/solo.go b/cmd/thor/solo/solo.go index 010ed0d07..39aa2bbc9 100644 --- a/cmd/thor/solo/solo.go +++ b/cmd/thor/solo/solo.go @@ -250,7 +250,7 @@ func (s *Solo) init(ctx context.Context) error { // newTx builds and signs a new transaction from the given clauses func (s *Solo) newTx(clauses []*tx.Clause, from genesis.DevAccount) (*tx.Transaction, error) { - builder := tx.NewTxBuilder(tx.LegacyTxType).ChainTag(s.repo.ChainTag()) + builder := tx.NewTxBuilder(tx.TypeLegacy).ChainTag(s.repo.ChainTag()) for _, c := range clauses { builder.Clause(c) } diff --git a/cmd/thor/utils.go b/cmd/thor/utils.go index 561b1276a..c77acca88 100644 --- a/cmd/thor/utils.go +++ b/cmd/thor/utils.go @@ -425,8 +425,10 @@ func initChainRepository(gene *genesis.Genesis, mainDB *muxdb.MuxDB, logDB *logd } w := logDB.NewWriter() if err := w.Write(genesisBlock, tx.Receipts{{ - Outputs: []*tx.Output{ - {Events: genesisEvents, Transfers: genesisTransfers}, + ReceiptBody: tx.ReceiptBody{ + Outputs: []*tx.Output{ + {Events: genesisEvents, Transfers: genesisTransfers}, + }, }, }}); err != nil { return nil, errors.Wrap(err, "write genesis logs") diff --git a/consensus/consensus_test.go b/consensus/consensus_test.go index 40c522467..134f5d65b 100644 --- a/consensus/consensus_test.go +++ b/consensus/consensus_test.go @@ -28,7 +28,7 @@ import ( "github.com/vechain/thor/v2/vrf" ) -func txBuilder(tag byte, txType int) *tx.Builder { +func txBuilder(tag byte, txType tx.TxType) *tx.Builder { address := thor.BytesToAddress([]byte("addr")) return tx.NewTxBuilder(txType). GasPriceCoef(1). @@ -98,7 +98,7 @@ func newTestConsensus() (*testConsensus, error) { addr := thor.BytesToAddress([]byte("to")) cla := tx.NewClause(&addr).WithValue(big.NewInt(10000)) - txBuilder := txBuilder(repo.ChainTag(), tx.LegacyTxType).Clause(cla) + txBuilder := txBuilder(repo.ChainTag(), tx.TypeLegacy).Clause(cla) transaction := txSign(txBuilder) err = flow.Adopt(transaction) @@ -472,8 +472,8 @@ func TestVerifyBlock(t *testing.T) { }{ { "TxDepBroken", func(t *testing.T) { - txID := txSign(txBuilder(tc.tag, tx.LegacyTxType)).ID() - tx := txSign(txBuilder(tc.tag, tx.LegacyTxType).DependsOn(&txID)) + txID := txSign(txBuilder(tc.tag, tx.TypeLegacy)).ID() + tx := txSign(txBuilder(tc.tag, tx.TypeLegacy).DependsOn(&txID)) blk, err := tc.sign(tc.builder(tc.original.Header()).Transaction(tx)) if err != nil { @@ -487,7 +487,7 @@ func TestVerifyBlock(t *testing.T) { }, { "TxAlreadyExists", func(t *testing.T) { - tx := txSign(txBuilder(tc.tag, tx.LegacyTxType)) + tx := txSign(txBuilder(tc.tag, tx.TypeLegacy)) blk, err := tc.sign(tc.builder(tc.original.Header()).Transaction(tx).Transaction(tx)) if err != nil { t.Fatal(err) @@ -584,7 +584,7 @@ func TestConsent(t *testing.T) { }{ { "ErrTxsRootMismatch", func(t *testing.T) { - transaction := txSign(txBuilder(tc.tag, tx.LegacyTxType)) + transaction := txSign(txBuilder(tc.tag, tx.TypeLegacy)) transactions := tx.Transactions{transaction} blk := block.Compose(tc.original.Header(), transactions) expected := consensusError( @@ -600,7 +600,7 @@ func TestConsent(t *testing.T) { }, { "ErrChainTagMismatch", func(t *testing.T) { - blk, err := tc.sign(tc.builder(tc.original.Header()).Transaction(txSign(txBuilder(tc.tag+1, tx.LegacyTxType)))) + blk, err := tc.sign(tc.builder(tc.original.Header()).Transaction(txSign(txBuilder(tc.tag+1, tx.TypeLegacy)))) if err != nil { t.Fatal(err) } @@ -619,7 +619,7 @@ func TestConsent(t *testing.T) { "ErrRefFutureBlock", func(t *testing.T) { blk, err := tc.sign( tc.builder(tc.original.Header()).Transaction( - txSign(txBuilder(tc.tag, tx.LegacyTxType).BlockRef(tx.NewBlockRef(100))), + txSign(txBuilder(tc.tag, tx.TypeLegacy).BlockRef(tx.NewBlockRef(100))), )) if err != nil { t.Fatal(err) @@ -632,7 +632,7 @@ func TestConsent(t *testing.T) { { "TxOriginBlocked", func(t *testing.T) { thor.MockBlocklist([]string{genesis.DevAccounts()[9].Address.String()}) - trx := tx.MustSign(txBuilder(tc.tag, tx.LegacyTxType).MustBuild(), genesis.DevAccounts()[9].PrivateKey) + trx := tx.MustSign(txBuilder(tc.tag, tx.TypeLegacy).MustBuild(), genesis.DevAccounts()[9].PrivateKey) blk, err := tc.sign( tc.builder(tc.original.Header()).Transaction(trx), @@ -649,7 +649,7 @@ func TestConsent(t *testing.T) { }, { "TxSignerUnavailable", func(t *testing.T) { - tx := txBuilder(tc.tag, tx.LegacyTxType).MustBuild() + tx := txBuilder(tc.tag, tx.TypeLegacy).MustBuild() var sig [65]byte tx = tx.WithSignature(sig[:]) @@ -668,7 +668,7 @@ func TestConsent(t *testing.T) { }, { "UnsupportedFeatures", func(t *testing.T) { - tx := txBuilder(tc.tag, tx.LegacyTxType).Features(tx.Features(2)).MustBuild() + tx := txBuilder(tc.tag, tx.TypeLegacy).Features(tx.Features(2)).MustBuild() sig, _ := crypto.Sign(tx.SigningHash().Bytes(), genesis.DevAccounts()[2].PrivateKey) tx = tx.WithSignature(sig) @@ -685,7 +685,7 @@ func TestConsent(t *testing.T) { }, { "TxExpired", func(t *testing.T) { - tx := txSign(txBuilder(tc.tag, tx.LegacyTxType).BlockRef(tx.NewBlockRef(0)).Expiration(0)) + tx := txSign(txBuilder(tc.tag, tx.TypeLegacy).BlockRef(tx.NewBlockRef(0)).Expiration(0)) blk, err := tc.sign(tc.builder(tc.original.Header()).Transaction(tx).Transaction(tx)) if err != nil { t.Fatal(err) @@ -705,7 +705,7 @@ func TestConsent(t *testing.T) { }, { "ZeroGasTx", func(t *testing.T) { - txBuilder := tx.NewTxBuilder(tx.LegacyTxType). + txBuilder := tx.NewTxBuilder(tx.TypeLegacy). GasPriceCoef(0). Gas(0). Expiration(100). diff --git a/consensus/fork/galactica.go b/consensus/fork/galactica.go index 9ab781108..90fca6fa7 100644 --- a/consensus/fork/galactica.go +++ b/consensus/fork/galactica.go @@ -95,10 +95,10 @@ func CalcBaseFee(config *thor.ForkConfig, parent *block.Header) *big.Int { func GalacticaTxGasPriceAdapter(tr *tx.Transaction, gasPrice *big.Int) *GalacticaFeeMarketItems { var maxPriorityFee, maxFee *big.Int switch tr.Type() { - case tx.LegacyTxType: + case tx.TypeLegacy: maxPriorityFee = gasPrice maxFee = gasPrice - case tx.DynamicFeeTxType: + case tx.TypeDynamicFee: maxPriorityFee = tr.MaxPriorityFeePerGas() maxFee = tr.MaxFeePerGas() } @@ -124,7 +124,8 @@ func GalacticaGasPrice(tr *tx.Transaction, baseGasPrice *big.Int, galacticaItems feeItems := GalacticaTxGasPriceAdapter(tr, gasPrice) // This gasPrice is the same that will be used when refunding the user - // it takes into account the priority fee that will be paid to the validator + // it takes into account the priority fee that will be paid to the validator and the base fee that will be implicitly burned + // tracked by Energy.TotalAddSub return math.BigMin(new(big.Int).Add(feeItems.MaxPriorityFee, galacticaItems.BaseFee), feeItems.MaxFee) } @@ -144,3 +145,15 @@ func GalacticaPriorityPrice(tr *tx.Transaction, baseGasPrice, provedWork *big.In */ return math.BigMin(feeItems.MaxPriorityFee, new(big.Int).Sub(feeItems.MaxFee, galacticaItems.BaseFee)) } + +func CalculateReward(gasUsed uint64, rewardGasPrice, rewardRatio *big.Int, isGalactica bool) *big.Int { + reward := new(big.Int).SetUint64(gasUsed) + reward.Mul(reward, rewardGasPrice) + if isGalactica { + return reward + } + // Returning the 30% of the reward + reward.Mul(reward, rewardRatio) + reward.Div(reward, big.NewInt(1e18)) + return reward +} diff --git a/consensus/fork/galactica_test.go b/consensus/fork/galactica_test.go index d7b9a222c..fe6bef00f 100644 --- a/consensus/fork/galactica_test.go +++ b/consensus/fork/galactica_test.go @@ -256,7 +256,7 @@ func TestBaseFeeLimits(t *testing.T) { func TestGalacticaGasPrice(t *testing.T) { baseGasPrice := big.NewInt(1_000_000_000) baseFee := big.NewInt(20_000_000) - legacyTr := tx.NewTxBuilder(tx.LegacyTxType).GasPriceCoef(255).MustBuild() + legacyTr := tx.NewTxBuilder(tx.TypeLegacy).GasPriceCoef(255).MustBuild() tests := []struct { name string @@ -285,7 +285,7 @@ func TestGalacticaGasPrice(t *testing.T) { { name: "galactica is activated, dynamic fee transaction with maxPriorityFee+baseFee as price", f: func(t *testing.T) { - tr := tx.NewTxBuilder(tx.DynamicFeeTxType).MaxFeePerGas(big.NewInt(250_000_000)).MaxPriorityFeePerGas(big.NewInt(15_000)).MustBuild() + tr := tx.NewTxBuilder(tx.TypeDynamicFee).MaxFeePerGas(big.NewInt(250_000_000)).MaxPriorityFeePerGas(big.NewInt(15_000)).MustBuild() res := GalacticaGasPrice(tr, baseGasPrice, &GalacticaItems{ IsActive: true, BaseFee: baseFee, @@ -297,7 +297,7 @@ func TestGalacticaGasPrice(t *testing.T) { { name: "galactica is activated, dynamic fee transaction with maxFee as price", f: func(t *testing.T) { - tr := tx.NewTxBuilder(tx.DynamicFeeTxType).MaxFeePerGas(big.NewInt(20_500_000)).MaxPriorityFeePerGas(big.NewInt(1_000_000)).MustBuild() + tr := tx.NewTxBuilder(tx.TypeDynamicFee).MaxFeePerGas(big.NewInt(20_500_000)).MaxPriorityFeePerGas(big.NewInt(1_000_000)).MustBuild() res := GalacticaGasPrice(tr, baseGasPrice, &GalacticaItems{ IsActive: true, BaseFee: baseFee, @@ -318,7 +318,7 @@ func TestGalacticaPriorityPrice(t *testing.T) { baseGasPrice := big.NewInt(1_000_000_000) baseFee := big.NewInt(20_000_000) provedWork := big.NewInt(1) - legacyTr := tx.NewTxBuilder(tx.LegacyTxType).GasPriceCoef(255).MustBuild() + legacyTr := tx.NewTxBuilder(tx.TypeLegacy).GasPriceCoef(255).MustBuild() tests := []struct { name string @@ -349,7 +349,7 @@ func TestGalacticaPriorityPrice(t *testing.T) { { name: "galactica is activated, dynamic fee transaction with maxPriorityFee as priority fee", f: func(t *testing.T) { - tr := tx.NewTxBuilder(tx.DynamicFeeTxType).MaxFeePerGas(big.NewInt(250_000_000)).MaxPriorityFeePerGas(big.NewInt(15_000)).MustBuild() + tr := tx.NewTxBuilder(tx.TypeDynamicFee).MaxFeePerGas(big.NewInt(250_000_000)).MaxPriorityFeePerGas(big.NewInt(15_000)).MustBuild() res := GalacticaPriorityPrice(tr, baseGasPrice, provedWork, &GalacticaItems{ IsActive: true, BaseFee: baseFee, @@ -360,7 +360,7 @@ func TestGalacticaPriorityPrice(t *testing.T) { { name: "galactica is activated, dynamic fee transaction with maxFee-baseFee as priority fee", f: func(t *testing.T) { - tr := tx.NewTxBuilder(tx.DynamicFeeTxType).MaxFeePerGas(big.NewInt(20_500_000)).MaxPriorityFeePerGas(big.NewInt(1_000_000)).MustBuild() + tr := tx.NewTxBuilder(tx.TypeDynamicFee).MaxFeePerGas(big.NewInt(20_500_000)).MaxPriorityFeePerGas(big.NewInt(1_000_000)).MustBuild() res := GalacticaPriorityPrice(tr, baseGasPrice, provedWork, &GalacticaItems{ IsActive: true, BaseFee: baseFee, @@ -377,3 +377,50 @@ func TestGalacticaPriorityPrice(t *testing.T) { }) } } + +func TestCalculateReward(t *testing.T) { + rewardRatio := thor.InitialRewardRatio + tests := []struct { + name string + gasUsed uint64 + rewardGasPrice *big.Int + isGalactica bool + expectedReward *big.Int + }{ + { + name: "Galactica active, full reward", + gasUsed: 1000, + rewardGasPrice: big.NewInt(100), + isGalactica: true, + expectedReward: big.NewInt(100000), + }, + { + name: "Galactica inactive, 30% reward", + gasUsed: 1000, + rewardGasPrice: big.NewInt(100), + isGalactica: false, + expectedReward: big.NewInt(30000), + }, + { + name: "Galactica active, zero gas used", + gasUsed: 0, + rewardGasPrice: big.NewInt(100), + isGalactica: true, + expectedReward: big.NewInt(0), + }, + { + name: "Galactica inactive, zero gas used", + gasUsed: 0, + rewardGasPrice: big.NewInt(100), + isGalactica: false, + expectedReward: big.NewInt(0), + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + reward := CalculateReward(tt.gasUsed, tt.rewardGasPrice, rewardRatio, tt.isGalactica) + assert.Equal(t, tt.expectedReward, reward) + }) + } +} diff --git a/consensus/validator.go b/consensus/validator.go index 9d84450d4..0e9302e4d 100644 --- a/consensus/validator.go +++ b/consensus/validator.go @@ -255,7 +255,7 @@ func (c *Consensus) validateBlockBody(blk *block.Block) error { return consensusError(fmt.Sprintf("tx ref future block: ref %v, current %v", tr.BlockRef().Number(), header.Number())) case tr.IsExpired(header.Number()): return consensusError(fmt.Sprintf("tx expired: ref %v, current %v, expiration %v", tr.BlockRef().Number(), header.Number(), tr.Expiration())) - case header.Number() < c.forkConfig.GALACTICA && tr.Type() != tx.LegacyTxType: + case header.Number() < c.forkConfig.GALACTICA && tr.Type() != tx.TypeLegacy: return consensusError(tx.ErrTxTypeNotSupported.Error()) } diff --git a/consensus/validator_test.go b/consensus/validator_test.go index 1d743eab8..ccb2ad1d9 100644 --- a/consensus/validator_test.go +++ b/consensus/validator_test.go @@ -38,7 +38,7 @@ func TestValidateBlockBody(t *testing.T) { { name: "supported legacy tx type before galactica fork", getBlock: func() *block.Block { - tr, err := tx.NewTxBuilder(tx.LegacyTxType).ChainTag(repo.ChainTag()).Expiration(10).Build() + tr, err := tx.NewTxBuilder(tx.TypeLegacy).ChainTag(repo.ChainTag()).Expiration(10).Build() assert.NoError(t, err) tr = tx.MustSign(tr, genesis.DevAccounts()[0].PrivateKey) return new(block.Builder).Transaction(tr).Build() @@ -49,7 +49,7 @@ func TestValidateBlockBody(t *testing.T) { { name: "unsupported tx type before galactica fork", getBlock: func() *block.Block { - tr, err := tx.NewTxBuilder(tx.DynamicFeeTxType).ChainTag(repo.ChainTag()).Expiration(10).Build() + tr, err := tx.NewTxBuilder(tx.TypeDynamicFee).ChainTag(repo.ChainTag()).Expiration(10).Build() assert.NoError(t, err) tr = tx.MustSign(tr, genesis.DevAccounts()[0].PrivateKey) return new(block.Builder).Transaction(tr).Build() @@ -60,7 +60,7 @@ func TestValidateBlockBody(t *testing.T) { { name: "supported legacy tx type after galactica fork", getBlock: func() *block.Block { - tr, err := tx.NewTxBuilder(tx.LegacyTxType).ChainTag(repo.ChainTag()).Expiration(10).Build() + tr, err := tx.NewTxBuilder(tx.TypeLegacy).ChainTag(repo.ChainTag()).Expiration(10).Build() assert.NoError(t, err) tr = tx.MustSign(tr, genesis.DevAccounts()[0].PrivateKey) return new(block.Builder).Transaction(tr).Build() @@ -71,7 +71,7 @@ func TestValidateBlockBody(t *testing.T) { { name: "supported tx type after galactica fork", getBlock: func() *block.Block { - tr, err := tx.NewTxBuilder(tx.DynamicFeeTxType).ChainTag(repo.ChainTag()).Expiration(10).Build() + tr, err := tx.NewTxBuilder(tx.TypeDynamicFee).ChainTag(repo.ChainTag()).Expiration(10).Build() assert.NoError(t, err) tr = tx.MustSign(tr, genesis.DevAccounts()[0].PrivateKey) return new(block.Builder).Transaction(tr).Build() diff --git a/logdb/logdb_bench_test.go b/logdb/logdb_bench_test.go index c406c6d9c..269b127cd 100644 --- a/logdb/logdb_bench_test.go +++ b/logdb/logdb_bench_test.go @@ -47,7 +47,7 @@ func BenchmarkFakeDB_NewestBlockID(t *testing.B) { b := new(block.Builder). ParentID(new(block.Builder).Build().Header().ID()). - Transaction(newTx(tx.LegacyTxType)). + Transaction(newTx(tx.TypeLegacy)). Build() receipts := tx.Receipts{newReceipt()} @@ -112,7 +112,7 @@ func BenchmarkFakeDB_WriteBlocks(t *testing.B) { for i := 0; i < writeCount; i++ { blk = new(block.Builder). ParentID(blk.Header().ID()). - Transaction(newTx(tx.LegacyTxType)). + Transaction(newTx(tx.TypeLegacy)). Build() receipts := tx.Receipts{newReceipt(), newReceipt()} require.NoError(t, w.Write(blk, receipts)) @@ -126,7 +126,7 @@ func BenchmarkFakeDB_WriteBlocks(t *testing.B) { for i := 0; i < writeCount; i++ { blk = new(block.Builder). ParentID(blk.Header().ID()). - Transaction(newTx(tx.LegacyTxType)). + Transaction(newTx(tx.TypeLegacy)). Build() receipts := tx.Receipts{newReceipt(), newReceipt()} require.NoError(t, w.Write(blk, receipts)) diff --git a/logdb/logdb_test.go b/logdb/logdb_test.go index 6d11c27b9..3dc3ea855 100644 --- a/logdb/logdb_test.go +++ b/logdb/logdb_test.go @@ -19,7 +19,7 @@ import ( "github.com/vechain/thor/v2/tx" ) -func newTx(txType int) *tx.Transaction { +func newTx(txType tx.TxType) *tx.Transaction { trx := tx.NewTxBuilder(txType).MustBuild() pk, _ := crypto.GenerateKey() @@ -40,18 +40,20 @@ func randBytes32() (b thor.Bytes32) { func newReceipt() *tx.Receipt { return &tx.Receipt{ - Outputs: []*tx.Output{ - { - Events: tx.Events{{ - Address: randAddress(), - Topics: []thor.Bytes32{randBytes32()}, - Data: randBytes32().Bytes(), - }}, - Transfers: tx.Transfers{{ - Sender: randAddress(), - Recipient: randAddress(), - Amount: new(big.Int).SetBytes(randAddress().Bytes()), - }}, + ReceiptBody: tx.ReceiptBody{ + Outputs: []*tx.Output{ + { + Events: tx.Events{{ + Address: randAddress(), + Topics: []thor.Bytes32{randBytes32()}, + Data: randBytes32().Bytes(), + }}, + Transfers: tx.Transfers{{ + Sender: randAddress(), + Recipient: randAddress(), + Amount: new(big.Int).SetBytes(randAddress().Bytes()), + }}, + }, }, }, } @@ -59,13 +61,15 @@ func newReceipt() *tx.Receipt { func newEventOnlyReceipt() *tx.Receipt { return &tx.Receipt{ - Outputs: []*tx.Output{ - { - Events: tx.Events{{ - Address: randAddress(), - Topics: []thor.Bytes32{randBytes32()}, - Data: randBytes32().Bytes(), - }}, + ReceiptBody: tx.ReceiptBody{ + Outputs: []*tx.Output{ + { + Events: tx.Events{{ + Address: randAddress(), + Topics: []thor.Bytes32{randBytes32()}, + Data: randBytes32().Bytes(), + }}, + }, }, }, } @@ -73,13 +77,15 @@ func newEventOnlyReceipt() *tx.Receipt { func newTransferOnlyReceipt() *tx.Receipt { return &tx.Receipt{ - Outputs: []*tx.Output{ - { - Transfers: tx.Transfers{{ - Sender: randAddress(), - Recipient: randAddress(), - Amount: new(big.Int).SetBytes(randAddress().Bytes()), - }}, + ReceiptBody: tx.ReceiptBody{ + Outputs: []*tx.Output{ + { + Transfers: tx.Transfers{{ + Sender: randAddress(), + Recipient: randAddress(), + Amount: new(big.Int).SetBytes(randAddress().Bytes()), + }}, + }, }, }, } @@ -127,7 +133,7 @@ func TestErrTxTypeNotSupported(t *testing.T) { t.Errorf("Expected to panic") } }() - nonExistingTxType := 100 + nonExistingTxType := tx.TxType(100) newTx(nonExistingTxType) } @@ -146,8 +152,8 @@ func TestEvents(t *testing.T) { for i := 0; i < 100; i++ { b = new(block.Builder). ParentID(b.Header().ID()). - Transaction(newTx(tx.LegacyTxType)). - Transaction(newTx(tx.DynamicFeeTxType)). + Transaction(newTx(tx.TypeLegacy)). + Transaction(newTx(tx.TypeDynamicFee)). Build() receipts := tx.Receipts{newReceipt(), newReceipt()} @@ -270,8 +276,8 @@ func TestLogDB_NewestBlockID(t *testing.T) { b = new(block.Builder). ParentID(b.Header().ID()). - Transaction(newTx(tx.LegacyTxType)). - Transaction(newTx(tx.DynamicFeeTxType)). + Transaction(newTx(tx.TypeLegacy)). + Transaction(newTx(tx.TypeDynamicFee)). Build() receipts := tx.Receipts{newReceipt()} @@ -314,7 +320,7 @@ func TestLogDB_NewestBlockID(t *testing.T) { func() (thor.Bytes32, error) { b = new(block.Builder). ParentID(b.Header().ID()). - Transaction(newTx(tx.LegacyTxType)). + Transaction(newTx(tx.TypeLegacy)). Build() receipts := tx.Receipts{newReceipt()} @@ -333,7 +339,7 @@ func TestLogDB_NewestBlockID(t *testing.T) { func() (thor.Bytes32, error) { b = new(block.Builder). ParentID(b.Header().ID()). - Transaction(newTx(tx.LegacyTxType)). + Transaction(newTx(tx.TypeLegacy)). Build() receipts := tx.Receipts{newEventOnlyReceipt()} @@ -352,7 +358,7 @@ func TestLogDB_NewestBlockID(t *testing.T) { func() (thor.Bytes32, error) { b = new(block.Builder). ParentID(b.Header().ID()). - Transaction(newTx(tx.LegacyTxType)). + Transaction(newTx(tx.TypeLegacy)). Build() receipts := tx.Receipts{newTransferOnlyReceipt()} @@ -394,8 +400,8 @@ func TestLogDB_HasBlockID(t *testing.T) { b := new(block.Builder). ParentID(b0.Header().ID()). - Transaction(newTx(tx.LegacyTxType)). - Transaction(newTx(tx.DynamicFeeTxType)). + Transaction(newTx(tx.TypeLegacy)). + Transaction(newTx(tx.TypeDynamicFee)). Build() b1 := b.Header().ID() receipts := tx.Receipts{newReceipt()} @@ -412,7 +418,7 @@ func TestLogDB_HasBlockID(t *testing.T) { b = new(block.Builder). ParentID(b2). - Transaction(newTx(tx.LegacyTxType)). + Transaction(newTx(tx.TypeLegacy)). Build() b3 := b.Header().ID() receipts = tx.Receipts{newEventOnlyReceipt()} diff --git a/packer/flow.go b/packer/flow.go index b1414b252..f5b0c9265 100644 --- a/packer/flow.go +++ b/packer/flow.go @@ -118,7 +118,7 @@ func (f *Flow) Adopt(t *tx.Transaction) error { } if f.Number() < f.packer.forkConfig.GALACTICA { - if t.Type() != tx.LegacyTxType { + if t.Type() != tx.TypeLegacy { return badTxError{"invalid tx type"} } } else { diff --git a/packer/flow_test.go b/packer/flow_test.go index 2385c89f6..5ba421f32 100644 --- a/packer/flow_test.go +++ b/packer/flow_test.go @@ -27,7 +27,7 @@ import ( "github.com/vechain/thor/v2/tx" ) -func createTx(txType int, chainTag byte, gasPriceCoef uint8, expiration uint32, gas uint64, nonce uint64, dependsOn *thor.Bytes32, clause *tx.Clause, br tx.BlockRef) *tx.Transaction { +func createTx(txType tx.TxType, chainTag byte, gasPriceCoef uint8, expiration uint32, gas uint64, nonce uint64, dependsOn *thor.Bytes32, clause *tx.Clause, br tx.BlockRef) *tx.Transaction { builder := tx.NewTxBuilder(txType). ChainTag(chainTag). GasPriceCoef(gasPriceCoef). @@ -73,12 +73,12 @@ func TestAdopt(t *testing.T) { t.Fatal("Error scheduling:", err) } - tx1 := createTx(tx.LegacyTxType, chainTag, 1, 10, 21000, 1, nil, clause, tx.NewBlockRef(0)) + tx1 := createTx(tx.TypeLegacy, chainTag, 1, 10, 21000, 1, nil, clause, tx.NewBlockRef(0)) if err := flow.Adopt(tx1); err != nil { t.Fatal("Error adopting tx1:", err) } - tx2 := createTx(tx.LegacyTxType, chainTag, 1, 10, 21000, 2, (*thor.Bytes32)(tx1.ID().Bytes()), clause, tx.NewBlockRef(0)) + tx2 := createTx(tx.TypeLegacy, chainTag, 1, 10, 21000, 2, (*thor.Bytes32)(tx1.ID().Bytes()), clause, tx.NewBlockRef(0)) if err := flow.Adopt(tx2); err != nil { t.Fatal("Error adopting tx2:", err) } @@ -90,7 +90,7 @@ func TestAdopt(t *testing.T) { } // Test dependency that does not exist - tx3 := createTx(tx.LegacyTxType, chainTag, 1, 10, 21000, 2, (*thor.Bytes32)((thor.Bytes32{0x1}).Bytes()), clause, tx.NewBlockRef(0)) + tx3 := createTx(tx.TypeLegacy, chainTag, 1, 10, 21000, 2, (*thor.Bytes32)((thor.Bytes32{0x1}).Bytes()), clause, tx.NewBlockRef(0)) expectedErrorMessage = "tx not adoptable now" if err := flow.Adopt(tx3); err.Error() != expectedErrorMessage { t.Fatalf("Expected error message: '%s', but got: '%s'", expectedErrorMessage, err.Error()) @@ -129,12 +129,12 @@ func TestAdoptTypedTxs(t *testing.T) { t.Fatal("Error scheduling:", err) } - tx1 := createTx(tx.LegacyTxType, chainTag, 1, 10, 21000, 1, nil, clause, tx.NewBlockRef(0)) + tx1 := createTx(tx.TypeLegacy, chainTag, 1, 10, 21000, 1, nil, clause, tx.NewBlockRef(0)) if err := flow.Adopt(tx1); err != nil { t.Fatal("Error adopting tx1:", err) } - tx2 := createTx(tx.DynamicFeeTxType, chainTag, 1, 10, 21000, 2, (*thor.Bytes32)(tx1.ID().Bytes()), clause, tx.NewBlockRef(0)) + tx2 := createTx(tx.TypeDynamicFee, chainTag, 1, 10, 21000, 2, (*thor.Bytes32)(tx1.ID().Bytes()), clause, tx.NewBlockRef(0)) if err := flow.Adopt(tx2); err != nil { t.Fatal("Error adopting tx2:", err) } @@ -146,7 +146,7 @@ func TestAdoptTypedTxs(t *testing.T) { } // Test dependency that does not exist - tx3 := createTx(tx.DynamicFeeTxType, chainTag, 1, 10, 21000, 2, (*thor.Bytes32)((thor.Bytes32{0x1}).Bytes()), clause, tx.NewBlockRef(0)) + tx3 := createTx(tx.TypeDynamicFee, chainTag, 1, 10, 21000, 2, (*thor.Bytes32)((thor.Bytes32{0x1}).Bytes()), clause, tx.NewBlockRef(0)) expectedErrorMessage = "tx not adoptable now" if err := flow.Adopt(tx3); err.Error() != expectedErrorMessage { t.Fatalf("Expected error message: '%s', but got: '%s'", expectedErrorMessage, err.Error()) @@ -226,7 +226,7 @@ func TestPackAfterGalacticaFork(t *testing.T) { assert.Equal(t, big.NewInt(thor.InitialBaseFee), block.Header().BaseFee()) // Adopt a tx which has not enough max fee to cover for base fee - badTx := tx.NewTxBuilder(tx.DynamicFeeTxType).ChainTag(repo.ChainTag()).Gas(21000).MaxFeePerGas(big.NewInt(thor.InitialBaseFee - 1)).MaxPriorityFeePerGas(common.Big1).Expiration(100).MustBuild() + badTx := tx.NewTxBuilder(tx.TypeDynamicFee).ChainTag(repo.ChainTag()).Gas(21000).MaxFeePerGas(big.NewInt(thor.InitialBaseFee - 1)).MaxPriorityFeePerGas(common.Big1).Expiration(100).MustBuild() badTx = tx.MustSign(badTx, genesis.DevAccounts()[0].PrivateKey) expectedErrorMessage := fmt.Sprintf("bad tx: %s", runtime.ErrMaxFeePerGasTooLow.Error()) if err := flow.Adopt(badTx); err.Error() != expectedErrorMessage { @@ -268,21 +268,21 @@ func TestAdoptErr(t *testing.T) { flow, _ := pkr.Schedule(sum, uint64(time.Now().Unix())) // Test chain tag mismatch - tx1 := createTx(tx.LegacyTxType, byte(0xFF), 1, 10, 21000, 1, nil, clause, tx.NewBlockRef(0)) + tx1 := createTx(tx.TypeLegacy, byte(0xFF), 1, 10, 21000, 1, nil, clause, tx.NewBlockRef(0)) expectedErrorMessage := "bad tx: chain tag mismatch" if err := flow.Adopt(tx1); err.Error() != expectedErrorMessage { t.Fatalf("Expected error message: '%s', but got: '%s'", expectedErrorMessage, err.Error()) } // Test wrong block reference - tx2 := createTx(tx.LegacyTxType, repo.ChainTag(), 1, 10, 21000, 1, nil, clause, tx.NewBlockRef(1000)) + tx2 := createTx(tx.TypeLegacy, repo.ChainTag(), 1, 10, 21000, 1, nil, clause, tx.NewBlockRef(1000)) expectedErrorMessage = "tx not adoptable now" if err := flow.Adopt(tx2); err.Error() != expectedErrorMessage { t.Fatalf("Expected error message: '%s', but got: '%s'", expectedErrorMessage, err.Error()) } // Test exceeded gas limit - tx3 := createTx(tx.LegacyTxType, repo.ChainTag(), 1, 0, 1, 1, nil, clause, tx.NewBlockRef(1)) + tx3 := createTx(tx.TypeLegacy, repo.ChainTag(), 1, 0, 1, 1, nil, clause, tx.NewBlockRef(1)) expectedErrorMessage = "gas limit reached" if err := flow.Adopt(tx3); err.Error() != expectedErrorMessage { t.Fatalf("Expected error message: '%s', but got: '%s'", expectedErrorMessage, err.Error()) @@ -295,7 +295,7 @@ func TestAdoptErrorAfterGalactica(t *testing.T) { assert.NoError(t, err) // Try to adopt a dyn fee tx before galactica fork activates - FAILS - tr := tx.NewTxBuilder(tx.DynamicFeeTxType).ChainTag(chain.Repo().ChainTag()).Gas(21000).Expiration(100).MustBuild() + tr := tx.NewTxBuilder(tx.TypeDynamicFee).ChainTag(chain.Repo().ChainTag()).Gas(21000).Expiration(100).MustBuild() tr = tx.MustSign(tr, genesis.DevAccounts()[0].PrivateKey) err = chain.MintBlock(genesis.DevAccounts()[0], tr) @@ -303,13 +303,13 @@ func TestAdoptErrorAfterGalactica(t *testing.T) { assert.Equal(t, expectedErrMsg, err.Error()) // Try to adopt a legacy tx - SUCCESS - tr = tx.NewTxBuilder(tx.LegacyTxType).ChainTag(chain.Repo().ChainTag()).Gas(21000).Expiration(100).MustBuild() + tr = tx.NewTxBuilder(tx.TypeLegacy).ChainTag(chain.Repo().ChainTag()).Gas(21000).Expiration(100).MustBuild() tr = tx.MustSign(tr, genesis.DevAccounts()[0].PrivateKey) err = chain.MintBlock(genesis.DevAccounts()[0], tr) assert.NoError(t, err) // Try to adopt a dyn fee tx after galactica fork activates - SUCCESS - tr = tx.NewTxBuilder(tx.DynamicFeeTxType).ChainTag(chain.Repo().ChainTag()).MaxFeePerGas(big.NewInt(thor.InitialBaseFee)).Gas(21000).Expiration(100).MustBuild() + tr = tx.NewTxBuilder(tx.TypeDynamicFee).ChainTag(chain.Repo().ChainTag()).MaxFeePerGas(big.NewInt(thor.InitialBaseFee)).Gas(21000).Expiration(100).MustBuild() tr = tx.MustSign(tr, genesis.DevAccounts()[0].PrivateKey) err = chain.MintBlock(genesis.DevAccounts()[0], tr) assert.NoError(t, err) @@ -320,14 +320,14 @@ func TestAdoptErrorAfterGalactica(t *testing.T) { expectedBaseFee := fork.CalcBaseFee(&forks, best.Header()) notEnoughBaseFee := new(big.Int).Sub(expectedBaseFee, common.Big1) - tr = tx.NewTxBuilder(tx.DynamicFeeTxType).ChainTag(chain.Repo().ChainTag()).Nonce(2).MaxFeePerGas(notEnoughBaseFee).Gas(21000).Expiration(100).MustBuild() + tr = tx.NewTxBuilder(tx.TypeDynamicFee).ChainTag(chain.Repo().ChainTag()).Nonce(2).MaxFeePerGas(notEnoughBaseFee).Gas(21000).Expiration(100).MustBuild() tr = tx.MustSign(tr, genesis.DevAccounts()[0].PrivateKey) err = chain.MintBlock(genesis.DevAccounts()[0], tr) expectedErrMsg = "unable to adopt tx into block: bad tx: max fee per gas is less than block base fee" assert.Equal(t, expectedErrMsg, err.Error()) // Try to adopt a dyn fee with just the right amount of max fee per gas - SUCCESS - tr = tx.NewTxBuilder(tx.DynamicFeeTxType).ChainTag(chain.Repo().ChainTag()).Nonce(2).MaxFeePerGas(expectedBaseFee).Gas(21000).Expiration(100).MustBuild() + tr = tx.NewTxBuilder(tx.TypeDynamicFee).ChainTag(chain.Repo().ChainTag()).Nonce(2).MaxFeePerGas(expectedBaseFee).Gas(21000).Expiration(100).MustBuild() tr = tx.MustSign(tr, genesis.DevAccounts()[0].PrivateKey) err = chain.MintBlock(genesis.DevAccounts()[0], tr) assert.NoError(t, err) @@ -338,7 +338,7 @@ func TestAdoptErrorAfterGalactica(t *testing.T) { expectedBaseFee = fork.CalcBaseFee(&forks, best.Header()) maxPriorityFee := big.NewInt(10_000) maxFee := new(big.Int).Add(expectedBaseFee, maxPriorityFee) - tr = tx.NewTxBuilder(tx.DynamicFeeTxType).ChainTag(chain.Repo().ChainTag()).Nonce(3).MaxFeePerGas(maxFee).MaxPriorityFeePerGas(maxPriorityFee).Gas(21000).Expiration(100).MustBuild() + tr = tx.NewTxBuilder(tx.TypeDynamicFee).ChainTag(chain.Repo().ChainTag()).Nonce(3).MaxFeePerGas(maxFee).MaxPriorityFeePerGas(maxPriorityFee).Gas(21000).Expiration(100).MustBuild() tr = tx.MustSign(tr, genesis.DevAccounts()[0].PrivateKey) err = chain.MintBlock(genesis.DevAccounts()[0], tr) assert.NoError(t, err) @@ -348,7 +348,7 @@ func TestAdoptAfterGalacticaLowerBaseFeeThreshold(t *testing.T) { chain, err := testchain.NewWithFork(thor.ForkConfig{GALACTICA: 1}) assert.NoError(t, err) - tr := tx.NewTxBuilder(tx.LegacyTxType).ChainTag(chain.Repo().ChainTag()).Gas(21000).Expiration(100).MustBuild() + tr := tx.NewTxBuilder(tx.TypeLegacy).ChainTag(chain.Repo().ChainTag()).Gas(21000).Expiration(100).MustBuild() tr = tx.MustSign(tr, genesis.DevAccounts()[0].PrivateKey) err = chain.MintBlock(genesis.DevAccounts()[0], tr) assert.NoError(t, err) @@ -357,7 +357,7 @@ func TestAdoptAfterGalacticaLowerBaseFeeThreshold(t *testing.T) { best, err := chain.BestBlock() assert.NoError(t, err) expectedBaseFee := fork.CalcBaseFee(&thor.ForkConfig{}, best.Header()) - tr = tx.NewTxBuilder(tx.DynamicFeeTxType).ChainTag(chain.Repo().ChainTag()).Nonce(uint64(i + 2)).MaxFeePerGas(expectedBaseFee).Gas(21000).Expiration(1000000).MustBuild() + tr = tx.NewTxBuilder(tx.TypeDynamicFee).ChainTag(chain.Repo().ChainTag()).Nonce(uint64(i + 2)).MaxFeePerGas(expectedBaseFee).Gas(21000).Expiration(1000000).MustBuild() tr = tx.MustSign(tr, genesis.DevAccounts()[0].PrivateKey) err = chain.MintBlock(genesis.DevAccounts()[0], tr) assert.NoError(t, err) diff --git a/packer/packer_test.go b/packer/packer_test.go index c8a072d46..c15830107 100644 --- a/packer/packer_test.go +++ b/packer/packer_test.go @@ -50,7 +50,7 @@ func (ti *txIterator) Next() *tx.Transaction { data, _ := method.EncodeInput(a1.Address, big.NewInt(1)) - trx := tx.NewTxBuilder(tx.LegacyTxType). + trx := tx.NewTxBuilder(tx.TypeLegacy). ChainTag(ti.chainTag). Clause(tx.NewClause(&builtin.Energy.Address).WithData(data)). Gas(300000).GasPriceCoef(0).Nonce(nonce).Expiration(math.MaxUint32).MustBuild() @@ -208,7 +208,7 @@ func TestBlocklist(t *testing.T) { t.Fatal(err) } - tx0 := tx.NewTxBuilder(tx.LegacyTxType). + tx0 := tx.NewTxBuilder(tx.TypeLegacy). ChainTag(repo.ChainTag()). Clause(tx.NewClause(&a1.Address)). Gas(300000).GasPriceCoef(0).Nonce(0).Expiration(math.MaxUint32).MustBuild() diff --git a/runtime/resolved_tx_test.go b/runtime/resolved_tx_test.go index d4ec524be..d31a55c96 100644 --- a/runtime/resolved_tx_test.go +++ b/runtime/resolved_tx_test.go @@ -82,12 +82,12 @@ func (tr *testResolvedTransaction) TestResolveTransaction() { }{ { getBuilder: func() *tx.Builder { - return txBuilder(tr.repo.ChainTag(), tx.LegacyTxType) + return txBuilder(tr.repo.ChainTag(), tx.TypeLegacy) }, }, { getBuilder: func() *tx.Builder { - return txBuilder(tr.repo.ChainTag(), tx.DynamicFeeTxType) + return txBuilder(tr.repo.ChainTag(), tx.TypeDynamicFee) }, }, } @@ -124,12 +124,12 @@ func (tr *testResolvedTransaction) TestCommonTo() { }{ { getBuilder: func() *tx.Builder { - return txBuilder(tr.repo.ChainTag(), tx.LegacyTxType) + return txBuilder(tr.repo.ChainTag(), tx.TypeLegacy) }, }, { getBuilder: func() *tx.Builder { - return txBuilder(tr.repo.ChainTag(), tx.DynamicFeeTxType) + return txBuilder(tr.repo.ChainTag(), tx.TypeDynamicFee) }, }, } @@ -169,7 +169,7 @@ func (tr *testResolvedTransaction) TestBuyGas() { state := tr.currentState() txBuild := func() *tx.Builder { - return txBuilder(tr.repo.ChainTag(), tx.LegacyTxType) + return txBuilder(tr.repo.ChainTag(), tx.TypeLegacy) } targetTime := tr.repo.BestBlockSummary().Header.Timestamp() + thor.BlockInterval @@ -211,7 +211,7 @@ func clause() *tx.Clause { return tx.NewClause(&address).WithData(nil) } -func txBuilder(tag byte, txType int) *tx.Builder { +func txBuilder(tag byte, txType tx.TxType) *tx.Builder { return tx.NewTxBuilder(txType). GasPriceCoef(1). Gas(1000000). diff --git a/runtime/runtime.go b/runtime/runtime.go index 8246857b7..d1f75e9a6 100644 --- a/runtime/runtime.go +++ b/runtime/runtime.go @@ -501,10 +501,13 @@ func (rt *Runtime) PrepareTransaction(tx *tx.Transaction) (*TransactionExecutor, finalized = true receipt := &Tx.Receipt{ - Reverted: reverted, - Outputs: txOutputs, - GasUsed: tx.Gas() - leftOverGas, - GasPayer: payer, + Type: tx.Type(), + ReceiptBody: Tx.ReceiptBody{ + Reverted: reverted, + Outputs: txOutputs, + GasUsed: tx.Gas() - leftOverGas, + GasPayer: payer, + }, } receipt.Paid = new(big.Int).Mul(new(big.Int).SetUint64(receipt.GasUsed), gasPrice) @@ -523,11 +526,8 @@ func (rt *Runtime) PrepareTransaction(tx *tx.Transaction) (*TransactionExecutor, return nil, err } rewardGasPrice := fork.GalacticaPriorityPrice(tx, baseGasPrice, provedWork, &fork.GalacticaItems{IsActive: galactica, BaseFee: rt.ctx.BaseFee}) + reward := fork.CalculateReward(receipt.GasUsed, rewardGasPrice, rewardRatio, galactica) - reward := new(big.Int).SetUint64(receipt.GasUsed) - reward.Mul(reward, rewardGasPrice) - reward.Mul(reward, rewardRatio) - reward.Div(reward, big.NewInt(1e18)) if err := builtin.Energy.Native(rt.state, rt.ctx.Time).Add(rt.ctx.Beneficiary, reward); err != nil { return nil, err } diff --git a/runtime/runtime_test.go b/runtime/runtime_test.go index 470ee03e0..b74b39929 100644 --- a/runtime/runtime_test.go +++ b/runtime/runtime_test.go @@ -656,7 +656,7 @@ func TestCall(t *testing.T) { assert.Nil(t, err) } -func getMockTx(repo *chain.Repository, txType int, t *testing.T) *tx.Transaction { +func getMockTx(repo *chain.Repository, txType tx.TxType, t *testing.T) *tx.Transaction { var blockRef = tx.NewBlockRef(0) var chainTag = repo.ChainTag() var expiration = uint32(10) @@ -680,7 +680,7 @@ func getMockTx(repo *chain.Repository, txType int, t *testing.T) *tx.Transaction return tx } -func GetMockFailedTx(txType int) *tx.Transaction { +func GetMockFailedTx(txType tx.TxType) *tx.Transaction { to, _ := thor.ParseAddress("0x7567d83b7b8d80addcb281a71d54fc7b3364ffed") return tx.NewTxBuilder(txType).ChainTag(1). BlockRef(tx.BlockRef{0, 0, 0, 0, 0xaa, 0xbb, 0xcc, 0xdd}). @@ -734,7 +734,7 @@ func TestTransactionsWithNotEnoughGasFee(t *testing.T) { repo.AddBlock(b, tx.Receipts{}, 0, true) rt := runtime.New(chain, state, &xenv.BlockContext{BaseFee: big.NewInt(baseFee)}, thor.ForkConfig{}) - illegalTx, err := tx.NewTxBuilder(tx.DynamicFeeTxType). + illegalTx, err := tx.NewTxBuilder(tx.TypeDynamicFee). ChainTag(repo.ChainTag()). MaxFeePerGas(big.NewInt(baseFee / 10000)). MaxPriorityFeePerGas(big.NewInt(baseFee / 100)). @@ -769,7 +769,7 @@ func TestExecuteTransactionFailure(t *testing.T) { originEnergy.SetString("9000000000000000000000000000000000000", 10) state.SetEnergy(origin.Address, originEnergy, 0) - for _, txType := range []int{tx.LegacyTxType, tx.DynamicFeeTxType} { + for _, txType := range []tx.TxType{tx.TypeLegacy, tx.TypeDynamicFee} { tx := GetMockFailedTx(txType) rt := runtime.New(repo.NewChain(b0.Header().ID()), state, &xenv.BlockContext{}, thor.NoFork) @@ -791,7 +791,7 @@ func TestExecuteTransaction(t *testing.T) { maxFee := int64(thor.InitialBaseFee * 1000) maxPriorityFee := maxFee / 100 gas := uint64(42000) - dynTx, err := tx.NewTxBuilder(tx.DynamicFeeTxType). + dynTx, err := tx.NewTxBuilder(tx.TypeDynamicFee). ChainTag(repo.ChainTag()). Gas(gas). MaxFeePerGas(big.NewInt(maxFee)). @@ -800,7 +800,7 @@ func TestExecuteTransaction(t *testing.T) { assert.Nil(t, err) dynTx = tx.MustSign(dynTx, genesis.DevAccounts()[0].PrivateKey) - legacyTx, err := tx.NewTxBuilder(tx.LegacyTxType). + legacyTx, err := tx.NewTxBuilder(tx.TypeLegacy). ChainTag(repo.ChainTag()). Gas(gas). GasPriceCoef(128). @@ -909,8 +909,8 @@ func TestExecuteTransaction(t *testing.T) { t.Run("Test mixed txs", func(t *testing.T) { txs := []*tx.Transaction{ - getMockTx(repo, tx.LegacyTxType, t), - getMockTx(repo, tx.DynamicFeeTxType, t), + getMockTx(repo, tx.TypeLegacy, t), + getMockTx(repo, tx.TypeDynamicFee, t), } for _, trx := range txs { @@ -940,7 +940,7 @@ func TestNoRewards(t *testing.T) { { name: "Dyn tx with maxFeePerGas = 0", getTx: func() *tx.Transaction { - dt, err := tx.NewTxBuilder(tx.DynamicFeeTxType). + dt, err := tx.NewTxBuilder(tx.TypeDynamicFee). ChainTag(repo.ChainTag()). Gas(21000). MaxFeePerGas(big.NewInt(thor.InitialBaseFee * 1000)). @@ -953,7 +953,7 @@ func TestNoRewards(t *testing.T) { { name: "Dyn tx with maxFee equals to baseFee", getTx: func() *tx.Transaction { - dt, err := tx.NewTxBuilder(tx.DynamicFeeTxType). + dt, err := tx.NewTxBuilder(tx.TypeDynamicFee). ChainTag(repo.ChainTag()). Gas(21000). MaxFeePerGas(big.NewInt(thor.InitialBaseFee)). diff --git a/thorclient/api_test.go b/thorclient/api_test.go index 12cc16f99..620b82919 100644 --- a/thorclient/api_test.go +++ b/thorclient/api_test.go @@ -93,7 +93,7 @@ func initAPIServer(t *testing.T) (*testchain.Chain, *httptest.Server) { func mintTransactions(t *testing.T, thorChain *testchain.Chain) { toAddr := datagen.RandAddress() - noClausesTx := tx.NewTxBuilder(tx.LegacyTxType). + noClausesTx := tx.NewTxBuilder(tx.TypeLegacy). ChainTag(thorChain.Repo().ChainTag()). Expiration(10). Gas(21000). @@ -106,7 +106,7 @@ func mintTransactions(t *testing.T, thorChain *testchain.Chain) { cla := tx.NewClause(&toAddr).WithValue(big.NewInt(10000)) cla2 := tx.NewClause(&toAddr).WithValue(big.NewInt(10000)) - transaction := tx.NewTxBuilder(tx.LegacyTxType). + transaction := tx.NewTxBuilder(tx.TypeLegacy). ChainTag(thorChain.Repo().ChainTag()). GasPriceCoef(1). Expiration(10). @@ -225,7 +225,7 @@ func testTransactionsEndpoint(t *testing.T, thorChain *testchain.Chain, ts *http t.Run("SendTransaction", func(t *testing.T) { toAddr := thor.MustParseAddress("0x0123456789abcdef0123456789abcdef01234567") clause := tx.NewClause(&toAddr).WithValue(big.NewInt(10000)) - trx := tx.NewTxBuilder(tx.LegacyTxType). + trx := tx.NewTxBuilder(tx.TypeLegacy). ChainTag(thorChain.Repo().ChainTag()). Expiration(10). Gas(21000). @@ -238,7 +238,7 @@ func testTransactionsEndpoint(t *testing.T, thorChain *testchain.Chain, ts *http require.NotNil(t, sendResult) require.Equal(t, trx.ID().String(), sendResult.ID.String()) // Ensure transaction was successful - trx = tx.NewTxBuilder(tx.LegacyTxType). + trx = tx.NewTxBuilder(tx.TypeLegacy). ChainTag(thorChain.Repo().ChainTag()). Expiration(10). Gas(21000). diff --git a/tx/builder.go b/tx/builder.go index cd261d4b9..ff13f9904 100644 --- a/tx/builder.go +++ b/tx/builder.go @@ -14,7 +14,7 @@ import ( // Builder to make it easy to build transaction. type Builder struct { - txType int + txType TxType chainTag byte clauses []*Clause gasPriceCoef uint8 @@ -28,7 +28,7 @@ type Builder struct { reserved reserved } -func NewTxBuilder(txType int) *Builder { +func NewTxBuilder(txType TxType) *Builder { return &Builder{txType: txType} } @@ -114,7 +114,7 @@ func (b *Builder) Features(feat Features) *Builder { func (b *Builder) Build() (*Transaction, error) { var tx *Transaction switch b.txType { - case LegacyTxType: + case TypeLegacy: tx = &Transaction{ body: &LegacyTransaction{ ChainTag: b.chainTag, @@ -128,7 +128,7 @@ func (b *Builder) Build() (*Transaction, error) { Reserved: b.reserved, }, } - case DynamicFeeTxType: + case TypeDynamicFee: tx = &Transaction{ body: &DynamicFeeTransaction{ ChainTag: b.chainTag, diff --git a/tx/builder_test.go b/tx/builder_test.go index 610e1cdc7..13334db96 100644 --- a/tx/builder_test.go +++ b/tx/builder_test.go @@ -14,19 +14,19 @@ import ( ) func TestNewTxBuilder(t *testing.T) { - builder := NewTxBuilder(LegacyTxType) + builder := NewTxBuilder(TypeLegacy) assert.NotNil(t, builder) - assert.Equal(t, LegacyTxType, builder.txType) + assert.Equal(t, TypeLegacy, builder.txType) } func TestBuilder_ChainTag(t *testing.T) { - builder := NewTxBuilder(LegacyTxType) + builder := NewTxBuilder(TypeLegacy) builder.ChainTag(0x4a) assert.Equal(t, byte(0x4a), builder.chainTag) } func TestBuilder_Clause(t *testing.T) { - builder := NewTxBuilder(LegacyTxType) + builder := NewTxBuilder(TypeLegacy) addr := thor.BytesToAddress([]byte("to")) clause := NewClause(&addr) builder.Clause(clause) @@ -35,52 +35,52 @@ func TestBuilder_Clause(t *testing.T) { } func TestBuilder_GasPriceCoef(t *testing.T) { - builder := NewTxBuilder(LegacyTxType) + builder := NewTxBuilder(TypeLegacy) builder.GasPriceCoef(10) assert.Equal(t, uint8(10), builder.gasPriceCoef) } func TestBuilder_MaxFeePerGas(t *testing.T) { - builder := NewTxBuilder(DynamicFeeTxType) + builder := NewTxBuilder(TypeDynamicFee) maxFee := big.NewInt(1000000000) builder.MaxFeePerGas(maxFee) assert.Equal(t, maxFee, builder.maxFeePerGas) } func TestBuilder_MaxPriorityFeePerGas(t *testing.T) { - builder := NewTxBuilder(DynamicFeeTxType) + builder := NewTxBuilder(TypeDynamicFee) maxPriorityFee := big.NewInt(2000000000) builder.MaxPriorityFeePerGas(maxPriorityFee) assert.Equal(t, maxPriorityFee, builder.maxPriorityFeePerGas) } func TestBuilder_Gas(t *testing.T) { - builder := NewTxBuilder(LegacyTxType) + builder := NewTxBuilder(TypeLegacy) builder.Gas(21000) assert.Equal(t, uint64(21000), builder.gas) } func TestBuilder_BlockRef(t *testing.T) { - builder := NewTxBuilder(LegacyTxType) + builder := NewTxBuilder(TypeLegacy) blockRef := BlockRef{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08} builder.BlockRef(blockRef) assert.Equal(t, uint64(0x0102030405060708), builder.blockRef) } func TestBuilder_Expiration(t *testing.T) { - builder := NewTxBuilder(LegacyTxType) + builder := NewTxBuilder(TypeLegacy) builder.Expiration(100) assert.Equal(t, uint32(100), builder.expiration) } func TestBuilder_Nonce(t *testing.T) { - builder := NewTxBuilder(LegacyTxType) + builder := NewTxBuilder(TypeLegacy) builder.Nonce(12345) assert.Equal(t, uint64(12345), builder.nonce) } func TestBuilder_DependsOn(t *testing.T) { - builder := NewTxBuilder(LegacyTxType) + builder := NewTxBuilder(TypeLegacy) txID := thor.Bytes32{0x01, 0x02, 0x03, 0x04} builder.DependsOn(&txID) assert.Equal(t, &txID, builder.dependsOn) @@ -89,14 +89,14 @@ func TestBuilder_DependsOn(t *testing.T) { } func TestBuilder_Features(t *testing.T) { - builder := NewTxBuilder(LegacyTxType) + builder := NewTxBuilder(TypeLegacy) features := Features(0x01) builder.Features(features) assert.Equal(t, features, builder.reserved.Features) } func TestBuilder_Build_Legacy(t *testing.T) { - builder := NewTxBuilder(LegacyTxType). + builder := NewTxBuilder(TypeLegacy). ChainTag(0x4a). Clause(&Clause{}). GasPriceCoef(10). @@ -114,7 +114,7 @@ func TestBuilder_Build_Legacy(t *testing.T) { } func TestBuilder_Build_DynamicFee(t *testing.T) { - builder := NewTxBuilder(DynamicFeeTxType). + builder := NewTxBuilder(TypeDynamicFee). ChainTag(0x4a). Clause(&Clause{}). MaxFeePerGas(big.NewInt(1000000000)). diff --git a/tx/receipt.go b/tx/receipt.go index 45ad952db..b62eeaafe 100644 --- a/tx/receipt.go +++ b/tx/receipt.go @@ -6,6 +6,9 @@ package tx import ( + "bytes" + "errors" + "io" "math/big" "github.com/ethereum/go-ethereum/rlp" @@ -13,8 +16,19 @@ import ( "github.com/vechain/thor/v2/trie" ) +var ( + errEmptyTypedReceipt = errors.New("empty typed receipt bytes") + errShortTypedReceipt = errors.New("typed receipt too short") +) + // Receipt represents the results of a transaction. type Receipt struct { + // transaction type this receipt is associated with + Type byte + ReceiptBody +} + +type ReceiptBody struct { // gas used by this tx GasUsed uint64 // the one who paid for gas @@ -56,9 +70,137 @@ func (rs derivableReceipts) Len() int { return len(rs) } func (rs derivableReceipts) GetRlp(i int) []byte { - data, err := rlp.EncodeToBytes(rs[i]) + data, err := rs[i].MarshalBinary() if err != nil { panic(err) } return data } + +// EncodeRLP implements rlp.Encoder, and flattens the consensus fields of a receipt +// into an RLP stream. If no post state is present, byzantium fork is assumed. +func (r *Receipt) EncodeRLP(w io.Writer) error { + data := &ReceiptBody{ + r.GasUsed, r.GasPayer, r.Paid, r.Reward, r.Reverted, r.Outputs, + } + if r.Type == TypeLegacy { + return rlp.Encode(w, data) + } + + buf := encodeBufferPool.Get().(*bytes.Buffer) + defer encodeBufferPool.Put(buf) + buf.Reset() + buf.WriteByte(r.Type) + if err := rlp.Encode(buf, data); err != nil { + return err + } + return rlp.Encode(w, buf.Bytes()) +} + +// DecodeRLP implements rlp.Decoder, and loads the consensus fields of a receipt +// from an RLP stream. +func (r *Receipt) DecodeRLP(s *rlp.Stream) error { + kind, _, err := s.Kind() + switch { + case err != nil: + return err + case kind == rlp.List: + // It's a legacy receipt. + var dec ReceiptBody + if err := s.Decode(&dec); err != nil { + return err + } + r.Type = TypeLegacy + r.setFromRLP(dec) + case kind == rlp.String: + // It's an EIP-2718 typed tx receipt. + b, err := s.Bytes() + if err != nil { + return err + } + if len(b) == 0 { + return errEmptyTypedReceipt + } + r.Type = b[0] + switch r.Type { + case TypeDynamicFee: + var dec ReceiptBody + if err := rlp.DecodeBytes(b[1:], &dec); err != nil { + return err + } + r.setFromRLP(dec) + default: + return ErrTxTypeNotSupported + } + default: + return rlp.ErrExpectedList + } + + return nil +} + +func (r *Receipt) setFromRLP(dec ReceiptBody) { + r.GasUsed = dec.GasUsed + r.GasPayer = dec.GasPayer + r.Paid = dec.Paid + r.Reward = dec.Reward + r.Reverted = dec.Reverted + r.Outputs = dec.Outputs +} + +// MarshalBinary returns the consensus encoding of the receipt. +func (r *Receipt) MarshalBinary() ([]byte, error) { + if r.Type == TypeLegacy { + return rlp.EncodeToBytes(r) + } + data := &ReceiptBody{ + r.GasUsed, r.GasPayer, r.Paid, r.Reward, r.Reverted, r.Outputs, + } + var buf bytes.Buffer + err := r.encodeTyped(data, &buf) + return buf.Bytes(), err +} + +// UnmarshalBinary decodes the consensus encoding of receipts. +// It supports legacy RLP receipts and EIP-2718 typed receipts. +func (r *Receipt) UnmarshalBinary(b []byte) error { + if len(b) > 0 && b[0] > 0x7f { + // It's a legacy receipt decode the RLP + var data ReceiptBody + err := rlp.DecodeBytes(b, &data) + if err != nil { + return err + } + r.Type = TypeLegacy + r.setFromRLP(data) + return nil + } + // It's an EIP2718 typed transaction envelope. + return r.decodeTyped(b) +} + +// encodeTyped writes the canonical encoding of a typed receipt to w. +func (r *Receipt) encodeTyped(data *ReceiptBody, w *bytes.Buffer) error { + w.WriteByte(r.Type) + return rlp.Encode(w, data) +} + +// decodeTyped decodes a typed receipt from the canonical format. +func (r *Receipt) decodeTyped(b []byte) error { + if len(b) <= 1 { + return errShortTypedReceipt + } + switch b[0] { + case TypeDynamicFee: + var data ReceiptBody + err := rlp.DecodeBytes(b[1:], &data) + if err != nil { + return err + } + r.Type = b[0] + r.setFromRLP(data) + return nil + default: + return ErrTxTypeNotSupported + } +} diff --git a/tx/receipt_test.go b/tx/receipt_test.go index 08948009a..dec7c513d 100644 --- a/tx/receipt_test.go +++ b/tx/receipt_test.go @@ -3,58 +3,129 @@ // Distributed under the GNU Lesser General Public License v3.0 software license, see the accompanying // file LICENSE or -package tx_test +package tx import ( + "bytes" "fmt" "math/big" "testing" + "github.com/ethereum/go-ethereum/rlp" "github.com/stretchr/testify/assert" "github.com/vechain/thor/v2/thor" - "github.com/vechain/thor/v2/tx" ) -func getMockReceipt() tx.Receipt { - receipt := tx.Receipt{ - GasUsed: 1000, - GasPayer: thor.Address{}, - Paid: big.NewInt(100), - Reward: big.NewInt(50), - Reverted: false, - Outputs: []*tx.Output{}, +func getMockReceipt(txType byte) Receipt { + receipt := Receipt{ + Type: txType, + ReceiptBody: ReceiptBody{GasUsed: 1000, + GasPayer: thor.Address{}, + Paid: big.NewInt(100), + Reward: big.NewInt(50), + Reverted: false, + Outputs: []*Output{}, + }, } return receipt } func TestReceipt(t *testing.T) { - var rs tx.Receipts + var rs Receipts fmt.Println(rs.RootHash()) - var txs tx.Transactions + var txs Transactions fmt.Println(txs.RootHash()) } func TestReceiptStructure(t *testing.T) { - receipt := getMockReceipt() - - assert.Equal(t, uint64(1000), receipt.GasUsed) - assert.Equal(t, thor.Address{}, receipt.GasPayer) - assert.Equal(t, big.NewInt(100), receipt.Paid) - assert.Equal(t, big.NewInt(50), receipt.Reward) - assert.Equal(t, false, receipt.Reverted) - assert.Equal(t, []*tx.Output{}, receipt.Outputs) + for _, txType := range []TxType{TypeLegacy, TypeDynamicFee} { + receipt := getMockReceipt(txType) + + // assert.Equal(t, byte(txType), receipt.Type) + assert.Equal(t, uint64(1000), receipt.GasUsed) + assert.Equal(t, thor.Address{}, receipt.GasPayer) + assert.Equal(t, big.NewInt(100), receipt.Paid) + assert.Equal(t, big.NewInt(50), receipt.Reward) + assert.Equal(t, false, receipt.Reverted) + assert.Equal(t, []*Output{}, receipt.Outputs) + } } func TestEmptyRootHash(t *testing.T) { - receipt1 := getMockReceipt() - receipt2 := getMockReceipt() + tests := []struct { + name string + receipt1 Receipt + receipt2 Receipt + }{ + { + name: "LegacyReceipts", + receipt1: getMockReceipt(TypeLegacy), + receipt2: getMockReceipt(TypeLegacy), + }, + { + name: "DynamicFeeReceipts", + receipt1: getMockReceipt(TypeDynamicFee), + receipt2: getMockReceipt(TypeDynamicFee), + }, + { + name: "MixedReceipts", + receipt1: getMockReceipt(TypeLegacy), + receipt2: getMockReceipt(TypeDynamicFee), + }, + } - receipts := tx.Receipts{ - &receipt1, - &receipt2, + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + receipts := Receipts{ + &tt.receipt1, + &tt.receipt2, + } + + rootHash := receipts.RootHash() + assert.NotEmpty(t, rootHash, "Root hash should be empty") + }) } +} - rootHash := receipts.RootHash() - assert.NotEqual(t, thor.Bytes32{}, rootHash, "Root hash should not be empty") +func TestMarshalAndUnmarshalBinary(t *testing.T) { + for _, txType := range []TxType{TypeLegacy, TypeDynamicFee} { + originalReceipt := getMockReceipt(txType) + + data, err := originalReceipt.MarshalBinary() + assert.Nil(t, err) + + var unmarshalledReceipt Receipt + err = unmarshalledReceipt.UnmarshalBinary(data) + assert.Nil(t, err) + + assert.Equal(t, originalReceipt, unmarshalledReceipt) + } +} + +func TestEncodeAndDecodeReceipt(t *testing.T) { + for _, txType := range []TxType{TypeLegacy, TypeDynamicFee} { + originalReceipt := getMockReceipt(txType) + receiptBuf := new(bytes.Buffer) + // Encoding + err := originalReceipt.EncodeRLP(receiptBuf) + assert.Nil(t, err) + + s := rlp.NewStream(receiptBuf, 0) + var decodedReceipt Receipt + // Decoding + err = decodedReceipt.DecodeRLP(s) + assert.Nil(t, err) + + assert.Equal(t, originalReceipt, decodedReceipt) + } +} + +func TestDecodeEmptyTypedReceipt(t *testing.T) { + input := []byte{0x80} + var r Receipt + err := rlp.DecodeBytes(input, &r) + if err != errEmptyTypedReceipt { + t.Fatal("wrong error:", err) + } } diff --git a/tx/signer_test.go b/tx/signer_test.go index e9766b810..938472330 100644 --- a/tx/signer_test.go +++ b/tx/signer_test.go @@ -19,7 +19,7 @@ func TestSign(t *testing.T) { pk, err := crypto.GenerateKey() assert.NoError(t, err) - txTypes := []int{LegacyTxType, DynamicFeeTxType} + txTypes := []TxType{TypeLegacy, TypeDynamicFee} for _, txType := range txTypes { trx := NewTxBuilder(txType).MustBuild() @@ -50,7 +50,7 @@ func TestSignDelegated(t *testing.T) { originPK, err := crypto.GenerateKey() assert.NoError(t, err) - for _, txType := range []int{LegacyTxType, DynamicFeeTxType} { + for _, txType := range []TxType{TypeLegacy, TypeDynamicFee} { // Feature not enabled trx := NewTxBuilder(txType).MustBuild() signedTx, err := SignDelegated(trx, originPK, delegatorPK) diff --git a/tx/transaction.go b/tx/transaction.go index 2d04948b3..e3afa5c05 100644 --- a/tx/transaction.go +++ b/tx/transaction.go @@ -34,10 +34,12 @@ var ( ErrMaxFeeVeryHigh = errors.New("max fee per gas higher than 2^256-1") ) -// Starting from the max value allowed to avoid ambiguity with Ethereum tx type codes. +type TxType = byte + +// Starting from 0x51 to avoid ambiguity with Ethereum tx type codes. const ( - LegacyTxType = 0x00 - DynamicFeeTxType = 0x51 + TypeLegacy = TxType(0x00) + TypeDynamicFee = TxType(0x51) ) // Transaction is an immutable tx type. @@ -145,7 +147,7 @@ func (t *Transaction) Hash() (hash thor.Bytes32) { defer func() { t.cache.hash.Store(hash) }() // Legacy tx don't have type prefix. - if t.Type() == LegacyTxType { + if t.Type() == TypeLegacy { return rlpHash(t) } return prefixedRlpHash(t.Type(), t.body) @@ -297,7 +299,7 @@ func (t *Transaction) encodeTyped(w *bytes.Buffer) error { // For legacy transactions, it returns the RLP encoding. For typed // transactions, it returns the type RLP encoding of the tx. func (t *Transaction) MarshalBinary() ([]byte, error) { - if t.Type() == LegacyTxType { + if t.Type() == TypeLegacy { return rlp.EncodeToBytes(t.body) } var buf bytes.Buffer @@ -329,7 +331,7 @@ func (t *Transaction) UnmarshalBinary(b []byte) error { // EncodeRLP implements rlp.Encoder func (t *Transaction) EncodeRLP(w io.Writer) error { - if t.Type() == LegacyTxType { + if t.Type() == TypeLegacy { return rlp.Encode(w, &t.body) } buf := encodeBufferPool.Get().(*bytes.Buffer) @@ -381,7 +383,7 @@ func (t *Transaction) decodeTyped(b []byte) (TxData, error) { return nil, errEmptyTypedTx } switch b[0] { - case DynamicFeeTxType: + case TypeDynamicFee: var body DynamicFeeTransaction err := rlp.DecodeBytes(b[1:], &body) return &body, err @@ -536,7 +538,7 @@ func (t *Transaction) String() string { `, t.ID(), t.Size(), originStr, t.body.clauses(), t.body.gas(), t.body.chainTag(), br.Number(), br[4:], t.body.expiration(), dependsOn, t.body.nonce(), t.UnprovedWork(), delegatorStr, t.body.signature()) - if t.Type() == LegacyTxType { + if t.Type() == TypeLegacy { return fmt.Sprintf(`%v GasPriceCoef: %v `, s, t.body.gasPriceCoef()) diff --git a/tx/transaction_test.go b/tx/transaction_test.go index a6e8c971c..7f44101fe 100644 --- a/tx/transaction_test.go +++ b/tx/transaction_test.go @@ -17,7 +17,7 @@ import ( "github.com/vechain/thor/v2/tx" ) -func GetMockTx(txType int) *tx.Transaction { +func GetMockTx(txType tx.TxType) *tx.Transaction { to, _ := thor.ParseAddress("0x7567d83b7b8d80addcb281a71d54fc7b3364ffed") return tx.NewTxBuilder(txType).ChainTag(1). BlockRef(tx.BlockRef{0, 0, 0, 0, 0xaa, 0xbb, 0xcc, 0xdd}). @@ -33,7 +33,7 @@ func GetMockTx(txType int) *tx.Transaction { } func TestIsExpired(t *testing.T) { - for _, txType := range []int{tx.LegacyTxType, tx.DynamicFeeTxType} { + for _, txType := range []tx.TxType{tx.TypeLegacy, tx.TypeDynamicFee} { tx := GetMockTx(txType) res := tx.IsExpired(10) assert.Equal(t, res, false) @@ -41,7 +41,7 @@ func TestIsExpired(t *testing.T) { } func TestDependsOn(t *testing.T) { - for _, txType := range []int{tx.LegacyTxType, tx.DynamicFeeTxType} { + for _, txType := range []tx.TxType{tx.TypeLegacy, tx.TypeDynamicFee} { tx := GetMockTx(txType) res := tx.DependsOn() var expected *thor.Bytes32 @@ -50,7 +50,7 @@ func TestDependsOn(t *testing.T) { } func TestTestFeatures(t *testing.T) { - for _, txType := range []int{tx.LegacyTxType, tx.DynamicFeeTxType} { + for _, txType := range []tx.TxType{tx.TypeLegacy, tx.TypeDynamicFee} { tx := GetMockTx(txType) supportedFeatures := tx.Features() res := tx.TestFeatures(supportedFeatures) @@ -61,17 +61,17 @@ func TestTestFeatures(t *testing.T) { func TestToString(t *testing.T) { test := []struct { name string - txType int + txType tx.TxType expectedString string }{ { name: "Legacy transaction", - txType: tx.LegacyTxType, + txType: tx.TypeLegacy, expectedString: "\n\tTx(0x0000000000000000000000000000000000000000000000000000000000000000, 87 B)\n\tOrigin: N/A\n\tClauses: [\n\t\t(To:\t0x7567d83b7b8d80addcb281a71d54fc7b3364ffed\n\t\t Value:\t10000\n\t\t Data:\t0x000000606060) \n\t\t(To:\t0x7567d83b7b8d80addcb281a71d54fc7b3364ffed\n\t\t Value:\t20000\n\t\t Data:\t0x000000606060)]\n\tGas: 21000\n\tChainTag: 1\n\tBlockRef: 0-aabbccdd\n\tExpiration: 32\n\tDependsOn: nil\n\tNonce: 12345678\n\tUnprovedWork: 0\n\tDelegator: N/A\n\tSignature: 0x\n\n\t\tGasPriceCoef: 128\n\t\t", }, { name: "Dynamic fee transaction", - txType: tx.DynamicFeeTxType, + txType: tx.TypeDynamicFee, expectedString: "\n\tTx(0x0000000000000000000000000000000000000000000000000000000000000000, 95 B)\n\tOrigin: N/A\n\tClauses: [\n\t\t(To:\t0x7567d83b7b8d80addcb281a71d54fc7b3364ffed\n\t\t Value:\t10000\n\t\t Data:\t0x000000606060) \n\t\t(To:\t0x7567d83b7b8d80addcb281a71d54fc7b3364ffed\n\t\t Value:\t20000\n\t\t Data:\t0x000000606060)]\n\tGas: 21000\n\tChainTag: 1\n\tBlockRef: 0-aabbccdd\n\tExpiration: 32\n\tDependsOn: nil\n\tNonce: 12345678\n\tUnprovedWork: 0\n\tDelegator: N/A\n\tSignature: 0x\n\n\t\tMaxFeePerGas: 10000000\n\t\tMaxPriorityFeePerGas: 20000\n\t\t", }, } @@ -88,17 +88,17 @@ func TestToString(t *testing.T) { func TestTxSize(t *testing.T) { test := []struct { name string - txType int + txType tx.TxType expectedSize thor.StorageSize }{ { name: "Legacy transaction", - txType: tx.LegacyTxType, + txType: tx.TypeLegacy, expectedSize: thor.StorageSize(87), }, { name: "Dynamic fee transaction", - txType: tx.DynamicFeeTxType, + txType: tx.TypeDynamicFee, expectedSize: thor.StorageSize(95), }, } @@ -117,7 +117,7 @@ func TestProvedWork(t *testing.T) { return thor.Bytes32{}, nil } - for _, txType := range []int{tx.LegacyTxType, tx.DynamicFeeTxType} { + for _, txType := range []tx.TxType{tx.TypeLegacy, tx.TypeDynamicFee} { trx := GetMockTx(txType) headBlockNum := uint32(20) provedWork, err := trx.ProvedWork(headBlockNum, getBlockID) @@ -127,7 +127,7 @@ func TestProvedWork(t *testing.T) { } func TestChainTag(t *testing.T) { - for _, txType := range []int{tx.LegacyTxType, tx.DynamicFeeTxType} { + for _, txType := range []tx.TxType{tx.TypeLegacy, tx.TypeDynamicFee} { tx := GetMockTx(txType) res := tx.ChainTag() assert.Equal(t, res, uint8(0x1)) @@ -135,7 +135,7 @@ func TestChainTag(t *testing.T) { } func TestNonce(t *testing.T) { - for _, txType := range []int{tx.LegacyTxType, tx.DynamicFeeTxType} { + for _, txType := range []tx.TxType{tx.TypeLegacy, tx.TypeDynamicFee} { tx := GetMockTx(txType) res := tx.Nonce() assert.Equal(t, res, uint64(0xbc614e)) @@ -144,7 +144,7 @@ func TestNonce(t *testing.T) { func TestOverallGasPrice(t *testing.T) { // Mock or create a Transaction with necessary fields initialized - tx := GetMockTx(tx.LegacyTxType) + tx := GetMockTx(tx.TypeLegacy) // Define test cases testCases := []struct { @@ -184,19 +184,19 @@ func TestOverallGasPrice(t *testing.T) { func TestEvaluateWork(t *testing.T) { tests := []struct { name string - txType int + txType tx.TxType expectedFunc func(b *big.Int) bool }{ { name: "LegacyTxType", - txType: tx.LegacyTxType, + txType: tx.TypeLegacy, expectedFunc: func(res *big.Int) bool { return res.Cmp(big.NewInt(0)) > 0 }, }, { name: "DynamicFeeTxType", - txType: tx.DynamicFeeTxType, + txType: tx.TypeDynamicFee, expectedFunc: func(res *big.Int) bool { return res.Cmp(common.Big0) == 0 }, @@ -225,7 +225,7 @@ func TestEvaluateWork(t *testing.T) { func TestLegacyTx(t *testing.T) { to, _ := thor.ParseAddress("0x7567d83b7b8d80addcb281a71d54fc7b3364ffed") - trx := tx.NewTxBuilder(tx.LegacyTxType).ChainTag(1). + trx := tx.NewTxBuilder(tx.TypeLegacy).ChainTag(1). BlockRef(tx.BlockRef{0, 0, 0, 0, 0xaa, 0xbb, 0xcc, 0xdd}). Expiration(32). Clause(tx.NewClause(&to).WithValue(big.NewInt(10000)).WithData([]byte{0, 0, 0, 0x60, 0x60, 0x60})). @@ -238,7 +238,7 @@ func TestLegacyTx(t *testing.T) { assert.Equal(t, "0x2a1c25ce0d66f45276a5f308b99bf410e2fc7d5b6ea37a49f2ab9f1da9446478", trx.SigningHash().String()) assert.Equal(t, thor.Bytes32{}, trx.ID()) - assert.Equal(t, uint64(21000), func() uint64 { t := tx.NewTxBuilder(tx.LegacyTxType).MustBuild(); g, _ := t.IntrinsicGas(); return g }()) + assert.Equal(t, uint64(21000), func() uint64 { t := tx.NewTxBuilder(tx.TypeLegacy).MustBuild(); g, _ := t.IntrinsicGas(); return g }()) assert.Equal(t, uint64(37432), func() uint64 { g, _ := trx.IntrinsicGas(); return g }()) assert.Equal(t, big.NewInt(150), trx.GasPrice(big.NewInt(100))) @@ -270,7 +270,7 @@ func TestDelegatedTx(t *testing.T) { var feat tx.Features feat.SetDelegated(true) - trx := tx.NewTxBuilder(tx.LegacyTxType).ChainTag(0xa4). + trx := tx.NewTxBuilder(tx.TypeLegacy).ChainTag(0xa4). BlockRef(tx.BlockRef{0, 0, 0, 0, 0xaa, 0xbb, 0xcc, 0xdd}). Expiration(32). Clause(tx.NewClause(&to).WithValue(big.NewInt(10000)).WithData([]byte{0, 0, 0, 0x60, 0x60, 0x60})). @@ -335,7 +335,7 @@ func TestIntrinsicGas(t *testing.T) { } func BenchmarkTxMining(b *testing.B) { - for _, txType := range []int{tx.LegacyTxType, tx.DynamicFeeTxType} { + for _, txType := range []tx.TxType{tx.TypeLegacy, tx.TypeDynamicFee} { trx := tx.NewTxBuilder(txType).MustBuild() signer := thor.BytesToAddress([]byte("acc1")) maxWork := &big.Int{} diff --git a/tx/transactions_test.go b/tx/transactions_test.go index 7c9068e16..bf934c440 100644 --- a/tx/transactions_test.go +++ b/tx/transactions_test.go @@ -27,17 +27,17 @@ func TestRootHash(t *testing.T) { }, { name: "non-empty legacy transactions slice", - txs: tx.Transactions{GetMockTx(tx.LegacyTxType), GetMockTx(tx.LegacyTxType)}, + txs: tx.Transactions{GetMockTx(tx.TypeLegacy), GetMockTx(tx.TypeLegacy)}, expectedResult: thor.Bytes32{0x30, 0x9a, 0xd5, 0x4b, 0x28, 0x76, 0x65, 0x52, 0x66, 0x89, 0x7b, 0x19, 0x22, 0x24, 0x63, 0xd8, 0x27, 0xc8, 0x2a, 0xd6, 0x20, 0x17, 0x7a, 0xcf, 0x9a, 0xfa, 0xc, 0xce, 0xff, 0x12, 0x24, 0x48}, }, { name: "non-empty dyn fee transactions slice", - txs: tx.Transactions{GetMockTx(tx.DynamicFeeTxType), GetMockTx(tx.DynamicFeeTxType)}, + txs: tx.Transactions{GetMockTx(tx.TypeDynamicFee), GetMockTx(tx.TypeDynamicFee)}, expectedResult: thor.Bytes32{0xb6, 0xa5, 0x78, 0x74, 0x34, 0x61, 0xfb, 0xd5, 0x53, 0x95, 0x5, 0xe0, 0x91, 0xfe, 0x34, 0x3b, 0xf8, 0xcf, 0xa4, 0x50, 0xa9, 0xb6, 0x7f, 0x18, 0x37, 0x8a, 0xbb, 0x4d, 0x9, 0xda, 0x32, 0xe8}, }, { name: "non-empty mixed transactions slice", - txs: tx.Transactions{GetMockTx(tx.LegacyTxType), GetMockTx(tx.DynamicFeeTxType), GetMockTx(tx.DynamicFeeTxType), GetMockTx(tx.LegacyTxType)}, + txs: tx.Transactions{GetMockTx(tx.TypeLegacy), GetMockTx(tx.TypeDynamicFee), GetMockTx(tx.TypeDynamicFee), GetMockTx(tx.TypeLegacy)}, expectedResult: thor.Bytes32{0x11, 0xe6, 0xdc, 0x2f, 0xd3, 0x3b, 0x7c, 0xa7, 0xf9, 0xad, 0x6e, 0xeb, 0xff, 0x6c, 0xc8, 0x45, 0xa5, 0x61, 0xaf, 0x23, 0x9, 0x4a, 0x89, 0x78, 0xd7, 0x3b, 0xf4, 0xdf, 0x65, 0x1c, 0x5d, 0x90}, }, } diff --git a/tx/tx_dynamic_fee.go b/tx/tx_dynamic_fee.go index fc10e3990..37047b195 100644 --- a/tx/tx_dynamic_fee.go +++ b/tx/tx_dynamic_fee.go @@ -30,7 +30,7 @@ type DynamicFeeTransaction struct { } func (t *DynamicFeeTransaction) txType() byte { - return DynamicFeeTxType + return TypeDynamicFee } func (t *DynamicFeeTransaction) copy() TxData { diff --git a/tx/tx_legacy.go b/tx/tx_legacy.go index 8a617d079..9763b2513 100644 --- a/tx/tx_legacy.go +++ b/tx/tx_legacy.go @@ -29,7 +29,7 @@ type LegacyTransaction struct { } func (t *LegacyTransaction) txType() byte { - return LegacyTxType + return TypeLegacy } func (t *LegacyTransaction) copy() TxData { diff --git a/txpool/tx_object_map_test.go b/txpool/tx_object_map_test.go index ff2959ec5..df8de13bc 100644 --- a/txpool/tx_object_map_test.go +++ b/txpool/tx_object_map_test.go @@ -22,9 +22,9 @@ func TestGetByID(t *testing.T) { repo := newChainRepo(muxdb.NewMem()) // Creating transactions - tx1 := newTx(tx.LegacyTxType, repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), genesis.DevAccounts()[0]) - tx2 := newTx(tx.LegacyTxType, repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), genesis.DevAccounts()[1]) - tx3 := newTx(tx.DynamicFeeTxType, repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), genesis.DevAccounts()[2]) + tx1 := newTx(tx.TypeLegacy, repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), genesis.DevAccounts()[0]) + tx2 := newTx(tx.TypeLegacy, repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), genesis.DevAccounts()[1]) + tx3 := newTx(tx.TypeDynamicFee, repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), genesis.DevAccounts()[2]) // Resolving transactions into txObjects txObj1, _ := resolveTx(tx1, false) @@ -57,9 +57,9 @@ func TestFill(t *testing.T) { repo := newChainRepo(muxdb.NewMem()) // Creating transactions - tx1 := newTx(tx.LegacyTxType, repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), genesis.DevAccounts()[0]) - tx2 := newDelegatedTx(tx.LegacyTxType, repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, genesis.DevAccounts()[1], genesis.DevAccounts()[2]) - tx3 := newDelegatedTx(tx.DynamicFeeTxType, repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, genesis.DevAccounts()[3], genesis.DevAccounts()[4]) + tx1 := newTx(tx.TypeLegacy, repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), genesis.DevAccounts()[0]) + tx2 := newDelegatedTx(tx.TypeLegacy, repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, genesis.DevAccounts()[1], genesis.DevAccounts()[2]) + tx3 := newDelegatedTx(tx.TypeDynamicFee, repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, genesis.DevAccounts()[3], genesis.DevAccounts()[4]) // Resolving transactions into txObjects txObj1, _ := resolveTx(tx1, false) @@ -95,9 +95,9 @@ func TestFill(t *testing.T) { func TestTxObjMap(t *testing.T) { repo := newChainRepo(muxdb.NewMem()) - tx1 := newTx(tx.LegacyTxType, repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), genesis.DevAccounts()[0]) - tx2 := newTx(tx.DynamicFeeTxType, repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), genesis.DevAccounts()[0]) - tx3 := newTx(tx.LegacyTxType, repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), genesis.DevAccounts()[1]) + tx1 := newTx(tx.TypeLegacy, repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), genesis.DevAccounts()[0]) + tx2 := newTx(tx.TypeDynamicFee, repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), genesis.DevAccounts()[0]) + tx3 := newTx(tx.TypeLegacy, repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), genesis.DevAccounts()[1]) txObj1, _ := resolveTx(tx1, false) txObj2, _ := resolveTx(tx2, false) @@ -131,9 +131,9 @@ func TestTxObjMap(t *testing.T) { func TestLimitByDelegator(t *testing.T) { repo := newChainRepo(muxdb.NewMem()) - tx1 := newTx(tx.LegacyTxType, repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), genesis.DevAccounts()[0]) - tx2 := newDelegatedTx(tx.DynamicFeeTxType, repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, genesis.DevAccounts()[0], genesis.DevAccounts()[1]) - tx3 := newDelegatedTx(tx.LegacyTxType, repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, genesis.DevAccounts()[2], genesis.DevAccounts()[1]) + tx1 := newTx(tx.TypeLegacy, repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), genesis.DevAccounts()[0]) + tx2 := newDelegatedTx(tx.TypeDynamicFee, repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, genesis.DevAccounts()[0], genesis.DevAccounts()[1]) + tx3 := newDelegatedTx(tx.TypeLegacy, repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, genesis.DevAccounts()[2], genesis.DevAccounts()[1]) txObj1, _ := resolveTx(tx1, false) txObj2, _ := resolveTx(tx2, false) @@ -155,9 +155,9 @@ func TestPendingCost(t *testing.T) { stater := state.NewStater(db) // Creating transactions - tx1 := newTx(tx.LegacyTxType, repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), genesis.DevAccounts()[0]) - tx2 := newDelegatedTx(tx.LegacyTxType, repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, genesis.DevAccounts()[1], genesis.DevAccounts()[2]) - tx3 := newDelegatedTx(tx.DynamicFeeTxType, repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, genesis.DevAccounts()[1], genesis.DevAccounts()[2]) + tx1 := newTx(tx.TypeLegacy, repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), genesis.DevAccounts()[0]) + tx2 := newDelegatedTx(tx.TypeLegacy, repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, genesis.DevAccounts()[1], genesis.DevAccounts()[2]) + tx3 := newDelegatedTx(tx.TypeDynamicFee, repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, genesis.DevAccounts()[1], genesis.DevAccounts()[2]) // Resolving transactions into txObjects txObj1, _ := resolveTx(tx1, false) diff --git a/txpool/tx_object_test.go b/txpool/tx_object_test.go index 4e643ce90..951adc8a5 100644 --- a/txpool/tx_object_test.go +++ b/txpool/tx_object_test.go @@ -29,12 +29,12 @@ func newChainRepo(db *muxdb.MuxDB) *chain.Repository { return repo } -func newTx(txType int, chainTag byte, clauses []*tx.Clause, gas uint64, blockRef tx.BlockRef, expiration uint32, dependsOn *thor.Bytes32, features tx.Features, from genesis.DevAccount) *tx.Transaction { +func newTx(txType tx.TxType, chainTag byte, clauses []*tx.Clause, gas uint64, blockRef tx.BlockRef, expiration uint32, dependsOn *thor.Bytes32, features tx.Features, from genesis.DevAccount) *tx.Transaction { trx := txBuilder(txType, chainTag, clauses, gas, blockRef, expiration, dependsOn, features).MustBuild() return tx.MustSign(trx, from.PrivateKey) } -func newDelegatedTx(txType int, chainTag byte, clauses []*tx.Clause, gas uint64, blockRef tx.BlockRef, expiration uint32, dependsOn *thor.Bytes32, from genesis.DevAccount, delegator genesis.DevAccount) *tx.Transaction { +func newDelegatedTx(txType tx.TxType, chainTag byte, clauses []*tx.Clause, gas uint64, blockRef tx.BlockRef, expiration uint32, dependsOn *thor.Bytes32, from genesis.DevAccount, delegator genesis.DevAccount) *tx.Transaction { var features tx.Features features.SetDelegated(true) @@ -45,7 +45,7 @@ func newDelegatedTx(txType int, chainTag byte, clauses []*tx.Clause, gas uint64, ) } -func txBuilder(txType int, chainTag byte, clauses []*tx.Clause, gas uint64, blockRef tx.BlockRef, expiration uint32, dependsOn *thor.Bytes32, features tx.Features) *tx.Builder { +func txBuilder(txType tx.TxType, chainTag byte, clauses []*tx.Clause, gas uint64, blockRef tx.BlockRef, expiration uint32, dependsOn *thor.Bytes32, features tx.Features) *tx.Builder { builder := tx.NewTxBuilder(txType).ChainTag(chainTag) for _, c := range clauses { builder.Clause(c) @@ -82,8 +82,8 @@ func TestExecutableWithError(t *testing.T) { expected bool expectedErr string }{ - {newTx(tx.LegacyTxType, 0, nil, 21000, tx.BlockRef{0}, 100, nil, tx.Features(0), acc), false, ""}, - {newTx(tx.DynamicFeeTxType, 0, nil, 21000, tx.BlockRef{0}, 100, nil, tx.Features(0), acc), false, ""}, + {newTx(tx.TypeLegacy, 0, nil, 21000, tx.BlockRef{0}, 100, nil, tx.Features(0), acc), false, ""}, + {newTx(tx.TypeDynamicFee, 0, nil, 21000, tx.BlockRef{0}, 100, nil, tx.Features(0), acc), false, ""}, } for _, tt := range tests { @@ -120,7 +120,7 @@ func TestSort(t *testing.T) { func TestResolve(t *testing.T) { acc := genesis.DevAccounts()[0] - trx := newTx(tx.LegacyTxType, 0, nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), acc) + trx := newTx(tx.TypeLegacy, 0, nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), acc) txObj, err := resolveTx(trx, false) assert.Nil(t, err) @@ -128,7 +128,7 @@ func TestResolve(t *testing.T) { assert.Equal(t, acc.Address, txObj.Origin()) - trx = newTx(tx.DynamicFeeTxType, 0, nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), acc) + trx = newTx(tx.TypeDynamicFee, 0, nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), acc) txObj, err = resolveTx(trx, false) assert.Nil(t, err) assert.Equal(t, trx, txObj.Transaction) @@ -150,18 +150,18 @@ func TestExecutable(t *testing.T) { expected bool expectedErr string }{ - {newTx(tx.LegacyTxType, 0, nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), acc), true, ""}, - {newTx(tx.LegacyTxType, 0, nil, b1.Header().GasLimit(), tx.BlockRef{}, 100, nil, tx.Features(0), acc), true, ""}, - {newTx(tx.LegacyTxType, 0, nil, b1.Header().GasLimit()+1, tx.BlockRef{}, 100, nil, tx.Features(0), acc), false, "gas too large"}, - {newTx(tx.LegacyTxType, 0, nil, math.MaxUint64, tx.BlockRef{}, 100, nil, tx.Features(0), acc), false, "gas too large"}, - {newTx(tx.LegacyTxType, 0, nil, 21000, tx.BlockRef{1}, 100, nil, tx.Features(0), acc), true, "block ref out of schedule"}, - {newTx(tx.LegacyTxType, 0, nil, 21000, tx.BlockRef{0}, 0, nil, tx.Features(0), acc), true, "expired"}, - {newTx(tx.LegacyTxType, 0, nil, 21000, tx.BlockRef{0}, 100, &thor.Bytes32{}, tx.Features(0), acc), false, ""}, - {newTx(tx.DynamicFeeTxType, 0, nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), acc), true, ""}, - {newTx(tx.DynamicFeeTxType, 0, nil, math.MaxUint64, tx.BlockRef{}, 100, nil, tx.Features(0), acc), false, "gas too large"}, - {newTx(tx.DynamicFeeTxType, 0, nil, 21000, tx.BlockRef{1}, 100, nil, tx.Features(0), acc), true, "block ref out of schedule"}, - {newTx(tx.DynamicFeeTxType, 0, nil, 21000, tx.BlockRef{0}, 0, nil, tx.Features(0), acc), true, "expired"}, - {newTx(tx.DynamicFeeTxType, 0, nil, 21000, tx.BlockRef{0}, 100, &thor.Bytes32{}, tx.Features(0), acc), false, ""}, + {newTx(tx.TypeLegacy, 0, nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), acc), true, ""}, + {newTx(tx.TypeLegacy, 0, nil, b1.Header().GasLimit(), tx.BlockRef{}, 100, nil, tx.Features(0), acc), true, ""}, + {newTx(tx.TypeLegacy, 0, nil, b1.Header().GasLimit()+1, tx.BlockRef{}, 100, nil, tx.Features(0), acc), false, "gas too large"}, + {newTx(tx.TypeLegacy, 0, nil, math.MaxUint64, tx.BlockRef{}, 100, nil, tx.Features(0), acc), false, "gas too large"}, + {newTx(tx.TypeLegacy, 0, nil, 21000, tx.BlockRef{1}, 100, nil, tx.Features(0), acc), true, "block ref out of schedule"}, + {newTx(tx.TypeLegacy, 0, nil, 21000, tx.BlockRef{0}, 0, nil, tx.Features(0), acc), true, "expired"}, + {newTx(tx.TypeLegacy, 0, nil, 21000, tx.BlockRef{0}, 100, &thor.Bytes32{}, tx.Features(0), acc), false, ""}, + {newTx(tx.TypeDynamicFee, 0, nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), acc), true, ""}, + {newTx(tx.TypeDynamicFee, 0, nil, math.MaxUint64, tx.BlockRef{}, 100, nil, tx.Features(0), acc), false, "gas too large"}, + {newTx(tx.TypeDynamicFee, 0, nil, 21000, tx.BlockRef{1}, 100, nil, tx.Features(0), acc), true, "block ref out of schedule"}, + {newTx(tx.TypeDynamicFee, 0, nil, 21000, tx.BlockRef{0}, 0, nil, tx.Features(0), acc), true, "expired"}, + {newTx(tx.TypeDynamicFee, 0, nil, 21000, tx.BlockRef{0}, 100, &thor.Bytes32{}, tx.Features(0), acc), false, ""}, } for _, tt := range tests { diff --git a/txpool/tx_pool_test.go b/txpool/tx_pool_test.go index f3e534b3b..ec6c5ca2a 100644 --- a/txpool/tx_pool_test.go +++ b/txpool/tx_pool_test.go @@ -102,7 +102,7 @@ func TestNewCloseWithServer(t *testing.T) { // Create a slice of transactions to be added to the pool. txs := make(Tx.Transactions, 0, 30) for i := 0; i < 15; i++ { - tx := newTx(tx.LegacyTxType, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[i%len(devAccounts)]) + tx := newTx(tx.TypeLegacy, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[i%len(devAccounts)]) txs = append(txs, tx) } @@ -116,14 +116,14 @@ func FillPoolWithLegacyTxs(pool *TxPool, t *testing.T) { // Create a slice of transactions to be added to the pool. txs := make(Tx.Transactions, 0, 15) for i := 0; i < 12; i++ { - tx := newTx(tx.LegacyTxType, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[0]) + tx := newTx(tx.TypeLegacy, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[0]) txs = append(txs, tx) } // Call the Fill method pool.Fill(txs) - err := pool.Add(newTx(tx.LegacyTxType, pool.repo.ChainTag(), nil, 21000, tx.NewBlockRef(10), 100, nil, Tx.Features(0), devAccounts[0])) + err := pool.Add(newTx(tx.TypeLegacy, pool.repo.ChainTag(), nil, 21000, tx.NewBlockRef(10), 100, nil, Tx.Features(0), devAccounts[0])) assert.Equal(t, err.Error(), "tx rejected: pool is full") } @@ -134,14 +134,14 @@ func FillPoolWithDynFeeTxs(pool *TxPool, t *testing.T) { // Create a slice of transactions to be added to the pool. txs := make(Tx.Transactions, 0, 15) for i := 0; i < 12; i++ { - tx := newTx(tx.DynamicFeeTxType, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[0]) + tx := newTx(tx.TypeDynamicFee, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[0]) txs = append(txs, tx) } // Call the Fill method pool.Fill(txs) - err := pool.Add(newTx(tx.DynamicFeeTxType, pool.repo.ChainTag(), nil, 21000, tx.NewBlockRef(10), 100, nil, Tx.Features(0), devAccounts[0])) + err := pool.Add(newTx(tx.TypeDynamicFee, pool.repo.ChainTag(), nil, 21000, tx.NewBlockRef(10), 100, nil, Tx.Features(0), devAccounts[0])) assert.Equal(t, err.Error(), "tx rejected: pool is full") assert.Equal(t, "tx rejected: pool is full", err.Error()) } @@ -153,16 +153,16 @@ func FillPoolWithMixedTxs(pool *TxPool, t *testing.T) { // Create a slice of transactions to be added to the pool. txs := make(Tx.Transactions, 0, 15) for i := 0; i < 6; i++ { - trx := newTx(tx.LegacyTxType, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[0]) + trx := newTx(tx.TypeLegacy, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[0]) txs = append(txs, trx) - trx = newTx(tx.DynamicFeeTxType, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[0]) + trx = newTx(tx.TypeDynamicFee, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[0]) txs = append(txs, trx) } // Call the Fill method pool.Fill(txs) - err := pool.Add(newTx(tx.DynamicFeeTxType, pool.repo.ChainTag(), nil, 21000, tx.NewBlockRef(10), 100, nil, Tx.Features(0), devAccounts[0])) + err := pool.Add(newTx(tx.TypeDynamicFee, pool.repo.ChainTag(), nil, 21000, tx.NewBlockRef(10), 100, nil, Tx.Features(0), devAccounts[0])) assert.Equal(t, err.Error(), "tx rejected: pool is full") assert.Equal(t, "tx rejected: pool is full", err.Error()) } @@ -226,11 +226,11 @@ func TestDump(t *testing.T) { // Create and add transactions to the pool txsToAdd := make(tx.Transactions, 0, 10) for i := 0; i < 5; i++ { - trx := newTx(tx.LegacyTxType, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[i%len(devAccounts)]) + trx := newTx(tx.TypeLegacy, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[i%len(devAccounts)]) txsToAdd = append(txsToAdd, trx) assert.Nil(t, pool.Add(trx)) - trx = newTx(tx.DynamicFeeTxType, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[i%len(devAccounts)]) + trx = newTx(tx.TypeDynamicFee, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[i%len(devAccounts)]) txsToAdd = append(txsToAdd, trx) assert.Nil(t, pool.Add(trx)) } @@ -259,7 +259,7 @@ func TestRemove(t *testing.T) { defer pool.Close() // Create and add a legacy transaction to the pool - trx := newTx(tx.LegacyTxType, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[0]) + trx := newTx(tx.TypeLegacy, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[0]) assert.Nil(t, pool.Add(trx), "Adding transaction should not produce error") // Ensure the transaction is in the pool @@ -273,7 +273,7 @@ func TestRemove(t *testing.T) { assert.Nil(t, pool.Get(trx.ID()), "Transaction should not exist in the pool after removal") // Create and add a dyn fee transaction to the pool - trx = newTx(tx.DynamicFeeTxType, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[0]) + trx = newTx(tx.TypeDynamicFee, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[0]) assert.Nil(t, pool.Add(trx), "Adding transaction should not produce error") // Ensure the transaction is in the pool @@ -292,7 +292,7 @@ func TestRemoveWithError(t *testing.T) { defer pool.Close() // Create and add a transaction to the pool - tx := newTx(tx.LegacyTxType, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[0]) + tx := newTx(tx.TypeLegacy, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[0]) // assert.Nil(t, pool.Add(tx), "Adding transaction should not produce error") // Ensure the transaction is in the pool @@ -334,7 +334,7 @@ func TestSubscribeNewTx(t *testing.T) { pool.SubscribeTxEvent(txCh) - tx := newTx(tx.LegacyTxType, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[0]) + tx := newTx(tx.TypeLegacy, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[0]) assert.Nil(t, pool.Add(tx)) v := true @@ -368,7 +368,7 @@ func TestSubscribeNewTypedTx(t *testing.T) { pool.SubscribeTxEvent(txCh) - trx, err := tx.NewTxBuilder(tx.DynamicFeeTxType). + trx, err := tx.NewTxBuilder(tx.TypeDynamicFee). ChainTag(pool.repo.ChainTag()). Expiration(100). Gas(21000). @@ -392,7 +392,7 @@ func TestWashTxs(t *testing.T) { assert.Zero(t, len(txs)) assert.Zero(t, len(pool.Executables())) - tx1 := newTx(tx.LegacyTxType, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[0]) + tx1 := newTx(tx.TypeLegacy, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[0]) assert.Nil(t, pool.AddLocal(tx1)) // this tx won't participate in the wash out. txs, _, err = pool.wash(pool.repo.BestBlockSummary()) @@ -415,11 +415,11 @@ func TestWashTxs(t *testing.T) { assert.Nil(t, err) assert.Equal(t, Tx.Transactions{tx1}, txs) - tx2 := newTx(tx.LegacyTxType, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[1]) + tx2 := newTx(tx.TypeLegacy, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[1]) txObj2, _ := resolveTx(tx2, false) assert.Nil(t, pool.all.Add(txObj2, LIMIT_PER_ACCOUNT, func(_ thor.Address, _ *big.Int) error { return nil })) // this tx will participate in the wash out. - tx3 := newTx(tx.LegacyTxType, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[2]) + tx3 := newTx(tx.TypeLegacy, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[2]) txObj3, _ := resolveTx(tx3, false) assert.Nil(t, pool.all.Add(txObj3, LIMIT_PER_ACCOUNT, func(_ thor.Address, _ *big.Int) error { return nil })) // this tx will participate in the wash out. @@ -544,9 +544,9 @@ func TestOrderTxsAfterGalacticaForkSameValues(t *testing.T) { } func generateRandomTx(t *testing.T, seed int, chainTag byte) *tx.Transaction { - txType := tx.DynamicFeeTxType + txType := tx.TypeDynamicFee if (seed % 2) == 0 { - txType = tx.DynamicFeeTxType + txType = tx.TypeDynamicFee } maxFeePerGas := int64(thor.InitialBaseFee + r.IntN(thor.InitialBaseFee)) // #nosec G404 @@ -572,7 +572,7 @@ func TestFillPool(t *testing.T) { // Create a slice of transactions to be added to the pool. txs := make(Tx.Transactions, 0, 5) for i := 0; i < 5; i++ { - tx := newTx(tx.LegacyTxType, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[i%len(devAccounts)]) + tx := newTx(tx.TypeLegacy, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[i%len(devAccounts)]) txs = append(txs, tx) } @@ -630,10 +630,10 @@ func TestFillPoolWithMixedTxs(t *testing.T) { // Create a slice of transactions to be added to the pool. txs := make(Tx.Transactions, 0, 10) for i := 0; i < 5; i++ { - tr := newTx(tx.DynamicFeeTxType, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[i%len(devAccounts)]) + tr := newTx(tx.TypeDynamicFee, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[i%len(devAccounts)]) txs = append(txs, tr) - tr = newTx(tx.LegacyTxType, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[i%len(devAccounts)]) + tr = newTx(tx.TypeLegacy, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[i%len(devAccounts)]) txs = append(txs, tr) } @@ -676,14 +676,14 @@ func TestAdd(t *testing.T) { pool.repo.AddBlock(b1, nil, 0, true) acc := devAccounts[0] - dupTx := newTx(tx.LegacyTxType, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), acc) + dupTx := newTx(tx.TypeLegacy, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), acc) tests := []struct { tx *tx.Transaction errStr string }{ - {newTx(tx.LegacyTxType, pool.repo.ChainTag()+1, nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), acc), "bad tx: chain tag mismatch"}, - {newTx(tx.DynamicFeeTxType, pool.repo.ChainTag()+1, nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), acc), "bad tx: chain tag mismatch"}, + {newTx(tx.TypeLegacy, pool.repo.ChainTag()+1, nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), acc), "bad tx: chain tag mismatch"}, + {newTx(tx.TypeDynamicFee, pool.repo.ChainTag()+1, nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), acc), "bad tx: chain tag mismatch"}, {dupTx, ""}, {dupTx, ""}, } @@ -712,17 +712,17 @@ func TestAdd(t *testing.T) { tx *Tx.Transaction errStr string }{ - {newTx(tx.LegacyTxType, pool.repo.ChainTag(), nil, 21000, tx.NewBlockRef(10), 100, nil, Tx.Features(0), acc), "tx rejected: tx is not executable"}, - {newTx(tx.LegacyTxType, pool.repo.ChainTag(), nil, 21000, tx.NewBlockRef(100), 100, nil, Tx.Features(0), acc), "tx rejected: block ref out of schedule"}, - {newTx(tx.LegacyTxType, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, &thor.Bytes32{1}, Tx.Features(0), acc), "tx rejected: tx is not executable"}, - {newTx(tx.LegacyTxType, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, &thor.Bytes32{1}, Tx.Features(2), acc), "tx rejected: unsupported features"}, - {newTx(tx.LegacyTxType, pool.repo.ChainTag(), []*tx.Clause{tx.NewClause(nil).WithData(data[:])}, 21000, tx.BlockRef{}, 100, &thor.Bytes32{1}, Tx.Features(0), acc), "tx rejected: size too large"}, + {newTx(tx.TypeLegacy, pool.repo.ChainTag(), nil, 21000, tx.NewBlockRef(10), 100, nil, Tx.Features(0), acc), "tx rejected: tx is not executable"}, + {newTx(tx.TypeLegacy, pool.repo.ChainTag(), nil, 21000, tx.NewBlockRef(100), 100, nil, Tx.Features(0), acc), "tx rejected: block ref out of schedule"}, + {newTx(tx.TypeLegacy, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, &thor.Bytes32{1}, Tx.Features(0), acc), "tx rejected: tx is not executable"}, + {newTx(tx.TypeLegacy, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, &thor.Bytes32{1}, Tx.Features(2), acc), "tx rejected: unsupported features"}, + {newTx(tx.TypeLegacy, pool.repo.ChainTag(), []*tx.Clause{tx.NewClause(nil).WithData(data[:])}, 21000, tx.BlockRef{}, 100, &thor.Bytes32{1}, Tx.Features(0), acc), "tx rejected: size too large"}, {badReserved, "tx rejected: unsupported features"}, - {newTx(tx.DynamicFeeTxType, pool.repo.ChainTag(), nil, 21000, tx.NewBlockRef(10), 100, nil, Tx.Features(0), acc), "tx rejected: tx is not executable"}, - {newTx(tx.DynamicFeeTxType, pool.repo.ChainTag(), nil, 21000, tx.NewBlockRef(100), 100, nil, Tx.Features(0), acc), "tx rejected: block ref out of schedule"}, - {newTx(tx.DynamicFeeTxType, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, &thor.Bytes32{1}, Tx.Features(0), acc), "tx rejected: tx is not executable"}, - {newTx(tx.DynamicFeeTxType, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, &thor.Bytes32{1}, Tx.Features(2), acc), "tx rejected: unsupported features"}, - {newTx(tx.DynamicFeeTxType, pool.repo.ChainTag(), []*tx.Clause{tx.NewClause(nil).WithData(data[:])}, 21000, tx.BlockRef{}, 100, &thor.Bytes32{1}, Tx.Features(0), acc), "tx rejected: size too large"}, + {newTx(tx.TypeDynamicFee, pool.repo.ChainTag(), nil, 21000, tx.NewBlockRef(10), 100, nil, Tx.Features(0), acc), "tx rejected: tx is not executable"}, + {newTx(tx.TypeDynamicFee, pool.repo.ChainTag(), nil, 21000, tx.NewBlockRef(100), 100, nil, Tx.Features(0), acc), "tx rejected: block ref out of schedule"}, + {newTx(tx.TypeDynamicFee, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, &thor.Bytes32{1}, Tx.Features(0), acc), "tx rejected: tx is not executable"}, + {newTx(tx.TypeDynamicFee, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, &thor.Bytes32{1}, Tx.Features(2), acc), "tx rejected: unsupported features"}, + {newTx(tx.TypeDynamicFee, pool.repo.ChainTag(), []*tx.Clause{tx.NewClause(nil).WithData(data[:])}, 21000, tx.BlockRef{}, 100, &thor.Bytes32{1}, Tx.Features(0), acc), "tx rejected: size too large"}, } for _, tt := range tests { @@ -750,7 +750,7 @@ func TestBeforeVIP191Add(t *testing.T) { }, &thor.NoFork) defer pool.Close() - err := pool.StrictlyAdd(newTx(tx.LegacyTxType, pool.repo.ChainTag(), nil, 21000, tx.NewBlockRef(200), 100, nil, Tx.Features(1), acc)) + err := pool.StrictlyAdd(newTx(tx.TypeLegacy, pool.repo.ChainTag(), nil, 21000, tx.NewBlockRef(200), 100, nil, Tx.Features(1), acc)) assert.Equal(t, "tx rejected: unsupported features", err.Error()) } @@ -760,8 +760,8 @@ func TestPoolLimit(t *testing.T) { pool := newPoolWithParams(2, 1, "", "", uint64(time.Now().Unix()), &thor.NoFork) defer pool.Close() - trx1 := newTx(tx.LegacyTxType, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[0]) - trx2 := newTx(tx.LegacyTxType, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[0]) + trx1 := newTx(tx.TypeLegacy, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[0]) + trx2 := newTx(tx.TypeLegacy, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[0]) pool.add(trx1, false, false) err := pool.add(trx2, false, false) @@ -770,8 +770,8 @@ func TestPoolLimit(t *testing.T) { // not synced pool = newPool(2, 1, &thor.NoFork) - trx1 = newTx(tx.LegacyTxType, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[0]) - trx2 = newTx(tx.LegacyTxType, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[0]) + trx1 = newTx(tx.TypeLegacy, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[0]) + trx2 = newTx(tx.TypeLegacy, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[0]) pool.add(trx1, false, false) err = pool.add(trx2, false, false) assert.Equal(t, "tx rejected: account quota exceeded", err.Error()) @@ -785,18 +785,18 @@ func TestExecutableAndNonExecutableLimits(t *testing.T) { // Create a slice of transactions to be added to the pool. txs := make(Tx.Transactions, 0, 11) for i := 0; i < 12; i++ { - tx := newTx(tx.LegacyTxType, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[i%len(devAccounts)]) + tx := newTx(tx.TypeLegacy, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[i%len(devAccounts)]) pool.add(tx, false, false) txs = append(txs, tx) } pool.executables.Store(txs) - trx1 := newTx(tx.LegacyTxType, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[1]) + trx1 := newTx(tx.TypeLegacy, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[1]) err := pool.add(trx1, false, false) assert.Equal(t, "tx rejected: pool is full", err.Error()) - trx2 := newTx(tx.LegacyTxType, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, &thor.Bytes32{1}, tx.Features(0), devAccounts[1]) + trx2 := newTx(tx.TypeLegacy, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, &thor.Bytes32{1}, tx.Features(0), devAccounts[1]) err = pool.add(trx2, false, false) assert.Equal(t, "tx rejected: pool is full", err.Error()) @@ -805,20 +805,20 @@ func TestExecutableAndNonExecutableLimits(t *testing.T) { pool = newPoolWithParams(5, 2, "", "", uint64(time.Now().Unix()), &thor.NoFork) defer pool.Close() - trx1 = newTx(tx.LegacyTxType, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, &thor.Bytes32{1}, tx.Features(0), devAccounts[0]) + trx1 = newTx(tx.TypeLegacy, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, &thor.Bytes32{1}, tx.Features(0), devAccounts[0]) err = pool.add(trx1, false, false) assert.Nil(t, err) // dependant fails - trx2 = newTx(tx.LegacyTxType, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, &thor.Bytes32{1}, tx.Features(0), devAccounts[2]) + trx2 = newTx(tx.TypeLegacy, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, &thor.Bytes32{1}, tx.Features(0), devAccounts[2]) err = pool.add(trx2, false, false) assert.Equal(t, "tx rejected: non executable pool is full", err.Error()) // higher block fails - trx2 = newTx(tx.LegacyTxType, pool.repo.ChainTag(), nil, 21000, tx.NewBlockRef(tx.BlockRef{}.Number()+2), 100, nil, tx.Features(0), devAccounts[2]) + trx2 = newTx(tx.TypeLegacy, pool.repo.ChainTag(), nil, 21000, tx.NewBlockRef(tx.BlockRef{}.Number()+2), 100, nil, tx.Features(0), devAccounts[2]) err = pool.add(trx2, false, false) @@ -830,14 +830,14 @@ func TestNonExecutables(t *testing.T) { // loop 90 times for i := 0; i < 90; i++ { - assert.NoError(t, pool.AddLocal(newTx(tx.LegacyTxType, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[i%len(devAccounts)]))) + assert.NoError(t, pool.AddLocal(newTx(tx.TypeLegacy, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[i%len(devAccounts)]))) } executables, _, _ := pool.wash(pool.repo.BestBlockSummary()) pool.executables.Store(executables) // add 1 non-executable - assert.NoError(t, pool.AddLocal(newTx(tx.LegacyTxType, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, &thor.Bytes32{1}, tx.Features(0), devAccounts[2]))) + assert.NoError(t, pool.AddLocal(newTx(tx.TypeLegacy, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, &thor.Bytes32{1}, tx.Features(0), devAccounts[2]))) } func TestExpiredTxs(t *testing.T) { @@ -845,14 +845,14 @@ func TestExpiredTxs(t *testing.T) { // loop 90 times for i := 0; i < 90; i++ { - assert.NoError(t, pool.Add(newTx(tx.LegacyTxType, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[i%len(devAccounts)]))) + assert.NoError(t, pool.Add(newTx(tx.TypeLegacy, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[i%len(devAccounts)]))) } executables, _, _ := pool.wash(pool.repo.BestBlockSummary()) pool.executables.Store(executables) // add 1 non-executable - assert.NoError(t, pool.Add(newTx(tx.LegacyTxType, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, &thor.Bytes32{1}, tx.Features(0), devAccounts[2]))) + assert.NoError(t, pool.Add(newTx(tx.TypeLegacy, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, &thor.Bytes32{1}, tx.Features(0), devAccounts[2]))) executables, washed, err := pool.wash(pool.repo.BestBlockSummary()) assert.Nil(t, err) @@ -881,7 +881,7 @@ func TestBlocked(t *testing.T) { <-time.After(10 * time.Millisecond) // adding blocked should return nil - trx := newTx(tx.LegacyTxType, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[len(devAccounts)-1]) + trx := newTx(tx.TypeLegacy, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[len(devAccounts)-1]) err = pool.Add(trx) assert.Nil(t, err) @@ -907,7 +907,7 @@ func TestWash(t *testing.T) { }{ { "MaxLife", func(t *testing.T) { - trx := newTx(tx.LegacyTxType, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[len(devAccounts)-1]) + trx := newTx(tx.TypeLegacy, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[len(devAccounts)-1]) pool.add(trx, false, false) txObj := pool.all.mapByID[trx.ID()] @@ -928,7 +928,7 @@ func TestWash(t *testing.T) { PrivateKey: priv, } - trx := newTx(tx.LegacyTxType, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), acc) + trx := newTx(tx.TypeLegacy, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), acc) txObj, err := resolveTx(trx, false) assert.Nil(t, err) @@ -952,9 +952,9 @@ func TestWash(t *testing.T) { PrivateKey: priv, } - trx1 := newTx(tx.LegacyTxType, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[0]) - trx2 := newTx(tx.LegacyTxType, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[0]) - trx3 := newTx(tx.LegacyTxType, pool.repo.ChainTag(), nil, 21000, tx.NewBlockRef(pool.repo.BestBlockSummary().Header.Number()+10), 100, nil, tx.Features(0), acc) + trx1 := newTx(tx.TypeLegacy, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[0]) + trx2 := newTx(tx.TypeLegacy, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[0]) + trx3 := newTx(tx.TypeLegacy, pool.repo.ChainTag(), nil, 21000, tx.NewBlockRef(pool.repo.BestBlockSummary().Header.Number()+10), 100, nil, tx.Features(0), acc) pool.add(trx1, false, false) txObj, err := resolveTx(trx2, false) @@ -983,9 +983,9 @@ func TestWash(t *testing.T) { PrivateKey: priv, } - trx1 := newTx(tx.LegacyTxType, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[0]) - trx2 := newTx(tx.LegacyTxType, pool.repo.ChainTag(), nil, 21000, tx.NewBlockRef(pool.repo.BestBlockSummary().Header.Number()+10), 100, nil, tx.Features(0), devAccounts[0]) - trx3 := newTx(tx.LegacyTxType, pool.repo.ChainTag(), nil, 21000, tx.NewBlockRef(pool.repo.BestBlockSummary().Header.Number()+10), 100, nil, tx.Features(0), acc) + trx1 := newTx(tx.TypeLegacy, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[0]) + trx2 := newTx(tx.TypeLegacy, pool.repo.ChainTag(), nil, 21000, tx.NewBlockRef(pool.repo.BestBlockSummary().Header.Number()+10), 100, nil, tx.Features(0), devAccounts[0]) + trx3 := newTx(tx.TypeLegacy, pool.repo.ChainTag(), nil, 21000, tx.NewBlockRef(pool.repo.BestBlockSummary().Header.Number()+10), 100, nil, tx.Features(0), acc) pool.add(trx1, false, false) txObj, err := resolveTx(trx2, false) @@ -1040,7 +1040,7 @@ func TestWashWithDynFeeTx(t *testing.T) { }{ { "MaxLife with dynFeeTx", func(t *testing.T) { - trx := newTx(tx.DynamicFeeTxType, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[len(devAccounts)-1]) + trx := newTx(tx.TypeDynamicFee, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[len(devAccounts)-1]) pool.add(trx, false, false) txObj := pool.all.mapByID[trx.ID()] @@ -1061,7 +1061,7 @@ func TestWashWithDynFeeTx(t *testing.T) { PrivateKey: priv, } - trx := newTx(tx.DynamicFeeTxType, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), acc) + trx := newTx(tx.TypeDynamicFee, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), acc) txObj, err := resolveTx(trx, false) assert.Nil(t, err) @@ -1116,9 +1116,9 @@ func TestWashWithDynFeeTxAndPoolLimit(t *testing.T) { PrivateKey: priv, } - trx1 := newTx(tx.DynamicFeeTxType, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[0]) - trx2 := newTx(tx.DynamicFeeTxType, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[0]) - trx3 := newTx(tx.DynamicFeeTxType, pool.repo.ChainTag(), nil, 21000, tx.NewBlockRef(pool.repo.BestBlockSummary().Header.Number()+10), 100, nil, tx.Features(0), acc) + trx1 := newTx(tx.TypeDynamicFee, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[0]) + trx2 := newTx(tx.TypeDynamicFee, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[0]) + trx3 := newTx(tx.TypeDynamicFee, pool.repo.ChainTag(), nil, 21000, tx.NewBlockRef(pool.repo.BestBlockSummary().Header.Number()+10), 100, nil, tx.Features(0), acc) pool.add(trx1, false, false) txObj, err := resolveTx(trx2, false) @@ -1144,9 +1144,9 @@ func TestWashWithDynFeeTxAndPoolLimit(t *testing.T) { PrivateKey: priv, } - trx1 := newTx(tx.DynamicFeeTxType, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[0]) - trx2 := newTx(tx.DynamicFeeTxType, pool.repo.ChainTag(), nil, 21000, tx.NewBlockRef(pool.repo.BestBlockSummary().Header.Number()+10), 100, nil, tx.Features(0), devAccounts[0]) - trx3 := newTx(tx.DynamicFeeTxType, pool.repo.ChainTag(), nil, 21000, tx.NewBlockRef(pool.repo.BestBlockSummary().Header.Number()+10), 100, nil, tx.Features(0), acc) + trx1 := newTx(tx.TypeDynamicFee, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[0]) + trx2 := newTx(tx.TypeDynamicFee, pool.repo.ChainTag(), nil, 21000, tx.NewBlockRef(pool.repo.BestBlockSummary().Header.Number()+10), 100, nil, tx.Features(0), devAccounts[0]) + trx3 := newTx(tx.TypeDynamicFee, pool.repo.ChainTag(), nil, 21000, tx.NewBlockRef(pool.repo.BestBlockSummary().Header.Number()+10), 100, nil, tx.Features(0), acc) pool.add(trx1, false, false) txObj, err := resolveTx(trx2, false) @@ -1233,30 +1233,30 @@ func TestAddOverPendingCost(t *testing.T) { defer pool.Close() // first and second tx should be fine - err = pool.Add(newTx(tx.LegacyTxType, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[0])) + err = pool.Add(newTx(tx.TypeLegacy, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[0])) assert.Nil(t, err) - err = pool.Add(newTx(tx.LegacyTxType, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[0])) + err = pool.Add(newTx(tx.TypeLegacy, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[0])) assert.Nil(t, err) // third tx should be rejected due to insufficient energy - err = pool.Add(newTx(tx.LegacyTxType, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[0])) + err = pool.Add(newTx(tx.TypeLegacy, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[0])) assert.EqualError(t, err, "tx rejected: insufficient energy for overall pending cost") - err = pool.Add(newTx(tx.LegacyTxType, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[0])) + err = pool.Add(newTx(tx.TypeLegacy, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[0])) assert.EqualError(t, err, "tx rejected: insufficient energy for overall pending cost") // delegated fee should also be counted - err = pool.Add(newDelegatedTx(tx.LegacyTxType, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, devAccounts[9], devAccounts[0])) + err = pool.Add(newDelegatedTx(tx.TypeLegacy, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, devAccounts[9], devAccounts[0])) assert.EqualError(t, err, "tx rejected: insufficient energy for overall pending cost") - err = pool.Add(newDelegatedTx(tx.LegacyTxType, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, devAccounts[9], devAccounts[0])) + err = pool.Add(newDelegatedTx(tx.TypeLegacy, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, devAccounts[9], devAccounts[0])) assert.EqualError(t, err, "tx rejected: insufficient energy for overall pending cost") // first and second tx should be fine - err = pool.Add(newDelegatedTx(tx.LegacyTxType, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, devAccounts[1], devAccounts[2])) + err = pool.Add(newDelegatedTx(tx.TypeLegacy, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, devAccounts[1], devAccounts[2])) assert.Nil(t, err) - err = pool.Add(newTx(tx.LegacyTxType, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[2])) + err = pool.Add(newTx(tx.TypeLegacy, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[2])) assert.Nil(t, err) // delegated fee should also be counted - err = pool.Add(newDelegatedTx(tx.LegacyTxType, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, devAccounts[8], devAccounts[2])) + err = pool.Add(newDelegatedTx(tx.TypeLegacy, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, devAccounts[8], devAccounts[2])) assert.EqualError(t, err, "tx rejected: insufficient energy for overall pending cost") - err = pool.Add(newDelegatedTx(tx.LegacyTxType, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, devAccounts[8], devAccounts[2])) + err = pool.Add(newDelegatedTx(tx.TypeLegacy, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, devAccounts[8], devAccounts[2])) assert.EqualError(t, err, "tx rejected: insufficient energy for overall pending cost") } @@ -1322,29 +1322,29 @@ func TestAddOverPendingCostDynamicFee(t *testing.T) { defer pool.Close() // first and second tx should be fine - err = pool.Add(newTx(tx.DynamicFeeTxType, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[0])) + err = pool.Add(newTx(tx.TypeDynamicFee, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[0])) assert.Nil(t, err) - err = pool.Add(newTx(tx.DynamicFeeTxType, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[0])) + err = pool.Add(newTx(tx.TypeDynamicFee, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[0])) assert.Nil(t, err) // third tx should be rejected due to insufficient energy - err = pool.Add(newTx(tx.DynamicFeeTxType, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[0])) + err = pool.Add(newTx(tx.TypeDynamicFee, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[0])) assert.EqualError(t, err, "tx rejected: insufficient energy for overall pending cost") - err = pool.Add(newTx(tx.DynamicFeeTxType, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[0])) + err = pool.Add(newTx(tx.TypeDynamicFee, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[0])) assert.EqualError(t, err, "tx rejected: insufficient energy for overall pending cost") // delegated fee should also be counted - err = pool.Add(newDelegatedTx(tx.DynamicFeeTxType, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, devAccounts[9], devAccounts[0])) + err = pool.Add(newDelegatedTx(tx.TypeDynamicFee, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, devAccounts[9], devAccounts[0])) assert.EqualError(t, err, "tx rejected: insufficient energy for overall pending cost") - err = pool.Add(newDelegatedTx(tx.DynamicFeeTxType, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, devAccounts[9], devAccounts[0])) + err = pool.Add(newDelegatedTx(tx.TypeDynamicFee, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, devAccounts[9], devAccounts[0])) assert.EqualError(t, err, "tx rejected: insufficient energy for overall pending cost") // first and second tx should be fine - err = pool.Add(newDelegatedTx(tx.DynamicFeeTxType, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, devAccounts[1], devAccounts[2])) + err = pool.Add(newDelegatedTx(tx.TypeDynamicFee, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, devAccounts[1], devAccounts[2])) assert.Nil(t, err) - err = pool.Add(newTx(tx.DynamicFeeTxType, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[2])) + err = pool.Add(newTx(tx.TypeDynamicFee, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), devAccounts[2])) assert.Nil(t, err) // delegated fee should also be counted - err = pool.Add(newDelegatedTx(tx.DynamicFeeTxType, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, devAccounts[8], devAccounts[2])) + err = pool.Add(newDelegatedTx(tx.TypeDynamicFee, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, devAccounts[8], devAccounts[2])) assert.EqualError(t, err, "tx rejected: insufficient energy for overall pending cost") - err = pool.Add(newDelegatedTx(tx.DynamicFeeTxType, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, devAccounts[8], devAccounts[2])) + err = pool.Add(newDelegatedTx(tx.TypeDynamicFee, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, devAccounts[8], devAccounts[2])) assert.EqualError(t, err, "tx rejected: insufficient energy for overall pending cost") } diff --git a/txpool/validation.go b/txpool/validation.go index a5d9f98b2..08c154a91 100644 --- a/txpool/validation.go +++ b/txpool/validation.go @@ -25,7 +25,7 @@ func ValidateTransaction(tr *tx.Transaction, repo *chain.Repository, head *chain } if head.Header.Number() < forkConfig.GALACTICA { // Pre-Galactica, only support legacy tx - if tr.Type() != tx.LegacyTxType { + if tr.Type() != tx.TypeLegacy { return tx.ErrTxTypeNotSupported } } else { diff --git a/txpool/validation_test.go b/txpool/validation_test.go index 120ef1820..28ddb525f 100644 --- a/txpool/validation_test.go +++ b/txpool/validation_test.go @@ -34,14 +34,14 @@ func TestValidateTransaction(t *testing.T) { }{ { name: "invalid legacy tx chain tag", - getTx: func() *tx.Transaction { t, _ := tx.NewTxBuilder(tx.LegacyTxType).ChainTag(0xff).Build(); return t }, + getTx: func() *tx.Transaction { t, _ := tx.NewTxBuilder(tx.TypeLegacy).ChainTag(0xff).Build(); return t }, head: &chain.BlockSummary{}, forkConfig: &thor.NoFork, expectedErr: badTxError{"chain tag mismatch"}, }, { name: "invalid dyn fee tx chain tag", - getTx: func() *tx.Transaction { t, _ := tx.NewTxBuilder(tx.DynamicFeeTxType).ChainTag(0xff).Build(); return t }, + getTx: func() *tx.Transaction { t, _ := tx.NewTxBuilder(tx.TypeDynamicFee).ChainTag(0xff).Build(); return t }, head: &chain.BlockSummary{}, forkConfig: &thor.NoFork, expectedErr: badTxError{"chain tag mismatch"}, @@ -49,7 +49,7 @@ func TestValidateTransaction(t *testing.T) { { name: "legacy tx size too large", getTx: func() *tx.Transaction { - b := tx.NewTxBuilder(tx.LegacyTxType).ChainTag(repo.ChainTag()) + b := tx.NewTxBuilder(tx.TypeLegacy).ChainTag(repo.ChainTag()) // Including a lot of clauses to increase the size above the max allowed for i := 0; i < 50_000; i++ { b.Clause(&tx.Clause{}) @@ -64,7 +64,7 @@ func TestValidateTransaction(t *testing.T) { { name: "dyn fee tx size too large", getTx: func() *tx.Transaction { - b := tx.NewTxBuilder(tx.DynamicFeeTxType).ChainTag(repo.ChainTag()) + b := tx.NewTxBuilder(tx.TypeDynamicFee).ChainTag(repo.ChainTag()) // Including a lot of clauses to increase the size above the max allowed for i := 0; i < 50_000; i++ { b.Clause(&tx.Clause{}) @@ -79,7 +79,7 @@ func TestValidateTransaction(t *testing.T) { { name: "supported legacy transaction type before Galactica fork", getTx: func() *tx.Transaction { - t, _ := tx.NewTxBuilder(tx.LegacyTxType).ChainTag(repo.ChainTag()).Build() + t, _ := tx.NewTxBuilder(tx.TypeLegacy).ChainTag(repo.ChainTag()).Build() return t }, head: &chain.BlockSummary{Header: getHeader(1)}, @@ -89,7 +89,7 @@ func TestValidateTransaction(t *testing.T) { { name: "supported legacy transaction type after Galactica fork", getTx: func() *tx.Transaction { - t, _ := tx.NewTxBuilder(tx.LegacyTxType).ChainTag(repo.ChainTag()).Build() + t, _ := tx.NewTxBuilder(tx.TypeLegacy).ChainTag(repo.ChainTag()).Build() return t }, head: &chain.BlockSummary{Header: getHeader(100)}, @@ -99,7 +99,7 @@ func TestValidateTransaction(t *testing.T) { { name: "unsupported dyn fee transaction type before Galactica fork", getTx: func() *tx.Transaction { - t, _ := tx.NewTxBuilder(tx.DynamicFeeTxType).ChainTag(repo.ChainTag()).Build() + t, _ := tx.NewTxBuilder(tx.TypeDynamicFee).ChainTag(repo.ChainTag()).Build() return t }, head: &chain.BlockSummary{Header: getHeader(1)}, @@ -109,7 +109,7 @@ func TestValidateTransaction(t *testing.T) { { name: "supported dyn fee transaction type after Galactica fork", getTx: func() *tx.Transaction { - t, _ := tx.NewTxBuilder(tx.DynamicFeeTxType).ChainTag(repo.ChainTag()).MaxFeePerGas(big.NewInt(1000)).MaxPriorityFeePerGas(big.NewInt(10)).Build() + t, _ := tx.NewTxBuilder(tx.TypeDynamicFee).ChainTag(repo.ChainTag()).MaxFeePerGas(big.NewInt(1000)).MaxPriorityFeePerGas(big.NewInt(10)).Build() return t }, head: &chain.BlockSummary{Header: getHeader(100)}, @@ -119,7 +119,7 @@ func TestValidateTransaction(t *testing.T) { { name: "legacy transaction with unsupported features", getTx: func() *tx.Transaction { - t, _ := tx.NewTxBuilder(tx.LegacyTxType).ChainTag(repo.ChainTag()).Features(tx.Features(4)).Build() + t, _ := tx.NewTxBuilder(tx.TypeLegacy).ChainTag(repo.ChainTag()).Features(tx.Features(4)).Build() return t }, head: &chain.BlockSummary{Header: new(block.Builder).TransactionFeatures(tx.Features(1)).Build().Header()}, @@ -129,7 +129,7 @@ func TestValidateTransaction(t *testing.T) { { name: "dyn fee transaction with unsupported features", getTx: func() *tx.Transaction { - t, _ := tx.NewTxBuilder(tx.DynamicFeeTxType).ChainTag(repo.ChainTag()).MaxFeePerGas(big.NewInt(10_000)).MaxPriorityFeePerGas(big.NewInt(100)).Features(tx.Features(4)).Build() + t, _ := tx.NewTxBuilder(tx.TypeDynamicFee).ChainTag(repo.ChainTag()).MaxFeePerGas(big.NewInt(10_000)).MaxPriorityFeePerGas(big.NewInt(100)).Features(tx.Features(4)).Build() return t }, head: &chain.BlockSummary{Header: new(block.Builder).TransactionFeatures(tx.Features(1)).Build().Header()}, @@ -139,7 +139,7 @@ func TestValidateTransaction(t *testing.T) { { name: "legacy transaction with supported features", getTx: func() *tx.Transaction { - t, _ := tx.NewTxBuilder(tx.LegacyTxType).ChainTag(repo.ChainTag()).Features(tx.DelegationFeature).Build() + t, _ := tx.NewTxBuilder(tx.TypeLegacy).ChainTag(repo.ChainTag()).Features(tx.DelegationFeature).Build() return t }, head: &chain.BlockSummary{Header: getHeader(1)}, @@ -149,7 +149,7 @@ func TestValidateTransaction(t *testing.T) { { name: "dyn fee transaction with supported features", getTx: func() *tx.Transaction { - t, _ := tx.NewTxBuilder(tx.DynamicFeeTxType).ChainTag(repo.ChainTag()).Features(tx.DelegationFeature).MaxFeePerGas(big.NewInt(1000)).MaxPriorityFeePerGas(big.NewInt(10)).Build() + t, _ := tx.NewTxBuilder(tx.TypeDynamicFee).ChainTag(repo.ChainTag()).Features(tx.DelegationFeature).MaxFeePerGas(big.NewInt(1000)).MaxPriorityFeePerGas(big.NewInt(10)).Build() return t }, head: &chain.BlockSummary{Header: getHeader(1)}, @@ -159,7 +159,7 @@ func TestValidateTransaction(t *testing.T) { { name: "max fee per gas less than max priority fee per gas", getTx: func() *tx.Transaction { - t, _ := tx.NewTxBuilder(tx.DynamicFeeTxType).ChainTag(repo.ChainTag()).MaxFeePerGas(big.NewInt(10)).MaxPriorityFeePerGas(big.NewInt(100)).Build() + t, _ := tx.NewTxBuilder(tx.TypeDynamicFee).ChainTag(repo.ChainTag()).MaxFeePerGas(big.NewInt(10)).MaxPriorityFeePerGas(big.NewInt(100)).Build() return t }, head: &chain.BlockSummary{Header: getHeader(1)}, @@ -169,7 +169,7 @@ func TestValidateTransaction(t *testing.T) { { name: "max fee per gas is negative", getTx: func() *tx.Transaction { - t, _ := tx.NewTxBuilder(tx.DynamicFeeTxType).ChainTag(repo.ChainTag()).MaxFeePerGas(big.NewInt(-10)).MaxPriorityFeePerGas(big.NewInt(-100)).Build() + t, _ := tx.NewTxBuilder(tx.TypeDynamicFee).ChainTag(repo.ChainTag()).MaxFeePerGas(big.NewInt(-10)).MaxPriorityFeePerGas(big.NewInt(-100)).Build() return t }, head: &chain.BlockSummary{Header: getHeader(1)}, @@ -179,7 +179,7 @@ func TestValidateTransaction(t *testing.T) { { name: "max priority fee per gas is negative", getTx: func() *tx.Transaction { - t, _ := tx.NewTxBuilder(tx.DynamicFeeTxType).ChainTag(repo.ChainTag()).MaxFeePerGas(big.NewInt(10)).MaxPriorityFeePerGas(big.NewInt(-100)).Build() + t, _ := tx.NewTxBuilder(tx.TypeDynamicFee).ChainTag(repo.ChainTag()).MaxFeePerGas(big.NewInt(10)).MaxPriorityFeePerGas(big.NewInt(-100)).Build() return t }, head: &chain.BlockSummary{Header: getHeader(1)}, @@ -189,7 +189,7 @@ func TestValidateTransaction(t *testing.T) { { name: "max fee per gas is exceding 256 bits", getTx: func() *tx.Transaction { - t, _ := tx.NewTxBuilder(tx.DynamicFeeTxType).ChainTag(repo.ChainTag()).MaxFeePerGas(new(big.Int).Lsh(big.NewInt(1), 257)).MaxPriorityFeePerGas(big.NewInt(10)).Build() + t, _ := tx.NewTxBuilder(tx.TypeDynamicFee).ChainTag(repo.ChainTag()).MaxFeePerGas(new(big.Int).Lsh(big.NewInt(1), 257)).MaxPriorityFeePerGas(big.NewInt(10)).Build() return t }, head: &chain.BlockSummary{Header: getHeader(1)}, @@ -199,7 +199,7 @@ func TestValidateTransaction(t *testing.T) { { name: "max fee per gas is nil", getTx: func() *tx.Transaction { - t, _ := tx.NewTxBuilder(tx.DynamicFeeTxType).ChainTag(repo.ChainTag()).MaxPriorityFeePerGas(big.NewInt(10)).Build() + t, _ := tx.NewTxBuilder(tx.TypeDynamicFee).ChainTag(repo.ChainTag()).MaxPriorityFeePerGas(big.NewInt(10)).Build() return t }, head: &chain.BlockSummary{Header: getHeader(1)}, @@ -209,7 +209,7 @@ func TestValidateTransaction(t *testing.T) { { name: "max priority fee per gas is nil", getTx: func() *tx.Transaction { - t, _ := tx.NewTxBuilder(tx.DynamicFeeTxType).ChainTag(repo.ChainTag()).MaxFeePerGas(big.NewInt(10)).Build() + t, _ := tx.NewTxBuilder(tx.TypeDynamicFee).ChainTag(repo.ChainTag()).MaxFeePerGas(big.NewInt(10)).Build() return t }, head: &chain.BlockSummary{Header: getHeader(1)}, @@ -243,7 +243,7 @@ func TestValidateTransactionWithState(t *testing.T) { name: "dyn fee tx with not enough fee to pay for base fee", getTx: func() *tx.Transaction { maxFee := big.NewInt(thor.InitialBaseFee - 1) - t, _ := tx.NewTxBuilder(tx.DynamicFeeTxType).ChainTag(repo.ChainTag()).MaxFeePerGas(maxFee).Build() + t, _ := tx.NewTxBuilder(tx.TypeDynamicFee).ChainTag(repo.ChainTag()).MaxFeePerGas(maxFee).Build() return t }, head: &chain.BlockSummary{Header: getHeader(1)}, @@ -254,7 +254,7 @@ func TestValidateTransactionWithState(t *testing.T) { name: "dyn fee tx with max fee equals to base fee + 1", getTx: func() *tx.Transaction { maxFee := big.NewInt(thor.InitialBaseFee + 1) - t, _ := tx.NewTxBuilder(tx.DynamicFeeTxType).ChainTag(repo.ChainTag()).MaxFeePerGas(maxFee).Build() + t, _ := tx.NewTxBuilder(tx.TypeDynamicFee).ChainTag(repo.ChainTag()).MaxFeePerGas(maxFee).Build() return t }, head: &chain.BlockSummary{Header: getHeader(1)}, @@ -264,7 +264,7 @@ func TestValidateTransactionWithState(t *testing.T) { { name: "legacy tx with gasPriceCoef 0", getTx: func() *tx.Transaction { - t, _ := tx.NewTxBuilder(tx.LegacyTxType).ChainTag(repo.ChainTag()).GasPriceCoef(0).Build() + t, _ := tx.NewTxBuilder(tx.TypeLegacy).ChainTag(repo.ChainTag()).GasPriceCoef(0).Build() return t }, head: &chain.BlockSummary{Header: getHeader(1)}, @@ -274,7 +274,7 @@ func TestValidateTransactionWithState(t *testing.T) { { name: "dyn fee tx not accepted with maxFeePerGas equals to zero", getTx: func() *tx.Transaction { - t, _ := tx.NewTxBuilder(tx.DynamicFeeTxType).ChainTag(repo.ChainTag()).MaxFeePerGas(common.Big0).Build() + t, _ := tx.NewTxBuilder(tx.TypeDynamicFee).ChainTag(repo.ChainTag()).MaxFeePerGas(common.Big0).Build() return t }, head: &chain.BlockSummary{Header: getHeader(1)}, @@ -285,7 +285,7 @@ func TestValidateTransactionWithState(t *testing.T) { name: "dyn fee tx with maxPriorityFeePerGas = 0, maxFeePerGas > baseFee", getTx: func() *tx.Transaction { baseFee := new(big.Int).Add(getHeader(1).BaseFee(), common.Big1) - t, _ := tx.NewTxBuilder(tx.DynamicFeeTxType).ChainTag(repo.ChainTag()).MaxFeePerGas(baseFee).MaxPriorityFeePerGas(common.Big0).Build() + t, _ := tx.NewTxBuilder(tx.TypeDynamicFee).ChainTag(repo.ChainTag()).MaxFeePerGas(baseFee).MaxPriorityFeePerGas(common.Big0).Build() return t }, head: &chain.BlockSummary{Header: getHeader(1)}, @@ -295,7 +295,7 @@ func TestValidateTransactionWithState(t *testing.T) { { name: "dyn fee tx with maxPriorityFeePerGas = 0, maxFeePerGas > baseFee", getTx: func() *tx.Transaction { - t, _ := tx.NewTxBuilder(tx.DynamicFeeTxType).ChainTag(repo.ChainTag()).MaxFeePerGas(getHeader(1).BaseFee()).MaxPriorityFeePerGas(common.Big0).Build() + t, _ := tx.NewTxBuilder(tx.TypeDynamicFee).ChainTag(repo.ChainTag()).MaxFeePerGas(getHeader(1).BaseFee()).MaxPriorityFeePerGas(common.Big0).Build() return t }, head: &chain.BlockSummary{Header: getHeader(1)}, @@ -306,7 +306,7 @@ func TestValidateTransactionWithState(t *testing.T) { name: "dyn fee tx with maxPriorityFeePerGas = 0, maxFeePerGas > baseFee", getTx: func() *tx.Transaction { baseFee := new(big.Int).Sub(getHeader(1).BaseFee(), common.Big1) - t, _ := tx.NewTxBuilder(tx.DynamicFeeTxType).ChainTag(repo.ChainTag()).MaxFeePerGas(baseFee).MaxPriorityFeePerGas(common.Big0).Build() + t, _ := tx.NewTxBuilder(tx.TypeDynamicFee).ChainTag(repo.ChainTag()).MaxFeePerGas(baseFee).MaxPriorityFeePerGas(common.Big0).Build() return t }, head: &chain.BlockSummary{Header: getHeader(1)},