Skip to content

Commit

Permalink
fix(genesis): sporadic hash failure remediation
Browse files Browse the repository at this point in the history
  • Loading branch information
revitteth committed Mar 8, 2024
1 parent bbf7a04 commit 97dd82a
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 31 deletions.
43 changes: 28 additions & 15 deletions core/genesis_write.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import (
"github.com/ledgerwatch/erigon/chain"
"github.com/ledgerwatch/log/v3"

"os"

"github.com/ledgerwatch/erigon/common"
"github.com/ledgerwatch/erigon/common/hexutil"
"github.com/ledgerwatch/erigon/consensus/ethash"
Expand All @@ -49,6 +51,7 @@ import (
"github.com/ledgerwatch/erigon/params/networkname"
"github.com/ledgerwatch/erigon/smt/pkg/db"
"github.com/ledgerwatch/erigon/smt/pkg/smt"
"golang.org/x/exp/slices"
)

// CommitGenesisBlock writes or updates the genesis block in db.
Expand Down Expand Up @@ -108,6 +111,15 @@ func WriteGenesisBlock(tx kv.RwTx, genesis *types.Genesis, overrideShanghaiTime
genesis = MainnetGenesisBlock()
custom = false
}
// clear tmpDir
if tmpDir != "" {
if err := os.RemoveAll(tmpDir); err != nil {
return genesis.Config, nil, err
}
if err := os.MkdirAll(tmpDir, 0755); err != nil {
return genesis.Config, nil, err
}
}
applyOverrides(genesis.Config)
block, _, err1 := write(tx, genesis, tmpDir)
if err1 != nil {
Expand Down Expand Up @@ -460,8 +472,8 @@ func DeveloperGenesisBlock(period uint64, faucet libcommon.Address) *types.Genes
}
}

var genesisTmpDB kv.RwDB
var genesisDBLock sync.Mutex
var GenesisTmpDB kv.RwDB
var GenesisDBLock sync.Mutex

