-
Notifications
You must be signed in to change notification settings - Fork 467
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
move BlockTime to plumbing add BlockClock interface #2894
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ import ( | |
"fmt" | ||
"time" | ||
|
||
"github.com/filecoin-project/go-filecoin/plumbing/clock" | ||
"github.com/filecoin-project/go-filecoin/types" | ||
) | ||
|
||
|
@@ -49,16 +50,14 @@ func (ebc *DefaultBlockValidationClock) EpochSeconds() uint64 { | |
|
||
// DefaultBlockValidator implements the BlockValidator interface. | ||
type DefaultBlockValidator struct { | ||
clock BlockValidationClock | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe we can replace the |
||
blockTime time.Duration | ||
clock clock.BlockClock | ||
} | ||
|
||
// NewDefaultBlockValidator returns a new DefaultBlockValidator. It uses `blkTime` | ||
// to validate blocks and uses the DefaultBlockValidationClock. | ||
func NewDefaultBlockValidator(blkTime time.Duration) *DefaultBlockValidator { | ||
func NewDefaultBlockValidator(c clock.BlockClock) *DefaultBlockValidator { | ||
return &DefaultBlockValidator{ | ||
clock: NewDefaultBlockValidationClock(), | ||
blockTime: blkTime, | ||
clock: c, | ||
} | ||
} | ||
|
||
|
@@ -83,5 +82,5 @@ func (dv *DefaultBlockValidator) ValidateSyntax(ctx context.Context, blk *types. | |
// BlockTime returns the block time the DefaultBlockValidator uses to validate | ||
/// blocks against. | ||
func (dv *DefaultBlockValidator) BlockTime() time.Duration { | ||
return dv.blockTime | ||
return dv.clock.BlockTime() | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
|
@@ -72,6 +68,10 @@ 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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could be extended to replace other fields on the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree it could. Furthermore I think the mining worker should eventually be code in the block miner protocol's I also agree all of this is out of scope of your PR. |
||
BlockTime(ctx context.Context) time.Duration | ||
} | ||
|
||
// DefaultWorker runs a mining job. | ||
type DefaultWorker struct { | ||
createPoSTFunc DoSomeWorkFunc | ||
|
@@ -91,7 +91,7 @@ type DefaultWorker struct { | |
powerTable consensus.PowerTableView | ||
blockstore blockstore.Blockstore | ||
cstore *hamt.CborIpldStore | ||
blockTime time.Duration | ||
clock workerPorcelainAPI | ||
} | ||
|
||
// NewDefaultWorker instantiates a new Worker. | ||
|
@@ -107,7 +107,7 @@ func NewDefaultWorker(messageSource MessageSource, | |
minerOwner address.Address, | ||
minerPubKey []byte, | ||
workerSigner consensus.TicketSigner, | ||
bt time.Duration) *DefaultWorker { | ||
clock workerPorcelainAPI) *DefaultWorker { | ||
|
||
w := NewDefaultWorkerWithDeps(messageSource, | ||
getStateTree, | ||
|
@@ -121,7 +121,7 @@ func NewDefaultWorker(messageSource MessageSource, | |
minerOwner, | ||
minerPubKey, | ||
workerSigner, | ||
bt, | ||
clock, | ||
func() {}) | ||
|
||
// TODO: create real PoST. | ||
|
@@ -144,22 +144,22 @@ func NewDefaultWorkerWithDeps(messageSource MessageSource, | |
minerOwner address.Address, | ||
minerPubKey []byte, | ||
workerSigner consensus.TicketSigner, | ||
bt time.Duration, | ||
clock workerPorcelainAPI, | ||
createPoST DoSomeWorkFunc) *DefaultWorker { | ||
return &DefaultWorker{ | ||
blockstore: bs, | ||
clock: clock, | ||
createPoSTFunc: createPoST, | ||
cstore: cst, | ||
getAncestors: getAncestors, | ||
getStateTree: getStateTree, | ||
getWeight: getWeight, | ||
getAncestors: getAncestors, | ||
messageSource: messageSource, | ||
processor: processor, | ||
powerTable: powerTable, | ||
blockstore: bs, | ||
cstore: cst, | ||
createPoSTFunc: createPoST, | ||
minerAddr: miner, | ||
minerOwnerAddr: minerOwner, | ||
minerPubKey: minerPubKey, | ||
blockTime: bt, | ||
powerTable: powerTable, | ||
processor: processor, | ||
workerSigner: workerSigner, | ||
} | ||
} | ||
|
@@ -259,5 +259,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.clock.BlockTime(context.Background())) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,6 +51,7 @@ import ( | |
"github.com/filecoin-project/go-filecoin/paths" | ||
"github.com/filecoin-project/go-filecoin/plumbing" | ||
"github.com/filecoin-project/go-filecoin/plumbing/cfg" | ||
"github.com/filecoin-project/go-filecoin/plumbing/clock" | ||
"github.com/filecoin-project/go-filecoin/plumbing/cst" | ||
"github.com/filecoin-project/go-filecoin/plumbing/dag" | ||
"github.com/filecoin-project/go-filecoin/plumbing/msg" | ||
|
@@ -126,7 +127,6 @@ type Node struct { | |
|
||
// Mining stuff. | ||
AddNewlyMinedBlock newBlockFunc | ||
blockTime time.Duration | ||
cancelMining context.CancelFunc | ||
MiningWorker mining.Worker | ||
MiningScheduler mining.Scheduler | ||
|
@@ -185,7 +185,7 @@ type Node struct { | |
|
||
// Config is a helper to aid in the construction of a filecoin node. | ||
type Config struct { | ||
BlockTime time.Duration | ||
Clock clock.BlockClock | ||
Libp2pOpts []libp2p.Option | ||
OfflineMode bool | ||
Verifier proofs.Verifier | ||
|
@@ -213,10 +213,10 @@ func IsRelay() ConfigOpt { | |
} | ||
} | ||
|
||
// BlockTime sets the blockTime. | ||
func BlockTime(blockTime time.Duration) ConfigOpt { | ||
// BlockClock sets the clock. | ||
func BlockClock(clk clock.BlockClock) ConfigOpt { | ||
return func(c *Config) error { | ||
c.BlockTime = blockTime | ||
c.Clock = clk | ||
return nil | ||
} | ||
} | ||
|
@@ -378,8 +378,14 @@ func (nc *Config) Build(ctx context.Context) (*Node, error) { | |
// set up pinger | ||
pingService := ping.NewPingService(peerHost) | ||
|
||
// DO NOT MERGE this code smells like poo, this smell exists elsewhere, | ||
// should have defaults set on it _before_ we get here? | ||
if nc.Clock == nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not a fan of this pattern, however since There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about passing an integer parsed from the CLI (or the default) on the node config and passing that to a clock constructor during the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You leave the concrete implementation of |
||
nc.Clock = clock.NewDefaultBlockClock() | ||
} | ||
|
||
// setup block validation | ||
blkValid := consensus.NewDefaultBlockValidator(nc.BlockTime) | ||
blkValid := consensus.NewDefaultBlockValidator(nc.Clock) | ||
|
||
// set up bitswap | ||
nwork := bsnet.NewFromIpfsHost(peerHost, router) | ||
|
@@ -440,6 +446,7 @@ func (nc *Config) Build(ctx context.Context) (*Node, error) { | |
PorcelainAPI := porcelain.New(plumbing.New(&plumbing.APIDeps{ | ||
Bitswap: bswap, | ||
Chain: chainState, | ||
Clock: nc.Clock, | ||
Config: cfg.NewConfig(nc.Repo), | ||
DAG: dag.NewDAG(merkledag.NewDAGService(bservice)), | ||
Deals: strgdls.New(nc.Repo.DealsDatastore()), | ||
|
@@ -470,7 +477,6 @@ func (nc *Config) Build(ctx context.Context) (*Node, error) { | |
PeerHost: peerHost, | ||
Repo: nc.Repo, | ||
Wallet: fcWallet, | ||
blockTime: nc.BlockTime, | ||
Router: router, | ||
} | ||
|
||
|
@@ -753,19 +759,9 @@ func (node *Node) miningAddress() (address.Address, error) { | |
// Note this is mocked behavior, in production this time is determined by how | ||
// long it takes to generate PoSTs. | ||
func (node *Node) MiningTimes() (time.Duration, time.Duration) { | ||
mineDelay := node.GetBlockTime() / mining.MineDelayConversionFactor | ||
return node.GetBlockTime(), mineDelay | ||
} | ||
|
||
// GetBlockTime returns the current block time. | ||
// TODO this should be surfaced somewhere in the plumbing API. | ||
func (node *Node) GetBlockTime() time.Duration { | ||
return node.blockTime | ||
} | ||
|
||
// SetBlockTime sets the block time. | ||
func (node *Node) SetBlockTime(blockTime time.Duration) { | ||
node.blockTime = blockTime | ||
blockTime := node.PorcelainAPI.BlockTime(context.Background()) | ||
mineDelay := blockTime / mining.MineDelayConversionFactor | ||
return blockTime, mineDelay | ||
} | ||
|
||
// StartMining causes the node to start feeding blocks to the mining worker and initializes | ||
|
@@ -1026,11 +1022,11 @@ func (node *Node) setupProtocols() error { | |
node.BlockMiningAPI = &blockMiningAPI | ||
|
||
// set up retrieval client and api | ||
retapi := retrieval.NewAPI(retrieval.NewClient(node.host, node.blockTime, node.PorcelainAPI)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎉 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: |
||
retapi := retrieval.NewAPI(retrieval.NewClient(node.host, node.PorcelainAPI)) | ||
node.RetrievalAPI = &retapi | ||
|
||
// set up storage client and api | ||
smc := storage.NewClient(node.blockTime, node.host, node.PorcelainAPI) | ||
smc := storage.NewClient(node.host, node.PorcelainAPI) | ||
smcAPI := storage.NewAPI(smc) | ||
node.StorageAPI = &smcAPI | ||
return nil | ||
|
@@ -1059,7 +1055,7 @@ func (node *Node) CreateMiningWorker(ctx context.Context) (mining.Worker, error) | |
return mining.NewDefaultWorker( | ||
node.Inbox.Pool(), node.getStateTree, node.getWeight, node.getAncestors, processor, node.PowerTable, | ||
node.Blockstore, node.CborStore(), minerAddr, minerOwnerAddr, minerPubKey, | ||
node.Wallet, node.blockTime), nil | ||
node.Wallet, node.PorcelainAPI), nil | ||
} | ||
|
||
// getStateFromKey returns the state tree based on tipset fetched with provided key tsKey | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is the first time plumbing has made its way into the consensus package. I am interested in the long term implications of this change. Does it mean consensus can't be part of plumbing itself? Does it mean consensus will have a stronger pull towards being put on plumbing?
It seems like there is lack of intentional design around the interactions between "core components" and the plumbing injection things. These are just general observations, no action required in this PR.