Skip to content

Commit adb6c00

Browse files
committed
.github/workflows,params/types/genesisT: change Ignore strategy; keep original generated file tracked
With the ignore strategy before, we would miss cases where, say, a field was added to the genesis type. Our 'hotpatched' version of the file would overwrite the newly-generated version, and we would not assert that our hotpatched version was 'on top of' the latest generated version. With this change, we generate the interested file (gen_genesis) to a filepath with a .go.orig extension, and track that 'original' version, using it to compare against the 'go generate' output; signalling to us if anything we assume changes. Date: 2023-01-12 07:08:34-08:00 Signed-off-by: meows <[email protected]>
1 parent 633eae8 commit adb6c00

File tree

3 files changed

+131
-11
lines changed

3 files changed

+131
-11
lines changed

.github/workflows/go-generate-check.yml

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,7 @@ env:
88
# https://github.com/ethereum/go-ethereum/blob/master/accounts/usbwallet/trezor/trezor.go#L21-L43
99
GENERATE_EXCEPTIONS: |
1010
trezor
11-
# GENERATE_IGNORES are applied after the 'go generate' command.
12-
# These files will be reverted after 'go generate' to their original ref version.
13-
#
14-
# - gen_genesis.go: file which has custom modifications for unmarshaling the various genesis formats
15-
GENERATE_IGNORES: |
16-
params/types/genesisT/gen_genesis.go
11+
1712
jobs:
1813
go-generate-check:
1914
name: Check if "go generate" has been run
@@ -46,10 +41,6 @@ jobs:
4641
list="$(grep -v "$pattern" <<< "$list")"
4742
done
4843
go generate "$list"
49-
- name: Skip whitelisted generated go files
50-
id: skip-files
51-
run: |
52-
git checkout -- ${GENERATE_IGNORES[@]}
5344
5445
- name: Check for modified files
5546
id: git-check
@@ -59,3 +50,8 @@ jobs:
5950
git status
6051
exit 1
6152
fi
53+
- name: failer
54+
id: failer
55+
run: |
56+
echo "no joy"
57+
exit 1
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
// Code generated by github.com/fjl/gencodec. DO NOT EDIT.
2+
3+
package genesisT
4+
5+
import (
6+
"encoding/json"
7+
"errors"
8+
"math/big"
9+
10+
"github.com/ethereum/go-ethereum/common"
11+
"github.com/ethereum/go-ethereum/common/hexutil"
12+
"github.com/ethereum/go-ethereum/common/math"
13+
"github.com/ethereum/go-ethereum/params/types/ctypes"
14+
)
15+
16+
var _ = (*genesisSpecMarshaling)(nil)
17+
18+
// MarshalJSON marshals as JSON.
19+
func (g Genesis) MarshalJSON() ([]byte, error) {
20+
type Genesis struct {
21+
Config ctypes.ChainConfigurator `json:"config"`
22+
Nonce math.HexOrDecimal64 `json:"nonce"`
23+
Timestamp math.HexOrDecimal64 `json:"timestamp"`
24+
ExtraData hexutil.Bytes `json:"extraData"`
25+
GasLimit math.HexOrDecimal64 `json:"gasLimit" gencodec:"required"`
26+
Difficulty *math.HexOrDecimal256 `json:"difficulty" gencodec:"required"`
27+
Mixhash common.Hash `json:"mixHash"`
28+
Coinbase common.Address `json:"coinbase"`
29+
Alloc map[common.UnprefixedAddress]GenesisAccount `json:"alloc" gencodec:"required"`
30+
Number math.HexOrDecimal64 `json:"number"`
31+
GasUsed math.HexOrDecimal64 `json:"gasUsed"`
32+
ParentHash common.Hash `json:"parentHash"`
33+
BaseFee *math.HexOrDecimal256 `json:"baseFeePerGas"`
34+
}
35+
var enc Genesis
36+
enc.Config = g.Config
37+
enc.Nonce = math.HexOrDecimal64(g.Nonce)
38+
enc.Timestamp = math.HexOrDecimal64(g.Timestamp)
39+
enc.ExtraData = g.ExtraData
40+
enc.GasLimit = math.HexOrDecimal64(g.GasLimit)
41+
enc.Difficulty = (*math.HexOrDecimal256)(g.Difficulty)
42+
enc.Mixhash = g.Mixhash
43+
enc.Coinbase = g.Coinbase
44+
if g.Alloc != nil {
45+
enc.Alloc = make(map[common.UnprefixedAddress]GenesisAccount, len(g.Alloc))
46+
for k, v := range g.Alloc {
47+
enc.Alloc[common.UnprefixedAddress(k)] = v
48+
}
49+
}
50+
enc.Number = math.HexOrDecimal64(g.Number)
51+
enc.GasUsed = math.HexOrDecimal64(g.GasUsed)
52+
enc.ParentHash = g.ParentHash
53+
enc.BaseFee = (*math.HexOrDecimal256)(g.BaseFee)
54+
return json.Marshal(&enc)
55+
}
56+
57+
// UnmarshalJSON unmarshals from JSON.
58+
func (g *Genesis) UnmarshalJSON(input []byte) error {
59+
type Genesis struct {
60+
Config ctypes.ChainConfigurator `json:"config"`
61+
Nonce *math.HexOrDecimal64 `json:"nonce"`
62+
Timestamp *math.HexOrDecimal64 `json:"timestamp"`
63+
ExtraData *hexutil.Bytes `json:"extraData"`
64+
GasLimit *math.HexOrDecimal64 `json:"gasLimit" gencodec:"required"`
65+
Difficulty *math.HexOrDecimal256 `json:"difficulty" gencodec:"required"`
66+
Mixhash *common.Hash `json:"mixHash"`
67+
Coinbase *common.Address `json:"coinbase"`
68+
Alloc map[common.UnprefixedAddress]GenesisAccount `json:"alloc" gencodec:"required"`
69+
Number *math.HexOrDecimal64 `json:"number"`
70+
GasUsed *math.HexOrDecimal64 `json:"gasUsed"`
71+
ParentHash *common.Hash `json:"parentHash"`
72+
BaseFee *math.HexOrDecimal256 `json:"baseFeePerGas"`
73+
}
74+
var dec Genesis
75+
if err := json.Unmarshal(input, &dec); err != nil {
76+
return err
77+
}
78+
if dec.Config != nil {
79+
g.Config = dec.Config
80+
}
81+
if dec.Nonce != nil {
82+
g.Nonce = uint64(*dec.Nonce)
83+
}
84+
if dec.Timestamp != nil {
85+
g.Timestamp = uint64(*dec.Timestamp)
86+
}
87+
if dec.ExtraData != nil {
88+
g.ExtraData = *dec.ExtraData
89+
}
90+
if dec.GasLimit == nil {
91+
return errors.New("missing required field 'gasLimit' for Genesis")
92+
}
93+
g.GasLimit = uint64(*dec.GasLimit)
94+
if dec.Difficulty == nil {
95+
return errors.New("missing required field 'difficulty' for Genesis")
96+
}
97+
g.Difficulty = (*big.Int)(dec.Difficulty)
98+
if dec.Mixhash != nil {
99+
g.Mixhash = *dec.Mixhash
100+
}
101+
if dec.Coinbase != nil {
102+
g.Coinbase = *dec.Coinbase
103+
}
104+
if dec.Alloc == nil {
105+
return errors.New("missing required field 'alloc' for Genesis")
106+
}
107+
g.Alloc = make(GenesisAlloc, len(dec.Alloc))
108+
for k, v := range dec.Alloc {
109+
g.Alloc[common.Address(k)] = v
110+
}
111+
if dec.Number != nil {
112+
g.Number = uint64(*dec.Number)
113+
}
114+
if dec.GasUsed != nil {
115+
g.GasUsed = uint64(*dec.GasUsed)
116+
}
117+
if dec.ParentHash != nil {
118+
g.ParentHash = *dec.ParentHash
119+
}
120+
if dec.BaseFee != nil {
121+
g.BaseFee = (*big.Int)(dec.BaseFee)
122+
}
123+
return nil
124+
}

params/types/genesisT/genesis.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import (
3232
"github.com/ethereum/go-ethereum/rlp"
3333
)
3434

35-
//go:generate go run github.com/fjl/gencodec -type Genesis -field-override genesisSpecMarshaling -out gen_genesis.go
35+
//go:generate go run github.com/fjl/gencodec -type Genesis -field-override genesisSpecMarshaling -out gen_genesis.go.orig
3636
//go:generate go run github.com/fjl/gencodec -type GenesisAccount -field-override genesisAccountMarshaling -out gen_genesis_account.go
3737

3838
var ErrGenesisNoConfig = errors.New("genesis has no chain configuration")

0 commit comments

Comments
 (0)