Skip to content

Commit

Permalink
finish implementation of slash storage fault
Browse files Browse the repository at this point in the history
  • Loading branch information
whyrusleeping committed Sep 20, 2019
1 parent 651b014 commit 4f60a85
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 85 deletions.
65 changes: 42 additions & 23 deletions chain/actors/actor_miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,17 @@ type MinerInfo struct {

// Amount of space in each sector committed to the network by this miner.
SectorSize types.BigInt

// Amount of filecoin the miner puts up as storage collateral for each sector
StorageCollateral types.BigInt
}

type StorageMinerConstructorParams struct {
Owner address.Address
Worker address.Address
SectorSize types.BigInt
PeerID peer.ID
Owner address.Address
Worker address.Address
SectorSize types.BigInt
PeerID peer.ID
StorageCollateral types.BigInt
}

type maMethods struct {
Expand Down Expand Up @@ -167,11 +171,13 @@ func loadMinerInfo(vmctx types.VMContext, m *StorageMinerActorState) (*MinerInfo
}

func (sma StorageMinerActor) StorageMinerConstructor(act *types.Actor, vmctx types.VMContext, params *StorageMinerConstructorParams) ([]byte, ActorError) {
fmt.Println("MINER CONSTRUCTOR")
minerInfo := &MinerInfo{
Owner: params.Owner,
Worker: params.Worker,
PeerID: params.PeerID,
SectorSize: params.SectorSize,
Owner: params.Owner,
Worker: params.Worker,
PeerID: params.PeerID,
SectorSize: params.SectorSize,
StorageCollateral: params.StorageCollateral,
}

minfocid, err := vmctx.Storage().Put(minerInfo)
Expand All @@ -189,6 +195,7 @@ func (sma StorageMinerActor) StorageMinerConstructor(act *types.Actor, vmctx typ
self.Sectors = scid
self.ProvingSet = scid
self.Info = minfocid
self.SlashedSet = scid

storage := vmctx.Storage()
c, err := storage.Put(&self)
Expand Down Expand Up @@ -454,14 +461,18 @@ func (sma StorageMinerActor) SlashStorageFault(act *types.Actor, vmctx types.VMC
if err != nil {
return nil, err
}
mi, err := loadMinerInfo(vmctx, self)
if err != nil {
return nil, err
}

// You can only be slashed once for missing your PoSt.
if self.SlashedAt > 0 {
return nil, aerrors.New(1, "miner already slashed")
}

// Only if the miner is actually late, they can be slashed.
if chain.Now() <= self.ProvingPeriodEnd+NoPostSlashThreshold {
if vmctx.BlockHeight() <= self.ProvingPeriodEnd+NoPostSlashThreshold {
return nil, aerrors.New(2, "miner is not yet tardy")
}

Expand All @@ -477,41 +488,49 @@ func (sma StorageMinerActor) SlashStorageFault(act *types.Actor, vmctx types.VMC
}

// Strip the miner of their power.
_, err := vmctx.Send(StorageMarketAddress, SMAMethods.UpdateStorage, types.NewInt(0), enc)
enc, err := SerializeParams(&UpdateStorageParams{Delta: self.Power})
if err != nil {
return nil, aerrors.Escalate(err, "failed to serialize update storage parameters")
}
_, err = vmctx.Send(StorageMarketAddress, SMAMethods.UpdateStorage, types.NewInt(0), enc)
if err != nil {
return nil, aerrors.Wrap(err, "failed to cut miners storage")
}

self.Power = 0
self.Power = types.NewInt(0)

self.SlashedSet = self.ProvingSet

// remove proving set from our sectors
// TODO: this might actually be pretty expensive, maybe reconsider?
sectors, err := amt.LoadAMT(bs, self.Sectors)
if err != nil {
return nil, aerrors.Escalate(err, "failed to load miners sector set")
sectors, lerr := amt.LoadAMT(bs, self.Sectors)
if lerr != nil {
return nil, aerrors.Escalate(lerr, "failed to load miners sector set")
}

if err := sectors.Subtract(ss); err != nil {
return nil, aerrors.Escalate(err, "failed to remove slashed sectors from proving set")
}

// clear proving set
emptySet, err := amt.NewAMT(bs).Flush()
if err != nil {
return nil, aerrors.Escalate(err, "failed to get cid for empty AMT")
emptySet, lerr := amt.NewAMT(bs).Flush()
if lerr != nil {
return nil, aerrors.Escalate(lerr, "failed to get cid for empty AMT")
}
self.ProvingSet = emptySet

vmctx.Send(StorageMarketAddress, SMAMethods.Constructor
CollateralForPower()
self.owedStorageCollateral = StorageMarketActor.StorageCollateralForSize(
self.slashedSet.Size() * self.SectorSize,
)
self.OwedStorageCollateral = types.BigMul(types.NewInt(ss.Count), mi.StorageCollateral)
self.SlashedAt = vmctx.BlockHeight()

self.SlashedAt = CurrentBlockHeight
ncid, err := vmctx.Storage().Put(self)
if err != nil {
return nil, err
}
if err := vmctx.Storage().Commit(oldstate, ncid); err != nil {
return nil, err
}

return nil, nil
}

func (sma StorageMinerActor) GetPower(act *types.Actor, vmctx types.VMContext, params *struct{}) ([]byte, ActorError) {
Expand Down
22 changes: 13 additions & 9 deletions chain/actors/actor_storagemarket.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package actors

import (
"context"
"fmt"

"github.com/filecoin-project/go-lotus/build"
"github.com/filecoin-project/go-lotus/chain/actors/aerrors"
Expand Down Expand Up @@ -47,25 +48,28 @@ type StorageMarketState struct {
}

type CreateStorageMinerParams struct {
Owner address.Address
Worker address.Address
SectorSize types.BigInt
PeerID peer.ID
Owner address.Address
Worker address.Address
SectorSize types.BigInt
PeerID peer.ID
StorageCollateral types.BigInt
}

func (sma StorageMarketActor) CreateStorageMiner(act *types.Actor, vmctx types.VMContext, params *CreateStorageMinerParams) ([]byte, ActorError) {
fmt.Println("CREATE STORAGE MINER")
if !SupportedSectorSize(params.SectorSize) {
return nil, aerrors.New(1, "Unsupported sector size")
}

encoded, err := CreateExecParams(StorageMinerCodeCid, &StorageMinerConstructorParams{
Owner: params.Owner,
Worker: params.Worker,
SectorSize: params.SectorSize,
PeerID: params.PeerID,
Owner: params.Owner,
Worker: params.Worker,
SectorSize: params.SectorSize,
PeerID: params.PeerID,
StorageCollateral: params.StorageCollateral,
})
if err != nil {
return nil, err
return nil, aerrors.Wrap(err, "failed to create exec parameters")
}

ret, err := vmctx.Send(InitActorAddress, IAMethods.Exec, vmctx.Message().Value, encoded)
Expand Down
2 changes: 1 addition & 1 deletion chain/actors/actors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func TestStorageMarketActorCreateMiner(t *testing.T) {
}

if ret.ExitCode != 0 {
t.Fatal("invocation failed: ", ret.ExitCode)
t.Fatalf("invocation failed: %d: %s", ret.ExitCode, ret.ActorErr)
}

outaddr, err := address.NewFromBytes(ret.Return)
Expand Down
87 changes: 74 additions & 13 deletions chain/actors/cbor_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -249,8 +249,14 @@ func (t *StorageMinerActorState) MarshalCBOR(w io.Writer) error {
return err
}

// t.t.SlashedAt (types.BigInt)
if err := t.SlashedAt.MarshalCBOR(w); err != nil {
// t.t.SlashedSet (cid.Cid)

if err := cbg.WriteCid(w, t.SlashedSet); err != nil {
return xerrors.Errorf("failed to write cid field t.SlashedSet: %w", err)
}

// t.t.SlashedAt (uint64)
if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajUnsignedInt, t.SlashedAt)); err != nil {
return err
}

Expand All @@ -277,7 +283,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")
}

Expand Down Expand Up @@ -371,15 +377,28 @@ func (t *StorageMinerActorState) UnmarshalCBOR(r io.Reader) error {
}

}
// t.t.SlashedAt (types.BigInt)
// t.t.SlashedSet (cid.Cid)

{

if err := t.SlashedAt.UnmarshalCBOR(br); err != nil {
return err
c, err := cbg.ReadCid(br)
if err != nil {
return xerrors.Errorf("failed to read cid field t.SlashedSet: %w", err)
}

t.SlashedSet = c

}
// t.t.SlashedAt (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.SlashedAt = extra
// t.t.OwedStorageCollateral (types.BigInt)

{
Expand Down Expand Up @@ -407,7 +426,7 @@ func (t *StorageMinerConstructorParams) MarshalCBOR(w io.Writer) error {
_, err := w.Write(cbg.CborNull)
return err
}
if _, err := w.Write([]byte{132}); err != nil {
if _, err := w.Write([]byte{133}); err != nil {
return err
}

Expand All @@ -433,6 +452,11 @@ func (t *StorageMinerConstructorParams) MarshalCBOR(w io.Writer) error {
if _, err := w.Write([]byte(t.PeerID)); err != nil {
return err
}

// t.t.StorageCollateral (types.BigInt)
if err := t.StorageCollateral.MarshalCBOR(w); err != nil {
return err
}
return nil
}

Expand All @@ -447,7 +471,7 @@ func (t *StorageMinerConstructorParams) UnmarshalCBOR(r io.Reader) error {
return fmt.Errorf("cbor input should be of type array")
}

if extra != 4 {
if extra != 5 {
return fmt.Errorf("cbor input had wrong number of fields")
}

Expand Down Expand Up @@ -488,6 +512,15 @@ func (t *StorageMinerConstructorParams) UnmarshalCBOR(r io.Reader) error {

t.PeerID = peer.ID(sval)
}
// t.t.StorageCollateral (types.BigInt)

{

if err := t.StorageCollateral.UnmarshalCBOR(br); err != nil {
return err
}

}
return nil
}

Expand Down Expand Up @@ -640,7 +673,7 @@ func (t *MinerInfo) MarshalCBOR(w io.Writer) error {
_, err := w.Write(cbg.CborNull)
return err
}
if _, err := w.Write([]byte{132}); err != nil {
if _, err := w.Write([]byte{133}); err != nil {
return err
}

Expand All @@ -666,6 +699,11 @@ func (t *MinerInfo) MarshalCBOR(w io.Writer) error {
if err := t.SectorSize.MarshalCBOR(w); err != nil {
return err
}

// t.t.StorageCollateral (types.BigInt)
if err := t.StorageCollateral.MarshalCBOR(w); err != nil {
return err
}
return nil
}

Expand All @@ -680,7 +718,7 @@ func (t *MinerInfo) UnmarshalCBOR(r io.Reader) error {
return fmt.Errorf("cbor input should be of type array")
}

if extra != 4 {
if extra != 5 {
return fmt.Errorf("cbor input had wrong number of fields")
}

Expand Down Expand Up @@ -720,6 +758,15 @@ func (t *MinerInfo) UnmarshalCBOR(r io.Reader) error {
return err
}

}
// t.t.StorageCollateral (types.BigInt)

{

if err := t.StorageCollateral.UnmarshalCBOR(br); err != nil {
return err
}

}
return nil
}
Expand Down Expand Up @@ -2445,7 +2492,7 @@ func (t *CreateStorageMinerParams) MarshalCBOR(w io.Writer) error {
_, err := w.Write(cbg.CborNull)
return err
}
if _, err := w.Write([]byte{132}); err != nil {
if _, err := w.Write([]byte{133}); err != nil {
return err
}

Expand All @@ -2471,6 +2518,11 @@ func (t *CreateStorageMinerParams) MarshalCBOR(w io.Writer) error {
if _, err := w.Write([]byte(t.PeerID)); err != nil {
return err
}

// t.t.StorageCollateral (types.BigInt)
if err := t.StorageCollateral.MarshalCBOR(w); err != nil {
return err
}
return nil
}

Expand All @@ -2485,7 +2537,7 @@ func (t *CreateStorageMinerParams) UnmarshalCBOR(r io.Reader) error {
return fmt.Errorf("cbor input should be of type array")
}

if extra != 4 {
if extra != 5 {
return fmt.Errorf("cbor input had wrong number of fields")
}

Expand Down Expand Up @@ -2526,6 +2578,15 @@ func (t *CreateStorageMinerParams) UnmarshalCBOR(r io.Reader) error {

t.PeerID = peer.ID(sval)
}
// t.t.StorageCollateral (types.BigInt)

{

if err := t.StorageCollateral.UnmarshalCBOR(br); err != nil {
return err
}

}
return nil
}

Expand Down
Loading

0 comments on commit 4f60a85

Please sign in to comment.