From fec4957bec3e29a802168189ccb404474424f7d1 Mon Sep 17 00:00:00 2001 From: Dusan Nosovic Date: Tue, 13 Feb 2024 12:37:33 +0100 Subject: [PATCH] comment fix --- txpool/txpool.go | 1 + types/access_list_tx.go | 34 ++++++++++++++-------------------- types/dynamic_fee_tx.go | 37 +++++++++++++++---------------------- types/legacy_tx.go | 30 ++++++++++++------------------ types/rlp_unmarshal.go | 8 ++++++++ types/state_tx.go | 32 +++++++++++++------------------- types/transaction.go | 4 ---- 7 files changed, 63 insertions(+), 83 deletions(-) diff --git a/txpool/txpool.go b/txpool/txpool.go index 3b6adec4cd..3fcba4a526 100644 --- a/txpool/txpool.go +++ b/txpool/txpool.go @@ -671,6 +671,7 @@ func (p *TxPool) validateTx(tx *types.Transaction) error { } if tx.GetGasPrice(baseFee).Cmp(new(big.Int).SetUint64(p.priceLimit)) < 0 { + // Make sure that the transaction is not underpriced metrics.IncrCounter([]string{txPoolMetrics, "underpriced_tx"}, 1) return ErrUnderpriced diff --git a/types/access_list_tx.go b/types/access_list_tx.go index efdadb4b35..2663b304e7 100644 --- a/types/access_list_tx.go +++ b/types/access_list_tx.go @@ -204,33 +204,27 @@ func (tx *AccessListTxn) setHash(h Hash) { // Use UnmarshalRLP in most cases func (tx *AccessListTxn) unmarshalRLPFrom(p *fastrlp.Parser, v *fastrlp.Value) error { numOfElems := 11 + var values rlpValues - elems, err := v.GetElems() + values, err := v.GetElems() if err != nil { return err } - getElem := func() *fastrlp.Value { - val := elems[0] - elems = elems[1:] - - return val - } - - if numElems := len(elems); numElems != numOfElems { + if numElems := len(values); numElems != numOfElems { return fmt.Errorf("incorrect number of transaction elements, expected %d but found %d", numOfElems, numElems) } txChainID := new(big.Int) - if err = getElem().GetBigInt(txChainID); err != nil { + if err = values.dequeueValue().GetBigInt(txChainID); err != nil { return err } tx.setChainID(txChainID) // nonce - txNonce, err := getElem().GetUint64() + txNonce, err := values.dequeueValue().GetUint64() if err != nil { return err } @@ -239,14 +233,14 @@ func (tx *AccessListTxn) unmarshalRLPFrom(p *fastrlp.Parser, v *fastrlp.Value) e // gasPrice txGasPrice := new(big.Int) - if err = getElem().GetBigInt(txGasPrice); err != nil { + if err = values.dequeueValue().GetBigInt(txGasPrice); err != nil { return err } tx.setGasPrice(txGasPrice) // gas - txGas, err := getElem().GetUint64() + txGas, err := values.dequeueValue().GetUint64() if err != nil { return err } @@ -254,7 +248,7 @@ func (tx *AccessListTxn) unmarshalRLPFrom(p *fastrlp.Parser, v *fastrlp.Value) e tx.setGas(txGas) // to - if vv, _ := getElem().Bytes(); len(vv) == 20 { + if vv, _ := values.dequeueValue().Bytes(); len(vv) == 20 { // address addr := BytesToAddress(vv) tx.setTo(&addr) @@ -265,7 +259,7 @@ func (tx *AccessListTxn) unmarshalRLPFrom(p *fastrlp.Parser, v *fastrlp.Value) e // value txValue := new(big.Int) - if err = getElem().GetBigInt(txValue); err != nil { + if err = values.dequeueValue().GetBigInt(txValue); err != nil { return err } @@ -274,7 +268,7 @@ func (tx *AccessListTxn) unmarshalRLPFrom(p *fastrlp.Parser, v *fastrlp.Value) e // input var txInput []byte - txInput, err = getElem().GetBytes(txInput) + txInput, err = values.dequeueValue().GetBytes(txInput) if err != nil { return err } @@ -282,7 +276,7 @@ func (tx *AccessListTxn) unmarshalRLPFrom(p *fastrlp.Parser, v *fastrlp.Value) e tx.setInput(txInput) //accessList - accessListVV, err := getElem().GetElems() + accessListVV, err := values.dequeueValue().GetElems() if err != nil { return err } @@ -300,19 +294,19 @@ func (tx *AccessListTxn) unmarshalRLPFrom(p *fastrlp.Parser, v *fastrlp.Value) e // V txV := new(big.Int) - if err = getElem().GetBigInt(txV); err != nil { + if err = values.dequeueValue().GetBigInt(txV); err != nil { return err } // R txR := new(big.Int) - if err = getElem().GetBigInt(txR); err != nil { + if err = values.dequeueValue().GetBigInt(txR); err != nil { return err } // S txS := new(big.Int) - if err = getElem().GetBigInt(txS); err != nil { + if err = values.dequeueValue().GetBigInt(txS); err != nil { return err } diff --git a/types/dynamic_fee_tx.go b/types/dynamic_fee_tx.go index 12dc491025..383745adb9 100644 --- a/types/dynamic_fee_tx.go +++ b/types/dynamic_fee_tx.go @@ -97,33 +97,27 @@ func (tx *DynamicFeeTx) setHash(h Hash) { tx.Hash = h } // Use UnmarshalRLP in most cases func (tx *DynamicFeeTx) unmarshalRLPFrom(p *fastrlp.Parser, v *fastrlp.Value) error { numOfElems := 12 + var values rlpValues - elems, err := v.GetElems() + values, err := v.GetElems() if err != nil { return err } - getElem := func() *fastrlp.Value { - val := elems[0] - elems = elems[1:] - - return val - } - - if numElems := len(elems); numElems != numOfElems { + if numElems := len(values); numElems != numOfElems { return fmt.Errorf("incorrect number of transaction elements, expected %d but found %d", numOfElems, numElems) } // Load Chain ID txChainID := new(big.Int) - if err = getElem().GetBigInt(txChainID); err != nil { + if err = values.dequeueValue().GetBigInt(txChainID); err != nil { return err } tx.setChainID(txChainID) // nonce - txNonce, err := getElem().GetUint64() + txNonce, err := values.dequeueValue().GetUint64() if err != nil { return err } @@ -132,7 +126,7 @@ func (tx *DynamicFeeTx) unmarshalRLPFrom(p *fastrlp.Parser, v *fastrlp.Value) er // gasTipCap txGasTipCap := new(big.Int) - if err = getElem().GetBigInt(txGasTipCap); err != nil { + if err = values.dequeueValue().GetBigInt(txGasTipCap); err != nil { return err } @@ -140,14 +134,14 @@ func (tx *DynamicFeeTx) unmarshalRLPFrom(p *fastrlp.Parser, v *fastrlp.Value) er // gasFeeCap txGasFeeCap := new(big.Int) - if err = getElem().GetBigInt(txGasFeeCap); err != nil { + if err = values.dequeueValue().GetBigInt(txGasFeeCap); err != nil { return err } tx.setGasFeeCap(txGasFeeCap) // gas - txGas, err := getElem().GetUint64() + txGas, err := values.dequeueValue().GetUint64() if err != nil { return err } @@ -155,8 +149,7 @@ func (tx *DynamicFeeTx) unmarshalRLPFrom(p *fastrlp.Parser, v *fastrlp.Value) er tx.setGas(txGas) // to - if vv, _ := getElem().Bytes(); len(vv) == 20 { - // address + if vv, _ := values.dequeueValue().Bytes(); len(vv) == 20 { addr := BytesToAddress(vv) tx.setTo(&addr) } else { @@ -166,7 +159,7 @@ func (tx *DynamicFeeTx) unmarshalRLPFrom(p *fastrlp.Parser, v *fastrlp.Value) er // value txValue := new(big.Int) - if err = getElem().GetBigInt(txValue); err != nil { + if err = values.dequeueValue().GetBigInt(txValue); err != nil { return err } @@ -175,14 +168,14 @@ func (tx *DynamicFeeTx) unmarshalRLPFrom(p *fastrlp.Parser, v *fastrlp.Value) er // input var txInput []byte - txInput, err = getElem().GetBytes(txInput) + txInput, err = values.dequeueValue().GetBytes(txInput) if err != nil { return err } tx.setInput(txInput) - accessListVV, err := getElem().GetElems() + accessListVV, err := values.dequeueValue().GetElems() if err != nil { return err } @@ -200,19 +193,19 @@ func (tx *DynamicFeeTx) unmarshalRLPFrom(p *fastrlp.Parser, v *fastrlp.Value) er // V txV := new(big.Int) - if err = getElem().GetBigInt(txV); err != nil { + if err = values.dequeueValue().GetBigInt(txV); err != nil { return err } // R txR := new(big.Int) - if err = getElem().GetBigInt(txR); err != nil { + if err = values.dequeueValue().GetBigInt(txR); err != nil { return err } // S txS := new(big.Int) - if err = getElem().GetBigInt(txS); err != nil { + if err = values.dequeueValue().GetBigInt(txS); err != nil { return err } diff --git a/types/legacy_tx.go b/types/legacy_tx.go index 533681e903..b6e4a4f6be 100644 --- a/types/legacy_tx.go +++ b/types/legacy_tx.go @@ -91,25 +91,19 @@ func (tx *LegacyTx) setHash(h Hash) { tx.Hash = h } // Use UnmarshalRLP in most cases func (tx *LegacyTx) unmarshalRLPFrom(p *fastrlp.Parser, v *fastrlp.Value) error { num := 9 + var values rlpValues - elems, err := v.GetElems() + values, err := v.GetElems() if err != nil { return err } - getElem := func() *fastrlp.Value { - val := elems[0] - elems = elems[1:] - - return val - } - - if numElems := len(elems); numElems != num { + if numElems := len(values); numElems != num { return fmt.Errorf("legacy incorrect number of transaction elements, expected %d but found %d", num, numElems) } // nonce - txNonce, err := getElem().GetUint64() + txNonce, err := values.dequeueValue().GetUint64() if err != nil { return err } @@ -118,14 +112,14 @@ func (tx *LegacyTx) unmarshalRLPFrom(p *fastrlp.Parser, v *fastrlp.Value) error // gasPrice txGasPrice := new(big.Int) - if err = getElem().GetBigInt(txGasPrice); err != nil { + if err = values.dequeueValue().GetBigInt(txGasPrice); err != nil { return err } tx.setGasPrice(txGasPrice) // gas - txGas, err := getElem().GetUint64() + txGas, err := values.dequeueValue().GetUint64() if err != nil { return err } @@ -133,7 +127,7 @@ func (tx *LegacyTx) unmarshalRLPFrom(p *fastrlp.Parser, v *fastrlp.Value) error tx.setGas(txGas) // to - if vv, _ := getElem().Bytes(); len(vv) == 20 { + if vv, _ := values.dequeueValue().Bytes(); len(vv) == 20 { // address addr := BytesToAddress(vv) tx.setTo(&addr) @@ -144,7 +138,7 @@ func (tx *LegacyTx) unmarshalRLPFrom(p *fastrlp.Parser, v *fastrlp.Value) error // value txValue := new(big.Int) - if err = getElem().GetBigInt(txValue); err != nil { + if err = values.dequeueValue().GetBigInt(txValue); err != nil { return err } @@ -153,7 +147,7 @@ func (tx *LegacyTx) unmarshalRLPFrom(p *fastrlp.Parser, v *fastrlp.Value) error // input var txInput []byte - txInput, err = getElem().GetBytes(txInput) + txInput, err = values.dequeueValue().GetBytes(txInput) if err != nil { return err } @@ -162,19 +156,19 @@ func (tx *LegacyTx) unmarshalRLPFrom(p *fastrlp.Parser, v *fastrlp.Value) error // V txV := new(big.Int) - if err = getElem().GetBigInt(txV); err != nil { + if err = values.dequeueValue().GetBigInt(txV); err != nil { return err } // R txR := new(big.Int) - if err = getElem().GetBigInt(txR); err != nil { + if err = values.dequeueValue().GetBigInt(txR); err != nil { return err } // S txS := new(big.Int) - if err = getElem().GetBigInt(txS); err != nil { + if err = values.dequeueValue().GetBigInt(txS); err != nil { return err } diff --git a/types/rlp_unmarshal.go b/types/rlp_unmarshal.go index 197b47da0f..3743a151a6 100644 --- a/types/rlp_unmarshal.go +++ b/types/rlp_unmarshal.go @@ -383,3 +383,11 @@ func (t *Transaction) UnmarshalRLP(input []byte) error { return nil } + +type rlpValues []*fastrlp.Value + +func (r rlpValues) dequeueValue() *fastrlp.Value { + val := r[0] + r = r[1:] + return val +} diff --git a/types/state_tx.go b/types/state_tx.go index ffc1cbb883..e0eaf3c72e 100644 --- a/types/state_tx.go +++ b/types/state_tx.go @@ -94,25 +94,19 @@ func (tx *StateTx) setHash(h Hash) { tx.Hash = h } // Use UnmarshalRLP in most cases func (tx *StateTx) unmarshalRLPFrom(p *fastrlp.Parser, v *fastrlp.Value) error { numOfElems := 10 + var values rlpValues - elems, err := v.GetElems() + values, err := v.GetElems() if err != nil { return err } - getElem := func() *fastrlp.Value { - val := elems[0] - elems = elems[1:] - - return val - } - - if numElems := len(elems); numElems != numOfElems { + if numElems := len(values); numElems != numOfElems { return fmt.Errorf("incorrect number of transaction elements, expected %d but found %d", numOfElems, numElems) } // nonce - txNonce, err := getElem().GetUint64() + txNonce, err := values.dequeueValue().GetUint64() if err != nil { return err } @@ -121,14 +115,14 @@ func (tx *StateTx) unmarshalRLPFrom(p *fastrlp.Parser, v *fastrlp.Value) error { // gasPrice txGasPrice := new(big.Int) - if err = getElem().GetBigInt(txGasPrice); err != nil { + if err = values.dequeueValue().GetBigInt(txGasPrice); err != nil { return err } tx.setGasPrice(txGasPrice) // gas - txGas, err := getElem().GetUint64() + txGas, err := values.dequeueValue().GetUint64() if err != nil { return err } @@ -136,7 +130,7 @@ func (tx *StateTx) unmarshalRLPFrom(p *fastrlp.Parser, v *fastrlp.Value) error { tx.setGas(txGas) // to - if vv, _ := getElem().Bytes(); len(vv) == 20 { + if vv, _ := values.dequeueValue().Bytes(); len(vv) == 20 { // address addr := BytesToAddress(vv) tx.setTo(&addr) @@ -147,7 +141,7 @@ func (tx *StateTx) unmarshalRLPFrom(p *fastrlp.Parser, v *fastrlp.Value) error { // value txValue := new(big.Int) - if err = getElem().GetBigInt(txValue); err != nil { + if err = values.dequeueValue().GetBigInt(txValue); err != nil { return err } @@ -156,7 +150,7 @@ func (tx *StateTx) unmarshalRLPFrom(p *fastrlp.Parser, v *fastrlp.Value) error { // input var txInput []byte - txInput, err = getElem().GetBytes(txInput) + txInput, err = values.dequeueValue().GetBytes(txInput) if err != nil { return err } @@ -165,19 +159,19 @@ func (tx *StateTx) unmarshalRLPFrom(p *fastrlp.Parser, v *fastrlp.Value) error { // V txV := new(big.Int) - if err = getElem().GetBigInt(txV); err != nil { + if err = values.dequeueValue().GetBigInt(txV); err != nil { return err } // R txR := new(big.Int) - if err = getElem().GetBigInt(txR); err != nil { + if err = values.dequeueValue().GetBigInt(txR); err != nil { return err } // S txS := new(big.Int) - if err = getElem().GetBigInt(txS); err != nil { + if err = values.dequeueValue().GetBigInt(txS); err != nil { return err } @@ -187,7 +181,7 @@ func (tx *StateTx) unmarshalRLPFrom(p *fastrlp.Parser, v *fastrlp.Value) error { // We need to set From field for state transaction, // because we are using unique, predefined address, for sending such transactions - if vv, err := getElem().Bytes(); err == nil && len(vv) == AddressLength { + if vv, err := values.dequeueValue().Bytes(); err == nil && len(vv) == AddressLength { // address tx.setFrom(BytesToAddress(vv)) } diff --git a/types/transaction.go b/types/transaction.go index 9a1076480a..8df1a40ddc 100644 --- a/types/transaction.go +++ b/types/transaction.go @@ -372,10 +372,6 @@ func (t *Transaction) GetGasFeeCap() *big.Int { } } -func (t *Transaction) SetTxData(data TxData) { - t.Inner = data -} - // FindTxByHash returns transaction and its index from a slice of transactions func FindTxByHash(txs []*Transaction, hash Hash) (*Transaction, int) { for idx, txn := range txs {