Skip to content

Commit

Permalink
gen_genesis.go: completion of writing the knot
Browse files Browse the repository at this point in the history
  • Loading branch information
0xalank committed Aug 29, 2022
1 parent a8b9677 commit 50bad32
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 31 deletions.
3 changes: 1 addition & 2 deletions cmd/genesis/gen_genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func main() {
var (
testdb = rawdb.NewMemoryDatabase()
genesis = core.RopstenPrimeGenesisBlock().MustCommit(testdb)
fn = "test_knot.rlp"
fn = "ropsten_knot.rlp"
)

blake3Config := blake3.Config{
Expand Down Expand Up @@ -46,7 +46,6 @@ func main() {
}

for _, block := range blocks {

if err := block.EncodeRLP(writer); err != nil {
log.Panic("error writing")
}
Expand Down
17 changes: 4 additions & 13 deletions core/chain_makers.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/spruce-solutions/go-quai/core/vm"
"github.com/spruce-solutions/go-quai/ethdb"
"github.com/spruce-solutions/go-quai/params"
"github.com/spruce-solutions/go-quai/trie"
)

// BlockGen creates blocks for testing.
Expand Down Expand Up @@ -266,15 +267,6 @@ func GenerateKnot(config *params.ChainConfig, parent *types.Block, engine consen
log.Println("PRIME:", block.Header().Location, block.Header().Number, block.Header().Hash())
}

// Write state changes to db
root, err := statedb.Commit(config.IsEIP158(block.Number()))
if err != nil {
panic(fmt.Sprintf("state write error: %v", err))
}
if err := statedb.Database().TrieDB().Commit(root, false, nil); err != nil {
panic(fmt.Sprintf("trie write error: %v", err))
}

k.block = block

defer wg.Done()
Expand All @@ -294,7 +286,7 @@ func GenerateKnot(config *params.ChainConfig, parent *types.Block, engine consen
}
if b.engine != nil {
// Finalize and seal the block
block, _ := b.engine.FinalizeAndAssemble(chainreader, b.header, statedb, b.txs, b.uncles, b.receipts)
block := types.NewBlock(b.header, nil, nil, nil, trie.NewStackTrie(nil))

// Mine the block
if err := engine.Seal(chainreader, block, resultCh, stopCh); err != nil {
Expand Down Expand Up @@ -356,19 +348,18 @@ func makeHeader(chain consensus.ChainReader, parent *types.Block, state *state.S
}

// Setting this to params.MinGasLimit for now since
header.GasLimit[types.QuaiNetworkContext] = params.MinGasLimit
header.GasLimit = []uint64{params.MinGasLimit, params.MinGasLimit, params.MinGasLimit}

parentHeader := parent.Header()

header.Root[types.QuaiNetworkContext] = state.IntermediateRoot(chain.Config().IsEIP158(parent.Number()))
header.ParentHash[types.QuaiNetworkContext] = parent.Hash()
header.Coinbase[types.QuaiNetworkContext] = parent.Coinbase()
header.Difficulty[types.QuaiNetworkContext] = engine.CalcDifficulty(chain, time, parentHeader, types.QuaiNetworkContext)

header.Number[types.QuaiNetworkContext] = new(big.Int).Add(parent.Number(), common.Big1)
if len(parent.Header().Location) > 0 && header.Location[0] == parent.Header().Location[0] {
header.Number[types.QuaiNetworkContext+1] = new(big.Int).Add(parent.Header().Number[types.QuaiNetworkContext+1], common.Big1)

header.ParentHash[types.QuaiNetworkContext+1] = parent.Hash()
}

return header
Expand Down
15 changes: 10 additions & 5 deletions core/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ func (g *Genesis) ToBlock(db ethdb.Database) *types.Block {
}

if len(g.GasLimit) == 0 {
head.GasLimit[types.QuaiNetworkContext] = params.GenesisGasLimit
head.GasLimit = []uint64{params.GenesisGasLimit, params.GenesisGasLimit, params.GenesisGasLimit}
}

statedb.Commit(false)
Expand Down Expand Up @@ -351,6 +351,7 @@ func GenesisBlockForTesting(db ethdb.Database, addr common.Address, balance *big
func MainnetPrimeGenesisBlock() *Genesis {
return &Genesis{
Config: params.MainnetPrimeChainConfig,
Knot: ReadKnot("./core/knot/mainnet_knot.rlp"),
ParentHash: []common.Hash{common.Hash{}, common.Hash{}, common.Hash{}},
Coinbase: []common.Address{common.Address{}, common.Address{}, common.Address{}},
Number: []*big.Int{big.NewInt(0), big.NewInt(0), big.NewInt(0)},
Expand All @@ -367,7 +368,9 @@ func MainnetPrimeGenesisBlock() *Genesis {
func MainnetRegionGenesisBlock(regionParams *params.ChainConfig) *Genesis {
return &Genesis{
Config: regionParams,
Knot: ReadKnot("./core/knot/mainnet_knot.rlp"),
ParentHash: []common.Hash{common.Hash{}, common.Hash{}, common.Hash{}},
Coinbase: []common.Address{common.Address{}, common.Address{}, common.Address{}},
Number: []*big.Int{big.NewInt(0), big.NewInt(0), big.NewInt(0)},
Nonce: 60066,
ExtraData: [][]byte{hexutil.MustDecode("0x11bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82fa"), hexutil.MustDecode("0x11bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82fa"), hexutil.MustDecode("0x11bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82fa")},
Expand All @@ -382,7 +385,9 @@ func MainnetRegionGenesisBlock(regionParams *params.ChainConfig) *Genesis {
func MainnetZoneGenesisBlock(zoneParams *params.ChainConfig) *Genesis {
return &Genesis{
Config: zoneParams,
Knot: ReadKnot("./core/knot/mainnet_knot.rlp"),
ParentHash: []common.Hash{common.Hash{}, common.Hash{}, common.Hash{}},
Coinbase: []common.Address{common.Address{}, common.Address{}, common.Address{}},
Number: []*big.Int{big.NewInt(0), big.NewInt(0), big.NewInt(0)},
Nonce: 60066,
ExtraData: [][]byte{hexutil.MustDecode("0x11bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82fa"), hexutil.MustDecode("0x11bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82fa"), hexutil.MustDecode("0x11bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82fa")},
Expand All @@ -397,15 +402,15 @@ func MainnetZoneGenesisBlock(zoneParams *params.ChainConfig) *Genesis {
func RopstenPrimeGenesisBlock() *Genesis {
return &Genesis{
Config: params.RopstenPrimeChainConfig,
Knot: ReadKnot("test_knot.rlp"),
Knot: ReadKnot("./core/knot/ropsten_knot.rlp"),
ParentHash: []common.Hash{common.Hash{}, common.Hash{}, common.Hash{}},
Coinbase: []common.Address{common.Address{}, common.Address{}, common.Address{}},
Number: []*big.Int{big.NewInt(0), big.NewInt(0), big.NewInt(0)},
Nonce: 11,
ExtraData: [][]byte{hexutil.MustDecode("0x11bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82fa"), hexutil.MustDecode("0x11bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82fa"), hexutil.MustDecode("0x11bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82fa")},
GasLimit: []uint64{500000, 500000, 500000},
GasUsed: []uint64{0, 0, 0},
Difficulty: []*big.Int{big.NewInt(30), big.NewInt(20), big.NewInt(10)},
Difficulty: []*big.Int{big.NewInt(3448576), big.NewInt(1248576), big.NewInt(168576)},
Alloc: decodePrealloc(ropstenAllocData),
}
}
Expand All @@ -414,7 +419,7 @@ func RopstenPrimeGenesisBlock() *Genesis {
func RopstenRegionGenesisBlock(regionParams *params.ChainConfig) *Genesis {
return &Genesis{
Config: regionParams,
Knot: ReadKnot("test_knot.rlp"),
Knot: ReadKnot("./core/knot/ropsten_knot.rlp"),
ParentHash: []common.Hash{common.Hash{}, common.Hash{}, common.Hash{}},
Coinbase: []common.Address{common.Address{}, common.Address{}, common.Address{}}, Number: []*big.Int{big.NewInt(0), big.NewInt(0), big.NewInt(0)},
Nonce: 11,
Expand All @@ -430,7 +435,7 @@ func RopstenRegionGenesisBlock(regionParams *params.ChainConfig) *Genesis {
func RopstenZoneGenesisBlock(zoneParams *params.ChainConfig) *Genesis {
return &Genesis{
Config: zoneParams,
Knot: ReadKnot("test_knot.rlp"),
Knot: ReadKnot("./core/knot/ropsten_knot.rlp"),
ParentHash: []common.Hash{common.Hash{}, common.Hash{}, common.Hash{}},
Coinbase: []common.Address{common.Address{}, common.Address{}, common.Address{}}, Number: []*big.Int{big.NewInt(0), big.NewInt(0), big.NewInt(0)},
Nonce: 11,
Expand Down
9 changes: 0 additions & 9 deletions core/knot.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package core

import (
"compress/gzip"
"fmt"
"io"
"os"
"strings"
Expand All @@ -13,17 +12,14 @@ import (

func ReadKnot(chainfile string) []*types.Block {
// Load chain.rlp.
fmt.Println("Loading ReadKnot")
fh, err := os.Open(chainfile)
if err != nil {
fmt.Println("OPEN ERR 1", err)
return nil
}
defer fh.Close()
var reader io.Reader = fh
if strings.HasSuffix(chainfile, ".gz") {
if reader, err = gzip.NewReader(reader); err != nil {
fmt.Println("READER ERR 1", err)
return nil
}
}
Expand All @@ -34,17 +30,12 @@ func ReadKnot(chainfile string) []*types.Block {
if err := stream.Decode(&b); err == io.EOF {
break
} else if err != nil {
fmt.Println("DECODE ERR 1", err)
return nil
}
if b.NumberU64() != uint64(i+1) {
fmt.Println("DECODE ERR 2", err)
return nil
}
blocks = append(blocks, &b)
}
for _, block := range blocks {
fmt.Println(block)
}
return blocks
}
Binary file added core/knot/mainnet_knot.rlp
Binary file not shown.
Binary file added core/knot/ropsten_knot.rlp
Binary file not shown.
4 changes: 2 additions & 2 deletions eth/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) {
}
}

for i, block := range knotSet {
if i != 0 {
for _, block := range knotSet {
if block != nil {
rawdb.WriteTd(chainDb, block.Hash(), block.NumberU64(), config.Genesis.Difficulty)
rawdb.WriteBlock(chainDb, block)
rawdb.WriteReceipts(chainDb, block.Hash(), block.NumberU64(), nil)
Expand Down
Binary file removed test_knot.rlp
Binary file not shown.

0 comments on commit 50bad32

Please sign in to comment.