Skip to content

Commit

Permalink
move blocktime onto consensus and plumb
Browse files Browse the repository at this point in the history
- This commit moves the blocktime default to the consensus package and
ands plumbing and porcelain cals to access it.
  • Loading branch information
frrist committed Jun 12, 2019
1 parent 3a320be commit 8ac5b9f
Show file tree
Hide file tree
Showing 20 changed files with 112 additions and 74 deletions.
2 changes: 1 addition & 1 deletion chain/default_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func initStoreTest(ctx context.Context, t *testing.T, dstP *DefaultSyncerTestPar
r := repo.NewInMemoryRepo()
bs := bstore.NewBlockstore(r.Datastore())
cst := hamt.NewCborStore()
con := consensus.NewExpected(cst, bs, th.NewTestProcessor(), th.NewFakeBlockValidator(), powerTable, dstP.genCid, proofs.NewFakeVerifier(true, nil))
con := consensus.NewExpected(cst, bs, th.NewTestProcessor(), th.NewFakeBlockValidator(), powerTable, dstP.genCid, proofs.NewFakeVerifier(true, nil), th.BlockTimeTest)
initGenesisWrapper := func(cst *hamt.CborIpldStore, bs bstore.Blockstore) (*types.Block, error) {
return initGenesis(dstP.minerAddress, dstP.minerOwnerAddress, dstP.minerPeerID, cst, bs)
}
Expand Down
12 changes: 6 additions & 6 deletions chain/default_syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ func loadSyncerFromRepo(t *testing.T, r repo.Repo, dstP *DefaultSyncerTestParams
bs := bstore.NewBlockstore(r.Datastore())
cst := hamt.NewCborStore()
verifier := proofs.NewFakeVerifier(true, nil)
con := consensus.NewExpected(cst, bs, th.NewTestProcessor(), th.NewFakeBlockValidator(), powerTable, dstP.genCid, verifier)
con := consensus.NewExpected(cst, bs, th.NewTestProcessor(), th.NewFakeBlockValidator(), powerTable, dstP.genCid, verifier, th.BlockTimeTest)

calcGenBlk, err := initGenesis(dstP.minerAddress, dstP.minerOwnerAddress, dstP.minerPeerID, cst, bs) // flushes state
require.NoError(t, err)
Expand All @@ -221,7 +221,7 @@ func initSyncTestDefault(t *testing.T, dstP *DefaultSyncerTestParams) (*chain.De
bs := bstore.NewBlockstore(r.Datastore())
cst := hamt.NewCborStore()
verifier := proofs.NewFakeVerifier(true, nil)
con := consensus.NewExpected(cst, bs, processor, th.NewFakeBlockValidator(), powerTable, dstP.genCid, verifier)
con := consensus.NewExpected(cst, bs, processor, th.NewFakeBlockValidator(), powerTable, dstP.genCid, verifier, th.BlockTimeTest)
requireSetTestChain(t, con, false, dstP)
initGenesisWrapper := func(cst *hamt.CborIpldStore, bs bstore.Blockstore) (*types.Block, error) {
return initGenesis(dstP.minerAddress, dstP.minerOwnerAddress, dstP.minerPeerID, cst, bs)
Expand All @@ -240,7 +240,7 @@ func initSyncTestWithMode(t *testing.T, dstP *DefaultSyncerTestParams, syncMode
bs := bstore.NewBlockstore(r.Datastore())
cst := hamt.NewCborStore()
verifier := proofs.NewFakeVerifier(true, nil)
con := consensus.NewExpected(cst, bs, processor, th.NewFakeBlockValidator(), powerTable, dstP.genCid, verifier)
con := consensus.NewExpected(cst, bs, processor, th.NewFakeBlockValidator(), powerTable, dstP.genCid, verifier, th.BlockTimeTest)
requireSetTestChain(t, con, false, dstP)
initGenesisWrapper := func(cst *hamt.CborIpldStore, bs bstore.Blockstore) (*types.Block, error) {
return initGenesis(dstP.minerAddress, dstP.minerOwnerAddress, dstP.minerPeerID, cst, bs)
Expand All @@ -257,7 +257,7 @@ func initSyncTestWithPowerTable(t *testing.T, powerTable consensus.PowerTableVie
bs := bstore.NewBlockstore(r.Datastore())
cst := hamt.NewCborStore()
verifier := proofs.NewFakeVerifier(true, nil)
con := consensus.NewExpected(cst, bs, processor, th.NewFakeBlockValidator(), powerTable, dstP.genCid, verifier)
con := consensus.NewExpected(cst, bs, processor, th.NewFakeBlockValidator(), powerTable, dstP.genCid, verifier, th.BlockTimeTest)
requireSetTestChain(t, con, false, dstP)
initGenesisWrapper := func(cst *hamt.CborIpldStore, bs bstore.Blockstore) (*types.Block, error) {
return initGenesis(dstP.minerAddress, dstP.minerOwnerAddress, dstP.minerPeerID, cst, bs)
Expand Down Expand Up @@ -1067,7 +1067,7 @@ func TestTipSetWeightDeep(t *testing.T) {
chainStore := chain.NewStore(r.ChainDatastore(), calcGenBlk.Cid())

verifier := proofs.NewFakeVerifier(true, nil)
con := consensus.NewExpected(cst, bs, th.NewTestProcessor(), th.NewFakeBlockValidator(), &th.TestView{}, calcGenBlk.Cid(), verifier)
con := consensus.NewExpected(cst, bs, th.NewTestProcessor(), th.NewFakeBlockValidator(), &th.TestView{}, calcGenBlk.Cid(), verifier, th.BlockTimeTest)

// Initialize stores to contain dstP.genesis block and state
calcGenTS := th.RequireNewTipSet(t, &calcGenBlk)
Expand All @@ -1086,7 +1086,7 @@ func TestTipSetWeightDeep(t *testing.T) {

// Now sync the chainStore with consensus using a MarketView.
verifier = proofs.NewFakeVerifier(true, nil)
con = consensus.NewExpected(cst, bs, th.NewTestProcessor(), th.NewFakeBlockValidator(), &consensus.MarketView{}, calcGenBlk.Cid(), verifier)
con = consensus.NewExpected(cst, bs, th.NewTestProcessor(), th.NewFakeBlockValidator(), &consensus.MarketView{}, calcGenBlk.Cid(), verifier, th.BlockTimeTest)
syncer := chain.NewDefaultSyncer(cst, con, chainStore, blockSource, chain.Syncing)
baseTS := requireHeadTipset(t, chainStore) // this is the last block of the bootstrapping chain creating miners
require.Equal(t, 1, baseTS.Len())
Expand Down
4 changes: 2 additions & 2 deletions commands/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"github.com/pkg/errors"

"github.com/filecoin-project/go-filecoin/config"
"github.com/filecoin-project/go-filecoin/mining"
"github.com/filecoin-project/go-filecoin/consensus"
"github.com/filecoin-project/go-filecoin/node"
"github.com/filecoin-project/go-filecoin/paths"
"github.com/filecoin-project/go-filecoin/repo"
Expand All @@ -38,7 +38,7 @@ var daemonCmd = &cmds.Command{
cmdkit.BoolOption(OfflineMode, "start the node without networking"),
cmdkit.BoolOption(ELStdout),
cmdkit.BoolOption(IsRelay, "advertise and allow filecoin network traffic to be relayed through this node"),
cmdkit.StringOption(BlockTime, "time a node waits before trying to mine the next block").WithDefault(mining.DefaultBlockTime.String()),
cmdkit.StringOption(BlockTime, "time a node waits before trying to mine the next block").WithDefault(consensus.DefaultBlockTime.String()),
},
Run: func(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) error {
return daemonRun(req, re, env)
Expand Down
15 changes: 14 additions & 1 deletion consensus/expected.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"fmt"
"math/big"
"strings"
"time"

"github.com/ipfs/go-cid"
"github.com/ipfs/go-hamt-ipld"
Expand Down Expand Up @@ -57,6 +58,10 @@ type TicketSigner interface {
SignBytes(data []byte, signerAddr address.Address) (types.Signature, error)
}

// DefaultBlockTime is the estimated proving period time.
// We define this so that we can fake mining in the current incomplete system.
const DefaultBlockTime = 30 * time.Second

// TODO none of these parameters are chosen correctly
// with respect to analysis under a security model:
// https://github.com/filecoin-project/go-filecoin/issues/1846
Expand Down Expand Up @@ -107,15 +112,18 @@ type Expected struct {
genesisCid cid.Cid

verifier proofs.Verifier

blockTime time.Duration
}

// Ensure Expected satisfies the Protocol interface at compile time.
var _ Protocol = (*Expected)(nil)

// NewExpected is the constructor for the Expected consenus.Protocol module.
func NewExpected(cs *hamt.CborIpldStore, bs blockstore.Blockstore, processor Processor, v BlockValidator, pt PowerTableView, gCid cid.Cid, verifier proofs.Verifier) *Expected {
func NewExpected(cs *hamt.CborIpldStore, bs blockstore.Blockstore, processor Processor, v BlockValidator, pt PowerTableView, gCid cid.Cid, verifier proofs.Verifier, bt time.Duration) *Expected {
return &Expected{
cstore: cs,
blockTime: bt,
bstore: bs,
processor: processor,
PwrTableView: pt,
Expand All @@ -125,6 +133,11 @@ func NewExpected(cs *hamt.CborIpldStore, bs blockstore.Blockstore, processor Pro
}
}

// BlockTime returns the block time used by the consensus protocol.
func (c *Expected) BlockTime(ctx context.Context) time.Duration {
return e.blockTime
}

// Weight returns the EC weight of this TipSet in uint64 encoded fixed point
// representation.
func (c *Expected) Weight(ctx context.Context, ts types.TipSet, pSt state.Tree) (uint64, error) {
Expand Down
6 changes: 3 additions & 3 deletions consensus/expected_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestNewExpected(t *testing.T) {
t.Run("a new Expected can be created", func(t *testing.T) {
cst, bstore, verifier := setupCborBlockstoreProofs()
ptv := th.NewTestPowerTableView(types.NewBytesAmount(1), types.NewBytesAmount(5))
exp := consensus.NewExpected(cst, bstore, consensus.NewDefaultProcessor(), th.NewFakeBlockValidator(), ptv, types.SomeCid(), verifier)
exp := consensus.NewExpected(cst, bstore, consensus.NewDefaultProcessor(), th.NewFakeBlockValidator(), ptv, types.SomeCid(), verifier, th.BlockTimeTest)
assert.NotNil(t, exp)
})
}
Expand Down Expand Up @@ -93,7 +93,7 @@ func TestExpected_RunStateTransition_validateMining(t *testing.T) {
totalPower := types.NewBytesAmount(1)

ptv := th.NewTestPowerTableView(minerPower, totalPower)
exp := consensus.NewExpected(cistore, bstore, th.NewTestProcessor(), th.NewFakeBlockValidator(), ptv, genesisBlock.Cid(), verifier)
exp := consensus.NewExpected(cistore, bstore, th.NewTestProcessor(), th.NewFakeBlockValidator(), ptv, genesisBlock.Cid(), verifier, th.BlockTimeTest)

pTipSet := types.RequireNewTipSet(t, genesisBlock)

Expand All @@ -112,7 +112,7 @@ func TestExpected_RunStateTransition_validateMining(t *testing.T) {
t.Run("returns nil + mining error when IsWinningTicket fails due to miner power error", func(t *testing.T) {

ptv := NewFailingMinerTestPowerTableView(types.NewBytesAmount(1), types.NewBytesAmount(5))
exp := consensus.NewExpected(cistore, bstore, consensus.NewDefaultProcessor(), th.NewFakeBlockValidator(), ptv, types.SomeCid(), verifier)
exp := consensus.NewExpected(cistore, bstore, consensus.NewDefaultProcessor(), th.NewFakeBlockValidator(), ptv, types.SomeCid(), verifier, th.BlockTimeTest)

pTipSet := types.RequireNewTipSet(t, genesisBlock)

Expand Down
4 changes: 4 additions & 0 deletions consensus/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ package consensus
// except for errors in the case the stores do not have a mapping.
import (
"context"
"time"

"github.com/filecoin-project/go-filecoin/state"
"github.com/filecoin-project/go-filecoin/types"
Expand Down Expand Up @@ -38,4 +39,7 @@ type Protocol interface {

// ValidateSemantic validates a block is correctly derived from its parent.
ValidateSemantic(ctx context.Context, child *types.Block, parents *types.TipSet) error

// BlockTime returns the block time used by the consensus protocol.
BlockTime(ctx context.Context) time.Duration
}
21 changes: 11 additions & 10 deletions mining/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ import (

var log = logging.Logger("mining")

// DefaultBlockTime is the estimated proving period time.
// We define this so that we can fake mining in the current incomplete system.
const DefaultBlockTime = 30 * time.Second

// Output is the result of a single mining run. It has either a new
// block or an error, mimicing the golang (retVal, error) pattern.
// If a mining run's context is canceled there is no output.
Expand Down Expand Up @@ -72,8 +68,14 @@ type MessageApplier interface {
ApplyMessagesAndPayRewards(ctx context.Context, st state.Tree, vms vm.StorageMap, messages []*types.SignedMessage, minerOwnerAddr address.Address, bh *types.BlockHeight, ancestors []types.TipSet) (consensus.ApplyMessagesResponse, error)
}

type workerPorcelainAPI interface {
BlockTime(ctx context.Context) time.Duration
}

// DefaultWorker runs a mining job.
type DefaultWorker struct {
api workerPorcelainAPI

createPoSTFunc DoSomeWorkFunc
minerAddr address.Address
minerOwnerAddr address.Address
Expand All @@ -91,7 +93,6 @@ type DefaultWorker struct {
powerTable consensus.PowerTableView
blockstore blockstore.Blockstore
cstore *hamt.CborIpldStore
blockTime time.Duration
}

// NewDefaultWorker instantiates a new Worker.
Expand All @@ -107,7 +108,7 @@ func NewDefaultWorker(messageSource MessageSource,
minerOwner address.Address,
minerPubKey []byte,
workerSigner consensus.TicketSigner,
bt time.Duration) *DefaultWorker {
api workerPorcelainAPI) *DefaultWorker {

w := NewDefaultWorkerWithDeps(messageSource,
getStateTree,
Expand All @@ -121,7 +122,7 @@ func NewDefaultWorker(messageSource MessageSource,
minerOwner,
minerPubKey,
workerSigner,
bt,
api,
func() {})

// TODO: create real PoST.
Expand All @@ -144,9 +145,10 @@ func NewDefaultWorkerWithDeps(messageSource MessageSource,
minerOwner address.Address,
minerPubKey []byte,
workerSigner consensus.TicketSigner,
bt time.Duration,
api workerPorcelainAPI,
createPoST DoSomeWorkFunc) *DefaultWorker {
return &DefaultWorker{
api: api,
getStateTree: getStateTree,
getWeight: getWeight,
getAncestors: getAncestors,
Expand All @@ -159,7 +161,6 @@ func NewDefaultWorkerWithDeps(messageSource MessageSource,
minerAddr: miner,
minerOwnerAddr: minerOwner,
minerPubKey: minerPubKey,
blockTime: bt,
workerSigner: workerSigner,
}
}
Expand Down Expand Up @@ -259,5 +260,5 @@ func createProof(challengeSeed types.PoStChallengeSeed, createPoST DoSomeWorkFun
// fakeCreatePoST is the default implementation of DoSomeWorkFunc.
// It simply sleeps for the blockTime.
func (w *DefaultWorker) fakeCreatePoST() {
time.Sleep(w.blockTime)
time.Sleep(w.api.BlockTime(context.Background()))
}
16 changes: 8 additions & 8 deletions mining/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func Test_Mine(t *testing.T) {
outCh := make(chan mining.Output)
worker := mining.NewDefaultWorkerWithDeps(
pool, getStateTree, getWeightTest, getAncestors, th.NewTestProcessor(), mining.NewTestPowerTableView(1),
bs, cst, minerAddr, minerOwnerAddr, blockSignerAddr, mockSigner, th.BlockTimeTest,
bs, cst, minerAddr, minerOwnerAddr, blockSignerAddr, mockSigner, th.NewDefaultTestWorkerPorcelainAPI(),
CreatePoSTFunc)

go worker.Mine(ctx, tipSet, 0, outCh)
Expand All @@ -70,7 +70,7 @@ func Test_Mine(t *testing.T) {
doSomeWorkCalled = false
ctx, cancel := context.WithCancel(context.Background())
worker := mining.NewDefaultWorkerWithDeps(pool, makeExplodingGetStateTree(st), getWeightTest, getAncestors, th.NewTestProcessor(),
mining.NewTestPowerTableView(1), bs, cst, minerAddr, minerOwnerAddr, blockSignerAddr, mockSigner, th.BlockTimeTest, CreatePoSTFunc)
mining.NewTestPowerTableView(1), bs, cst, minerAddr, minerOwnerAddr, blockSignerAddr, mockSigner, th.NewDefaultTestWorkerPorcelainAPI(), CreatePoSTFunc)
outCh := make(chan mining.Output)
doSomeWorkCalled = false
go worker.Mine(ctx, tipSet, 0, outCh)
Expand All @@ -85,7 +85,7 @@ func Test_Mine(t *testing.T) {
doSomeWorkCalled = false
ctx, cancel := context.WithCancel(context.Background())
worker := mining.NewDefaultWorkerWithDeps(pool, getStateTree, getWeightTest, getAncestors, th.NewTestProcessor(),
mining.NewTestPowerTableView(1), bs, cst, minerAddr, minerOwnerAddr, blockSignerAddr, mockSigner, th.BlockTimeTest, CreatePoSTFunc)
mining.NewTestPowerTableView(1), bs, cst, minerAddr, minerOwnerAddr, blockSignerAddr, mockSigner, th.NewDefaultTestWorkerPorcelainAPI(), CreatePoSTFunc)
input := types.TipSet{}
outCh := make(chan mining.Output)
go worker.Mine(ctx, input, 0, outCh)
Expand Down Expand Up @@ -212,7 +212,7 @@ func TestGenerateMultiBlockTipSet(t *testing.T) {
minerOwnerAddr := addrs[3]

worker := mining.NewDefaultWorkerWithDeps(pool, getStateTree, getWeightTest, getAncestors, th.NewTestProcessor(),
&th.TestView{}, bs, cst, minerAddr, minerOwnerAddr, blockSignerAddr, mockSigner, th.BlockTimeTest, CreatePoSTFunc)
&th.TestView{}, bs, cst, minerAddr, minerOwnerAddr, blockSignerAddr, mockSigner, th.NewDefaultTestWorkerPorcelainAPI(), CreatePoSTFunc)

parents := types.NewSortedCidSet(newCid())
stateRoot := newCid()
Expand Down Expand Up @@ -255,7 +255,7 @@ func TestGeneratePoolBlockResults(t *testing.T) {
return nil, nil
}
worker := mining.NewDefaultWorkerWithDeps(pool, getStateTree, getWeightTest, getAncestors, consensus.NewDefaultProcessor(),
&th.TestView{}, bs, cst, addrs[4], addrs[3], blockSignerAddr, mockSigner, th.BlockTimeTest, CreatePoSTFunc)
&th.TestView{}, bs, cst, addrs[4], addrs[3], blockSignerAddr, mockSigner, th.NewDefaultTestWorkerPorcelainAPI(), CreatePoSTFunc)

// addr3 doesn't correspond to an extant account, so this will trigger errAccountNotFound -- a temporary failure.
msg1 := types.NewMessage(addrs[2], addrs[0], 0, types.ZeroAttoFIL, "", nil)
Expand Down Expand Up @@ -337,7 +337,7 @@ func TestGenerateSetsBasicFields(t *testing.T) {
minerAddr := addrs[4]
minerOwnerAddr := addrs[3]
worker := mining.NewDefaultWorkerWithDeps(pool, getStateTree, getWeightTest, getAncestors, consensus.NewDefaultProcessor(),
&th.TestView{}, bs, cst, minerAddr, minerOwnerAddr, blockSignerAddr, mockSigner, th.BlockTimeTest, CreatePoSTFunc)
&th.TestView{}, bs, cst, minerAddr, minerOwnerAddr, blockSignerAddr, mockSigner, th.NewDefaultTestWorkerPorcelainAPI(), CreatePoSTFunc)

h := types.Uint64(100)
w := types.Uint64(1000)
Expand Down Expand Up @@ -379,7 +379,7 @@ func TestGenerateWithoutMessages(t *testing.T) {
return nil, nil
}
worker := mining.NewDefaultWorkerWithDeps(pool, getStateTree, getWeightTest, getAncestors, consensus.NewDefaultProcessor(),
&th.TestView{}, bs, cst, addrs[4], addrs[3], blockSignerAddr, mockSigner, th.BlockTimeTest, CreatePoSTFunc)
&th.TestView{}, bs, cst, addrs[4], addrs[3], blockSignerAddr, mockSigner, th.NewDefaultTestWorkerPorcelainAPI(), CreatePoSTFunc)

assert.Len(t, pool.Pending(), 0)
baseBlock := types.Block{
Expand Down Expand Up @@ -413,7 +413,7 @@ func TestGenerateError(t *testing.T) {
}
worker := mining.NewDefaultWorkerWithDeps(pool, makeExplodingGetStateTree(st), getWeightTest, getAncestors,
consensus.NewDefaultProcessor(),
&th.TestView{}, bs, cst, addrs[4], addrs[3], blockSignerAddr, mockSigner, th.BlockTimeTest, CreatePoSTFunc)
&th.TestView{}, bs, cst, addrs[4], addrs[3], blockSignerAddr, mockSigner, th.NewDefaultTestWorkerPorcelainAPI(), CreatePoSTFunc)

// This is actually okay and should result in a receipt
msg := types.NewMessage(addrs[0], addrs[1], 0, types.ZeroAttoFIL, "", nil)
Expand Down
Loading

0 comments on commit 8ac5b9f

Please sign in to comment.