Skip to content

Commit

Permalink
erigontech#2658: eliminated HeadersSeal function (erigontech#2731)
Browse files Browse the repository at this point in the history
* erigontech#2658: removed using HeadersSeal function from experiments module

* erigontech#2658: lint issues

* erigontech#2658: fix typo
  • Loading branch information
e-danko authored Oct 13, 2021
1 parent 91df893 commit 19b7354
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 18 deletions.
15 changes: 0 additions & 15 deletions common/debug/experiments.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,21 +104,6 @@ func SlowCommit() time.Duration {
return slowCommit
}

var (
headersSeal bool
getHeadersSeal sync.Once
)

func HeadersSeal() bool {
getHeadersSeal.Do(func() {
v, _ := os.LookupEnv("HEADERS_SEAL")
if v == "true" {
headersSeal = true
}
})
return headersSeal
}

var (
stopBeforeStage string
stopBeforeStageFlag sync.Once
Expand Down
37 changes: 37 additions & 0 deletions core/block_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,40 @@ func TestHeaderVerification(t *testing.T) {
engine.Close()
}
}

// Tests that simple header with seal verification works, for both good and bad blocks.
func TestHeaderWithSealVerification(t *testing.T) {
// Create a simple chain to verify
var (
gspec = &core.Genesis{Config: params.TestChainAuraConfig}
engine = ethash.NewFaker()
)
m := stages.MockWithGenesisEngine(t, gspec, engine)

chain, err := core.GenerateChain(m.ChainConfig, m.Genesis, m.Engine, m.DB, 8, nil, false /* intemediateHashes */)
if err != nil {
t.Fatalf("genetate chain: %v", err)
}

// Run the header checker for blocks one-by-one, checking for both valid and invalid nonces
for i := 0; i < chain.Length; i++ {
for j, valid := range []bool{true, false} {
if valid {
engine := ethash.NewFaker()
err = engine.VerifyHeaders(stagedsync.ChainReader{Cfg: *params.TestChainAuraConfig, Db: olddb.NewObjectDatabase(m.DB)}, []*types.Header{chain.Headers[i]}, []bool{true})
} else {
engine := ethash.NewFakeFailer(chain.Headers[i].Number.Uint64())
err = engine.VerifyHeaders(stagedsync.ChainReader{Cfg: *params.TestChainAuraConfig, Db: olddb.NewObjectDatabase(m.DB)}, []*types.Header{chain.Headers[i]}, []bool{true})
}
if (err == nil) != valid {
t.Errorf("test %d.%d: validity mismatch: have %v, want %v", i, j, err, valid)
}
}

if err = m.InsertChain(chain.Slice(i, i+1)); err != nil {
t.Fatalf("test %d: error inserting the block: %v", i, err)
}

engine.Close()
}
}
3 changes: 2 additions & 1 deletion core/chain_makers.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ func GenerateChain(config *params.ChainConfig, parent *types.Block, engine conse
config = params.TestChainConfig
}
headers, blocks, receipts := make([]*types.Header, n), make(types.Blocks, n), make([]types.Receipts, n)
types.SetHeaderSealFlag(config.IsHeaderWithSeal())
chainreader := &FakeChainReader{Cfg: config, current: parent}
tx, errBegin := db.BeginRw(context.Background())
if errBegin != nil {
Expand Down Expand Up @@ -413,7 +414,7 @@ func makeHeader(chain consensus.ChainReader, parent *types.Block, state *state.I
header.BaseFee = misc.CalcBaseFee(chain.Config(), parent.Header())
header.Eip1559 = true
}
//header.WithSeal = debug.HeadersSeal()
header.WithSeal = chain.Config().IsHeaderWithSeal()

return header
}
Expand Down
12 changes: 10 additions & 2 deletions core/types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,24 @@ import (

rlp2 "github.com/ledgerwatch/erigon-lib/rlp"
"github.com/ledgerwatch/erigon/common"
"github.com/ledgerwatch/erigon/common/debug"
"github.com/ledgerwatch/erigon/common/hexutil"
"github.com/ledgerwatch/erigon/rlp"
)

var (
EmptyRootHash = common.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421")
EmptyUncleHash = rlpHash([]*Header(nil))
headerWithSeal = false
)

func SetHeaderSealFlag(withSeal bool) {
headerWithSeal = withSeal
}

func IsHeaderWithSeal() bool {
return headerWithSeal
}

