Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release Candidate v2024.0.0 - HIP-32 code (no mainnet HF) + Improvements #4546

Merged
merged 136 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
136 commits
Select commit Hold shift + click to select a range
da863bf
Fixed memory leak and deadcode. (#4539)
Frozen Oct 20, 2023
a65f92d
Fix typo in README test steps (#4541)
UncertainBadg3r Oct 23, 2023
5faeb5f
Call `UpdateConsensusInformation` after bingo. (#4542)
Frozen Oct 23, 2023
d49715e
add new functions to p2p stream client for sharing the full states (#…
GheisMohammadi Oct 24, 2023
57dd5f2
Fixed. (#4543)
Frozen Oct 26, 2023
b1389da
Fixed data race. (#4544)
Frozen Oct 26, 2023
2f23d81
Last mile blocks insert.
Frozen Jun 13, 2023
fad17fe
Added logs.
Frozen Jun 14, 2023
c638184
Added logs.
Frozen Jun 14, 2023
b6987ec
Added logs.
Frozen Jun 14, 2023
525b20a
Start and finish syncing.
Frozen Jun 14, 2023
0e45682
Passed peerID to handlers.
Frozen Jun 25, 2023
a6ddc19
Implemented method for interface.
Frozen Jun 25, 2023
82b3f0c
Updated.
Frozen Jul 23, 2023
41900b0
Rebased onto dev.
Frozen Oct 17, 2023
f0b70d9
Fixed imports.
Frozen Oct 20, 2023
6179450
Fixed imports.
Frozen Oct 23, 2023
96e0386
Fixed imports.
Frozen Oct 23, 2023
64d7392
Clean up.
Frozen Oct 25, 2023
dc326f6
Rebased.
Frozen Oct 26, 2023
f24413e
Merge pull request #4455 from Frozen/block-peers
Frozen Oct 26, 2023
e124173
Cleanup. (#4547)
Frozen Oct 30, 2023
4b8cf56
Leader rotation. Check next leader aliveness. (#4359)
Frozen Oct 31, 2023
3167545
Cleanup (#4548)
Frozen Nov 2, 2023
b187cd7
Deadcode and cleanup. (#4550)
Frozen Nov 8, 2023
1f7d678
Refactored verify block. (#4554)
Frozen Nov 10, 2023
6f7a047
Fixed data race. (#4559)
Frozen Nov 10, 2023
dbe4d43
Timeout for block proposal. (#4553)
Frozen Nov 10, 2023
2089a0c
Fixed import and workaround for failing test. (#4560)
Frozen Nov 13, 2023
8f774ea
Fix typos (#4563)
omahs Nov 15, 2023
582a4cf
Fixed panic with dsync. (#4562)
Frozen Nov 16, 2023
6eda785
fix the issue of adding an existed block in legacy sync (#4565)
GheisMohammadi Nov 17, 2023
78685f9
Removed future blocks from blockchain_impl.go (#4569)
Frozen Nov 22, 2023
1f974af
Removed fast block. (#4571)
Frozen Nov 23, 2023
368fc9e
Removed unused functions and improved locks usage. (#4572)
Frozen Nov 24, 2023
b7b7fbf
Skip epochchain errors. (#4573)
Frozen Nov 24, 2023
fe1d97a
Removed outdated check. (#4574)
Frozen Nov 29, 2023
f993468
fix: max-rate bellow the era min-rate (#4552)
diego1q2w Nov 29, 2023
7be2b02
Revert "fix: max-rate bellow the era min-rate (#4552)" (#4578)
Frozen Dec 2, 2023
b7123fb
fix: eth json transaction (#4581)
diego1q2w Dec 5, 2023
86fca20
Fixed infinity loop sync. (#4575)
Frozen Dec 5, 2023
3b27215
use v2.NewReceipt for eth type receipt
adsorptionenthalpy Dec 6, 2023
29c6249
Fix. (#4583)
Frozen Dec 7, 2023
2001b34
Merge pull request #4582 from harmony-one/feature/HET104-12523
adsorptionenthalpy Dec 7, 2023
ca05f3f
add statesync as a new state to staged sync
GheisMohammadi Jun 25, 2023
9ec0272
add initial state download manager to stream sync
GheisMohammadi Jun 26, 2023
2064cfd
add protocol to stage statesync
GheisMohammadi Jun 26, 2023
702eb5e
add task management logic to state download manager in stream sync
GheisMohammadi Jun 26, 2023
4629fda
fix statesync config
GheisMohammadi Jun 26, 2023
9e1249a
refactor state download manager
GheisMohammadi Jun 28, 2023
841073d
refactor stage state sync
GheisMohammadi Jun 28, 2023
975857f
fix state download manager tasks issue
GheisMohammadi Jun 29, 2023
0da96b9
add receipt download manager
GheisMohammadi Jun 29, 2023
12d930f
fix receipt download manager result queue
GheisMohammadi Jun 29, 2023
6f3aa67
refactor stage receipts and change the stages sorting
GheisMohammadi Jun 29, 2023
e11b6ef
goimports staged stream sync
GheisMohammadi Jun 29, 2023
9103468
add block insertion without execution to blockchain implementation
GheisMohammadi Jul 3, 2023
cfc94bb
fix tests for new block insertion
GheisMohammadi Jul 3, 2023
9954a90
refactor staged stream sync to process fast sync and new block insertion
GheisMohammadi Jul 3, 2023
3522127
refactor stage receipts
GheisMohammadi Jul 3, 2023
591f223
fix block insertion in main.go
GheisMohammadi Jul 3, 2023
7006e15
goimports staged sync files
GheisMohammadi Jul 3, 2023
30de7c2
refactor stages list initialization based on the sync mode
GheisMohammadi Jul 3, 2023
f10dd1e
add SyncMode to configs
GheisMohammadi Jul 3, 2023
498bcc0
fix state download manager failure message
GheisMohammadi Jul 4, 2023
1f26944
split verifyAndInsertBlock function to be able to reuse verification …
GheisMohammadi Jul 5, 2023
7c3807a
refactor stage bodies to extract receip hashes in this stage rather t…
GheisMohammadi Jul 5, 2023
d4c8577
goimports
GheisMohammadi Jul 5, 2023
8f81810
add InsertReceiptChain to blockchain interface
GheisMohammadi Jul 5, 2023
57a77ab
refactor get receipts stage to use insertReceiptsChain
GheisMohammadi Jul 6, 2023
bcf1b77
remove using currentCycle, cleanup stage receipts
GheisMohammadi Jul 6, 2023
6f9a1ec
goimports
GheisMohammadi Jul 6, 2023
12235f5
fix stages forward order for staged stream sync
GheisMohammadi Jul 10, 2023
f6b8951
add SyncMode to flags
GheisMohammadi Jul 13, 2023
cd7ccbe
fix stages and replace with forward stages
GheisMohammadi Aug 16, 2023
772d865
fix block validation in stage bodies
GheisMohammadi Sep 6, 2023
c1d352b
add pivot to chain accessor, add CurrentFastBlock to blockchain_impl,…
GheisMohammadi Sep 26, 2023
8d66bdf
add getBlockByMaxVote to sync helper
GheisMohammadi Sep 27, 2023
917a301
add tests for node data request
GheisMohammadi Oct 3, 2023
7c21eef
fix stream tests
GheisMohammadi Oct 3, 2023
d534fea
add Validator method to blockchain to fix the interface
GheisMohammadi Oct 3, 2023
e96855b
fix shard chain test
GheisMohammadi Oct 3, 2023
ebd689f
remove blockExecution option from insertChain
GheisMohammadi Oct 3, 2023
36d2abd
remove extra blockExecutions
GheisMohammadi Oct 4, 2023
9629d9c
remove blockExecution option from staged stream sync
GheisMohammadi Oct 4, 2023
e4dcda6
refactor staged stream sync, fix the state sync functions
GheisMohammadi Oct 4, 2023
6348128
improve stage handling for create new instance of staged stream sync
GheisMohammadi Oct 4, 2023
c808f2b
fix pivot block issue for write on chain
GheisMohammadi Oct 5, 2023
bdd7f14
improve stream sync current cycle and pivot checks, fix edge case iss…
GheisMohammadi Oct 6, 2023
135c7da
fix WriteHeadBlock, fix GetDownloadDetails index, improve fetching cu…
GheisMohammadi Oct 24, 2023
3fcfad4
fix rebase conflicts
GheisMohammadi Oct 26, 2023
9992825
add state sync
GheisMohammadi Nov 14, 2023
c340c70
fix GetNextBatch to complete sync after there is no more pending stat…
GheisMohammadi Nov 16, 2023
3374100
fix state sync file name spell error
GheisMohammadi Nov 16, 2023
e141f79
add ProofSet and ProofList to staged stream sync
GheisMohammadi Dec 7, 2023
390bdb6
add client new functions to stream sync adapter, update GetAccountRan…
GheisMohammadi Dec 7, 2023
0901e92
add state sync full, complete full state sync stage
GheisMohammadi Dec 7, 2023
02e2fee
Fix: max rate issue (#4580)
diego1q2w Dec 10, 2023
f3ce9f3
return back deleted codes, fix rebase issues, goimports
GheisMohammadi Dec 11, 2023
191c55b
fix full state sync requests cap, add error handling to stage state s…
GheisMohammadi Dec 12, 2023
419aad1
remove state debug logs
GheisMohammadi Dec 12, 2023
c82599b
Merge pull request #4465 from harmony-one/feature/fastsync
adsorptionenthalpy Dec 12, 2023
9a5ba3c
blocksCountAliveness can't be bigger than minimumBlocksForLeaderInRow…
Frozen Dec 20, 2023
556444c
Feature: last signing power. (#4584)
Frozen Dec 20, 2023
d6690ed
Activate external rotation devnet. (#4596)
Frozen Dec 22, 2023
718286f
delay for a few epochs the leader rotations devnet (#4597)
diego1q2w Dec 24, 2023
41066fa
fix conflicts main->dev
adsorptionenthalpy Dec 29, 2023
e68b44f
Complete Fast Sync codes (#4594)
GheisMohammadi Jan 9, 2024
5443bf7
reduce internal voting power to 10% devnet (#4599)
diego1q2w Jan 9, 2024
7df72eb
Merge branch 'dev' into maintodev122023
ONECasey Jan 9, 2024
2927929
Merge pull request #4600 from harmony-one/maintodev122023
sophoah Jan 9, 2024
50a1a75
ignore known blocks for processing state sync (#4602)
GheisMohammadi Jan 9, 2024
f576910
Force verify all sign. (#4601)
Frozen Jan 10, 2024
00734e2
closing stream removes it from stream manager as well, so it doesn't …
GheisMohammadi Jan 11, 2024
1dd67a8
15 nodes per shard for devnet (#4607)
Frozen Jan 11, 2024
a31b4f5
Thread safe Decider. (#4610)
Frozen Jan 16, 2024
cdbc79e
Devnet: activate leader rotation at epoch 4 . (#4611)
Frozen Jan 17, 2024
e15bae1
Fix for panic "insertChain failed to update current block" (#4612)
Frozen Jan 18, 2024
17a2522
Recreate trie after revert. (#4608)
Frozen Jan 18, 2024
51a1ffe
prepare devnet reset (#4615)
diego1q2w Jan 23, 2024
2441a0b
Fix fast sync null snapshot and null response issue (#4613)
GheisMohammadi Jan 25, 2024
8d24b5b
fix wrong root hash for generating snapshot
GheisMohammadi Jan 25, 2024
d1ffd5c
add cache configurations
GheisMohammadi Jan 25, 2024
9b42870
return back sync default settings
GheisMohammadi Jan 25, 2024
f3fc633
Fix for revert. (#4617)
Frozen Jan 25, 2024
fe72c4f
activate devnet external hardfork (#4619)
diego1q2w Jan 26, 2024
0066c2e
push the activation of the external devnet epoch (#4620)
diego1q2w Jan 27, 2024
0082c4d
add system log for creating/loading snapshot
GheisMohammadi Jan 29, 2024
c01cc29
Merge pull request #4618 from harmony-one/fix/snapshot
adsorptionenthalpy Jan 29, 2024
ae4ffeb
fix allowed txs to be able to handle multiple txs for same from addre…
GheisMohammadi Jan 30, 2024
8d5f20f
Removed outdated flag, additional checks and simplified logic. (#4621)
Frozen Jan 30, 2024
ca91cb2
Skip check when the 100% check is achieved. (#4625)
diego1q2w Feb 2, 2024
b53a911
push devnet external and leader rotation epochs (#4634)
diego1q2w Feb 20, 2024
138a460
Update dev to include main hotfixes (#4633)
ONECasey Feb 21, 2024
86c4a21
Merge branch 'main' into dev
ONECasey Feb 21, 2024
49bba17
Fix. (#4635)
Frozen Feb 22, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,4 @@ debug_external: clean
bash test/debug-external.sh

build_localnet_validator:
bash test/build-localnet-validator.sh
bash test/build-localnet-validator.sh
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ make debug-kill

To keep things consistent, we have a docker image to run all tests. **These are the same tests ran on the pull request checks**.

Note that all testing docker container binds a couple of ports to the host machine for your convince. The ports are:
Note that all test Docker containers bind several ports to the host machine for your convenience. The ports are:
* `9500` - Shard 0 RPC for a validator
* `9501` - Shard 1 RPC for a validator
* `9599` - Shard 0 RPC for an explorer
Expand Down
14 changes: 12 additions & 2 deletions api/service/legacysync/epoch_syncing.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,18 @@ func processWithPayload(payload [][]byte, bc core.BlockChain) error {
decoded = append(decoded, block)
}

_, err := bc.InsertChain(decoded, true)
return err
for _, block := range decoded {
_, err := bc.InsertChain([]*types.Block{block}, true)
switch {
case errors.Is(err, core.ErrKnownBlock):
continue
case err != nil:
return err
default:
}
}

return nil
}

// CreateSyncConfig creates SyncConfig for StateSync object.
Expand Down
5 changes: 4 additions & 1 deletion api/service/stagedstreamsync/beacon_helper.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package stagedstreamsync

import (
"errors"
"time"

"github.com/harmony-one/harmony/core"
"github.com/harmony-one/harmony/core/types"
"github.com/harmony-one/harmony/internal/utils"
"github.com/rs/zerolog"
Expand Down Expand Up @@ -126,7 +128,8 @@ func (bh *beaconHelper) insertLastMileBlocks() (inserted int, bn uint64, err err
}
// TODO: Instruct the beacon helper to verify signatures. This may require some forks
// in pub-sub message (add commit sigs in node.block.sync messages)
if _, err = bh.bc.InsertChain(types.Blocks{b}, true); err != nil {
_, err = bh.bc.InsertChain(types.Blocks{b}, true)
if err != nil && !errors.Is(err, core.ErrKnownBlock) {
bn--
return
}
Expand Down
9 changes: 8 additions & 1 deletion api/service/stagedstreamsync/sig_verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package stagedstreamsync
import (
"fmt"

"github.com/harmony-one/harmony/core"
"github.com/harmony-one/harmony/core/types"
"github.com/harmony-one/harmony/crypto/bls"
"github.com/harmony-one/harmony/internal/chain"
Expand Down Expand Up @@ -53,8 +54,14 @@ func verifyAndInsertBlock(bc blockChain, block *types.Block, nextBlocks ...*type
if err := bc.Engine().VerifyHeader(bc, block.Header(), true); err != nil {
return errors.Wrap(err, "[VerifyHeader]")
}
if _, err := bc.InsertChain(types.Blocks{block}, false); err != nil {
_, err = bc.InsertChain(types.Blocks{block}, false)
switch {
case errors.Is(err, core.ErrKnownBlock):
return nil
case err != nil:
return errors.Wrap(err, "[InsertChain]")
default:

}
return nil
}
20 changes: 14 additions & 6 deletions api/service/stagedstreamsync/stage_epoch.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"

"github.com/harmony-one/harmony/core"
"github.com/harmony-one/harmony/core/types"
"github.com/harmony-one/harmony/internal/utils"
sttypes "github.com/harmony-one/harmony/p2p/stream/types"
"github.com/harmony-one/harmony/shard"
Expand Down Expand Up @@ -129,13 +130,20 @@ func (sr *StageEpoch) doShortRangeSyncForEpochSync(ctx context.Context, s *Stage
return 0, nil
}

n, err := s.state.bc.InsertChain(blocks, true)
numBlocksInsertedShortRangeHistogramVec.With(s.state.promLabels()).Observe(float64(n))
if err != nil {
utils.Logger().Info().Err(err).Int("blocks inserted", n).Msg("Insert block failed")
sh.streamsFailed([]sttypes.StreamID{streamID}, "corrupted data")
return n, err
n := 0
for _, block := range blocks {
_, err := s.state.bc.InsertChain([]*types.Block{block}, true)
switch {
case errors.Is(err, core.ErrKnownBlock):
case err != nil:
utils.Logger().Info().Err(err).Int("blocks inserted", n).Msg("Insert block failed")
sh.streamsFailed([]sttypes.StreamID{streamID}, "corrupted data")
return n, err
default:
}
n++
}
numBlocksInsertedShortRangeHistogramVec.With(s.state.promLabels()).Observe(float64(n))
return n, nil
}

Expand Down
8 changes: 6 additions & 2 deletions api/service/stagedstreamsync/staged_stream_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,8 @@ func (ss *StagedStreamSync) addConsensusLastMile(bc core.BlockChain, cs *consens
if block == nil {
break
}
if _, err := bc.InsertChain(types.Blocks{block}, true); err != nil {
_, err := bc.InsertChain(types.Blocks{block}, true)
if err != nil && !errors.Is(err, core.ErrKnownBlock) {
return errors.Wrap(err, "failed to InsertChain")
}
hashes = append(hashes, block.Header().Hash())
Expand Down Expand Up @@ -704,13 +705,16 @@ func (ss *StagedStreamSync) UpdateBlockAndStatus(block *types.Block, bc core.Blo
}

_, err := bc.InsertChain([]*types.Block{block}, false /* verifyHeaders */)
if err != nil {
switch {
case errors.Is(err, core.ErrKnownBlock):
case err != nil:
utils.Logger().Error().
Err(err).
Uint64("block number", block.NumberU64()).
Uint32("shard", block.ShardID()).
Msgf("[STAGED_STREAM_SYNC] UpdateBlockAndStatus: Error adding new block to blockchain")
return err
default:
}
utils.Logger().Info().
Uint64("blockHeight", block.NumberU64()).
Expand Down
2 changes: 1 addition & 1 deletion api/service/stagedsync/stage_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func (stg *StageStates) Exec(firstCycle bool, invalidBlockRevert bool, s *StageS
headBeforeNewBlocks := stg.configs.bc.CurrentBlock().NumberU64()
headHashBeforeNewBlocks := stg.configs.bc.CurrentBlock().Hash()
_, err = stg.configs.bc.InsertChain(newBlocks, false) //TODO: verifyHeaders can be done here
if err != nil {
if err != nil && !errors.Is(err, core.ErrKnownBlock) {
// TODO: handle chain rollback because of bad block
utils.Logger().Error().
Err(err).
Expand Down
7 changes: 5 additions & 2 deletions api/service/stagedsync/stagedsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -1091,13 +1091,16 @@ func (ss *StagedSync) UpdateBlockAndStatus(block *types.Block, bc core.BlockChai
}

_, err := bc.InsertChain([]*types.Block{block}, false /* verifyHeaders */)
if err != nil {
switch {
case errors.Is(err, core.ErrKnownBlock):
case err != nil:
utils.Logger().Error().
Err(err).
Uint64("block number", block.NumberU64()).
Uint32("shard", block.ShardID()).
Msgf("[STAGED_SYNC] UpdateBlockAndStatus: Error adding new block to blockchain")
return err
default:
}
utils.Logger().Info().
Uint64("blockHeight", block.NumberU64()).
Expand Down Expand Up @@ -1218,7 +1221,7 @@ func (ss *StagedSync) addConsensusLastMile(bc core.BlockChain, cs *consensus.Con
if block == nil {
break
}
if _, err := bc.InsertChain(types.Blocks{block}, true); err != nil {
if _, err := bc.InsertChain(types.Blocks{block}, true); err != nil && !errors.Is(err, core.ErrKnownBlock) {
return errors.Wrap(err, "failed to InsertChain")
}
}
Expand Down
2 changes: 1 addition & 1 deletion consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ func New(
// viewID has to be initialized as the height of
// the blockchain during initialization as it was
// displayed on explorer as Height right now
consensus.SetCurBlockViewID(0)
consensus.setCurBlockViewID(0)
consensus.SlashChan = make(chan slash.Record)
consensus.readySignal = make(chan ProposalType)
consensus.commitSigChannel = make(chan []byte)
Expand Down
8 changes: 5 additions & 3 deletions consensus/consensus_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,12 +514,14 @@ func (consensus *Consensus) setViewIDs(height uint64) {

// SetCurBlockViewID set the current view ID
func (consensus *Consensus) SetCurBlockViewID(viewID uint64) uint64 {
return consensus.current.SetCurBlockViewID(viewID)
consensus.mutex.Lock()
defer consensus.mutex.Unlock()
return consensus.setCurBlockViewID(viewID)
}

// SetCurBlockViewID set the current view ID
func (consensus *Consensus) setCurBlockViewID(viewID uint64) {
consensus.current.SetCurBlockViewID(viewID)
func (consensus *Consensus) setCurBlockViewID(viewID uint64) uint64 {
return consensus.current.SetCurBlockViewID(viewID)
}

// SetViewChangingID set the current view change ID
Expand Down
14 changes: 5 additions & 9 deletions consensus/consensus_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/harmony-one/harmony/core"
nodeconfig "github.com/harmony-one/harmony/internal/configs/node"
"github.com/harmony-one/harmony/internal/utils"
libp2p_peer "github.com/libp2p/go-libp2p/core/peer"
"github.com/rs/zerolog"

msg_pb "github.com/harmony-one/harmony/api/proto/message"
Expand Down Expand Up @@ -55,7 +56,7 @@ func (consensus *Consensus) isViewChangingMode() bool {
}

// HandleMessageUpdate will update the consensus state according to received message
func (consensus *Consensus) HandleMessageUpdate(ctx context.Context, msg *msg_pb.Message, senderKey *bls.SerializedPublicKey) error {
func (consensus *Consensus) HandleMessageUpdate(ctx context.Context, peer libp2p_peer.ID, msg *msg_pb.Message, senderKey *bls.SerializedPublicKey) error {
consensus.mutex.Lock()
defer consensus.mutex.Unlock()
// when node is in ViewChanging mode, it still accepts normal messages into FBFTLog
Expand Down Expand Up @@ -323,9 +324,7 @@ func (consensus *Consensus) Start(
consensus.mutex.Unlock()
}()

if consensus.dHelper != nil {
consensus.dHelper.start()
}
consensus.dHelper.start()
}

func (consensus *Consensus) StartChannel() {
Expand Down Expand Up @@ -448,10 +447,6 @@ func (consensus *Consensus) BlockChannel(newBlock *types.Block) {
Msg("[ConsensusMainLoop] STARTING CONSENSUS")
consensus.announce(newBlock)
})

if consensus.dHelper != nil {
consensus.dHelper.start()
}
}

// LastMileBlockIter is the iterator to iterate over the last mile blocks in consensus cache.
Expand Down Expand Up @@ -667,7 +662,8 @@ func (consensus *Consensus) tryCatchup() error {

func (consensus *Consensus) commitBlock(blk *types.Block, committedMsg *FBFTMessage) error {
if consensus.Blockchain().CurrentBlock().NumberU64() < blk.NumberU64() {
if _, err := consensus.Blockchain().InsertChain([]*types.Block{blk}, !consensus.fBFTLog.IsBlockVerified(blk.Hash())); err != nil {
_, err := consensus.Blockchain().InsertChain([]*types.Block{blk}, !consensus.fBFTLog.IsBlockVerified(blk.Hash()))
if err != nil && !errors.Is(err, core.ErrKnownBlock) {
consensus.getLogger().Error().Err(err).Msg("[commitBlock] Failed to add block to chain")
return err
}
Expand Down
34 changes: 10 additions & 24 deletions consensus/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package consensus

import (
"github.com/ethereum/go-ethereum/event"
"github.com/harmony-one/harmony/core"
"github.com/harmony-one/harmony/core/types"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -39,24 +40,20 @@ func newDownloadHelper(c *Consensus, d downloader) *downloadHelper {
finishedCh := make(chan struct{}, 1)
finishedSub := d.SubscribeDownloadFinished(finishedCh)

return &downloadHelper{
out := &downloadHelper{
c: c,
d: d,
startedCh: startedCh,
finishedCh: finishedCh,
startedSub: startedSub,
finishedSub: finishedSub,
}
go out.downloadStartedLoop()
go out.downloadFinishedLoop()
return out
}

func (dh *downloadHelper) start() {
go dh.downloadStartedLoop()
go dh.downloadFinishedLoop()
}

func (dh *downloadHelper) close() {
dh.startedSub.Unsubscribe()
dh.finishedSub.Unsubscribe()
}

func (dh *downloadHelper) downloadStartedLoop() {
Expand Down Expand Up @@ -97,7 +94,7 @@ func (consensus *Consensus) AddConsensusLastMile() error {
if block == nil {
break
}
if _, err := consensus.Blockchain().InsertChain(types.Blocks{block}, true); err != nil {
if _, err := consensus.Blockchain().InsertChain(types.Blocks{block}, true); err != nil && !errors.Is(err, core.ErrKnownBlock) {
return errors.Wrap(err, "failed to InsertChain")
}
}
Expand All @@ -107,21 +104,10 @@ func (consensus *Consensus) AddConsensusLastMile() error {
}

func (consensus *Consensus) spinUpStateSync() {
if consensus.dHelper != nil {
consensus.dHelper.d.DownloadAsync()
consensus.current.SetMode(Syncing)
for _, v := range consensus.consensusTimeout {
v.Stop()
}
} else {
select {
case consensus.BlockNumLowChan <- struct{}{}:
consensus.current.SetMode(Syncing)
for _, v := range consensus.consensusTimeout {
v.Stop()
}
default:
}
consensus.dHelper.d.DownloadAsync()
consensus.current.SetMode(Syncing)
for _, v := range consensus.consensusTimeout {
v.Stop()
}
}

Expand Down
8 changes: 8 additions & 0 deletions consensus/engine/consensus_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import (
"math/big"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/trie"
"github.com/harmony-one/harmony/block"
"github.com/harmony-one/harmony/consensus/reward"
"github.com/harmony-one/harmony/core/state"
"github.com/harmony-one/harmony/core/state/snapshot"
"github.com/harmony-one/harmony/core/types"
"github.com/harmony-one/harmony/crypto/bls"
"github.com/harmony-one/harmony/internal/params"
Expand All @@ -23,6 +25,9 @@ type ChainReader interface {
// Config retrieves the blockchain's chain configuration.
Config() *params.ChainConfig

// TrieDB returns trie database
TrieDB() *trie.Database

// TrieNode retrieves a blob of data associated with a trie node
// either from ephemeral in-memory cache, or from persistent storage.
TrieNode(hash common.Hash) ([]byte, error)
Expand Down Expand Up @@ -62,6 +67,9 @@ type ChainReader interface {
// GetBlock retrieves a block from the database by hash and number.
GetBlock(hash common.Hash, number uint64) *types.Block

// Snapshots returns the blockchain snapshot tree.
Snapshots() *snapshot.Tree

// ReadShardState retrieves sharding state given the epoch number.
// This api reads the shard state cached or saved on the chaindb.
// Thus, only should be used to read the shard state of the current chain.
Expand Down
5 changes: 5 additions & 0 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/trie"
"github.com/harmony-one/harmony/block"
"github.com/harmony-one/harmony/consensus/engine"
"github.com/harmony-one/harmony/consensus/reward"
Expand Down Expand Up @@ -62,6 +63,10 @@ type BlockChain interface {
State() (*state.DB, error)
// StateAt returns a new mutable state based on a particular point in time.
StateAt(root common.Hash) (*state.DB, error)
// Snapshots returns the blockchain snapshot tree.
Snapshots() *snapshot.Tree
// TrieDB returns trie database
TrieDB() *trie.Database
// HasBlock checks if a block is fully present in the database or not.
HasBlock(hash common.Hash, number uint64) bool
// HasState checks if state trie is fully present in the database or not.
Expand Down
5 changes: 5 additions & 0 deletions core/blockchain_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -1122,6 +1122,11 @@ func (bc *BlockChainImpl) GetUnclesInChain(b *types.Block, length int) []*block.
return uncles
}

// TrieDB returns trie database
func (bc *BlockChainImpl) TrieDB() *trie.Database {
return bc.stateCache.TrieDB()
}

// TrieNode retrieves a blob of data associated with a trie node (or code hash)
// either from ephemeral in-memory cache, or from persistent storage.
func (bc *BlockChainImpl) TrieNode(hash common.Hash) ([]byte, error) {
Expand Down
Loading