From 2040f8c78170b0c0d95b95361ded7a5e4848ca2e Mon Sep 17 00:00:00 2001 From: maskpp <32827930+mask-pp@users.noreply.github.com> Date: Wed, 23 Feb 2022 16:01:59 +0800 Subject: [PATCH] Fix bug about genesis file struct (#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 Co-authored-by: Haichen Shen --- Makefile | 3 ++ cmd/geth/genesis_test.go | 37 ++++++++++++++++++++ genesis.json | 66 +++++++++++++++++------------------- go.mod | 12 ------- internal/cmdtest/test_cmd.go | 20 ++++++++++- 5 files changed, 91 insertions(+), 47 deletions(-) diff --git a/Makefile b/Makefile index 32aa088ce046..235da0595f29 100644 --- a/Makefile +++ b/Makefile @@ -29,6 +29,9 @@ ios: @echo "Import \"$(GOBIN)/Geth.framework\" to use the library." test: all + # genesis test + cd ${PWD}/cmd/geth; go test -test.run TestCustomGenesis + # module test $(GORUN) build/ci.go test ./consensus ./core ./eth ./miner ./node ./trie lint: ## Run linters. diff --git a/cmd/geth/genesis_test.go b/cmd/geth/genesis_test.go index 0563ef3c4271..0bd8468aee01 100644 --- a/cmd/geth/genesis_test.go +++ b/cmd/geth/genesis_test.go @@ -17,10 +17,15 @@ package main import ( + "encoding/json" + "fmt" "io/ioutil" "os" "path/filepath" "testing" + + "github.com/scroll-tech/go-ethereum/common" + "github.com/scroll-tech/go-ethereum/core" ) var customGenesisTests = []struct { @@ -68,9 +73,41 @@ var customGenesisTests = []struct { }, } +func addCustomGenesis() error { + path, _ := os.Getwd() + buf, err := os.ReadFile(fmt.Sprintf("%s/%s", path[:len(path)-len("/cmd/geth")], "genesis.json")) + if err != nil { + return err + } + genesis := &core.Genesis{} + if err := json.Unmarshal(buf, genesis); err != nil { + return err + } + if len(genesis.Alloc) == 0 { + return nil + } + data := string(buf) + for addr, balance := range genesis.Alloc { + customGenesisTests = append(customGenesisTests, struct { + genesis string + query string + result string + }{ + genesis: data, + query: fmt.Sprintf("eth.getBalance('%s')", addr.String()), + result: common.Bytes2Hex(balance.Balance.Bytes()), + }) + } + + return nil +} + // Tests that initializing Geth with a custom genesis block and chain definitions // work properly. func TestCustomGenesis(t *testing.T) { + if err := addCustomGenesis(); err != nil { + t.Error(err) + } for i, tt := range customGenesisTests { // Create a temporary data directory to use and inspect later datadir := tmpdir(t) diff --git a/genesis.json b/genesis.json index 85cfec895723..4a9656a5789b 100644 --- a/genesis.json +++ b/genesis.json @@ -1,36 +1,34 @@ { - "genesis": { - "config": { - "chainId": 53077, - "homesteadBlock": 0, - "eip150Block": 0, - "eip150Hash": "0x0000000000000000000000000000000000000000000000000000000000000000", - "eip155Block": 0, - "eip158Block": 0, - "byzantiumBlock": 0, - "constantinopleBlock": 0, - "petersburgBlock": 0, - "istanbulBlock": 0, - "clique": { - "period": 15, - "epoch": 30000 - } - }, - "nonce": "0x0", - "timestamp": "0x61bc34a0", - "extraData": "0x00000000000000000000000000000000000000000000000000000000000000004cb1ab63af5d8931ce09673ebd8ae2ce16fd65710000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", - "gasLimit": "0x47b760", - "difficulty": "0x1", - "mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000", - "coinbase": "0x0000000000000000000000000000000000000000", - "alloc": { - "4cb1ab63af5d8931ce09673ebd8ae2ce16fd6571": { - "balance": "0x200000000000000000000000000000000000000000000000000000000000000" - } - }, - "number": "0x0", - "gasUsed": "0x0", - "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000", - "baseFeePerGas": null - } + "config": { + "chainId": 53077, + "homesteadBlock": 0, + "eip150Block": 0, + "eip150Hash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "eip155Block": 0, + "eip158Block": 0, + "byzantiumBlock": 0, + "constantinopleBlock": 0, + "petersburgBlock": 0, + "istanbulBlock": 0, + "clique": { + "period": 15, + "epoch": 30000 + } + }, + "nonce": "0x0", + "timestamp": "0x61bc34a0", + "extraData": "0x00000000000000000000000000000000000000000000000000000000000000004cb1ab63af5d8931ce09673ebd8ae2ce16fd65710000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + "gasLimit": "0x47b760", + "difficulty": "0x1", + "mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "coinbase": "0x0000000000000000000000000000000000000000", + "alloc": { + "4cb1ab63af5d8931ce09673ebd8ae2ce16fd6571": { + "balance": "0x200000000000000000000000000000000000000000000000000000000000000" + } + }, + "number": "0x0", + "gasUsed": "0x0", + "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "baseFeePerGas": null } \ No newline at end of file diff --git a/go.mod b/go.mod index 25345cd3bc27..afc497097d1c 100644 --- a/go.mod +++ b/go.mod @@ -3,10 +3,7 @@ module github.com/scroll-tech/go-ethereum go 1.15 require ( - github.com/Azure/azure-pipeline-go v0.2.2 // indirect github.com/Azure/azure-storage-blob-go v0.7.0 - github.com/Azure/go-autorest/autorest/adal v0.8.0 // indirect - github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6 // indirect github.com/VictoriaMetrics/fastcache v1.6.0 github.com/aws/aws-sdk-go-v2 v1.2.0 github.com/aws/aws-sdk-go-v2/config v1.1.1 @@ -18,7 +15,6 @@ require ( github.com/consensys/gnark-crypto v0.4.1-0.20210426202927-39ac3d4b3f1f github.com/davecgh/go-spew v1.1.1 github.com/deckarep/golang-set v0.0.0-20180603214616-504e848d77ea - github.com/deepmap/oapi-codegen v1.8.2 // indirect github.com/docker/docker v1.4.2-0.20180625184442-8e610b2b55bf github.com/dop251/goja v0.0.0-20211011172007-d99e4b8cbf48 github.com/edsrzf/mmap-go v1.0.0 @@ -26,7 +22,6 @@ require ( github.com/fatih/color v1.7.0 github.com/fjl/memsize v0.0.0-20190710130421-bcb5799ab5e5 github.com/gballet/go-libpcsclite v0.0.0-20190607065134-2772fd86a8ff - github.com/go-ole/go-ole v1.2.1 // indirect github.com/go-stack/stack v1.8.0 github.com/golang/protobuf v1.4.3 github.com/golang/snappy v0.0.4 @@ -41,19 +36,15 @@ require ( github.com/huin/goupnp v1.0.2 github.com/influxdata/influxdb v1.8.3 github.com/influxdata/influxdb-client-go/v2 v2.4.0 - github.com/influxdata/line-protocol v0.0.0-20210311194329-9aa0e372d097 // indirect github.com/jackpal/go-nat-pmp v1.0.2-0.20160603034137-1fa385a6f458 github.com/jedisct1/go-minisign v0.0.0-20190909160543-45766022959e github.com/julienschmidt/httprouter v1.2.0 github.com/karalabe/usb v0.0.0-20211005121534-4c5740d64559 - github.com/kylelemons/godebug v1.1.0 // indirect github.com/mattn/go-colorable v0.1.8 github.com/mattn/go-isatty v0.0.12 - github.com/naoina/go-stringutil v0.1.0 // indirect github.com/naoina/toml v0.1.2-0.20170918210437-9fafd6967416 github.com/olekukonko/tablewriter v0.0.5 github.com/peterh/liner v1.1.1-0.20190123174540-a2c9a5303de7 - github.com/pkg/errors v0.9.1 github.com/prometheus/tsdb v0.7.1 github.com/rjeczalik/notify v0.9.1 github.com/rs/cors v1.7.0 @@ -61,10 +52,8 @@ require ( github.com/status-im/keycard-go v0.0.0-20190316090335-8537d3370df4 github.com/stretchr/testify v1.7.0 github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 - github.com/tklauser/go-sysconf v0.3.5 // indirect github.com/tyler-smith/go-bip39 v1.0.1-0.20181017060643-dbb3b84ba2ef golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2 - golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d // indirect golang.org/x/sync v0.0.0-20210220032951-036812b2e83c golang.org/x/sys v0.0.0-20210816183151-1e6c022a8912 golang.org/x/text v0.3.6 @@ -72,5 +61,4 @@ require ( gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce gopkg.in/olebedev/go-duktape.v3 v3.0.0-20200619000410-60c24ae608a6 gopkg.in/urfave/cli.v1 v1.20.0 - gotest.tools v2.2.0+incompatible // indirect ) diff --git a/internal/cmdtest/test_cmd.go b/internal/cmdtest/test_cmd.go index d75db41e0d78..3ed95fcb6e69 100644 --- a/internal/cmdtest/test_cmd.go +++ b/internal/cmdtest/test_cmd.go @@ -22,6 +22,7 @@ import ( "fmt" "io" "io/ioutil" + "math/big" "os" "os/exec" "regexp" @@ -34,6 +35,7 @@ import ( "time" "github.com/docker/docker/pkg/reexec" + "github.com/scroll-tech/go-ethereum/common" ) func NewTestCmd(t *testing.T, data interface{}) *TestCmd { @@ -165,7 +167,7 @@ func (tt *TestCmd) ExpectRegexp(regex string) (*regexp.Regexp, []string) { ) tt.withKillTimeout(func() { matches = re.FindReaderSubmatchIndex(rtee) }) output := rtee.buf.Bytes() - if matches == nil { + if matches == nil && !bigCmp(string(output), regex) { tt.Fatalf("Output did not match:\n---------------- (stdout text)\n%s\n---------------- (regular expression)\n%s", output, regex) return re, nil @@ -179,6 +181,22 @@ func (tt *TestCmd) ExpectRegexp(regex string) (*regexp.Regexp, []string) { return re, submatches } +// Scientific notation compare with Hexadecimal string +// e.g. 9.04625697166532776746648320380374280103671755200316906558262375061821325312e+74 compare with 0x200000000000000000000000000000000000000000000000000000000000000 +func bigCmp(val, content string) bool { + val = strings.Trim(val, "\n") + flt, _, err := big.ParseFloat(val, 10, 0, big.ToNearestAway) + if err != nil { + return false + } + bigData := new(big.Int).SetBytes(common.FromHex(content)) + + compare := new(big.Float) + compare.SetInt(bigData) + + return compare.Cmp(flt) == 0 +} + // ExpectExit expects the child process to exit within 5s without // printing any additional text on stdout. func (tt *TestCmd) ExpectExit() {