Skip to content

Commit 65e5cc2

Browse files
authored
Merge pull request #4470 from harmony-one/dev
Release Candidate 2023.2.5 (dev -> main)
2 parents c7a63ba + 0e4b629 commit 65e5cc2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1382
-276
lines changed

Diff for: api/service/stagedstreamsync/adapter.go

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ type syncProtocol interface {
1818
GetRawBlocksByNumber(ctx context.Context, bns []uint64, opts ...syncproto.Option) ([][]byte, [][]byte, sttypes.StreamID, error)
1919
GetBlockHashes(ctx context.Context, bns []uint64, opts ...syncproto.Option) ([]common.Hash, sttypes.StreamID, error)
2020
GetBlocksByHashes(ctx context.Context, hs []common.Hash, opts ...syncproto.Option) ([]*types.Block, sttypes.StreamID, error)
21+
GetReceipts(ctx context.Context, hs []common.Hash, opts ...syncproto.Option) (receipts []types.Receipts, stid sttypes.StreamID, err error)
22+
GetNodeData(ctx context.Context, hs []common.Hash, opts ...syncproto.Option) (data [][]byte, stid sttypes.StreamID, err error)
2123

2224
RemoveStream(stID sttypes.StreamID) // If a stream delivers invalid data, remove the stream
2325
StreamFailed(stID sttypes.StreamID, reason string)

Diff for: consensus/checks.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func (consensus *Consensus) isRightBlockNumAndViewID(recvMsg *FBFTMessage) bool
6868
}
6969

