Fix most power slashing issues

This commit is contained in:
Łukasz Magiera 2019-11-14 12:56:17 +01:00
parent 2dd155e9e9
commit f4fc3bcc29
7 changed files with 98 additions and 16 deletions

View File

@ -1,6 +1,7 @@
package actors
import (
"bytes"
"context"
"encoding/binary"
"fmt"
@ -751,8 +752,12 @@ func (sma StorageMinerActor) IsLate(act *types.Actor, vmctx types.VMContext, par
return cbg.EncodeBool(isLate(vmctx.BlockHeight(), self)), nil
}
type CheckMinerParams struct {
NetworkPower types.BigInt
}
// TODO: better name
func (sma StorageMinerActor) CheckMiner(act *types.Actor, vmctx types.VMContext, params *struct{}) ([]byte, ActorError) {
func (sma StorageMinerActor) CheckMiner(act *types.Actor, vmctx types.VMContext, params *CheckMinerParams) ([]byte, ActorError) {
if vmctx.Message().From != StoragePowerAddress {
return nil, aerrors.New(2, "only the storage power actor can check miner")
}
@ -764,7 +769,7 @@ func (sma StorageMinerActor) CheckMiner(act *types.Actor, vmctx types.VMContext,
if !isLate(vmctx.BlockHeight(), self) {
// Everything's fine
return cbg.EncodeBool(true), nil
return nil, nil
}
if self.SlashedAt != 0 {
@ -772,6 +777,13 @@ func (sma StorageMinerActor) CheckMiner(act *types.Actor, vmctx types.VMContext,
return nil, nil
}
if params.NetworkPower.Equals(self.Power) {
// Don't break the network when there's only one miner left
log.Warnf("can't slash miner %s for missed PoSt, no power would be left in the network", vmctx.Message().To)
return nil, nil
}
// Slash for being late
self.SlashedAt = vmctx.BlockHeight()
@ -784,7 +796,11 @@ func (sma StorageMinerActor) CheckMiner(act *types.Actor, vmctx types.VMContext,
return nil, err
}
return self.Power.Bytes(), nil
var out bytes.Buffer
if err := self.Power.MarshalCBOR(&out); err != nil {
return nil, aerrors.HandleExternalError(err, "marshaling return value")
}
return out.Bytes(), nil
}
type DeclareFaultsParams struct {

View File

@ -582,7 +582,12 @@ func (spa StoragePowerActor) CheckProofSubmissions(act *types.Actor, vmctx types
return aerrors.Escalate(err, "parsing miner address")
}
ret, err := vmctx.Send(maddr, MAMethods.CheckMiner, types.NewInt(0), nil)
params, err := SerializeParams(&CheckMinerParams{NetworkPower: self.TotalStorage})
if err != nil {
return err
}
ret, err := vmctx.Send(maddr, MAMethods.CheckMiner, types.NewInt(0), params)
if err != nil {
return err
}
@ -591,13 +596,16 @@ func (spa StoragePowerActor) CheckProofSubmissions(act *types.Actor, vmctx types
return nil // miner is fine
}
log.Warnf("slashing miner %s for missed PoSt", maddr)
var power types.BigInt
if err := power.UnmarshalCBOR(bytes.NewReader(ret)); err != nil {
return xerrors.Errorf("unmarshaling CheckMiner response (%x): %w", ret, err)
}
power := types.NewInt(0)
power.SetBytes(ret)
self.TotalStorage = types.BigSub(self.TotalStorage, power)
if power.GreaterThan(types.NewInt(0)) {
log.Warnf("slashing miner %s for missed PoSt (%s B)", maddr, power)
self.TotalStorage = types.BigSub(self.TotalStorage, power)
}
return nil
})
if err != nil {

View File

@ -3896,3 +3896,46 @@ func (t *SectorProveCommitInfo) UnmarshalCBOR(r io.Reader) error {
return nil
}
func (t *CheckMinerParams) MarshalCBOR(w io.Writer) error {
if t == nil {
_, err := w.Write(cbg.CborNull)
return err
}
if _, err := w.Write([]byte{129}); err != nil {
return err
}
// t.t.NetworkPower (types.BigInt) (struct)
if err := t.NetworkPower.MarshalCBOR(w); err != nil {
return err
}
return nil
}
func (t *CheckMinerParams) 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 != 1 {
return fmt.Errorf("cbor input had wrong number of fields")
}
// t.t.NetworkPower (types.BigInt) (struct)
{
if err := t.NetworkPower.UnmarshalCBOR(br); err != nil {
return err
}
}
return nil
}

View File

@ -92,11 +92,16 @@ func (bi BigInt) LessThan(o BigInt) bool {
return BigCmp(bi, o) < 0
}
// LessThan returns true if bi > o
// GreaterThan returns true if bi > o
func (bi BigInt) GreaterThan(o BigInt) bool {
return BigCmp(bi, o) > 0
}
// Equals returns true if bi == o
func (bi BigInt) Equals(o BigInt) bool {
return BigCmp(bi, o) == 0
}
func (bi *BigInt) MarshalJSON() ([]byte, error) {
return json.Marshal(bi.String())
}
@ -186,7 +191,7 @@ func (bi *BigInt) UnmarshalCBOR(br io.Reader) error {
}
if maj != cbg.MajByteString {
return fmt.Errorf("cbor input for fil big int was not a byte string")
return fmt.Errorf("cbor input for fil big int was not a byte string (%x)", maj)
}
if extra == 0 {

View File

@ -128,6 +128,7 @@ func main() {
actors.OnChainDeal{},
actors.ComputeDataCommitmentParams{},
actors.SectorProveCommitInfo{},
actors.CheckMinerParams{},
)
if err != nil {
fmt.Println(err)

View File

@ -74,18 +74,26 @@ class PowerState extends React.Component {
constructor(props) {
super(props)
this.state = {actors: []}
this.state = {actors: [], state: {State: {}}}
}
async componentDidMount() {
const tipset = await this.props.client.call("Filecoin.ChainHead", []) // TODO: from props
const actors = await this.props.client.call("Filecoin.StateListMiners", [tipset])
this.setState({actors: actors})
const state = await this.props.client.call('Filecoin.StateReadState', [this.props.actor, tipset])
this.setState({actors, state})
}
render() {
return this.state.actors.sort((a, b) => (Number(a.substr(1)) > Number(b.substr(1))))
.map(addr => <div key={addr}><Address miner={true} addr={addr} client={this.props.client} mountWindow={this.props.mountWindow}/></div>)
return <div>
<div>
<div>Total Power: <b>{this.state.state.State.TotalStorage}</b></div>
</div>
<div>---</div>
<div>{this.state.actors.sort((a, b) => (Number(a.substr(1)) > Number(b.substr(1))))
.map(addr => <div key={addr}><Address miner={true} addr={addr} client={this.props.client} mountWindow={this.props.mountWindow}/></div>)}</div>
</div>
}
}
@ -174,6 +182,7 @@ class MinerState extends React.Component {
<div>Sector Size: <b>{this.state.sectorSize/1024}</b> KiB</div>
<div>Power: <b>{state.Power}</b> (<b>{state.Power/this.state.networkPower*100}</b>%)</div>
<div>Proving Period End: <b>{state.ProvingPeriodEnd}</b></div>
<div>Slashed: <b>{state.SlashedAt === 0 ? "NO" : state.SlashedAt}</b></div>
<div>
<div>----</div>
<div>Sectors:</div>

View File

@ -89,7 +89,7 @@ func (pmgr *PeerMgr) Run(ctx context.Context) {
if pcount < pmgr.minFilPeers {
pmgr.expandPeers()
} else if pcount > pmgr.maxFilPeers {
log.Infof("peer count about threshold: %d > %d", pcount, pmgr.maxFilPeers)
log.Debug("peer count about threshold: %d > %d", pcount, pmgr.maxFilPeers)
}
}
}