Skip to content

Commit

Permalink
Fix timestamp handling, should be int64
Browse files Browse the repository at this point in the history
Signed-off-by: Jim Zhang <[email protected]>
  • Loading branch information
jimthematrix committed Nov 4, 2022
1 parent e8eab15 commit 3bf447f
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 10 deletions.
2 changes: 1 addition & 1 deletion internal/events/api/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand Down
2 changes: 1 addition & 1 deletion internal/events/eventstream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions internal/events/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions internal/fabric/utils/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand All @@ -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"`
Expand Down
5 changes: 3 additions & 2 deletions internal/fabric/utils/blockdecoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"encoding/base64"
"encoding/hex"
"strconv"
"time"

"github.com/golang/protobuf/proto" //nolint
"github.com/hyperledger/fabric-protos-go/common"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion internal/fabric/utils/blockdecoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion internal/fabric/utils/rawblock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down

0 comments on commit 3bf447f

Please sign in to comment.