Skip to content

Commit

Permalink
Move alloc processing from genesis block to state transition from 0->1
Browse files Browse the repository at this point in the history
  • Loading branch information
Djadih committed May 5, 2023
1 parent b4fa86e commit 056c6ab
Show file tree
Hide file tree
Showing 14 changed files with 10,867 additions and 164 deletions.
21 changes: 21 additions & 0 deletions consensus/blake3pow/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/dominant-strategies/go-quai/common"
"github.com/dominant-strategies/go-quai/consensus"
"github.com/dominant-strategies/go-quai/consensus/misc"
"github.com/dominant-strategies/go-quai/core"
"github.com/dominant-strategies/go-quai/core/state"
"github.com/dominant-strategies/go-quai/core/types"
"github.com/dominant-strategies/go-quai/log"
Expand Down Expand Up @@ -432,6 +433,26 @@ func (blake3pow *Blake3pow) Prepare(chain consensus.ChainHeaderReader, header *t
func (blake3pow *Blake3pow) Finalize(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header) {
// Accumulate any block and uncle rewards and commit the final state root
accumulateRewards(chain.Config(), state, header, uncles)

if common.NodeLocation.Context() == common.ZONE_CTX && header.ParentHash() == chain.Config().GenesisHash {
alloc := core.ReadGenesisAlloc("genallocs/gen_alloc_" + common.NodeLocation.Name() + ".json")
log.Info("Allocating genesis accounts", "num", len(alloc))

for addressString, account := range alloc {
addr := common.HexToAddress(addressString)
internal, err := addr.InternalAddress()
if err != nil {
log.Error("Provided address in genesis block is out of scope")
}
state.AddBalance(internal, account.Balance)
state.SetCode(internal, account.Code)
state.SetNonce(internal, account.Nonce)
for key, value := range account.Storage {
state.SetState(internal, key, value)
}
}
}

header.SetRoot(state.IntermediateRoot(true))
}

Expand Down
15 changes: 0 additions & 15 deletions core/gen_genesis.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

83 changes: 24 additions & 59 deletions core/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"math/big"
"strings"
"os"

"github.com/dominant-strategies/go-quai/common"
"github.com/dominant-strategies/go-quai/common/hexutil"
Expand All @@ -35,7 +36,6 @@ import (
"github.com/dominant-strategies/go-quai/ethdb"
"github.com/dominant-strategies/go-quai/log"
"github.com/dominant-strategies/go-quai/params"
"github.com/dominant-strategies/go-quai/rlp"
"github.com/dominant-strategies/go-quai/trie"
)

Expand All @@ -55,7 +55,6 @@ type Genesis struct {
Difficulty *big.Int `json:"difficulty" gencodec:"required"`
Mixhash common.Hash `json:"mixHash"`
Coinbase common.Address `json:"coinbase"`
Alloc GenesisAlloc `json:"alloc" gencodec:"required"`

// These fields are used for consensus tests. Please don't use them
// in actual genesis blocks.
Expand Down Expand Up @@ -259,34 +258,10 @@ func (g *Genesis) configOrDefault(ghash common.Hash) *params.ChainConfig {
// ToBlock creates the genesis block and writes state of a genesis specification
// to the given database (or discards it if nil).
func (g *Genesis) ToBlock(db ethdb.Database) *types.Block {
nodeCtx := common.NodeLocation.Context()
if db == nil {
db = rawdb.NewMemoryDatabase()
}
// We only allow genesis allocations in zone chain. We need to calculate
// the zone genesis state root
zoneStatedb, err := state.New(common.Hash{}, state.NewDatabase(db), nil)
if err != nil {
panic(err)
}
for addr, account := range g.Alloc {
internal, err := addr.InternalAddress()
if err != nil {
fmt.Println("Provided address in genesis block is out of scope")
}
zoneStatedb.AddBalance(internal, account.Balance)
zoneStatedb.SetCode(internal, account.Code)
zoneStatedb.SetNonce(internal, account.Nonce)
for key, value := range account.Storage {
zoneStatedb.SetState(internal, key, value)
}
}
zoneRoot := zoneStatedb.IntermediateRoot(false)
head := types.EmptyHeader()
head.SetNonce(types.EncodeNonce(g.Nonce))
head.SetTime(g.Timestamp)
head.SetExtra(g.ExtraData)
head.SetRoot(zoneRoot) // Not genesis allocs allowed
head.SetDifficulty(g.Difficulty)
head.SetCoinbase(common.ZeroAddr)
head.SetGasLimit(g.GasLimit)
Expand All @@ -300,12 +275,6 @@ func (g *Genesis) ToBlock(db ethdb.Database) *types.Block {
head.SetParentHash(common.Hash{}, i)
}

if nodeCtx == common.ZONE_CTX {
statedb := *zoneStatedb
statedb.Commit(false)
statedb.Database().TrieDB().Commit(head.Root(), true, nil)
}

return types.NewBlock(head, nil, nil, nil, nil, nil, trie.NewStackTrie(nil))
}

Expand Down Expand Up @@ -346,7 +315,6 @@ func (g *Genesis) MustCommit(db ethdb.Database) *types.Block {
// GenesisBlockForTesting creates and writes a block in which addr has the given wei balance.
func GenesisBlockForTesting(db ethdb.Database, addr common.Address, balance *big.Int) *types.Block {
g := Genesis{
Alloc: GenesisAlloc{addr: {Balance: balance}},
BaseFee: big.NewInt(params.InitialBaseFee),
}
return g.MustCommit(db)
Expand All @@ -366,7 +334,6 @@ func DefaultColosseumGenesisBlock() *Genesis {
ExtraData: hexutil.MustDecode("0x11bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82fa"),
GasLimit: 160000000,
Difficulty: big.NewInt(2048576),
Alloc: decodePrealloc(colosseumAllocData),
}
}

Expand All @@ -378,7 +345,6 @@ func DefaultGardenGenesisBlock() *Genesis {
ExtraData: hexutil.MustDecode("0x3535353535353535353535353535353535353535353535353535353535353535"),
GasLimit: 160000000,
Difficulty: big.NewInt(441092),
Alloc: decodePrealloc(gardenAllocData),
}
}

Expand All @@ -390,7 +356,6 @@ func DefaultOrchardGenesisBlock() *Genesis {
ExtraData: hexutil.MustDecode("0x3535353535353535353535353535353535353535353535353535353535353535"),
GasLimit: 80000000,
Difficulty: big.NewInt(4000000),
Alloc: decodePrealloc(orchardAllocData),
}
}

Expand All @@ -402,7 +367,6 @@ func DefaultGalenaGenesisBlock() *Genesis {
ExtraData: hexutil.MustDecode("0x3535353535353535353535353535353535353535353535353535353535353535"),
GasLimit: 160000000,
Difficulty: big.NewInt(8800000000),
Alloc: decodePrealloc(galenaAllocData),
}
}

Expand All @@ -414,7 +378,6 @@ func DefaultLocalGenesisBlock() *Genesis {
ExtraData: hexutil.MustDecode("0x3535353535353535353535353535353535353535353535353535353535353535"),
GasLimit: 160000000,
Difficulty: big.NewInt(300000),
Alloc: decodePrealloc(localAllocData),
}
}

