Skip to content

Commit

Permalink
go/roothash/api/block: Use custom TimestampType for block's header
Browse files Browse the repository at this point in the history
This enables prettier Oasis Node's control status CLI command's output
for runtimes' latest_time field and matches the format of consensus'
latest_time field.
  • Loading branch information
tjanez committed Aug 5, 2021
1 parent 90b46f0 commit d340816
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changelog/4183.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
go/roothash/api/block: Use custom `TimestampType` for block's header

This enables prettier Oasis Node's `control status` CLI command's output
for runtimes' `latest_time` field and matches the format of consensus'
`latest_time` field.
3 changes: 2 additions & 1 deletion go/control/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/oasisprotocol/oasis-core/go/common/node"
consensus "github.com/oasisprotocol/oasis-core/go/consensus/api"
registry "github.com/oasisprotocol/oasis-core/go/registry/api"
block "github.com/oasisprotocol/oasis-core/go/roothash/api/block"
storage "github.com/oasisprotocol/oasis-core/go/storage/api"
upgrade "github.com/oasisprotocol/oasis-core/go/upgrade/api"
commonWorker "github.com/oasisprotocol/oasis-core/go/worker/common/api"
Expand Down Expand Up @@ -113,7 +114,7 @@ type RuntimeStatus struct {
// LatestHash is the hash of the latest runtime block.
LatestHash hash.Hash `json:"latest_hash"`
// LatestTime is the timestamp of the latest runtime block.
LatestTime uint64 `json:"latest_time"`
LatestTime block.TimestampType `json:"latest_time"`
// LatestStateRoot is the Merkle root of the runtime state tree.
LatestStateRoot storage.Root `json:"latest_state_root"`

Expand Down
2 changes: 1 addition & 1 deletion go/roothash/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ func SanityCheckBlocks(blocks map[common.Namespace]*block.Block) error {
for _, blk := range blocks {
hdr := blk.Header

if hdr.Timestamp > uint64(time.Now().Unix()+61*60) {
if hdr.Timestamp > block.TimestampType(time.Now().Unix()+61*60) {
return fmt.Errorf("roothash: sanity check failed: block header timestamp is more than 1h1m in the future")
}
}
Expand Down
4 changes: 2 additions & 2 deletions go/roothash/api/block/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func NewGenesisBlock(id common.Namespace, timestamp uint64) *Block {
var blk Block

blk.Header.Version = 0
blk.Header.Timestamp = timestamp
blk.Header.Timestamp = TimestampType(timestamp)
blk.Header.HeaderType = Normal
blk.Header.Namespace = id
blk.Header.PreviousHash.Empty()
Expand All @@ -35,7 +35,7 @@ func NewEmptyBlock(child *Block, timestamp uint64, htype HeaderType) *Block {
blk.Header.Version = child.Header.Version
blk.Header.Namespace = child.Header.Namespace
blk.Header.Round = child.Header.Round + 1
blk.Header.Timestamp = timestamp
blk.Header.Timestamp = TimestampType(timestamp)
blk.Header.HeaderType = htype
blk.Header.PreviousHash = child.Header.EncodedHash()
blk.Header.IORoot.Empty()
Expand Down
14 changes: 13 additions & 1 deletion go/roothash/api/block/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package block

import (
"bytes"
"encoding/json"
"errors"
"time"

"github.com/oasisprotocol/oasis-core/go/common"
"github.com/oasisprotocol/oasis-core/go/common/cbor"
Expand All @@ -17,6 +19,16 @@ var ErrInvalidVersion = errors.New("roothash: invalid version")
// HeaderType is the type of header.
type HeaderType uint8

// TimestampType is a custom time stamp type that encodes like time.Time when
// marshaling to JSON.
type TimestampType uint64

// MarshalJSON encodes the time stamp to JSON by converting it from Unix time to
// local time.
func (tst TimestampType) MarshalJSON() ([]byte, error) {
return json.Marshal(time.Unix(int64(tst), 0))
}

const (
// Invalid is an invalid header type and should never be stored.
Invalid HeaderType = 0
Expand Down Expand Up @@ -57,7 +69,7 @@ type Header struct { // nolint: maligned
Round uint64 `json:"round"`

// Timestamp is the block timestamp (POSIX time).
Timestamp uint64 `json:"timestamp"`
Timestamp TimestampType `json:"timestamp"`

// HeaderType is the header type.
HeaderType HeaderType `json:"header_type"`
Expand Down

0 comments on commit d340816

Please sign in to comment.