Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion cmd/opera/launcher/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package launcher

import (
"bufio"
"encoding/json"
"errors"
"fmt"
"os"
Expand All @@ -14,6 +15,7 @@ import (
"github.com/Fantom-foundation/lachesis-base/utils/cachescale"
"github.com/ethereum/go-ethereum/cmd/utils"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/p2p/enode"
Expand Down Expand Up @@ -210,7 +212,24 @@ func mayGetGenesisStore(ctx *cli.Context) *genesisstore.Store {
if err != nil {
log.Crit("Invalid flag", "flag", FakeNetFlag.Name, "err", err)
}
return makefakegenesis.FakeGenesisStore(num, futils.ToFtm(1000000000), futils.ToFtm(5000000))
if ctx.GlobalIsSet(InitGenesisFlag.Name) {
genesisPath := ctx.GlobalString(InitGenesisFlag.Name)
if len(genesisPath) == 0 {
utils.Fatalf("invalid path to genesis file")
}
file, err := os.Open(genesisPath)
if err != nil {
utils.Fatalf("Failed to read genesis file: %v", err)
}
defer file.Close()

genesis := new(core.Genesis)
if err := json.NewDecoder(file).Decode(genesis); err != nil {
utils.Fatalf("invalid genesis file: %v", err)
}
return makefakegenesis.FakeGenesisStore(num, futils.ToFtm(1000000000), futils.ToFtm(5000000), genesis)
}
return makefakegenesis.FakeGenesisStore(num, futils.ToFtm(1000000000), futils.ToFtm(5000000), nil)
case ctx.GlobalIsSet(GenesisFlag.Name):
genesisPath := ctx.GlobalString(GenesisFlag.Name)

Expand Down
5 changes: 5 additions & 0 deletions cmd/opera/launcher/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ var FakeNetFlag = cli.StringFlag{
Usage: "'n/N' - sets coinbase as fake n-th key from genesis of N validators.",
}

var InitGenesisFlag = cli.StringFlag{
Name: "init",
Usage: "path to genesis json file to allocate account/contract genesis state for testing purpose on fakenet",
}

func getFakeValidatorKey(ctx *cli.Context) *ecdsa.PrivateKey {
id, _, err := parseFakeGen(ctx.GlobalString(FakeNetFlag.Name))
if err != nil || id == 0 {
Expand Down
1 change: 1 addition & 0 deletions cmd/opera/launcher/launcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func initFlags() {
// Flags for testing purpose.
testFlags = []cli.Flag{
FakeNetFlag,
InitGenesisFlag,
}

// Flags that configure the node.
Expand Down
2 changes: 1 addition & 1 deletion docker/Dockerfile.opera
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.14-alpine as builder
FROM golang:1.17-alpine as builder

RUN apk add --no-cache make gcc musl-dev linux-headers git

Expand Down
2 changes: 1 addition & 1 deletion gossip/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func newTestEnv(firstEpoch idx.Epoch, validatorsNum idx.Validator) *testEnv {
rules.Epochs.MaxEpochDuration = inter.Timestamp(maxEpochDuration)
rules.Blocks.MaxEmptyBlockSkipPeriod = 0

genStore := makefakegenesis.FakeGenesisStoreWithRulesAndStart(validatorsNum, utils.ToFtm(genesisBalance), utils.ToFtm(genesisStake), rules, firstEpoch, 2)
genStore := makefakegenesis.FakeGenesisStoreWithRulesAndStart(validatorsNum, utils.ToFtm(genesisBalance), utils.ToFtm(genesisStake), rules, firstEpoch, 2, nil)
genesis := genStore.Genesis()

store := NewMemStore()
Expand Down
2 changes: 1 addition & 1 deletion integration/bench_db_flush_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
func BenchmarkFlushDBs(b *testing.B) {
dir := tmpDir("flush_bench")
defer os.RemoveAll(dir)
genStore := makefakegenesis.FakeGenesisStore(1, utils.ToFtm(1), utils.ToFtm(1))
genStore := makefakegenesis.FakeGenesisStore(1, utils.ToFtm(1), utils.ToFtm(1), nil)
g := genStore.Genesis()
_, _, store, s2, _, closeDBs := MakeEngine(dir, &g, Configs{
Opera: gossip.DefaultConfig(cachescale.Identity),
Expand Down
24 changes: 18 additions & 6 deletions integration/makefakegenesis/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/Fantom-foundation/lachesis-base/kvdb/memorydb"
"github.com/Fantom-foundation/lachesis-base/lachesis"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"

Expand Down Expand Up @@ -43,20 +44,31 @@ func FakeKey(n idx.ValidatorID) *ecdsa.PrivateKey {
return evmcore.FakeKey(int(n))
}

func FakeGenesisStore(num idx.Validator, balance, stake *big.Int) *genesisstore.Store {
return FakeGenesisStoreWithRules(num, balance, stake, opera.FakeNetRules())
func FakeGenesisStore(num idx.Validator, balance, stake *big.Int, genesis *core.Genesis) *genesisstore.Store {
return FakeGenesisStoreWithRules(num, balance, stake, opera.FakeNetRules(), genesis)
}

func FakeGenesisStoreWithRules(num idx.Validator, balance, stake *big.Int, rules opera.Rules) *genesisstore.Store {
return FakeGenesisStoreWithRulesAndStart(num, balance, stake, rules, 2, 1)
func FakeGenesisStoreWithRules(num idx.Validator, balance, stake *big.Int, rules opera.Rules, genesis *core.Genesis) *genesisstore.Store {
return FakeGenesisStoreWithRulesAndStart(num, balance, stake, rules, 2, 1, genesis)
}

func FakeGenesisStoreWithRulesAndStart(num idx.Validator, balance, stake *big.Int, rules opera.Rules, epoch idx.Epoch, block idx.Block) *genesisstore.Store {
func FakeGenesisStoreWithRulesAndStart(num idx.Validator, balance, stake *big.Int, rules opera.Rules, epoch idx.Epoch, block idx.Block, g *core.Genesis) *genesisstore.Store {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No needs to use whole core.Genesis when you need core.Genesis.Alloc only.
Anyway ethreum core.Genesis is foreign struct for go-opera, no reasons to use it at all. Let's keep fake genesis as simple as possible (i.e. as it is).
The way I suggest is create test accounts and contracts at the beginning of the test, not by genesis. It is more clean to see full account history in the test scenario and don't keep genesis state in mind.

builder := makegenesis.NewGenesisBuilder(memorydb.NewProducer(""))

validators := GetFakeValidators(num)
// init genesis alloc accounts if any
if g != nil {
for addr, account := range g.Alloc {
builder.AddBalance(addr, account.Balance)
builder.SetCode(addr, account.Code)
builder.SetNonce(addr, account.Nonce)
for key, value := range account.Storage {
builder.SetStorage(addr, key, value)
}
}
}

// add balances to validators
validators := GetFakeValidators(num)
var delegations []drivercall.Delegation
for _, val := range validators {
builder.AddBalance(val.Address, balance)
Expand Down