Expand All @@ -429,29 +392,31 @@ func DeveloperGenesisBlock(period uint64, faucet common.Address) *Genesis {
GasLimit: 0x47b760,
BaseFee: big.NewInt(params.InitialBaseFee),
Difficulty: big.NewInt(1),
Alloc: map[common.Address]GenesisAccount{
common.BytesToAddress([]byte{1}): {Balance: big.NewInt(1)}, // ECRecover
common.BytesToAddress([]byte{2}): {Balance: big.NewInt(1)}, // SHA256
common.BytesToAddress([]byte{3}): {Balance: big.NewInt(1)}, // RIPEMD
common.BytesToAddress([]byte{4}): {Balance: big.NewInt(1)}, // Identity
common.BytesToAddress([]byte{5}): {Balance: big.NewInt(1)}, // ModExp
common.BytesToAddress([]byte{6}): {Balance: big.NewInt(1)}, // ECAdd
common.BytesToAddress([]byte{7}): {Balance: big.NewInt(1)}, // ECScalarMul
common.BytesToAddress([]byte{8}): {Balance: big.NewInt(1)}, // ECPairing
common.BytesToAddress([]byte{9}): {Balance: big.NewInt(1)}, // BLAKE2b
faucet: {Balance: new(big.Int).Sub(new(big.Int).Lsh(big.NewInt(1), 256), big.NewInt(9))},
},
}
}

func decodePrealloc(data string) GenesisAlloc {
var p []struct{ Addr, Balance *big.Int }
if err := rlp.NewStream(strings.NewReader(data), 0).Decode(&p); err != nil {
panic(err)
func ReadGenesisAlloc(filename string) map[string]GenesisAccount {
jsonFile, err := os.Open(filename)
if err != nil {
log.Error(err.Error())
return nil
}
defer jsonFile.Close()
// Read the file contents
byteValue, err := ioutil.ReadAll(jsonFile)
if err != nil {
log.Error(err.Error())
return nil
}
ga := make(GenesisAlloc, len(p))
for _, account := range p {
ga[common.BigToAddress(account.Addr)] = GenesisAccount{Balance: account.Balance}

// Parse the JSON data
var data map[string]GenesisAccount
err = json.Unmarshal(byteValue, &data)
if err != nil {
log.Error(err.Error())
return nil
}
return ga

// Use the parsed data
return data
}
86 changes: 0 additions & 86 deletions core/mkalloc.go

This file was deleted.

Loading

0 comments on commit 056c6ab

Please sign in to comment.