|
| 1 | +// (c) 2025, Ava Labs, Inc. All rights reserved. |
| 2 | +// See the file LICENSE for licensing terms. |
| 3 | + |
| 4 | +package types |
| 5 | + |
| 6 | +import ( |
| 7 | + "math/big" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/ava-labs/libevm/common" |
| 11 | + ethtypes "github.com/ava-labs/libevm/core/types" |
| 12 | + "github.com/ava-labs/libevm/rlp" |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | +) |
| 16 | + |
| 17 | +func TestBodyExtraGetWith(t *testing.T) { |
| 18 | + t.Parallel() |
| 19 | + |
| 20 | + b := &Body{} |
| 21 | + |
| 22 | + extra := GetBodyExtra(b) |
| 23 | + require.NotNil(t, extra) |
| 24 | + assert.Equal(t, &BodyExtra{}, extra) |
| 25 | + |
| 26 | + extra = &BodyExtra{ |
| 27 | + Version: 1, |
| 28 | + } |
| 29 | + WithBodyExtra(b, extra) |
| 30 | + |
| 31 | + extra = GetBodyExtra(b) |
| 32 | + wantExtra := &BodyExtra{ |
| 33 | + Version: 1, |
| 34 | + } |
| 35 | + assert.Equal(t, wantExtra, extra) |
| 36 | +} |
| 37 | + |
| 38 | +func TestBodyExtraRLP(t *testing.T) { |
| 39 | + t.Parallel() |
| 40 | + |
| 41 | + testTx := NewTransaction(0, common.Address{1}, big.NewInt(2), 3, big.NewInt(4), []byte{5}) |
| 42 | + |
| 43 | + uncle := &Header{ParentHash: common.Hash{6}} |
| 44 | + uncleExtra := &HeaderExtra{ExtDataHash: common.Hash{7}} |
| 45 | + _ = WithHeaderExtra(uncle, uncleExtra) |
| 46 | + |
| 47 | + body := &Body{ |
| 48 | + Transactions: []*Transaction{testTx}, |
| 49 | + Uncles: []*ethtypes.Header{uncle}, |
| 50 | + Withdrawals: []*ethtypes.Withdrawal{{Index: 7}}, // ignored |
| 51 | + } |
| 52 | + extra := &BodyExtra{ |
| 53 | + Version: 1, |
| 54 | + ExtData: ptrTo([]byte{2}), |
| 55 | + } |
| 56 | + _ = WithBodyExtra(body, extra) |
| 57 | + |
| 58 | + b, err := rlp.EncodeToBytes(body) |
| 59 | + require.NoError(t, err) |
| 60 | + |
| 61 | + gotBody := new(Body) |
| 62 | + err = rlp.DecodeBytes(b, gotBody) |
| 63 | + require.NoError(t, err) |
| 64 | + |
| 65 | + // Check transactions in the following because unexported fields |
| 66 | + // size and time are set by the RLP decoder. |
| 67 | + require.Len(t, gotBody.Transactions, 1) |
| 68 | + gotTx := gotBody.Transactions[0] |
| 69 | + gotTxJSON, err := gotTx.MarshalJSON() |
| 70 | + require.NoError(t, err) |
| 71 | + wantTxJSON, err := testTx.MarshalJSON() |
| 72 | + require.NoError(t, err) |
| 73 | + assert.Equal(t, string(wantTxJSON), string(gotTxJSON)) |
| 74 | + gotBody.Transactions = nil |
| 75 | + |
| 76 | + wantUncle := &Header{ |
| 77 | + ParentHash: common.Hash{6}, |
| 78 | + Difficulty: new(big.Int), |
| 79 | + Number: new(big.Int), |
| 80 | + Extra: []byte{}, |
| 81 | + } |
| 82 | + wantUncleExtra := &HeaderExtra{ExtDataHash: common.Hash{7}} |
| 83 | + _ = WithHeaderExtra(wantUncle, wantUncleExtra) |
| 84 | + |
| 85 | + wantBody := &Body{ |
| 86 | + Transactions: nil, // checked above |
| 87 | + Uncles: []*ethtypes.Header{wantUncle}, |
| 88 | + } |
| 89 | + wantExtra := &BodyExtra{ |
| 90 | + Version: 1, |
| 91 | + ExtData: ptrTo([]byte{2}), |
| 92 | + } |
| 93 | + _ = WithBodyExtra(wantBody, wantExtra) |
| 94 | + |
| 95 | + assert.Equal(t, wantBody, gotBody) |
| 96 | + assert.Equal(t, wantExtra, GetBodyExtra(gotBody)) |
| 97 | +} |
0 commit comments