Skip to content

#777 update validate block function signature : remove timestamp #779

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 2 commits into from
Apr 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/blockchainsync/downloadBlockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions core/blockchainsync/processFork.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion core/service/blockCoreService.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 3 additions & 9 deletions core/service/blockMainService.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"reflect"
"strconv"
"sync"
"time"

"github.com/dgraph-io/badger"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -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)]
Expand Down Expand Up @@ -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")
}
Expand All @@ -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")
}
Expand Down
11 changes: 2 additions & 9 deletions core/service/blockMainService_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -3710,7 +3709,6 @@ func TestBlockService_ValidateBlock(t *testing.T) {
block: &model.Block{
Timestamp: 1572246820 + constant.GenerateBlockTimeoutSec + 1,
},
curTime: 1572246820,
},
fields: fields{},
wantErr: true,
Expand All @@ -3723,7 +3721,6 @@ func TestBlockService_ValidateBlock(t *testing.T) {
BlockSignature: []byte{},
BlocksmithPublicKey: []byte{},
},
curTime: 1572246820,
},
fields: fields{
Signature: &mockSignatureFail{},
Expand All @@ -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{},
Expand All @@ -3748,7 +3744,6 @@ func TestBlockService_ValidateBlock(t *testing.T) {
args: args{
block: mockValidateBadBlockInvalidBlockHash,
previousLastBlock: &model.Block{},
curTime: 1572246820,
},
fields: fields{
Signature: &mockSignature{},
Expand All @@ -3768,7 +3763,6 @@ func TestBlockService_ValidateBlock(t *testing.T) {
CumulativeDifficulty: "10",
},
previousLastBlock: &model.Block{},
curTime: 1572246820,
},
fields: fields{
Signature: &mockSignature{},
Expand All @@ -3783,7 +3777,6 @@ func TestBlockService_ValidateBlock(t *testing.T) {
args: args{
block: mockValidateBlockSuccess,
previousLastBlock: &model.Block{},
curTime: mockValidateBlockSuccess.Timestamp,
},
fields: fields{
Signature: &mockSignature{},
Expand Down Expand Up @@ -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)
}
})
Expand Down
11 changes: 3 additions & 8 deletions core/service/blockSpineService.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"math/big"
"sync"
"time"

log "github.com/sirupsen/logrus"
"github.com/zoobc/zoobc-core/common/blocker"
Expand Down Expand Up @@ -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)]
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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")
}
Expand Down
11 changes: 2 additions & 9 deletions core/service/blockSpineService_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -2917,7 +2916,6 @@ func TestBlockSpineService_ValidateBlock(t *testing.T) {
block: &model.Block{
Timestamp: 1572246820 + constant.GenerateBlockTimeoutSec + 1,
},
curTime: 1572246820,
},
fields: fields{},
wantErr: true,
Expand All @@ -2930,7 +2928,6 @@ func TestBlockSpineService_ValidateBlock(t *testing.T) {
BlockSignature: []byte{},
BlocksmithPublicKey: []byte{},
},
curTime: 1572246820,
},
fields: fields{
Signature: &mockSpineSignatureFail{},
Expand All @@ -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{},
Expand All @@ -2955,7 +2951,6 @@ func TestBlockSpineService_ValidateBlock(t *testing.T) {
args: args{
block: mockSpineValidateBadBlockInvalidBlockHash,
previousLastBlock: &model.Block{},
curTime: 1572246820,
},
fields: fields{
Signature: &mockSpineSignature{},
Expand All @@ -2975,7 +2970,6 @@ func TestBlockSpineService_ValidateBlock(t *testing.T) {
CumulativeDifficulty: "10",
},
previousLastBlock: &model.Block{},
curTime: 1572246820,
},
fields: fields{
Signature: &mockSpineSignature{},
Expand All @@ -2990,7 +2984,6 @@ func TestBlockSpineService_ValidateBlock(t *testing.T) {
args: args{
block: mockSpineValidateBlockSuccess,
previousLastBlock: &model.Block{},
curTime: mockSpineValidateBlockSuccess.Timestamp,
},
fields: fields{
Signature: &mockSpineSignature{},
Expand All @@ -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)
}
})
Expand Down
4 changes: 2 additions & 2 deletions core/smith/blockchainProcessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down