From d17ce93ad4f21618b5275e826687aa703c41ee90 Mon Sep 17 00:00:00 2001 From: Dusan Nosovic Date: Thu, 8 Feb 2024 13:18:37 +0100 Subject: [PATCH] comment fix --- blockchain/blockchain_test.go | 19 +----- jsonrpc/helper.go | 46 ++++---------- state/transition_test.go | 2 +- txpool/txpool.go | 9 +-- txpool/txpool_test.go | 49 ++++---------- types/access_list_tx.go | 113 ++++++++++++++++++--------------- types/dynamic_fee_tx.go | 59 ++--------------- types/legacy_tx.go | 12 ++-- types/rlp_encoding_test.go | 4 +- types/rlp_marshal.go | 2 +- types/rlp_marshal_storage.go | 2 +- types/rlp_unmarshal.go | 6 +- types/rlp_unmarshal_storage.go | 5 +- types/state_tx.go | 12 ++-- types/transaction.go | 16 +++++ 15 files changed, 134 insertions(+), 222 deletions(-) diff --git a/blockchain/blockchain_test.go b/blockchain/blockchain_test.go index cb3ad4e6f1..68651af31b 100644 --- a/blockchain/blockchain_test.go +++ b/blockchain/blockchain_test.go @@ -1872,8 +1872,6 @@ func customJSONBlockUnmarshall(tb testing.TB, jsonData []byte) (*types.FullBlock for _, transactionJSON := range transactionsJSON { tr := transactionJSON.(map[string]interface{}) - var txData types.TxData - txType := tr["type"].(string) txTypeNumber, err := common.ParseUint64orHex(&txType) @@ -1881,22 +1879,7 @@ func customJSONBlockUnmarshall(tb testing.TB, jsonData []byte) (*types.FullBlock return nil, err } - switch types.TxType(txTypeNumber) { - case types.AccessListTxType: - txData = &types.AccessListTxn{} - case types.StateTxType: - txData = &types.DynamicFeeTx{} - case types.LegacyTxType: - txData = &types.LegacyTx{} - default: - txData = &types.DynamicFeeTx{} - } - - if txData == nil { - continue - } - - transaction := types.NewTx(txData) + transaction := types.NewTxWithType(types.TxType(txTypeNumber)) transaction.SetHash(types.StringToHash(tr["hash"].(string))) nonce := tr["nonce"].(string) diff --git a/jsonrpc/helper.go b/jsonrpc/helper.go index ca711b6bcf..618739b548 100644 --- a/jsonrpc/helper.go +++ b/jsonrpc/helper.go @@ -163,8 +163,6 @@ func GetNextNonce(address types.Address, number BlockNumber, store nonceGetter) } func DecodeTxn(arg *txnArgs, blockNumber uint64, store nonceGetter, forceSetNonce bool) (*types.Transaction, error) { - var txn *types.Transaction - if arg == nil { return nil, errors.New("missing value for required argument 0") } @@ -221,44 +219,22 @@ func DecodeTxn(arg *txnArgs, blockNumber uint64, store nonceGetter, forceSetNonc txType = types.TxType(*arg.Type) } + txn := types.NewTxWithType(txType) + switch txType { - case types.AccessListTxType: - txn = types.NewTx(&types.AccessListTxn{ - From: *arg.From, - Gas: uint64(*arg.Gas), - Value: new(big.Int).SetBytes(*arg.Value), - Input: input, - Nonce: uint64(*arg.Nonce), - }) case types.LegacyTxType: - txn = types.NewTx(&types.LegacyTx{ - From: *arg.From, - Gas: uint64(*arg.Gas), - Value: new(big.Int).SetBytes(*arg.Value), - Input: input, - Nonce: uint64(*arg.Nonce), - GasPrice: new(big.Int).SetBytes(*arg.GasPrice), - }) - case types.StateTxType: - txn = types.NewTx(&types.StateTx{ - From: *arg.From, - Gas: uint64(*arg.Gas), - Value: new(big.Int).SetBytes(*arg.Value), - Input: input, - Nonce: uint64(*arg.Nonce), - }) + txn.SetGasPrice(new(big.Int).SetBytes(*arg.GasPrice)) case types.DynamicFeeTxType: - txn = types.NewTx(&types.DynamicFeeTx{ - From: *arg.From, - Gas: uint64(*arg.Gas), - GasTipCap: new(big.Int).SetBytes(*arg.GasTipCap), - GasFeeCap: new(big.Int).SetBytes(*arg.GasFeeCap), - Value: new(big.Int).SetBytes(*arg.Value), - Input: input, - Nonce: uint64(*arg.Nonce), - }) + txn.SetGasTipCap(new(big.Int).SetBytes(*arg.GasTipCap)) + txn.SetGasFeeCap(new(big.Int).SetBytes(*arg.GasFeeCap)) } + txn.SetFrom(*arg.From) + txn.SetGas(uint64(*arg.Gas)) + txn.SetValue(new(big.Int).SetBytes(*arg.Value)) + txn.SetInput(input) + txn.SetNonce(uint64(*arg.Nonce)) + if arg.To != nil { txn.SetTo(arg.To) } diff --git a/state/transition_test.go b/state/transition_test.go index 55ffa36af6..4c63e3823f 100644 --- a/state/transition_test.go +++ b/state/transition_test.go @@ -69,7 +69,7 @@ func TestSubGasLimitPrice(t *testing.T) { t.Parallel() transition := newTestTransition(tt.preState) - msg := types.NewTx(&types.StateTx{ + msg := types.NewTx(&types.LegacyTx{ From: tt.from, Gas: tt.gas, GasPrice: big.NewInt(tt.gasPrice), diff --git a/txpool/txpool.go b/txpool/txpool.go index 129e700879..3b6adec4cd 100644 --- a/txpool/txpool.go +++ b/txpool/txpool.go @@ -670,13 +670,10 @@ func (p *TxPool) validateTx(tx *types.Transaction) error { } } - // Check if the given tx is not underpriced - if tx.Type() != types.DynamicFeeTxType { - if tx.GetGasPrice(baseFee).Cmp(new(big.Int).SetUint64(p.priceLimit)) < 0 { - metrics.IncrCounter([]string{txPoolMetrics, "underpriced_tx"}, 1) + if tx.GetGasPrice(baseFee).Cmp(new(big.Int).SetUint64(p.priceLimit)) < 0 { + metrics.IncrCounter([]string{txPoolMetrics, "underpriced_tx"}, 1) - return ErrUnderpriced - } + return ErrUnderpriced } // Check nonce ordering diff --git a/txpool/txpool_test.go b/txpool/txpool_test.go index ce53303d92..72fc1af1f4 100644 --- a/txpool/txpool_test.go +++ b/txpool/txpool_test.go @@ -56,48 +56,23 @@ func newTx(addr types.Address, nonce, slots uint64, txType types.TxType) *types. return nil } - var tx *types.Transaction + tx := types.NewTxWithType(txType) switch txType { - case types.AccessListTxType: - tx = types.NewTx(&types.AccessListTxn{ - From: addr, - Nonce: nonce, - Value: big.NewInt(1), - GasPrice: big.NewInt(0).SetUint64(defaultPriceLimit), - Gas: validGasLimit, - Input: input, - }) - case types.LegacyTxType: - tx = types.NewTx(&types.LegacyTx{ - From: addr, - Nonce: nonce, - Value: big.NewInt(1), - GasPrice: big.NewInt(0).SetUint64(defaultPriceLimit), - Gas: validGasLimit, - Input: input, - }) - case types.StateTxType: - tx = types.NewTx(&types.StateTx{ - From: addr, - Nonce: nonce, - Value: big.NewInt(1), - GasPrice: big.NewInt(0).SetUint64(defaultPriceLimit), - Gas: validGasLimit, - Input: input, - }) + case types.DynamicFeeTxType: + tx.SetGasFeeCap(big.NewInt(100)) + tx.SetGasTipCap(big.NewInt(100)) + default: - tx = types.NewTx(&types.DynamicFeeTx{ - From: addr, - Nonce: nonce, - Value: big.NewInt(1), - GasTipCap: big.NewInt(100), - GasFeeCap: big.NewInt(100), - Gas: validGasLimit, - Input: input, - }) + tx.SetGasPrice(big.NewInt(0).SetUint64(defaultPriceLimit)) } + tx.SetFrom(addr) + tx.SetNonce(nonce) + tx.SetValue(big.NewInt(1)) + tx.SetGas(validGasLimit) + tx.SetInput(input) + return tx } diff --git a/types/access_list_tx.go b/types/access_list_tx.go index 79193b4994..44baf033ce 100644 --- a/types/access_list_tx.go +++ b/types/access_list_tx.go @@ -45,6 +45,64 @@ func (al TxAccessList) Copy() TxAccessList { return newAccessList } +func (al TxAccessList) unmarshallRLPFrom(p *fastrlp.Parser, accessListVV []*fastrlp.Value) error { + for i, accessTupleVV := range accessListVV { + accessTupleElems, err := accessTupleVV.GetElems() + if err != nil { + return err + } + + // Read the address + addressVV := accessTupleElems[0] + + addressBytes, err := addressVV.Bytes() + if err != nil { + return err + } + + al[i].Address = BytesToAddress(addressBytes) + + // Read the storage keys + storageKeysArrayVV := accessTupleElems[1] + + storageKeysElems, err := storageKeysArrayVV.GetElems() + if err != nil { + return err + } + + al[i].StorageKeys = make([]Hash, len(storageKeysElems)) + + for j, storageKeyVV := range storageKeysElems { + storageKeyBytes, err := storageKeyVV.Bytes() + if err != nil { + return err + } + + al[i].StorageKeys[j] = BytesToHash(storageKeyBytes) + } + } + + return nil +} + +func (al TxAccessList) marshallRLPWith(arena *fastrlp.Arena) *fastrlp.Value { + accessListVV := arena.NewArray() + + for _, accessTuple := range al { + accessTupleVV := arena.NewArray() + accessTupleVV.Set(arena.NewCopyBytes(accessTuple.Address.Bytes())) + + storageKeysVV := arena.NewArray() + for _, storageKey := range accessTuple.StorageKeys { + storageKeysVV.Set(arena.NewCopyBytes(storageKey.Bytes())) + } + + accessTupleVV.Set(storageKeysVV) + accessListVV.Set(accessTupleVV) + } + return accessListVV +} + type AccessListTxn struct { Nonce uint64 GasPrice *big.Int @@ -233,41 +291,7 @@ func (tx *AccessListTxn) unmarshalRLPFrom(p *fastrlp.Parser, v *fastrlp.Value) e txAccessList = make(TxAccessList, len(accessListVV)) } - for i, accessTupleVV := range accessListVV { - accessTupleElems, err := accessTupleVV.GetElems() - if err != nil { - return err - } - - // Read the address - addressVV := accessTupleElems[0] - - addressBytes, err := addressVV.Bytes() - if err != nil { - return err - } - - txAccessList[i].Address = BytesToAddress(addressBytes) - - // Read the storage keys - storageKeysArrayVV := accessTupleElems[1] - - storageKeysElems, err := storageKeysArrayVV.GetElems() - if err != nil { - return err - } - - txAccessList[i].StorageKeys = make([]Hash, len(storageKeysElems)) - - for j, storageKeyVV := range storageKeysElems { - storageKeyBytes, err := storageKeyVV.Bytes() - if err != nil { - return err - } - - txAccessList[i].StorageKeys[j] = BytesToHash(storageKeyBytes) - } - } + txAccessList.unmarshallRLPFrom(p, accessListVV) tx.setAccessList(txAccessList) @@ -315,23 +339,8 @@ func (tx *AccessListTxn) marshalRLPWith(arena *fastrlp.Arena) *fastrlp.Value { vv.Set(arena.NewBigInt(tx.value())) vv.Set(arena.NewCopyBytes(tx.input())) - // add accessList - accessListVV := arena.NewArray() - - for _, accessTuple := range tx.accessList() { - accessTupleVV := arena.NewArray() - accessTupleVV.Set(arena.NewCopyBytes(accessTuple.Address.Bytes())) - - storageKeysVV := arena.NewArray() - for _, storageKey := range accessTuple.StorageKeys { - storageKeysVV.Set(arena.NewCopyBytes(storageKey.Bytes())) - } - - accessTupleVV.Set(storageKeysVV) - accessListVV.Set(accessTupleVV) - } - - vv.Set(accessListVV) + // Convert TxAccessList to RLP format and add it to the vv array. + vv.Set(tx.accessList().marshallRLPWith(arena)) v, r, s := tx.rawSignatureValues() vv.Set(arena.NewBigInt(v)) diff --git a/types/dynamic_fee_tx.go b/types/dynamic_fee_tx.go index 78e5e5e951..4f360c822f 100644 --- a/types/dynamic_fee_tx.go +++ b/types/dynamic_fee_tx.go @@ -27,7 +27,7 @@ func (tx *DynamicFeeTx) transactionType() TxType { return DynamicFeeTxType } func (tx *DynamicFeeTx) chainID() *big.Int { return tx.ChainID } func (tx *DynamicFeeTx) input() []byte { return tx.Input } func (tx *DynamicFeeTx) gas() uint64 { return tx.Gas } -func (tx *DynamicFeeTx) gasPrice() *big.Int { return nil } +func (tx *DynamicFeeTx) gasPrice() *big.Int { return tx.GasFeeCap } func (tx *DynamicFeeTx) gasTipCap() *big.Int { return tx.GasTipCap } func (tx *DynamicFeeTx) gasFeeCap() *big.Int { return tx.GasFeeCap } func (tx *DynamicFeeTx) value() *big.Int { return tx.Value } @@ -57,7 +57,9 @@ func (tx *DynamicFeeTx) setChainID(id *big.Int) { tx.ChainID = id } -func (tx *DynamicFeeTx) setGasPrice(gas *big.Int) {} +func (tx *DynamicFeeTx) setGasPrice(gas *big.Int) { + tx.GasTipCap = gas +} func (tx *DynamicFeeTx) setGasFeeCap(gas *big.Int) { tx.GasFeeCap = gas @@ -190,41 +192,7 @@ func (tx *DynamicFeeTx) unmarshalRLPFrom(p *fastrlp.Parser, v *fastrlp.Value) er txAccessList = make(TxAccessList, len(accessListVV)) } - for i, accessTupleVV := range accessListVV { - accessTupleElems, err := accessTupleVV.GetElems() - if err != nil { - return err - } - - // Read the address - addressVV := accessTupleElems[0] - - addressBytes, err := addressVV.Bytes() - if err != nil { - return err - } - - txAccessList[i].Address = BytesToAddress(addressBytes) - - // Read the storage keys - storageKeysArrayVV := accessTupleElems[1] - - storageKeysElems, err := storageKeysArrayVV.GetElems() - if err != nil { - return err - } - - txAccessList[i].StorageKeys = make([]Hash, len(storageKeysElems)) - - for j, storageKeyVV := range storageKeysElems { - storageKeyBytes, err := storageKeyVV.Bytes() - if err != nil { - return err - } - - txAccessList[i].StorageKeys[j] = BytesToHash(storageKeyBytes) - } - } + txAccessList.unmarshallRLPFrom(p, accessListVV) tx.setAccessList(txAccessList) @@ -276,22 +244,7 @@ func (tx *DynamicFeeTx) marshalRLPWith(arena *fastrlp.Arena) *fastrlp.Value { vv.Set(arena.NewCopyBytes(tx.input())) // Convert TxAccessList to RLP format and add it to the vv array. - accessListVV := arena.NewArray() - - for _, accessTuple := range tx.accessList() { - accessTupleVV := arena.NewArray() - accessTupleVV.Set(arena.NewCopyBytes(accessTuple.Address.Bytes())) - - storageKeysVV := arena.NewArray() - for _, storageKey := range accessTuple.StorageKeys { - storageKeysVV.Set(arena.NewCopyBytes(storageKey.Bytes())) - } - - accessTupleVV.Set(storageKeysVV) - accessListVV.Set(accessTupleVV) - } - - vv.Set(accessListVV) + vv.Set(tx.accessList().marshallRLPWith(arena)) // signature values v, r, s := tx.rawSignatureValues() diff --git a/types/legacy_tx.go b/types/legacy_tx.go index e01df22caa..f3fff8a206 100644 --- a/types/legacy_tx.go +++ b/types/legacy_tx.go @@ -24,8 +24,8 @@ func (tx *LegacyTx) chainID() *big.Int { return nil } func (tx *LegacyTx) input() []byte { return tx.Input } func (tx *LegacyTx) gas() uint64 { return tx.Gas } func (tx *LegacyTx) gasPrice() *big.Int { return tx.GasPrice } -func (tx *LegacyTx) gasTipCap() *big.Int { return nil } -func (tx *LegacyTx) gasFeeCap() *big.Int { return nil } +func (tx *LegacyTx) gasTipCap() *big.Int { return tx.GasPrice } +func (tx *LegacyTx) gasFeeCap() *big.Int { return tx.GasPrice } func (tx *LegacyTx) value() *big.Int { return tx.Value } func (tx *LegacyTx) nonce() uint64 { return tx.Nonce } func (tx *LegacyTx) to() *Address { return tx.To } @@ -56,9 +56,13 @@ func (tx *LegacyTx) setGasPrice(gas *big.Int) { tx.GasPrice = gas } -func (tx *LegacyTx) setGasFeeCap(gas *big.Int) {} +func (tx *LegacyTx) setGasFeeCap(gas *big.Int) { + tx.GasPrice = gas +} -func (tx *LegacyTx) setGasTipCap(gas *big.Int) {} +func (tx *LegacyTx) setGasTipCap(gas *big.Int) { + tx.GasPrice = gas +} func (tx *LegacyTx) setTransactionType(t TxType) {} diff --git a/types/rlp_encoding_test.go b/types/rlp_encoding_test.go index 3190de7242..db334ec809 100644 --- a/types/rlp_encoding_test.go +++ b/types/rlp_encoding_test.go @@ -276,9 +276,7 @@ func TestRLPMarshall_Unmarshall_Missing_Data(t *testing.T) { v, err := parser.Parse(testData) assert.Nil(t, err) - unmarshalledTx := &Transaction{} - - unmarshalledTx.InitInnerData(txType) + unmarshalledTx := NewTxWithType(txType) if tt.expectedErr { assert.Error(t, unmarshalledTx.Inner.unmarshalRLPFrom(parser, v), tt.name) diff --git a/types/rlp_marshal.go b/types/rlp_marshal.go index af27c344d2..0a106d79eb 100644 --- a/types/rlp_marshal.go +++ b/types/rlp_marshal.go @@ -184,5 +184,5 @@ func (t *Transaction) MarshalRLPTo(dst []byte) []byte { dst = append(dst, byte(t.Type())) } - return MarshalRLPTo(t.Inner.marshalRLPWith, dst) + return MarshalRLPTo(t.MarshalRLPWith, dst) } diff --git a/types/rlp_marshal_storage.go b/types/rlp_marshal_storage.go index a38152fa67..6d11d009ab 100644 --- a/types/rlp_marshal_storage.go +++ b/types/rlp_marshal_storage.go @@ -49,7 +49,7 @@ func (t *Transaction) marshalStoreRLPWith(a *fastrlp.Arena) *fastrlp.Value { } // consensus part - vv.Set(t.Inner.marshalRLPWith(a)) + vv.Set(t.MarshalRLPWith(a)) // context part vv.Set(a.NewBytes(t.From().Bytes())) diff --git a/types/rlp_unmarshal.go b/types/rlp_unmarshal.go index 107cc63451..197b47da0f 100644 --- a/types/rlp_unmarshal.go +++ b/types/rlp_unmarshal.go @@ -104,9 +104,7 @@ func (b *Block) unmarshalRLPFrom(p *fastrlp.Parser, v *fastrlp.Value) error { // transactions if err = unmarshalRLPFrom(p, elems[1], func(txType TxType, p *fastrlp.Parser, v *fastrlp.Value) error { - bTxn := &Transaction{} - - bTxn.InitInnerData(txType) + bTxn := NewTxWithType(txType) if err = bTxn.Inner.unmarshalRLPFrom(p, v); err != nil { return err @@ -379,7 +377,7 @@ func (t *Transaction) UnmarshalRLP(input []byte) error { t.InitInnerData(txType) - if err := UnmarshalRlp(t.Inner.unmarshalRLPFrom, input[offset:]); err != nil { + if err := UnmarshalRlp(t.UnmarshalRLPFrom, input[offset:]); err != nil { return err } diff --git a/types/rlp_unmarshal_storage.go b/types/rlp_unmarshal_storage.go index d82672289e..e3711f3776 100644 --- a/types/rlp_unmarshal_storage.go +++ b/types/rlp_unmarshal_storage.go @@ -29,8 +29,7 @@ func (b *Body) unmarshalRLPFrom(p *fastrlp.Parser, v *fastrlp.Value) error { // transactions if err = unmarshalRLPFrom(p, tuple[0], func(txType TxType, p *fastrlp.Parser, v *fastrlp.Value) error { - bTxn := &Transaction{} - bTxn.InitInnerData(txType) + bTxn := NewTxWithType(txType) if err = bTxn.unmarshalStoreRLPFrom(p, v); err != nil { return err @@ -102,7 +101,7 @@ func (t *Transaction) unmarshalStoreRLPFrom(p *fastrlp.Parser, v *fastrlp.Value) } // consensus part - if err = t.Inner.unmarshalRLPFrom(p, elems[0]); err != nil { + if err = t.UnmarshalRLPFrom(p, elems[0]); err != nil { return err } diff --git a/types/state_tx.go b/types/state_tx.go index e67ebda8e9..ffc1cbb883 100644 --- a/types/state_tx.go +++ b/types/state_tx.go @@ -24,8 +24,8 @@ func (tx *StateTx) chainID() *big.Int { return nil } func (tx *StateTx) input() []byte { return tx.Input } func (tx *StateTx) gas() uint64 { return tx.Gas } func (tx *StateTx) gasPrice() *big.Int { return tx.GasPrice } -func (tx *StateTx) gasTipCap() *big.Int { return nil } -func (tx *StateTx) gasFeeCap() *big.Int { return nil } +func (tx *StateTx) gasTipCap() *big.Int { return tx.GasPrice } +func (tx *StateTx) gasFeeCap() *big.Int { return tx.GasPrice } func (tx *StateTx) value() *big.Int { return tx.Value } func (tx *StateTx) nonce() uint64 { return tx.Nonce } func (tx *StateTx) to() *Address { return tx.To } @@ -60,9 +60,13 @@ func (tx *StateTx) setGasPrice(gas *big.Int) { tx.GasPrice = gas } -func (tx *StateTx) setGasFeeCap(gas *big.Int) {} +func (tx *StateTx) setGasFeeCap(gas *big.Int) { + tx.GasPrice = gas +} -func (tx *StateTx) setGasTipCap(gas *big.Int) {} +func (tx *StateTx) setGasTipCap(gas *big.Int) { + tx.GasPrice = gas +} func (tx *StateTx) setValue(value *big.Int) { tx.Value = value diff --git a/types/transaction.go b/types/transaction.go index d6a5240ef0..9a1076480a 100644 --- a/types/transaction.go +++ b/types/transaction.go @@ -228,6 +228,14 @@ func (t *Transaction) SetHash(h Hash) { t.Inner.setHash(h) } +func (t *Transaction) MarshalRLPWith(a *fastrlp.Arena) *fastrlp.Value { + return t.Inner.marshalRLPWith(a) +} + +func (t *Transaction) UnmarshalRLPFrom(p *fastrlp.Parser, v *fastrlp.Value) error { + return t.Inner.unmarshalRLPFrom(p, v) +} + // IsContractCreation checks if tx is contract creation func (t *Transaction) IsContractCreation() bool { return t.To() == nil @@ -378,3 +386,11 @@ func FindTxByHash(txs []*Transaction, hash Hash) (*Transaction, int) { return nil, -1 } + +func NewTxWithType(txType TxType) *Transaction { + tx := &Transaction{} + + tx.InitInnerData(txType) + + return tx +}