Skip to content

Commit

Permalink
refactor clock to timestamp for #136
Browse files Browse the repository at this point in the history
  • Loading branch information
shisoft committed Oct 3, 2018
1 parent 5adfab6 commit ee42936
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 53 deletions.
3 changes: 1 addition & 2 deletions accounts/keystore/accountmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"math/big"
"testing"

"github.com/facebookgo/clock"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -131,7 +130,7 @@ func TestAccountManager_SignHash(t *testing.T) {
require := require.New(t)
m := NewMemAccountManager()

blk := blockchain.NewBlock(1, 0, hash.ZeroHash32B, clock.New(), nil, nil, nil)
blk := blockchain.NewBlock(1, 0, hash.ZeroHash32B, testutil.TimestampNow(), nil, nil, nil)
hash := blk.HashBlock()

signature, err := m.SignHash(rawAddr1, hash[:])
Expand Down
3 changes: 2 additions & 1 deletion accounts/keystore/single_accountmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package keystore

import (
"encoding/json"
"github.com/iotexproject/iotex-core/testutil"
"math/big"
"testing"

Expand Down Expand Up @@ -86,7 +87,7 @@ func TestSingleAccountManager_SignHash(t *testing.T) {
m, err := NewSingleAccountManager(accountManager)
require.NoError(err)

blk := blockchain.NewBlock(1, 0, hash.ZeroHash32B, clock.New(), nil, nil, nil)
blk := blockchain.NewBlock(1, 0, hash.ZeroHash32B, testutil.TimestampNow(), nil, nil, nil)
hash := blk.HashBlock()
signature, err := m.SignHash(hash[:])
require.NoError(err)
Expand Down
6 changes: 3 additions & 3 deletions blockchain/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type BlockHeader struct {
version uint32 // version
chainID uint32 // this chain's ID
height uint64 // block height
timestamp uint64 // unix timestamp
timestamp uint64 // unix now
prevBlockHash hash.Hash32B // hash of previous block
txRoot hash.Hash32B // merkle root of all transactions
stateRoot hash.Hash32B // root of state trie
Expand Down Expand Up @@ -71,7 +71,7 @@ func NewBlock(
chainID uint32,
height uint64,
prevBlockHash hash.Hash32B,
c clock.Clock,
timestamp uint64,
tsf []*action.Transfer,
vote []*action.Vote,
executions []*action.Execution) *Block {
Expand All @@ -80,7 +80,7 @@ func NewBlock(
version: version.ProtocolVersion,
chainID: chainID,
height: height,
timestamp: uint64(c.Now().Unix()),
timestamp: timestamp,
prevBlockHash: prevBlockHash,
txRoot: hash.ZeroHash32B,
stateRoot: hash.ZeroHash32B,
Expand Down
37 changes: 18 additions & 19 deletions blockchain/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"strings"
"testing"

"github.com/facebookgo/clock"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"
"golang.org/x/crypto/blake2b"
Expand Down Expand Up @@ -124,7 +123,7 @@ func TestMerkle(t *testing.T) {
0,
0,
hash.ZeroHash32B,
clock.New(),
testutil.TimestampNow(),
[]*action.Transfer{cbtsf0, cbtsf1, cbtsf2, cbtsf3, cbtsf4},
nil,
nil,
Expand Down Expand Up @@ -201,7 +200,7 @@ func TestWrongRootHash(t *testing.T) {
require.NoError(err)
require.NoError(action.Sign(tsf2, ta.Addrinfo["producer"].PrivateKey))
hash := tsf1.Hash()
blk := NewBlock(1, 1, hash, clock.New(), []*action.Transfer{tsf1, tsf2}, nil, nil)
blk := NewBlock(1, 1, hash, timestamp.Now(), []*action.Transfer{tsf1, tsf2}, nil, nil)
blk.Header.Pubkey = ta.Addrinfo["producer"].PublicKey
blkHash := blk.HashBlock()
blk.Header.blockSig = crypto.EC283.Sign(ta.Addrinfo["producer"].PrivateKey, blkHash[:])
Expand All @@ -220,7 +219,7 @@ func TestSignBlock(t *testing.T) {
require.NoError(err)
require.NoError(action.Sign(tsf2, ta.Addrinfo["producer"].PrivateKey))
hash := tsf1.Hash()
blk := NewBlock(1, 3, hash, clock.New(), []*action.Transfer{tsf1, tsf2}, nil, nil)
blk := NewBlock(1, 3, hash, timestamp.Now(), []*action.Transfer{tsf1, tsf2}, nil, nil)
err = blk.SignBlock(ta.Addrinfo["producer"])
require.Nil(err)
require.Nil(val.Validate(blk, 2, hash, true))
Expand Down Expand Up @@ -251,7 +250,7 @@ func TestWrongNonce(t *testing.T) {
require.NoError(err)
require.NoError(action.Sign(tsf1, ta.Addrinfo["producer"].PrivateKey))
hash := tsf1.Hash()
blk := NewBlock(chainID, 3, hash, clock.New(), []*action.Transfer{coinbaseTsf, tsf1}, nil, nil)
blk := NewBlock(chainID, 3, hash, timestamp.Now(), []*action.Transfer{coinbaseTsf, tsf1}, nil, nil)
err = blk.SignBlock(ta.Addrinfo["producer"])
require.NoError(err)
require.Nil(val.Validate(blk, 2, hash, true))
Expand All @@ -264,7 +263,7 @@ func TestWrongNonce(t *testing.T) {
require.NoError(err)
require.NoError(action.Sign(tsf2, ta.Addrinfo["producer"].PrivateKey))
hash = tsf1.Hash()
blk = NewBlock(chainID, 3, hash, clock.New(), []*action.Transfer{coinbaseTsf, tsf1, tsf2}, nil, nil)
blk = NewBlock(chainID, 3, hash, timestamp.Now(), []*action.Transfer{coinbaseTsf, tsf1, tsf2}, nil, nil)
err = blk.SignBlock(ta.Addrinfo["producer"])
require.NoError(err)
err = val.Validate(blk, 2, hash, true)
Expand All @@ -274,7 +273,7 @@ func TestWrongNonce(t *testing.T) {
require.NoError(err)
require.NoError(action.Sign(vote, ta.Addrinfo["producer"].PrivateKey))
hash = tsf1.Hash()
blk = NewBlock(chainID, 3, hash, clock.New(), []*action.Transfer{coinbaseTsf}, []*action.Vote{vote}, nil)
blk = NewBlock(chainID, 3, hash, timestamp.Now(), []*action.Transfer{coinbaseTsf}, []*action.Vote{vote}, nil)
err = blk.SignBlock(ta.Addrinfo["producer"])
require.NoError(err)
err = val.Validate(blk, 2, hash, true)
Expand All @@ -289,7 +288,7 @@ func TestWrongNonce(t *testing.T) {
require.NoError(err)
require.NoError(action.Sign(tsf4, ta.Addrinfo["producer"].PrivateKey))
hash = tsf1.Hash()
blk = NewBlock(chainID, 3, hash, clock.New(), []*action.Transfer{coinbaseTsf, tsf3, tsf4}, nil, nil)
blk = NewBlock(chainID, 3, hash, timestamp.Now(), []*action.Transfer{coinbaseTsf, tsf3, tsf4}, nil, nil)
err = blk.SignBlock(ta.Addrinfo["producer"])
require.NoError(err)
err = val.Validate(blk, 2, hash, true)
Expand All @@ -303,7 +302,7 @@ func TestWrongNonce(t *testing.T) {
require.NoError(err)
require.NoError(action.Sign(vote3, ta.Addrinfo["producer"].PrivateKey))
hash = tsf1.Hash()
blk = NewBlock(chainID, 3, hash, clock.New(), []*action.Transfer{coinbaseTsf}, []*action.Vote{vote2, vote3}, nil)
blk = NewBlock(chainID, 3, hash, timestamp.Now(), []*action.Transfer{coinbaseTsf}, []*action.Vote{vote2, vote3}, nil)
err = blk.SignBlock(ta.Addrinfo["producer"])
require.NoError(err)
err = val.Validate(blk, 2, hash, true)
Expand All @@ -318,7 +317,7 @@ func TestWrongNonce(t *testing.T) {
require.NoError(err)
require.NoError(action.Sign(tsf6, ta.Addrinfo["producer"].PrivateKey))
hash = tsf1.Hash()
blk = NewBlock(chainID, 3, hash, clock.New(), []*action.Transfer{coinbaseTsf, tsf5, tsf6}, nil, nil)
blk = NewBlock(chainID, 3, hash, timestamp.Now(), []*action.Transfer{coinbaseTsf, tsf5, tsf6}, nil, nil)
err = blk.SignBlock(ta.Addrinfo["producer"])
require.NoError(err)
err = val.Validate(blk, 2, hash, true)
Expand All @@ -332,7 +331,7 @@ func TestWrongNonce(t *testing.T) {
require.NoError(err)
require.NoError(action.Sign(vote5, ta.Addrinfo["producer"].PrivateKey))
hash = tsf1.Hash()
blk = NewBlock(chainID, 3, hash, clock.New(), []*action.Transfer{coinbaseTsf}, []*action.Vote{vote4, vote5}, nil)
blk = NewBlock(chainID, 3, hash, timestamp.Now(), []*action.Transfer{coinbaseTsf}, []*action.Vote{vote4, vote5}, nil)
err = blk.SignBlock(ta.Addrinfo["producer"])
require.NoError(err)
err = val.Validate(blk, 2, hash, true)
Expand Down Expand Up @@ -362,7 +361,7 @@ func TestWrongCoinbaseTsf(t *testing.T) {
require.NoError(err)
require.NoError(action.Sign(tsf1, ta.Addrinfo["producer"].PrivateKey))
hash := tsf1.Hash()
blk := NewBlock(1, 3, hash, clock.New(), []*action.Transfer{tsf1}, nil, nil)
blk := NewBlock(1, 3, hash, timestamp.Now(), []*action.Transfer{tsf1}, nil, nil)
err = blk.SignBlock(ta.Addrinfo["producer"])
require.NoError(err)
err = val.Validate(blk, 2, hash, true)
Expand All @@ -372,7 +371,7 @@ func TestWrongCoinbaseTsf(t *testing.T) {
)

// extra coinbase transfer
blk = NewBlock(1, 3, hash, clock.New(), []*action.Transfer{coinbaseTsf, coinbaseTsf, tsf1}, nil, nil)
blk = NewBlock(1, 3, hash, timestamp.Now(), []*action.Transfer{coinbaseTsf, coinbaseTsf, tsf1}, nil, nil)
err = blk.SignBlock(ta.Addrinfo["producer"])
require.NoError(err)
err = val.Validate(blk, 2, hash, true)
Expand All @@ -382,7 +381,7 @@ func TestWrongCoinbaseTsf(t *testing.T) {
)

// no transfer
blk = NewBlock(1, 3, hash, clock.New(), []*action.Transfer{}, nil, nil)
blk = NewBlock(1, 3, hash, timestamp.Now(), []*action.Transfer{}, nil, nil)
err = blk.SignBlock(ta.Addrinfo["producer"])
require.NoError(err)
err = val.Validate(blk, 2, hash, true)
Expand All @@ -397,23 +396,23 @@ func TestWrongAddress(t *testing.T) {
invalidRecipient := "io1qyqsyqcyq5narhapakcsrhksfajfcpl24us3xp38zwvsep"
tsf, err := action.NewTransfer(1, big.NewInt(1), ta.Addrinfo["producer"].RawAddress, invalidRecipient, []byte{}, uint64(100000), big.NewInt(10))
require.NoError(t, err)
blk1 := NewBlock(1, 3, hash.ZeroHash32B, clock.New(), []*action.Transfer{tsf}, nil, nil)
blk1 := NewBlock(1, 3, hash.ZeroHash32B, timestamp.Now(), []*action.Transfer{tsf}, nil, nil)
err = val.verifyActions(blk1, true)
require.Error(t, err)
require.True(t, strings.Contains(err.Error(), "failed to validate transfer recipient's address"))

invalidVotee := "ioaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
vote, err := action.NewVote(1, ta.Addrinfo["producer"].RawAddress, invalidVotee, uint64(100000), big.NewInt(10))
require.NoError(t, err)
blk2 := NewBlock(1, 3, hash.ZeroHash32B, clock.New(), nil, []*action.Vote{vote}, nil)
blk2 := NewBlock(1, 3, hash.ZeroHash32B, timestamp.Now(), nil, []*action.Vote{vote}, nil)
err = val.verifyActions(blk2, true)
require.Error(t, err)
require.True(t, strings.Contains(err.Error(), "failed to validate votee's address"))

invalidContract := "123"
execution, err := action.NewExecution(ta.Addrinfo["producer"].RawAddress, invalidContract, 1, big.NewInt(1), uint64(100000), big.NewInt(10), []byte{})
require.NoError(t, err)
blk3 := NewBlock(1, 3, hash.ZeroHash32B, clock.New(), nil, nil, []*action.Execution{execution})
blk3 := NewBlock(1, 3, hash.ZeroHash32B, timestamp.Now(), nil, nil, []*action.Execution{execution})
err = val.verifyActions(blk3, true)
require.Error(t, err)
require.True(t, strings.Contains(err.Error(), "failed to validate contract's address"))
Expand Down Expand Up @@ -487,7 +486,7 @@ func TestValidateSecretBlock(t *testing.T) {
secretWitness, err := action.NewSecretWitness(uint64(22), delegates[0], witness)
require.NoError(err)
hash := secretProposals[0].Hash()
blk := NewSecretBlock(1, 3, hash, clock.New(), secretProposals, secretWitness)
blk := NewSecretBlock(1, 3, hash, timestamp.Now(), secretProposals, secretWitness)
err = blk.SignBlock(ta.Addrinfo["producer"])
require.NoError(err)

Expand All @@ -498,7 +497,7 @@ func TestValidateSecretBlock(t *testing.T) {
dummySecretProposal, err := action.NewSecretProposal(2, delegates[0], delegates[1], []uint32{1, 2, 3, 4, 5})
require.NoError(err)
secretProposals[1] = dummySecretProposal
blk = NewSecretBlock(1, 3, hash, clock.New(), secretProposals, secretWitness)
blk = NewSecretBlock(1, 3, hash, timestamp.Now(), secretProposals, secretWitness)
err = blk.SignBlock(ta.Addrinfo["producer"])
require.NoError(err)
err = val.Validate(blk, 2, hash, false)
Expand Down
10 changes: 7 additions & 3 deletions blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ func (bc *blockchain) MintNewBlock(tsf []*action.Transfer, vote []*action.Vote,
defer bc.mu.RUnlock()

tsf = append(tsf, action.NewCoinBaseTransfer(big.NewInt(int64(bc.genesis.BlockReward)), producer.RawAddress))
blk := NewBlock(bc.config.Chain.ID, bc.tipHeight+1, bc.tipHash, bc.clk, tsf, vote, executions)
blk := NewBlock(bc.config.Chain.ID, bc.tipHeight+1, bc.tipHash, bc.now(), tsf, vote, executions)
blk.Header.DKGID = []byte{}
blk.Header.DKGPubkey = []byte{}
blk.Header.DKGBlockSig = []byte{}
Expand All @@ -639,7 +639,7 @@ func (bc *blockchain) MintNewDKGBlock(tsf []*action.Transfer, vote []*action.Vot
defer bc.mu.RUnlock()

tsf = append(tsf, action.NewCoinBaseTransfer(big.NewInt(int64(bc.genesis.BlockReward)), producer.RawAddress))
blk := NewBlock(bc.config.Chain.ID, bc.tipHeight+1, bc.tipHash, bc.clk, tsf, vote, executions)
blk := NewBlock(bc.config.Chain.ID, bc.tipHeight+1, bc.tipHash, bc.now(), tsf, vote, executions)
blk.Header.DKGID = []byte{}
blk.Header.DKGPubkey = []byte{}
blk.Header.DKGBlockSig = []byte{}
Expand Down Expand Up @@ -690,7 +690,7 @@ func (bc *blockchain) MintNewDummyBlock() *Block {
bc.mu.RLock()
defer bc.mu.RUnlock()

blk := NewBlock(bc.config.Chain.ID, bc.tipHeight+1, bc.tipHash, bc.clk, nil, nil, nil)
blk := NewBlock(bc.config.Chain.ID, bc.tipHeight+1, bc.tipHash, bc.now(), nil, nil, nil)
blk.Header.Pubkey = keypair.ZeroPublicKey
blk.Header.blockSig = []byte{}

Expand Down Expand Up @@ -845,3 +845,7 @@ func (bc *blockchain) replaceHeightAndHash(blk *Block) (uint64, hash.Hash32B, er
}
return tipHeight, tipHash, nil
}

func (bc *blockchain) now() uint64 {
return uint64(bc.clk.Now().Unix())
}
8 changes: 4 additions & 4 deletions blockchain/blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,15 +307,15 @@ func TestLoadBlockchainfromDB(t *testing.T) {
// add block with wrong height
cbTsf := action.NewCoinBaseTransfer(big.NewInt(50), ta.Addrinfo["bravo"].RawAddress)
require.NotNil(cbTsf)
blk = NewBlock(0, h+2, hash, clock.New(), []*action.Transfer{cbTsf}, nil, nil)
blk = NewBlock(0, h+2, hash, testutil.TimestampNow(), []*action.Transfer{cbTsf}, nil, nil)
err = bc.ValidateBlock(blk, true)
require.NotNil(err)
fmt.Printf("Cannot validate block %d: %v\n", blk.Height(), err)

// add block with zero prev hash
cbTsf2 := action.NewCoinBaseTransfer(big.NewInt(50), ta.Addrinfo["bravo"].RawAddress)
require.NotNil(cbTsf2)
blk = NewBlock(0, h+1, _hash.ZeroHash32B, clock.New(), []*action.Transfer{cbTsf2}, nil, nil)
blk = NewBlock(0, h+1, _hash.ZeroHash32B, testutil.TimestampNow(), []*action.Transfer{cbTsf2}, nil, nil)
err = bc.ValidateBlock(blk, true)
require.NotNil(err)
fmt.Printf("Cannot validate block %d: %v\n", blk.Height(), err)
Expand Down Expand Up @@ -488,14 +488,14 @@ func TestLoadBlockchainfromDBWithoutExplorer(t *testing.T) {
// add block with wrong height
cbTsf := action.NewCoinBaseTransfer(big.NewInt(50), ta.Addrinfo["bravo"].RawAddress)
require.NotNil(cbTsf)
blk = NewBlock(0, h+2, hash, clock.New(), []*action.Transfer{cbTsf}, nil, nil)
blk = NewBlock(0, h+2, hash, testutil.TimestampNow(), []*action.Transfer{cbTsf}, nil, nil)
err = bc.ValidateBlock(blk, true)
require.NotNil(err)
fmt.Printf("Cannot validate block %d: %v\n", blk.Height(), err)
// add block with zero prev hash
cbTsf2 := action.NewCoinBaseTransfer(big.NewInt(50), ta.Addrinfo["bravo"].RawAddress)
require.NotNil(cbTsf2)
blk = NewBlock(0, h+1, _hash.ZeroHash32B, clock.New(), []*action.Transfer{cbTsf2}, nil, nil)
blk = NewBlock(0, h+1, _hash.ZeroHash32B, testutil.TimestampNow(), []*action.Transfer{cbTsf2}, nil, nil)
err = bc.ValidateBlock(blk, true)
require.NotNil(err)
fmt.Printf("Cannot validate block %d: %v\n", blk.Height(), err)
Expand Down
7 changes: 3 additions & 4 deletions blockchain/blockdao_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"math/rand"
"testing"

"github.com/facebookgo/clock"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -55,13 +54,13 @@ func TestBlockDAO(t *testing.T) {

hash1 := hash.Hash32B{}
fnv.New32().Sum(hash1[:])
blk1 := NewBlock(0, 1, hash1, clock.New(), []*action.Transfer{cbTsf1}, []*action.Vote{vote1}, []*action.Execution{execution1})
blk1 := NewBlock(0, 1, hash1, testutil.TimestampNow(), []*action.Transfer{cbTsf1}, []*action.Vote{vote1}, []*action.Execution{execution1})
hash2 := hash.Hash32B{}
fnv.New32().Sum(hash2[:])
blk2 := NewBlock(0, 2, hash2, clock.New(), []*action.Transfer{cbTsf2}, []*action.Vote{vote2}, []*action.Execution{execution2})
blk2 := NewBlock(0, 2, hash2, testutil.TimestampNow(), []*action.Transfer{cbTsf2}, []*action.Vote{vote2}, []*action.Execution{execution2})
hash3 := hash.Hash32B{}
fnv.New32().Sum(hash3[:])
blk3 := NewBlock(0, 3, hash3, clock.New(), []*action.Transfer{cbTsf3}, []*action.Vote{vote3}, []*action.Execution{execution3})
blk3 := NewBlock(0, 3, hash3, testutil.TimestampNow(), []*action.Transfer{cbTsf3}, []*action.Vote{vote3}, []*action.Execution{execution3})
return []*Block{blk1, blk2, blk3}
}

Expand Down
4 changes: 2 additions & 2 deletions blocksync/blocksync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func TestNewBlockSyncer(t *testing.T) {
// TipHeight return ERROR
mBc.EXPECT().TipHeight().AnyTimes().Return(uint64(0))
mBc.EXPECT().ChainID().AnyTimes().Return(config.Default.Chain.ID)
blk := bc.NewBlock(uint32(123), uint64(0), hash.Hash32B{}, clock.New(), nil, nil, nil)
blk := bc.NewBlock(uint32(123), uint64(0), hash.Hash32B{}, testutil.TimestampNow(), nil, nil, nil)
mBc.EXPECT().GetBlockByHeight(gomock.Any()).AnyTimes().Return(blk, nil)

cfg, err := newTestConfig()
Expand Down Expand Up @@ -162,7 +162,7 @@ func TestBlockSyncerProcessSyncRequest(t *testing.T) {

mBc := mock_blockchain.NewMockBlockchain(ctrl)
mBc.EXPECT().ChainID().AnyTimes().Return(config.Default.Chain.ID)
blk := bc.NewBlock(uint32(123), uint64(0), hash.Hash32B{}, clock.New(), nil, nil, nil)
blk := bc.NewBlock(uint32(123), uint64(0), hash.Hash32B{}, testutil.TimestampNow(), nil, nil, nil)
mBc.EXPECT().GetBlockByHeight(gomock.Any()).AnyTimes().Return(blk, nil)
mBc.EXPECT().TipHeight().AnyTimes().Return(uint64(0))
cfg, err := newTestConfig()
Expand Down
Loading

0 comments on commit ee42936

Please sign in to comment.