Skip to content

Commit

Permalink
[beta] Stricter uint256 RLP decoding. Update consensus tests to 10.1 (#…
Browse files Browse the repository at this point in the history
…3089) (#3095)

* Clean up test runners. Don't run legacy tests

* Cherry pick ethereum/go-ethereum#22927

* Tests update 10.1: Transaction Tests

* Port decodeBigInt changes to decodeUint256

* Introduce (*Stream) Uint256Bytes

* Temporarily disable stTransactionTest/HighGasPrice

* linter

* ttWrongRLP transaction tests pass now

* Fix stTransactionTest/HighGasPrice

Co-authored-by: Felix Lange <fjl@twurst.com>

Co-authored-by: Andrew Ashikhmin <34320705+yperbasis@users.noreply.github.com>
Co-authored-by: Felix Lange <fjl@twurst.com>
  • Loading branch information
3 people committed Dec 6, 2021
1 parent 276b6c0 commit 0e4fab2
Show file tree
Hide file tree
Showing 17 changed files with 191 additions and 285 deletions.
15 changes: 12 additions & 3 deletions core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,21 @@ func (st *StateTransition) to() common.Address {
func (st *StateTransition) buyGas(gasBailout bool) error {
mgval := st.sharedBuyGas
mgval.SetUint64(st.msg.Gas())
mgval = mgval.Mul(mgval, st.gasPrice)
mgval, overflow := mgval.MulOverflow(mgval, st.gasPrice)
if overflow {
return fmt.Errorf("%w: address %v", ErrInsufficientFunds, st.msg.From().Hex())
}
balanceCheck := mgval
if st.gasFeeCap != nil {
balanceCheck = st.sharedBuyGasBalance.SetUint64(st.msg.Gas())
balanceCheck = balanceCheck.Mul(balanceCheck, st.gasFeeCap)
balanceCheck.Add(balanceCheck, st.value)
balanceCheck, overflow = balanceCheck.MulOverflow(balanceCheck, st.gasFeeCap)
if overflow {
return fmt.Errorf("%w: address %v", ErrInsufficientFunds, st.msg.From().Hex())
}
balanceCheck, overflow = balanceCheck.AddOverflow(balanceCheck, st.value)
if overflow {
return fmt.Errorf("%w: address %v", ErrInsufficientFunds, st.msg.From().Hex())
}
}
if have, want := st.state.GetBalance(st.msg.From()), balanceCheck; have.Cmp(want) < 0 {
if !gasBailout {
Expand Down
30 changes: 6 additions & 24 deletions core/types/access_list_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,22 +465,16 @@ func (tx *AccessListTx) DecodeRLP(s *rlp.Stream) error {
return err
}
var b []byte
if b, err = s.Bytes(); err != nil {
if b, err = s.Uint256Bytes(); err != nil {
return fmt.Errorf("read ChainID: %w", err)
}
if len(b) > 32 {
return fmt.Errorf("wrong size for ChainID: %d", len(b))
}
tx.ChainID = new(uint256.Int).SetBytes(b)
if tx.Nonce, err = s.Uint(); err != nil {
return fmt.Errorf("read Nonce: %w", err)
}
if b, err = s.Bytes(); err != nil {
if b, err = s.Uint256Bytes(); err != nil {
return fmt.Errorf("read GasPrice: %w", err)
}
if len(b) > 32 {
return fmt.Errorf("wrong size for GasPrice: %d", len(b))
}
tx.GasPrice = new(uint256.Int).SetBytes(b)
if tx.Gas, err = s.Uint(); err != nil {
return fmt.Errorf("read Gas: %w", err)
Expand All @@ -495,12 +489,9 @@ func (tx *AccessListTx) DecodeRLP(s *rlp.Stream) error {
tx.To = &common.Address{}
copy((*tx.To)[:], b)
}
if b, err = s.Bytes(); err != nil {
if b, err = s.Uint256Bytes(); err != nil {
return fmt.Errorf("read Value: %w", err)
}
if len(b) > 32 {
return fmt.Errorf("wrong size for Value: %d", len(b))
}
tx.Value = new(uint256.Int).SetBytes(b)
if tx.Data, err = s.Bytes(); err != nil {
return fmt.Errorf("read Data: %w", err)
Expand All @@ -511,26 +502,17 @@ func (tx *AccessListTx) DecodeRLP(s *rlp.Stream) error {
return fmt.Errorf("read AccessList: %w", err)
}
// decode V
if b, err = s.Bytes(); err != nil {
if b, err = s.Uint256Bytes(); err != nil {
return fmt.Errorf("read V: %w", err)
}
if len(b) > 32 {
return fmt.Errorf("wrong size for V: %d", len(b))
}
tx.V.SetBytes(b)
if b, err = s.Bytes(); err != nil {
if b, err = s.Uint256Bytes(); err != nil {
return fmt.Errorf("read R: %w", err)
}
if len(b) > 32 {
return fmt.Errorf("wrong size for R: %d", len(b))
}
tx.R.SetBytes(b)
if b, err = s.Bytes(); err != nil {
if b, err = s.Uint256Bytes(); err != nil {
return fmt.Errorf("read S: %w", err)
}
if len(b) > 32 {
return fmt.Errorf("wrong size for S: %d", len(b))
}
tx.S.SetBytes(b)
if err := s.ListEnd(); err != nil {
return fmt.Errorf("close AccessListTx: %w", err)
Expand Down
15 changes: 3 additions & 12 deletions core/types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -463,19 +463,13 @@ func (h *Header) DecodeRLP(s *rlp.Stream) error {
return fmt.Errorf("wrong size for Bloom: %d", len(b))
}
copy(h.Bloom[:], b)
if b, err = s.Bytes(); err != nil {
if b, err = s.Uint256Bytes(); err != nil {
return fmt.Errorf("read Difficulty: %w", err)
}
if len(b) > 32 {
return fmt.Errorf("wrong size for Difficulty: %d", len(b))
}
h.Difficulty = new(big.Int).SetBytes(b)
if b, err = s.Bytes(); err != nil {
if b, err = s.Uint256Bytes(); err != nil {
return fmt.Errorf("read Number: %w", err)
}
if len(b) > 32 {
return fmt.Errorf("wrong size for Number: %d", len(b))
}
h.Number = new(big.Int).SetBytes(b)
if h.GasLimit, err = s.Uint(); err != nil {
return fmt.Errorf("read GasLimit: %w", err)
Expand Down Expand Up @@ -513,7 +507,7 @@ func (h *Header) DecodeRLP(s *rlp.Stream) error {
return fmt.Errorf("wrong size for Nonce: %d", len(b))
}
copy(h.Nonce[:], b)
if b, err = s.Bytes(); err != nil {
if b, err = s.Uint256Bytes(); err != nil {
if errors.Is(err, rlp.EOL) {
h.BaseFee = nil
h.Eip1559 = false
Expand All @@ -524,9 +518,6 @@ func (h *Header) DecodeRLP(s *rlp.Stream) error {
}
return fmt.Errorf("read BaseFee: %w", err)
}
if len(b) > 32 {
return fmt.Errorf("wrong size for BaseFee: %d", len(b))
}
h.Eip1559 = true
h.BaseFee = new(big.Int).SetBytes(b)
}
Expand Down
35 changes: 7 additions & 28 deletions core/types/dynamic_fee_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,29 +378,20 @@ func (tx *DynamicFeeTransaction) DecodeRLP(s *rlp.Stream) error {
return err
}
var b []byte
if b, err = s.Bytes(); err != nil {
if b, err = s.Uint256Bytes(); err != nil {
return err
}
if len(b) > 32 {
return fmt.Errorf("wrong size for ChainID: %d", len(b))
}
tx.ChainID = new(uint256.Int).SetBytes(b)
if tx.Nonce, err = s.Uint(); err != nil {
return err
}
if b, err = s.Bytes(); err != nil {
if b, err = s.Uint256Bytes(); err != nil {
return err
}
if len(b) > 32 {
return fmt.Errorf("wrong size for MaxPriorityFeePerGas: %d", len(b))
}
tx.Tip = new(uint256.Int).SetBytes(b)
if b, err = s.Bytes(); err != nil {
if b, err = s.Uint256Bytes(); err != nil {
return err
}
if len(b) > 32 {
return fmt.Errorf("wrong size for MaxFeePerGas: %d", len(b))
}
tx.FeeCap = new(uint256.Int).SetBytes(b)
if tx.Gas, err = s.Uint(); err != nil {
return err
Expand All @@ -415,12 +406,9 @@ func (tx *DynamicFeeTransaction) DecodeRLP(s *rlp.Stream) error {
tx.To = &common.Address{}
copy((*tx.To)[:], b)
}
if b, err = s.Bytes(); err != nil {
if b, err = s.Uint256Bytes(); err != nil {
return err
}
if len(b) > 32 {
return fmt.Errorf("wrong size for Value: %d", len(b))
}
tx.Value = new(uint256.Int).SetBytes(b)
if tx.Data, err = s.Bytes(); err != nil {
return err
Expand All @@ -431,26 +419,17 @@ func (tx *DynamicFeeTransaction) DecodeRLP(s *rlp.Stream) error {
return err
}
// decode V
if b, err = s.Bytes(); err != nil {
if b, err = s.Uint256Bytes(); err != nil {
return err
}
if len(b) > 32 {
return fmt.Errorf("wrong size for V: %d", len(b))
}
tx.V.SetBytes(b)
if b, err = s.Bytes(); err != nil {
if b, err = s.Uint256Bytes(); err != nil {
return err
}
if len(b) > 32 {
return fmt.Errorf("wrong size for R: %d", len(b))
}
tx.R.SetBytes(b)
if b, err = s.Bytes(); err != nil {
if b, err = s.Uint256Bytes(); err != nil {
return err
}
if len(b) > 32 {
return fmt.Errorf("wrong size for S: %d", len(b))
}
tx.S.SetBytes(b)
return s.ListEnd()

Expand Down
25 changes: 5 additions & 20 deletions core/types/legacy_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,12 +369,9 @@ func (tx *LegacyTx) DecodeRLP(s *rlp.Stream, encodingSize uint64) error {
return fmt.Errorf("read Nonce: %w", err)
}
var b []byte
if b, err = s.Bytes(); err != nil {
if b, err = s.Uint256Bytes(); err != nil {
return fmt.Errorf("read GasPrice: %w", err)
}
if len(b) > 32 {
return fmt.Errorf("wrong size for GasPrice: %d", len(b))
}
tx.GasPrice = new(uint256.Int).SetBytes(b)
if tx.Gas, err = s.Uint(); err != nil {
return fmt.Errorf("read Gas: %w", err)
Expand All @@ -389,36 +386,24 @@ func (tx *LegacyTx) DecodeRLP(s *rlp.Stream, encodingSize uint64) error {
tx.To = &common.Address{}
copy((*tx.To)[:], b)
}
if b, err = s.Bytes(); err != nil {
if b, err = s.Uint256Bytes(); err != nil {
return fmt.Errorf("read Value: %w", err)
}
if len(b) > 32 {
return fmt.Errorf("wrong size for Value: %d", len(b))
}
tx.Value = new(uint256.Int).SetBytes(b)
if tx.Data, err = s.Bytes(); err != nil {
return fmt.Errorf("read Data: %w", err)
}
if b, err = s.Bytes(); err != nil {
if b, err = s.Uint256Bytes(); err != nil {
return fmt.Errorf("read V: %w", err)
}
if len(b) > 32 {
return fmt.Errorf("wrong size for V: %d", len(b))
}
tx.V.SetBytes(b)
if b, err = s.Bytes(); err != nil {
if b, err = s.Uint256Bytes(); err != nil {
return fmt.Errorf("read R: %w", err)
}
if len(b) > 32 {
return fmt.Errorf("wrong size for R: %d", len(b))
}
tx.R.SetBytes(b)
if b, err = s.Bytes(); err != nil {
if b, err = s.Uint256Bytes(); err != nil {
return fmt.Errorf("read S: %w", err)
}
if len(b) > 32 {
return fmt.Errorf("wrong size for S: %d", len(b))
}
tx.S.SetBytes(b)
if err = s.ListEnd(); err != nil {
return fmt.Errorf("close tx struct: %w", err)
Expand Down
5 changes: 1 addition & 4 deletions eth/protocols/eth/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,12 +402,9 @@ func (nbp *NewBlockPacket) DecodeRLP(s *rlp.Stream) error {
}
// decode TD
var b []byte
if b, err = s.Bytes(); err != nil {
if b, err = s.Uint256Bytes(); err != nil {
return fmt.Errorf("read TD: %w", err)
}
if len(b) > 32 {
return fmt.Errorf("wrong size for TD: %d", len(b))
}
nbp.TD = new(big.Int).SetBytes(b)
if err = s.ListEnd(); err != nil {
return err
Expand Down
Loading

0 comments on commit 0e4fab2

Please sign in to comment.