From d25732c949f5d1aa2cf7ddd6c4efcb4ba265f1ea Mon Sep 17 00:00:00 2001 From: Pratik Patil Date: Thu, 8 Jun 2023 11:16:55 +0530 Subject: [PATCH] Revert "miner :: add : check to run worker commitWork (#828)" This reverts commit 6dee9b5f4016cc58d3b470ecdcd0092ee4ba9fd3. --- miner/worker.go | 4 +- tests/bor/bor_test.go | 153 +++++++++++++++++------------------------- 2 files changed, 63 insertions(+), 94 deletions(-) diff --git a/miner/worker.go b/miner/worker.go index 219322362a83..72fb1373890c 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -599,9 +599,7 @@ func (w *worker) mainLoop(ctx context.Context) { select { case req := <-w.newWorkCh: //nolint:contextcheck - if w.isRunning() { - w.commitWork(req.ctx, req.interrupt, req.noempty, req.timestamp) - } + w.commitWork(req.ctx, req.interrupt, req.noempty, req.timestamp) case req := <-w.getWorkCh: //nolint:contextcheck diff --git a/tests/bor/bor_test.go b/tests/bor/bor_test.go index 24c579caad06..dd3359cb284a 100644 --- a/tests/bor/bor_test.go +++ b/tests/bor/bor_test.go @@ -577,112 +577,90 @@ func TestFetchStateSyncEvents_2(t *testing.T) { require.Equal(t, uint64(6), lastStateID.Uint64()) } -// Abs returns the absolute value of x. -func Abs(x int) int { - if x < 0 { - return -x - } - return x -} - func TestOutOfTurnSigning(t *testing.T) { - log.Root().SetHandler(log.LvlFilterHandler(log.LvlInfo, log.StreamHandler(os.Stderr, log.TerminalFormat(true)))) - fdlimit.Raise(2048) + init := buildEthereumInstance(t, rawdb.NewMemoryDatabase()) + chain := init.ethereum.BlockChain() + engine := init.ethereum.Engine() + _bor := engine.(*bor.Bor) - // Generate a batch of accounts to seal and fund with - faucets := make([]*ecdsa.PrivateKey, 128) - for i := 0; i < len(faucets); i++ { - faucets[i], _ = crypto.GenerateKey() - } + defer _bor.Close() - // Create an Ethash network based off of the Ropsten config - // Generate a batch of accounts to seal and fund with - genesis := InitGenesis(t, faucets, "./testdata/genesis_2val.json", 8) + _, heimdallSpan := loadSpanFromFile(t) + proposer := valset.NewValidator(addr, 10) + heimdallSpan.ValidatorSet.Validators = append(heimdallSpan.ValidatorSet.Validators, proposer) - var ( - stacks []*node.Node - nodes []*eth.Ethereum - enodes []*enode.Node - ) - for i := 0; i < 2; i++ { - // Start the node and wait until it's up - stack, ethBackend, err := InitMiner(genesis, keys[i], true) - if err != nil { - panic(err) - } - defer stack.Close() + // add the block producer + h, ctrl := getMockedHeimdallClient(t, heimdallSpan) + defer ctrl.Finish() - for stack.Server().NodeInfo().Ports.Listener == 0 { - time.Sleep(250 * time.Millisecond) - } - // Connect the node to all the previous ones - for _, n := range enodes { - stack.Server().AddPeer(n) - } - // Start tracking the node and its enode - stacks = append(stacks, stack) - nodes = append(nodes, ethBackend) - enodes = append(enodes, stack.Server().Self()) - } + h.EXPECT().Close().AnyTimes() - // Iterate over all the nodes and start mining - time.Sleep(3 * time.Second) - for _, node := range nodes { - if err := node.StartMining(1); err != nil { - panic(err) + spanner := getMockedSpanner(t, heimdallSpan.ValidatorSet.Validators) + _bor.SetSpanner(spanner) + _bor.SetHeimdallClient(h) + + db := init.ethereum.ChainDb() + block := init.genesis.ToBlock(db) + + setDifficulty := func(header *types.Header) { + if IsSprintStart(header.Number.Uint64()) { + header.Difficulty = big.NewInt(int64(len(heimdallSpan.ValidatorSet.Validators))) } } - for { - - // for block 1 to 8, the primary validator is node0 - blockHeaderVal0 := nodes[0].BlockChain().CurrentHeader() - blockHeaderVal1 := nodes[1].BlockChain().CurrentHeader() + for i := uint64(1); i < spanSize; i++ { + block = buildNextBlock(t, _bor, chain, block, nil, init.genesis.Config.Bor, nil, heimdallSpan.ValidatorSet.Validators, setDifficulty) + insertNewBlock(t, chain, block) + } - // we remove peer connection between node0 and node1 - if blockHeaderVal0.Number.Uint64() == 1 { - stacks[0].Server().RemovePeer(enodes[1]) - } + // insert spanSize-th block + // This account is one the out-of-turn validators for 1st (0-indexed) span + signer := "c8deb0bea5c41afe8e37b4d1bd84e31adff11b09c8c96ff4b605003cce067cd9" + signerKey, _ := hex.DecodeString(signer) + newKey, _ := crypto.HexToECDSA(signer) + newAddr := crypto.PubkeyToAddress(newKey.PublicKey) + expectedSuccessionNumber := 2 - if blockHeaderVal1.Number.Uint64() == 9 { - break - } + parentTime := block.Time() + setParentTime := func(header *types.Header) { + header.Time = parentTime + 1 } - // function to get timestamp difference between two nodes of the same blockNumber - timeDiff := func(blockNum int) int { - blockHeaderVal0 := nodes[0].BlockChain().GetHeaderByNumber(uint64(blockNum)) - blockHeaderVal1 := nodes[1].BlockChain().GetHeaderByNumber(uint64(blockNum)) + const turn = 1 - return Abs(int(blockHeaderVal0.Time - blockHeaderVal1.Time)) + setDifficulty = func(header *types.Header) { + header.Difficulty = big.NewInt(int64(len(heimdallSpan.ValidatorSet.Validators)) - turn) } - // check block 10 miner ; expected author is node1 signer - blockHeaderVal0 := nodes[0].BlockChain().GetHeaderByNumber(3) - blockHeaderVal1 := nodes[1].BlockChain().GetHeaderByNumber(3) + block = buildNextBlock(t, _bor, chain, block, signerKey, init.genesis.Config.Bor, nil, heimdallSpan.ValidatorSet.Validators, setParentTime, setDifficulty) + _, err := chain.InsertChain([]*types.Block{block}) + require.Equal(t, + bor.BlockTooSoonError{Number: spanSize, Succession: expectedSuccessionNumber}, + *err.(*bor.BlockTooSoonError)) - // check both nodes have the same block 10 - assert.Equal(t, blockHeaderVal0.Difficulty, common.Big2) + expectedDifficulty := uint64(len(heimdallSpan.ValidatorSet.Validators) - expectedSuccessionNumber - turn) // len(validators) - succession + header := block.Header() - // check node0 has block mined by node1 - assert.Equal(t, blockHeaderVal1.Difficulty, common.Big1) + diff := bor.CalcProducerDelay(header.Number.Uint64(), expectedSuccessionNumber, init.genesis.Config.Bor) + header.Time += diff - // node seperated at block 1, so at block 3, node0 should be at 3 seconds, and node1 should be at 5( 1 + 2*2 ) seconds - assert.Equal(t, timeDiff(3), 2) + sign(t, header, signerKey, init.genesis.Config.Bor) - // check block 10 miner ; expected author is node1 signer - blockHeaderVal0 = nodes[0].BlockChain().GetHeaderByNumber(7) - blockHeaderVal1 = nodes[1].BlockChain().GetHeaderByNumber(7) + block = types.NewBlockWithHeader(header) - // check both nodes have the same block 10 - assert.Equal(t, blockHeaderVal0.Difficulty, common.Big2) + _, err = chain.InsertChain([]*types.Block{block}) + require.NotNil(t, err) + require.Equal(t, + bor.WrongDifficultyError{Number: spanSize, Expected: expectedDifficulty, Actual: 3, Signer: newAddr.Bytes()}, + *err.(*bor.WrongDifficultyError)) - // check node0 has block mined by node1 - assert.Equal(t, blockHeaderVal1.Difficulty, common.Big1) + header.Difficulty = new(big.Int).SetUint64(expectedDifficulty) + sign(t, header, signerKey, init.genesis.Config.Bor) + block = types.NewBlockWithHeader(header) - // node seperated at block 1, so at block 7, node0 should be at 7 seconds, and node1 should be at 13( 1 + 2*6 ) seconds - assert.Equal(t, timeDiff(7), 6) + _, err = chain.InsertChain([]*types.Block{block}) + require.Nil(t, err) } func TestSignerNotFound(t *testing.T) { @@ -1121,18 +1099,11 @@ func TestJaipurFork(t *testing.T) { res, _ := loadSpanFromFile(t) - currentValidators := []*valset.Validator{valset.NewValidator(addr, 10)} - - spanner := getMockedSpanner(t, currentValidators) + spanner := getMockedSpanner(t, res.Result.ValidatorSet.Validators) _bor.SetSpanner(spanner) for i := uint64(1); i < sprintSize; i++ { - if IsSpanEnd(i) { - currentValidators = res.Result.ValidatorSet.Validators - } - - block = buildNextBlock(t, _bor, chain, block, nil, init.genesis.Config.Bor, nil, currentValidators) - + block = buildNextBlock(t, _bor, chain, block, nil, init.genesis.Config.Bor, nil, res.Result.ValidatorSet.Validators) insertNewBlock(t, chain, block) if block.Number().Uint64() == init.genesis.Config.Bor.JaipurBlock.Uint64()-1 {