// ToBlock creates the genesis block and writes state of a genesis specification
// to the given database (or discards it if nil).
Expand Down Expand Up @@ -514,13 +526,17 @@ func GenesisToBlock(g *types.Genesis, tmpDir string) (*types.Block, *state.Intra
go func() { // we may run inside write tx, can't open 2nd write tx in same goroutine
// TODO(yperbasis): use memdb.MemoryMutation instead
defer wg.Done()
genesisDBLock.Lock()
defer genesisDBLock.Unlock()
if genesisTmpDB == nil {
genesisTmpDB = mdbx.NewMDBX(log.New()).InMem(tmpDir).MapSize(2 * datasize.GB).MustOpen()
GenesisDBLock.Lock()
defer GenesisDBLock.Unlock()
if GenesisTmpDB == nil {
GenesisTmpDB = mdbx.NewMDBX(log.New()).InMem(tmpDir).MapSize(2 * datasize.GB).MustOpen()
defer func() {
GenesisTmpDB.Close()
GenesisTmpDB = nil
}()
}
var tx kv.RwTx
if tx, err = genesisTmpDB.BeginRw(context.Background()); err != nil {
if tx, err = GenesisTmpDB.BeginRw(context.Background()); err != nil {
return
}
defer tx.Rollback()
Expand Down Expand Up @@ -557,10 +573,9 @@ func GenesisToBlock(g *types.Genesis, tmpDir string) (*types.Block, *state.Intra
statedb.SetCode(addr, account.Code)
statedb.SetNonce(addr, account.Nonce)

for key, value := range account.Storage {
key := key
for k, value := range account.Storage {
val := uint256.NewInt(0).SetBytes(value.Bytes())
statedb.SetState(addr, &key, *val)
statedb.SetState(addr, &k, *val)
}

if len(account.Constructor) > 0 {
Expand All @@ -582,11 +597,9 @@ func GenesisToBlock(g *types.Genesis, tmpDir string) (*types.Block, *state.Intra
return
}

//if root, err = trie.CalcRoot("genesis", tx); err != nil {
// return
//}

root = libcommon.BigToHash(ro)
s = nil
db = nil
}()
wg.Wait()
if err != nil {
Expand All @@ -605,7 +618,7 @@ func sortedAllocKeys(m types.GenesisAlloc) []string {
keys[i] = string(k.Bytes())
i++
}
//slices.Sort(keys)
slices.Sort(keys)
return keys
}

Expand Down
17 changes: 9 additions & 8 deletions core/genesis_zkevm_test.go
Original file line number Diff line number Diff line change
@@ -1,36 +1,37 @@
//go:build notzkevm
// +build notzkevm

package core_test

import (
"context"
"testing"

"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon-lib/kv/memdb"
"github.com/ledgerwatch/erigon/core"
"github.com/ledgerwatch/erigon/params"
"github.com/ledgerwatch/erigon/params/networkname"
"github.com/stretchr/testify/require"
"testing"
)

func TestGenesisBlockHashesZkevm(t *testing.T) {
db := memdb.NewTestDB(t)
check := func(network string) {
db := memdb.NewTestDB(t)
defer db.Close()
genesis := core.GenesisBlockByChainName(network)
tx, err := db.BeginRw(context.Background())
if err != nil {
t.Fatal(err)
}
defer tx.Rollback()
_, block, err := core.WriteGenesisBlock(tx, genesis, nil, "")
_, block, err := core.WriteGenesisBlock(tx, genesis, nil, "/tmp/"+network)
require.NoError(t, err)
expect := params.GenesisHashByChainName(network)
require.NotNil(t, expect, network)
require.Equal(t, block.Hash().Bytes(), expect.Bytes(), network)
require.Equal(t, expect.Hex(), block.Hash().Hex(), network)
}
for _, network := range networkname.Zkevm {
check(network)
t.Run(network, func(t *testing.T) {
check(network)
})
}
}

Expand Down
2 changes: 1 addition & 1 deletion params/chainspecs/hermez-etrog.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"ChainName": "etrog",
"ChainName": "hermez-etrog",
"chainId": 10010,
"consensus": "ethash",
"homesteadBlock": 0,
Expand Down
15 changes: 8 additions & 7 deletions params/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ var (
BorDevnetGenesisHash = libcommon.HexToHash("0x5a06b25b0c6530708ea0b98a3409290e39dce6be7f558493aeb6e4b99a172a87")
GnosisGenesisHash = libcommon.HexToHash("0x4f1dd23188aab3a76b463e4af801b52b1248ef073c648cbdc4c9333d3da79756")
ChiadoGenesisHash = libcommon.HexToHash("0xada44fd8d2ecab8b08f256af07ad3e777f17fb434f8f8e678b312f576212ba9a")
HermezMainnetGenesisHash = libcommon.HexToHash("0x3f86b09b43e3e49a41fc20a07579b79eba044253367817d5c241d23c0e2bc5c9")
HermezMainnetShadowforkGenesisHash = libcommon.HexToHash("0x3f86b09b43e3e49a41fc20a07579b79eba044253367817d5c241d23c0e2bc5c9")
HermezLocalDevnetGenesisHash = libcommon.HexToHash("0x65531c46ddeb39ba78d50f4eb6b87a279fa28bb6f683b327779392a4cd4770f3")
HermezCardonaGenesisHash = libcommon.HexToHash("0xf1a89745a08cfd1af739b93f960ad89546e3cec2a7babe8607703eb1e4e94b4d")
HermezCardonaInternalGenesisHash = libcommon.HexToHash("0x676657dd8a7dce782f4fe8a97ba794de3ff732b40233fce01da77d1ba1a24ee1")
X1TestnetGenesisHash = libcommon.HexToHash("0xb2fbff62137228e52809081a425bfcd30c0fdc8c1213085278c739676a7669b8")
HermezEtrogGenesisHash = libcommon.HexToHash("0xe5ce2305f2ec92fa22130f6eb2345ad66037b657b44b183c0f55a9439409123d")
HermezMainnetGenesisHash = libcommon.HexToHash("0x81005434635456a16f74ff7023fbe0bf423abbc8a8deb093ffff455c0ad3b741")
HermezMainnetShadowforkGenesisHash = libcommon.HexToHash("0xe54709058a084845156393707161a7b3347859b1796167ca014354841f68373c")
HermezLocalDevnetGenesisHash = libcommon.HexToHash("0x433043a1b0948d109cd92a6b7e0e5a3f011c761d70eebe3135ec8f7a39815a65")
HermezCardonaGenesisHash = libcommon.HexToHash("0x676c1a76a6c5855a32bdf7c61977a0d1510088a4eeac1330466453b3d08b60b9")
HermezCardonaInternalGenesisHash = libcommon.HexToHash("0xe65e09471025d299d0fd5b57e9ae325ecf16037cbd2f456db435b6bb1369154d")
X1TestnetGenesisHash = libcommon.HexToHash("0x0ffb92e130f1acaabd8b12aa1bb409b46561ef7568cb8aa7eb8d102a6ab76566")
HermezEtrogGenesisHash = libcommon.HexToHash("0xccfed260e3ef666b058dcd577551de8e00c743c47774a39ca7dbcd9214ba370a")
)

var (
Expand Down Expand Up @@ -318,6 +318,7 @@ func ChainConfigByGenesisHash(genesisHash libcommon.Hash) *chain.Config {
case genesisHash == HermezCardonaInternalGenesisHash:
return HermezCardonaInternalChainConfig
default:
panic(fmt.Sprintf("Unknown genesis hash: %s", genesisHash.Hex()))
return nil
}
}
Expand Down

0 comments on commit 97dd82a

Please sign in to comment.