Skip to content

Commit ff7195a

Browse files
committed
params/confp/(internal,generic): refactor configurator unmarshaler, w/ test
- Removes networkId from stureby_geth.json file; I put that in there a while back as a workaround for exactly the issues addressed in this change set. This field is NOT included in goetherum chain config json marshaling. - Adds stureby_multigeth.json file * This test TestUnmarshalConfigurator IS NONDETERMINISTIC. WTF. Signed-off-by: meows <b5c6@protonmail.com>
1 parent 8eec618 commit ff7195a

File tree

6 files changed

+194
-23
lines changed

6 files changed

+194
-23
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ require (
5454
github.com/steakknife/hamming v0.0.0-20180906055917-c99c65617cd3 // indirect
5555
github.com/stretchr/testify v1.4.0
5656
github.com/syndtr/goleveldb v1.0.1-0.20190923125748-758128399b1d
57+
github.com/tidwall/gjson v1.3.5
5758
github.com/tyler-smith/go-bip39 v0.0.0-20181017060643-dbb3b84ba2ef
5859
github.com/wsddn/go-ecdh v0.0.0-20161211032359-48726bab9208
5960
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,12 @@ github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJy
179179
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
180180
github.com/syndtr/goleveldb v1.0.1-0.20190923125748-758128399b1d h1:gZZadD8H+fF+n9CmNhYL1Y0dJB+kLOmKd7FbPJLeGHs=
181181
github.com/syndtr/goleveldb v1.0.1-0.20190923125748-758128399b1d/go.mod h1:9OrXJhf154huy1nPWmuSrkgjPUtUNhA+Zmy+6AESzuA=
182+
github.com/tidwall/gjson v1.3.5 h1:2oW9FBNu8qt9jy5URgrzsVx/T/KSn3qn/smJQ0crlDQ=
183+
github.com/tidwall/gjson v1.3.5/go.mod h1:P256ACg0Mn+j1RXIDXoss50DeIABTYK1PULOJHhxOls=
184+
github.com/tidwall/match v1.0.1 h1:PnKP62LPNxHKTwvHHZZzdOAOCtsJTjo6dZLCwpKm5xc=
185+
github.com/tidwall/match v1.0.1/go.mod h1:LujAq0jyVjBy028G1WhWfIzbpQfMO8bBZ6Tyb0+pL9E=
186+
github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4=
187+
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
182188
github.com/tyler-smith/go-bip39 v0.0.0-20181017060643-dbb3b84ba2ef h1:luEzjJzktS9eU0CmI0uApXHLP/lKzOoRPrJhd71J8ik=
183189
github.com/tyler-smith/go-bip39 v0.0.0-20181017060643-dbb3b84ba2ef/go.mod h1:sJ5fKU0s6JVwZjjcUEX2zFOnvq0ASQ2K9Zr6cf67kNs=
184190
github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=

params/confp/generic/generic.go

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ package generic
1919

2020
import (
2121
"encoding/json"
22+
"errors"
2223
"fmt"
2324

2425
"github.com/ethereum/go-ethereum/params/types/ctypes"
2526
"github.com/ethereum/go-ethereum/params/types/goethereum"
2627
"github.com/ethereum/go-ethereum/params/types/multigeth"
2728
"github.com/ethereum/go-ethereum/params/types/parity"
2829
"github.com/ethereum/go-ethereum/params/vars"
30+
"github.com/tidwall/gjson"
2931
)
3032

3133
// GenericCC is a generic-y struct type used to expose some meta-logic methods
@@ -57,34 +59,51 @@ func (c GenericCC) DAOSupport() bool {
5759
panic(fmt.Sprintf("uimplemented DAO logic, config: %v", c.ChainConfigurator))
5860
}
5961

60-
func UnmarshalChainConfigurator(input []byte) (ctypes.ChainConfigurator, error) {
61-
var map1 = make(map[string]interface{})
62-
err := json.Unmarshal(input, &map1)
63-
if err != nil {
64-
return nil, err
62+
// Following vars define sufficient JSON schema keys for configurator type inference.
63+
var (
64+
paritySchemaKeysMust = []string{
65+
"engine",
66+
"genesis.seal",
6567
}
66-
if _, ok := map1["params"]; ok {
67-
pspec := &parity.ParityChainSpec{}
68-
err = json.Unmarshal(input, pspec)
69-
if err != nil {
70-
return nil, err
71-
}
72-
return pspec, nil
68+
multigethSchemaMust = []string{
69+
"networkId", "config.networkId",
70+
"eip2FBlock", "config.eip2FBlock",
71+
}
72+
goethereumSchemaMust = []string{
73+
"difficulty",
74+
"chainId", "config.chainId",
75+
"eip158Block", "config.eip158Block",
76+
"byzantiumBlock", "config.byzantiumBlock",
7377
}
78+
)
7479

75-
if _, ok := map1["networkId"]; ok {
76-
mspec := &multigeth.MultiGethChainConfig{}
77-
err = json.Unmarshal(input, mspec)
80+
func UnmarshalChainConfigurator(input []byte) (ctypes.ChainConfigurator, error) {
81+
var cases = map[ctypes.ChainConfigurator][]string{
82+
&parity.ParityChainSpec{}: paritySchemaKeysMust,
83+
&multigeth.MultiGethChainConfig{}: multigethSchemaMust,
84+
&goethereum.ChainConfig{}: goethereumSchemaMust,
85+
}
86+
for c, fn := range cases {
87+
ok, err := asMapHasAnyKey(input, fn)
7888
if err != nil {
7989
return nil, err
8090
}
81-
return mspec, nil
91+
if ok {
92+
if err := json.Unmarshal(input, c); err != nil {
93+
return nil, err
94+
}
95+
return c, nil
96+
}
8297
}
98+
return nil, errors.New("invalid configurator schema")
99+
}
83100

84-
gspec := &goethereum.ChainConfig{}
85-
err = json.Unmarshal(input,gspec)
86-
if err != nil {
87-
return nil, err
101+
func asMapHasAnyKey(input []byte, keys []string) (bool, error) {
102+
results := gjson.GetManyBytes(input, keys...)
103+
for _, g := range results {
104+
if g.Exists() {
105+
return true, nil
106+
}
88107
}
89-
return gspec, nil
90-
}
108+
return false, nil
109+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package generic
2+
3+
import (
4+
"encoding/json"
5+
"io/ioutil"
6+
"path/filepath"
7+
"reflect"
8+
"testing"
9+
10+
"github.com/ethereum/go-ethereum/params/types/goethereum"
11+
"github.com/ethereum/go-ethereum/params/types/multigeth"
12+
"github.com/ethereum/go-ethereum/params/types/parity"
13+
)
14+
15+
func TestUnmarshalChainConfigurator(t *testing.T) {
16+
cases := []struct{
17+
file string
18+
wantT interface{}
19+
}{
20+
{
21+
filepath.Join("..", "internal", "testdata", "stureby_parity.json"),
22+
&parity.ParityChainSpec{},
23+
},
24+
{
25+
filepath.Join("..", "internal", "testdata", "stureby_geth.json"),
26+
&goethereum.ChainConfig{},
27+
},
28+
{
29+
filepath.Join("..", "internal", "testdata", "stureby_multigeth.json"),
30+
&multigeth.MultiGethChainConfig{},
31+
},
32+
}
33+
34+
for i, c := range cases {
35+
b, err := ioutil.ReadFile(c.file)
36+
if err != nil {
37+
t.Fatal(err)
38+
}
39+
got, err := UnmarshalChainConfigurator(b)
40+
if err != nil {
41+
t.Fatal(err)
42+
}
43+
if reflect.TypeOf(got) != reflect.TypeOf(c.wantT) {
44+
gotb, _ := json.MarshalIndent(got, "", " ")
45+
t.Errorf(`%d / wrong type
46+
want: (%s)
47+
got: (%s)
48+
---
49+
file:
50+
%s
51+
---
52+
result:
53+
%s
54+
`,
55+
i,
56+
reflect.TypeOf(c.wantT).String(),
57+
reflect.TypeOf(got).String(),
58+
string(b),
59+
string(gotb),
60+
)
61+
}
62+
}
63+
}

params/confp/internal/testdata/stureby_geth.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{
22
"config": {
3-
"networkId": 314158,
43
"chainId": 314158,
54
"homesteadBlock": 10000,
65
"eip150Block": 15000,
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
{
2+
"config": {
3+
"networkId": 314158,
4+
"chainId": 314158,
5+
"eip2FBlock": 10000,
6+
"eip7FBlock": 10000,
7+
"eip150Block": 15000,
8+
"eip155Block": 23000,
9+
"eip160Block": 23000,
10+
"eip161FBlock": 23000,
11+
"eip170FBlock": 23000,
12+
"eip100FBlock": 30000,
13+
"eip140FBlock": 30000,
14+
"eip198FBlock": 30000,
15+
"eip211FBlock": 30000,
16+
"eip212FBlock": 30000,
17+
"eip213FBlock": 30000,
18+
"eip214FBlock": 30000,
19+
"eip658FBlock": 30000,
20+
"eip145FBlock": 40000,
21+
"eip1014FBlock": 40000,
22+
"eip1052FBlock": 40000,
23+
"eip1283FBlock": 40000,
24+
"petersburgBlock": 40000,
25+
"eip152FBlock": 50000,
26+
"eip1108FBlock": 50000,
27+
"eip1344FBlock": 50000,
28+
"eip1884FBlock": 50000,
29+
"eip2028FBlock": 50000,
30+
"eip2200FBlock": 50000,
31+
"ethash": {},
32+
"trustedCheckpoint": null,
33+
"trustedCheckpointOracle": null,
34+
"difficultyBombDelays": {
35+
"0x7530": "0x2dc6c0",
36+
"0x9c40": "0x4c4b40"
37+
},
38+
"blockReward": {
39+
"0x7530": "0x29a2241af62c0000",
40+
"0x9c40": "0x1bc16d674ec80000"
41+
},
42+
"requireBlockHashes": null
43+
},
44+
"nonce": "0x0",
45+
"timestamp": "0x59a4e76d",
46+
"extraData": "0x0000000000000000000000000000000000000000000000000000000b4dc0ffee",
47+
"gasLimit": "0x47b760",
48+
"difficulty": "0x20000",
49+
"mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
50+
"coinbase": "0x0000000000000000000000000000000000000000",
51+
"alloc": {
52+
"0000000000000000000000000000000000000001": {
53+
"balance": "0x1"
54+
},
55+
"0000000000000000000000000000000000000002": {
56+
"balance": "0x1"
57+
},
58+
"0000000000000000000000000000000000000003": {
59+
"balance": "0x1"
60+
},
61+
"0000000000000000000000000000000000000004": {
62+
"balance": "0x1"
63+
},
64+
"0000000000000000000000000000000000000005": {
65+
"balance": "0x1"
66+
},
67+
"0000000000000000000000000000000000000006": {
68+
"balance": "0x1"
69+
},
70+
"0000000000000000000000000000000000000007": {
71+
"balance": "0x1"
72+
},
73+
"0000000000000000000000000000000000000008": {
74+
"balance": "0x1"
75+
},
76+
"0000000000000000000000000000000000000009": {
77+
"balance": "0x1"
78+
}
79+
},
80+
"number": "0x0",
81+
"gasUsed": "0x0",
82+
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000"
83+
}

0 commit comments

Comments
 (0)