initial actor changes for interactive porep

This commit is contained in:
whyrusleeping 2019-10-24 21:39:13 +08:00 committed by Łukasz Magiera
parent 413314b44b
commit 3e5654d575
5 changed files with 250 additions and 69 deletions

View File

@ -81,6 +81,9 @@ const PowerCollateralProportion = 5
const PerCapitaCollateralProportion = 1
const CollateralPrecision = 1000
// Blocks
const InteractivePoRepDelay = 10
// /////
// Devnet settings

View File

@ -2,6 +2,7 @@ package actors
import (
"context"
"encoding/binary"
"fmt"
"github.com/filecoin-project/lotus/build"
@ -52,9 +53,9 @@ type StorageMinerActorState struct {
// removal penalization is needed.
NextDoneSet types.BitField
// Deals this miner has been slashed for since the last post submission.
//TODO: unsupported map key type "Cid" (if you want to use struct keys, your atlas needs a transform to string)
//ArbitratedDeals map[cid.Cid]struct{}
// UnprovenSectors is the set of sectors that have been committed to but not
// yet had their proofs submitted
UnprovenSectors map[string]*UnprovenSector
// Amount of power this miner has.
Power types.BigInt
@ -90,6 +91,12 @@ type MinerInfo struct {
SectorSize uint64
}
type UnprovenSector struct {
CommD []byte
CommR []byte
SubmitHeight uint64
}
type StorageMinerConstructorParams struct {
Owner address.Address
Worker address.Address
@ -205,12 +212,11 @@ type OnChainSealVerifyInfo struct {
Epoch uint64
Proof []byte
DealIDs []uint64
SectorNumber uint64
}
func (sma StorageMinerActor) CommitSector(act *types.Actor, vmctx types.VMContext, params *OnChainSealVerifyInfo) ([]byte, ActorError) {
ctx := context.TODO()
ctx := vmctx.Context()
oldstate, self, err := loadState(vmctx)
if err != nil {
return nil, err
@ -225,20 +231,6 @@ func (sma StorageMinerActor) CommitSector(act *types.Actor, vmctx types.VMContex
return nil, aerrors.New(1, "not authorized to commit sector for miner")
}
// TODO: this needs to get normalized to either the ID address or the actor address
maddr := vmctx.Message().To
ticket, err := vmctx.GetRandomness(params.Epoch)
if err != nil {
return nil, aerrors.Wrap(err, "failed to get randomness for commitsector")
}
if ok, err := ValidatePoRep(maddr, mi.SectorSize, params, ticket); err != nil {
return nil, err
} else if !ok {
return nil, aerrors.New(2, "bad proof!")
}
// make sure the miner isnt trying to submit a pre-existing sector
unique, err := SectorIsUnique(ctx, vmctx.Storage(), self.Sectors, params.SectorNumber)
if err != nil {
@ -257,10 +249,67 @@ func (sma StorageMinerActor) CommitSector(act *types.Actor, vmctx types.VMContex
return nil, aerrors.New(4, "not enough collateral")
}
self.UnprovenSectors[uintToStringKey(params.SectorNumber)] = &UnprovenSector{
CommR: params.CommR,
CommD: params.CommD,
SubmitHeight: vmctx.BlockHeight(),
}
nstate, err := vmctx.Storage().Put(self)
if err != nil {
return nil, err
}
if err := vmctx.Storage().Commit(oldstate, nstate); err != nil {
return nil, err
}
return nil, nil
}
func uintToStringKey(i uint64) string {
buf := make([]byte, 10)
n := binary.PutUvarint(buf, i)
return string(buf[:n])
}
type SubmitSectorProofParams struct {
Proof []byte
SectorID uint64
DealIDs []uint64
}
func (sma StorageMinerActor) SubmitSectorProof(act *types.Actor, vmctx types.VMContext, params *SubmitSectorProofParams) ([]byte, ActorError) {
ctx := vmctx.Context()
oldstate, self, err := loadState(vmctx)
if err != nil {
return nil, err
}
mi, err := loadMinerInfo(vmctx, self)
if err != nil {
return nil, err
}
us, ok := self.UnprovenSectors[uintToStringKey(params.SectorID)]
if !ok {
return nil, aerrors.New(1, "no pre-commitment found for sector")
}
delete(self.UnprovenSectors, uintToStringKey(params.SectorID))
// TODO: ensure normalization to ID address
maddr := vmctx.Message().To
rand, err := vmctx.GetRandomness(us.SubmitHeight + build.InteractivePoRepDelay)
if ok, err := ValidatePoRep(maddr, mi.SectorSize, us.CommD, us.CommR, rand, params.Proof, params.SectorID); err != nil {
return nil, err
} else if !ok {
return nil, aerrors.New(2, "bad proof!")
}
// 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.SectorNumber, params.CommR, params.CommD)
nssroot, err := AddToSectorSet(ctx, vmctx.Storage(), self.Sectors, params.SectorID, us.CommR, us.CommD)
if err != nil {
return nil, err
}
@ -525,8 +574,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 uint64, params *OnChainSealVerifyInfo, ticket []byte) (bool, ActorError) {
ok, err := sectorbuilder.VerifySeal(ssize, params.CommR, params.CommD, maddr, ticket, params.SectorNumber, params.Proof)
func ValidatePoRep(maddr address.Address, ssize uint64, commD, commR, ticket, proof []byte, sectorID uint64) (bool, ActorError) {
ok, err := sectorbuilder.VerifySeal(ssize, commR, commD, maddr, ticket, sectorID, proof)
if err != nil {
return false, aerrors.Absorb(err, 25, "verify seal failed")
}

View File

@ -197,7 +197,7 @@ func (t *StorageMinerActorState) MarshalCBOR(w io.Writer) error {
_, err := w.Write(cbg.CborNull)
return err
}
if _, err := w.Write([]byte{140}); err != nil {
if _, err := w.Write([]byte{141}); err != nil {
return err
}
@ -244,6 +244,26 @@ func (t *StorageMinerActorState) MarshalCBOR(w io.Writer) error {
return err
}
// t.t.UnprovenSectors (map[string]*actors.UnprovenSector)
if err := cbg.CborWriteHeader(w, cbg.MajMap, uint64(len(t.UnprovenSectors))); err != nil {
return err
}
for k, v := range t.UnprovenSectors {
if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajTextString, uint64(len(k)))); err != nil {
return err
}
if _, err := w.Write([]byte(k)); err != nil {
return err
}
if err := v.MarshalCBOR(w); err != nil {
return err
}
}
// t.t.Power (types.BigInt)
if err := t.Power.MarshalCBOR(w); err != nil {
return err
@ -277,7 +297,7 @@ func (t *StorageMinerActorState) UnmarshalCBOR(r io.Reader) error {
return fmt.Errorf("cbor input should be of type array")
}
if extra != 12 {
if extra != 13 {
return fmt.Errorf("cbor input had wrong number of fields")
}
@ -361,6 +381,59 @@ func (t *StorageMinerActorState) UnmarshalCBOR(r io.Reader) error {
return err
}
}
// t.t.UnprovenSectors (map[string]*actors.UnprovenSector)
maj, extra, err = cbg.CborReadHeader(br)
if err != nil {
return err
}
if maj != cbg.MajMap {
return fmt.Errorf("expected a map (major type 5)")
}
if extra > 4096 {
return fmt.Errorf("t.UnprovenSectors: map too large")
}
t.UnprovenSectors = make(map[string]*UnprovenSector, extra)
for i, l := 0, int(extra); i < l; i++ {
var k string
{
sval, err := cbg.ReadString(br)
if err != nil {
return err
}
k = string(sval)
}
var v *UnprovenSector
{
pb, err := br.PeekByte()
if err != nil {
return err
}
if pb == cbg.CborNull[0] {
var nbuf [1]byte
if _, err := br.Read(nbuf[:]); err != nil {
return err
}
} else {
v = new(UnprovenSector)
if err := v.UnmarshalCBOR(br); err != nil {
return err
}
}
}
t.UnprovenSectors[k] = v
}
// t.t.Power (types.BigInt)
@ -497,7 +570,7 @@ func (t *OnChainSealVerifyInfo) MarshalCBOR(w io.Writer) error {
_, err := w.Write(cbg.CborNull)
return err
}
if _, err := w.Write([]byte{134}); err != nil {
if _, err := w.Write([]byte{133}); err != nil {
return err
}
@ -530,16 +603,6 @@ func (t *OnChainSealVerifyInfo) MarshalCBOR(w io.Writer) error {
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
@ -558,7 +621,7 @@ func (t *OnChainSealVerifyInfo) UnmarshalCBOR(r io.Reader) error {
return fmt.Errorf("cbor input should be of type array")
}
if extra != 6 {
if extra != 5 {
return fmt.Errorf("cbor input had wrong number of fields")
}
@ -623,36 +686,6 @@ func (t *OnChainSealVerifyInfo) 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)
@ -666,6 +699,100 @@ func (t *OnChainSealVerifyInfo) UnmarshalCBOR(r io.Reader) error {
return nil
}
func (t *UnprovenSector) MarshalCBOR(w io.Writer) error {
if t == nil {
_, err := w.Write(cbg.CborNull)
return err
}
if _, err := w.Write([]byte{131}); err != nil {
return err
}
// t.t.CommD ([]uint8)
if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajByteString, uint64(len(t.CommD)))); err != nil {
return err
}
if _, err := w.Write(t.CommD); err != nil {
return err
}
// t.t.CommR ([]uint8)
if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajByteString, uint64(len(t.CommR)))); err != nil {
return err
}
if _, err := w.Write(t.CommR); err != nil {
return err
}
// t.t.SubmitHeight (uint64)
if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajUnsignedInt, t.SubmitHeight)); err != nil {
return err
}
return nil
}
func (t *UnprovenSector) UnmarshalCBOR(r io.Reader) error {
br := cbg.GetPeeker(r)
maj, extra, err := cbg.CborReadHeader(br)
if err != nil {
return err
}
if maj != cbg.MajArray {
return fmt.Errorf("cbor input should be of type array")
}
if extra != 3 {
return fmt.Errorf("cbor input had wrong number of fields")
}
// t.t.CommD ([]uint8)
maj, extra, err = cbg.CborReadHeader(br)
if err != nil {
return err
}
if extra > 8192 {
return fmt.Errorf("t.CommD: array too large (%d)", extra)
}
if maj != cbg.MajByteString {
return fmt.Errorf("expected byte array")
}
t.CommD = make([]byte, extra)
if _, err := io.ReadFull(br, t.CommD); err != nil {
return err
}
// t.t.CommR ([]uint8)
maj, extra, err = cbg.CborReadHeader(br)
if err != nil {
return err
}
if extra > 8192 {
return fmt.Errorf("t.CommR: array too large (%d)", extra)
}
if maj != cbg.MajByteString {
return fmt.Errorf("expected byte array")
}
t.CommR = make([]byte, extra)
if _, err := io.ReadFull(br, t.CommR); err != nil {
return err
}
// t.t.SubmitHeight (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.SubmitHeight = extra
return nil
}
func (t *MinerInfo) MarshalCBOR(w io.Writer) error {
if t == nil {
_, err := w.Write(cbg.CborNull)

View File

@ -51,6 +51,7 @@ func main() {
actors.StorageMinerActorState{},
actors.StorageMinerConstructorParams{},
actors.OnChainSealVerifyInfo{},
actors.UnprovenSector{},
actors.MinerInfo{},
actors.SubmitPoStParams{},
actors.PaymentVerifyParams{},

View File

@ -170,18 +170,19 @@ func (m *Miner) commitSector(ctx context.Context, sinfo sectorbuilder.SectorSeal
log.Error("seal we just created failed verification")
}
deals, err := m.secst.DealsForCommit(sinfo.SectorID)
// TODO: 2 stage commit
/*deals, err := m.secst.DealsForCommit(sinfo.SectorID)
if err != nil {
return xerrors.Errorf("getting sector deals failed: %w", err)
}
*/
params := &actors.OnChainSealVerifyInfo{
CommD: sinfo.CommD[:],
CommR: sinfo.CommR[:],
Proof: sinfo.Proof,
Epoch: sinfo.Ticket.BlockHeight,
DealIDs: deals,
//DealIDs: deals,
SectorNumber: sinfo.SectorID,
}
enc, aerr := actors.SerializeParams(params)