Skip to content

Commit

Permalink
Replace ptypes.TimestampProto with timestamppb.New (#3183)
Browse files Browse the repository at this point in the history
  • Loading branch information
LuckyPigeon committed Mar 16, 2022
1 parent 50a49a5 commit bde7db5
Show file tree
Hide file tree
Showing 14 changed files with 59 additions and 91 deletions.
11 changes: 4 additions & 7 deletions action/protocol/poll/blockmeta.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ package poll
import (
"time"

"github.com/golang/protobuf/ptypes"
"github.com/pkg/errors"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/timestamppb"

"github.com/iotexproject/iotex-core/action/protocol/poll/blockmetapb"
)
Expand Down Expand Up @@ -43,10 +43,7 @@ func (bm *BlockMeta) Serialize() ([]byte, error) {

// Proto converts the BlockMeta struct to a protobuf message
func (bm *BlockMeta) Proto() (*blockmetapb.BlockMeta, error) {
blkTime, err := ptypes.TimestampProto(bm.MintTime)
if err != nil {
return nil, err
}
blkTime := timestamppb.New(bm.MintTime)
return &blockmetapb.BlockMeta{
BlockHeight: bm.Height,
BlockProducer: bm.Producer,
Expand All @@ -65,10 +62,10 @@ func (bm *BlockMeta) Deserialize(buf []byte) error {

// LoadProto loads blockMeta from proto
func (bm *BlockMeta) LoadProto(pb *blockmetapb.BlockMeta) error {
mintTime, err := ptypes.Timestamp(pb.GetBlockTime())
if err != nil {
if err := pb.GetBlockTime().CheckValid(); err != nil {
return err
}
mintTime := pb.GetBlockTime().AsTime()
bm.Height = pb.GetBlockHeight()
bm.Producer = pb.GetBlockProducer()
bm.MintTime = mintTime.UTC()
Expand Down
46 changes: 15 additions & 31 deletions action/protocol/staking/vote_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import (
"math/big"
"time"

"github.com/golang/protobuf/ptypes"
"github.com/iotexproject/iotex-address/address"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
"github.com/pkg/errors"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/timestamppb"

"github.com/iotexproject/iotex-core/action/protocol"
"github.com/iotexproject/iotex-core/action/protocol/staking/stakingpb"
Expand Down Expand Up @@ -87,18 +87,20 @@ func (vb *VoteBucket) fromProto(pb *stakingpb.Bucket) error {
return err
}

createTime, err := ptypes.Timestamp(pb.GetCreateTime())
if err != nil {
if err := pb.GetCreateTime().CheckValid(); err != nil {
return err
}
stakeTime, err := ptypes.Timestamp(pb.GetStakeStartTime())
if err != nil {
createTime := pb.GetCreateTime().AsTime()

if err := pb.GetStakeStartTime().CheckValid(); err != nil {
return err
}
unstakeTime, err := ptypes.Timestamp(pb.GetUnstakeStartTime())
if err != nil {
stakeTime := pb.GetStakeStartTime().AsTime()

if err := pb.GetUnstakeStartTime().CheckValid(); err != nil {
return err
}
unstakeTime := pb.GetUnstakeStartTime().AsTime()

vb.Index = pb.GetIndex()
vb.Candidate = candAddr
Expand All @@ -116,18 +118,9 @@ func (vb *VoteBucket) toProto() (*stakingpb.Bucket, error) {
if vb.Candidate == nil || vb.Owner == nil || vb.StakedAmount == nil {
return nil, ErrMissingField
}
createTime, err := ptypes.TimestampProto(vb.CreateTime)
if err != nil {
return nil, err
}
stakeTime, err := ptypes.TimestampProto(vb.StakeStartTime)
if err != nil {
return nil, err
}
unstakeTime, err := ptypes.TimestampProto(vb.UnstakeStartTime)
if err != nil {
return nil, err
}
createTime := timestamppb.New(vb.CreateTime)
stakeTime := timestamppb.New(vb.StakeStartTime)
unstakeTime := timestamppb.New(vb.UnstakeStartTime)

return &stakingpb.Bucket{
Index: vb.Index,
Expand All @@ -143,18 +136,9 @@ func (vb *VoteBucket) toProto() (*stakingpb.Bucket, error) {
}

func (vb *VoteBucket) toIoTeXTypes() (*iotextypes.VoteBucket, error) {
createTime, err := ptypes.TimestampProto(vb.CreateTime)
if err != nil {
return nil, err
}
stakeTime, err := ptypes.TimestampProto(vb.StakeStartTime)
if err != nil {
return nil, err
}
unstakeTime, err := ptypes.TimestampProto(vb.UnstakeStartTime)
if err != nil {
return nil, err
}
createTime := timestamppb.New(vb.CreateTime)
stakeTime := timestamppb.New(vb.StakeStartTime)
unstakeTime := timestamppb.New(vb.UnstakeStartTime)

return &iotextypes.VoteBucket{
Index: vb.Index,
Expand Down
12 changes: 5 additions & 7 deletions api/coreservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ import (
"strconv"
"time"

"github.com/golang/protobuf/ptypes"
"github.com/pkg/errors"
"go.uber.org/zap"
"google.golang.org/genproto/googleapis/rpc/errdetails"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/durationpb"
"google.golang.org/protobuf/types/known/timestamppb"

"github.com/iotexproject/go-pkgs/hash"
"github.com/iotexproject/iotex-address/address"
Expand Down Expand Up @@ -671,16 +672,13 @@ func (core *coreService) ElectionBuckets(epochNum uint64) ([]*iotextypes.Electio
}
re := make([]*iotextypes.ElectionBucket, len(buckets))
for i, b := range buckets {
startTime, err := ptypes.TimestampProto(b.StartTime())
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
startTime := timestamppb.New(b.StartTime())
re[i] = &iotextypes.ElectionBucket{
Voter: b.Voter(),
Candidate: b.Candidate(),
Amount: b.Amount().Bytes(),
StartTime: startTime,
Duration: ptypes.DurationProto(b.Duration()),
Duration: durationpb.New(b.Duration()),
Decay: b.Decay(),
}
}
Expand Down Expand Up @@ -1118,7 +1116,7 @@ func (core *coreService) getBlockMetaByHeight(height uint64) (*iotextypes.BlockM
func generateBlockMeta(blk *block.Block) *iotextypes.BlockMeta {
header := blk.Header
height := header.Height()
ts, _ := ptypes.TimestampProto(header.Timestamp())
ts := timestamppb.New(header.Timestamp())
var (
producerAddress string
h hash.Hash256
Expand Down
5 changes: 2 additions & 3 deletions api/grpcserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ import (
"time"

"github.com/golang/mock/gomock"
"github.com/golang/protobuf/ptypes"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/timestamppb"

"github.com/iotexproject/go-pkgs/crypto"
"github.com/iotexproject/go-pkgs/hash"
Expand Down Expand Up @@ -2201,8 +2201,7 @@ func TestGrpcServer_GetRawBlocks(t *testing.T) {
header := blkInfos[0].Block.Header.Core
require.EqualValues(version.ProtocolVersion, header.Version)
require.Zero(header.Height)
ts, err := ptypes.TimestampProto(time.Unix(genesis.Timestamp(), 0))
require.NoError(err)
ts := timestamppb.New(time.Unix(genesis.Timestamp(), 0))
require.Equal(ts, header.Timestamp)
require.Equal(0, bytes.Compare(hash.ZeroHash256[:], header.PrevBlockHash))
require.Equal(0, bytes.Compare(hash.ZeroHash256[:], header.TxRoot))
Expand Down
4 changes: 2 additions & 2 deletions blockchain/block/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import (
"testing"
"time"

"github.com/golang/protobuf/ptypes"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
"google.golang.org/protobuf/types/known/timestamppb"

"github.com/iotexproject/go-pkgs/hash"
"github.com/iotexproject/iotex-core/action"
Expand Down Expand Up @@ -83,7 +83,7 @@ var (
Core: &iotextypes.BlockHeaderCore{
Version: version.ProtocolVersion,
Height: 123456789,
Timestamp: ptypes.TimestampNow(),
Timestamp: timestamppb.Now(),
},
ProducerPubkey: pkBytes,
},
Expand Down
11 changes: 4 additions & 7 deletions blockchain/block/footer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ package block
import (
"time"

"github.com/golang/protobuf/ptypes"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/timestamppb"

"github.com/iotexproject/iotex-core/endorsement"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
Expand All @@ -25,10 +25,7 @@ type Footer struct {
// ConvertToBlockFooterPb converts BlockFooter
func (f *Footer) ConvertToBlockFooterPb() (*iotextypes.BlockFooter, error) {
pb := iotextypes.BlockFooter{}
commitTime, err := ptypes.TimestampProto(f.commitTime)
if err != nil {
return nil, err
}
commitTime := timestamppb.New(f.commitTime)
pb.Timestamp = commitTime
pb.Endorsements = []*iotextypes.Endorsement{}
for _, en := range f.endorsements {
Expand All @@ -46,10 +43,10 @@ func (f *Footer) ConvertFromBlockFooterPb(pb *iotextypes.BlockFooter) error {
if pb == nil {
return nil
}
commitTime, err := ptypes.Timestamp(pb.GetTimestamp())
if err != nil {
if err := pb.GetTimestamp().CheckValid(); err != nil {
return err
}
commitTime := pb.GetTimestamp().AsTime()
f.commitTime = commitTime
pbEndorsements := pb.GetEndorsements()
if pbEndorsements == nil {
Expand Down
12 changes: 5 additions & 7 deletions blockchain/block/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ package block
import (
"time"

"github.com/golang/protobuf/ptypes"
"github.com/iotexproject/go-pkgs/bloom"
"github.com/iotexproject/go-pkgs/crypto"
"github.com/iotexproject/go-pkgs/hash"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
"github.com/pkg/errors"
"go.uber.org/zap"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/timestamppb"

"github.com/iotexproject/iotex-core/pkg/log"
"github.com/iotexproject/iotex-core/pkg/util/byteutil"
Expand Down Expand Up @@ -89,10 +89,7 @@ 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))
}
ts := timestamppb.New(h.timestamp)
header := iotextypes.BlockHeaderCore{
Version: h.version,
Height: h.height,
Expand Down Expand Up @@ -127,15 +124,16 @@ func (h *Header) LoadFromBlockHeaderProto(pb *iotextypes.BlockHeader) error {
func (h *Header) loadFromBlockHeaderCoreProto(pb *iotextypes.BlockHeaderCore) error {
h.version = pb.GetVersion()
h.height = pb.GetHeight()
ts, err := ptypes.Timestamp(pb.GetTimestamp())
if err != nil {
if err := pb.GetTimestamp().CheckValid(); err != nil {
return err
}
ts := pb.GetTimestamp().AsTime()
h.timestamp = ts
copy(h.prevBlockHash[:], pb.GetPrevBlockHash())
copy(h.txRoot[:], pb.GetTxRoot())
copy(h.deltaStateDigest[:], pb.GetDeltaStateDigest())
copy(h.receiptRoot[:], pb.GetReceiptRoot())
var err error
if pb.GetLogsBloom() != nil {
h.logsBloom, err = bloom.NewBloomFilterLegacy(2048, 3)
if err != nil {
Expand Down
5 changes: 2 additions & 3 deletions consensus/scheme/rolldpos/rolldpos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (

"github.com/facebookgo/clock"
"github.com/golang/mock/gomock"
"github.com/golang/protobuf/ptypes"
"github.com/iotexproject/go-pkgs/crypto"
"github.com/iotexproject/go-pkgs/hash"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
Expand All @@ -25,6 +24,7 @@ import (
"github.com/stretchr/testify/require"
"golang.org/x/net/context"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/timestamppb"

"github.com/iotexproject/iotex-core/action/protocol"
"github.com/iotexproject/iotex-core/action/protocol/account"
Expand Down Expand Up @@ -169,8 +169,7 @@ func makeBlock(t *testing.T, accountIndex, numOfEndosements int, makeInvalidEndo
require.NoError(t, err)
typesFooter.Endorsements = append(typesFooter.Endorsements, enProto)
}
ts, err := ptypes.TimestampProto(time.Unix(int64(unixTime), 0))
require.NoError(t, err)
ts := timestamppb.New(time.Unix(int64(unixTime), 0))
typesFooter.Timestamp = ts
require.NotNil(t, typesFooter.Timestamp)
err = footerForBlk.ConvertFromBlockFooterPb(&typesFooter)
Expand Down
4 changes: 2 additions & 2 deletions consensus/scheme/rolldpos/subchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
"testing"

"github.com/golang/mock/gomock"
"github.com/golang/protobuf/ptypes"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/types/known/timestamppb"

"github.com/iotexproject/iotex-core/action"
"github.com/iotexproject/iotex-core/blockchain/block"
Expand All @@ -33,7 +33,7 @@ func TestPutBlockToParentChain(t *testing.T) {
Core: &iotextypes.BlockHeaderCore{
Version: version.ProtocolVersion,
Height: 123456789,
Timestamp: ptypes.TimestampNow(),
Timestamp: timestamppb.Now(),
},
ProducerPubkey: pubKey.Bytes(),
},
Expand Down
10 changes: 4 additions & 6 deletions endorsement/endorsement.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ package endorsement
import (
"time"

"github.com/golang/protobuf/ptypes"
"github.com/iotexproject/go-pkgs/crypto"
"github.com/iotexproject/go-pkgs/hash"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
"google.golang.org/protobuf/types/known/timestamppb"

"github.com/iotexproject/iotex-core/pkg/util/byteutil"
)
Expand Down Expand Up @@ -115,10 +115,7 @@ func (en *Endorsement) Signature() []byte {

// Proto converts an endorsement to protobuf message
func (en *Endorsement) Proto() (*iotextypes.Endorsement, error) {
ts, err := ptypes.TimestampProto(en.ts)
if err != nil {
return nil, err
}
ts := timestamppb.New(en.ts)
return &iotextypes.Endorsement{
Timestamp: ts,
Endorser: en.endorser.Bytes(),
Expand All @@ -128,9 +125,10 @@ func (en *Endorsement) Proto() (*iotextypes.Endorsement, error) {

// LoadProto converts a protobuf message to endorsement
func (en *Endorsement) LoadProto(ePb *iotextypes.Endorsement) (err error) {
if en.ts, err = ptypes.Timestamp(ePb.Timestamp); err != nil {
if err = ePb.Timestamp.CheckValid(); err != nil {
return err
}
en.ts = ePb.Timestamp.AsTime()
eb := make([]byte, len(ePb.Endorser))
copy(eb, ePb.Endorser)
if en.endorser, err = crypto.BytesToPublicKey(eb); err != nil {
Expand Down
5 changes: 2 additions & 3 deletions ioctl/cmd/action/actionhash.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"strconv"

protoV1 "github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"
"github.com/grpc-ecosystem/go-grpc-middleware/util/metautils"
"github.com/spf13/cobra"
"google.golang.org/grpc/codes"
Expand Down Expand Up @@ -148,10 +147,10 @@ func printAction(actionInfo *iotexapi.ActionInfo) (string, error) {
return "", err
}
if actionInfo.Timestamp != nil {
ts, err := ptypes.Timestamp(actionInfo.Timestamp)
if err != nil {
if err := actionInfo.Timestamp.CheckValid(); err != nil {
return "", err
}
ts := actionInfo.Timestamp.AsTime()
result += fmt.Sprintf("timeStamp: %d\n", ts.Unix())
result += fmt.Sprintf("blkHash: %s\n", actionInfo.BlkHash)
}
Expand Down
Loading

0 comments on commit bde7db5

Please sign in to comment.