// A BlockNonce is a 64-bit hash which proves (combined with the
// mix-hash) that a sufficient amount of computation has been carried
// out on a block.
Expand Down Expand Up @@ -398,7 +406,7 @@ func (h Header) EncodeRLP(w io.Writer) error {

func (h *Header) DecodeRLP(s *rlp.Stream) error {
if !h.WithSeal { // then tests can enable without env flag
h.WithSeal = debug.HeadersSeal()
h.WithSeal = IsHeaderWithSeal()
}
_, err := s.List()
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions eth/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ func New(stack *node.Node, config *ethconfig.Config, logger log.Logger) (*Ethere
if _, ok := genesisErr.(*params.ConfigCompatError); genesisErr != nil && !ok {
return nil, genesisErr
}
types.SetHeaderSealFlag(chainConfig.IsHeaderWithSeal())
log.Info("Initialised chain configuration", "config", chainConfig)

ctx, ctxCancel := context.WithCancel(context.Background())
Expand Down
45 changes: 45 additions & 0 deletions params/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ const (
FermionChainName = "fermion"
)

type ConsensusType string

const (
AuRaConsensus ConsensusType = "aura"
EtHashConsensus ConsensusType = "ethash"
CliqueConsensus ConsensusType = "clique"
)

// Genesis hashes to enforce below configs on.
var (
MainnetGenesisHash = common.HexToHash("0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3")
Expand Down Expand Up @@ -64,6 +72,7 @@ var (
MainnetChainConfig = &ChainConfig{
ChainName: MainnetChainName,
ChainID: big.NewInt(1),
Consensus: EtHashConsensus,
HomesteadBlock: big.NewInt(1_150_000),
DAOForkBlock: big.NewInt(1_920_000),
DAOForkSupport: true,
Expand All @@ -85,6 +94,7 @@ var (
RopstenChainConfig = &ChainConfig{
ChainName: RopstenChainName,
ChainID: big.NewInt(3),
Consensus: EtHashConsensus,
HomesteadBlock: big.NewInt(0),
DAOForkBlock: nil,
DAOForkSupport: true,
Expand All @@ -106,6 +116,7 @@ var (
RinkebyChainConfig = &ChainConfig{
ChainName: RinkebyChainName,
ChainID: big.NewInt(4),
Consensus: CliqueConsensus,
HomesteadBlock: big.NewInt(1),
DAOForkBlock: nil,
DAOForkSupport: true,
Expand All @@ -130,6 +141,7 @@ var (
GoerliChainConfig = &ChainConfig{
ChainName: GoerliChainName,
ChainID: big.NewInt(5),
Consensus: CliqueConsensus,
HomesteadBlock: big.NewInt(0),
DAOForkBlock: nil,
DAOForkSupport: true,
Expand All @@ -153,6 +165,7 @@ var (
ErigonChainConfig = &ChainConfig{
ChainName: ErigonMineName,
ChainID: new(big.Int).SetBytes([]byte("erigon-mine")),
Consensus: EtHashConsensus,
HomesteadBlock: big.NewInt(0),
DAOForkBlock: nil,
DAOForkSupport: true,
Expand All @@ -171,6 +184,7 @@ var (
SokolChainConfig = &ChainConfig{
ChainName: SokolChainName,
ChainID: big.NewInt(77),
Consensus: AuRaConsensus,
HomesteadBlock: big.NewInt(0),
DAOForkBlock: nil,
DAOForkSupport: false,
Expand Down Expand Up @@ -234,6 +248,7 @@ var (
KovanChainConfig = &ChainConfig{
ChainName: KovanChainName,
ChainID: big.NewInt(42),
Consensus: AuRaConsensus,
HomesteadBlock: big.NewInt(0),
DAOForkBlock: nil,
DAOForkSupport: false,
Expand Down Expand Up @@ -278,6 +293,7 @@ var (
// adding flags to the config to also have to set these fields.
AllEthashProtocolChanges = &ChainConfig{
ChainID: big.NewInt(1337),
Consensus: EtHashConsensus,
HomesteadBlock: big.NewInt(0),
DAOForkBlock: nil,
DAOForkSupport: false,
Expand All @@ -304,6 +320,7 @@ var (
// adding flags to the config to also have to set these fields.
AllCliqueProtocolChanges = &ChainConfig{
ChainID: big.NewInt(1337),
Consensus: CliqueConsensus,
HomesteadBlock: big.NewInt(0),
DAOForkBlock: nil,
DAOForkSupport: false,
Expand All @@ -327,6 +344,7 @@ var (

TestChainConfig = &ChainConfig{
ChainID: big.NewInt(1),
Consensus: EtHashConsensus,
HomesteadBlock: big.NewInt(0),
DAOForkBlock: nil,
DAOForkSupport: false,
Expand All @@ -346,6 +364,27 @@ var (
Clique: nil,
}

TestChainAuraConfig = &ChainConfig{
ChainID: big.NewInt(1),
Consensus: AuRaConsensus,
HomesteadBlock: big.NewInt(0),
DAOForkBlock: nil,
DAOForkSupport: false,
EIP150Block: big.NewInt(0),
EIP150Hash: common.Hash{},
EIP155Block: big.NewInt(0),
EIP158Block: big.NewInt(0),
ByzantiumBlock: big.NewInt(0),
ConstantinopleBlock: big.NewInt(0),
PetersburgBlock: big.NewInt(0),
IstanbulBlock: big.NewInt(0),
MuirGlacierBlock: big.NewInt(0),
BerlinBlock: big.NewInt(0),
LondonBlock: nil,
CatalystBlock: nil,
Aura: &AuRaConfig{},
}

TestRules = TestChainConfig.Rules(0)
)

Expand All @@ -358,6 +397,8 @@ type ChainConfig struct {
ChainName string
ChainID *big.Int `json:"chainId"` // chainId identifies the current chain and is used for replay protection

Consensus ConsensusType `json:"consensus,omitempty"` // aura, ethash or clique

HomesteadBlock *big.Int `json:"homesteadBlock,omitempty"` // Homestead switch block (nil = no fork, 0 = already homestead)

DAOForkBlock *big.Int `json:"daoForkBlock,omitempty"` // TheDAO hard-fork switch block (nil = no fork)
Expand Down Expand Up @@ -441,6 +482,10 @@ func (c *ChainConfig) String() string {
)
}

func (c *ChainConfig) IsHeaderWithSeal() bool {
return c.Consensus == AuRaConsensus
}

type SnapshotConfig struct {
CheckpointInterval uint64 // Number of blocks after which to save the vote snapshot to the database
InmemorySnapshots int // Number of recent vote snapshots to keep in memory
Expand Down

0 comments on commit 19b7354

Please sign in to comment.