lotus/chain/gen/genesis/miners.go

257 lines
8.2 KiB
Go
Raw Normal View History

2020-02-11 20:48:03 +00:00
package genesis
import (
"bytes"
"context"
"fmt"
"math/rand"
2020-04-10 21:07:18 +00:00
"github.com/ipfs/go-cid"
2020-02-18 21:37:59 +00:00
cbor "github.com/ipfs/go-ipld-cbor"
2020-04-20 13:54:06 +00:00
cbg "github.com/whyrusleeping/cbor-gen"
2020-04-10 21:07:18 +00:00
"golang.org/x/xerrors"
2020-02-11 20:48:03 +00:00
"github.com/filecoin-project/go-address"
2020-04-29 18:06:05 +00:00
"github.com/filecoin-project/sector-storage/ffiwrapper"
2020-02-11 20:48:03 +00:00
"github.com/filecoin-project/specs-actors/actors/abi"
"github.com/filecoin-project/specs-actors/actors/abi/big"
"github.com/filecoin-project/specs-actors/actors/builtin"
2020-02-12 02:13:00 +00:00
"github.com/filecoin-project/specs-actors/actors/builtin/market"
2020-02-11 20:48:03 +00:00
"github.com/filecoin-project/specs-actors/actors/builtin/miner"
2020-04-10 21:07:18 +00:00
"github.com/filecoin-project/specs-actors/actors/builtin/power"
2020-02-12 02:13:00 +00:00
"github.com/filecoin-project/specs-actors/actors/crypto"
"github.com/filecoin-project/specs-actors/actors/runtime"
2020-02-11 20:48:03 +00:00
2020-02-18 21:37:59 +00:00
"github.com/filecoin-project/lotus/chain/state"
2020-02-11 20:48:03 +00:00
"github.com/filecoin-project/lotus/chain/store"
2020-04-10 20:04:04 +00:00
"github.com/filecoin-project/lotus/chain/types"
2020-02-11 20:48:03 +00:00
"github.com/filecoin-project/lotus/chain/vm"
"github.com/filecoin-project/lotus/genesis"
)
func MinerAddress(genesisIndex uint64) address.Address {
maddr, err := address.NewIDAddress(MinerStart + genesisIndex)
if err != nil {
panic(err)
}
return maddr
}
type fakedSigSyscalls struct {
runtime.Syscalls
}
func (fss *fakedSigSyscalls) VerifySignature(signature crypto.Signature, signer address.Address, plaintext []byte) error {
return nil
}
2020-02-12 02:13:00 +00:00
func SetupStorageMiners(ctx context.Context, cs *store.ChainStore, sroot cid.Cid, miners []genesis.Miner) (cid.Cid, error) {
vm, err := vm.NewVM(sroot, 0, &fakeRand{}, cs.Blockstore(), &fakedSigSyscalls{cs.VMSys()})
2020-02-11 20:48:03 +00:00
if err != nil {
2020-02-12 02:13:00 +00:00
return cid.Undef, xerrors.Errorf("failed to create NewVM: %w", err)
2020-02-11 20:48:03 +00:00
}
2020-02-12 02:13:00 +00:00
if len(miners) == 0 {
return cid.Undef, xerrors.New("no genesis miners")
2020-02-11 20:48:03 +00:00
}
2020-02-12 02:13:00 +00:00
for i, m := range miners {
// Create miner through power actor
2020-02-11 20:48:03 +00:00
2020-04-29 18:06:05 +00:00
spt, err := ffiwrapper.SealProofTypeFromSectorSize(m.SectorSize)
if err != nil {
return cid.Undef, err
}
2020-02-12 02:13:00 +00:00
var maddr address.Address
{
2020-02-14 21:38:18 +00:00
constructorParams := &power.CreateMinerParams{
2020-04-29 18:06:05 +00:00
Owner: m.Worker,
Worker: m.Worker,
Peer: m.PeerId,
SealProofType: spt,
2020-02-11 20:48:03 +00:00
}
2020-02-12 02:13:00 +00:00
params := mustEnc(constructorParams)
2020-02-25 20:54:58 +00:00
rval, err := doExecValue(ctx, vm, builtin.StoragePowerActorAddr, m.Owner, m.PowerBalance, builtin.MethodsPower.CreateMiner, params)
2020-02-12 02:13:00 +00:00
if err != nil {
return cid.Undef, xerrors.Errorf("failed to create genesis miner: %w", err)
}
2020-02-11 20:48:03 +00:00
var ma power.CreateMinerReturn
if err := ma.UnmarshalCBOR(bytes.NewReader(rval)); err != nil {
2020-04-21 14:32:17 +00:00
return cid.Undef, xerrors.Errorf("unmarshaling CreateMinerReturn: %w", err)
2020-02-12 02:13:00 +00:00
}
expma := MinerAddress(uint64(i))
if ma.IDAddress != expma {
return cid.Undef, xerrors.Errorf("miner assigned wrong address: %s != %s", ma.IDAddress, expma)
2020-02-11 20:48:03 +00:00
}
maddr = ma.IDAddress
2020-02-11 20:48:03 +00:00
}
2020-02-12 02:13:00 +00:00
// Add market funds
2020-02-11 20:48:03 +00:00
2020-02-12 02:13:00 +00:00
{
params := mustEnc(&maddr)
2020-02-25 20:54:58 +00:00
_, err := doExecValue(ctx, vm, builtin.StorageMarketActorAddr, m.Worker, m.MarketBalance, builtin.MethodsMarket.AddBalance, params)
2020-02-12 02:13:00 +00:00
if err != nil {
return cid.Undef, xerrors.Errorf("failed to create genesis miner: %w", err)
}
2020-02-11 20:48:03 +00:00
}
2020-02-21 16:57:40 +00:00
{
params := mustEnc(&m.Worker)
2020-02-25 20:54:58 +00:00
_, err := doExecValue(ctx, vm, builtin.StorageMarketActorAddr, m.Worker, big.Zero(), builtin.MethodsMarket.AddBalance, params)
2020-02-21 16:57:40 +00:00
if err != nil {
return cid.Undef, xerrors.Errorf("failed to create genesis miner: %w", err)
}
}
2020-02-11 20:48:03 +00:00
2020-02-12 02:13:00 +00:00
// Publish preseal deals
2020-02-11 20:48:03 +00:00
2020-02-12 02:13:00 +00:00
var dealIDs []abi.DealID
{
2020-04-20 13:54:06 +00:00
publish := func(params *market.PublishStorageDealsParams) error {
fmt.Printf("publishing %d storage deals on miner %s with worker %s\n", len(params.Deals), params.Deals[0].Proposal.Provider, m.Worker)
ret, err := doExecValue(ctx, vm, builtin.StorageMarketActorAddr, m.Worker, big.Zero(), builtin.MethodsMarket.PublishStorageDeals, mustEnc(params))
if err != nil {
return xerrors.Errorf("failed to create genesis miner: %w", err)
}
var ids market.PublishStorageDealsReturn
if err := ids.UnmarshalCBOR(bytes.NewReader(ret)); err != nil {
2020-04-21 14:32:17 +00:00
return xerrors.Errorf("unmarsahling publishStorageDeals result: %w", err)
2020-04-20 13:54:06 +00:00
}
dealIDs = append(dealIDs, ids.IDs...)
return nil
}
2020-02-12 02:13:00 +00:00
params := &market.PublishStorageDealsParams{}
for _, preseal := range m.Sectors {
params.Deals = append(params.Deals, market.ClientDealProposal{
Proposal: preseal.Deal,
2020-02-21 19:28:20 +00:00
ClientSignature: crypto.Signature{Type: crypto.SigTypeBLS}, // TODO: do we want to sign these? Or do we want to fake signatures for genesis setup?
2020-02-12 02:13:00 +00:00
})
2020-02-11 20:48:03 +00:00
2020-04-20 13:54:06 +00:00
if len(params.Deals) == cbg.MaxLength {
if err := publish(params); err != nil {
return cid.Undef, err
}
params = &market.PublishStorageDealsParams{}
}
2020-02-11 20:48:03 +00:00
}
2020-04-20 13:54:06 +00:00
if len(params.Deals) > 0 {
if err := publish(params); err != nil {
return cid.Undef, err
}
}
2020-02-11 20:48:03 +00:00
}
2020-02-18 21:37:59 +00:00
// Commit sectors
for pi, preseal := range m.Sectors {
// TODO: Maybe check seal (Can just be snark inputs, doesn't go into the genesis file)
2020-02-19 19:26:11 +00:00
// check deals, get dealWeight
2020-04-21 14:32:17 +00:00
var dealWeight market.VerifyDealsOnSectorProveCommitReturn
2020-02-18 21:37:59 +00:00
{
params := &market.VerifyDealsOnSectorProveCommitParams{
DealIDs: []abi.DealID{dealIDs[pi]},
SectorExpiry: preseal.Deal.EndEpoch,
}
ret, err := doExecValue(ctx, vm, builtin.StorageMarketActorAddr, maddr, big.Zero(), builtin.MethodsMarket.VerifyDealsOnSectorProveCommit, mustEnc(params))
if err != nil {
return cid.Undef, xerrors.Errorf("failed to verify preseal deals miner: %w", err)
}
if err := dealWeight.UnmarshalCBOR(bytes.NewReader(ret)); err != nil {
2020-04-21 14:32:17 +00:00
return cid.Undef, xerrors.Errorf("unmarshaling market onProveCommit result: %w", err)
2020-02-18 21:37:59 +00:00
}
}
2020-02-19 19:26:11 +00:00
// update power claims
2020-02-18 21:37:59 +00:00
{
err = vm.MutateState(ctx, builtin.StoragePowerActorAddr, func(cst cbor.IpldStore, st *power.State) error {
weight := &power.SectorStorageWeightDesc{
2020-04-21 17:23:49 +00:00
SectorSize: m.SectorSize,
Duration: preseal.Deal.Duration(),
DealWeight: dealWeight.DealWeight,
VerifiedDealWeight: dealWeight.VerifiedDealWeight,
2020-02-18 21:37:59 +00:00
}
2020-04-10 20:04:04 +00:00
qapower := power.QAPowerForWeight(weight)
2020-04-13 21:05:34 +00:00
err := st.AddToClaim(&state.AdtStore{cst}, maddr, types.NewInt(uint64(weight.SectorSize)), qapower)
2020-02-18 21:37:59 +00:00
if err != nil {
return xerrors.Errorf("add to claim: %w", err)
}
2020-04-10 20:04:04 +00:00
fmt.Println("Added weight to claim: ", st.TotalRawBytePower, st.TotalQualityAdjPower)
2020-02-18 21:37:59 +00:00
return nil
})
if err != nil {
return cid.Undef, xerrors.Errorf("register power claim in power actor: %w", err)
}
}
2020-02-18 21:37:59 +00:00
2020-02-19 19:26:11 +00:00
// Put sectors to miner sector sets
2020-02-18 21:37:59 +00:00
{
newSectorInfo := &miner.SectorOnChainInfo{
Info: miner.SectorPreCommitInfo{
RegisteredProof: preseal.ProofType,
SectorNumber: preseal.SectorID,
SealedCID: preseal.CommR,
2020-04-21 21:38:26 +00:00
SealRandEpoch: 0,
DealIDs: []abi.DealID{dealIDs[pi]},
Expiration: preseal.Deal.EndEpoch,
2020-02-18 21:37:59 +00:00
},
2020-04-21 21:38:26 +00:00
ActivationEpoch: 0,
2020-04-21 17:23:49 +00:00
DealWeight: dealWeight.DealWeight,
VerifiedDealWeight: dealWeight.VerifiedDealWeight,
2020-02-18 21:37:59 +00:00
}
err = vm.MutateState(ctx, maddr, func(cst cbor.IpldStore, st *miner.State) error {
store := &state.AdtStore{cst}
if err = st.PutSector(store, newSectorInfo); err != nil {
2020-04-21 12:36:54 +00:00
return xerrors.Errorf("failed to put sector: %v", err)
}
if err := st.AddNewSectors(newSectorInfo.Info.SectorNumber); err != nil {
return xerrors.Errorf("failed to add NewSector: %w", err)
2020-02-18 21:37:59 +00:00
}
return nil
})
2020-02-19 19:26:11 +00:00
if err != nil {
return cid.Cid{}, xerrors.Errorf("put to sset: %w", err)
}
2020-02-18 21:37:59 +00:00
}
2020-02-11 20:48:03 +00:00
}
2020-02-18 21:37:59 +00:00
2020-02-11 20:48:03 +00:00
}
2020-03-11 07:29:17 +00:00
// TODO: to avoid division by zero, we set the initial power actor power to 1, this adjusts that back down so the accounting is accurate.
err = vm.MutateState(ctx, builtin.StoragePowerActorAddr, func(cst cbor.IpldStore, st *power.State) error {
2020-04-10 20:04:04 +00:00
st.TotalQualityAdjPower = big.Sub(st.TotalQualityAdjPower, big.NewInt(1))
2020-03-11 07:29:17 +00:00
return nil
})
2020-02-11 20:48:03 +00:00
c, err := vm.Flush(ctx)
2020-04-21 14:32:17 +00:00
if err != nil {
2020-04-21 21:38:26 +00:00
return cid.Undef, xerrors.Errorf("flushing vm: %w", err)
2020-04-21 14:32:17 +00:00
}
return c, nil
2020-02-11 20:48:03 +00:00
}
// TODO: copied from actors test harness, deduplicate or remove from here
type fakeRand struct{}
2020-04-08 15:11:42 +00:00
func (fr *fakeRand) GetRandomness(ctx context.Context, personalization crypto.DomainSeparationTag, randEpoch abi.ChainEpoch, entropy []byte) ([]byte, error) {
out := make([]byte, 32)
_, _ = rand.New(rand.NewSource(int64(randEpoch))).Read(out)
return out, nil
}