Skip to content

Commit 2d72751

Browse files
committed
Use big.int for chain ID in transactions.
1 parent 1c4c01d commit 2d72751

9 files changed

+814
-106
lines changed

spec/type0transaction.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929
type Type0Transaction struct {
3030
BlockHash *types.Hash
3131
BlockNumber *uint32
32-
ChainID *uint64
32+
ChainID *big.Int
3333
From types.Address
3434
Gas uint32
3535
GasPrice uint64
@@ -78,7 +78,7 @@ func (t *Type0Transaction) MarshalJSON() ([]byte, error) {
7878
}
7979
var chainID string
8080
if t.ChainID != nil {
81-
chainID = util.MarshalUint64(*t.ChainID)
81+
chainID = util.MarshalBigInt(t.ChainID)
8282
}
8383
to := ""
8484
if t.To != nil {
@@ -153,11 +153,10 @@ func (t *Type0Transaction) unpack(data *type0TransactionJSON) error {
153153
}
154154

155155
if data.ChainID != "" {
156-
chainID, err := strconv.ParseUint(util.PreUnmarshalHexString(data.ChainID), 16, 32)
157-
if err != nil {
158-
return errors.Wrap(err, "chain ID invalid")
156+
t.ChainID, success = new(big.Int).SetString(util.PreUnmarshalHexString(data.ChainID), 16)
157+
if !success {
158+
return errors.New("chain ID invalid")
159159
}
160-
t.ChainID = &chainID
161160
}
162161

163162
if data.From == "" {
@@ -246,7 +245,6 @@ func (t *Type0Transaction) unpack(data *type0TransactionJSON) error {
246245
if data.R == "" {
247246
return errors.New("r missing")
248247
}
249-
fmt.Printf("%v\n", data.R)
250248
t.R, success = new(big.Int).SetString(util.PreUnmarshalHexString(data.R), 16)
251249
if !success {
252250
return errors.New("r invalid")

spec/type1transaction.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ type Type1Transaction struct {
3131
AccessList []*AccessListEntry
3232
BlockHash *types.Hash
3333
BlockNumber *uint32
34-
ChainID uint64
34+
ChainID *big.Int
3535
From types.Address
3636
Gas uint32
3737
GasPrice uint64
@@ -92,7 +92,7 @@ func (t *Type1Transaction) MarshalJSON() ([]byte, error) {
9292
AccessList: t.AccessList,
9393
BlockHash: blockHash,
9494
BlockNumber: blockNumber,
95-
ChainID: util.MarshalUint64(t.ChainID),
95+
ChainID: util.MarshalBigInt(t.ChainID),
9696
From: util.MarshalByteArray(t.From[:]),
9797
Gas: util.MarshalUint32(t.Gas),
9898
GasPrice: util.MarshalUint64(t.GasPrice),
@@ -159,9 +159,9 @@ func (t *Type1Transaction) unpack(data *type1TransactionJSON) error {
159159
if data.ChainID == "" {
160160
return errors.New("chain id missing")
161161
}
162-
t.ChainID, err = strconv.ParseUint(util.PreUnmarshalHexString(data.ChainID), 16, 64)
163-
if err != nil {
164-
return errors.Wrap(err, "chain id invalid")
162+
t.ChainID, success = new(big.Int).SetString(util.PreUnmarshalHexString(data.ChainID), 16)
163+
if !success {
164+
return errors.New("chain id invalid")
165165
}
166166

167167
if data.From == "" {
@@ -250,7 +250,6 @@ func (t *Type1Transaction) unpack(data *type1TransactionJSON) error {
250250
if data.R == "" {
251251
return errors.New("r missing")
252252
}
253-
fmt.Printf("%v\n", data.R)
254253
t.R, success = new(big.Int).SetString(util.PreUnmarshalHexString(data.R), 16)
255254
if !success {
256255
return errors.New("r invalid")
@@ -274,7 +273,7 @@ func (t *Type1Transaction) MarshalRLP() ([]byte, error) {
274273
bufB := bytes.NewBuffer(make([]byte, 0, 1024))
275274

276275
// Transaction data.
277-
util.RLPUint64(bufA, t.ChainID)
276+
util.RLPBytes(bufA, t.ChainID.Bytes())
278277
util.RLPUint64(bufA, t.Nonce)
279278
util.RLPUint64(bufA, t.GasPrice)
280279
util.RLPUint64(bufA, uint64(t.Gas))

spec/type1transaction_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func TestType1TransactionJSON(t *testing.T) {
9595
{
9696
name: "ChainIDInvalid",
9797
input: []byte(`{"accessList":[],"blockHash":"0xb257c7e147b9dc4a5d9868522f7a7ff0949cd0a11231dd1f2ebe3f66c40982dd","blockNumber":"0xcff0c8","chainId":"true","from":"0x46eaadc8f2199463db26d1797131900575f0d264","gas":"0x4fd44","gasPrice":"0x5bf72b4854","hash":"0xc7d73aa9e0e43232010f7e28e4ad333be75f5df04dd1f9a30be80746dc42468b","input":"0xab9d206a0000000000000000000000000000000000000000000000000000000000cff0c80000000000000000000000008c54aa2a32a779e6f6fbea568ad85a19e0109c26000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000031d2054f6200000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001ba01a0b86991c6218b36c1d19d4a2e9eb0ce3606eb480001f4956f47f50a910163d8bf957cf5846d573e7f87ca8c54aa2a32a779e6f6fbea568ad85a19e0109c2600000000000000000000000000000000000000000000000000000031d2054f6294b0a3d511b6ecdb17ebf877278ab030acb0a878000000000000000000000000000000000000000000002d5baff8458b92ac5f8001c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20001f4a0b86991c6218b36c1d19d4a2e9eb0ce3606eb4888e6a0c2ddd26feeb64f039a2c41296fcb3f5640000000000000000000000000000000000000000000000002aea07fa399d7a0008c54aa2a32a779e6f6fbea568ad85a19e0109c2600000000000000000000000000000000000000000000000000000031d2054f620094b0a3d511b6ecdb17ebf877278ab030acb0a87801000000000000000000000000000000000000000000000002afe8dddb94ca63f28698d9d5ea99809c00426484a80be2add4e54581c02aaa39b223fe8d0a0e5c4f27ead9083c756cc288e6a0c2ddd26feeb64f039a2c41296fcb3f5640000000000000000000000000000000000000000000000002aea07fa399d7a000000000000000","nonce":"0xebea","r":"0x901ee907c0a921d816a73cac17405d6745c2354d6b046330780c9f281a995eeb","s":"0x7ccc54f6778c0cdbffed8cf9a1b96ef6b49a6c2e673d585471868ee7b7a9b355","to":"0x8698d9d5ea99809c00426484a80be2add4e54581","transactionIndex":"0x1","type":"0x1","v":"0x0","value":"0x0"}`),
98-
err: "chain id invalid: strconv.ParseUint: parsing \"true\": invalid syntax",
98+
err: "chain id invalid",
9999
},
100100
{
101101
name: "FromMissing",
@@ -345,7 +345,7 @@ func TestType1TransactionRLP(t *testing.T) {
345345
{
346346
name: "Function",
347347
input: &spec.Type1Transaction{
348-
ChainID: 1,
348+
ChainID: new(big.Int).SetBytes(byteslice("0x01")),
349349
Nonce: 6450,
350350
Gas: 478848,
351351
GasPrice: 125000000000,

spec/type2transaction.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type Type2Transaction struct {
3333
BlockHash *types.Hash
3434
// BlockNumber is only available for transactions included in a block, so optional.
3535
BlockNumber *uint32
36-
ChainID uint64
36+
ChainID *big.Int
3737
From types.Address
3838
Gas uint32
3939
// GasPrice is only available for transactions included in a block, so optional.
@@ -106,7 +106,7 @@ func (t *Type2Transaction) MarshalJSON() ([]byte, error) {
106106
AccessList: t.AccessList,
107107
BlockHash: blockHash,
108108
BlockNumber: blockNumber,
109-
ChainID: util.MarshalUint64(t.ChainID),
109+
ChainID: util.MarshalBigInt(t.ChainID),
110110
From: util.MarshalByteArray(t.From[:]),
111111
Gas: util.MarshalUint32(t.Gas),
112112
GasPrice: gasPrice,
@@ -175,9 +175,9 @@ func (t *Type2Transaction) unpack(data *type2TransactionJSON) error {
175175
if data.ChainID == "" {
176176
return errors.New("chain id missing")
177177
}
178-
t.ChainID, err = strconv.ParseUint(util.PreUnmarshalHexString(data.ChainID), 16, 64)
179-
if err != nil {
180-
return errors.Wrap(err, "chain id invalid")
178+
t.ChainID, success = new(big.Int).SetString(util.PreUnmarshalHexString(data.ChainID), 16)
179+
if !success {
180+
return errors.New("chain id invalid")
181181
}
182182

183183
if data.From == "" {
@@ -282,7 +282,6 @@ func (t *Type2Transaction) unpack(data *type2TransactionJSON) error {
282282
if data.R == "" {
283283
return errors.New("r missing")
284284
}
285-
fmt.Printf("%v\n", data.R)
286285
t.R, success = new(big.Int).SetString(util.PreUnmarshalHexString(data.R), 16)
287286
if !success {
288287
return errors.New("r invalid")
@@ -306,7 +305,7 @@ func (t *Type2Transaction) MarshalRLP() ([]byte, error) {
306305
bufB := bytes.NewBuffer(make([]byte, 0, 1024))
307306

308307
// Transaction data.
309-
util.RLPUint64(bufA, t.ChainID)
308+
util.RLPBytes(bufA, t.ChainID.Bytes())
310309
util.RLPUint64(bufA, t.Nonce)
311310
util.RLPUint64(bufA, t.MaxPriorityFeePerGas)
312311
util.RLPUint64(bufA, t.MaxFeePerGas)

spec/type2transaction_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func TestType2TransactionJSON(t *testing.T) {
100100
{
101101
name: "ChainIDInvalid",
102102
input: []byte(`{"accessList":[{"address":"0xceff51756c56ceffca006cd410b03ffc46dd3a58","storageKeys":["0x0000000000000000000000000000000000000000000000000000000000000008","0x0000000000000000000000000000000000000000000000000000000000000009","0x0000000000000000000000000000000000000000000000000000000000000007","0x000000000000000000000000000000000000000000000000000000000000000a","0x000000000000000000000000000000000000000000000000000000000000000c","0x0000000000000000000000000000000000000000000000000000000000000006"]},{"address":"0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2","storageKeys":["0x96693869bd9caff1b0916c4e11fe79467174d34d9d4aad910b52d5a6333d2192","0x30bd84b96629f958113934633d3bd1b64c3d259a85c57ceac65da8c5ec9bf3a7"]},{"address":"0xf424018c3d4473e014c1def44171772059f2d720","storageKeys":[]},{"address":"0x2260fac5e5542a773aa44fbcfedf7c193bc2c599","storageKeys":["0x14ac31782b75d0b8926aa44db6f6caf09df8257632d9c607f23247258a0f6d0b","0x631603854828263717f78aebdf44701ab316ae3c9d57ea74169f98df3affd293","0x0000000000000000000000000000000000000000000000000000000000000005","0xf7c84b5d1f3a0563cd20b346c00dcaaaf749870e80d340ce8dc6213863709a60"]}],"blockHash":"0xb257c7e147b9dc4a5d9868522f7a7ff0949cd0a11231dd1f2ebe3f66c40982dd","blockNumber":"0xcff0c8","chainId":"true","from":"0x9ce3ce3978cbee75df235a499503d719da697ceb","gas":"0x61a80","gasPrice":"0x1b301f66ae","hash":"0x3eb2f1bc543ced1004437c75ba58f2bd03b7b3592ef96da57808e9460c18cd01","input":"0x1cff79cd000000000000000000000000f424018c3d4473e014c1def44171772059f2d720000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000001042fdc7315000000000000000000000000ceff51756c56ceffca006cd410b03ffc46dd3a580000000000000000000000002260fac5e5542a773aa44fbcfedf7c193bc2c59900000000000000000000000056178a0d5f301baf6cf3e1cd53d9863437345bf900000000000000000000000000000000000000000000000000000004f74ba90000000000000000000000000000000000000000000012a25c7039192ee0d8a8000000000000000000000000000000000000000001c67aff541cec1086a24edef4000000000000000000000000000000000000000000000000000000006193d9fe330000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","maxFeePerGas":"0x1dd855f4d2","maxPriorityFeePerGas":"0x0","nonce":"0x8d89","r":"0x979132c4f106dfc44eb3ea9e24e654a6d73fd8f9081daa2b0576b6ec8bbe6e79","s":"0x461093462822fd309b9d20add4886fc4a76a190c97cb0779a26475cefbd81bce","to":"0xa57bd00134b2850b2a1c55860c9e9ea100fdd6cf","transactionIndex":"0x2","type":"0x2","v":"0x1","value":"0x0"}`),
103-
err: "chain id invalid: strconv.ParseUint: parsing \"true\": invalid syntax",
103+
err: "chain id invalid",
104104
},
105105
{
106106
name: "FromMissing",
@@ -341,7 +341,7 @@ func TestType2TransactionRLP(t *testing.T) {
341341
{
342342
name: "Transfer",
343343
input: &spec.Type2Transaction{
344-
ChainID: 1,
344+
ChainID: new(big.Int).SetBytes(byteslice("0x01")),
345345
Nonce: 2599,
346346
Gas: 21000,
347347
MaxPriorityFeePerGas: 1250000000,
@@ -357,7 +357,7 @@ func TestType2TransactionRLP(t *testing.T) {
357357
{
358358
name: "ContractCreation",
359359
input: &spec.Type2Transaction{
360-
ChainID: 1,
360+
ChainID: new(big.Int).SetBytes(byteslice("0x01")),
361361
Nonce: 2,
362362
Gas: 1006654,
363363
MaxPriorityFeePerGas: 1500000000,
@@ -373,7 +373,7 @@ func TestType2TransactionRLP(t *testing.T) {
373373
{
374374
name: "Function",
375375
input: &spec.Type2Transaction{
376-
ChainID: 1,
376+
ChainID: new(big.Int).SetBytes(byteslice("0x01")),
377377
Nonce: 177,
378378
Gas: 359305,
379379
MaxPriorityFeePerGas: 1500000000,
@@ -390,7 +390,7 @@ func TestType2TransactionRLP(t *testing.T) {
390390
{
391391
name: "AccessList",
392392
input: &spec.Type2Transaction{
393-
ChainID: 1,
393+
ChainID: new(big.Int).SetBytes(byteslice("0x01")),
394394
Nonce: 3157,
395395
Gas: 330030,
396396
MaxPriorityFeePerGas: 0,
@@ -466,7 +466,7 @@ func TestType2TransactionRLP(t *testing.T) {
466466

467467
func BenchmarkRLP(b *testing.B) {
468468
tx := &spec.Type2Transaction{
469-
ChainID: 1,
469+
ChainID: new(big.Int).SetBytes(byteslice("0x01")),
470470
Nonce: 3157,
471471
Gas: 330030,
472472
MaxPriorityFeePerGas: 0,

0 commit comments

Comments
 (0)