44package types
55
66import (
7+ "math/big"
78 "testing"
89
10+ "github.com/ava-labs/libevm/common"
11+ ethtypes "github.com/ava-labs/libevm/core/types"
912 "github.com/stretchr/testify/assert"
1013 "github.com/stretchr/testify/require"
1114)
@@ -29,3 +32,142 @@ func TestHeaderExtraGetWith(t *testing.T) {
2932 ExtDataHash : [32 ]byte {1 },
3033 }, extra )
3134}
35+
36+ func TestHeaderSerializable_updates (t * testing.T ) {
37+ t .Parallel ()
38+
39+ t .Run ("from" , func (t * testing.T ) {
40+ t .Parallel ()
41+
42+ eth := & ethtypes.Header {
43+ ParentHash : common.Hash {1 },
44+ UncleHash : common.Hash {2 },
45+ Coinbase : common.Address {3 },
46+ Root : common.Hash {4 },
47+ TxHash : common.Hash {5 },
48+ ReceiptHash : common.Hash {6 },
49+ Bloom : Bloom {7 },
50+ Difficulty : big .NewInt (8 ),
51+ Number : big .NewInt (9 ),
52+ GasLimit : 10 ,
53+ GasUsed : 11 ,
54+ Time : 12 ,
55+ Extra : []byte {13 },
56+ MixDigest : common.Hash {14 },
57+ Nonce : BlockNonce {15 },
58+ BaseFee : big .NewInt (16 ),
59+ WithdrawalsHash : & common.Hash {17 },
60+ BlobGasUsed : ptrTo (uint64 (18 )),
61+ ExcessBlobGas : ptrTo (uint64 (19 )),
62+ ParentBeaconRoot : & common.Hash {20 },
63+ }
64+ extras := & HeaderExtra {
65+ ExtDataHash : common.Hash {21 },
66+ ExtDataGasUsed : big .NewInt (22 ),
67+ BlockGasCost : big .NewInt (23 ),
68+ }
69+
70+ got := new (HeaderSerializable )
71+ got .updateFromEth (eth )
72+ got .updateFromExtras (extras )
73+
74+ allFieldsAreSet (t , got )
75+
76+ want := & HeaderSerializable {
77+ ParentHash : common.Hash {1 },
78+ UncleHash : common.Hash {2 },
79+ Coinbase : common.Address {3 },
80+ Root : common.Hash {4 },
81+ TxHash : common.Hash {5 },
82+ ReceiptHash : common.Hash {6 },
83+ Bloom : Bloom {7 },
84+ Difficulty : big .NewInt (8 ),
85+ Number : big .NewInt (9 ),
86+ GasLimit : 10 ,
87+ GasUsed : 11 ,
88+ Time : 12 ,
89+ Extra : []byte {13 },
90+ MixDigest : common.Hash {14 },
91+ Nonce : BlockNonce {15 },
92+ BaseFee : big .NewInt (16 ),
93+ BlobGasUsed : ptrTo (uint64 (18 )),
94+ ExcessBlobGas : ptrTo (uint64 (19 )),
95+ ParentBeaconRoot : & common.Hash {20 },
96+ ExtDataHash : common.Hash {21 },
97+ ExtDataGasUsed : big .NewInt (22 ),
98+ BlockGasCost : big .NewInt (23 ),
99+ }
100+ assert .Equal (t , want , got )
101+ })
102+
103+ t .Run ("to" , func (t * testing.T ) {
104+ t .Parallel ()
105+
106+ serializable := & HeaderSerializable {
107+ ParentHash : common.Hash {1 },
108+ UncleHash : common.Hash {2 },
109+ Coinbase : common.Address {3 },
110+ Root : common.Hash {4 },
111+ TxHash : common.Hash {5 },
112+ ReceiptHash : common.Hash {6 },
113+ Bloom : Bloom {7 },
114+ Difficulty : big .NewInt (8 ),
115+ Number : big .NewInt (9 ),
116+ GasLimit : 10 ,
117+ GasUsed : 11 ,
118+ Time : 12 ,
119+ Extra : []byte {13 },
120+ MixDigest : common.Hash {14 },
121+ Nonce : BlockNonce {15 },
122+ BaseFee : big .NewInt (16 ),
123+ BlobGasUsed : ptrTo (uint64 (18 )),
124+ ExcessBlobGas : ptrTo (uint64 (19 )),
125+ ParentBeaconRoot : & common.Hash {20 },
126+ ExtDataHash : common.Hash {21 },
127+ ExtDataGasUsed : big .NewInt (22 ),
128+ BlockGasCost : big .NewInt (23 ),
129+ }
130+ allFieldsAreSet (t , serializable )
131+
132+ eth := new (ethtypes.Header )
133+ extras := new (HeaderExtra )
134+
135+ serializable .updateToEth (eth )
136+ serializable .updateToExtras (extras )
137+
138+ // Note eth isn't checked for all its fields to be set
139+ // since some of its fields may not be required to be set,
140+ // such as WithdrawalsHash.
141+ allFieldsAreSet (t , extras )
142+
143+ wantedEth := & ethtypes.Header {
144+ ParentHash : common.Hash {1 },
145+ UncleHash : common.Hash {2 },
146+ Coinbase : common.Address {3 },
147+ Root : common.Hash {4 },
148+ TxHash : common.Hash {5 },
149+ ReceiptHash : common.Hash {6 },
150+ Bloom : Bloom {7 },
151+ Difficulty : big .NewInt (8 ),
152+ Number : big .NewInt (9 ),
153+ GasLimit : 10 ,
154+ GasUsed : 11 ,
155+ Time : 12 ,
156+ Extra : []byte {13 },
157+ MixDigest : common.Hash {14 },
158+ Nonce : BlockNonce {15 },
159+ BaseFee : big .NewInt (16 ),
160+ BlobGasUsed : ptrTo (uint64 (18 )),
161+ ExcessBlobGas : ptrTo (uint64 (19 )),
162+ ParentBeaconRoot : & common.Hash {20 },
163+ }
164+ wantedExtras := & HeaderExtra {
165+ ExtDataHash : common.Hash {21 },
166+ ExtDataGasUsed : big .NewInt (22 ),
167+ BlockGasCost : big .NewInt (23 ),
168+ }
169+
170+ assert .Equal (t , wantedEth , eth )
171+ assert .Equal (t , wantedExtras , extras )
172+ })
173+ }
0 commit comments