From b69557251c0701c9d1894cfb73d4019f1a1a46ff Mon Sep 17 00:00:00 2001 From: whyrusleeping Date: Thu, 29 Aug 2019 19:59:54 -0700 Subject: [PATCH 1/2] Add proper timestamping and checking of timestamps --- build/chain.go | 5 +++++ chain/gen/gen.go | 7 +++++-- chain/gen/mining.go | 9 +++++---- chain/gen/utils.go | 3 ++- chain/sync.go | 11 +++++++++++ chain/types/tipset.go | 10 ++++++++++ miner/miner.go | 3 ++- node/modules/testing/genesis.go | 4 ++-- 8 files changed, 42 insertions(+), 10 deletions(-) create mode 100644 build/chain.go diff --git a/build/chain.go b/build/chain.go new file mode 100644 index 000000000..b490aeedf --- /dev/null +++ b/build/chain.go @@ -0,0 +1,5 @@ +package build + +const BlockDelay = 5 + +const AllowableClockDrift = BlockDelay * 2 diff --git a/chain/gen/gen.go b/chain/gen/gen.go index 6e10a451e..554674e9d 100644 --- a/chain/gen/gen.go +++ b/chain/gen/gen.go @@ -11,6 +11,7 @@ import ( "github.com/ipfs/go-merkledag" "golang.org/x/xerrors" + "github.com/filecoin-project/go-lotus/build" "github.com/filecoin-project/go-lotus/chain/address" "github.com/filecoin-project/go-lotus/chain/store" "github.com/filecoin-project/go-lotus/chain/types" @@ -122,7 +123,7 @@ func NewGenerator() (*ChainGen, error) { genb, err := MakeGenesisBlock(bs, map[address.Address]types.BigInt{ worker: types.NewInt(50000), banker: types.NewInt(90000000), - }, minercfg) + }, minercfg, 100000) if err != nil { return nil, xerrors.Errorf("make genesis block failed: %w", err) } @@ -253,7 +254,9 @@ func (cg *ChainGen) NextBlock() (*types.FullBlock, []*types.SignedMessage, error return nil, nil, err } - fblk, err := MinerCreateBlock(context.TODO(), cg.cs, cg.w, miner, parents, tickets, proof, msgs, 0) + ts := parents.MinTimestamp() + (uint64(len(tickets)) * build.BlockDelay) + + fblk, err := MinerCreateBlock(context.TODO(), cg.cs, cg.w, miner, parents, tickets, proof, msgs, ts) if err != nil { return nil, nil, err } diff --git a/chain/gen/mining.go b/chain/gen/mining.go index 72185d03a..ebe26d1ac 100644 --- a/chain/gen/mining.go +++ b/chain/gen/mining.go @@ -47,10 +47,11 @@ func MinerCreateBlock(ctx context.Context, cs *store.ChainStore, w *wallet.Walle } next := &types.BlockHeader{ - Miner: miner, - Parents: parents.Cids(), - Tickets: tickets, - Height: height, + Miner: miner, + Parents: parents.Cids(), + Tickets: tickets, + Height: height, + Timestamp: timestamp, } var blsMessages []*types.Message diff --git a/chain/gen/utils.go b/chain/gen/utils.go index ce5e4607e..ff31a1c7c 100644 --- a/chain/gen/utils.go +++ b/chain/gen/utils.go @@ -246,7 +246,7 @@ func doExec(ctx context.Context, vm *vm.VM, to, from address.Address, method uin return ret.Return, nil } -func MakeGenesisBlock(bs bstore.Blockstore, balances map[address.Address]types.BigInt, gmcfg *GenMinerCfg) (*GenesisBootstrap, error) { +func MakeGenesisBlock(bs bstore.Blockstore, balances map[address.Address]types.BigInt, gmcfg *GenMinerCfg, ts uint64) (*GenesisBootstrap, error) { ctx := context.Background() state, err := MakeInitialStateTree(bs, balances) @@ -305,6 +305,7 @@ func MakeGenesisBlock(bs bstore.Blockstore, balances map[address.Address]types.B MessageReceipts: emptyroot, BLSAggregate: types.Signature{Type: types.KTBLS, Data: []byte("signatureeee")}, BlockSig: types.Signature{Type: types.KTBLS, Data: []byte("block signatureeee")}, + Timestamp: ts, } sb, err := b.ToStorageBlock() diff --git a/chain/sync.go b/chain/sync.go index c0581f28b..64fbd7f05 100644 --- a/chain/sync.go +++ b/chain/sync.go @@ -4,9 +4,11 @@ import ( "context" "fmt" "sync" + "time" "golang.org/x/xerrors" + "github.com/filecoin-project/go-lotus/build" "github.com/filecoin-project/go-lotus/chain/actors" "github.com/filecoin-project/go-lotus/chain/address" "github.com/filecoin-project/go-lotus/chain/store" @@ -434,6 +436,15 @@ func (syncer *Syncer) ValidateBlock(ctx context.Context, b *types.FullBlock) err return xerrors.Errorf("load tipset failed: %w", err) } + if h.Timestamp > uint64(time.Now().Unix()+build.AllowableClockDrift) { + return xerrors.Errorf("block was from the future") + } + + if h.Timestamp < baseTs.MinTimestamp()+uint64(build.BlockDelay*len(h.Tickets)) { + log.Warn("timestamp funtimes: ", h.Timestamp, baseTs.MinTimestamp(), len(h.Tickets)) + return xerrors.Errorf("block was generated too soon (timestamp < BLOCK_DELAY * len(tickets))") + } + if err := syncer.minerIsValid(ctx, h.Miner, baseTs); err != nil { return xerrors.Errorf("minerIsValid failed: %w", err) } diff --git a/chain/types/tipset.go b/chain/types/tipset.go index 661f2d4e9..06c85b59a 100644 --- a/chain/types/tipset.go +++ b/chain/types/tipset.go @@ -110,3 +110,13 @@ func (ts *TipSet) MinTicket() *Ticket { return minTicket } + +func (ts *TipSet) MinTimestamp() uint64 { + minTs := ts.Blocks()[0].Timestamp + for _, bh := range ts.Blocks()[1:] { + if bh.Timestamp < minTs { + minTs = bh.Timestamp + } + } + return minTs +} diff --git a/miner/miner.go b/miner/miner.go index 26e1d4650..7546b79e6 100644 --- a/miner/miner.go +++ b/miner/miner.go @@ -7,6 +7,7 @@ import ( "sync" "time" + "github.com/filecoin-project/go-lotus/build" chain "github.com/filecoin-project/go-lotus/chain" "github.com/filecoin-project/go-lotus/chain/actors" "github.com/filecoin-project/go-lotus/chain/address" @@ -36,7 +37,7 @@ type api struct { func NewMiner(api api) *Miner { return &Miner{ api: api, - Delay: time.Second * 4, + Delay: build.BlockDelay, } } diff --git a/node/modules/testing/genesis.go b/node/modules/testing/genesis.go index 6951282ed..524c53adf 100644 --- a/node/modules/testing/genesis.go +++ b/node/modules/testing/genesis.go @@ -41,7 +41,7 @@ func MakeGenesisMem(out io.Writer) func(bs dtypes.ChainBlockstore, w *wallet.Wal w: types.NewInt(100000), } - b, err := gen.MakeGenesisBlock(bs, alloc, gmc) + b, err := gen.MakeGenesisBlock(bs, alloc, gmc, 100000) if err != nil { return nil, err } @@ -76,7 +76,7 @@ func MakeGenesis(outFile string) func(bs dtypes.ChainBlockstore, w *wallet.Walle minerAddr: types.NewInt(50000000), } - b, err := gen.MakeGenesisBlock(bs, addrs, gmc) + b, err := gen.MakeGenesisBlock(bs, addrs, gmc, 100000) if err != nil { return nil, err } From 94cdb231ebbf503e509463466112ecd5ec5e1453 Mon Sep 17 00:00:00 2001 From: whyrusleeping Date: Thu, 29 Aug 2019 23:27:02 -0700 Subject: [PATCH 2/2] fix delay in miner --- miner/miner.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/miner/miner.go b/miner/miner.go index 7546b79e6..4464e3654 100644 --- a/miner/miner.go +++ b/miner/miner.go @@ -37,7 +37,7 @@ type api struct { func NewMiner(api api) *Miner { return &Miner{ api: api, - Delay: build.BlockDelay, + Delay: build.BlockDelay * time.Second, } }