diff --git a/core/blockchainsync/downloadBlockchain.go b/core/blockchainsync/downloadBlockchain.go index 84ec9a66e..1a97b3299 100644 --- a/core/blockchainsync/downloadBlockchain.go +++ b/core/blockchainsync/downloadBlockchain.go @@ -359,7 +359,7 @@ func (bd *BlockchainDownloader) DownloadFromPeer(feederPeer *model.Peer, chainBl monitoring.IncrementMainchainDownloadCycleDebugger(bd.ChainType, 67) if lastBlock.ID == previousBlockID { monitoring.IncrementMainchainDownloadCycleDebugger(bd.ChainType, 68) - err := bd.BlockService.ValidateBlock(block, lastBlock, time.Now().Unix()) + err := bd.BlockService.ValidateBlock(block, lastBlock) if err != nil { monitoring.IncrementMainchainDownloadCycleDebugger(bd.ChainType, 69) bd.Logger.Infof("[download blockchain] failed to verify block %v from peer: %s\nwith previous: %v\n", block.ID, err, lastBlock.ID) diff --git a/core/blockchainsync/processFork.go b/core/blockchainsync/processFork.go index 0f7bb50db..949c74fa0 100644 --- a/core/blockchainsync/processFork.go +++ b/core/blockchainsync/processFork.go @@ -91,7 +91,7 @@ func (fp *ForkingProcessor) ProcessFork(forkBlocks []*model.Block, commonBlock * monitoring.IncrementMainchainDownloadCycleDebugger(fp.ChainType, 92) if bytes.Equal(lastBlockHash, block.PreviousBlockHash) { monitoring.IncrementMainchainDownloadCycleDebugger(fp.ChainType, 93) - err := fp.BlockService.ValidateBlock(block, lastBlock, time.Now().Unix()) + err := fp.BlockService.ValidateBlock(block, lastBlock) monitoring.IncrementMainchainDownloadCycleDebugger(fp.ChainType, 94) if err != nil { monitoring.IncrementMainchainDownloadCycleDebugger(fp.ChainType, 95) @@ -161,7 +161,7 @@ func (fp *ForkingProcessor) ProcessFork(forkBlocks []*model.Block, commonBlock * monitoring.IncrementMainchainDownloadCycleDebugger(fp.ChainType, 109) return err } - err = fp.BlockService.ValidateBlock(block, lastBlock, time.Now().Unix()) + err = fp.BlockService.ValidateBlock(block, lastBlock) monitoring.IncrementMainchainDownloadCycleDebugger(fp.ChainType, 110) if err != nil { monitoring.IncrementMainchainDownloadCycleDebugger(fp.ChainType, 111) diff --git a/core/service/blockCoreService.go b/core/service/blockCoreService.go index 2ad0ef304..ca10c3c4b 100644 --- a/core/service/blockCoreService.go +++ b/core/service/blockCoreService.go @@ -22,7 +22,7 @@ type ( timestamp int64, empty bool, ) (*model.Block, error) - ValidateBlock(block, previousLastBlock *model.Block, curTime int64) error + ValidateBlock(block, previousLastBlock *model.Block) error ValidatePayloadHash(block *model.Block) error GetPayloadHashAndLength(block *model.Block) (payloadHash []byte, payloadLength uint32, err error) PushBlock(previousBlock, block *model.Block, broadcast, persist bool) error diff --git a/core/service/blockMainService.go b/core/service/blockMainService.go index abb865244..7abc14871 100644 --- a/core/service/blockMainService.go +++ b/core/service/blockMainService.go @@ -9,7 +9,6 @@ import ( "reflect" "strconv" "sync" - "time" "github.com/dgraph-io/badger" log "github.com/sirupsen/logrus" @@ -293,16 +292,11 @@ func (bs *BlockService) PreValidateBlock(block, previousLastBlock *model.Block) } // ValidateBlock validate block to be pushed into the blockchain -func (bs *BlockService) ValidateBlock(block, previousLastBlock *model.Block, curTime int64) error { +func (bs *BlockService) ValidateBlock(block, previousLastBlock *model.Block) error { if err := bs.ValidatePayloadHash(block); err != nil { return err } - // check block timestamp - if block.GetTimestamp() > curTime+constant.GenerateBlockTimeoutSec { - return blocker.NewBlocker(blocker.BlockErr, "InvalidTimestamp") - } - // check if blocksmith can smith at the time blocksmithsMap := bs.BlocksmithStrategy.GetSortedBlocksmithsMap(previousLastBlock) blocksmithIndex := blocksmithsMap[string(block.BlocksmithPublicKey)] @@ -1491,7 +1485,7 @@ func (bs *BlockService) ProcessCompletedBlock(block *model.Block) error { "fail to get last block", ) } - err = bs.ValidateBlock(block, previousBlock, time.Now().Unix()) + err = bs.ValidateBlock(block, previousBlock) if err != nil { return status.Error(codes.InvalidArgument, "InvalidBlock") } @@ -1517,7 +1511,7 @@ func (bs *BlockService) ProcessCompletedBlock(block *model.Block) error { ) } // Validate incoming block - err = bs.ValidateBlock(block, lastBlock, time.Now().Unix()) + err = bs.ValidateBlock(block, lastBlock) if err != nil { return status.Error(codes.InvalidArgument, "InvalidBlock") } diff --git a/core/service/blockMainService_test.go b/core/service/blockMainService_test.go index f683d6a42..a4fba3771 100644 --- a/core/service/blockMainService_test.go +++ b/core/service/blockMainService_test.go @@ -3696,7 +3696,6 @@ func TestBlockService_ValidateBlock(t *testing.T) { type args struct { block *model.Block previousLastBlock *model.Block - curTime int64 } tests := []struct { name string @@ -3710,7 +3709,6 @@ func TestBlockService_ValidateBlock(t *testing.T) { block: &model.Block{ Timestamp: 1572246820 + constant.GenerateBlockTimeoutSec + 1, }, - curTime: 1572246820, }, fields: fields{}, wantErr: true, @@ -3723,7 +3721,6 @@ func TestBlockService_ValidateBlock(t *testing.T) { BlockSignature: []byte{}, BlocksmithPublicKey: []byte{}, }, - curTime: 1572246820, }, fields: fields{ Signature: &mockSignatureFail{}, @@ -3734,8 +3731,7 @@ func TestBlockService_ValidateBlock(t *testing.T) { { name: "ValidateBlock:fail-{InvalidSignature}", args: args{ - block: mockValidateBadBlockInvalidBlockHash, - curTime: 1572246820, + block: mockValidateBadBlockInvalidBlockHash, }, fields: fields{ Signature: &mockSignatureFail{}, @@ -3748,7 +3744,6 @@ func TestBlockService_ValidateBlock(t *testing.T) { args: args{ block: mockValidateBadBlockInvalidBlockHash, previousLastBlock: &model.Block{}, - curTime: 1572246820, }, fields: fields{ Signature: &mockSignature{}, @@ -3768,7 +3763,6 @@ func TestBlockService_ValidateBlock(t *testing.T) { CumulativeDifficulty: "10", }, previousLastBlock: &model.Block{}, - curTime: 1572246820, }, fields: fields{ Signature: &mockSignature{}, @@ -3783,7 +3777,6 @@ func TestBlockService_ValidateBlock(t *testing.T) { args: args{ block: mockValidateBlockSuccess, previousLastBlock: &model.Block{}, - curTime: mockValidateBlockSuccess.Timestamp, }, fields: fields{ Signature: &mockSignature{}, @@ -3814,7 +3807,7 @@ func TestBlockService_ValidateBlock(t *testing.T) { Observer: tt.fields.Observer, Logger: tt.fields.Logger, } - if err := bs.ValidateBlock(tt.args.block, tt.args.previousLastBlock, tt.args.curTime); (err != nil) != tt.wantErr { + if err := bs.ValidateBlock(tt.args.block, tt.args.previousLastBlock); (err != nil) != tt.wantErr { t.Errorf("BlockService.ValidateBlock() error = %v, wantErr %v", err, tt.wantErr) } }) diff --git a/core/service/blockSpineService.go b/core/service/blockSpineService.go index 0dd694c6e..e3ba2c195 100644 --- a/core/service/blockSpineService.go +++ b/core/service/blockSpineService.go @@ -6,7 +6,6 @@ import ( "fmt" "math/big" "sync" - "time" log "github.com/sirupsen/logrus" "github.com/zoobc/zoobc-core/common/blocker" @@ -208,16 +207,12 @@ func (bs *BlockSpineService) ValidatePayloadHash(block *model.Block) error { } // ValidateBlock validate block to be pushed into the blockchain -func (bs *BlockSpineService) ValidateBlock(block, previousLastBlock *model.Block, curTime int64) error { +func (bs *BlockSpineService) ValidateBlock(block, previousLastBlock *model.Block) error { // validate block's payload data if err := bs.ValidatePayloadHash(block); err != nil { return err } - // todo: validate previous time - if block.GetTimestamp() > curTime+constant.GenerateBlockTimeoutSec { - return blocker.NewBlocker(blocker.BlockErr, "InvalidTimestamp") - } // check if blocksmith can smith at the time blocksmithsMap := bs.BlocksmithStrategy.GetSortedBlocksmithsMap(previousLastBlock) blocksmithIndex := blocksmithsMap[string(block.BlocksmithPublicKey)] @@ -716,7 +711,7 @@ func (bs *BlockSpineService) ReceiveBlock( if err != nil { return err } - err = bs.ValidateBlock(block, previousBlock, time.Now().Unix()) + err = bs.ValidateBlock(block, previousBlock) if err != nil { errPushBlock := bs.PushBlock(previousBlock, lastBlocks[0], false, true) if errPushBlock != nil { @@ -760,7 +755,7 @@ func (bs *BlockSpineService) ReceiveBlock( ) } // Validate incoming block - err = bs.ValidateBlock(block, lastBlock, time.Now().Unix()) + err = bs.ValidateBlock(block, lastBlock) if err != nil { return status.Error(codes.InvalidArgument, "InvalidBlock") } diff --git a/core/service/blockSpineService_test.go b/core/service/blockSpineService_test.go index 03c68d7ee..9fe8722ac 100644 --- a/core/service/blockSpineService_test.go +++ b/core/service/blockSpineService_test.go @@ -2903,7 +2903,6 @@ func TestBlockSpineService_ValidateBlock(t *testing.T) { type args struct { block *model.Block previousLastBlock *model.Block - curTime int64 } tests := []struct { name string @@ -2917,7 +2916,6 @@ func TestBlockSpineService_ValidateBlock(t *testing.T) { block: &model.Block{ Timestamp: 1572246820 + constant.GenerateBlockTimeoutSec + 1, }, - curTime: 1572246820, }, fields: fields{}, wantErr: true, @@ -2930,7 +2928,6 @@ func TestBlockSpineService_ValidateBlock(t *testing.T) { BlockSignature: []byte{}, BlocksmithPublicKey: []byte{}, }, - curTime: 1572246820, }, fields: fields{ Signature: &mockSpineSignatureFail{}, @@ -2941,8 +2938,7 @@ func TestBlockSpineService_ValidateBlock(t *testing.T) { { name: "ValidateBlock:fail-{InvalidSignature}", args: args{ - block: mockSpineValidateBadBlockInvalidBlockHash, - curTime: 1572246820, + block: mockSpineValidateBadBlockInvalidBlockHash, }, fields: fields{ Signature: &mockSpineSignatureFail{}, @@ -2955,7 +2951,6 @@ func TestBlockSpineService_ValidateBlock(t *testing.T) { args: args{ block: mockSpineValidateBadBlockInvalidBlockHash, previousLastBlock: &model.Block{}, - curTime: 1572246820, }, fields: fields{ Signature: &mockSpineSignature{}, @@ -2975,7 +2970,6 @@ func TestBlockSpineService_ValidateBlock(t *testing.T) { CumulativeDifficulty: "10", }, previousLastBlock: &model.Block{}, - curTime: 1572246820, }, fields: fields{ Signature: &mockSpineSignature{}, @@ -2990,7 +2984,6 @@ func TestBlockSpineService_ValidateBlock(t *testing.T) { args: args{ block: mockSpineValidateBlockSuccess, previousLastBlock: &model.Block{}, - curTime: mockSpineValidateBlockSuccess.Timestamp, }, fields: fields{ Signature: &mockSpineSignature{}, @@ -3011,7 +3004,7 @@ func TestBlockSpineService_ValidateBlock(t *testing.T) { Observer: tt.fields.Observer, Logger: tt.fields.Logger, } - if err := bs.ValidateBlock(tt.args.block, tt.args.previousLastBlock, tt.args.curTime); (err != nil) != tt.wantErr { + if err := bs.ValidateBlock(tt.args.block, tt.args.previousLastBlock); (err != nil) != tt.wantErr { t.Errorf("BlockSpineService.ValidateBlock() error = %v, wantErr %v", err, tt.wantErr) } }) diff --git a/core/smith/blockchainProcessor.go b/core/smith/blockchainProcessor.go index d6e7b57ef..7385a2e78 100644 --- a/core/smith/blockchainProcessor.go +++ b/core/smith/blockchainProcessor.go @@ -105,7 +105,7 @@ func (bp *BlockchainProcessor) FakeSmithing(numberOfBlocks int, fromGenesis bool return err } // validate - err = bp.BlockService.ValidateBlock(block, previousBlock, timeNow) // err / !err + err = bp.BlockService.ValidateBlock(block, previousBlock) // err / !err if err != nil { return err } @@ -150,7 +150,7 @@ func (bp *BlockchainProcessor) StartSmithing() error { return err } // validate - err = bp.BlockService.ValidateBlock(block, lastBlock, timestamp) + err = bp.BlockService.ValidateBlock(block, lastBlock) if err != nil { return err }