From 4b1e5741de54a8191422fe5dfd015131fddc72da Mon Sep 17 00:00:00 2001 From: andy-shi88 Date: Fri, 27 Mar 2020 13:57:34 +0800 Subject: [PATCH 1/8] #690 add empty flag to enable empty block generate --- core/service/blockCoreService.go | 3 +- core/service/blockMainService.go | 49 +++++---- core/service/blockMainService_test.go | 104 ++++++++++++++++++ core/service/blockSpineService.go | 11 +- core/service/blockSpineService_test.go | 2 + core/smith/blockchainProcessor.go | 8 +- core/smith/strategy/blocksmithStrategyMain.go | 1 + 7 files changed, 148 insertions(+), 30 deletions(-) diff --git a/core/service/blockCoreService.go b/core/service/blockCoreService.go index 71b719e2a..2ad0ef304 100644 --- a/core/service/blockCoreService.go +++ b/core/service/blockCoreService.go @@ -20,6 +20,7 @@ type ( previousBlock *model.Block, secretPhrase string, timestamp int64, + empty bool, ) (*model.Block, error) ValidateBlock(block, previousLastBlock *model.Block, curTime int64) error ValidatePayloadHash(block *model.Block) error @@ -54,6 +55,6 @@ type ( WillSmith( blocksmith *model.Blocksmith, blockchainProcessorLastBlockID int64, - ) (int64, error) + ) (lastBlockID, blocksmithIndex int64, err error) } ) diff --git a/core/service/blockMainService.go b/core/service/blockMainService.go index 4a468f49f..8fbd9ada7 100644 --- a/core/service/blockMainService.go +++ b/core/service/blockMainService.go @@ -985,11 +985,12 @@ func (bs *BlockService) GetPayloadHashAndLength(block *model.Block) (payloadHash return } -// GenerateBlock generate block from transactions in mempool +// GenerateBlock generate block from transactions in mempool, pass empty flag to generate an empty block func (bs *BlockService) GenerateBlock( previousBlock *model.Block, secretPhrase string, timestamp int64, + empty bool, ) (*model.Block, error) { var ( totalAmount, totalFee, totalCoinbase int64 @@ -1003,19 +1004,22 @@ func (bs *BlockService) GenerateBlock( newBlockHeight := previousBlock.Height + 1 // calculate total coinbase to be added to the block totalCoinbase = bs.CoinbaseService.GetCoinbase() - sortedTransactions, err = bs.MempoolService.SelectTransactionsFromMempool(timestamp) - if err != nil { - return nil, errors.New("MempoolReadError") - } - // select transactions from mempool to be added to the block - for _, tx := range sortedTransactions { - txType, errType := bs.ActionTypeSwitcher.GetTransactionType(tx) - if errType != nil { - return nil, err + if !empty { + sortedTransactions, err = bs.MempoolService.SelectTransactionsFromMempool(timestamp) + if err != nil { + return nil, errors.New("MempoolReadError") + } + // select transactions from mempool to be added to the block + for _, tx := range sortedTransactions { + txType, errType := bs.ActionTypeSwitcher.GetTransactionType(tx) + if errType != nil { + return nil, err + } + totalAmount += txType.GetAmount() + totalFee += tx.Fee } - totalAmount += txType.GetAmount() - totalFee += tx.Fee } + // select published receipts to be added to the block publishedReceipts, err = bs.ReceiptService.SelectReceipts( timestamp, bs.ReceiptUtil.GetNumberOfMaxReceipts( @@ -1445,11 +1449,11 @@ func (bs *BlockService) PopOffToBlock(commonBlock *model.Block) ([]*model.Block, func (bs *BlockService) WillSmith( blocksmith *model.Blocksmith, blockchainProcessorLastBlockID int64, -) (int64, error) { - var blocksmithScore int64 +) (lastBlockID, index int64, err error) { + var blocksmithScore, blocksmithIndex int64 lastBlock, err := bs.GetLastBlock() if err != nil { - return blockchainProcessorLastBlockID, blocker.NewBlocker( + return blockchainProcessorLastBlockID, blocksmithIndex, blocker.NewBlocker( blocker.SmithingErr, "genesis block has not been applied") } @@ -1460,7 +1464,7 @@ func (bs *BlockService) WillSmith( // check if eligible to create block in this round blocksmithsMap := bs.BlocksmithStrategy.GetSortedBlocksmithsMap(lastBlock) if blocksmithsMap[string(blocksmith.NodePublicKey)] == nil { - return blockchainProcessorLastBlockID, + return blockchainProcessorLastBlockID, blocksmithIndex, blocker.NewBlocker(blocker.SmithingErr, "BlocksmithNotInBlocksmithList") } // calculate blocksmith score for the block type @@ -1484,25 +1488,26 @@ func (bs *BlockService) WillSmith( blocksmithScore, ) if err != nil { - return blockchainProcessorLastBlockID, err + return blockchainProcessorLastBlockID, blocksmithIndex, err } monitoring.SetBlockchainSmithTime(bs.GetChainType(), blocksmith.SmithTime-lastBlock.Timestamp) } // check for block pool duplicate blocksmithsMap := bs.BlocksmithStrategy.GetSortedBlocksmithsMap(lastBlock) - blocksmithIndex, ok := blocksmithsMap[string(blocksmith.NodePublicKey)] + blocksmithIdxPtr, ok := blocksmithsMap[string(blocksmith.NodePublicKey)] if !ok { - return blockchainProcessorLastBlockID, blocker.NewBlocker( + return blockchainProcessorLastBlockID, blocksmithIndex, blocker.NewBlocker( blocker.BlockErr, "BlocksmithNotInSmithingList", ) } - blockPool := bs.BlockPoolService.GetBlock(*blocksmithIndex) + blocksmithIndex = *blocksmithIdxPtr + blockPool := bs.BlockPoolService.GetBlock(blocksmithIndex) if blockPool != nil { - return blockchainProcessorLastBlockID, blocker.NewBlocker( + return blockchainProcessorLastBlockID, blocksmithIndex, blocker.NewBlocker( blocker.BlockErr, "DuplicateBlockPool", ) } - return blockchainProcessorLastBlockID, nil + return blockchainProcessorLastBlockID, blocksmithIndex, nil } // ProcessCompletedBlock to process block that already having all needed transactions diff --git a/core/service/blockMainService_test.go b/core/service/blockMainService_test.go index 973bb1cd0..fe760b74a 100644 --- a/core/service/blockMainService_test.go +++ b/core/service/blockMainService_test.go @@ -1705,6 +1705,7 @@ func TestBlockService_GenerateBlock(t *testing.T) { secretPhrase string timestamp int64 blockSmithAccountAddress string + empty bool } tests := []struct { name string @@ -1740,6 +1741,7 @@ func TestBlockService_GenerateBlock(t *testing.T) { secretPhrase: "phasepress", timestamp: 12344587645, blockSmithAccountAddress: "BCZ", + empty: false, }, wantErr: true, }, @@ -1778,6 +1780,7 @@ func TestBlockService_GenerateBlock(t *testing.T) { }, secretPhrase: "", timestamp: 12345678, + empty: false, }, wantErr: false, }, @@ -1802,6 +1805,7 @@ func TestBlockService_GenerateBlock(t *testing.T) { tt.args.previousBlock, tt.args.secretPhrase, tt.args.timestamp, + tt.args.empty, ) if (err != nil) != tt.wantErr { t.Errorf("BlockService.GenerateBlock() error = %v, wantErr %v", err, tt.wantErr) @@ -5164,3 +5168,103 @@ func TestBlockService_ValidatePayloadHash(t *testing.T) { }) } } + +func TestBlockService_WillSmith(t *testing.T) { + type fields struct { + RWMutex sync.RWMutex + Chaintype chaintype.ChainType + KVExecutor kvdb.KVExecutorInterface + QueryExecutor query.ExecutorInterface + BlockQuery query.BlockQueryInterface + MempoolQuery query.MempoolQueryInterface + TransactionQuery query.TransactionQueryInterface + PublishedReceiptQuery query.PublishedReceiptQueryInterface + SkippedBlocksmithQuery query.SkippedBlocksmithQueryInterface + Signature crypto.SignatureInterface + MempoolService MempoolServiceInterface + ReceiptService ReceiptServiceInterface + NodeRegistrationService NodeRegistrationServiceInterface + BlocksmithService BlocksmithServiceInterface + ActionTypeSwitcher transaction.TypeActionSwitcher + AccountBalanceQuery query.AccountBalanceQueryInterface + ParticipationScoreQuery query.ParticipationScoreQueryInterface + NodeRegistrationQuery query.NodeRegistrationQueryInterface + AccountLedgerQuery query.AccountLedgerQueryInterface + BlocksmithStrategy strategy.BlocksmithStrategyInterface + BlockIncompleteQueueService BlockIncompleteQueueServiceInterface + BlockPoolService BlockPoolServiceInterface + Observer *observer.Observer + Logger *log.Logger + TransactionUtil transaction.UtilInterface + ReceiptUtil coreUtil.ReceiptUtilInterface + PublishedReceiptUtil coreUtil.PublishedReceiptUtilInterface + TransactionCoreService TransactionCoreServiceInterface + CoinbaseService CoinbaseServiceInterface + ParticipationScoreService ParticipationScoreServiceInterface + PublishedReceiptService PublishedReceiptServiceInterface + } + type args struct { + blocksmith *model.Blocksmith + blockchainProcessorLastBlockID int64 + } + tests := []struct { + name string + fields fields + args args + wantLastBlockID int64 + wantIndex int64 + wantErr bool + }{ + { + // + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + bs := &BlockService{ + RWMutex: tt.fields.RWMutex, + Chaintype: tt.fields.Chaintype, + KVExecutor: tt.fields.KVExecutor, + QueryExecutor: tt.fields.QueryExecutor, + BlockQuery: tt.fields.BlockQuery, + MempoolQuery: tt.fields.MempoolQuery, + TransactionQuery: tt.fields.TransactionQuery, + PublishedReceiptQuery: tt.fields.PublishedReceiptQuery, + SkippedBlocksmithQuery: tt.fields.SkippedBlocksmithQuery, + Signature: tt.fields.Signature, + MempoolService: tt.fields.MempoolService, + ReceiptService: tt.fields.ReceiptService, + NodeRegistrationService: tt.fields.NodeRegistrationService, + BlocksmithService: tt.fields.BlocksmithService, + ActionTypeSwitcher: tt.fields.ActionTypeSwitcher, + AccountBalanceQuery: tt.fields.AccountBalanceQuery, + ParticipationScoreQuery: tt.fields.ParticipationScoreQuery, + NodeRegistrationQuery: tt.fields.NodeRegistrationQuery, + AccountLedgerQuery: tt.fields.AccountLedgerQuery, + BlocksmithStrategy: tt.fields.BlocksmithStrategy, + BlockIncompleteQueueService: tt.fields.BlockIncompleteQueueService, + BlockPoolService: tt.fields.BlockPoolService, + Observer: tt.fields.Observer, + Logger: tt.fields.Logger, + TransactionUtil: tt.fields.TransactionUtil, + ReceiptUtil: tt.fields.ReceiptUtil, + PublishedReceiptUtil: tt.fields.PublishedReceiptUtil, + TransactionCoreService: tt.fields.TransactionCoreService, + CoinbaseService: tt.fields.CoinbaseService, + ParticipationScoreService: tt.fields.ParticipationScoreService, + PublishedReceiptService: tt.fields.PublishedReceiptService, + } + gotLastBlockID, gotIndex, err := bs.WillSmith(tt.args.blocksmith, tt.args.blockchainProcessorLastBlockID) + if (err != nil) != tt.wantErr { + t.Errorf("WillSmith() error = %v, wantErr %v", err, tt.wantErr) + return + } + if gotLastBlockID != tt.wantLastBlockID { + t.Errorf("WillSmith() gotLastBlockID = %v, want %v", gotLastBlockID, tt.wantLastBlockID) + } + if gotIndex != tt.wantIndex { + t.Errorf("WillSmith() gotIndex = %v, want %v", gotIndex, tt.wantIndex) + } + }) + } +} diff --git a/core/service/blockSpineService.go b/core/service/blockSpineService.go index 7a60d2c3f..f1521c9bf 100644 --- a/core/service/blockSpineService.go +++ b/core/service/blockSpineService.go @@ -530,6 +530,7 @@ func (bs *BlockSpineService) GenerateBlock( previousBlock *model.Block, secretPhrase string, timestamp int64, + _ bool, ) (*model.Block, error) { var ( spinePublicKeys []*model.SpinePublicKey @@ -877,10 +878,10 @@ func (bs *BlockSpineService) BlockTransactionsRequestedListener() observer.Liste func (bs *BlockSpineService) WillSmith( blocksmith *model.Blocksmith, blockchainProcessorLastBlockID int64, -) (int64, error) { +) (lastBlockId, index int64, err error) { lastBlock, err := bs.GetLastBlock() if err != nil { - return blockchainProcessorLastBlockID, blocker.NewBlocker( + return blockchainProcessorLastBlockID, 0, blocker.NewBlocker( blocker.SmithingErr, "genesis block has not been applied") } // caching: only calculate smith time once per new block @@ -891,7 +892,7 @@ func (bs *BlockSpineService) WillSmith( // check if eligible to create block in this round blocksmithsMap := blockSmithStrategy.GetSortedBlocksmithsMap(lastBlock) if blocksmithsMap[string(blocksmith.NodePublicKey)] == nil { - return blockchainProcessorLastBlockID, + return blockchainProcessorLastBlockID, 0, blocker.NewBlocker(blocker.SmithingErr, "BlocksmithNotInBlocksmithList") } // calculate blocksmith score for the block type @@ -904,11 +905,11 @@ func (bs *BlockSpineService) WillSmith( blocksmithScore, ) if err != nil { - return blockchainProcessorLastBlockID, err + return blockchainProcessorLastBlockID, 0, err } monitoring.SetBlockchainSmithTime(bs.GetChainType(), blocksmith.SmithTime-lastBlock.Timestamp) } - return blockchainProcessorLastBlockID, nil + return blockchainProcessorLastBlockID, 0, nil } func (bs *BlockSpineService) ValidateSpineBlockManifest(spineBlockManifest *model.SpineBlockManifest) error { diff --git a/core/service/blockSpineService_test.go b/core/service/blockSpineService_test.go index 79cf743b0..e762b1666 100644 --- a/core/service/blockSpineService_test.go +++ b/core/service/blockSpineService_test.go @@ -1410,6 +1410,7 @@ func TestBlockSpineService_GenerateBlock(t *testing.T) { previousBlock *model.Block secretPhrase string timestamp int64 + empty bool } tests := []struct { name string @@ -1492,6 +1493,7 @@ func TestBlockSpineService_GenerateBlock(t *testing.T) { tt.args.previousBlock, tt.args.secretPhrase, tt.args.timestamp, + tt.args.empty, ) if (err != nil) != tt.wantErr { t.Errorf("BlockSpineService.GenerateBlock() error = %v, wantErr %v", err, tt.wantErr) diff --git a/core/smith/blockchainProcessor.go b/core/smith/blockchainProcessor.go index 5e03a30dd..faacab35b 100644 --- a/core/smith/blockchainProcessor.go +++ b/core/smith/blockchainProcessor.go @@ -1,9 +1,10 @@ package smith import ( - "github.com/zoobc/zoobc-core/common/chaintype" "time" + "github.com/zoobc/zoobc-core/common/chaintype" + log "github.com/sirupsen/logrus" "github.com/zoobc/zoobc-core/common/blocker" "github.com/zoobc/zoobc-core/common/constant" @@ -103,6 +104,7 @@ func (bp *BlockchainProcessor) FakeSmithing(numberOfBlocks int, fromGenesis bool previousBlock, bp.Generator.SecretPhrase, timeNow, + true, ) if err != nil { return err @@ -128,6 +130,7 @@ func (bp *BlockchainProcessor) StartSmithing() error { bp.BlockService.ChainWriteLock(constant.BlockchainStatusGeneratingBlock) defer bp.BlockService.ChainWriteUnlock(constant.BlockchainStatusGeneratingBlock) + var blocksmithIndex int64 lastBlock, err := bp.BlockService.GetLastBlock() if err != nil { return blocker.NewBlocker( @@ -135,7 +138,7 @@ func (bp *BlockchainProcessor) StartSmithing() error { } // todo: move this piece of code to service layer // caching: only calculate smith time once per new block - bp.LastBlockID, err = bp.BlockService.WillSmith( + bp.LastBlockID, blocksmithIndex, err = bp.BlockService.WillSmith( bp.Generator, bp.LastBlockID, ) if err != nil { @@ -149,6 +152,7 @@ func (bp *BlockchainProcessor) StartSmithing() error { lastBlock, bp.Generator.SecretPhrase, timestamp, + blocksmithIndex >= constant.EmptyBlockSkippedBlocksmithLimit, ) if err != nil { return err diff --git a/core/smith/strategy/blocksmithStrategyMain.go b/core/smith/strategy/blocksmithStrategyMain.go index 3fb5f0f2c..eb2a7a37b 100644 --- a/core/smith/strategy/blocksmithStrategyMain.go +++ b/core/smith/strategy/blocksmithStrategyMain.go @@ -104,6 +104,7 @@ func (bss *BlocksmithStrategyMain) GetSortedBlocksmithsMap(block *model.Block) m return result } +// SortBlocksmiths sort the list of active node of current block height, index start from 0. func (bss *BlocksmithStrategyMain) SortBlocksmiths(block *model.Block, withLock bool) { if block.ID == bss.LastSortedBlockID && block.ID != constant.MainchainGenesisBlockID { return From 3c3a044536ce9f1294ffabaa4f05a578d52aa274 Mon Sep 17 00:00:00 2001 From: andy-shi88 Date: Fri, 27 Mar 2020 14:17:13 +0800 Subject: [PATCH 2/8] #690 add limit for skipped block smith --- common/constant/smith.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/common/constant/smith.go b/common/constant/smith.go index 263c15a63..c0b872274 100644 --- a/common/constant/smith.go +++ b/common/constant/smith.go @@ -24,4 +24,7 @@ var ( MainChainSmithIdlePeriod = 500 * time.Millisecond // MainChainSmithingPeriod one main block every 15 seconds + block pool delay (max +30 seconds) MainChainSmithingPeriod = int64(15) + // EmptyBlockSkippedBlocksmithLimit state the number of allowed skipped blocksmith until only empty block can be generated + // 0 will set node to always create empty block + EmptyBlockSkippedBlocksmithLimit = int64(1) ) From b1a6b31b073c9ea04bb0b7bf42d8ba55e0c78a15 Mon Sep 17 00:00:00 2001 From: andy-shi88 Date: Fri, 27 Mar 2020 14:26:26 +0800 Subject: [PATCH 3/8] #690 set the skipped limit back to 15 --- common/constant/smith.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/constant/smith.go b/common/constant/smith.go index c0b872274..3f3be432b 100644 --- a/common/constant/smith.go +++ b/common/constant/smith.go @@ -26,5 +26,5 @@ var ( MainChainSmithingPeriod = int64(15) // EmptyBlockSkippedBlocksmithLimit state the number of allowed skipped blocksmith until only empty block can be generated // 0 will set node to always create empty block - EmptyBlockSkippedBlocksmithLimit = int64(1) + EmptyBlockSkippedBlocksmithLimit = int64(15) ) From 4df4875e5795a78d457de5b80c79302d10f1288c Mon Sep 17 00:00:00 2001 From: andy-shi88 Date: Fri, 27 Mar 2020 14:35:01 +0800 Subject: [PATCH 4/8] #690 remove unimplemented test --- core/service/blockMainService_test.go | 100 -------------------------- 1 file changed, 100 deletions(-) diff --git a/core/service/blockMainService_test.go b/core/service/blockMainService_test.go index fe760b74a..21740249a 100644 --- a/core/service/blockMainService_test.go +++ b/core/service/blockMainService_test.go @@ -5168,103 +5168,3 @@ func TestBlockService_ValidatePayloadHash(t *testing.T) { }) } } - -func TestBlockService_WillSmith(t *testing.T) { - type fields struct { - RWMutex sync.RWMutex - Chaintype chaintype.ChainType - KVExecutor kvdb.KVExecutorInterface - QueryExecutor query.ExecutorInterface - BlockQuery query.BlockQueryInterface - MempoolQuery query.MempoolQueryInterface - TransactionQuery query.TransactionQueryInterface - PublishedReceiptQuery query.PublishedReceiptQueryInterface - SkippedBlocksmithQuery query.SkippedBlocksmithQueryInterface - Signature crypto.SignatureInterface - MempoolService MempoolServiceInterface - ReceiptService ReceiptServiceInterface - NodeRegistrationService NodeRegistrationServiceInterface - BlocksmithService BlocksmithServiceInterface - ActionTypeSwitcher transaction.TypeActionSwitcher - AccountBalanceQuery query.AccountBalanceQueryInterface - ParticipationScoreQuery query.ParticipationScoreQueryInterface - NodeRegistrationQuery query.NodeRegistrationQueryInterface - AccountLedgerQuery query.AccountLedgerQueryInterface - BlocksmithStrategy strategy.BlocksmithStrategyInterface - BlockIncompleteQueueService BlockIncompleteQueueServiceInterface - BlockPoolService BlockPoolServiceInterface - Observer *observer.Observer - Logger *log.Logger - TransactionUtil transaction.UtilInterface - ReceiptUtil coreUtil.ReceiptUtilInterface - PublishedReceiptUtil coreUtil.PublishedReceiptUtilInterface - TransactionCoreService TransactionCoreServiceInterface - CoinbaseService CoinbaseServiceInterface - ParticipationScoreService ParticipationScoreServiceInterface - PublishedReceiptService PublishedReceiptServiceInterface - } - type args struct { - blocksmith *model.Blocksmith - blockchainProcessorLastBlockID int64 - } - tests := []struct { - name string - fields fields - args args - wantLastBlockID int64 - wantIndex int64 - wantErr bool - }{ - { - // - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - bs := &BlockService{ - RWMutex: tt.fields.RWMutex, - Chaintype: tt.fields.Chaintype, - KVExecutor: tt.fields.KVExecutor, - QueryExecutor: tt.fields.QueryExecutor, - BlockQuery: tt.fields.BlockQuery, - MempoolQuery: tt.fields.MempoolQuery, - TransactionQuery: tt.fields.TransactionQuery, - PublishedReceiptQuery: tt.fields.PublishedReceiptQuery, - SkippedBlocksmithQuery: tt.fields.SkippedBlocksmithQuery, - Signature: tt.fields.Signature, - MempoolService: tt.fields.MempoolService, - ReceiptService: tt.fields.ReceiptService, - NodeRegistrationService: tt.fields.NodeRegistrationService, - BlocksmithService: tt.fields.BlocksmithService, - ActionTypeSwitcher: tt.fields.ActionTypeSwitcher, - AccountBalanceQuery: tt.fields.AccountBalanceQuery, - ParticipationScoreQuery: tt.fields.ParticipationScoreQuery, - NodeRegistrationQuery: tt.fields.NodeRegistrationQuery, - AccountLedgerQuery: tt.fields.AccountLedgerQuery, - BlocksmithStrategy: tt.fields.BlocksmithStrategy, - BlockIncompleteQueueService: tt.fields.BlockIncompleteQueueService, - BlockPoolService: tt.fields.BlockPoolService, - Observer: tt.fields.Observer, - Logger: tt.fields.Logger, - TransactionUtil: tt.fields.TransactionUtil, - ReceiptUtil: tt.fields.ReceiptUtil, - PublishedReceiptUtil: tt.fields.PublishedReceiptUtil, - TransactionCoreService: tt.fields.TransactionCoreService, - CoinbaseService: tt.fields.CoinbaseService, - ParticipationScoreService: tt.fields.ParticipationScoreService, - PublishedReceiptService: tt.fields.PublishedReceiptService, - } - gotLastBlockID, gotIndex, err := bs.WillSmith(tt.args.blocksmith, tt.args.blockchainProcessorLastBlockID) - if (err != nil) != tt.wantErr { - t.Errorf("WillSmith() error = %v, wantErr %v", err, tt.wantErr) - return - } - if gotLastBlockID != tt.wantLastBlockID { - t.Errorf("WillSmith() gotLastBlockID = %v, want %v", gotLastBlockID, tt.wantLastBlockID) - } - if gotIndex != tt.wantIndex { - t.Errorf("WillSmith() gotIndex = %v, want %v", gotIndex, tt.wantIndex) - } - }) - } -} From 85414f756622996a78a4ea153062ee5f10b8af4f Mon Sep 17 00:00:00 2001 From: andy-shi88 Date: Mon, 30 Mar 2020 10:35:47 +0800 Subject: [PATCH 5/8] #690 fix return arguments --- core/service/blockMainService.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/service/blockMainService.go b/core/service/blockMainService.go index 3feb5a5e5..40e91c4b2 100644 --- a/core/service/blockMainService.go +++ b/core/service/blockMainService.go @@ -1480,7 +1480,7 @@ func (bs *BlockService) WillSmith( // no negative scores allowed blocksmithScore = 0 bs.Logger.Errorf("Participation score calculation: %s", err) - return 0, blocker.NewBlocker(blocker.ZeroParticipationScoreErr, "participation score = 0") + return 0, 0, blocker.NewBlocker(blocker.ZeroParticipationScoreErr, "participation score = 0") } err = bs.BlocksmithStrategy.CalculateSmith( lastBlock, From d6ce4ec29df69404de56242c0ea7456f79cb571b Mon Sep 17 00:00:00 2001 From: andy-shi88 Date: Mon, 30 Mar 2020 10:40:00 +0800 Subject: [PATCH 6/8] #690 remove unnecessary variable initialization --- core/service/blockMainService.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/service/blockMainService.go b/core/service/blockMainService.go index 40e91c4b2..c64819284 100644 --- a/core/service/blockMainService.go +++ b/core/service/blockMainService.go @@ -1449,8 +1449,8 @@ func (bs *BlockService) PopOffToBlock(commonBlock *model.Block) ([]*model.Block, func (bs *BlockService) WillSmith( blocksmith *model.Blocksmith, blockchainProcessorLastBlockID int64, -) (lastBlockID, index int64, err error) { - var blocksmithScore, blocksmithIndex int64 +) (lastBlockID, blocksmithIndex int64, err error) { + var blocksmithScore int64 lastBlock, err := bs.GetLastBlock() if err != nil { return blockchainProcessorLastBlockID, blocksmithIndex, blocker.NewBlocker( From a5155f8095aba468287db3feabf5545d9693c68b Mon Sep 17 00:00:00 2001 From: andy-shi88 Date: Mon, 30 Mar 2020 10:41:43 +0800 Subject: [PATCH 7/8] #690 make use of default int64 value --- core/service/blockSpineService.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/core/service/blockSpineService.go b/core/service/blockSpineService.go index f1521c9bf..216ce24e3 100644 --- a/core/service/blockSpineService.go +++ b/core/service/blockSpineService.go @@ -878,10 +878,10 @@ func (bs *BlockSpineService) BlockTransactionsRequestedListener() observer.Liste func (bs *BlockSpineService) WillSmith( blocksmith *model.Blocksmith, blockchainProcessorLastBlockID int64, -) (lastBlockId, index int64, err error) { +) (lastBlockId, blocksmithIndex int64, err error) { lastBlock, err := bs.GetLastBlock() if err != nil { - return blockchainProcessorLastBlockID, 0, blocker.NewBlocker( + return blockchainProcessorLastBlockID, blocksmithIndex, blocker.NewBlocker( blocker.SmithingErr, "genesis block has not been applied") } // caching: only calculate smith time once per new block @@ -892,7 +892,7 @@ func (bs *BlockSpineService) WillSmith( // check if eligible to create block in this round blocksmithsMap := blockSmithStrategy.GetSortedBlocksmithsMap(lastBlock) if blocksmithsMap[string(blocksmith.NodePublicKey)] == nil { - return blockchainProcessorLastBlockID, 0, + return blockchainProcessorLastBlockID, blocksmithIndex, blocker.NewBlocker(blocker.SmithingErr, "BlocksmithNotInBlocksmithList") } // calculate blocksmith score for the block type @@ -905,11 +905,11 @@ func (bs *BlockSpineService) WillSmith( blocksmithScore, ) if err != nil { - return blockchainProcessorLastBlockID, 0, err + return blockchainProcessorLastBlockID, blocksmithIndex, err } monitoring.SetBlockchainSmithTime(bs.GetChainType(), blocksmith.SmithTime-lastBlock.Timestamp) } - return blockchainProcessorLastBlockID, 0, nil + return blockchainProcessorLastBlockID, blocksmithIndex, nil } func (bs *BlockSpineService) ValidateSpineBlockManifest(spineBlockManifest *model.SpineBlockManifest) error { From 9aed23fca2123ad200330eaf2653a06842feeceb Mon Sep 17 00:00:00 2001 From: andy-shi88 Date: Mon, 30 Mar 2020 11:03:02 +0800 Subject: [PATCH 8/8] #690 smaller skipped blocksmith constant so we can see the effect on dev net --- common/constant/smith.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/constant/smith.go b/common/constant/smith.go index 3f3be432b..9f2a44406 100644 --- a/common/constant/smith.go +++ b/common/constant/smith.go @@ -26,5 +26,5 @@ var ( MainChainSmithingPeriod = int64(15) // EmptyBlockSkippedBlocksmithLimit state the number of allowed skipped blocksmith until only empty block can be generated // 0 will set node to always create empty block - EmptyBlockSkippedBlocksmithLimit = int64(15) + EmptyBlockSkippedBlocksmithLimit = int64(2) )