Skip to content

Commit 2040f8c

Browse files
mask-ppicemelon
andauthored
Fix bug about genesis file struct (ethereum#32)
* Fix bug about genesis file struct * Add test case if custom genesis file * Add test case if custom genesis file * Fix a mistake * Update Makefile Co-authored-by: Haichen Shen <[email protected]> Co-authored-by: Haichen Shen <[email protected]>
1 parent 69c291c commit 2040f8c

File tree

5 files changed

+91
-47
lines changed

5 files changed

+91
-47
lines changed

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ ios:
2929
@echo "Import \"$(GOBIN)/Geth.framework\" to use the library."
3030

3131
test: all
32+
# genesis test
33+
cd ${PWD}/cmd/geth; go test -test.run TestCustomGenesis
34+
# module test
3235
$(GORUN) build/ci.go test ./consensus ./core ./eth ./miner ./node ./trie
3336

3437
lint: ## Run linters.

cmd/geth/genesis_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,15 @@
1717
package main
1818

1919
import (
20+
"encoding/json"
21+
"fmt"
2022
"io/ioutil"
2123
"os"
2224
"path/filepath"
2325
"testing"
26+
27+
"github.com/scroll-tech/go-ethereum/common"
28+
"github.com/scroll-tech/go-ethereum/core"
2429
)
2530

2631
var customGenesisTests = []struct {
@@ -68,9 +73,41 @@ var customGenesisTests = []struct {
6873
},
6974
}
7075

76+
func addCustomGenesis() error {
77+
path, _ := os.Getwd()
78+
buf, err := os.ReadFile(fmt.Sprintf("%s/%s", path[:len(path)-len("/cmd/geth")], "genesis.json"))
79+
if err != nil {
80+
return err
81+
}
82+
genesis := &core.Genesis{}
83+
if err := json.Unmarshal(buf, genesis); err != nil {
84+
return err
85+
}
86+
if len(genesis.Alloc) == 0 {
87+
return nil
88+
}
89+
data := string(buf)
90+
for addr, balance := range genesis.Alloc {
91+
customGenesisTests = append(customGenesisTests, struct {
92+
genesis string
93+
query string
94+
result string
95+
}{
96+
genesis: data,
97+
query: fmt.Sprintf("eth.getBalance('%s')", addr.String()),
98+
result: common.Bytes2Hex(balance.Balance.Bytes()),
99+
})
100+
}
101+
102+
return nil
103+
}
104+
71105
// Tests that initializing Geth with a custom genesis block and chain definitions
72106
// work properly.
73107
func TestCustomGenesis(t *testing.T) {
108+
if err := addCustomGenesis(); err != nil {
109+
t.Error(err)
110+
}
74111
for i, tt := range customGenesisTests {
75112
// Create a temporary data directory to use and inspect later
76113
datadir := tmpdir(t)

genesis.json

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,34 @@
11
{
2-
"genesis": {
3-
"config": {
4-
"chainId": 53077,
5-
"homesteadBlock": 0,
6-
"eip150Block": 0,
7-
"eip150Hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
8-
"eip155Block": 0,
9-
"eip158Block": 0,
10-
"byzantiumBlock": 0,
11-
"constantinopleBlock": 0,
12-
"petersburgBlock": 0,
13-
"istanbulBlock": 0,
14-
"clique": {
15-
"period": 15,
16-
"epoch": 30000
17-
}
18-
},
19-
"nonce": "0x0",
20-
"timestamp": "0x61bc34a0",
21-
"extraData": "0x00000000000000000000000000000000000000000000000000000000000000004cb1ab63af5d8931ce09673ebd8ae2ce16fd65710000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
22-
"gasLimit": "0x47b760",
23-
"difficulty": "0x1",
24-
"mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
25-
"coinbase": "0x0000000000000000000000000000000000000000",
26-
"alloc": {
27-
"4cb1ab63af5d8931ce09673ebd8ae2ce16fd6571": {
28-
"balance": "0x200000000000000000000000000000000000000000000000000000000000000"
29-
}
30-
},
31-
"number": "0x0",
32-
"gasUsed": "0x0",
33-
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
34-
"baseFeePerGas": null
35-
}
2+
"config": {
3+
"chainId": 53077,
4+
"homesteadBlock": 0,
5+
"eip150Block": 0,
6+
"eip150Hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
7+
"eip155Block": 0,
8+
"eip158Block": 0,
9+
"byzantiumBlock": 0,
10+
"constantinopleBlock": 0,
11+
"petersburgBlock": 0,
12+
"istanbulBlock": 0,
13+
"clique": {
14+
"period": 15,
15+
"epoch": 30000
16+
}
17+
},
18+
"nonce": "0x0",
19+
"timestamp": "0x61bc34a0",
20+
"extraData": "0x00000000000000000000000000000000000000000000000000000000000000004cb1ab63af5d8931ce09673ebd8ae2ce16fd65710000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
21+
"gasLimit": "0x47b760",
22+
"difficulty": "0x1",
23+
"mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
24+
"coinbase": "0x0000000000000000000000000000000000000000",
25+
"alloc": {
26+
"4cb1ab63af5d8931ce09673ebd8ae2ce16fd6571": {
27+
"balance": "0x200000000000000000000000000000000000000000000000000000000000000"
28+
}
29+
},
30+
"number": "0x0",
31+
"gasUsed": "0x0",
32+
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
33+
"baseFeePerGas": null
3634
}

go.mod

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@ module github.com/scroll-tech/go-ethereum
33
go 1.15
44

55
require (
6-
github.com/Azure/azure-pipeline-go v0.2.2 // indirect
76
github.com/Azure/azure-storage-blob-go v0.7.0
8-
github.com/Azure/go-autorest/autorest/adal v0.8.0 // indirect
9-
github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6 // indirect
107
github.com/VictoriaMetrics/fastcache v1.6.0
118
github.com/aws/aws-sdk-go-v2 v1.2.0
129
github.com/aws/aws-sdk-go-v2/config v1.1.1
@@ -18,15 +15,13 @@ require (
1815
github.com/consensys/gnark-crypto v0.4.1-0.20210426202927-39ac3d4b3f1f
1916
github.com/davecgh/go-spew v1.1.1
2017
github.com/deckarep/golang-set v0.0.0-20180603214616-504e848d77ea
21-
github.com/deepmap/oapi-codegen v1.8.2 // indirect
2218
github.com/docker/docker v1.4.2-0.20180625184442-8e610b2b55bf
2319
github.com/dop251/goja v0.0.0-20211011172007-d99e4b8cbf48
2420
github.com/edsrzf/mmap-go v1.0.0
2521
github.com/ethereum/go-ethereum v1.10.13
2622
github.com/fatih/color v1.7.0
2723
github.com/fjl/memsize v0.0.0-20190710130421-bcb5799ab5e5
2824
github.com/gballet/go-libpcsclite v0.0.0-20190607065134-2772fd86a8ff
29-
github.com/go-ole/go-ole v1.2.1 // indirect
3025
github.com/go-stack/stack v1.8.0
3126
github.com/golang/protobuf v1.4.3
3227
github.com/golang/snappy v0.0.4
@@ -41,36 +36,29 @@ require (
4136
github.com/huin/goupnp v1.0.2
4237
github.com/influxdata/influxdb v1.8.3
4338
github.com/influxdata/influxdb-client-go/v2 v2.4.0
44-
github.com/influxdata/line-protocol v0.0.0-20210311194329-9aa0e372d097 // indirect
4539
github.com/jackpal/go-nat-pmp v1.0.2-0.20160603034137-1fa385a6f458
4640
github.com/jedisct1/go-minisign v0.0.0-20190909160543-45766022959e
4741
github.com/julienschmidt/httprouter v1.2.0
4842
github.com/karalabe/usb v0.0.0-20211005121534-4c5740d64559
49-
github.com/kylelemons/godebug v1.1.0 // indirect
5043
github.com/mattn/go-colorable v0.1.8
5144
github.com/mattn/go-isatty v0.0.12
52-
github.com/naoina/go-stringutil v0.1.0 // indirect
5345
github.com/naoina/toml v0.1.2-0.20170918210437-9fafd6967416
5446
github.com/olekukonko/tablewriter v0.0.5
5547
github.com/peterh/liner v1.1.1-0.20190123174540-a2c9a5303de7
56-
github.com/pkg/errors v0.9.1
5748
github.com/prometheus/tsdb v0.7.1
5849
github.com/rjeczalik/notify v0.9.1
5950
github.com/rs/cors v1.7.0
6051
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible
6152
github.com/status-im/keycard-go v0.0.0-20190316090335-8537d3370df4
6253
github.com/stretchr/testify v1.7.0
6354
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7
64-
github.com/tklauser/go-sysconf v0.3.5 // indirect
6555
github.com/tyler-smith/go-bip39 v1.0.1-0.20181017060643-dbb3b84ba2ef
6656
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2
67-
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d // indirect
6857
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
6958
golang.org/x/sys v0.0.0-20210816183151-1e6c022a8912
7059
golang.org/x/text v0.3.6
7160
golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba
7261
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce
7362
gopkg.in/olebedev/go-duktape.v3 v3.0.0-20200619000410-60c24ae608a6
7463
gopkg.in/urfave/cli.v1 v1.20.0
75-
gotest.tools v2.2.0+incompatible // indirect
7664
)

internal/cmdtest/test_cmd.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"fmt"
2323
"io"
2424
"io/ioutil"
25+
"math/big"
2526
"os"
2627
"os/exec"
2728
"regexp"
@@ -34,6 +35,7 @@ import (
3435
"time"
3536

3637
"github.com/docker/docker/pkg/reexec"
38+
"github.com/scroll-tech/go-ethereum/common"
3739
)
3840

3941
func NewTestCmd(t *testing.T, data interface{}) *TestCmd {
@@ -165,7 +167,7 @@ func (tt *TestCmd) ExpectRegexp(regex string) (*regexp.Regexp, []string) {
165167
)
166168
tt.withKillTimeout(func() { matches = re.FindReaderSubmatchIndex(rtee) })
167169
output := rtee.buf.Bytes()
168-
if matches == nil {
170+
if matches == nil && !bigCmp(string(output), regex) {
169171
tt.Fatalf("Output did not match:\n---------------- (stdout text)\n%s\n---------------- (regular expression)\n%s",
170172
output, regex)
171173
return re, nil
@@ -179,6 +181,22 @@ func (tt *TestCmd) ExpectRegexp(regex string) (*regexp.Regexp, []string) {
179181
return re, submatches
180182
}
181183

184+
// Scientific notation compare with Hexadecimal string
185+
// e.g. 9.04625697166532776746648320380374280103671755200316906558262375061821325312e+74 compare with 0x200000000000000000000000000000000000000000000000000000000000000
186+
func bigCmp(val, content string) bool {
187+
val = strings.Trim(val, "\n")
188+
flt, _, err := big.ParseFloat(val, 10, 0, big.ToNearestAway)
189+
if err != nil {
190+
return false
191+
}
192+
bigData := new(big.Int).SetBytes(common.FromHex(content))
193+
194+
compare := new(big.Float)
195+
compare.SetInt(bigData)
196+
197+
return compare.Cmp(flt) == 0
198+
}
199+
182200
// ExpectExit expects the child process to exit within 5s without
183201
// printing any additional text on stdout.
184202
func (tt *TestCmd) ExpectExit() {

0 commit comments

Comments
 (0)