Skip to content

Commit

Permalink
Fix bug about genesis file struct (ethereum#32)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
mask-pp and icemelon committed Feb 23, 2022
1 parent 69c291c commit 2040f8c
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 47 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
37 changes: 37 additions & 0 deletions cmd/geth/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
66 changes: 32 additions & 34 deletions genesis.json
Original file line number Diff line number Diff line change
@@ -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
}
12 changes: 0 additions & 12 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -18,15 +15,13 @@ 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
github.com/ethereum/go-ethereum v1.10.13
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
Expand All @@ -41,36 +36,29 @@ 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
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible
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
golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba
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
)
20 changes: 19 additions & 1 deletion internal/cmdtest/test_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"
"io"
"io/ioutil"
"math/big"
"os"
"os/exec"
"regexp"
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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() {
Expand Down

0 comments on commit 2040f8c

Please sign in to comment.