7070
func (consensus *Consensus) onAnnounceSanityChecks(recvMsg *FBFTMessage) bool {
71-
logMsgs := consensus.FBFTLog.GetMessagesByTypeSeqView(
71+
logMsgs := consensus.fBFTLog.GetMessagesByTypeSeqView(
7272
msg_pb.MessageType_ANNOUNCE, recvMsg.BlockNum, recvMsg.ViewID,
7373
)
7474
if len(logMsgs) > 0 {

Diff for: consensus/consensus.go

+25-18
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ type VerifyBlockFunc func(*types.Block) error
4646
type Consensus struct {
4747
Decider quorum.Decider
4848
// FBFTLog stores the pbft messages and blocks during FBFT process
49-
FBFTLog *FBFTLog
49+
fBFTLog *FBFTLog
5050
// phase: different phase of FBFT protocol: pre-prepare, prepare, commit, finish etc
5151
phase FBFTPhase
5252
// current indicates what state a node is in
@@ -84,7 +84,7 @@ type Consensus struct {
8484
// IgnoreViewIDCheck determines whether to ignore viewID check
8585
IgnoreViewIDCheck *abool.AtomicBool
8686
// consensus mutex
87-
mutex sync.RWMutex
87+
mutex *sync.RWMutex
8888
// ViewChange struct
8989
vc *viewChange
9090
// Signal channel for proposing a new block and start new consensus
@@ -140,6 +140,13 @@ func (consensus *Consensus) Blockchain() core.BlockChain {
140140
return consensus.registry.GetBlockchain()
141141
}
142142

143+
func (consensus *Consensus) FBFTLog() FBFT {
144+
return threadsafeFBFTLog{
145+
log: consensus.fBFTLog,
146+
mu: consensus.mutex,
147+
}
148+
}
149+
143150
// ChainReader returns the chain reader.
144151
// This is mostly the same as Blockchain, but it returns only read methods, so we assume it's safe for concurrent use.
145152
func (consensus *Consensus) ChainReader() engine.ChainReader {
@@ -165,11 +172,11 @@ func (consensus *Consensus) Beaconchain() core.BlockChain {
165172

166173
// VerifyBlock is a function used to verify the block and keep trace of verified blocks.
167174
func (consensus *Consensus) verifyBlock(block *types.Block) error {
168-
if !consensus.FBFTLog.IsBlockVerified(block.Hash()) {
175+
if !consensus.fBFTLog.IsBlockVerified(block.Hash()) {
169176
if err := consensus.BlockVerifier(block); err != nil {
170177
return errors.Errorf("Block verification failed: %s", err)
171178
}
172-
consensus.FBFTLog.MarkBlockVerified(block)
179+
consensus.fBFTLog.MarkBlockVerified(block)
173180
}
174181
return nil
175182
}
@@ -261,21 +268,21 @@ func New(
261268
Decider quorum.Decider, minPeers int, aggregateSig bool,
262269
) (*Consensus, error) {
263270
consensus := Consensus{
264-
ShardID: shard,
271+
mutex: &sync.RWMutex{},
272+
ShardID: shard,
273+
fBFTLog: NewFBFTLog(),
274+
phase: FBFTAnnounce,
275+
current: State{mode: Normal},
276+
Decider: Decider,
277+
registry: registry,
278+
MinPeers: minPeers,
279+
AggregateSig: aggregateSig,
280+
host: host,
281+
msgSender: NewMessageSender(host),
282+
BlockNumLowChan: make(chan struct{}, 1),
283+
// FBFT timeout
284+
consensusTimeout: createTimeout(),
265285
}
266-
consensus.Decider = Decider
267-
consensus.registry = registry
268-
consensus.MinPeers = minPeers
269-
consensus.AggregateSig = aggregateSig
270-
consensus.host = host
271-
consensus.msgSender = NewMessageSender(host)
272-
consensus.BlockNumLowChan = make(chan struct{}, 1)
273-
// FBFT related
274-
consensus.FBFTLog = NewFBFTLog()
275-
consensus.phase = FBFTAnnounce
276-
consensus.current = State{mode: Normal}
277-
// FBFT timeout
278-
consensus.consensusTimeout = createTimeout()
279286

280287
if multiBLSPriKey != nil {
281288
consensus.priKey = multiBLSPriKey

Diff for: consensus/consensus_service.go

+6-9
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,9 @@ func (consensus *Consensus) updateBitmaps() {
145145
Str("MessageType", consensus.phase.String()).
146146
Msg("[UpdateBitmaps] Updating consensus bitmaps")
147147
members := consensus.Decider.Participants()
148-
prepareBitmap, _ := bls_cosi.NewMask(members, nil)
149-
commitBitmap, _ := bls_cosi.NewMask(members, nil)
150-
multiSigBitmap, _ := bls_cosi.NewMask(members, nil)
148+
prepareBitmap := bls_cosi.NewMask(members)
149+
commitBitmap := bls_cosi.NewMask(members)
150+
multiSigBitmap := bls_cosi.NewMask(members)
151151
consensus.prepareBitmap = prepareBitmap
152152
consensus.commitBitmap = commitBitmap
153153
consensus.multiSigBitmap = multiSigBitmap
@@ -575,7 +575,7 @@ func (consensus *Consensus) selfCommit(payload []byte) error {
575575
copy(blockHash[:], payload[:32])
576576

577577
// Leader sign and add commit message
578-
block := consensus.FBFTLog.GetBlockByHash(blockHash)
578+
block := consensus.fBFTLog.GetBlockByHash(blockHash)
579579
if block == nil {
580580
return errGetPreparedBlock
581581
}
@@ -627,11 +627,8 @@ func (consensus *Consensus) NumSignaturesIncludedInBlock(block *types.Block) uin
627627
count := uint32(0)
628628
members := consensus.Decider.Participants()
629629
// TODO(audit): do not reconstruct the Mask
630-
mask, err := bls.NewMask(members, nil)
631-
if err != nil {
632-
return count
633-
}
634-
err = mask.SetMask(block.Header().LastCommitBitmap())
630+
mask := bls.NewMask(members)
631+
err := mask.SetMask(block.Header().LastCommitBitmap())
635632
if err != nil {
636633
return count
637634
}

Diff for: consensus/consensus_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ func TestConsensusInitialization(t *testing.T) {
2222
assert.NoError(t, err)
2323

2424
messageSender := &MessageSender{host: host, retryTimes: int(phaseDuration.Seconds()) / RetryIntervalInSec}
25-
fbtLog := NewFBFTLog()
2625
state := State{mode: Normal}
2726

2827
timeouts := createTimeout()
@@ -37,7 +36,7 @@ func TestConsensusInitialization(t *testing.T) {
3736
assert.IsType(t, make(chan struct{}), consensus.BlockNumLowChan)
3837

3938
// FBFTLog
40-
assert.Equal(t, fbtLog, consensus.FBFTLog)
39+
assert.NotNil(t, consensus.FBFTLog())
4140

4241
assert.Equal(t, FBFTAnnounce, consensus.phase)
4342

Diff for: consensus/consensus_v2.go

+13-20
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,10 @@ func (consensus *Consensus) finalCommit() {
161161
network.Bytes,
162162
network.FBFTMsg
163163
commitSigAndBitmap := FBFTMsg.Payload
164-
consensus.FBFTLog.AddVerifiedMessage(FBFTMsg)
164+
consensus.fBFTLog.AddVerifiedMessage(FBFTMsg)
165165
// find correct block content
166166
curBlockHash := consensus.blockHash
167-
block := consensus.FBFTLog.GetBlockByHash(curBlockHash)
167+
block := consensus.fBFTLog.GetBlockByHash(curBlockHash)
168168
if block == nil {
169169
consensus.getLogger().Warn().
170170
Str("blockHash", hex.EncodeToString(curBlockHash[:])).
@@ -278,7 +278,7 @@ func (consensus *Consensus) BlockCommitSigs(blockNum uint64) ([]byte, error) {
278278
lastCommits, err := consensus.Blockchain().ReadCommitSig(blockNum)
279279
if err != nil ||
280280
len(lastCommits) < bls.BLSSignatureSizeInBytes {
281-
msgs := consensus.FBFTLog.GetMessagesByTypeSeq(
281+
msgs := consensus.FBFTLog().GetMessagesByTypeSeq(
282282
msg_pb.MessageType_COMMITTED, blockNum,
283283
)
284284
if len(msgs) != 1 {
@@ -482,7 +482,7 @@ func (consensus *Consensus) getLastMileBlockIter(bnStart uint64, cb func(iter *L
482482
}
483483
return cb(&LastMileBlockIter{
484484
blockCandidates: blocks,
485-
fbftLog: consensus.FBFTLog,
485+
fbftLog: consensus.fBFTLog,
486486
verify: consensus.BlockVerifier,
487487
curIndex: 0,
488488
logger: consensus.getLogger(),
@@ -513,7 +513,7 @@ func (consensus *Consensus) getLastMileBlocksAndMsg(bnStart uint64) ([]*types.Bl
513513
msgs []*FBFTMessage
514514
)
515515
for blockNum := bnStart; ; blockNum++ {
516-
blk, msg, err := consensus.FBFTLog.GetCommittedBlockAndMsgsFromNumber(blockNum, consensus.getLogger())
516+
blk, msg, err := consensus.fBFTLog.GetCommittedBlockAndMsgsFromNumber(blockNum, consensus.getLogger())
517517
if err != nil {
518518
if err == errFBFTLogNotFound {
519519
break
@@ -551,7 +551,7 @@ func (consensus *Consensus) preCommitAndPropose(blk *types.Block) error {
551551
network.Bytes,
552552
network.FBFTMsg
553553
bareMinimumCommit := FBFTMsg.Payload
554-
consensus.FBFTLog.AddVerifiedMessage(FBFTMsg)
554+
consensus.fBFTLog.AddVerifiedMessage(FBFTMsg)
555555

556556
if err := consensus.verifyLastCommitSig(bareMinimumCommit, blk); err != nil {
557557
return errors.Wrap(err, "[preCommitAndPropose] failed verifying last commit sig")
@@ -567,16 +567,16 @@ func (consensus *Consensus) preCommitAndPropose(blk *types.Block) error {
567567
nodeconfig.NewGroupIDByShardID(nodeconfig.ShardID(consensus.ShardID)),
568568
},
569569
p2p.ConstructMessage(msgToSend)); err != nil {
570-
consensus.getLogger().Warn().Err(err).Msg("[preCommitAndPropose] Cannot send committed message")
570+
consensus.GetLogger().Warn().Err(err).Msg("[preCommitAndPropose] Cannot send committed message")
571571
} else {
572-
consensus.getLogger().Info().
572+
consensus.GetLogger().Info().
573573
Str("blockHash", blk.Hash().Hex()).
574574
Uint64("blockNum", consensus.BlockNum()).
575575
Hex("lastCommitSig", bareMinimumCommit).
576576
Msg("[preCommitAndPropose] Sent Committed Message")
577577
}
578578

579-
if _, err := consensus.Blockchain().InsertChain([]*types.Block{blk}, !consensus.FBFTLog.IsBlockVerified(blk.Hash())); err != nil {
579+
if _, err := consensus.Blockchain().InsertChain([]*types.Block{blk}, !consensus.FBFTLog().IsBlockVerified(blk.Hash())); err != nil {
580580
consensus.getLogger().Error().Err(err).Msg("[preCommitAndPropose] Failed to add block to chain")
581581
return
582582
}
@@ -661,7 +661,7 @@ func (consensus *Consensus) tryCatchup() error {
661661

662662
func (consensus *Consensus) commitBlock(blk *types.Block, committedMsg *FBFTMessage) error {
663663
if consensus.Blockchain().CurrentBlock().NumberU64() < blk.NumberU64() {
664-
if _, err := consensus.Blockchain().InsertChain([]*types.Block{blk}, !consensus.FBFTLog.IsBlockVerified(blk.Hash())); err != nil {
664+
if _, err := consensus.Blockchain().InsertChain([]*types.Block{blk}, !consensus.fBFTLog.IsBlockVerified(blk.Hash())); err != nil {
665665
consensus.getLogger().Error().Err(err).Msg("[commitBlock] Failed to add block to chain")
666666
return err
667667
}
@@ -785,7 +785,7 @@ func (consensus *Consensus) setupForNewConsensus(blk *types.Block, committedMsg
785785
if blk.IsLastBlockInEpoch() {
786786
consensus.setMode(consensus.updateConsensusInformation())
787787
}
788-
consensus.FBFTLog.PruneCacheBeforeBlock(blk.NumberU64())
788+
consensus.fBFTLog.PruneCacheBeforeBlock(blk.NumberU64())
789789
consensus.resetState()
790790
}
791791

@@ -920,19 +920,12 @@ func (consensus *Consensus) ValidateVdfAndProof(headerObj *block.Header) bool {
920920
func (consensus *Consensus) DeleteBlocksLessThan(number uint64) {
921921
consensus.mutex.Lock()
922922
defer consensus.mutex.Unlock()
923-
consensus.FBFTLog.deleteBlocksLessThan(number)
923+
consensus.fBFTLog.deleteBlocksLessThan(number)
924924
}
925925

926926
// DeleteMessagesLessThan deletes messages less than given block number.
927927
func (consensus *Consensus) DeleteMessagesLessThan(number uint64) {
928928
consensus.mutex.Lock()
929929
defer consensus.mutex.Unlock()
930-
consensus.FBFTLog.deleteMessagesLessThan(number)
931-
}
932-
933-
// DeleteBlockByNumber deletes block by given block number.
934-
func (consensus *Consensus) DeleteBlockByNumber(number uint64) {
935-
consensus.mutex.Lock()
936-
defer consensus.mutex.Unlock()
937-
consensus.FBFTLog.deleteBlockByNumber(number)
930+
consensus.fBFTLog.deleteMessagesLessThan(number)
938931
}

Diff for: consensus/construct.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,7 @@ func (consensus *Consensus) construct(
8282
)
8383
} else {
8484
// TODO: use a persistent bitmap to report bitmap
85-
mask, err := bls.NewMask(consensus.Decider.Participants(), nil)
86-
if err != nil {
87-
utils.Logger().Warn().Err(err).Msg("unable to setup mask for multi-sig message")
88-
return nil, err
89-
}
85+
mask := bls.NewMask(consensus.Decider.Participants())
9086
for _, key := range priKeys {
9187
mask.SetKey(key.Pub.Bytes, true)
9288
}

Diff for: consensus/double_sign.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func (consensus *Consensus) checkDoubleSign(recvMsg *FBFTMessage) bool {
2222
); alreadyCastBallot != nil {
2323
for _, pubKey1 := range alreadyCastBallot.SignerPubKeys {
2424
if bytes.Compare(pubKey2.Bytes[:], pubKey1[:]) == 0 {
25-
for _, blk := range consensus.FBFTLog.GetBlocksByNumber(recvMsg.BlockNum) {
25+
for _, blk := range consensus.fBFTLog.GetBlocksByNumber(recvMsg.BlockNum) {
2626
firstSignedHeader := blk.Header()
2727
areHeightsEqual := firstSignedHeader.Number().Uint64() == recvMsg.BlockNum
2828
areViewIDsEqual := firstSignedHeader.ViewID().Uint64() == recvMsg.ViewID
@@ -138,8 +138,8 @@ func (consensus *Consensus) couldThisBeADoubleSigner(
138138
recvMsg *FBFTMessage,
139139
) bool {
140140
num, hash := consensus.BlockNum(), recvMsg.BlockHash
141-
suspicious := !consensus.FBFTLog.HasMatchingAnnounce(num, hash) ||
142-
!consensus.FBFTLog.HasMatchingPrepared(num, hash)
141+
suspicious := !consensus.fBFTLog.HasMatchingAnnounce(num, hash) ||
142+
!consensus.fBFTLog.HasMatchingPrepared(num, hash)
143143
if suspicious {
144144
consensus.getLogger().Debug().
145145
Str("message", recvMsg.String()).

Diff for: consensus/engine/consensus_engine.go

+21
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,27 @@ type ChainReader interface {
2323
// Config retrieves the blockchain's chain configuration.
2424
Config() *params.ChainConfig
2525

26+
// TrieNode retrieves a blob of data associated with a trie node
27+
// either from ephemeral in-memory cache, or from persistent storage.
28+
TrieNode(hash common.Hash) ([]byte, error)
29+
30+
// ContractCode retrieves a blob of data associated with a contract
31+
// hash either from ephemeral in-memory cache, or from persistent storage.
32+
//
33+
// If the code doesn't exist in the in-memory cache, check the storage with
34+
// new code scheme.
35+
ContractCode(hash common.Hash) ([]byte, error)
36+
37+
// ValidatorCode retrieves a blob of data associated with a validator
38+
// hash either from ephemeral in-memory cache, or from persistent storage.
39+
//
40+
// If the code doesn't exist in the in-memory cache, check the storage with
41+
// new code scheme.
42+
ValidatorCode(hash common.Hash) ([]byte, error)
43+
44+
// GetReceiptsByHash retrieves the receipts for all transactions in a given block.
45+
GetReceiptsByHash(hash common.Hash) types.Receipts
46+
2647
// CurrentHeader retrieves the current header from the local chain.
2748
CurrentHeader() *block.Header
2849

0 commit comments

Comments
 (0)