-
Notifications
You must be signed in to change notification settings - Fork 3
709 Smithing All Blocksmith Skipped #725
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
c6147f1
#709 add smithing pending blocker
andy-shi88 bbf9b8a
#709 remove smith time from blcoksmith model
andy-shi88 93631a8
#709 update smithing process: recycle blocksmith when all blocksmith …
andy-shi88 f73935b
#709 separate smith time constants between spinechain and mainchain
andy-shi88 e72b80b
#709 inject spinechain blocksmith service
andy-shi88 8c1818c
#709 update metric smithtime to smith index
andy-shi88 c15ada0
#709 fix unit tests
andy-shi88 c63502e
#709 update function signature for ease of test
andy-shi88 dfb4cc0
#709 cover new smithing process unit tests
andy-shi88 381d91e
Merge branch 'develop' into 709-all-blocksmith-skipped
andy-shi88 8b1d1f0
#709 use constant'
andy-shi88 84e3495
Merge branch '709-all-blocksmith-skipped' of github.com:andy-shi88/zo…
andy-shi88 10d78bc
#709 update nil checking of map
andy-shi88 a6a3cab
#709 fix conflict
andy-shi88 fa7d8bb
#709 fix comment on blocksmith strategy & update fake smith function …
andy-shi88 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -285,8 +285,8 @@ func (bs *BlockService) PreValidateBlock(block, previousLastBlock *model.Block) | |
return blocker.NewBlocker(blocker.BlockErr, "InvalidBlocksmith") | ||
} | ||
// check smithtime | ||
blocksmithTime := bs.BlocksmithStrategy.GetSmithTime(*blocksmithIndex, previousLastBlock) | ||
if blocksmithTime > block.GetTimestamp() { | ||
err := bs.BlocksmithStrategy.IsValidSmithTime(*blocksmithIndex, int64(len(blocksmithsMap)), previousLastBlock) | ||
if err != nil { | ||
return blocker.NewBlocker(blocker.BlockErr, "InvalidSmithTime") | ||
} | ||
return nil | ||
|
@@ -597,7 +597,8 @@ func (bs *BlockService) PushBlock(previousBlock, block *model.Block, broadcast, | |
// handle if is first index | ||
if *blocksmithIndex > 0 { | ||
// check if current block is in pushable window | ||
if !bs.canPersistBlock(*blocksmithIndex, previousBlock) { | ||
err = bs.BlocksmithStrategy.CanPersistBlock(*blocksmithIndex, int64(len(blocksmithsMap)), previousBlock) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
if err != nil { | ||
// insert into block pool | ||
bs.BlockPoolService.InsertBlock(block, *blocksmithIndex) | ||
if rollbackErr := bs.QueryExecutor.RollbackTx(); rollbackErr != nil { | ||
|
@@ -643,8 +644,10 @@ func (bs *BlockService) ScanBlockPool() error { | |
return err | ||
} | ||
blocks := bs.BlockPoolService.GetBlocks() | ||
blocksmithsMap := bs.BlocksmithStrategy.GetSortedBlocksmiths(previousBlock) | ||
for index, block := range blocks { | ||
if bs.canPersistBlock(index, previousBlock) { | ||
err = bs.BlocksmithStrategy.CanPersistBlock(index, int64(len(blocksmithsMap)), previousBlock) | ||
if err == nil { | ||
err := func(block *model.Block) error { | ||
bs.ChainWriteLock(constant.BlockchainStatusReceivingBlock) | ||
defer bs.ChainWriteUnlock(constant.BlockchainStatusReceivingBlock) | ||
|
@@ -659,64 +662,10 @@ func (bs *BlockService) ScanBlockPool() error { | |
break | ||
} | ||
} | ||
// check if we have passed the last blocksmith expiry time | ||
numberOfBlocksmiths := len(bs.BlocksmithStrategy.GetSortedBlocksmithsMap(previousBlock)) | ||
lastBlocksmithExpiry := bs.BlocksmithStrategy.GetSmithTime(int64(numberOfBlocksmiths-1), previousBlock) + | ||
constant.SmithingBlockCreationTime + constant.SmithingNetworkTolerance | ||
if len(blocks) > 0 && | ||
time.Now().Unix() > lastBlocksmithExpiry { | ||
// choose block to persist since all blocksmith time has expired | ||
var ( | ||
bestIndex int64 | ||
bestCumDiff = new(big.Int).SetInt64(0) | ||
) | ||
for index, block := range blocks { | ||
currBlockCumDiff, _ := new(big.Int).SetString(block.GetCumulativeDifficulty(), 10) | ||
if currBlockCumDiff.Cmp(bestCumDiff) > 0 { | ||
bestCumDiff = currBlockCumDiff | ||
bestIndex = index | ||
} | ||
} | ||
err := func() error { | ||
bs.ChainWriteLock(constant.BlockchainStatusReceivingBlock) | ||
defer bs.ChainWriteUnlock(constant.BlockchainStatusReceivingBlock) | ||
err := bs.PushBlock(previousBlock, blocks[bestIndex], true, true) | ||
return err | ||
}() | ||
if err != nil { | ||
return blocker.NewBlocker( | ||
blocker.BlockErr, "ScanBlockPool:PushBlockFail", | ||
) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// canPersistBlock check if the blocksmith can push the block based on previous block's blocksmiths order | ||
// this function must only run when receiving / generating block, not on download block since it uses the current machine | ||
// time as comparison | ||
// todo: will move this to block pool service + write the test when refactoring the block service | ||
func (bs *BlockService) canPersistBlock(blocksmithIndex int64, previousBlock *model.Block) bool { | ||
if blocksmithIndex < 1 { | ||
return true | ||
} | ||
var ( | ||
currentTime = time.Now().Unix() | ||
) | ||
blocksmithAllowedBeginTime := bs.BlocksmithStrategy.GetSmithTime(blocksmithIndex, previousBlock) | ||
blocksmithExpiredPersistTime := blocksmithAllowedBeginTime + | ||
constant.SmithingBlockCreationTime + constant.SmithingNetworkTolerance | ||
previousBlocksmithAllowedBeginTime := blocksmithAllowedBeginTime - constant.SmithingBlocksmithTimeGap | ||
blocksmithAllowedPersistTime := previousBlocksmithAllowedBeginTime + | ||
constant.SmithingBlockCreationTime + constant.SmithingNetworkTolerance | ||
// allowed time window = lastBlocksmithExpiredTime < current_time <= currentBlocksmithExpiredTime | ||
if previousBlock.GetHeight() == 0 { | ||
return currentTime > blocksmithAllowedPersistTime | ||
} | ||
return currentTime >= blocksmithAllowedPersistTime && currentTime <= blocksmithExpiredPersistTime | ||
} | ||
|
||
// adminNodes seelct and admit nodes from node registry | ||
// adminNodes select and admit nodes from node registry | ||
func (bs *BlockService) admitNodes(block *model.Block) error { | ||
// select n (= MaxNodeAdmittancePerCycle) queued nodes with the highest locked balance from node registry | ||
nodeRegistrations, err := bs.NodeRegistrationService.SelectNodesToBeAdmitted(constant.MaxNodeAdmittancePerCycle) | ||
|
@@ -1463,7 +1412,8 @@ func (bs *BlockService) WillSmith( | |
bs.BlocksmithStrategy.SortBlocksmiths(lastBlock, true) | ||
// check if eligible to create block in this round | ||
blocksmithsMap := bs.BlocksmithStrategy.GetSortedBlocksmithsMap(lastBlock) | ||
if blocksmithsMap[string(blocksmith.NodePublicKey)] == nil { | ||
blocksmithIdx := blocksmithsMap[string(blocksmith.NodePublicKey)] | ||
if blocksmithIdx == nil { | ||
return blockchainProcessorLastBlockID, blocksmithIndex, | ||
blocker.NewBlocker(blocker.SmithingErr, "BlocksmithNotInBlocksmithList") | ||
} | ||
|
@@ -1482,16 +1432,11 @@ func (bs *BlockService) WillSmith( | |
bs.Logger.Errorf("Participation score calculation: %s", err) | ||
return 0, 0, blocker.NewBlocker(blocker.ZeroParticipationScoreErr, "participation score = 0") | ||
} | ||
err = bs.BlocksmithStrategy.CalculateSmith( | ||
lastBlock, | ||
*(blocksmithsMap[string(blocksmith.NodePublicKey)]), | ||
blocksmith, | ||
blocksmithScore, | ||
) | ||
err = bs.BlocksmithStrategy.CalculateScore(blocksmith, blocksmithScore) | ||
if err != nil { | ||
return blockchainProcessorLastBlockID, blocksmithIndex, err | ||
} | ||
monitoring.SetBlockchainSmithTime(bs.GetChainType(), blocksmith.SmithTime-lastBlock.Timestamp) | ||
monitoring.SetBlockchainSmithIndex(bs.GetChainType(), *blocksmithIdx) | ||
} | ||
// check for block pool duplicate | ||
blocksmithsMap := bs.BlocksmithStrategy.GetSortedBlocksmithsMap(lastBlock) | ||
|
@@ -1508,7 +1453,14 @@ func (bs *BlockService) WillSmith( | |
blocker.BlockErr, "DuplicateBlockPool", | ||
) | ||
} | ||
return blockchainProcessorLastBlockID, blocksmithIndex, nil | ||
// check if it's legal to create block for current blocksmith now | ||
err = bs.BlocksmithStrategy.IsValidSmithTime(blocksmithIndex, int64(len(blocksmithsMap)), lastBlock) | ||
if err == nil { | ||
return blockchainProcessorLastBlockID, blocksmithIndex, nil | ||
} | ||
return blockchainProcessorLastBlockID, blocksmithIndex, blocker.NewBlocker( | ||
blocker.SmithingErr, "NotTimeToSmithYet", | ||
) | ||
} | ||
|
||
// ProcessCompletedBlock to process block that already having all needed transactions | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this can be unified in one constant
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm worried that we might need a longer network tolerance time for spine block since it is of larger size. is that a possibility?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok then :)