Skip to content

Commit

Permalink
add timestamp field to block, fix tipset tests
Browse files Browse the repository at this point in the history
  • Loading branch information
frrist committed Jun 6, 2019
1 parent bc08932 commit 9ee8457
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
6 changes: 5 additions & 1 deletion commands/schema/filecoin_block.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@
"string",
"null"
]
},
"timestamp": {
"type": "string"
}
},
"required": [
Expand All @@ -74,7 +77,8 @@
"parents",
"proof",
"stateRoot",
"ticket"
"ticket",
"timestamp"
],
"type": "object"
},
Expand Down
3 changes: 3 additions & 0 deletions types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ type Block struct {
// a challenge
Proof PoStProof `json:"proof"`

// The timestamp, in seconds since the Unix epoch, at which this block was created.
Timestamp Uint64 `json:"timestamp"`

cachedCid cid.Cid

cachedBytes []byte
Expand Down
3 changes: 2 additions & 1 deletion types/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,12 @@ func TestTriangleEncoding(t *testing.T) {
ParentWeight: Uint64(1000),
Proof: NewTestPoSt(),
StateRoot: SomeCid(),
Timestamp: Uint64(1),
}
s := reflect.TypeOf(*b)
// This check is here to request that you add a non-zero value for new fields
// to the above (and update the field count below).
require.Equal(t, 12, s.NumField()) // Note: this also counts private fields
require.Equal(t, 13, s.NumField()) // Note: this also counts private fields
testRoundTrip(t, b)
})
}
Expand Down
20 changes: 13 additions & 7 deletions types/tipset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func init() {
mockSignerForTest, _ = NewMockSignersAndKeyInfo(2)
}

func block(t *testing.T, ticket []byte, height int, parentCid cid.Cid, parentWeight uint64, msg string) *Block {
func block(t *testing.T, ticket []byte, height int, parentCid cid.Cid, parentWeight, timestamp uint64, msg string) *Block {
addrGetter := address.NewForTestGetter()

m1 := NewMessage(mockSignerForTest.Addresses[0], addrGetter(), 0, NewAttoFILFromFIL(10), "hello", []byte(msg))
Expand All @@ -45,6 +45,7 @@ func block(t *testing.T, ticket []byte, height int, parentCid cid.Cid, parentWei
Messages: []*SignedMessage{sm1},
StateRoot: SomeCid(),
MessageReceipts: []*MessageReceipt{{ExitCode: 1, Return: [][]byte{ret}}},
Timestamp: Uint64(timestamp),
}
}

Expand All @@ -68,8 +69,8 @@ func TestTipSet(t *testing.T) {
})

t.Run("order breaks ties with CID", func(t *testing.T) {
b1 := block(t, []byte{1}, 1, cid1, parentWeight, "1")
b2 := block(t, []byte{1}, 1, cid1, parentWeight, "2")
b1 := block(t, []byte{1}, 1, cid1, parentWeight, 1, "1")
b2 := block(t, []byte{1}, 1, cid1, parentWeight, 2, "2")

ts := RequireNewTipSet(t, b1, b2)
if bytes.Compare(b1.Cid().Bytes(), b2.Cid().Bytes()) < 0 {
Expand Down Expand Up @@ -153,7 +154,12 @@ func TestTipSet(t *testing.T) {
// String shouldn't really need testing, but some existing code uses the string as a
// datastore key and depends on the format exactly.
assert.Equal(t, "{ "+b1.Cid().String()+" }", RequireNewTipSet(t, b1).String())
assert.Equal(t, "{ "+b1.Cid().String()+" "+b2.Cid().String()+" "+b3.Cid().String()+" }",

// DONOTMETGE the below assertion needed to be fixed after adding a timestamp to block.
// I believe this is because tipsets are sorted by tickets, but when converted to a
// string they are sorted by CID's, the new timstamp field effects CID's.
// I believe I am missing something or it was a fluke this test passed in the first place...
assert.Equal(t, "{ "+b2.Cid().String()+" "+b1.Cid().String()+" "+b3.Cid().String()+" }",
RequireNewTipSet(t, b3, b2, b1).String())
})

Expand Down Expand Up @@ -197,8 +203,8 @@ func TestTipSet(t *testing.T) {

// Test methods: String, ToSortedCidSet, ToSlice, MinTicket, Height, NewTipSet, Equals
func makeTestBlocks(t *testing.T) (*Block, *Block, *Block) {
b1 := block(t, []byte{1}, 1, cid1, parentWeight, "1")
b2 := block(t, []byte{2}, 1, cid1, parentWeight, "2")
b3 := block(t, []byte{3}, 1, cid1, parentWeight, "3")
b1 := block(t, []byte{1}, 1, cid1, parentWeight, 1, "1")
b2 := block(t, []byte{2}, 1, cid1, parentWeight, 2, "2")
b3 := block(t, []byte{3}, 1, cid1, parentWeight, 3, "3")
return b1, b2, b3
}

0 comments on commit 9ee8457

Please sign in to comment.