Skip to content

Commit 56bebd7

Browse files
Merge pull request #23 from karalabe/merge-interop-spec-sync4
core, eth: various tiny fixups integrating sync
2 parents 716ff95 + 7834589 commit 56bebd7

File tree

8 files changed

+31
-68
lines changed

8 files changed

+31
-68
lines changed

core/blockchain.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -244,10 +244,7 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *par
244244
engine: engine,
245245
vmConfig: vmConfig,
246246
}
247-
bc.forker = NewForkChoice(bc, merger.LeftPoW(), shouldPreserve)
248-
merger.SubscribeLeavePoW(func() {
249-
bc.forker.MarkTransitioned()
250-
})
247+
bc.forker = NewForkChoice(bc, shouldPreserve)
251248
bc.validator = NewBlockValidator(chainConfig, bc, engine)
252249
bc.prefetcher = newStatePrefetcher(chainConfig, bc, engine)
253250
bc.processor = NewStateProcessor(chainConfig, bc, engine)

core/blockchain_test.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,6 @@ func testInsertAfterMerge(t *testing.T, blockchain *BlockChain, i, n int, full b
230230
t.Errorf("chain content mismatch at %d: have hash %v, want hash %v", i, hash2, hash1)
231231
}
232232

233-
// Trigger the transition explicitly
234-
blockchain2.forker.MarkTransitioned()
235-
236233
// Extend the newly created chain
237234
if full {
238235
blockChainB := makeBlockChain(blockchain2.CurrentBlock(), n, ethash.NewFaker(), db, forkSeed)
@@ -1963,7 +1960,6 @@ func testSideImport(t *testing.T, numCanonBlocksInSidechain, blocksBetweenCommon
19631960
if mergePoint == 0 {
19641961
genEngine.MarkTransitioned()
19651962
runEngine.MarkTransitioned()
1966-
chain.forker.MarkTransitioned()
19671963
}
19681964
blocks, _ := GenerateChain(params.TestChainConfig, genesis, genEngine, db, 2*TriesInMemory, func(i int, gen *BlockGen) {
19691965
tx, err := types.SignTx(types.NewTransaction(nonce, common.HexToAddress("deadbeef"), big.NewInt(100), 21000, big.NewInt(int64(i+1)*params.GWei), nil), signer, key)
@@ -1994,7 +1990,6 @@ func testSideImport(t *testing.T, numCanonBlocksInSidechain, blocksBetweenCommon
19941990
if mergePoint == 1 {
19951991
genEngine.MarkTransitioned()
19961992
runEngine.MarkTransitioned()
1997-
chain.forker.MarkTransitioned()
19981993
}
19991994

20001995
// Generate the sidechain
@@ -2218,9 +2213,6 @@ func testInsertKnownChainDataWithMerging(t *testing.T, typ string, mergeHeight i
22182213
if engine != nil {
22192214
engine.MarkTransitioned()
22202215
}
2221-
if forker != nil {
2222-
forker.MarkTransitioned()
2223-
}
22242216
}
22252217

22262218
// Apply merging since genesis

core/forkchoice.go

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,16 @@ import (
2727
"github.com/ethereum/go-ethereum/common/math"
2828
"github.com/ethereum/go-ethereum/core/types"
2929
"github.com/ethereum/go-ethereum/log"
30+
"github.com/ethereum/go-ethereum/params"
3031
)
3132

3233
// ChainReader defines a small collection of methods needed to access the local
3334
// blockchain during header verification. It's implemented by both blockchain
3435
// and lightchain.
3536
type ChainReader interface {
37+
// Config retrieves the header chain's chain configuration.
38+
Config() *params.ChainConfig
39+
3640
// GetTd returns the total difficulty of a local block.
3741
GetTd(common.Hash, uint64) *big.Int
3842
}
@@ -45,12 +49,7 @@ type ChainReader interface {
4549
type ForkChoice struct {
4650
chain ChainReader
4751
rand *mrand.Rand
48-
49-
// transitioned is the flag whether the chain has started(or finished)
50-
// the transition. It's triggered by receiving the first "NewHead" message
51-
// from the external consensus engine.
52-
transitioned bool
53-
lock sync.RWMutex
52+
lock sync.RWMutex
5453

5554
// preserve is a helper function used in td fork choice.
5655
// Miners will prefer to choose the local mined block if the
@@ -59,17 +58,16 @@ type ForkChoice struct {
5958
preserve func(header *types.Header) bool
6059
}
6160

62-
func NewForkChoice(chainReader ChainReader, transitioned bool, preserve func(header *types.Header) bool) *ForkChoice {
61+
func NewForkChoice(chainReader ChainReader, preserve func(header *types.Header) bool) *ForkChoice {
6362
// Seed a fast but crypto originating random generator
6463
seed, err := crand.Int(crand.Reader, big.NewInt(math.MaxInt64))
6564
if err != nil {
6665
log.Crit("Failed to initialize random seed", "err", err)
6766
}
6867
return &ForkChoice{
69-
chain: chainReader,
70-
rand: mrand.New(mrand.NewSource(seed.Int64())),
71-
transitioned: transitioned,
72-
preserve: preserve,
68+
chain: chainReader,
69+
rand: mrand.New(mrand.NewSource(seed.Int64())),
70+
preserve: preserve,
7371
}
7472
}
7573

@@ -82,19 +80,19 @@ func (f *ForkChoice) Reorg(current *types.Header, header *types.Header) (bool, e
8280
f.lock.RLock()
8381
defer f.lock.RUnlock()
8482

85-
// Accept the new header as the chain head if the transition
86-
// is already triggered. We assume all the headers after the
87-
// transition come from the trusted consensus layer.
88-
if f.transitioned {
89-
return true, nil
90-
}
9183
var (
9284
localTD = f.chain.GetTd(current.Hash(), current.Number.Uint64())
9385
externTd = f.chain.GetTd(header.Hash(), header.Number.Uint64())
9486
)
9587
if localTD == nil || externTd == nil {
9688
return false, errors.New("missing td")
9789
}
90+
// Accept the new header as the chain head if the transition
91+
// is already triggered. We assume all the headers after the
92+
// transition come from the trusted consensus layer.
93+
if ttd := f.chain.Config().TerminalTotalDifficulty; ttd != nil && ttd.Cmp(externTd) <= 0 {
94+
return true, nil
95+
}
9896
// If the total difficulty is higher than our known, add it to the canonical chain
9997
// Second clause in the if statement reduces the vulnerability to selfish mining.
10098
// Please refer to http://www.cs.cornell.edu/~ie53/publications/btcProcFC.pdf
@@ -113,11 +111,3 @@ func (f *ForkChoice) Reorg(current *types.Header, header *types.Header) (bool, e
113111
}
114112
return reorg, nil
115113
}
116-
117-
// MarkTransitioned marks the transition has started.
118-
func (f *ForkChoice) MarkTransitioned() {
119-
f.lock.Lock()
120-
defer f.lock.Unlock()
121-
122-
f.transitioned = true
123-
}

core/headerchain_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func TestHeaderInsertion(t *testing.T) {
8484
chainB := makeHeaderChain(chainA[0], 128, ethash.NewFaker(), db, 10)
8585
log.Root().SetHandler(log.StdoutHandler)
8686

87-
forker := NewForkChoice(hc, false, nil)
87+
forker := NewForkChoice(hc, nil)
8888
// Inserting 64 headers on an empty chain, expecting
8989
// 1 callbacks, 1 canon-status, 0 sidestatus,
9090
testInsert(t, hc, chainA[:64], CanonStatTy, nil, forker)

eth/catalyst/api.go

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ func (api *ConsensusAPI) ExecutePayload(params ExecutableData) (GenericStringRes
231231
if parent == nil {
232232
return INVALID, fmt.Errorf("could not find parent %x", params.ParentHash)
233233
}
234-
block, err := ExecutableDataToBlock(api.les.BlockChain().Config(), parent, params)
234+
block, err := ExecutableDataToBlock(params)
235235
if err != nil {
236236
return INVALID, err
237237
}
@@ -240,17 +240,7 @@ func (api *ConsensusAPI) ExecutePayload(params ExecutableData) (GenericStringRes
240240
}
241241
return VALID, nil
242242
}
243-
parent := api.eth.BlockChain().GetBlockByHash(params.ParentHash)
244-
if parent == nil {
245-
return INVALID, fmt.Errorf("could not find parent %x", params.ParentHash)
246-
}
247-
248-
td := api.eth.BlockChain().GetTdByHash(parent.Hash())
249-
ttd := api.eth.BlockChain().Config().TerminalTotalDifficulty
250-
if td.Cmp(ttd) < 0 {
251-
return INVALID, fmt.Errorf("can not execute payload on top of block with low td got: %v threshold %v", td, ttd)
252-
}
253-
block, err := ExecutableDataToBlock(api.eth.BlockChain().Config(), parent.Header(), params)
243+
block, err := ExecutableDataToBlock(params)
254244
if err != nil {
255245
return INVALID, err
256246
}
@@ -260,6 +250,12 @@ func (api *ConsensusAPI) ExecutePayload(params ExecutableData) (GenericStringRes
260250
}
261251
return SYNCING, nil
262252
}
253+
parent := api.eth.BlockChain().GetBlockByHash(params.ParentHash)
254+
td := api.eth.BlockChain().GetTdByHash(parent.Hash())
255+
ttd := api.eth.BlockChain().Config().TerminalTotalDifficulty
256+
if td.Cmp(ttd) < 0 {
257+
return INVALID, fmt.Errorf("can not execute payload on top of block with low td got: %v threshold %v", td, ttd)
258+
}
263259
if err := api.eth.BlockChain().InsertBlock(block); err != nil {
264260
return INVALID, err
265261
}
@@ -392,7 +388,7 @@ func decodeTransactions(enc [][]byte) ([]*types.Transaction, error) {
392388
return txs, nil
393389
}
394390

395-
func ExecutableDataToBlock(config *chainParams.ChainConfig, parent *types.Header, params ExecutableData) (*types.Block, error) {
391+
func ExecutableDataToBlock(params ExecutableData) (*types.Block, error) {
396392
txs, err := decodeTransactions(params.Transactions)
397393
if err != nil {
398394
return nil, err
@@ -419,9 +415,6 @@ func ExecutableDataToBlock(config *chainParams.ChainConfig, parent *types.Header
419415
Extra: params.ExtraData,
420416
// TODO (MariusVanDerWijden) add params.Random to header once required
421417
}
422-
if config.IsLondon(number) {
423-
header.BaseFee = misc.CalcBaseFee(config, parent)
424-
}
425418
block := types.NewBlockWithHeader(header).WithBody(txs, nil /* uncles */)
426419
if block.Hash() != params.BlockHash {
427420
return nil, fmt.Errorf("blockhash mismatch, want %x, got %x", params.BlockHash, block.Hash())

eth/catalyst/api_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ func TestEth2NewBlock(t *testing.T) {
255255
if err != nil {
256256
t.Fatalf("Failed to create the executable data %v", err)
257257
}
258-
block, err := ExecutableDataToBlock(ethservice.BlockChain().Config(), parent.Header(), *execData)
258+
block, err := ExecutableDataToBlock(*execData)
259259
if err != nil {
260260
t.Fatalf("Failed to convert executable data to block %v", err)
261261
}
@@ -292,7 +292,7 @@ func TestEth2NewBlock(t *testing.T) {
292292
if err != nil {
293293
t.Fatalf("Failed to create the executable data %v", err)
294294
}
295-
block, err := ExecutableDataToBlock(ethservice.BlockChain().Config(), parent.Header(), *execData)
295+
block, err := ExecutableDataToBlock(*execData)
296296
if err != nil {
297297
t.Fatalf("Failed to convert executable data to block %v", err)
298298
}

light/lightchain.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,7 @@ func NewLightChain(odr OdrBackend, config *params.ChainConfig, engine consensus.
9393
blockCache: blockCache,
9494
engine: engine,
9595
}
96-
bc.forker = core.NewForkChoice(bc, merger.LeftPoW(), nil)
97-
merger.SubscribeLeavePoW(func() {
98-
bc.forker.MarkTransitioned()
99-
})
96+
bc.forker = core.NewForkChoice(bc, nil)
10097
var err error
10198
bc.hc, err = core.NewHeaderChain(odr.Database(), config, bc.engine, bc.getProcInterrupt)
10299
if err != nil {

miner/stress/beacon/main.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,7 @@ func (n *ethNode) insertBlockAndSetHead(parent *types.Header, ed catalyst.Execut
172172
if err := n.insertBlock(ed); err != nil {
173173
return err
174174
}
175-
var config *params.ChainConfig
176-
if n.typ == eth2LightClient {
177-
config = n.lesBackend.BlockChain().Config()
178-
} else {
179-
config = n.ethBackend.BlockChain().Config()
180-
}
181-
block, err := catalyst.ExecutableDataToBlock(config, parent, ed)
175+
block, err := catalyst.ExecutableDataToBlock(ed)
182176
if err != nil {
183177
return err
184178
}
@@ -319,7 +313,7 @@ func (mgr *nodeManager) run() {
319313
log.Error("Failed to assemble the block", "err", err)
320314
continue
321315
}
322-
block, _ := catalyst.ExecutableDataToBlock(chain.Config(), parentBlock.Header(), *ed)
316+
block, _ := catalyst.ExecutableDataToBlock(*ed)
323317

324318
nodes := mgr.getNodes(eth2MiningNode)
325319
nodes = append(nodes, mgr.getNodes(eth2NormalNode)...)

0 commit comments

Comments
 (0)