Skip to content

Commit

Permalink
add timestamp field to block struct
Browse files Browse the repository at this point in the history
  • Loading branch information
frrist committed Jun 5, 2019
1 parent 918065b commit e04275a
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 15 deletions.
2 changes: 1 addition & 1 deletion chain/default_syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1058,7 +1058,7 @@ func TestTipSetWeightDeep(t *testing.T) {

totalPower := types.NewBytesAmount(1000).Mul(types.OneKiBSectorSize)

info, err := gengen.GenGen(ctx, genCfg, cst, bs, 0)
info, err := gengen.GenGen(ctx, genCfg, cst, bs, 0, 0)
require.NoError(t, err)

var calcGenBlk types.Block
Expand Down
2 changes: 1 addition & 1 deletion chain/power_table_view_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func requireMinerWithNumCommittedSectors(ctx context.Context, t *testing.T, numC
},
}

info, err := gengen.GenGen(ctx, genCfg, cst, bs, 0)
info, err := gengen.GenGen(ctx, genCfg, cst, bs, 0, 0)
require.NoError(t, err)

var calcGenBlk types.Block
Expand Down
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
6 changes: 4 additions & 2 deletions gengen/util/gengen.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
mrand "math/rand"
"strconv"
"time"

"github.com/filecoin-project/go-filecoin/actor"
"github.com/filecoin-project/go-filecoin/actor/builtin"
Expand Down Expand Up @@ -97,7 +98,7 @@ type RenderedMinerInfo struct {
// the final genesis block.
//
// WARNING: Do not use maps in this code, they will make this code non deterministic.
func GenGen(ctx context.Context, cfg *GenesisCfg, cst *hamt.CborIpldStore, bs blockstore.Blockstore, seed int64) (*RenderedGenInfo, error) {
func GenGen(ctx context.Context, cfg *GenesisCfg, cst *hamt.CborIpldStore, bs blockstore.Blockstore, seed, timestamp int64) (*RenderedGenInfo, error) {
pnrg := mrand.New(mrand.NewSource(seed))
keys, err := genKeys(cfg.Keys, pnrg)
if err != nil {
Expand Down Expand Up @@ -148,6 +149,7 @@ func GenGen(ctx context.Context, cfg *GenesisCfg, cst *hamt.CborIpldStore, bs bl

geneblk := &types.Block{
StateRoot: stateRoot,
Timestamp: types.Uint64(timestamp),
}

c, err := cst.Put(ctx, geneblk)
Expand Down Expand Up @@ -312,7 +314,7 @@ func GenGenesisCar(cfg *GenesisCfg, out io.Writer, seed int64) (*RenderedGenInfo

ctx := context.Background()

info, err := GenGen(ctx, cfg, cst, bstore, seed)
info, err := GenGen(ctx, cfg, cst, bstore, seed, time.Now().Unix())
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion gengen/util/gengen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func TestGenGenDeterministicBetweenBuilds(t *testing.T) {

ctx := context.Background()

inf, err := GenGen(ctx, testConfig, cst, bstore, 0)
inf, err := GenGen(ctx, testConfig, cst, bstore, 0, 0)
assert.NoError(t, err)
if info == nil {
info = inf
Expand Down
2 changes: 2 additions & 0 deletions mining/block_generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ func (w *DefaultWorker) Generate(ctx context.Context,
Proof: proof,
StateRoot: newStateTreeCid,
Ticket: ticket,
// TODO user BlockClock interface from #2894 while completing #2886
Timestamp: 0,
}

for i, msg := range res.PermanentFailures {
Expand Down
3 changes: 2 additions & 1 deletion node/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io/ioutil"
"math/rand"
"testing"
"time"

bserv "github.com/ipfs/go-blockservice"
ds "github.com/ipfs/go-datastore"
Expand Down Expand Up @@ -54,7 +55,7 @@ func MakeChainSeed(t *testing.T, cfg *gengen.GenesisCfg) *ChainSeed {
blkserv := bserv.New(bstore, offl)
cst := &hamt.CborIpldStore{Blocks: blkserv}

info, err := gengen.GenGen(context.TODO(), cfg, cst, bstore, 0)
info, err := gengen.GenGen(context.TODO(), cfg, cst, bstore, 0, time.Now().Unix())
require.NoError(t, err)

return &ChainSeed{
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
15 changes: 8 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,7 @@ 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()+" }",
assert.Equal(t, "{ "+b2.Cid().String()+" "+b1.Cid().String()+" "+b3.Cid().String()+" }",
RequireNewTipSet(t, b3, b2, b1).String())
})

Expand Down Expand Up @@ -197,8 +198,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 e04275a

Please sign in to comment.