From 3bf447ffa8d69fbcbabd9cdabc1c2eef1607ac75 Mon Sep 17 00:00:00 2001 From: Jim Zhang Date: Fri, 4 Nov 2022 14:43:06 -0400 Subject: [PATCH] Fix timestamp handling, should be int64 Signed-off-by: Jim Zhang --- internal/events/api/event.go | 2 +- internal/events/eventstream_test.go | 2 +- internal/events/subscription.go | 4 ++-- internal/fabric/utils/block.go | 4 ++-- internal/fabric/utils/blockdecoder.go | 5 +++-- internal/fabric/utils/blockdecoder_test.go | 2 +- internal/fabric/utils/rawblock.go | 2 +- 7 files changed, 11 insertions(+), 10 deletions(-) diff --git a/internal/events/api/event.go b/internal/events/api/event.go index 0c300a2..00442aa 100644 --- a/internal/events/api/event.go +++ b/internal/events/api/event.go @@ -69,7 +69,7 @@ type EventEntry struct { EventIndex int `json:"eventIndex"` EventName string `json:"eventName"` Payload interface{} `json:"payload"` - Timestamp int32 `json:"timestamp,omitempty"` + Timestamp int64 `json:"timestamp,omitempty"` SubID string `json:"subId"` } diff --git a/internal/events/eventstream_test.go b/internal/events/eventstream_test.go index afc17ec..c823a6d 100644 --- a/internal/events/eventstream_test.go +++ b/internal/events/eventstream_test.go @@ -375,7 +375,7 @@ func TestProcessEventsEnd2EndWebhook(t *testing.T) { e2s := <-eventStream assert.Equal(1, len(e2s)) assert.Equal(uint64(10), e2s[0].BlockNumber) - assert.Equal(int32(1000000), e2s[0].Timestamp) + assert.Equal(int64(1000000), e2s[0].Timestamp) wg.Done() }() wg.Wait() diff --git a/internal/events/subscription.go b/internal/events/subscription.go index 3a8814d..f1c2cc3 100644 --- a/internal/events/subscription.go +++ b/internal/events/subscription.go @@ -152,7 +152,7 @@ func (s *subscription) getEventTimestamp(evt *eventsapi.EventEntry) { blockNumber := strconv.FormatUint(evt.BlockNumber, 10) if ts, ok := s.ep.stream.blockTimestampCache.Get(blockNumber); ok { // we found the timestamp for the block in our local cache, assert it's type and return, no need to query the chain - timestamps := ts.([]int32) + timestamps := ts.([]int64) evt.Timestamp = timestamps[evt.TransactionIndex] return } @@ -165,7 +165,7 @@ func (s *subscription) getEventTimestamp(evt *eventsapi.EventEntry) { } // blocks in Fabric does not have a timestamp. instead only transactions have their own timestamps // so each entry in the cache is a slice of (tx timestamp) - timestamps := make([]int32, len(block.Transactions)) + timestamps := make([]int64, len(block.Transactions)) for idx, tx := range block.Transactions { timestamps[idx] = tx.Timestamp } diff --git a/internal/fabric/utils/block.go b/internal/fabric/utils/block.go index f657c9c..391b854 100644 --- a/internal/fabric/utils/block.go +++ b/internal/fabric/utils/block.go @@ -44,7 +44,7 @@ type Transaction struct { Creator *Creator `json:"creator"` Status string `json:"status"` Signature string `json:"signature"` - Timestamp int32 `json:"timestamp"` // unix nano + Timestamp int64 `json:"timestamp"` // unix nano Actions []*TransactionAction `json:"actions"` } @@ -61,7 +61,7 @@ type TransactionAction struct { type ConfigRecord struct { Type string `json:"type"` Signature string `json:"signature"` - Timestamp int32 `json:"timestamp"` // unix nano + Timestamp int64 `json:"timestamp"` // unix nano Nonce string `json:"nonce"` // hex string Creator *Creator `json:"creator"` Config *common.Config `json:"config"` diff --git a/internal/fabric/utils/blockdecoder.go b/internal/fabric/utils/blockdecoder.go index fe65836..15c463b 100644 --- a/internal/fabric/utils/blockdecoder.go +++ b/internal/fabric/utils/blockdecoder.go @@ -20,6 +20,7 @@ import ( "encoding/base64" "encoding/hex" "strconv" + "time" "github.com/golang/protobuf/proto" //nolint "github.com/hyperledger/fabric-protos-go/common" @@ -174,7 +175,7 @@ func (block *RawBlock) decodePayload(payload *common.Payload, _payload *Payload) } for _, action := range _payloadData.Actions { if action.Payload.Action.ProposalResponsePayload.Extension.Events != nil { - action.Payload.Action.ProposalResponsePayload.Extension.Events.Timestamp = strconv.Itoa(int(timestamp)) + action.Payload.Action.ProposalResponsePayload.Extension.Events.Timestamp = strconv.FormatInt(timestamp, 10) } } return _transaction, nil @@ -204,7 +205,7 @@ func (block *RawBlock) decodePayloadHeader(header *common.Header, _header *Paylo } _channelHeader.ChannelId = channelHeader.ChannelId _channelHeader.Epoch = strconv.FormatUint(channelHeader.Epoch, 10) - _channelHeader.Timestamp = channelHeader.Timestamp.GetNanos() + _channelHeader.Timestamp = time.Unix(channelHeader.Timestamp.GetSeconds(), int64(channelHeader.Timestamp.GetNanos())).UnixNano() _channelHeader.TxId = channelHeader.TxId _channelHeader.Type = common.HeaderType_name[channelHeader.Type] _channelHeader.Version = int(channelHeader.Version) diff --git a/internal/fabric/utils/blockdecoder_test.go b/internal/fabric/utils/blockdecoder_test.go index 3c13b92..98fd9f6 100644 --- a/internal/fabric/utils/blockdecoder_test.go +++ b/internal/fabric/utils/blockdecoder_test.go @@ -117,5 +117,5 @@ func TestGetEvents(t *testing.T) { assert.Equal("AssetCreated", entry.EventName) assert.Regexp("[0-9a-f]{64}", entry.TransactionId) assert.Equal(0, entry.TransactionIndex) - assert.Equal(int32(312746000), entry.Timestamp) + assert.Equal(int64(1641861241312746000), entry.Timestamp) } diff --git a/internal/fabric/utils/rawblock.go b/internal/fabric/utils/rawblock.go index cb8dd9b..7b11696 100644 --- a/internal/fabric/utils/rawblock.go +++ b/internal/fabric/utils/rawblock.go @@ -50,7 +50,7 @@ type PayloadHeader struct { type ChannelHeader struct { ChannelId string `json:"channel_id"` Epoch string `json:"epoch"` - Timestamp int32 `json:"timestamp"` + Timestamp int64 `json:"timestamp"` TxId string `json:"tx_id"` Type string `json:"type"` Version int `json:"version"`