Skip to content

Commit

Permalink
Merge pull request #76 from filecoin-project/feat/mine-natural-time
Browse files Browse the repository at this point in the history
Option to mine in natural time
  • Loading branch information
vyzo authored Jun 29, 2020
2 parents 6c8abeb + 6b44e6d commit 24730e1
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 38 deletions.
56 changes: 31 additions & 25 deletions lotus-soup/common_roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,33 +43,40 @@ func runMiner(t *TestEnvironment) error {
// mine / stop mining
mine := true
done := make(chan struct{})
go func() {
defer close(done)
var i int
for i = 0; mine; i++ {
// synchronize all miners to mine the next block
t.RecordMessage("synchronizing all miners to mine next block [%d]", i)
stateMineNext := sync.State(fmt.Sprintf("mine-block-%d", i))
t.SyncClient.MustSignalAndWait(ctx, stateMineNext, miners)

ch := make(chan struct{})
err := miner.MineOne(ctx, func(mined bool) {
if mined {
t.D().Counter(fmt.Sprintf("block.mine,miner=%s", myActorAddr)).Inc(1)

if miner.MineOne != nil {
go func() {
defer t.RecordMessage("shutting down mining")
defer close(done)

var i int
for i = 0; mine; i++ {
// synchronize all miners to mine the next block
t.RecordMessage("synchronizing all miners to mine next block [%d]", i)
stateMineNext := sync.State(fmt.Sprintf("mine-block-%d", i))
t.SyncClient.MustSignalAndWait(ctx, stateMineNext, miners)

ch := make(chan struct{})
err := miner.MineOne(ctx, func(mined bool) {
if mined {
t.D().Counter(fmt.Sprintf("block.mine,miner=%s", myActorAddr)).Inc(1)
}
close(ch)
})
if err != nil {
panic(err)
}
close(ch)
})
if err != nil {
panic(err)
<-ch
}
<-ch
}

// signal the last block to make sure no miners are left stuck waiting for the next block signal
// while the others have stopped
stateMineLast := sync.State(fmt.Sprintf("mine-block-%d", i))
t.SyncClient.MustSignalEntry(ctx, stateMineLast)
}()
// signal the last block to make sure no miners are left stuck waiting for the next block signal
// while the others have stopped
stateMineLast := sync.State(fmt.Sprintf("mine-block-%d", i))
t.SyncClient.MustSignalEntry(ctx, stateMineLast)
}()
} else {
close(done)
}

// wait for a signal from all clients to stop mining
err = <-t.SyncClient.MustBarrier(ctx, stateStopMining, clients).C
Expand All @@ -78,7 +85,6 @@ func runMiner(t *TestEnvironment) error {
}

mine = false
t.RecordMessage("shutting down mining")
<-done

t.SyncClient.MustSignalAndWait(ctx, stateDone, t.TestInstanceCount)
Expand Down
49 changes: 49 additions & 0 deletions lotus-soup/compositions/composition-natural.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
[metadata]
name = "lotus-soup"
author = ""

[global]
plan = "lotus-soup"
case = "lotus-baseline"
total_instances = 6
builder = "docker:go"
runner = "local:docker"

[global.build_config]
enable_go_build_cache = true

[global.run.test_params]
clients = "3"
miners = "2"
genesis_timestamp_offset = "100000"
balance = "2000000000"
sectors = "10"
random_beacon_type = "mock"

[[groups]]
id = "bootstrapper"
[groups.instances]
count = 1
percentage = 0.0
[groups.run]
[groups.run.test_params]
role = "bootstrapper"

[[groups]]
id = "miners"
[groups.instances]
count = 2
percentage = 0.0
[groups.run]
[groups.run.test_params]
role = "miner"
mining_mode = "natural"

[[groups]]
id = "clients"
[groups.instances]
count = 3
percentage = 0.0
[groups.run]
[groups.run.test_params]
role = "client"
3 changes: 3 additions & 0 deletions lotus-soup/manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,6 @@ instances = { min = 1, max = 100, default = 5 }

# Params relevant to pubsub tracing
enable_pubsub_tracer = { type = "bool", default = false }

# Mining Mode: synchronized -vs- natural time
mining_mode = { type = "enum", default = "synchronized", options = ["synchronized", "natural"] }
31 changes: 18 additions & 13 deletions lotus-soup/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,15 +397,29 @@ func prepareMiner(t *TestEnvironment) (*Node, error) {
return nil, err
}

mineBlock := make(chan func(bool))
stop2, err := node.New(context.Background(),
minerOpts := []node.Option{
node.StorageMiner(&n.minerApi),
node.Online(),
node.Repo(minerRepo),
node.Override(new(api.FullNode), n.fullApi),
node.Override(new(*miner.Miner), miner.NewTestMiner(mineBlock, minerAddr)),
withMinerListenAddress(minerIP),
)
}

if t.StringParam("mining_mode") != "natural" {
mineBlock := make(chan func(bool))
minerOpts = append(minerOpts,
node.Override(new(*miner.Miner), miner.NewTestMiner(mineBlock, minerAddr)))
n.MineOne = func(ctx context.Context, cb func(bool)) error {
select {
case mineBlock <- cb:
return nil
case <-ctx.Done():
return ctx.Err()
}
}
}

stop2, err := node.New(context.Background(), minerOpts...)
if err != nil {
stop1(context.TODO())
return nil, err
Expand All @@ -430,15 +444,6 @@ func prepareMiner(t *TestEnvironment) (*Node, error) {
panic(err)
}

n.MineOne = func(ctx context.Context, cb func(bool)) error {
select {
case mineBlock <- cb:
return nil
case <-ctx.Done():
return ctx.Err()
}
}

// add local storage for presealed sectors
err = n.minerApi.StorageAddLocal(ctx, presealDir)
if err != nil {
Expand Down

0 comments on commit 24730e1

Please sign in to comment.