start multisig abstraction

This commit is contained in:
Steven Allen 2020-09-15 14:44:03 -07:00
parent 91e9573863
commit 4e01fad0d4
3 changed files with 69 additions and 33 deletions

View File

@ -0,0 +1,31 @@
package multisig
import (
"golang.org/x/xerrors"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/cbor"
v0builtin "github.com/filecoin-project/specs-actors/actors/builtin"
"github.com/filecoin-project/lotus/chain/actors/adt"
"github.com/filecoin-project/lotus/chain/types"
)
func Load(store adt.Store, act *types.Actor) (State, error) {
switch act.Code {
case v0builtin.MultisigActorCodeID:
out := v0State{store: store}
err := store.Get(store.Context(), act.Head, &out)
if err != nil {
return nil, err
}
return &out, nil
}
return nil, xerrors.Errorf("unknown actor code %s", act.Code)
}
type State interface {
cbor.Marshaler
LockedBalance(epoch abi.ChainEpoch) (abi.TokenAmount, error)
}

View File

@ -0,0 +1,16 @@
package multisig
import (
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/lotus/chain/actors/adt"
"github.com/filecoin-project/specs-actors/actors/builtin/multisig"
)
type v0State struct {
multisig.State
store adt.Store
}
func (s *v0State) LockedBalance(currEpoch abi.ChainEpoch) (abi.TokenAmount, error) {
return s.State.AmountLocked(currEpoch - s.StartEpoch), nil
}

View File

@ -22,7 +22,6 @@ import (
"github.com/filecoin-project/lotus/extern/sector-storage/ffiwrapper" "github.com/filecoin-project/lotus/extern/sector-storage/ffiwrapper"
"github.com/filecoin-project/specs-actors/actors/builtin" "github.com/filecoin-project/specs-actors/actors/builtin"
"github.com/filecoin-project/specs-actors/actors/builtin/market" "github.com/filecoin-project/specs-actors/actors/builtin/market"
samsig "github.com/filecoin-project/specs-actors/actors/builtin/multisig"
"github.com/filecoin-project/specs-actors/actors/builtin/power" "github.com/filecoin-project/specs-actors/actors/builtin/power"
"github.com/filecoin-project/specs-actors/actors/builtin/reward" "github.com/filecoin-project/specs-actors/actors/builtin/reward"
"github.com/filecoin-project/specs-actors/actors/builtin/verifreg" "github.com/filecoin-project/specs-actors/actors/builtin/verifreg"
@ -31,6 +30,7 @@ import (
"github.com/filecoin-project/lotus/api" "github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/chain/actors" "github.com/filecoin-project/lotus/chain/actors"
"github.com/filecoin-project/lotus/chain/actors/builtin/miner" "github.com/filecoin-project/lotus/chain/actors/builtin/miner"
"github.com/filecoin-project/lotus/chain/actors/builtin/multisig"
"github.com/filecoin-project/lotus/chain/beacon" "github.com/filecoin-project/lotus/chain/beacon"
"github.com/filecoin-project/lotus/chain/gen" "github.com/filecoin-project/lotus/chain/gen"
"github.com/filecoin-project/lotus/chain/state" "github.com/filecoin-project/lotus/chain/state"
@ -790,28 +790,19 @@ func (a *StateAPI) MsigGetAvailableBalance(ctx context.Context, addr address.Add
return types.EmptyInt, xerrors.Errorf("loading tipset %s: %w", tsk, err) return types.EmptyInt, xerrors.Errorf("loading tipset %s: %w", tsk, err)
} }
var st samsig.State act, err := a.StateManager.LoadActor(ctx, addr, ts)
act, err := a.StateManager.LoadActorState(ctx, addr, &st, ts) if err != nil {
return types.EmptyInt, xerrors.Errorf("failed to load multisig actor: %w", err)
}
msas, err := multisig.Load(a.Chain.Store(ctx), act)
if err != nil { if err != nil {
return types.EmptyInt, xerrors.Errorf("failed to load multisig actor state: %w", err) return types.EmptyInt, xerrors.Errorf("failed to load multisig actor state: %w", err)
} }
locked, err := msas.LockedBalance(ts.Height())
if act.Code != builtin.MultisigActorCodeID { if err != nil {
return types.EmptyInt, fmt.Errorf("given actor was not a multisig") return types.EmptyInt, xerrors.Errorf("failed to compute locked multisig balance: %w", err)
} }
return types.BigSub(act.Balance, locked), nil
if st.UnlockDuration == 0 {
return act.Balance, nil
}
offset := ts.Height() - st.StartEpoch
if offset > st.UnlockDuration {
return act.Balance, nil
}
minBalance := types.BigDiv(st.InitialBalance, types.NewInt(uint64(st.UnlockDuration)))
minBalance = types.BigMul(minBalance, types.NewInt(uint64(offset)))
return types.BigSub(act.Balance, minBalance), nil
} }
func (a *StateAPI) MsigGetVested(ctx context.Context, addr address.Address, start types.TipSetKey, end types.TipSetKey) (types.BigInt, error) { func (a *StateAPI) MsigGetVested(ctx context.Context, addr address.Address, start types.TipSetKey, end types.TipSetKey) (types.BigInt, error) {
@ -831,29 +822,27 @@ func (a *StateAPI) MsigGetVested(ctx context.Context, addr address.Address, star
return big.Zero(), nil return big.Zero(), nil
} }
var mst samsig.State act, err := a.StateManager.LoadActor(ctx, addr, endTs)
act, err := a.StateManager.LoadActorState(ctx, addr, &mst, endTs)
if err != nil { if err != nil {
return types.EmptyInt, xerrors.Errorf("failed to load multisig actor state at end epoch: %w", err) return types.EmptyInt, xerrors.Errorf("failed to load multisig actor at end epoch: %w", err)
} }
if act.Code != builtin.MultisigActorCodeID { msas, err := multisig.Load(a.Chain.Store(ctx), act)
return types.EmptyInt, fmt.Errorf("given actor was not a multisig") if err != nil {
return types.EmptyInt, xerrors.Errorf("failed to load multisig actor state: %w", err)
} }
if mst.UnlockDuration == 0 || startLk, err := msas.LockedBalance(startTs.Height())
mst.InitialBalance.IsZero() || if err != nil {
mst.StartEpoch+mst.UnlockDuration <= startTs.Height() || return types.EmptyInt, xerrors.Errorf("failed to compute locked balance at start height: %w", err)
mst.StartEpoch >= endTs.Height() {
return big.Zero(), nil
} }
startLk := mst.InitialBalance endLk, err := msas.LockedBalance(endTs.Height())
if startTs.Height() > mst.StartEpoch { if err != nil {
startLk = mst.AmountLocked(startTs.Height() - mst.StartEpoch) return types.EmptyInt, xerrors.Errorf("failed to compute locked balance at end height: %w", err)
} }
return big.Sub(startLk, mst.AmountLocked(endTs.Height()-mst.StartEpoch)), nil return types.BigSub(startLk, endLk), nil
} }
var initialPledgeNum = types.NewInt(110) var initialPledgeNum = types.NewInt(110)