Skip to content

Commit

Permalink
core/types: extend tx size test with decoded sizes, fix error
Browse files Browse the repository at this point in the history
  • Loading branch information
karalabe committed Oct 26, 2022
1 parent f37fe2c commit 3bf25cc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
4 changes: 2 additions & 2 deletions core/types/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func (tx *Transaction) DecodeRLP(s *rlp.Stream) error {
}
inner, err := tx.decodeTyped(b)
if err == nil {
tx.setDecoded(inner, uint64(len(b))+1 /* type byte */)
tx.setDecoded(inner, uint64(len(b)))
}
return err
}
Expand All @@ -166,7 +166,7 @@ func (tx *Transaction) UnmarshalBinary(b []byte) error {
if err != nil {
return err
}
tx.setDecoded(inner, uint64(len(b))+1 /* type byte */)
tx.setDecoded(inner, uint64(len(b)))
return nil
}

Expand Down
14 changes: 11 additions & 3 deletions core/types/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ func assertEqual(orig *Transaction, cpy *Transaction) error {
return nil
}

func TestSize(t *testing.T) {
func TestTransactionSizes(t *testing.T) {
signer := NewLondonSigner(big.NewInt(123))
key, _ := crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
to := common.HexToAddress("0x01")
Expand Down Expand Up @@ -575,19 +575,27 @@ func TestSize(t *testing.T) {
GasFeeCap: big.NewInt(500),
},
} {

tx, err := SignNewTx(key, signer, txdata)
if err != nil {
t.Fatalf("test %d: %v", i, err)
}
bin, _ := tx.MarshalBinary()
// Check initial calc.

// Check initial calc
if have, want := int(tx.Size()), len(bin); have != want {
t.Errorf("test %d: size wrong, have %d want %d", i, have, want)
}
// Check cached version too
if have, want := int(tx.Size()), len(bin); have != want {
t.Errorf("test %d: (cached) size wrong, have %d want %d", i, have, want)
}
// Check unmarshalled version too
utx := new(Transaction)
if err := utx.UnmarshalBinary(bin); err != nil {
t.Fatalf("test %d: failed to unmarshal tx: %v", i, err)
}
if have, want := int(utx.Size()), len(bin); have != want {
t.Errorf("test %d: (unmarshalled) size wrong, have %d want %d", i, have, want)
}
}
}

0 comments on commit 3bf25cc

Please sign in to comment.