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"
"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
}

View File

@ -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

View File

@ -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()

View File

@ -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)
}

View File

@ -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
}

View File

@ -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,
}
}

View File

@ -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
}