diff --git a/process/block/baseProcess.go b/process/block/baseProcess.go index 944441e7af..6438eeb94a 100644 --- a/process/block/baseProcess.go +++ b/process/block/baseProcess.go @@ -706,8 +706,11 @@ func (bp *baseProcessor) sortHeaderHashesForCurrentBlockByNonce(usedInBlock bool func (bp *baseProcessor) hasMissingProof(headerInfo *hdrInfo, hdrHash string) bool { isFlagEnabledForHeader := bp.enableEpochsHandler.IsFlagEnabledInEpoch(common.EquivalentMessagesFlag, headerInfo.hdr.GetEpoch()) - hasProof := bp.proofsPool.HasProof(headerInfo.hdr.GetShardID(), []byte(hdrHash)) - return isFlagEnabledForHeader && !hasProof + if !isFlagEnabledForHeader { + return false + } + + return !bp.proofsPool.HasProof(headerInfo.hdr.GetShardID(), []byte(hdrHash)) } func (bp *baseProcessor) createMiniBlockHeaderHandlers( @@ -2232,3 +2235,12 @@ func (bp *baseProcessor) addPrevProofIfNeeded(header data.HeaderHandler) error { header.SetPreviousProof(prevBlockProof) return nil } + +func (bp *baseProcessor) getHeaderHash(header data.HeaderHandler) ([]byte, error) { + marshalledHeader, errMarshal := bp.marshalizer.Marshal(header) + if errMarshal != nil { + return nil, errMarshal + } + + return bp.hasher.Compute(string(marshalledHeader)), nil +} diff --git a/process/block/export_test.go b/process/block/export_test.go index 56365b44a2..a1edd702f9 100644 --- a/process/block/export_test.go +++ b/process/block/export_test.go @@ -31,14 +31,17 @@ import ( storageStubs "github.com/multiversx/mx-chain-go/testscommon/storage" ) +// ComputeHeaderHash - func (bp *baseProcessor) ComputeHeaderHash(hdr data.HeaderHandler) ([]byte, error) { return core.CalculateHash(bp.marshalizer, bp.hasher, hdr) } +// VerifyStateRoot - func (bp *baseProcessor) VerifyStateRoot(rootHash []byte) bool { return bp.verifyStateRoot(rootHash) } +// CheckBlockValidity - func (bp *baseProcessor) CheckBlockValidity( headerHandler data.HeaderHandler, bodyHandler data.BodyHandler, @@ -46,6 +49,7 @@ func (bp *baseProcessor) CheckBlockValidity( return bp.checkBlockValidity(headerHandler, bodyHandler) } +// RemoveHeadersBehindNonceFromPools - func (bp *baseProcessor) RemoveHeadersBehindNonceFromPools( shouldRemoveBlockBody bool, shardId uint32, @@ -54,34 +58,42 @@ func (bp *baseProcessor) RemoveHeadersBehindNonceFromPools( bp.removeHeadersBehindNonceFromPools(shouldRemoveBlockBody, shardId, nonce) } +// GetPruningHandler - func (bp *baseProcessor) GetPruningHandler(finalHeaderNonce uint64) state.PruningHandler { return bp.getPruningHandler(finalHeaderNonce) } +// SetLastRestartNonce - func (bp *baseProcessor) SetLastRestartNonce(lastRestartNonce uint64) { bp.lastRestartNonce = lastRestartNonce } +// CommitTrieEpochRootHashIfNeeded - func (bp *baseProcessor) CommitTrieEpochRootHashIfNeeded(metaBlock *block.MetaBlock, rootHash []byte) error { return bp.commitTrieEpochRootHashIfNeeded(metaBlock, rootHash) } +// ReceivedMetaBlock - func (sp *shardProcessor) ReceivedMetaBlock(header data.HeaderHandler, metaBlockHash []byte) { sp.receivedMetaBlock(header, metaBlockHash) } +// CreateMiniBlocks - func (sp *shardProcessor) CreateMiniBlocks(haveTime func() bool) (*block.Body, map[string]*processedMb.ProcessedMiniBlockInfo, error) { return sp.createMiniBlocks(haveTime, []byte("random")) } +// GetOrderedProcessedMetaBlocksFromHeader - func (sp *shardProcessor) GetOrderedProcessedMetaBlocksFromHeader(header data.HeaderHandler) ([]data.HeaderHandler, error) { return sp.getOrderedProcessedMetaBlocksFromHeader(header) } +// UpdateCrossShardInfo - func (sp *shardProcessor) UpdateCrossShardInfo(processedMetaHdrs []data.HeaderHandler) error { return sp.updateCrossShardInfo(processedMetaHdrs) } +// UpdateStateStorage - func (sp *shardProcessor) UpdateStateStorage(finalHeaders []data.HeaderHandler, currentHeader data.HeaderHandler) { currShardHeader, ok := currentHeader.(data.ShardHeaderHandler) if !ok { @@ -90,6 +102,7 @@ func (sp *shardProcessor) UpdateStateStorage(finalHeaders []data.HeaderHandler, sp.updateState(finalHeaders, currShardHeader) } +// NewShardProcessorEmptyWith3shards - func NewShardProcessorEmptyWith3shards( tdp dataRetriever.PoolsHolder, genesisBlocks map[uint32]data.HeaderHandler, @@ -176,18 +189,22 @@ func NewShardProcessorEmptyWith3shards( return shardProc, err } +// RequestBlockHeaders - func (mp *metaProcessor) RequestBlockHeaders(header *block.MetaBlock) (uint32, uint32) { return mp.requestShardHeaders(header) } +// ReceivedShardHeader - func (mp *metaProcessor) ReceivedShardHeader(header data.HeaderHandler, shardHeaderHash []byte) { mp.receivedShardHeader(header, shardHeaderHash) } +// GetDataPool - func (mp *metaProcessor) GetDataPool() dataRetriever.PoolsHolder { return mp.dataPool } +// AddHdrHashToRequestedList - func (mp *metaProcessor) AddHdrHashToRequestedList(hdr data.HeaderHandler, hdrHash []byte) { mp.hdrsForCurrBlock.mutHdrsForBlock.Lock() defer mp.hdrsForCurrBlock.mutHdrsForBlock.Unlock() @@ -204,6 +221,7 @@ func (mp *metaProcessor) AddHdrHashToRequestedList(hdr data.HeaderHandler, hdrHa mp.hdrsForCurrBlock.missingHdrs++ } +// IsHdrMissing - func (mp *metaProcessor) IsHdrMissing(hdrHash []byte) bool { mp.hdrsForCurrBlock.mutHdrsForBlock.RLock() defer mp.hdrsForCurrBlock.mutHdrsForBlock.RUnlock() @@ -216,10 +234,12 @@ func (mp *metaProcessor) IsHdrMissing(hdrHash []byte) bool { return check.IfNil(hdrInfoValue.hdr) } +// CreateShardInfo - func (mp *metaProcessor) CreateShardInfo() ([]data.ShardDataHandler, error) { return mp.createShardInfo() } +// RequestMissingFinalityAttestingShardHeaders - func (mp *metaProcessor) RequestMissingFinalityAttestingShardHeaders() uint32 { mp.hdrsForCurrBlock.mutHdrsForBlock.Lock() defer mp.hdrsForCurrBlock.mutHdrsForBlock.Unlock() @@ -227,10 +247,12 @@ func (mp *metaProcessor) RequestMissingFinalityAttestingShardHeaders() uint32 { return mp.requestMissingFinalityAttestingShardHeaders() } +// SaveMetricCrossCheckBlockHeight - func (mp *metaProcessor) SaveMetricCrossCheckBlockHeight() { mp.saveMetricCrossCheckBlockHeight() } +// NotarizedHdrs - func (bp *baseProcessor) NotarizedHdrs() map[uint32][]data.HeaderHandler { lastCrossNotarizedHeaders := make(map[uint32][]data.HeaderHandler) for shardID := uint32(0); shardID < bp.shardCoordinator.NumberOfShards(); shardID++ { @@ -248,6 +270,7 @@ func (bp *baseProcessor) NotarizedHdrs() map[uint32][]data.HeaderHandler { return lastCrossNotarizedHeaders } +// LastNotarizedHdrForShard - func (bp *baseProcessor) LastNotarizedHdrForShard(shardID uint32) data.HeaderHandler { lastCrossNotarizedHeaderForShard, _, _ := bp.blockTracker.GetLastCrossNotarizedHeader(shardID) if check.IfNil(lastCrossNotarizedHeaderForShard) { @@ -257,76 +280,94 @@ func (bp *baseProcessor) LastNotarizedHdrForShard(shardID uint32) data.HeaderHan return lastCrossNotarizedHeaderForShard } +// SetMarshalizer - func (bp *baseProcessor) SetMarshalizer(marshal marshal.Marshalizer) { bp.marshalizer = marshal } +// SetHasher - func (bp *baseProcessor) SetHasher(hasher hashing.Hasher) { bp.hasher = hasher } +// SetHeaderValidator - func (bp *baseProcessor) SetHeaderValidator(validator process.HeaderConstructionValidator) { bp.headerValidator = validator } +// RequestHeadersIfMissing - func (bp *baseProcessor) RequestHeadersIfMissing(sortedHdrs []data.HeaderHandler, shardId uint32) error { return bp.requestHeadersIfMissing(sortedHdrs, shardId) } +// SetShardBlockFinality - func (mp *metaProcessor) SetShardBlockFinality(val uint32) { mp.hdrsForCurrBlock.mutHdrsForBlock.Lock() mp.shardBlockFinality = val mp.hdrsForCurrBlock.mutHdrsForBlock.Unlock() } +// SaveLastNotarizedHeader - func (mp *metaProcessor) SaveLastNotarizedHeader(header *block.MetaBlock) error { return mp.saveLastNotarizedHeader(header) } +// CheckShardHeadersValidity - func (mp *metaProcessor) CheckShardHeadersValidity(header *block.MetaBlock) (map[uint32]data.HeaderHandler, error) { return mp.checkShardHeadersValidity(header) } +// CheckShardHeadersFinality - func (mp *metaProcessor) CheckShardHeadersFinality(highestNonceHdrs map[uint32]data.HeaderHandler) error { return mp.checkShardHeadersFinality(highestNonceHdrs) } +// CheckHeaderBodyCorrelation - func (mp *metaProcessor) CheckHeaderBodyCorrelation(hdr data.HeaderHandler, body *block.Body) error { return mp.checkHeaderBodyCorrelation(hdr.GetMiniBlockHeaderHandlers(), body) } +// IsHdrConstructionValid - func (bp *baseProcessor) IsHdrConstructionValid(currHdr, prevHdr data.HeaderHandler) error { return bp.headerValidator.IsHeaderConstructionValid(currHdr, prevHdr) } +// ChRcvAllHdrs - func (mp *metaProcessor) ChRcvAllHdrs() chan bool { return mp.chRcvAllHdrs } +// UpdateShardsHeadersNonce - func (mp *metaProcessor) UpdateShardsHeadersNonce(key uint32, value uint64) { mp.updateShardHeadersNonce(key, value) } +// GetShardsHeadersNonce - func (mp *metaProcessor) GetShardsHeadersNonce() *sync.Map { return mp.shardsHeadersNonce } +// SaveLastNotarizedHeader - func (sp *shardProcessor) SaveLastNotarizedHeader(shardId uint32, processedHdrs []data.HeaderHandler) error { return sp.saveLastNotarizedHeader(shardId, processedHdrs) } +// CheckHeaderBodyCorrelation - func (sp *shardProcessor) CheckHeaderBodyCorrelation(hdr data.HeaderHandler, body *block.Body) error { return sp.checkHeaderBodyCorrelation(hdr.GetMiniBlockHeaderHandlers(), body) } +// CheckAndRequestIfMetaHeadersMissing - func (sp *shardProcessor) CheckAndRequestIfMetaHeadersMissing() { sp.checkAndRequestIfMetaHeadersMissing() } +// GetHashAndHdrStruct - func (sp *shardProcessor) GetHashAndHdrStruct(header data.HeaderHandler, hash []byte) *hashAndHdr { return &hashAndHdr{header, hash} } +// RequestMissingFinalityAttestingHeaders - func (sp *shardProcessor) RequestMissingFinalityAttestingHeaders() uint32 { sp.hdrsForCurrBlock.mutHdrsForBlock.Lock() defer sp.hdrsForCurrBlock.mutHdrsForBlock.Unlock() @@ -337,10 +378,12 @@ func (sp *shardProcessor) RequestMissingFinalityAttestingHeaders() uint32 { ) } +// CheckMetaHeadersValidityAndFinality - func (sp *shardProcessor) CheckMetaHeadersValidityAndFinality() error { return sp.checkMetaHeadersValidityAndFinality() } +// CreateAndProcessMiniBlocksDstMe - func (sp *shardProcessor) CreateAndProcessMiniBlocksDstMe( haveTime func() bool, ) (block.MiniBlockSlice, uint32, uint32, error) { @@ -348,6 +391,7 @@ func (sp *shardProcessor) CreateAndProcessMiniBlocksDstMe( return createAndProcessInfo.miniBlocks, createAndProcessInfo.numHdrsAdded, createAndProcessInfo.numTxsAdded, err } +// DisplayLogInfo - func (sp *shardProcessor) DisplayLogInfo( header data.HeaderHandler, body *block.Body, @@ -360,10 +404,12 @@ func (sp *shardProcessor) DisplayLogInfo( sp.txCounter.displayLogInfo(header, body, headerHash, numShards, selfId, dataPool, blockTracker) } +// GetHighestHdrForOwnShardFromMetachain - func (sp *shardProcessor) GetHighestHdrForOwnShardFromMetachain(processedHdrs []data.HeaderHandler) ([]data.HeaderHandler, [][]byte, error) { return sp.getHighestHdrForOwnShardFromMetachain(processedHdrs) } +// RestoreMetaBlockIntoPool - func (sp *shardProcessor) RestoreMetaBlockIntoPool( miniBlockHashes map[string]uint32, metaBlockHashes [][]byte, @@ -372,60 +418,94 @@ func (sp *shardProcessor) RestoreMetaBlockIntoPool( return sp.restoreMetaBlockIntoPool(headerHandler, miniBlockHashes, metaBlockHashes) } +// GetAllMiniBlockDstMeFromMeta - func (sp *shardProcessor) GetAllMiniBlockDstMeFromMeta( header data.ShardHeaderHandler, ) (map[string][]byte, error) { return sp.getAllMiniBlockDstMeFromMeta(header) } +// SetHdrForCurrentBlock - func (bp *baseProcessor) SetHdrForCurrentBlock(headerHash []byte, headerHandler data.HeaderHandler, usedInBlock bool) { bp.hdrsForCurrBlock.mutHdrsForBlock.Lock() bp.hdrsForCurrBlock.hdrHashAndInfo[string(headerHash)] = &hdrInfo{hdr: headerHandler, usedInBlock: usedInBlock} bp.hdrsForCurrBlock.mutHdrsForBlock.Unlock() } +// SetHighestHdrNonceForCurrentBlock - func (bp *baseProcessor) SetHighestHdrNonceForCurrentBlock(shardId uint32, value uint64) { bp.hdrsForCurrBlock.mutHdrsForBlock.Lock() bp.hdrsForCurrBlock.highestHdrNonce[shardId] = value bp.hdrsForCurrBlock.mutHdrsForBlock.Unlock() } +// LastNotarizedHeaderInfo - +type LastNotarizedHeaderInfo struct { + Header data.HeaderHandler + Hash []byte + NotarizedBasedOnProof bool + HasProof bool +} + +// SetLastNotarizedHeaderForShard - +func (bp *baseProcessor) SetLastNotarizedHeaderForShard(shardId uint32, info *LastNotarizedHeaderInfo) { + bp.hdrsForCurrBlock.mutHdrsForBlock.Lock() + lastNotarizedShardInfo := &lastNotarizedHeaderInfo{ + header: info.Header, + hash: info.Hash, + notarizedBasedOnProof: info.NotarizedBasedOnProof, + hasProof: info.HasProof, + } + bp.hdrsForCurrBlock.lastNotarizedShardHeaders[shardId] = lastNotarizedShardInfo + bp.hdrsForCurrBlock.mutHdrsForBlock.Unlock() +} + +// CreateBlockStarted - func (bp *baseProcessor) CreateBlockStarted() error { return bp.createBlockStarted() } +// AddProcessedCrossMiniBlocksFromHeader - func (sp *shardProcessor) AddProcessedCrossMiniBlocksFromHeader(header data.HeaderHandler) error { return sp.addProcessedCrossMiniBlocksFromHeader(header) } +// VerifyCrossShardMiniBlockDstMe - func (mp *metaProcessor) VerifyCrossShardMiniBlockDstMe(header *block.MetaBlock) error { return mp.verifyCrossShardMiniBlockDstMe(header) } +// ApplyBodyToHeader - func (mp *metaProcessor) ApplyBodyToHeader(metaHdr data.MetaHeaderHandler, body *block.Body) (data.BodyHandler, error) { return mp.applyBodyToHeader(metaHdr, body) } +// ApplyBodyToHeader - func (sp *shardProcessor) ApplyBodyToHeader(shardHdr data.ShardHeaderHandler, body *block.Body, processedMiniBlocksDestMeInfo map[string]*processedMb.ProcessedMiniBlockInfo) (*block.Body, error) { return sp.applyBodyToHeader(shardHdr, body, processedMiniBlocksDestMeInfo) } +// CreateBlockBody - func (mp *metaProcessor) CreateBlockBody(metaBlock data.HeaderHandler, haveTime func() bool) (data.BodyHandler, error) { return mp.createBlockBody(metaBlock, haveTime) } +// CreateBlockBody - func (sp *shardProcessor) CreateBlockBody(shardHdr data.HeaderHandler, haveTime func() bool) (data.BodyHandler, map[string]*processedMb.ProcessedMiniBlockInfo, error) { return sp.createBlockBody(shardHdr, haveTime) } +// CheckEpochCorrectnessCrossChain - func (sp *shardProcessor) CheckEpochCorrectnessCrossChain() error { return sp.checkEpochCorrectnessCrossChain() } +// CheckEpochCorrectness - func (sp *shardProcessor) CheckEpochCorrectness(header *block.Header) error { return sp.checkEpochCorrectness(header) } +// GetBootstrapHeadersInfo - func (sp *shardProcessor) GetBootstrapHeadersInfo( selfNotarizedHeaders []data.HeaderHandler, selfNotarizedHeadersHashes [][]byte, @@ -433,18 +513,22 @@ func (sp *shardProcessor) GetBootstrapHeadersInfo( return sp.getBootstrapHeadersInfo(selfNotarizedHeaders, selfNotarizedHeadersHashes) } +// RequestMetaHeadersIfNeeded - func (sp *shardProcessor) RequestMetaHeadersIfNeeded(hdrsAdded uint32, lastMetaHdr data.HeaderHandler) { sp.requestMetaHeadersIfNeeded(hdrsAdded, lastMetaHdr) } +// RequestShardHeadersIfNeeded - func (mp *metaProcessor) RequestShardHeadersIfNeeded(hdrsAddedForShard map[uint32]uint32, lastShardHdr map[uint32]data.HeaderHandler) { mp.requestShardHeadersIfNeeded(hdrsAddedForShard, lastShardHdr) } +// AddHeaderIntoTrackerPool - func (bp *baseProcessor) AddHeaderIntoTrackerPool(nonce uint64, shardID uint32) { bp.addHeaderIntoTrackerPool(nonce, shardID) } +// UpdateState - func (bp *baseProcessor) UpdateState( finalHeader data.HeaderHandler, rootHash []byte, @@ -454,14 +538,17 @@ func (bp *baseProcessor) UpdateState( bp.updateStateStorage(finalHeader, rootHash, prevRootHash, accounts) } +// GasAndFeesDelta - func GasAndFeesDelta(initialGasAndFees, finalGasAndFees scheduled.GasAndFees) scheduled.GasAndFees { return gasAndFeesDelta(initialGasAndFees, finalGasAndFees) } +// RequestEpochStartInfo - func (sp *shardProcessor) RequestEpochStartInfo(header data.ShardHeaderHandler, haveTime func() time.Duration) error { return sp.requestEpochStartInfo(header, haveTime) } +// ProcessEpochStartMetaBlock - func (mp *metaProcessor) ProcessEpochStartMetaBlock( header *block.MetaBlock, body *block.Body, @@ -469,30 +556,37 @@ func (mp *metaProcessor) ProcessEpochStartMetaBlock( return mp.processEpochStartMetaBlock(header, body) } +// UpdateEpochStartHeader - func (mp *metaProcessor) UpdateEpochStartHeader(metaHdr *block.MetaBlock) error { return mp.updateEpochStartHeader(metaHdr) } +// CreateEpochStartBody - func (mp *metaProcessor) CreateEpochStartBody(metaBlock *block.MetaBlock) (data.BodyHandler, error) { return mp.createEpochStartBody(metaBlock) } +// GetIndexOfFirstMiniBlockToBeExecuted - func (bp *baseProcessor) GetIndexOfFirstMiniBlockToBeExecuted(header data.HeaderHandler) int { return bp.getIndexOfFirstMiniBlockToBeExecuted(header) } +// GetFinalMiniBlocks - func (bp *baseProcessor) GetFinalMiniBlocks(header data.HeaderHandler, body *block.Body) (*block.Body, error) { return bp.getFinalMiniBlocks(header, body) } +// GetScheduledMiniBlocksFromMe - func GetScheduledMiniBlocksFromMe(headerHandler data.HeaderHandler, bodyHandler data.BodyHandler) (block.MiniBlockSlice, error) { return getScheduledMiniBlocksFromMe(headerHandler, bodyHandler) } +// CheckScheduledMiniBlocksValidity - func (bp *baseProcessor) CheckScheduledMiniBlocksValidity(headerHandler data.HeaderHandler) error { return bp.checkScheduledMiniBlocksValidity(headerHandler) } +// SetMiniBlockHeaderReservedField - func (bp *baseProcessor) SetMiniBlockHeaderReservedField( miniBlock *block.MiniBlock, miniBlockHeaderHandler data.MiniBlockHeaderHandler, @@ -501,18 +595,22 @@ func (bp *baseProcessor) SetMiniBlockHeaderReservedField( return bp.setMiniBlockHeaderReservedField(miniBlock, miniBlockHeaderHandler, processedMiniBlocksDestMeInfo) } +// GetFinalMiniBlockHeaders - func (mp *metaProcessor) GetFinalMiniBlockHeaders(miniBlockHeaderHandlers []data.MiniBlockHeaderHandler) []data.MiniBlockHeaderHandler { return mp.getFinalMiniBlockHeaders(miniBlockHeaderHandlers) } +// CheckProcessorNilParameters - func CheckProcessorNilParameters(arguments ArgBaseProcessor) error { return checkProcessorParameters(arguments) } +// SetIndexOfFirstTxProcessed - func (bp *baseProcessor) SetIndexOfFirstTxProcessed(miniBlockHeaderHandler data.MiniBlockHeaderHandler) error { return bp.setIndexOfFirstTxProcessed(miniBlockHeaderHandler) } +// SetIndexOfLastTxProcessed - func (bp *baseProcessor) SetIndexOfLastTxProcessed( miniBlockHeaderHandler data.MiniBlockHeaderHandler, processedMiniBlocksDestMeInfo map[string]*processedMb.ProcessedMiniBlockInfo, @@ -520,10 +618,12 @@ func (bp *baseProcessor) SetIndexOfLastTxProcessed( return bp.setIndexOfLastTxProcessed(miniBlockHeaderHandler, processedMiniBlocksDestMeInfo) } +// GetProcessedMiniBlocksTracker - func (bp *baseProcessor) GetProcessedMiniBlocksTracker() process.ProcessedMiniBlocksTracker { return bp.processedMiniBlocksTracker } +// SetProcessingTypeAndConstructionStateForScheduledMb - func (bp *baseProcessor) SetProcessingTypeAndConstructionStateForScheduledMb( miniBlockHeaderHandler data.MiniBlockHeaderHandler, processedMiniBlocksDestMeInfo map[string]*processedMb.ProcessedMiniBlockInfo, @@ -531,6 +631,7 @@ func (bp *baseProcessor) SetProcessingTypeAndConstructionStateForScheduledMb( return bp.setProcessingTypeAndConstructionStateForScheduledMb(miniBlockHeaderHandler, processedMiniBlocksDestMeInfo) } +// SetProcessingTypeAndConstructionStateForNormalMb - func (bp *baseProcessor) SetProcessingTypeAndConstructionStateForNormalMb( miniBlockHeaderHandler data.MiniBlockHeaderHandler, processedMiniBlocksDestMeInfo map[string]*processedMb.ProcessedMiniBlockInfo, @@ -538,26 +639,32 @@ func (bp *baseProcessor) SetProcessingTypeAndConstructionStateForNormalMb( return bp.setProcessingTypeAndConstructionStateForNormalMb(miniBlockHeaderHandler, processedMiniBlocksDestMeInfo) } +// RollBackProcessedMiniBlockInfo - func (sp *shardProcessor) RollBackProcessedMiniBlockInfo(miniBlockHeader data.MiniBlockHeaderHandler, miniBlockHash []byte) { sp.rollBackProcessedMiniBlockInfo(miniBlockHeader, miniBlockHash) } +// SetProcessedMiniBlocksInfo - func (sp *shardProcessor) SetProcessedMiniBlocksInfo(miniBlockHashes [][]byte, metaBlockHash string, metaBlock *block.MetaBlock) { sp.setProcessedMiniBlocksInfo(miniBlockHashes, metaBlockHash, metaBlock) } +// GetIndexOfLastTxProcessedInMiniBlock - func (sp *shardProcessor) GetIndexOfLastTxProcessedInMiniBlock(miniBlockHash []byte, metaBlock *block.MetaBlock) int32 { return getIndexOfLastTxProcessedInMiniBlock(miniBlockHash, metaBlock) } +// RollBackProcessedMiniBlocksInfo - func (sp *shardProcessor) RollBackProcessedMiniBlocksInfo(headerHandler data.HeaderHandler, mapMiniBlockHashes map[string]uint32) { sp.rollBackProcessedMiniBlocksInfo(headerHandler, mapMiniBlockHashes) } +// CheckConstructionStateAndIndexesCorrectness - func (bp *baseProcessor) CheckConstructionStateAndIndexesCorrectness(mbh data.MiniBlockHeaderHandler) error { return checkConstructionStateAndIndexesCorrectness(mbh) } +// GetAllMarshalledTxs - func (mp *metaProcessor) GetAllMarshalledTxs(body *block.Body) map[string][][]byte { return mp.getAllMarshalledTxs(body) } diff --git a/process/block/hdrForBlock.go b/process/block/hdrForBlock.go index fd7384aedc..74463b4294 100644 --- a/process/block/hdrForBlock.go +++ b/process/block/hdrForBlock.go @@ -6,18 +6,27 @@ import ( "github.com/multiversx/mx-chain-core-go/data" ) +type lastNotarizedHeaderInfo struct { + header data.HeaderHandler + hash []byte + notarizedBasedOnProof bool + hasProof bool +} + type hdrForBlock struct { missingHdrs uint32 missingFinalityAttestingHdrs uint32 highestHdrNonce map[uint32]uint64 mutHdrsForBlock sync.RWMutex hdrHashAndInfo map[string]*hdrInfo + lastNotarizedShardHeaders map[uint32]*lastNotarizedHeaderInfo } func newHdrForBlock() *hdrForBlock { return &hdrForBlock{ - hdrHashAndInfo: make(map[string]*hdrInfo), - highestHdrNonce: make(map[uint32]uint64), + hdrHashAndInfo: make(map[string]*hdrInfo), + highestHdrNonce: make(map[uint32]uint64), + lastNotarizedShardHeaders: make(map[uint32]*lastNotarizedHeaderInfo), } } @@ -25,6 +34,7 @@ func (hfb *hdrForBlock) initMaps() { hfb.mutHdrsForBlock.Lock() hfb.hdrHashAndInfo = make(map[string]*hdrInfo) hfb.highestHdrNonce = make(map[uint32]uint64) + hfb.lastNotarizedShardHeaders = make(map[uint32]*lastNotarizedHeaderInfo) hfb.mutHdrsForBlock.Unlock() } diff --git a/process/block/interceptedBlocks/interceptedEquivalentProof.go b/process/block/interceptedBlocks/interceptedEquivalentProof.go index e8186e6379..5f39e0e03f 100644 --- a/process/block/interceptedBlocks/interceptedEquivalentProof.go +++ b/process/block/interceptedBlocks/interceptedEquivalentProof.go @@ -131,6 +131,7 @@ func extractIsForCurrentShard(shardCoordinator sharding.Coordinator, equivalentP // CheckValidity checks if the received proof is valid func (iep *interceptedEquivalentProof) CheckValidity() error { + log.Debug("Checking intercepted equivalent proof validity", "proof header hash", iep.proof.HeaderHash) err := iep.integrity() if err != nil { return err diff --git a/process/block/metablock.go b/process/block/metablock.go index a7ebc79abb..868713789f 100644 --- a/process/block/metablock.go +++ b/process/block/metablock.go @@ -441,6 +441,10 @@ func (mp *metaProcessor) checkProofsForShardData(header *block.MetaBlock) error return fmt.Errorf("%w for header hash %s", process.ErrMissingHeaderProof, hex.EncodeToString(shardData.HeaderHash)) } + if !common.ShouldBlockHavePrevProof(shardHeader.hdr, mp.enableEpochsHandler, common.EquivalentMessagesFlag) { + continue + } + shardHeadersStorer, err := mp.store.GetStorer(dataRetriever.BlockHeaderUnit) if err != nil { return err @@ -1900,13 +1904,16 @@ func (mp *metaProcessor) checkShardHeadersValidity(metaHdr *block.MetaBlock) (ma } if mp.enableEpochsHandler.IsFlagEnabledInEpoch(common.EquivalentMessagesFlag, shardHdr.GetEpoch()) { if shardData.Epoch != shardHdr.GetEpoch() { - return nil, process.ErrEpochMissmatch + return nil, process.ErrEpochMismatch } } - err = verifyProof(shardData.GetPreviousProof()) - if err != nil { - return nil, err + if common.ShouldBlockHavePrevProof(shardHdr, mp.enableEpochsHandler, common.EquivalentMessagesFlag) { + // TODO: proper check of the fields + err = verifyProof(shardData.GetPreviousProof()) + if err != nil { + return nil, err + } } mapMiniBlockHeadersInMetaBlock := make(map[string]struct{}) @@ -1983,22 +1990,18 @@ func (mp *metaProcessor) checkShardHeadersFinality( continue } + isNotarizedBasedOnProofs, errCheckProof := mp.checkShardHeaderFinalityBasedOnProofs(lastVerifiedHdr, shardId) + if isNotarizedBasedOnProofs { + if errCheckProof != nil { + return errCheckProof + } + + continue + } + // verify if there are "K" block after current to make this one final nextBlocksVerified := uint32(0) - isNotarizedBasedOnProofs := false for _, shardHdr := range finalityAttestingShardHdrs[shardId] { - var errCheckProof error - isNotarizedBasedOnProofs, errCheckProof = mp.checkShardHeaderFinalityBasedOnProofs(shardHdr, shardId) - // if the header is notarized based on proofs and there is no error, break the loop - // if there is any error, forward it, header is not final - if isNotarizedBasedOnProofs { - if errCheckProof != nil { - return err - } - - break - } - if nextBlocksVerified >= mp.shardBlockFinality { break } @@ -2012,6 +2015,15 @@ func (mp *metaProcessor) checkShardHeadersFinality( continue } + isNotarizedBasedOnProofs, errCheckProof = mp.checkShardHeaderFinalityBasedOnProofs(shardHdr, shardId) + if isNotarizedBasedOnProofs { + if errCheckProof != nil { + return errCheckProof + } + + break + } + lastVerifiedHdr = shardHdr nextBlocksVerified += 1 } @@ -2074,10 +2086,10 @@ func (mp *metaProcessor) receivedShardHeader(headerHandler data.HeaderHandler, s if shardHeader.GetNonce() > mp.hdrsForCurrBlock.highestHdrNonce[shardHeader.GetShardID()] { mp.hdrsForCurrBlock.highestHdrNonce[shardHeader.GetShardID()] = shardHeader.GetNonce() } + mp.updateLastNotarizedBlockForShard(shardHeader, shardHeaderHash) } - shouldConsiderProofsForNotarization := mp.enableEpochsHandler.IsFlagEnabledInEpoch(common.EquivalentMessagesFlag, shardHeader.GetEpoch()) - if mp.hdrsForCurrBlock.missingHdrs == 0 && !shouldConsiderProofsForNotarization { + if mp.hdrsForCurrBlock.missingHdrs == 0 { mp.hdrsForCurrBlock.missingFinalityAttestingHdrs = mp.requestMissingFinalityAttestingShardHeaders() if mp.hdrsForCurrBlock.missingFinalityAttestingHdrs == 0 { log.Debug("received all missing finality attesting shard headers") @@ -2101,17 +2113,20 @@ func (mp *metaProcessor) receivedShardHeader(headerHandler data.HeaderHandler, s // requestMissingFinalityAttestingShardHeaders requests the headers needed to accept the current selected headers for // processing the current block. It requests the shardBlockFinality headers greater than the highest shard header, -// for each shard, related to the block which should be processed +// for the given shard, related to the block which should be processed // this method should be called only under the mutex protection: hdrsForCurrBlock.mutHdrsForBlock func (mp *metaProcessor) requestMissingFinalityAttestingShardHeaders() uint32 { missingFinalityAttestingShardHeaders := uint32(0) for shardId := uint32(0); shardId < mp.shardCoordinator.NumberOfShards(); shardId++ { - missingFinalityAttestingHeaders := mp.requestMissingFinalityAttestingHeaders( - shardId, - mp.shardBlockFinality, - ) - + lastNotarizedShardHeader := mp.hdrsForCurrBlock.lastNotarizedShardHeaders[shardId] + missingFinalityAttestingHeaders := uint32(0) + if lastNotarizedShardHeader != nil && !lastNotarizedShardHeader.notarizedBasedOnProof { + missingFinalityAttestingHeaders = mp.requestMissingFinalityAttestingHeaders( + shardId, + mp.shardBlockFinality, + ) + } missingFinalityAttestingShardHeaders += missingFinalityAttestingHeaders } @@ -2128,11 +2143,31 @@ func (mp *metaProcessor) requestShardHeaders(metaBlock *block.MetaBlock) (uint32 return mp.computeExistingAndRequestMissingShardHeaders(metaBlock) } +func (mp *metaProcessor) updateLastNotarizedBlockForShard(hdr data.ShardHeaderHandler, headerHash []byte) { + lastNotarizedForShard := mp.hdrsForCurrBlock.lastNotarizedShardHeaders[hdr.GetShardID()] + if lastNotarizedForShard == nil { + lastNotarizedForShard = &lastNotarizedHeaderInfo{header: hdr} + mp.hdrsForCurrBlock.lastNotarizedShardHeaders[hdr.GetShardID()] = lastNotarizedForShard + } + + if hdr.GetNonce() >= lastNotarizedForShard.header.GetNonce() { + notarizedBasedOnProofs := mp.enableEpochsHandler.IsFlagEnabledInEpoch(common.EquivalentMessagesFlag, hdr.GetEpoch()) + hasProof := false + if notarizedBasedOnProofs { + hasProof = mp.proofsPool.HasProof(hdr.GetShardID(), headerHash) + } + + lastNotarizedForShard.header = hdr + lastNotarizedForShard.hash = headerHash + lastNotarizedForShard.notarizedBasedOnProof = notarizedBasedOnProofs + lastNotarizedForShard.hasProof = hasProof + } +} + func (mp *metaProcessor) computeExistingAndRequestMissingShardHeaders(metaBlock *block.MetaBlock) (uint32, uint32) { mp.hdrsForCurrBlock.mutHdrsForBlock.Lock() defer mp.hdrsForCurrBlock.mutHdrsForBlock.Unlock() - notarizedShardHdrsBasedOnProofs := 0 for _, shardData := range metaBlock.ShardInfo { if shardData.Nonce == mp.genesisNonce { lastCrossNotarizedHeaderForShard, hash, err := mp.blockTracker.GetLastCrossNotarizedHeader(shardData.ShardID) @@ -2141,7 +2176,7 @@ func (mp *metaProcessor) computeExistingAndRequestMissingShardHeaders(metaBlock continue } if !bytes.Equal(hash, shardData.HeaderHash) { - log.Warn("genesis hash missmatch", + log.Warn("genesis hash mismatch", "last notarized nonce", lastCrossNotarizedHeaderForShard.GetNonce(), "last notarized hash", hash, "genesis nonce", mp.genesisNonce, @@ -2174,13 +2209,10 @@ func (mp *metaProcessor) computeExistingAndRequestMissingShardHeaders(metaBlock mp.hdrsForCurrBlock.highestHdrNonce[shardData.ShardID] = hdr.GetNonce() } - if mp.enableEpochsHandler.IsFlagEnabledInEpoch(common.EquivalentMessagesFlag, hdr.GetEpoch()) { - notarizedShardHdrsBasedOnProofs++ - } + mp.updateLastNotarizedBlockForShard(hdr, shardData.HeaderHash) } - shouldRequestMissingFinalityAttestingShardHeaders := notarizedShardHdrsBasedOnProofs != len(metaBlock.ShardInfo) - if mp.hdrsForCurrBlock.missingHdrs == 0 && shouldRequestMissingFinalityAttestingShardHeaders { + if mp.hdrsForCurrBlock.missingHdrs == 0 { mp.hdrsForCurrBlock.missingFinalityAttestingHdrs = mp.requestMissingFinalityAttestingShardHeaders() } diff --git a/process/block/metablock_test.go b/process/block/metablock_test.go index d26f5074ea..ec196bbe0b 100644 --- a/process/block/metablock_test.go +++ b/process/block/metablock_test.go @@ -820,10 +820,28 @@ func TestMetaProcessor_RequestFinalMissingHeaderShouldPass(t *testing.T) { mp, _ := blproc.NewMetaProcessor(arguments) mp.AddHdrHashToRequestedList(&block.Header{}, []byte("header_hash")) mp.SetHighestHdrNonceForCurrentBlock(0, 1) + mp.SetLastNotarizedHeaderForShard(0, &blproc.LastNotarizedHeaderInfo{ + Header: &block.Header{Nonce: 0, ShardID: 0}, + Hash: []byte("header hash"), + NotarizedBasedOnProof: false, + HasProof: false, + }) mp.SetHighestHdrNonceForCurrentBlock(1, 2) + mp.SetLastNotarizedHeaderForShard(1, &blproc.LastNotarizedHeaderInfo{ + Header: &block.Header{Nonce: 2, ShardID: 1}, + Hash: []byte("header hash"), + NotarizedBasedOnProof: false, + HasProof: false, + }) mp.SetHighestHdrNonceForCurrentBlock(2, 3) + mp.SetLastNotarizedHeaderForShard(2, &blproc.LastNotarizedHeaderInfo{ + Header: &block.Header{Nonce: 3, ShardID: 2}, + Hash: []byte("header hash"), + NotarizedBasedOnProof: false, + HasProof: false, + }) res := mp.RequestMissingFinalityAttestingShardHeaders() - assert.Equal(t, res, uint32(3)) + assert.Equal(t, uint32(3), res) } // ------- CommitBlock diff --git a/process/block/shardblock.go b/process/block/shardblock.go index e3539ac463..830a4f46d3 100644 --- a/process/block/shardblock.go +++ b/process/block/shardblock.go @@ -640,15 +640,6 @@ func (sp *shardProcessor) checkMetaHdrFinality(header data.HeaderHandler) error return nil } -func (sp *shardProcessor) getHeaderHash(header data.HeaderHandler) ([]byte, error) { - marshalledHeader, errMarshal := sp.marshalizer.Marshal(header) - if errMarshal != nil { - return nil, errMarshal - } - - return sp.hasher.Compute(string(marshalledHeader)), nil -} - func (sp *shardProcessor) checkAndRequestIfMetaHeadersMissing() { orderedMetaBlocks, _ := sp.blockTracker.GetTrackedHeaders(core.MetachainShardId) diff --git a/process/errors.go b/process/errors.go index 728ec6348c..ded117ecb7 100644 --- a/process/errors.go +++ b/process/errors.go @@ -1275,5 +1275,5 @@ var ErrInvalidHeaderProof = errors.New("invalid equivalent proof") // ErrUnexpectedHeaderProof signals that a header proof has been provided unexpectedly var ErrUnexpectedHeaderProof = errors.New("unexpected header proof") -// ErrEpochMissmatch signals that the epoch do not match -var ErrEpochMissmatch = errors.New("epoch missmatch") +// ErrEpochMismatch signals that the epoch do not match +var ErrEpochMismatch = errors.New("epoch mismatch") diff --git a/process/track/baseBlockTrack.go b/process/track/baseBlockTrack.go index ecea5c4e79..bd3a288ca3 100644 --- a/process/track/baseBlockTrack.go +++ b/process/track/baseBlockTrack.go @@ -136,6 +136,13 @@ func (bbt *baseBlockTrack) receivedProof(proof data.HeaderProofHandler) { log.Debug("baseBlockTrack.receivedProof with missing header", "headerHash", headerHash) return } + log.Debug("received proof from network in block tracker", + "shard", proof.GetHeaderShardId(), + "epoch", proof.GetHeaderEpoch(), + "round", proof.GetHeaderRound(), + "nonce", proof.GetHeaderNonce(), + "hash", proof.GetHeaderHash(), + ) bbt.receivedHeader(header, headerHash) } diff --git a/process/track/blockProcessor.go b/process/track/blockProcessor.go index f006f4db4e..5ce30a0988 100644 --- a/process/track/blockProcessor.go +++ b/process/track/blockProcessor.go @@ -316,7 +316,11 @@ func (bp *blockProcessor) checkHeaderFinality( if bp.enableEpochsHandler.IsFlagEnabledInEpoch(common.EquivalentMessagesFlag, header.GetEpoch()) { // the index in argument is for the next block after header - if bp.proofsPool.HasProof(header.GetShardID(), sortedHeadersHashes[index-1]) { + hashIndex := index + if index > 0 { + hashIndex = index - 1 + } + if bp.proofsPool.HasProof(header.GetShardID(), sortedHeadersHashes[hashIndex]) { return nil } @@ -339,7 +343,7 @@ func (bp *blockProcessor) checkHeaderFinality( // if the currentHeader(the one that should confirm the finality of the prev) // is the epoch start block of equivalent messages, we must check for its proof as well if bp.enableEpochsHandler.IsFlagEnabledInEpoch(common.EquivalentMessagesFlag, currHeader.GetEpoch()) { - if bp.proofsPool.HasProof(currHeader.GetShardID(), sortedHeadersHashes[index]) { + if bp.proofsPool.HasProof(currHeader.GetShardID(), sortedHeadersHashes[i]) { return nil }