Skip to content
Merged
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
77 changes: 61 additions & 16 deletions cmd/hivechain/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@ import (
"crypto/ecdsa"
"crypto/rand"
"fmt"
"io"
"io/ioutil"
"log"
"math/big"
"os"
"path/filepath"
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/consensus"
"github.com/ethereum/go-ethereum/consensus/beacon"
"github.com/ethereum/go-ethereum/consensus/ethash"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/rawdb"
Expand Down Expand Up @@ -44,12 +47,15 @@ var (
)

type generatorConfig struct {
txInterval int // frequency of blocks containing transactions
txCount int // number of txs in block
blockCount int // number of generated blocks
blockTimeSec int // block time in seconds, influences difficulty
powMode ethash.Mode
genesis core.Genesis
txInterval int // frequency of blocks containing transactions
txCount int // number of txs in block
blockCount int // number of generated pow blocks
posBlockCount int // number of generated pos blocks
blockTimeSec int // block time in seconds, influences difficulty
powMode ethash.Mode
genesis core.Genesis
isPoS bool // true if the generator should create post pos blocks
modifyBlock func(*types.Block) *types.Block // modify the block during exporting
}

// loadGenesis loads genesis.json.
Expand All @@ -68,6 +74,8 @@ func (cfg generatorConfig) writeTestChain(outputPath string) error {
gen.OffsetTime(int64((i+1)*int(cfg.blockTimeSec) - 10))
cfg.addTxForKnownAccounts(i, gen)
}
// Do not modify blocks
cfg.modifyBlock = func(b *types.Block) *types.Block { return b }
return cfg.generateAndSave(outputPath, blockModifier)
}

