From 669527f3828f85c7e58f4d5c377075cb1c834f62 Mon Sep 17 00:00:00 2001 From: Rachit Sonthalia Date: Mon, 4 Sep 2023 18:30:16 +0530 Subject: [PATCH 1/6] added baseFee in block header --- structs.go | 1 + structs_unmarshal.go | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/structs.go b/structs.go index 699d5899..2b3920c1 100644 --- a/structs.go +++ b/structs.go @@ -148,6 +148,7 @@ type Block struct { Transactions []*Transaction TransactionsHashes []Hash Uncles []Hash + BaseFee uint64 } func (b *Block) Copy() *Block { diff --git a/structs_unmarshal.go b/structs_unmarshal.go index a3f28c0b..903af587 100644 --- a/structs_unmarshal.go +++ b/structs_unmarshal.go @@ -104,6 +104,10 @@ func (b *Block) UnmarshalJSON(buf []byte) error { b.Uncles = append(b.Uncles, h) } + if b.BaseFee, err = decodeUint(v, "baseFeePerGas"); err != nil { + return err + } + return nil } From 95728808524a7e829a39b705bc12eb3f610c3526 Mon Sep 17 00:00:00 2001 From: Rachit Sonthalia Date: Mon, 4 Sep 2023 19:07:31 +0530 Subject: [PATCH 2/6] baseFee in block marshalling --- structs_marshal.go | 1 + 1 file changed, 1 insertion(+) diff --git a/structs_marshal.go b/structs_marshal.go index b88e079e..43361500 100644 --- a/structs_marshal.go +++ b/structs_marshal.go @@ -65,6 +65,7 @@ func (t *Block) MarshalJSON() ([]byte, error) { o.Set("extraData", a.NewString("0x"+hex.EncodeToString(t.ExtraData))) o.Set("mixHash", a.NewString("0x"+hex.EncodeToString(t.MixHash[:]))) o.Set("nonce", a.NewString("0x"+hex.EncodeToString(t.Nonce[:]))) + o.Set("baseFeePerGas", a.NewString(fmt.Sprintf("0x%x", t.BaseFee))) // uncles if len(t.Uncles) != 0 { From 4861ef315f5bde520f6c02f114f27fc9f813509c Mon Sep 17 00:00:00 2001 From: Rachit Sonthalia Date: Mon, 4 Sep 2023 19:09:43 +0530 Subject: [PATCH 3/6] corrected implementation --- structs_unmarshal.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/structs_unmarshal.go b/structs_unmarshal.go index 903af587..d90f77ae 100644 --- a/structs_unmarshal.go +++ b/structs_unmarshal.go @@ -67,6 +67,9 @@ func (b *Block) UnmarshalJSON(buf []byte) error { if b.ExtraData, err = decodeBytes(b.ExtraData[:0], v, "extraData"); err != nil { return err } + if b.BaseFee, err = decodeUint(v, "baseFeePerGas"); err != nil { + return err + } b.TransactionsHashes = b.TransactionsHashes[:0] b.Transactions = b.Transactions[:0] @@ -104,10 +107,6 @@ func (b *Block) UnmarshalJSON(buf []byte) error { b.Uncles = append(b.Uncles, h) } - if b.BaseFee, err = decodeUint(v, "baseFeePerGas"); err != nil { - return err - } - return nil } From 72fdee4729e4324273cca14e810dab35092b1145 Mon Sep 17 00:00:00 2001 From: Rachit Sonthalia Date: Mon, 4 Sep 2023 19:41:48 +0530 Subject: [PATCH 4/6] refactored and corrected implementation --- structs.go | 2 +- structs_marshal.go | 5 ++++- structs_unmarshal.go | 6 ++++-- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/structs.go b/structs.go index 2b3920c1..c5b0c156 100644 --- a/structs.go +++ b/structs.go @@ -148,7 +148,7 @@ type Block struct { Transactions []*Transaction TransactionsHashes []Hash Uncles []Hash - BaseFee uint64 + BaseFee *big.Int } func (b *Block) Copy() *Block { diff --git a/structs_marshal.go b/structs_marshal.go index 43361500..7cb25071 100644 --- a/structs_marshal.go +++ b/structs_marshal.go @@ -65,7 +65,10 @@ func (t *Block) MarshalJSON() ([]byte, error) { o.Set("extraData", a.NewString("0x"+hex.EncodeToString(t.ExtraData))) o.Set("mixHash", a.NewString("0x"+hex.EncodeToString(t.MixHash[:]))) o.Set("nonce", a.NewString("0x"+hex.EncodeToString(t.Nonce[:]))) - o.Set("baseFeePerGas", a.NewString(fmt.Sprintf("0x%x", t.BaseFee))) + + if t.BaseFee != nil { + o.Set("baseFeePerGas", a.NewString(fmt.Sprintf("0x%x", t.BaseFee))) + } // uncles if len(t.Uncles) != 0 { diff --git a/structs_unmarshal.go b/structs_unmarshal.go index d90f77ae..25500ced 100644 --- a/structs_unmarshal.go +++ b/structs_unmarshal.go @@ -67,8 +67,10 @@ func (b *Block) UnmarshalJSON(buf []byte) error { if b.ExtraData, err = decodeBytes(b.ExtraData[:0], v, "extraData"); err != nil { return err } - if b.BaseFee, err = decodeUint(v, "baseFeePerGas"); err != nil { - return err + if b.BaseFee, err = decodeBigInt(b.BaseFee, v, "baseFee"); err != nil { + if err.Error() != "field baseFee not found" { + return err + } } b.TransactionsHashes = b.TransactionsHashes[:0] From 112e37f7f8072d9a1488af6083c3a0afdd53939c Mon Sep 17 00:00:00 2001 From: Rachit Sonthalia Date: Mon, 4 Sep 2023 19:42:54 +0530 Subject: [PATCH 5/6] fix typo --- structs_marshal.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/structs_marshal.go b/structs_marshal.go index 7cb25071..a8f2ec11 100644 --- a/structs_marshal.go +++ b/structs_marshal.go @@ -67,7 +67,7 @@ func (t *Block) MarshalJSON() ([]byte, error) { o.Set("nonce", a.NewString("0x"+hex.EncodeToString(t.Nonce[:]))) if t.BaseFee != nil { - o.Set("baseFeePerGas", a.NewString(fmt.Sprintf("0x%x", t.BaseFee))) + o.Set("baseFee", a.NewString(fmt.Sprintf("0x%x", t.BaseFee))) } // uncles From fd688a06f570dea11ec8bac790eba8c37b4bdaba Mon Sep 17 00:00:00 2001 From: Rachit Sonthalia Date: Tue, 5 Sep 2023 18:17:39 +0530 Subject: [PATCH 6/6] fix error string --- structs_unmarshal.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/structs_unmarshal.go b/structs_unmarshal.go index 25500ced..66c14d9c 100644 --- a/structs_unmarshal.go +++ b/structs_unmarshal.go @@ -68,7 +68,7 @@ func (b *Block) UnmarshalJSON(buf []byte) error { return err } if b.BaseFee, err = decodeBigInt(b.BaseFee, v, "baseFee"); err != nil { - if err.Error() != "field baseFee not found" { + if err.Error() != "field 'baseFee' not found" { return err } }