/
mining.go
116 lines (101 loc) · 3.59 KB
/
mining.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package testhelpers
import (
"context"
"crypto/rand"
"time"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/specs-actors/actors/abi"
"github.com/filecoin-project/specs-actors/actors/abi/big"
acrypto "github.com/filecoin-project/specs-actors/actors/crypto"
"github.com/filecoin-project/go-filecoin/internal/pkg/block"
"github.com/filecoin-project/go-filecoin/internal/pkg/consensus"
"github.com/filecoin-project/go-filecoin/internal/pkg/state"
"github.com/filecoin-project/go-filecoin/internal/pkg/types"
)
// BlockTimeTest is the block time used by workers during testing.
const BlockTimeTest = time.Second
// FakeWorkerPorcelainAPI implements the WorkerPorcelainAPI>
type FakeWorkerPorcelainAPI struct {
blockTime time.Duration
stateView *state.FakeStateView
rnd consensus.ChainRandomness
}
// NewDefaultFakeWorkerPorcelainAPI returns a FakeWorkerPorcelainAPI.
func NewDefaultFakeWorkerPorcelainAPI(signer address.Address, rnd consensus.ChainRandomness) *FakeWorkerPorcelainAPI {
return &FakeWorkerPorcelainAPI{
blockTime: BlockTimeTest,
stateView: &state.FakeStateView{
Power: &state.NetworkPower{
RawBytePower: big.NewInt(1),
QualityAdjustedPower: big.NewInt(1),
MinerCount: 0,
MinPowerMinerCount: 0,
},
Miners: map[address.Address]*state.FakeMinerState{},
},
rnd: rnd,
}
}
// NewFakeWorkerPorcelainAPI produces an api suitable to use as the worker's porcelain api.
func NewFakeWorkerPorcelainAPI(rnd consensus.ChainRandomness, totalPower uint64, minerToWorker map[address.Address]address.Address) *FakeWorkerPorcelainAPI {
f := &FakeWorkerPorcelainAPI{
blockTime: BlockTimeTest,
stateView: &state.FakeStateView{
Power: &state.NetworkPower{
RawBytePower: big.NewIntUnsigned(totalPower),
QualityAdjustedPower: big.NewIntUnsigned(totalPower),
MinerCount: 0,
MinPowerMinerCount: 0,
},
Miners: map[address.Address]*state.FakeMinerState{},
},
rnd: rnd,
}
for k, v := range minerToWorker {
f.stateView.Miners[k] = &state.FakeMinerState{
Owner: v,
Worker: v,
ClaimedRawPower: big.Zero(),
ClaimedQAPower: big.Zero(),
PledgeRequirement: big.Zero(),
PledgeBalance: big.Zero(),
}
}
return f
}
// BlockTime returns the blocktime FakeWorkerPorcelainAPI is configured with.
func (t *FakeWorkerPorcelainAPI) BlockTime() time.Duration {
return t.blockTime
}
// PowerStateView returns the state view.
func (t *FakeWorkerPorcelainAPI) PowerStateView(_ block.TipSetKey) (consensus.PowerStateView, error) {
return t.stateView, nil
}
func (t *FakeWorkerPorcelainAPI) FaultsStateView(_ block.TipSetKey) (consensus.FaultStateView, error) {
return t.stateView, nil
}
func (t *FakeWorkerPorcelainAPI) SampleChainRandomness(ctx context.Context, head block.TipSetKey, tag acrypto.DomainSeparationTag,
epoch abi.ChainEpoch, entropy []byte) (abi.Randomness, error) {
return t.rnd.SampleChainRandomness(ctx, head, tag, epoch, entropy)
}
// MakeCommitment creates a random commitment.
func MakeCommitment() []byte {
return MakeRandomBytes(32)
}
// MakeCommitments creates three random commitments for constructing a
// types.Commitments.
func MakeCommitments() types.Commitments {
comms := types.Commitments{}
copy(comms.CommD[:], MakeCommitment()[:])
copy(comms.CommR[:], MakeCommitment()[:])
copy(comms.CommRStar[:], MakeCommitment()[:])
return comms
}
// MakeRandomBytes generates a randomized byte slice of size 'size'
func MakeRandomBytes(size int) []byte {
comm := make([]byte, size)
if _, err := rand.Read(comm); err != nil {
panic(err)
}
return comm
}