Expand Down Expand Up @@ -180,20 +188,36 @@ func createTxGasLimit(gen *core.BlockGen, genesis *core.Genesis, data []byte) ui
func (cfg generatorConfig) generateAndSave(path string, blockModifier func(i int, gen *core.BlockGen)) error {
db := rawdb.NewMemoryDatabase()
genesis := cfg.genesis.MustCommit(db)
config := ethash.Config{
config := cfg.genesis.Config
ethashConf := ethash.Config{
PowMode: cfg.powMode,
CachesInMem: 2,
DatasetsOnDisk: 2,
DatasetDir: ethashDir(),
}
engine := ethash.New(config, nil, false)

// Generate a chain where each block is created, modified, and immediately sealed.
insta := instaSeal{engine}
chain, _ := core.GenerateChain(cfg.genesis.Config, genesis, insta, db, cfg.blockCount, blockModifier)
powEngine := ethash.New(ethashConf, nil, false)
posEngine := beacon.New(powEngine)
engine := instaSeal{posEngine}

// Create the PoW chain.
chain, _ := core.GenerateChain(config, genesis, engine, db, cfg.blockCount, blockModifier)

// Create the PoS chain extension.
if cfg.isPoS {
// Set TTD to the head of the PoW chain.
totalDifficulty := big.NewInt(0)
for _, b := range chain {
totalDifficulty.Add(totalDifficulty, b.Difficulty())
}
config.TerminalTotalDifficulty = totalDifficulty

posChain, _ := core.GenerateChain(config, chain[len(chain)-1], engine, db, cfg.posBlockCount, blockModifier)
chain = append(chain, posChain...)
}

// Import the chain. This runs all block validation rules.
blockchain, err := core.NewBlockChain(db, nil, cfg.genesis.Config, engine, vm.Config{}, nil, nil)
blockchain, err := core.NewBlockChain(db, nil, config, engine, vm.Config{}, nil, nil)
if err != nil {
return fmt.Errorf("can't create blockchain: %v", err)
}
Expand All @@ -209,10 +233,10 @@ func (cfg generatorConfig) generateAndSave(path string, blockModifier func(i int
dump := headstate.Dump(&state.DumpConfig{})

// Write out the generated blockchain
if err := writeChain(blockchain, filepath.Join(path, "chain.rlp"), 1); err != nil {
if err := writeChain(blockchain, filepath.Join(path, "chain.rlp"), 1, cfg.modifyBlock); err != nil {
return err
}
if err := writeChain(blockchain, filepath.Join(path, "chain_genesis.rlp"), 0); err != nil {
if err := writeChain(blockchain, filepath.Join(path, "chain_genesis.rlp"), 0, cfg.modifyBlock); err != nil {
return err
}
if err := ioutil.WriteFile(filepath.Join(path, "chain_poststate.json"), dump, 0644); err != nil {
Expand All @@ -231,13 +255,13 @@ func ethashDir() string {
}

// writeChain exports the given chain to a file.
func writeChain(chain *core.BlockChain, filename string, start uint64) error {
func writeChain(chain *core.BlockChain, filename string, start uint64, modifyBlock func(*types.Block) *types.Block) error {
out, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil {
return err
}
defer out.Close()
return chain.ExportN(out, start, chain.CurrentBlock().NumberU64())
return exportN(chain, out, start, chain.CurrentBlock().NumberU64(), modifyBlock)
}

// instaSeal wraps a consensus engine with instant block sealing. When a block is produced
Expand All @@ -257,3 +281,24 @@ func (e instaSeal) FinalizeAndAssemble(chain consensus.ChainHeaderReader, header
}
return <-sealedBlock, nil
}

func exportN(bc *core.BlockChain, w io.Writer, first uint64, last uint64, modifyBlock func(*types.Block) *types.Block) error {
fmt.Printf("Exporting batch of blocks, count %v \n", last-first+1)

start, reported := time.Now(), time.Now()
for nr := first; nr <= last; nr++ {
block := bc.GetBlockByNumber(nr)
if block == nil {
return fmt.Errorf("export failed on #%d: not found", nr)
}
block = modifyBlock(block)
if err := block.EncodeRLP(w); err != nil {
return err
}
if time.Since(reported) >= 8*time.Second {
fmt.Printf("Exporting blocks, exported: %v, elapsed: %v \n", block.NumberU64()-first, common.PrettyDuration(time.Since(start)))
reported = time.Now()
}
}
return nil
}
7 changes: 6 additions & 1 deletion cmd/hivechain/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,10 @@ func generateCommand(args []string) {
genesis = flag.String("genesis", "", "The path and filename to the source genesis.json")
outdir = flag.String("output", ".", "Chain destination folder")
mine = flag.Bool("mine", false, "Enables ethash mining")
pos = flag.Bool("pos", false, "Enables PoS chain")
)
flag.IntVar(&cfg.blockCount, "length", 2, "The length of the chain to generate")
flag.IntVar(&cfg.blockCount, "length", 2, "The length of the pow chain to generate")
flag.IntVar(&cfg.posBlockCount, "poslength", 2, "The length of the pos chain to generate")
flag.IntVar(&cfg.blockTimeSec, "blocktime", 30, "The desired block time in seconds")
flag.IntVar(&cfg.txInterval, "tx-interval", 10, "Add transactions to chain every n blocks")
flag.IntVar(&cfg.txCount, "tx-count", 1, "Maximum number of txs per block")
Expand All @@ -175,6 +177,9 @@ func generateCommand(args []string) {
} else {
cfg.powMode = ethash.ModeFullFake
}

cfg.isPoS = *pos

gspec, err := loadGenesis(*genesis)
if err != nil {
fatal(err)
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/ethereum/go-ethereum v1.10.17
github.com/fsouza/go-dockerclient v1.7.11
github.com/gorilla/mux v1.8.0
github.com/rauljordan/engine-proxy v0.0.0-20220517190449-e62b2e2f6e27
gopkg.in/inconshreveable/log15.v2 v2.0.0-20200109203555-b30bc20e4fd1
gopkg.in/yaml.v2 v2.4.0
)
Expand All @@ -31,6 +32,7 @@ require (
github.com/go-ole/go-ole v1.2.5 // indirect
github.com/go-stack/stack v1.8.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-jwt/jwt/v4 v4.3.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
Expand All @@ -57,6 +59,6 @@ require (
github.com/tklauser/numcpus v0.2.2 // indirect
go.opencensus.io v0.23.0 // indirect
golang.org/x/crypto v0.0.0-20211202192323-5770296d904e // indirect
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6 // indirect
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce // indirect
)
Loading