Add proper timestamping and checking of timestamps

This commit is contained in:
whyrusleeping 2019-08-29 19:59:54 -07:00
parent 55abcaa7d0
commit b69557251c
8 changed files with 42 additions and 10 deletions

5
build/chain.go Normal file
View File

@ -0,0 +1,5 @@
package build
const BlockDelay = 5
const AllowableClockDrift = BlockDelay * 2

View File

@ -11,6 +11,7 @@ import (
"github.com/ipfs/go-merkledag" "github.com/ipfs/go-merkledag"
"golang.org/x/xerrors" "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/address"
"github.com/filecoin-project/go-lotus/chain/store" "github.com/filecoin-project/go-lotus/chain/store"
"github.com/filecoin-project/go-lotus/chain/types" "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{ genb, err := MakeGenesisBlock(bs, map[address.Address]types.BigInt{
worker: types.NewInt(50000), worker: types.NewInt(50000),
banker: types.NewInt(90000000), banker: types.NewInt(90000000),
}, minercfg) }, minercfg, 100000)
if err != nil { if err != nil {
return nil, xerrors.Errorf("make genesis block failed: %w", err) 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 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 { if err != nil {
return nil, nil, err return nil, nil, err
} }

View File

@ -47,10 +47,11 @@ func MinerCreateBlock(ctx context.Context, cs *store.ChainStore, w *wallet.Walle
} }
next := &types.BlockHeader{ next := &types.BlockHeader{
Miner: miner, Miner: miner,
Parents: parents.Cids(), Parents: parents.Cids(),
Tickets: tickets, Tickets: tickets,
Height: height, Height: height,
Timestamp: timestamp,
} }
var blsMessages []*types.Message var blsMessages []*types.Message

View File

@ -246,7 +246,7 @@ func doExec(ctx context.Context, vm *vm.VM, to, from address.Address, method uin
return ret.Return, nil 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() ctx := context.Background()
state, err := MakeInitialStateTree(bs, balances) state, err := MakeInitialStateTree(bs, balances)
@ -305,6 +305,7 @@ func MakeGenesisBlock(bs bstore.Blockstore, balances map[address.Address]types.B
MessageReceipts: emptyroot, MessageReceipts: emptyroot,
BLSAggregate: types.Signature{Type: types.KTBLS, Data: []byte("signatureeee")}, BLSAggregate: types.Signature{Type: types.KTBLS, Data: []byte("signatureeee")},
BlockSig: types.Signature{Type: types.KTBLS, Data: []byte("block signatureeee")}, BlockSig: types.Signature{Type: types.KTBLS, Data: []byte("block signatureeee")},
Timestamp: ts,
} }
sb, err := b.ToStorageBlock() sb, err := b.ToStorageBlock()

View File

@ -4,9 +4,11 @@ import (
"context" "context"
"fmt" "fmt"
"sync" "sync"
"time"
"golang.org/x/xerrors" "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/actors"
"github.com/filecoin-project/go-lotus/chain/address" "github.com/filecoin-project/go-lotus/chain/address"
"github.com/filecoin-project/go-lotus/chain/store" "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) 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 { if err := syncer.minerIsValid(ctx, h.Miner, baseTs); err != nil {
return xerrors.Errorf("minerIsValid failed: %w", err) return xerrors.Errorf("minerIsValid failed: %w", err)
} }

View File

@ -110,3 +110,13 @@ func (ts *TipSet) MinTicket() *Ticket {
return minTicket 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
}

View File

@ -7,6 +7,7 @@ import (
"sync" "sync"
"time" "time"
"github.com/filecoin-project/go-lotus/build"
chain "github.com/filecoin-project/go-lotus/chain" chain "github.com/filecoin-project/go-lotus/chain"
"github.com/filecoin-project/go-lotus/chain/actors" "github.com/filecoin-project/go-lotus/chain/actors"
"github.com/filecoin-project/go-lotus/chain/address" "github.com/filecoin-project/go-lotus/chain/address"
@ -36,7 +37,7 @@ type api struct {
func NewMiner(api api) *Miner { func NewMiner(api api) *Miner {
return &Miner{ return &Miner{
api: api, api: api,
Delay: time.Second * 4, Delay: build.BlockDelay,
} }
} }

View File

@ -41,7 +41,7 @@ func MakeGenesisMem(out io.Writer) func(bs dtypes.ChainBlockstore, w *wallet.Wal
w: types.NewInt(100000), w: types.NewInt(100000),
} }
b, err := gen.MakeGenesisBlock(bs, alloc, gmc) b, err := gen.MakeGenesisBlock(bs, alloc, gmc, 100000)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -76,7 +76,7 @@ func MakeGenesis(outFile string) func(bs dtypes.ChainBlockstore, w *wallet.Walle
minerAddr: types.NewInt(50000000), minerAddr: types.NewInt(50000000),
} }
b, err := gen.MakeGenesisBlock(bs, addrs, gmc) b, err := gen.MakeGenesisBlock(bs, addrs, gmc, 100000)
if err != nil { if err != nil {
return nil, err return nil, err
} }