Skip to content

Commit

Permalink
Use big.int for chain ID in transactions.
Browse files Browse the repository at this point in the history
  • Loading branch information
mcdee committed Dec 3, 2023
1 parent 1c4c01d commit 2d72751
Show file tree
Hide file tree
Showing 9 changed files with 814 additions and 106 deletions.
12 changes: 5 additions & 7 deletions spec/type0transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
type Type0Transaction struct {
BlockHash *types.Hash
BlockNumber *uint32
ChainID *uint64
ChainID *big.Int
From types.Address
Gas uint32
GasPrice uint64
Expand Down Expand Up @@ -78,7 +78,7 @@ func (t *Type0Transaction) MarshalJSON() ([]byte, error) {
}
var chainID string
if t.ChainID != nil {
chainID = util.MarshalUint64(*t.ChainID)
chainID = util.MarshalBigInt(t.ChainID)
}
to := ""
if t.To != nil {
Expand Down Expand Up @@ -153,11 +153,10 @@ func (t *Type0Transaction) unpack(data *type0TransactionJSON) error {
}

if data.ChainID != "" {
chainID, err := strconv.ParseUint(util.PreUnmarshalHexString(data.ChainID), 16, 32)
if err != nil {
return errors.Wrap(err, "chain ID invalid")
t.ChainID, success = new(big.Int).SetString(util.PreUnmarshalHexString(data.ChainID), 16)
if !success {
return errors.New("chain ID invalid")
}
t.ChainID = &chainID
}

if data.From == "" {
Expand Down Expand Up @@ -246,7 +245,6 @@ func (t *Type0Transaction) unpack(data *type0TransactionJSON) error {
if data.R == "" {
return errors.New("r missing")
}
fmt.Printf("%v\n", data.R)
t.R, success = new(big.Int).SetString(util.PreUnmarshalHexString(data.R), 16)
if !success {
return errors.New("r invalid")
Expand Down
13 changes: 6 additions & 7 deletions spec/type1transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type Type1Transaction struct {
AccessList []*AccessListEntry
BlockHash *types.Hash
BlockNumber *uint32
ChainID uint64
ChainID *big.Int
From types.Address
Gas uint32
GasPrice uint64
Expand Down Expand Up @@ -92,7 +92,7 @@ func (t *Type1Transaction) MarshalJSON() ([]byte, error) {
AccessList: t.AccessList,
BlockHash: blockHash,
BlockNumber: blockNumber,
ChainID: util.MarshalUint64(t.ChainID),
ChainID: util.MarshalBigInt(t.ChainID),
From: util.MarshalByteArray(t.From[:]),
Gas: util.MarshalUint32(t.Gas),
GasPrice: util.MarshalUint64(t.GasPrice),
Expand Down Expand Up @@ -159,9 +159,9 @@ func (t *Type1Transaction) unpack(data *type1TransactionJSON) error {
if data.ChainID == "" {
return errors.New("chain id missing")
}
t.ChainID, err = strconv.ParseUint(util.PreUnmarshalHexString(data.ChainID), 16, 64)
if err != nil {
return errors.Wrap(err, "chain id invalid")
t.ChainID, success = new(big.Int).SetString(util.PreUnmarshalHexString(data.ChainID), 16)
if !success {
return errors.New("chain id invalid")
}

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

// Transaction data.
util.RLPUint64(bufA, t.ChainID)
util.RLPBytes(bufA, t.ChainID.Bytes())
util.RLPUint64(bufA, t.Nonce)
util.RLPUint64(bufA, t.GasPrice)
util.RLPUint64(bufA, uint64(t.Gas))
Expand Down
4 changes: 2 additions & 2 deletions spec/type1transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func TestType1TransactionJSON(t *testing.T) {
{
name: "ChainIDInvalid",
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"}`),
err: "chain id invalid: strconv.ParseUint: parsing \"true\": invalid syntax",
err: "chain id invalid",
},
{
name: "FromMissing",
Expand Down Expand Up @@ -345,7 +345,7 @@ func TestType1TransactionRLP(t *testing.T) {
{
name: "Function",
input: &spec.Type1Transaction{
ChainID: 1,
ChainID: new(big.Int).SetBytes(byteslice("0x01")),
Nonce: 6450,
Gas: 478848,
GasPrice: 125000000000,
Expand Down
13 changes: 6 additions & 7 deletions spec/type2transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type Type2Transaction struct {
BlockHash *types.Hash
// BlockNumber is only available for transactions included in a block, so optional.
BlockNumber *uint32
ChainID uint64
ChainID *big.Int
From types.Address
Gas uint32
// GasPrice is only available for transactions included in a block, so optional.
Expand Down Expand Up @@ -106,7 +106,7 @@ func (t *Type2Transaction) MarshalJSON() ([]byte, error) {
AccessList: t.AccessList,
BlockHash: blockHash,
BlockNumber: blockNumber,
ChainID: util.MarshalUint64(t.ChainID),
ChainID: util.MarshalBigInt(t.ChainID),
From: util.MarshalByteArray(t.From[:]),
Gas: util.MarshalUint32(t.Gas),
GasPrice: gasPrice,
Expand Down Expand Up @@ -175,9 +175,9 @@ func (t *Type2Transaction) unpack(data *type2TransactionJSON) error {
if data.ChainID == "" {
return errors.New("chain id missing")
}
t.ChainID, err = strconv.ParseUint(util.PreUnmarshalHexString(data.ChainID), 16, 64)
if err != nil {
return errors.Wrap(err, "chain id invalid")
t.ChainID, success = new(big.Int).SetString(util.PreUnmarshalHexString(data.ChainID), 16)
if !success {
return errors.New("chain id invalid")
}

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

// Transaction data.
util.RLPUint64(bufA, t.ChainID)
util.RLPBytes(bufA, t.ChainID.Bytes())
util.RLPUint64(bufA, t.Nonce)
util.RLPUint64(bufA, t.MaxPriorityFeePerGas)
util.RLPUint64(bufA, t.MaxFeePerGas)
Expand Down
12 changes: 6 additions & 6 deletions spec/type2transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func TestType2TransactionJSON(t *testing.T) {
{
name: "ChainIDInvalid",
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"}`),
err: "chain id invalid: strconv.ParseUint: parsing \"true\": invalid syntax",
err: "chain id invalid",
},
{
name: "FromMissing",
Expand Down Expand Up @@ -341,7 +341,7 @@ func TestType2TransactionRLP(t *testing.T) {
{
name: "Transfer",
input: &spec.Type2Transaction{
ChainID: 1,
ChainID: new(big.Int).SetBytes(byteslice("0x01")),
Nonce: 2599,
Gas: 21000,
MaxPriorityFeePerGas: 1250000000,
Expand All @@ -357,7 +357,7 @@ func TestType2TransactionRLP(t *testing.T) {
{
name: "ContractCreation",
input: &spec.Type2Transaction{
ChainID: 1,
ChainID: new(big.Int).SetBytes(byteslice("0x01")),
Nonce: 2,
Gas: 1006654,
MaxPriorityFeePerGas: 1500000000,
Expand All @@ -373,7 +373,7 @@ func TestType2TransactionRLP(t *testing.T) {
{
name: "Function",
input: &spec.Type2Transaction{
ChainID: 1,
ChainID: new(big.Int).SetBytes(byteslice("0x01")),
Nonce: 177,
Gas: 359305,
MaxPriorityFeePerGas: 1500000000,
Expand All @@ -390,7 +390,7 @@ func TestType2TransactionRLP(t *testing.T) {
{
name: "AccessList",
input: &spec.Type2Transaction{
ChainID: 1,
ChainID: new(big.Int).SetBytes(byteslice("0x01")),
Nonce: 3157,
Gas: 330030,
MaxPriorityFeePerGas: 0,
Expand Down Expand Up @@ -466,7 +466,7 @@ func TestType2TransactionRLP(t *testing.T) {

func BenchmarkRLP(b *testing.B) {
tx := &spec.Type2Transaction{
ChainID: 1,
ChainID: new(big.Int).SetBytes(byteslice("0x01")),
Nonce: 3157,
Gas: 330030,
MaxPriorityFeePerGas: 0,
Expand Down
Loading

0 comments on commit 2d72751

Please sign in to comment.