From e9019c8bcc0232593fb11743f8853c3e3d174428 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Tue, 18 Feb 2020 22:37:59 +0100 Subject: [PATCH] Working Genesis Generator --- chain/gen/genesis/miners.go | 149 +++++++++++++++++++++++++++------ chain/gen/genesis/t04_power.go | 2 +- chain/state/statetree.go | 10 +-- chain/vm/vm.go | 32 +++++++ go.mod | 5 +- go.sum | 22 +---- 6 files changed, 164 insertions(+), 56 deletions(-) diff --git a/chain/gen/genesis/miners.go b/chain/gen/genesis/miners.go index 23a2ed8f8..acdf126ab 100644 --- a/chain/gen/genesis/miners.go +++ b/chain/gen/genesis/miners.go @@ -6,10 +6,12 @@ import ( "fmt" "math/rand" + cborutil "github.com/filecoin-project/go-cbor-util" + commcid "github.com/filecoin-project/go-fil-commcid" "github.com/filecoin-project/specs-actors/actors/builtin/power" + cbor "github.com/ipfs/go-ipld-cbor" "github.com/filecoin-project/go-address" - commcid "github.com/filecoin-project/go-fil-commcid" "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" @@ -20,6 +22,7 @@ import ( "golang.org/x/xerrors" "github.com/filecoin-project/lotus/chain/actors" + "github.com/filecoin-project/lotus/chain/state" "github.com/filecoin-project/lotus/chain/store" "github.com/filecoin-project/lotus/chain/vm" "github.com/filecoin-project/lotus/genesis" @@ -35,12 +38,21 @@ func MinerAddress(genesisIndex uint64) address.Address { } func SetupStorageMiners(ctx context.Context, cs *store.ChainStore, sroot cid.Cid, miners []genesis.Miner) (cid.Cid, error) { + networkPower := big.Zero() + for _, m := range miners { + networkPower = big.Add(networkPower, big.NewInt(int64(m.SectorSize)*int64(len(m.Sectors)))) + } vm, err := vm.NewVM(sroot, 0, &fakeRand{}, actors.SystemAddress, cs.Blockstore(), cs.VMSys()) if err != nil { return cid.Undef, xerrors.Errorf("failed to create NewVM: %w", err) } + err = vm.MutateState(ctx, builtin.StoragePowerActorAddr, func(cst cbor.IpldStore, st *power.State) error { + st.TotalNetworkPower = networkPower + return nil + }) + if len(miners) == 0 { return cid.Undef, xerrors.New("no genesis miners") } @@ -110,37 +122,122 @@ func SetupStorageMiners(ctx context.Context, cs *store.ChainStore, sroot cid.Cid dealIDs = ids.IDs } - // Publish preseals + // setup windowed post + { + err = vm.MutateState(ctx, maddr, func(cst cbor.IpldStore, st *miner.State) error { + // TODO: Randomize so all genesis miners don't fall on the same epoch + st.PoStState.ProvingPeriodStart = miner.ProvingPeriod + return nil + }) + payload, err := cborutil.Dump(&miner.CronEventPayload{ + EventType: miner.CronEventWindowedPoStExpiration, + }) + if err != nil { + return cid.Undef, err + } + params := &power.EnrollCronEventParams{ + EventEpoch: miner.ProvingPeriod, + Payload: payload, + } + + _, err = doExecValue(ctx, vm, builtin.StoragePowerActorAddr, maddr, big.Zero(), builtin.MethodsPower.EnrollCronEvent, mustEnc(params)) + if err != nil { + return cid.Undef, xerrors.Errorf("failed to verify preseal deals miner: %w", err) + } + } + + // Commit sectors for pi, preseal := range m.Sectors { - // Precommit - params := &miner.SectorPreCommitInfo{ - SectorNumber: preseal.SectorID, - SealedCID: commcid.ReplicaCommitmentV1ToCID(preseal.CommR[:]), - SealEpoch: 0, - DealIDs: []abi.DealID{dealIDs[pi]}, - Expiration: preseal.Deal.EndEpoch, + // TODO: Maybe check seal (Can just be snark inputs, doesn't go into the genesis file) + + dealWeight := big.Zero() + { + 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 { + return cid.Undef, err + } } - _, err := doExecValue(ctx, vm, maddr, m.Worker, big.Zero(), builtin.MethodsMiner.PreCommitSector, mustEnc(params)) - if err != nil { - return cid.Undef, xerrors.Errorf("failed to create genesis miner: %w", err) + + pledge := big.Zero() + { + err = vm.MutateState(ctx, builtin.StoragePowerActorAddr, func(cst cbor.IpldStore, st *power.State) error { + weight := &power.SectorStorageWeightDesc{ + SectorSize: m.SectorSize, + Duration: preseal.Deal.Duration(), + DealWeight: dealWeight, + } + + spower := power.ConsensusPowerForWeight(weight) + pledge = power.PledgeForWeight(weight, big.Sub(st.TotalNetworkPower, spower)) + err := st.AddToClaim(&state.AdtStore{cst}, maddr, spower, pledge) + if err != nil { + return xerrors.Errorf("add to claim: %w", err) + } + return nil + }) + if err != nil { + return cid.Undef, xerrors.Errorf("register power claim in power actor: %w", err) + } + } + + { + newSectorInfo := &miner.SectorOnChainInfo{ + Info: miner.SectorPreCommitInfo{ + SectorNumber: preseal.SectorID, + SealedCID: commcid.ReplicaCommitmentV1ToCID(preseal.CommR[:]), + SealEpoch: 0, + DealIDs: []abi.DealID{dealIDs[pi]}, + Expiration: preseal.Deal.EndEpoch, + }, + ActivationEpoch: 0, + DealWeight: dealWeight, + PledgeRequirement: pledge, + } + + 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 { + return xerrors.Errorf("failed to prove commit: %v", err) + } + + st.ProvingSet = st.Sectors + return nil + }) + } + + { + sectorBf := abi.NewBitField() + sectorBf.Set(uint64(preseal.SectorID)) + + payload, err := cborutil.Dump(&miner.CronEventPayload{ + EventType: miner.CronEventSectorExpiry, + Sectors: §orBf, + }) + if err != nil { + return cid.Undef, err + } + params := &power.EnrollCronEventParams{ + EventEpoch: preseal.Deal.EndEpoch, + Payload: payload, + } + + _, err = doExecValue(ctx, vm, builtin.StoragePowerActorAddr, maddr, big.Zero(), builtin.MethodsPower.EnrollCronEvent, mustEnc(params)) + if err != nil { + return cid.Undef, xerrors.Errorf("failed to verify preseal deals miner: %w", err) + } } } - // You can't prove sector commitments at the same height that you precommit them - vm.SetBlockHeight(7) // needs to be between PorepMinDelay and PorepMaxDelay - - // Commit - for _, preseal := range m.Sectors { - params := &miner.ProveCommitSectorParams{ - SectorNumber: preseal.SectorID, - Proof: nil, - } - _, err := doExecValue(ctx, vm, maddr, m.Worker, big.Zero(), builtin.MethodsMiner.ProveCommitSector, mustEnc(params)) - if err != nil { - return cid.Undef, xerrors.Errorf("failed to create genesis miner: %w", err) - } - } } c, err := vm.Flush(ctx) diff --git a/chain/gen/genesis/t04_power.go b/chain/gen/genesis/t04_power.go index e2a5b4cc4..48fa9ef8f 100644 --- a/chain/gen/genesis/t04_power.go +++ b/chain/gen/genesis/t04_power.go @@ -23,7 +23,7 @@ func SetupStoragePowerActor(bs bstore.Blockstore) (*types.Actor, error) { } sms := &power.State{ - TotalNetworkPower: big.Zero(), + TotalNetworkPower: big.NewInt(1), MinerCount: 0, EscrowTable: emptyhamt, CronEventQueue: emptyhamt, diff --git a/chain/state/statetree.go b/chain/state/statetree.go index 64d567a6a..d7464d3dd 100644 --- a/chain/state/statetree.go +++ b/chain/state/statetree.go @@ -83,7 +83,7 @@ func (st *StateTree) LookupID(addr address.Address) (address.Address, error) { return address.Undef, xerrors.Errorf("loading init actor state: %w", err) } - return ias.ResolveAddress(&adtStore{st.Store}, addr) + return ias.ResolveAddress(&AdtStore{st.Store}, addr) } func (st *StateTree) GetActor(addr address.Address) (*types.Actor, error) { @@ -158,7 +158,7 @@ func (st *StateTree) RegisterNewAddress(addr address.Address, act *types.Actor) return err } - oaddr, err := ias.MapAddressToNewID(&adtStore{st.Store}, addr) + oaddr, err := ias.MapAddressToNewID(&AdtStore{st.Store}, addr) if err != nil { return err } @@ -183,13 +183,13 @@ func (st *StateTree) RegisterNewAddress(addr address.Address, act *types.Actor) return out, nil } -type adtStore struct{ cbor.IpldStore } +type AdtStore struct{ cbor.IpldStore } -func (a *adtStore) Context() context.Context { +func (a *AdtStore) Context() context.Context { return context.TODO() } -var _ adt.Store = (*adtStore)(nil) +var _ adt.Store = (*AdtStore)(nil) func (st *StateTree) Revert() error { nd, err := hamt.LoadNode(context.Background(), st.Store, st.snapshot, hamt.UseTreeBitWidth(5)) diff --git a/chain/vm/vm.go b/chain/vm/vm.go index 6976ae081..85c90f3dd 100644 --- a/chain/vm/vm.go +++ b/chain/vm/vm.go @@ -5,6 +5,7 @@ import ( "context" "fmt" "math/big" + "reflect" "github.com/filecoin-project/specs-actors/actors/abi" "github.com/filecoin-project/specs-actors/actors/builtin/account" @@ -556,6 +557,37 @@ func (vm *VM) Flush(ctx context.Context) (cid.Cid, error) { return root, nil } +// vm.MutateState(idAddr, func(cst cbor.IpldStore, st *ActorStateType) error {...}) +func (vm *VM) MutateState(ctx context.Context, addr address.Address, fn interface{}) error { + act, err := vm.cstate.GetActor(addr) + if err != nil { + return xerrors.Errorf("actor not found: %w", err) + } + + st := reflect.New(reflect.TypeOf(fn).In(1).Elem()) + if err := vm.cst.Get(ctx, act.Head, st.Interface()); err != nil { + return xerrors.Errorf("read actor head: %w", err) + } + + out := reflect.ValueOf(fn).Call([]reflect.Value{reflect.ValueOf(vm.cst), st}) + if !out[0].IsNil() && out[0].Interface().(error) != nil { + return out[0].Interface().(error) + } + + head, err := vm.cst.Put(ctx, st.Interface()) + if err != nil { + return xerrors.Errorf("put new actor head: %w", err) + } + + act.Head = head + + if err := vm.cstate.SetActor(addr, act); err != nil { + return xerrors.Errorf("set actor: %w", err) + } + + return nil +} + func linksForObj(blk block.Block) ([]cid.Cid, error) { switch blk.Cid().Prefix().Codec { case cid.DagCBOR: diff --git a/go.mod b/go.mod index b333d0014..9bef620e7 100644 --- a/go.mod +++ b/go.mod @@ -23,7 +23,7 @@ require ( github.com/filecoin-project/go-paramfetch v0.0.1 github.com/filecoin-project/go-sectorbuilder v0.0.2-0.20200210220012-eb75ec747d6b github.com/filecoin-project/go-statestore v0.1.0 - github.com/filecoin-project/specs-actors v0.0.0-20200217221016-d2e2112b7a8b + github.com/filecoin-project/specs-actors v0.0.0-20200218211922-372fea3f131f github.com/gbrlsnchs/jwt/v3 v3.0.0-beta.1 github.com/go-ole/go-ole v1.2.4 // indirect github.com/gorilla/mux v1.7.3 @@ -85,7 +85,6 @@ require ( github.com/multiformats/go-varint v0.0.5 github.com/opentracing/opentracing-go v1.1.0 github.com/prometheus/common v0.4.0 - github.com/rogpeppe/go-internal v1.3.0 github.com/stretchr/testify v1.4.0 github.com/whyrusleeping/bencher v0.0.0-20190829221104-bb6607aa8bba github.com/whyrusleeping/cbor-gen v0.0.0-20200213013405-80352c7ae952 @@ -115,5 +114,3 @@ replace github.com/golangci/golangci-lint => github.com/golangci/golangci-lint v replace github.com/filecoin-project/filecoin-ffi => ./extern/filecoin-ffi replace github.com/coreos/go-systemd => github.com/coreos/go-systemd/v22 v22.0.0 - -replace github.com/filecoin-project/specs-actors => ../specs-actors diff --git a/go.sum b/go.sum index e369a74bf..89cb4d124 100644 --- a/go.sum +++ b/go.sum @@ -128,26 +128,8 @@ github.com/filecoin-project/go-statestore v0.1.0 h1:t56reH59843TwXHkMcwyuayStBIi github.com/filecoin-project/go-statestore v0.1.0/go.mod h1:LFc9hD+fRxPqiHiaqUEZOinUJB4WARkRfNl10O7kTnI= github.com/filecoin-project/specs-actors v0.0.0-20200210130641-2d1fbd8672cf h1:fbxBG12yrxilPFV1EG2lYqpUyAlRZWkvtqjk2svSeXY= github.com/filecoin-project/specs-actors v0.0.0-20200210130641-2d1fbd8672cf/go.mod h1:xtDZUB6pe4Pksa/bAJbJ693OilaC5Wbot9jMhLm3cZA= -github.com/filecoin-project/specs-actors v0.0.0-20200211202147-e5d0a86dd8fb h1:6kQpqlJmRuMLCaK7XkCTyyAuFWaaBwowG6UCYc2M2bo= -github.com/filecoin-project/specs-actors v0.0.0-20200211202147-e5d0a86dd8fb/go.mod h1:xtDZUB6pe4Pksa/bAJbJ693OilaC5Wbot9jMhLm3cZA= -github.com/filecoin-project/specs-actors v0.0.0-20200212234534-e2abd13ec4b1 h1:jU0PeLjpsrvDBhVyzVgupcwicSioVvXWreN0NMhuMN0= -github.com/filecoin-project/specs-actors v0.0.0-20200212234534-e2abd13ec4b1/go.mod h1:xtDZUB6pe4Pksa/bAJbJ693OilaC5Wbot9jMhLm3cZA= -github.com/filecoin-project/specs-actors v0.0.0-20200214164743-74b45ccaaff9 h1:i6hLXvnse+C7DE2/1anKrlKahBFbusa066LG0K/v8KM= -github.com/filecoin-project/specs-actors v0.0.0-20200214164743-74b45ccaaff9/go.mod h1:xtDZUB6pe4Pksa/bAJbJ693OilaC5Wbot9jMhLm3cZA= -github.com/filecoin-project/specs-actors v0.0.0-20200214213304-f699edd41914 h1:3AqKuA/ha8amt24Ya3l/45+PfFDlHcm4Jt1YWKuw6A0= -github.com/filecoin-project/specs-actors v0.0.0-20200214213304-f699edd41914/go.mod h1:xtDZUB6pe4Pksa/bAJbJ693OilaC5Wbot9jMhLm3cZA= -github.com/filecoin-project/specs-actors v0.0.0-20200217082309-eacd01300b6d h1:6mllLyY8O/QZPrmDC3HWVo0AK6uecPWZxgjhx5ebA1I= -github.com/filecoin-project/specs-actors v0.0.0-20200217082309-eacd01300b6d/go.mod h1:xtDZUB6pe4Pksa/bAJbJ693OilaC5Wbot9jMhLm3cZA= -github.com/filecoin-project/specs-actors v0.0.0-20200217084223-58b973dc2c1f h1:YGhskEZ/N8rKrU85BvUgRuubhykawibsNluzqZSosHE= -github.com/filecoin-project/specs-actors v0.0.0-20200217084223-58b973dc2c1f/go.mod h1:xtDZUB6pe4Pksa/bAJbJ693OilaC5Wbot9jMhLm3cZA= -github.com/filecoin-project/specs-actors v0.0.0-20200217171557-6190f3bd509b h1:7TpLVtW0BBiTacx4Vnjn8dX/Ahfl9gLl/XPHN5dxcig= -github.com/filecoin-project/specs-actors v0.0.0-20200217171557-6190f3bd509b/go.mod h1:xtDZUB6pe4Pksa/bAJbJ693OilaC5Wbot9jMhLm3cZA= -github.com/filecoin-project/specs-actors v0.0.0-20200217172540-9989695a4859 h1:qGeYJJFhAsBakwtnWIUMqIGdOH5yD8e3pfQ4NmEWaVc= -github.com/filecoin-project/specs-actors v0.0.0-20200217172540-9989695a4859/go.mod h1:xtDZUB6pe4Pksa/bAJbJ693OilaC5Wbot9jMhLm3cZA= -github.com/filecoin-project/specs-actors v0.0.0-20200217213446-ac1ea4569f6a h1:pD0cvMAT7aLFYsNsuIIYPzwzQJyI4IPh11KCtYEU2CY= -github.com/filecoin-project/specs-actors v0.0.0-20200217213446-ac1ea4569f6a/go.mod h1:xtDZUB6pe4Pksa/bAJbJ693OilaC5Wbot9jMhLm3cZA= -github.com/filecoin-project/specs-actors v0.0.0-20200217221016-d2e2112b7a8b h1:cb37QS3vhAGimWq746yaeXoJS/+hSqKQL0pUxZDoIHQ= -github.com/filecoin-project/specs-actors v0.0.0-20200217221016-d2e2112b7a8b/go.mod h1:xtDZUB6pe4Pksa/bAJbJ693OilaC5Wbot9jMhLm3cZA= +github.com/filecoin-project/specs-actors v0.0.0-20200218211922-372fea3f131f h1:xfjeZfwhrNlp9/71gprhfC+HL8htJukplomyGxrdkBY= +github.com/filecoin-project/specs-actors v0.0.0-20200218211922-372fea3f131f/go.mod h1:Ecx+3v6Bn4exCS0NbPu7VjrDCu2J+t1PdEpYin1/qOU= github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/gbrlsnchs/jwt/v3 v3.0.0-beta.1 h1:EzDjxMg43q1tA2c0MV3tNbaontnHLplHyFF6M5KiVP0=