on chain deals: Get things to actually run!

This commit is contained in:
Łukasz Magiera 2019-10-22 19:34:59 +02:00
parent 47d92d4a11
commit 64bfb38834
9 changed files with 148 additions and 54 deletions

View File

@ -30,7 +30,7 @@ const MaxVouchersPerDeal = 768 // roughly one voucher per 10h over a year
// Consensus / Network
// Seconds
const BlockDelay = 30
const BlockDelay = 3
// Seconds
const AllowableClockDrift = BlockDelay * 2
@ -51,7 +51,7 @@ const RandomnessLookback = 20
const ProvingPeriodDuration = 40
// Blocks
const PoSTChallangeTime = 20
const PoSTChallangeTime = 35
const PowerCollateralProportion = 5
const PerCapitaCollateralProportion = 1

View File

@ -170,7 +170,7 @@ func IsBuiltinActor(code cid.Cid) bool {
}
func IsSingletonActor(code cid.Cid) bool {
return code == StoragePowerCodeCid || code == InitCodeCid
return code == StoragePowerCodeCid || code == StorageMarketCodeCid || code == InitCodeCid
}
func (ias *InitActorState) AddActor(cst *hamt.CborIpldStore, addr address.Address) (address.Address, error) {

View File

@ -18,8 +18,6 @@ import (
"golang.org/x/xerrors"
)
const POST_SECTORS_COUNT = 8192
type StorageMinerActor struct{}
type StorageMinerActorState struct {
@ -201,23 +199,18 @@ func (sma StorageMinerActor) StorageMinerConstructor(act *types.Actor, vmctx typ
return nil, nil
}
type CommitSectorParams struct {
SectorID uint64
CommD []byte
type OnChainSealVerifyInfo struct {
CommD []byte // TODO: update proofs code
CommR []byte
CommRStar []byte
Proof []byte
}
type OnChainSealVerifyInfo struct {
SealedCID cid.Cid // CommR .. TODO: spec says cid, but it feels weird
Epoch uint64
//Epoch uint64
Proof []byte
DealIDs []uint64
SectorNumber uint64
}
func (sma StorageMinerActor) CommitSector(act *types.Actor, vmctx types.VMContext, params *CommitSectorParams) ([]byte, ActorError) {
func (sma StorageMinerActor) CommitSector(act *types.Actor, vmctx types.VMContext, params *OnChainSealVerifyInfo) ([]byte, ActorError) {
ctx := context.TODO()
oldstate, self, err := loadState(vmctx)
if err != nil {
@ -239,7 +232,7 @@ func (sma StorageMinerActor) CommitSector(act *types.Actor, vmctx types.VMContex
}
// make sure the miner isnt trying to submit a pre-existing sector
unique, err := SectorIsUnique(ctx, vmctx.Storage(), self.Sectors, params.SectorID)
unique, err := SectorIsUnique(ctx, vmctx.Storage(), self.Sectors, params.SectorNumber)
if err != nil {
return nil, err
}
@ -251,6 +244,7 @@ func (sma StorageMinerActor) CommitSector(act *types.Actor, vmctx types.VMContex
futurePower := types.BigAdd(self.Power, mi.SectorSize)
collateralRequired := CollateralForPower(futurePower)
// TODO: grab from market?
if act.Balance.LessThan(collateralRequired) {
return nil, aerrors.New(3, "not enough collateral")
}
@ -258,7 +252,7 @@ func (sma StorageMinerActor) CommitSector(act *types.Actor, vmctx types.VMContex
// Note: There must exist a unique index in the miner's sector set for each
// sector ID. The `faults`, `recovered`, and `done` parameters of the
// SubmitPoSt method express indices into this sector set.
nssroot, err := AddToSectorSet(ctx, vmctx.Storage(), self.Sectors, params.SectorID, params.CommR, params.CommD)
nssroot, err := AddToSectorSet(ctx, vmctx.Storage(), self.Sectors, params.SectorNumber, params.CommR, params.CommD)
if err != nil {
return nil, err
}
@ -290,7 +284,15 @@ func (sma StorageMinerActor) CommitSector(act *types.Actor, vmctx types.VMContex
return nil, err
}
return nil, nil
activateParams, err := SerializeParams(&ActivateStorageDealsParams{
Deals: params.DealIDs,
})
if err != nil {
return nil, err
}
_, err = vmctx.Send(StorageMarketAddress, SMAMethods.ActivateStorageDeals, types.NewInt(0), activateParams)
return nil, err
}
type SubmitPoStParams struct {
@ -515,8 +517,8 @@ func GetFromSectorSet(ctx context.Context, s types.Storage, ss cid.Cid, sectorID
return true, comms[0], comms[1], nil
}
func ValidatePoRep(maddr address.Address, ssize types.BigInt, params *CommitSectorParams) (bool, ActorError) {
ok, err := sectorbuilder.VerifySeal(ssize.Uint64(), params.CommR, params.CommD, params.CommRStar, maddr, params.SectorID, params.Proof)
func ValidatePoRep(maddr address.Address, ssize types.BigInt, params *OnChainSealVerifyInfo) (bool, ActorError) {
ok, err := sectorbuilder.VerifySeal(ssize.Uint64(), params.CommR, params.CommD, params.CommRStar, maddr, params.SectorNumber, params.Proof)
if err != nil {
return false, aerrors.Absorb(err, 25, "verify seal failed")
}

View File

@ -491,17 +491,12 @@ func (t *StorageMinerConstructorParams) UnmarshalCBOR(r io.Reader) error {
return nil
}
func (t *CommitSectorParams) MarshalCBOR(w io.Writer) error {
func (t *OnChainSealVerifyInfo) MarshalCBOR(w io.Writer) error {
if t == nil {
_, err := w.Write(cbg.CborNull)
return err
}
if _, err := w.Write([]byte{133}); err != nil {
return err
}
// t.t.SectorID (uint64)
if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajUnsignedInt, t.SectorID)); err != nil {
if _, err := w.Write([]byte{134}); err != nil {
return err
}
@ -536,10 +531,25 @@ func (t *CommitSectorParams) MarshalCBOR(w io.Writer) error {
if _, err := w.Write(t.Proof); err != nil {
return err
}
// t.t.DealIDs ([]uint64)
if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajArray, uint64(len(t.DealIDs)))); err != nil {
return err
}
for _, v := range t.DealIDs {
if err := cbg.CborWriteHeader(w, cbg.MajUnsignedInt, v); err != nil {
return err
}
}
// t.t.SectorNumber (uint64)
if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajUnsignedInt, t.SectorNumber)); err != nil {
return err
}
return nil
}
func (t *CommitSectorParams) UnmarshalCBOR(r io.Reader) error {
func (t *OnChainSealVerifyInfo) UnmarshalCBOR(r io.Reader) error {
br := cbg.GetPeeker(r)
maj, extra, err := cbg.CborReadHeader(br)
@ -550,20 +560,10 @@ func (t *CommitSectorParams) UnmarshalCBOR(r io.Reader) error {
return fmt.Errorf("cbor input should be of type array")
}
if extra != 5 {
if extra != 6 {
return fmt.Errorf("cbor input had wrong number of fields")
}
// t.t.SectorID (uint64)
maj, extra, err = cbg.CborReadHeader(br)
if err != nil {
return err
}
if maj != cbg.MajUnsignedInt {
return fmt.Errorf("wrong type for uint64 field")
}
t.SectorID = extra
// t.t.CommD ([]uint8)
maj, extra, err = cbg.CborReadHeader(br)
@ -632,6 +632,46 @@ func (t *CommitSectorParams) UnmarshalCBOR(r io.Reader) error {
if _, err := io.ReadFull(br, t.Proof); err != nil {
return err
}
// t.t.DealIDs ([]uint64)
maj, extra, err = cbg.CborReadHeader(br)
if err != nil {
return err
}
if extra > 8192 {
return fmt.Errorf("t.DealIDs: array too large (%d)", extra)
}
if maj != cbg.MajArray {
return fmt.Errorf("expected cbor array")
}
if extra > 0 {
t.DealIDs = make([]uint64, extra)
}
for i := 0; i < int(extra); i++ {
maj, val, err := cbg.CborReadHeader(br)
if err != nil {
return xerrors.Errorf("failed to read uint64 for t.DealIDs slice: %w", err)
}
if maj != cbg.MajUnsignedInt {
return xerrors.Errorf("value read for array t.DealIDs was not a uint, instead got %d", maj)
}
t.DealIDs[i] = val
}
// t.t.SectorNumber (uint64)
maj, extra, err = cbg.CborReadHeader(br)
if err != nil {
return err
}
if maj != cbg.MajUnsignedInt {
return fmt.Errorf("wrong type for uint64 field")
}
t.SectorNumber = extra
return nil
}
@ -2934,7 +2974,7 @@ func (t *StorageDealProposal) MarshalCBOR(w io.Writer) error {
return err
}
// t.t.PieceSerialization (actors.SerializationMode)
// t.t.PieceSerialization (uint64)
if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajUnsignedInt, t.PieceSerialization)); err != nil {
return err
}
@ -3018,7 +3058,7 @@ func (t *StorageDealProposal) UnmarshalCBOR(r io.Reader) error {
return fmt.Errorf("wrong type for uint64 field")
}
t.PieceSize = extra
// t.t.PieceSerialization (actors.SerializationMode)
// t.t.PieceSerialization (uint64)
maj, extra, err = cbg.CborReadHeader(br)
if err != nil {

View File

@ -1,6 +1,7 @@
package deals
import (
"bytes"
"context"
"time"
@ -9,7 +10,6 @@ import (
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/lib/cborrpc"
datastore "github.com/ipfs/go-datastore"
cbor "github.com/ipfs/go-ipld-cbor"
inet "github.com/libp2p/go-libp2p-core/network"
"golang.org/x/xerrors"
)
@ -98,7 +98,7 @@ func (p *Provider) loadAsk() error {
}
var ssa types.SignedStorageAsk
if err := cbor.DecodeInto(askb, &ssa); err != nil {
if err := cborrpc.ReadCborRPC(bytes.NewReader(askb), &ssa); err != nil {
return err
}
@ -107,7 +107,7 @@ func (p *Provider) loadAsk() error {
}
func (p *Provider) signAsk(a *types.StorageAsk) (*types.SignedStorageAsk, error) {
b, err := cbor.DumpObject(a)
b, err := cborrpc.Dump(a)
if err != nil {
return nil, err
}
@ -129,7 +129,7 @@ func (p *Provider) signAsk(a *types.StorageAsk) (*types.SignedStorageAsk, error)
}
func (p *Provider) saveAsk(a *types.SignedStorageAsk) error {
b, err := cbor.DumpObject(a)
b, err := cborrpc.Dump(a)
if err != nil {
return err
}
@ -150,7 +150,7 @@ func (c *Client) checkAskSignature(ask *types.SignedStorageAsk) error {
return xerrors.Errorf("failed to get worker for miner in ask", err)
}
sigb, err := cbor.DumpObject(ask.Ask)
sigb, err := cborrpc.Dump(ask.Ask)
if err != nil {
return xerrors.Errorf("failed to re-serialize ask")
}

View File

@ -89,12 +89,21 @@ func MakeInitialStateTree(bs bstore.Blockstore, actmap map[address.Address]types
return nil, xerrors.Errorf("set init actor: %w", err)
}
spact, err := SetupStoragePowerActor(bs)
if err != nil {
return nil, xerrors.Errorf("setup storage market actor: %w", err)
}
if err := state.SetActor(actors.StoragePowerAddress, spact); err != nil {
return nil, xerrors.Errorf("set storage market actor: %w", err)
}
smact, err := SetupStorageMarketActor(bs)
if err != nil {
return nil, xerrors.Errorf("setup storage market actor: %w", err)
}
if err := state.SetActor(actors.StoragePowerAddress, smact); err != nil {
if err := state.SetActor(actors.StorageMarketAddress, smact); err != nil {
return nil, xerrors.Errorf("set storage market actor: %w", err)
}
@ -135,7 +144,7 @@ func MakeInitialStateTree(bs bstore.Blockstore, actmap map[address.Address]types
return state, nil
}
func SetupStorageMarketActor(bs bstore.Blockstore) (*types.Actor, error) {
func SetupStoragePowerActor(bs bstore.Blockstore) (*types.Actor, error) {
cst := hamt.CSTFromBstore(bs)
nd := hamt.NewNode(cst)
emptyhamt, err := cst.Put(context.TODO(), nd)
@ -161,6 +170,40 @@ func SetupStorageMarketActor(bs bstore.Blockstore) (*types.Actor, error) {
}, nil
}
func SetupStorageMarketActor(bs bstore.Blockstore) (*types.Actor, error) {
cst := hamt.CSTFromBstore(bs)
nd := hamt.NewNode(cst)
emptyHAMT, err := cst.Put(context.TODO(), nd)
if err != nil {
return nil, err
}
blks := amt.WrapBlockstore(bs)
emptyAMT, err := amt.FromArray(blks, nil)
if err != nil {
return nil, xerrors.Errorf("amt build failed: %w", err)
}
sms := &actors.StorageMarketState{
Balances: emptyHAMT,
Deals: emptyAMT,
NextDealID: 0,
}
stcid, err := cst.Put(context.TODO(), sms)
if err != nil {
return nil, err
}
return &types.Actor{
Code: actors.StorageMarketCodeCid,
Head: stcid,
Nonce: 0,
Balance: types.NewInt(0),
}, nil
}
type GenMinerCfg struct {
Owners []address.Address
Workers []address.Address

View File

@ -50,7 +50,7 @@ func main() {
actors.AccountActorState{},
actors.StorageMinerActorState{},
actors.StorageMinerConstructorParams{},
actors.CommitSectorParams{},
actors.OnChainSealVerifyInfo{},
actors.MinerInfo{},
actors.SubmitPoStParams{},
actors.PaymentVerifyParams{},

View File

@ -1,6 +1,7 @@
package cborrpc
import (
"bytes"
"encoding/hex"
"io"
@ -43,3 +44,11 @@ func ReadCborRPC(r io.Reader, out interface{}) error {
}
return cbor.DecodeReader(r, out)
}
func Dump(obj interface{}) ([]byte, error) {
var out bytes.Buffer
if err := WriteCborRPC(&out, obj); err != nil {
return nil, err
}
return out.Bytes(), nil
}

View File

@ -129,12 +129,12 @@ func (m *Miner) commitSector(ctx context.Context, sinfo sectorbuilder.SectorSeal
log.Error("seal we just created failed verification")
}
params := &actors.CommitSectorParams{
SectorID: sinfo.SectorID,
CommD: sinfo.CommD[:],
CommR: sinfo.CommR[:],
CommRStar: sinfo.CommRStar[:],
Proof: sinfo.Proof,
params := &actors.OnChainSealVerifyInfo{
SectorNumber: sinfo.SectorID,
CommD: sinfo.CommD[:],
CommR: sinfo.CommR[:],
CommRStar: sinfo.CommRStar[:],
Proof: sinfo.Proof,
}
enc, aerr := actors.SerializeParams(params)
if aerr != nil {