Skip to content

Commit 2c6f7bf

Browse files
authored
Move fake EC out of production API (#641)
Move fake EC, primarily used for testing out of production scope API and into an `internal` package. This simplifies the integration code in lots.
1 parent d58532b commit 2c6f7bf

File tree

6 files changed

+32
-27
lines changed

6 files changed

+32
-27
lines changed

cmd/f3/run.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import (
66
"os"
77

88
"github.com/filecoin-project/go-f3"
9-
"github.com/filecoin-project/go-f3/ec"
109
"github.com/filecoin-project/go-f3/gpbft"
10+
"github.com/filecoin-project/go-f3/internal/consensus"
1111
"github.com/filecoin-project/go-f3/manifest"
1212
"github.com/filecoin-project/go-f3/sim/signing"
1313
leveldb "github.com/ipfs/go-ds-leveldb"
@@ -112,7 +112,7 @@ var runCmd = cli.Command{
112112
id := c.Uint64("id")
113113
signingBackend.Allow(int(id))
114114

115-
ec := ec.NewFakeEC(ctx, 1, m.BootstrapEpoch, m.EC.Period, initialPowerTable)
115+
ec := consensus.NewFakeEC(ctx, 1, m.BootstrapEpoch, m.EC.Period, initialPowerTable)
116116

117117
module, err := f3.New(ctx, mprovider, ds, h, ps, signingBackend, ec)
118118
if err != nil {

ec/powerdelta_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/filecoin-project/go-f3/ec"
88
"github.com/filecoin-project/go-f3/gpbft"
9+
"github.com/filecoin-project/go-f3/internal/consensus"
910

1011
"github.com/stretchr/testify/require"
1112
)
@@ -29,7 +30,7 @@ var powerTableC = gpbft.PowerEntries{
2930
}
3031

3132
func TestReplacePowerTable(t *testing.T) {
32-
backend := ec.NewFakeEC(context.Background(), 0, 0, 30, powerTableA)
33+
backend := consensus.NewFakeEC(context.Background(), 0, 0, 30, powerTableA)
3334
modifiedBackend := ec.WithModifiedPower(backend, powerTableB, true)
3435

3536
head, err := modifiedBackend.GetHead(context.Background())
@@ -42,7 +43,7 @@ func TestReplacePowerTable(t *testing.T) {
4243
}
4344

4445
func TestModifyPowerTable(t *testing.T) {
45-
backend := ec.NewFakeEC(context.Background(), 0, 0, 30, powerTableA)
46+
backend := consensus.NewFakeEC(context.Background(), 0, 0, 30, powerTableA)
4647
modifiedBackend := ec.WithModifiedPower(backend, powerTableB, false)
4748

4849
head, err := modifiedBackend.GetHead(context.Background())
@@ -54,7 +55,7 @@ func TestModifyPowerTable(t *testing.T) {
5455
}
5556

5657
func TestBypassModifiedPowerTable(t *testing.T) {
57-
backend := ec.NewFakeEC(context.Background(), 0, 0, 30, powerTableA)
58+
backend := consensus.NewFakeEC(context.Background(), 0, 0, 30, powerTableA)
5859
modifiedBackend := ec.WithModifiedPower(backend, nil, false)
5960
require.Equal(t, backend, modifiedBackend)
6061
}

f3_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import (
88
"time"
99

1010
"github.com/filecoin-project/go-f3"
11-
"github.com/filecoin-project/go-f3/ec"
1211
"github.com/filecoin-project/go-f3/gpbft"
1312
"github.com/filecoin-project/go-f3/internal/clock"
13+
"github.com/filecoin-project/go-f3/internal/consensus"
1414
"github.com/filecoin-project/go-f3/internal/psutil"
1515
"github.com/filecoin-project/go-f3/manifest"
1616
"github.com/filecoin-project/go-f3/sim/signing"
@@ -290,7 +290,7 @@ type testEnv struct {
290290
testCtx context.Context
291291
signingBackend *signing.FakeBackend
292292
nodes []*testNode
293-
ec *ec.FakeEC
293+
ec *consensus.FakeEC
294294
manifestSender *manifest.ManifestSender
295295
net mocknet.Mocknet
296296
clock *clock.Mock
@@ -436,7 +436,7 @@ func newTestEnvironment(t *testing.T, n int, dynamicManifest bool) *testEnv {
436436
})
437437
}
438438
env.manifest = m
439-
env.ec = ec.NewFakeEC(ctx, 1, m.BootstrapEpoch+m.EC.Finality, m.EC.Period, initialPowerTable)
439+
env.ec = consensus.NewFakeEC(ctx, 1, m.BootstrapEpoch+m.EC.Finality, m.EC.Period, initialPowerTable)
440440

441441
var manifestServer peer.ID
442442
if dynamicManifest {

ec/fake_ec.go renamed to internal/consensus/fake_ec.go

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package ec
1+
package consensus
22

33
import (
44
"context"
@@ -7,14 +7,18 @@ import (
77
"sync"
88
"time"
99

10+
"github.com/filecoin-project/go-f3/ec"
1011
"golang.org/x/crypto/blake2b"
1112

1213
"github.com/filecoin-project/go-f3/gpbft"
1314
"github.com/filecoin-project/go-f3/internal/clock"
1415
mbase "github.com/multiformats/go-multibase"
1516
)
1617

17-
var _ Backend = (*FakeEC)(nil)
18+
var (
19+
_ ec.Backend = (*FakeEC)(nil)
20+
_ ec.TipSet = (*tipset)(nil)
21+
)
1822

1923
type FakeEC struct {
2024
clock clock.Clock
@@ -28,20 +32,20 @@ type FakeEC struct {
2832
pausedAt *time.Time
2933
}
3034

31-
type Tipset struct {
35+
type tipset struct {
3236
tsk []byte
3337
epoch int64
3438
timestamp time.Time
3539
}
3640

37-
func (ts *Tipset) Key() gpbft.TipSetKey {
41+
func (ts *tipset) Key() gpbft.TipSetKey {
3842
return ts.tsk
3943
}
4044

41-
func (ts *Tipset) Epoch() int64 {
45+
func (ts *tipset) Epoch() int64 {
4246
return ts.epoch
4347
}
44-
func (ts *Tipset) Beacon() []byte {
48+
func (ts *tipset) Beacon() []byte {
4549
h, err := blake2b.New256([]byte("beacon"))
4650
if err != nil {
4751
panic(err)
@@ -50,11 +54,11 @@ func (ts *Tipset) Beacon() []byte {
5054
return h.Sum(nil)
5155
}
5256

53-
func (ts *Tipset) Timestamp() time.Time {
57+
func (ts *tipset) Timestamp() time.Time {
5458
return ts.timestamp
5559
}
5660

57-
func (ts *Tipset) String() string {
61+
func (ts *tipset) String() string {
5862
res, _ := mbase.Encode(mbase.Base32, ts.tsk[:gpbft.CidMaxLen])
5963
for i := 1; i*gpbft.CidMaxLen < len(ts.tsk); i++ {
6064
enc, _ := mbase.Encode(mbase.Base32, ts.tsk[gpbft.CidMaxLen*i:gpbft.CidMaxLen*(i+1)])
@@ -77,7 +81,7 @@ func NewFakeEC(ctx context.Context, seed uint64, bootstrapEpoch int64, ecPeriod
7781

7882
var cidPrefixBytes = gpbft.CidPrefix.Bytes()
7983

80-
func (ec *FakeEC) genTipset(epoch int64) *Tipset {
84+
func (ec *FakeEC) genTipset(epoch int64) *tipset {
8185
h, err := blake2b.New256(ec.seed)
8286
if err != nil {
8387
panic(err)
@@ -107,15 +111,15 @@ func (ec *FakeEC) genTipset(epoch int64) *Tipset {
107111
tsk = append(tsk, cidPrefixBytes...)
108112
tsk = append(tsk, digest...)
109113
}
110-
return &Tipset{
114+
return &tipset{
111115
tsk: tsk,
112116
epoch: epoch,
113117
timestamp: ec.ecStart.Add(time.Duration(epoch) * ec.ecPeriod),
114118
}
115119
}
116120

117121
// GetTipsetByHeight should return a tipset or nil/empty byte array if it does not exists
118-
func (ec *FakeEC) GetTipsetByEpoch(ctx context.Context, epoch int64) (TipSet, error) {
122+
func (ec *FakeEC) GetTipsetByEpoch(ctx context.Context, epoch int64) (ec.TipSet, error) {
119123
if ec.GetCurrentHead() < epoch {
120124
return nil, fmt.Errorf("does not yet exist")
121125
}
@@ -127,8 +131,7 @@ func (ec *FakeEC) GetTipsetByEpoch(ctx context.Context, epoch int64) (TipSet, er
127131
return ts, nil
128132
}
129133

130-
func (ec *FakeEC) GetParent(ctx context.Context, ts TipSet) (TipSet, error) {
131-
134+
func (ec *FakeEC) GetParent(ctx context.Context, ts ec.TipSet) (ec.TipSet, error) {
132135
for epoch := ts.Epoch() - 1; epoch > 0; epoch-- {
133136
ts, err := ec.GetTipsetByEpoch(ctx, epoch)
134137
if err != nil {
@@ -168,15 +171,15 @@ func (ec *FakeEC) Resume() {
168171
ec.pausedAt = nil
169172
}
170173

171-
func (ec *FakeEC) GetHead(ctx context.Context) (TipSet, error) {
174+
func (ec *FakeEC) GetHead(ctx context.Context) (ec.TipSet, error) {
172175
return ec.GetTipsetByEpoch(ctx, ec.GetCurrentHead())
173176
}
174177

175-
func (ec *FakeEC) GetPowerTable(ctx context.Context, tsk gpbft.TipSetKey) (gpbft.PowerEntries, error) {
178+
func (ec *FakeEC) GetPowerTable(context.Context, gpbft.TipSetKey) (gpbft.PowerEntries, error) {
176179
return ec.initialPowerTable, nil
177180
}
178181

179-
func (ec *FakeEC) GetTipset(ctx context.Context, tsk gpbft.TipSetKey) (TipSet, error) {
182+
func (ec *FakeEC) GetTipset(_ context.Context, tsk gpbft.TipSetKey) (ec.TipSet, error) {
180183
epoch := binary.BigEndian.Uint64(tsk[6+32-8 : 6+32])
181184
return ec.genTipset(int64(epoch)), nil
182185
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package ec
1+
package consensus
22

33
import (
44
"testing"

internal/powerstore/powerstore_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/filecoin-project/go-f3/ec"
1313
"github.com/filecoin-project/go-f3/gpbft"
1414
"github.com/filecoin-project/go-f3/internal/clock"
15+
"github.com/filecoin-project/go-f3/internal/consensus"
1516
"github.com/filecoin-project/go-f3/internal/powerstore"
1617
"github.com/filecoin-project/go-f3/manifest"
1718
"github.com/filecoin-project/go-state-types/big"
@@ -23,7 +24,7 @@ import (
2324
)
2425

2526
type forgetfulEC struct {
26-
*ec.FakeEC
27+
*consensus.FakeEC
2728

2829
ecFinality int64
2930
}
@@ -68,7 +69,7 @@ func TestPowerStore(t *testing.T) {
6869
m := manifest.LocalDevnetManifest()
6970

7071
ec := &forgetfulEC{
71-
FakeEC: ec.NewFakeEC(ctx, 1234, m.BootstrapEpoch, m.EC.Period, basePowerTable),
72+
FakeEC: consensus.NewFakeEC(ctx, 1234, m.BootstrapEpoch, m.EC.Period, basePowerTable),
7273
ecFinality: m.EC.Finality,
7374
}
7475

0 commit comments

Comments
 (0)