Skip to content
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

use time.Time as block time #782

Merged
merged 1 commit into from
Mar 20, 2019
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
3 changes: 2 additions & 1 deletion action/protocol/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package protocol
import (
"context"
"math/big"
"time"

"github.com/iotexproject/iotex-core/address"
"github.com/iotexproject/iotex-core/pkg/hash"
Expand All @@ -24,7 +25,7 @@ type RunActionsCtx struct {
// height of block containing those actions
BlockHeight uint64
// timestamp of block containing those actions
BlockTimeStamp int64
BlockTimeStamp time.Time
// gas Limit for perform those actions
GasLimit uint64
// Producer is the address of whom composes the block containing this action
Expand Down
2 changes: 1 addition & 1 deletion action/protocol/execution/evm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func NewParams(raCtx protocol.RunActionsCtx, execution *action.Execution, stateD
Origin: executorAddr,
Coinbase: producer,
BlockNumber: new(big.Int).SetUint64(raCtx.BlockHeight),
Time: new(big.Int).SetInt64(raCtx.BlockTimeStamp),
Time: new(big.Int).SetInt64(raCtx.BlockTimeStamp.Unix()),
Difficulty: new(big.Int).SetUint64(uint64(50)),
GasLimit: gasLimit,
GasPrice: execution.GasPrice(),
Expand Down
26 changes: 13 additions & 13 deletions action/protocol/execution/protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func runExecution(
actionMap[ecfg.executor] = []action.SealedEnvelope{selp}
blk, err := bc.MintNewBlock(
actionMap,
0,
testutil.TimestampNow(),
)
if err != nil {
return nil, err
Expand Down Expand Up @@ -277,7 +277,7 @@ func TestProtocol_Handle(t *testing.T) {
actionMap[testaddress.Addrinfo["producer"].String()] = []action.SealedEnvelope{selp}
blk, err := bc.MintNewBlock(
actionMap,
0,
testutil.TimestampNow(),
)
require.NoError(err)
require.NoError(bc.ValidateBlock(blk))
Expand Down Expand Up @@ -330,7 +330,7 @@ func TestProtocol_Handle(t *testing.T) {
actionMap[testaddress.Addrinfo["producer"].String()] = []action.SealedEnvelope{selp}
blk, err = bc.MintNewBlock(
actionMap,
0,
testutil.TimestampNow(),
)
require.NoError(err)
require.NoError(bc.ValidateBlock(blk))
Expand Down Expand Up @@ -365,7 +365,7 @@ func TestProtocol_Handle(t *testing.T) {
actionMap[testaddress.Addrinfo["producer"].String()] = []action.SealedEnvelope{selp}
blk, err = bc.MintNewBlock(
actionMap,
0,
testutil.TimestampNow(),
)
require.NoError(err)
require.NoError(bc.ValidateBlock(blk))
Expand All @@ -391,7 +391,7 @@ func TestProtocol_Handle(t *testing.T) {
actionMap[testaddress.Addrinfo["producer"].String()] = []action.SealedEnvelope{selp}
blk, err = bc.MintNewBlock(
actionMap,
0,
testutil.TimestampNow(),
)
require.NoError(err)
require.NoError(bc.ValidateBlock(blk))
Expand Down Expand Up @@ -470,7 +470,7 @@ func TestProtocol_Handle(t *testing.T) {
actionMap[testaddress.Addrinfo["producer"].String()] = []action.SealedEnvelope{selp}
blk, err := bc.MintNewBlock(
actionMap,
0,
testutil.TimestampNow(),
)
require.NoError(err)
require.NoError(bc.ValidateBlock(blk))
Expand All @@ -497,7 +497,7 @@ func TestProtocol_Handle(t *testing.T) {
actionMap[testaddress.Addrinfo["producer"].String()] = []action.SealedEnvelope{selp}
blk, err = bc.MintNewBlock(
actionMap,
0,
testutil.TimestampNow(),
)
require.NoError(err)
require.NoError(bc.ValidateBlock(blk))
Expand Down Expand Up @@ -525,7 +525,7 @@ func TestProtocol_Handle(t *testing.T) {
actionMap[testaddress.Addrinfo["producer"].String()] = []action.SealedEnvelope{selp}
blk, err = bc.MintNewBlock(
actionMap,
0,
testutil.TimestampNow(),
)
require.NoError(err)
require.NoError(bc.ValidateBlock(blk))
Expand Down Expand Up @@ -559,7 +559,7 @@ func TestProtocol_Handle(t *testing.T) {
actionMap[testaddress.Addrinfo["bravo"].String()] = []action.SealedEnvelope{selp}
blk, err = bc.MintNewBlock(
actionMap,
0,
testutil.TimestampNow(),
)
require.NoError(err)
require.NoError(bc.ValidateBlock(blk))
Expand Down Expand Up @@ -640,7 +640,7 @@ func TestProtocol_Handle(t *testing.T) {
actionMap[testaddress.Addrinfo["producer"].String()] = []action.SealedEnvelope{selp}
blk, err := bc.MintNewBlock(
actionMap,
0,
testutil.TimestampNow(),
)
require.NoError(err)
require.NoError(bc.ValidateBlock(blk))
Expand Down Expand Up @@ -710,7 +710,7 @@ func TestProtocol_Handle(t *testing.T) {
actionMap[testaddress.Addrinfo["producer"].String()] = []action.SealedEnvelope{selp, selp2}
blk, err = bc.MintNewBlock(
actionMap,
0,
testutil.TimestampNow(),
)
require.NoError(err)
require.NoError(bc.ValidateBlock(blk))
Expand Down Expand Up @@ -739,7 +739,7 @@ func TestProtocol_Handle(t *testing.T) {
actionMap[testaddress.Addrinfo["alfa"].String()] = []action.SealedEnvelope{selp3}
blk, err = bc.MintNewBlock(
actionMap,
0,
testutil.TimestampNow(),
)
require.NoError(err)
require.NoError(bc.ValidateBlock(blk))
Expand All @@ -762,7 +762,7 @@ func TestProtocol_Handle(t *testing.T) {
actionMap[testaddress.Addrinfo["producer"].String()] = []action.SealedEnvelope{selp}
blk, err = bc.MintNewBlock(
actionMap,
0,
testutil.TimestampNow(),
)
require.NoError(err)
require.NoError(bc.ValidateBlock(blk))
Expand Down
8 changes: 4 additions & 4 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -930,7 +930,7 @@ func addTestingBlocks(bc blockchain.Blockchain) error {
actionMap[addr0] = []action.SealedEnvelope{tsf}
blk, err := bc.MintNewBlock(
actionMap,
time.Now().Unix(),
testutil.TimestampNow(),
)
if err != nil {
return err
Expand Down Expand Up @@ -971,7 +971,7 @@ func addTestingBlocks(bc blockchain.Blockchain) error {
actionMap[addr3] = selps
if blk, err = bc.MintNewBlock(
actionMap,
time.Now().Unix(),
testutil.TimestampNow(),
); err != nil {
return err
}
Expand All @@ -986,7 +986,7 @@ func addTestingBlocks(bc blockchain.Blockchain) error {
// Empty actions
if blk, err = bc.MintNewBlock(
nil,
time.Now().Unix(),
testutil.TimestampNow(),
); err != nil {
return err
}
Expand Down Expand Up @@ -1026,7 +1026,7 @@ func addTestingBlocks(bc blockchain.Blockchain) error {
actionMap[addr1] = []action.SealedEnvelope{vote2, execution2}
if blk, err = bc.MintNewBlock(
actionMap,
time.Now().Unix(),
testutil.TimestampNow(),
); err != nil {
return err
}
Expand Down
7 changes: 6 additions & 1 deletion blockchain/block/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"

"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"
"github.com/pkg/errors"
"go.uber.org/zap"

Expand Down Expand Up @@ -65,7 +66,11 @@ func (b *Block) ConvertFromBlockHeaderPb(pbBlock *iotextypes.Block) error {

b.Header.version = pbBlock.GetHeader().GetCore().GetVersion()
b.Header.height = pbBlock.GetHeader().GetCore().GetHeight()
b.Header.timestamp = pbBlock.GetHeader().GetCore().GetTimestamp().GetSeconds()
ts, err := ptypes.Timestamp(pbBlock.GetHeader().GetCore().GetTimestamp())
if err != nil {
return err
}
b.Header.timestamp = ts
copy(b.Header.prevBlockHash[:], pbBlock.GetHeader().GetCore().GetPrevBlockHash())
copy(b.Header.txRoot[:], pbBlock.GetHeader().GetCore().GetTxRoot())
copy(b.Header.deltaStateDigest[:], pbBlock.GetHeader().GetCore().GetDeltaStateDigest())
Expand Down
23 changes: 10 additions & 13 deletions blockchain/block/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,18 @@ import (
"testing"
"time"

"go.uber.org/zap"

"github.com/iotexproject/iotex-core/pkg/log"

"github.com/iotexproject/iotex-core/pkg/compress"

"github.com/iotexproject/iotex-core/test/identityset"

"github.com/iotexproject/iotex-core/pkg/unit"

"github.com/golang/protobuf/ptypes"
"github.com/stretchr/testify/require"
"go.uber.org/zap"

"github.com/iotexproject/iotex-core/action"
"github.com/iotexproject/iotex-core/pkg/compress"
"github.com/iotexproject/iotex-core/pkg/hash"
"github.com/iotexproject/iotex-core/pkg/log"
"github.com/iotexproject/iotex-core/pkg/unit"
"github.com/iotexproject/iotex-core/pkg/version"
"github.com/iotexproject/iotex-core/protogen/iotextypes"
"github.com/iotexproject/iotex-core/test/identityset"
ta "github.com/iotexproject/iotex-core/test/testaddress"
"github.com/iotexproject/iotex-core/testutil"
)
Expand Down Expand Up @@ -78,8 +74,9 @@ func TestConvertFromBlockPb(t *testing.T) {
require.NoError(t, blk.ConvertFromBlockPb(&iotextypes.Block{
Header: &iotextypes.BlockHeader{
Core: &iotextypes.BlockHeaderCore{
Version: version.ProtocolVersion,
Height: 123456789,
Version: version.ProtocolVersion,
Height: 123456789,
Timestamp: ptypes.TimestampNow(),
},
ProducerPubkey: senderPubKey.Bytes(),
},
Expand Down Expand Up @@ -231,7 +228,7 @@ func makeBlock(tb testing.TB, n int) *Block {
rap := RunnableActionsBuilder{}
ra := rap.
SetHeight(1).
SetTimeStamp(time.Now().Unix()).
SetTimeStamp(time.Now()).
AddActions(sevlps...).
Build(identityset.PrivateKey(0).PublicKey())
blk, err := NewBuilder(ra).
Expand Down
22 changes: 13 additions & 9 deletions blockchain/block/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
package block

import (
"time"

"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes/timestamp"
"github.com/golang/protobuf/ptypes"
"github.com/iotexproject/iotex-core/pkg/hash"
"github.com/iotexproject/iotex-core/pkg/keypair"
"github.com/iotexproject/iotex-core/pkg/log"
Expand All @@ -22,7 +24,7 @@ import (
type Header struct {
version uint32 // version
height uint64 // block height
timestamp int64 // unix timestamp
timestamp time.Time // propose timestamp
prevBlockHash hash.Hash256 // hash of previous block
txRoot hash.Hash256 // merkle root of all transactions
deltaStateDigest hash.Hash256 // digest of state change by this block
Expand All @@ -38,7 +40,7 @@ func (h Header) Version() uint32 { return h.version }
func (h Header) Height() uint64 { return h.height }

// Timestamp returns the Timestamp of this block.
func (h Header) Timestamp() int64 { return h.timestamp }
func (h Header) Timestamp() time.Time { return h.timestamp }

// PrevHash returns the hash of prev block.
func (h Header) PrevHash() hash.Hash256 { return h.prevBlockHash }
Expand Down Expand Up @@ -66,12 +68,14 @@ func (h Header) BlockHeaderProto() *iotextypes.BlockHeader {

// BlockHeaderCoreProto returns BlockHeaderCore proto.
func (h Header) BlockHeaderCoreProto() *iotextypes.BlockHeaderCore {
ts, err := ptypes.TimestampProto(h.timestamp)
if err != nil {
log.L().Panic("failed to cast to ptypes.timestamp", zap.Error(err))
}
return &iotextypes.BlockHeaderCore{
Version: h.version,
Height: h.height,
Timestamp: &timestamp.Timestamp{
Seconds: h.timestamp,
},
Version: h.version,
Height: h.height,
Timestamp: ts,
PrevBlockHash: h.prevBlockHash[:],
TxRoot: h.txRoot[:],
DeltaStateDigest: h.deltaStateDigest[:],
Expand Down Expand Up @@ -105,7 +109,7 @@ func (h Header) HashHeaderCore() hash.Hash256 {
func (h Header) HeaderLogger(l *zap.Logger) *zap.Logger {
return l.With(zap.Uint32("version", h.version),
zap.Uint64("height", h.height),
zap.Int64("timeStamp", h.timestamp),
zap.String("timestamp", h.timestamp.String()),
log.Hex("prevBlockHash", h.prevBlockHash[:]),
log.Hex("txRoot", h.txRoot[:]),
log.Hex("receiptRoot", h.receiptRoot[:]),
Expand Down
8 changes: 5 additions & 3 deletions blockchain/block/runnable.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
package block

import (
"time"

"github.com/iotexproject/iotex-core/action"
"github.com/iotexproject/iotex-core/pkg/hash"
"github.com/iotexproject/iotex-core/pkg/keypair"
Expand All @@ -15,7 +17,7 @@ import (
// RunnableActions is abstructed from block which contains information to execute all actions in a block.
type RunnableActions struct {
blockHeight uint64
blockTimeStamp int64
blockTimeStamp time.Time
blockProducerPubKey keypair.PublicKey
txHash hash.Hash256
actions []action.SealedEnvelope
Expand All @@ -27,7 +29,7 @@ func (ra RunnableActions) BlockHeight() uint64 {
}

// BlockTimeStamp returns blockTimeStamp.
func (ra RunnableActions) BlockTimeStamp() int64 {
func (ra RunnableActions) BlockTimeStamp() time.Time {
return ra.blockTimeStamp
}

Expand Down Expand Up @@ -57,7 +59,7 @@ func (b *RunnableActionsBuilder) SetHeight(h uint64) *RunnableActionsBuilder {
}

// SetTimeStamp sets the time stamp for block which is building.
func (b *RunnableActionsBuilder) SetTimeStamp(ts int64) *RunnableActionsBuilder {
func (b *RunnableActionsBuilder) SetTimeStamp(ts time.Time) *RunnableActionsBuilder {
b.ra.blockTimeStamp = ts
return b
}
Expand Down
6 changes: 4 additions & 2 deletions blockchain/block/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
package block

import (
"time"

"github.com/pkg/errors"

"github.com/iotexproject/iotex-core/action"
Expand Down Expand Up @@ -42,7 +44,7 @@ func (b *TestingBuilder) SetHeight(h uint64) *TestingBuilder {
}

// SetTimeStamp sets the time stamp for block which is building.
func (b *TestingBuilder) SetTimeStamp(ts int64) *TestingBuilder {
func (b *TestingBuilder) SetTimeStamp(ts time.Time) *TestingBuilder {
b.blk.Header.timestamp = ts
return b
}
Expand Down Expand Up @@ -87,7 +89,7 @@ func NewBlockDeprecated(
chainID uint32,
height uint64,
prevBlockHash hash.Hash256,
timestamp int64,
timestamp time.Time,
producer keypair.PublicKey,
actions []action.SealedEnvelope,
) *Block {
Expand Down
Loading