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 <shenhaichen@gmail.com>

Co-authored-by: Haichen Shen <shenhaichen@gmail.com>
This commit is contained in:
maskpp 2022-02-23 16:01:59 +08:00 committed by GitHub
parent 69c291cf7a
commit 2040f8c781
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 91 additions and 47 deletions

View file

@ -29,6 +29,9 @@ ios:
@echo "Import \"$(GOBIN)/Geth.framework\" to use the library." @echo "Import \"$(GOBIN)/Geth.framework\" to use the library."
test: all 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 $(GORUN) build/ci.go test ./consensus ./core ./eth ./miner ./node ./trie
lint: ## Run linters. lint: ## Run linters.

View file

@ -17,10 +17,15 @@
package main package main
import ( import (
"encoding/json"
"fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"testing" "testing"
"github.com/scroll-tech/go-ethereum/common"
"github.com/scroll-tech/go-ethereum/core"
) )
var customGenesisTests = []struct { 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 // Tests that initializing Geth with a custom genesis block and chain definitions
// work properly. // work properly.
func TestCustomGenesis(t *testing.T) { func TestCustomGenesis(t *testing.T) {
if err := addCustomGenesis(); err != nil {
t.Error(err)
}
for i, tt := range customGenesisTests { for i, tt := range customGenesisTests {
// Create a temporary data directory to use and inspect later // Create a temporary data directory to use and inspect later
datadir := tmpdir(t) datadir := tmpdir(t)

View file

@ -1,5 +1,4 @@
{ {
"genesis": {
"config": { "config": {
"chainId": 53077, "chainId": 53077,
"homesteadBlock": 0, "homesteadBlock": 0,
@ -32,5 +31,4 @@
"gasUsed": "0x0", "gasUsed": "0x0",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000", "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"baseFeePerGas": null "baseFeePerGas": null
}
} }

12
go.mod
View file

@ -3,10 +3,7 @@ module github.com/scroll-tech/go-ethereum
go 1.15 go 1.15
require ( require (
github.com/Azure/azure-pipeline-go v0.2.2 // indirect
github.com/Azure/azure-storage-blob-go v0.7.0 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/VictoriaMetrics/fastcache v1.6.0
github.com/aws/aws-sdk-go-v2 v1.2.0 github.com/aws/aws-sdk-go-v2 v1.2.0
github.com/aws/aws-sdk-go-v2/config v1.1.1 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/consensys/gnark-crypto v0.4.1-0.20210426202927-39ac3d4b3f1f
github.com/davecgh/go-spew v1.1.1 github.com/davecgh/go-spew v1.1.1
github.com/deckarep/golang-set v0.0.0-20180603214616-504e848d77ea 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/docker/docker v1.4.2-0.20180625184442-8e610b2b55bf
github.com/dop251/goja v0.0.0-20211011172007-d99e4b8cbf48 github.com/dop251/goja v0.0.0-20211011172007-d99e4b8cbf48
github.com/edsrzf/mmap-go v1.0.0 github.com/edsrzf/mmap-go v1.0.0
@ -26,7 +22,6 @@ require (
github.com/fatih/color v1.7.0 github.com/fatih/color v1.7.0
github.com/fjl/memsize v0.0.0-20190710130421-bcb5799ab5e5 github.com/fjl/memsize v0.0.0-20190710130421-bcb5799ab5e5
github.com/gballet/go-libpcsclite v0.0.0-20190607065134-2772fd86a8ff 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/go-stack/stack v1.8.0
github.com/golang/protobuf v1.4.3 github.com/golang/protobuf v1.4.3
github.com/golang/snappy v0.0.4 github.com/golang/snappy v0.0.4
@ -41,19 +36,15 @@ require (
github.com/huin/goupnp v1.0.2 github.com/huin/goupnp v1.0.2
github.com/influxdata/influxdb v1.8.3 github.com/influxdata/influxdb v1.8.3
github.com/influxdata/influxdb-client-go/v2 v2.4.0 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/jackpal/go-nat-pmp v1.0.2-0.20160603034137-1fa385a6f458
github.com/jedisct1/go-minisign v0.0.0-20190909160543-45766022959e github.com/jedisct1/go-minisign v0.0.0-20190909160543-45766022959e
github.com/julienschmidt/httprouter v1.2.0 github.com/julienschmidt/httprouter v1.2.0
github.com/karalabe/usb v0.0.0-20211005121534-4c5740d64559 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-colorable v0.1.8
github.com/mattn/go-isatty v0.0.12 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/naoina/toml v0.1.2-0.20170918210437-9fafd6967416
github.com/olekukonko/tablewriter v0.0.5 github.com/olekukonko/tablewriter v0.0.5
github.com/peterh/liner v1.1.1-0.20190123174540-a2c9a5303de7 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/prometheus/tsdb v0.7.1
github.com/rjeczalik/notify v0.9.1 github.com/rjeczalik/notify v0.9.1
github.com/rs/cors v1.7.0 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/status-im/keycard-go v0.0.0-20190316090335-8537d3370df4
github.com/stretchr/testify v1.7.0 github.com/stretchr/testify v1.7.0
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 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 github.com/tyler-smith/go-bip39 v1.0.1-0.20181017060643-dbb3b84ba2ef
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2 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/sync v0.0.0-20210220032951-036812b2e83c
golang.org/x/sys v0.0.0-20210816183151-1e6c022a8912 golang.org/x/sys v0.0.0-20210816183151-1e6c022a8912
golang.org/x/text v0.3.6 golang.org/x/text v0.3.6
@ -72,5 +61,4 @@ require (
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce
gopkg.in/olebedev/go-duktape.v3 v3.0.0-20200619000410-60c24ae608a6 gopkg.in/olebedev/go-duktape.v3 v3.0.0-20200619000410-60c24ae608a6
gopkg.in/urfave/cli.v1 v1.20.0 gopkg.in/urfave/cli.v1 v1.20.0
gotest.tools v2.2.0+incompatible // indirect
) )

View file

@ -22,6 +22,7 @@ import (
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"math/big"
"os" "os"
"os/exec" "os/exec"
"regexp" "regexp"
@ -34,6 +35,7 @@ import (
"time" "time"
"github.com/docker/docker/pkg/reexec" "github.com/docker/docker/pkg/reexec"
"github.com/scroll-tech/go-ethereum/common"
) )
func NewTestCmd(t *testing.T, data interface{}) *TestCmd { 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) }) tt.withKillTimeout(func() { matches = re.FindReaderSubmatchIndex(rtee) })
output := rtee.buf.Bytes() 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", tt.Fatalf("Output did not match:\n---------------- (stdout text)\n%s\n---------------- (regular expression)\n%s",
output, regex) output, regex)
return re, nil return re, nil
@ -179,6 +181,22 @@ func (tt *TestCmd) ExpectRegexp(regex string) (*regexp.Regexp, []string) {
return re, submatches 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 // ExpectExit expects the child process to exit within 5s without
// printing any additional text on stdout. // printing any additional text on stdout.
func (tt *TestCmd) ExpectExit() { func (tt *TestCmd) ExpectExit() {