lotus/chain/actors/builtin/multisig/multisig.go

153 lines
4.4 KiB
Go
Raw Normal View History

2020-09-15 21:44:03 +00:00
package multisig
import (
"fmt"
"github.com/minio/blake2b-simd"
cbg "github.com/whyrusleeping/cbor-gen"
2020-09-15 21:44:03 +00:00
"golang.org/x/xerrors"
"github.com/filecoin-project/go-address"
2020-09-15 21:44:03 +00:00
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/cbor"
"github.com/ipfs/go-cid"
msig4 "github.com/filecoin-project/specs-actors/v4/actors/builtin/multisig"
builtin0 "github.com/filecoin-project/specs-actors/actors/builtin"
2021-04-30 15:51:24 +00:00
builtin2 "github.com/filecoin-project/specs-actors/v2/actors/builtin"
2021-04-30 15:51:24 +00:00
2021-01-16 07:00:14 +00:00
builtin3 "github.com/filecoin-project/specs-actors/v3/actors/builtin"
2021-04-30 15:51:24 +00:00
builtin4 "github.com/filecoin-project/specs-actors/v4/actors/builtin"
2020-09-15 21:44:03 +00:00
"github.com/filecoin-project/lotus/chain/actors"
2020-09-15 21:44:03 +00:00
"github.com/filecoin-project/lotus/chain/actors/adt"
"github.com/filecoin-project/lotus/chain/actors/builtin"
2020-09-15 21:44:03 +00:00
"github.com/filecoin-project/lotus/chain/types"
)
func init() {
2021-04-30 15:51:24 +00:00
builtin.RegisterActorState(builtin0.MultisigActorCodeID, func(store adt.Store, root cid.Cid) (cbor.Marshaler, error) {
return load0(store, root)
})
2021-04-30 15:51:24 +00:00
builtin.RegisterActorState(builtin2.MultisigActorCodeID, func(store adt.Store, root cid.Cid) (cbor.Marshaler, error) {
return load2(store, root)
})
2021-04-30 15:51:24 +00:00
2021-01-16 07:00:14 +00:00
builtin.RegisterActorState(builtin3.MultisigActorCodeID, func(store adt.Store, root cid.Cid) (cbor.Marshaler, error) {
return load3(store, root)
})
2021-04-30 15:51:24 +00:00
builtin.RegisterActorState(builtin4.MultisigActorCodeID, func(store adt.Store, root cid.Cid) (cbor.Marshaler, error) {
return load4(store, root)
})
}
2020-09-15 21:44:03 +00:00
func Load(store adt.Store, act *types.Actor) (State, error) {
switch act.Code {
2021-04-30 15:51:24 +00:00
case builtin0.MultisigActorCodeID:
return load0(store, act.Head)
2021-04-30 15:51:24 +00:00
case builtin2.MultisigActorCodeID:
return load2(store, act.Head)
2021-04-30 15:51:24 +00:00
2021-01-18 23:04:32 +00:00
case builtin3.MultisigActorCodeID:
return load3(store, act.Head)
2021-04-30 15:51:24 +00:00
case builtin4.MultisigActorCodeID:
return load4(store, act.Head)
2021-04-30 15:51:24 +00:00
2020-09-15 21:44:03 +00:00
}
return nil, xerrors.Errorf("unknown actor code %s", act.Code)
}
type State interface {
cbor.Marshaler
LockedBalance(epoch abi.ChainEpoch) (abi.TokenAmount, error)
StartEpoch() (abi.ChainEpoch, error)
UnlockDuration() (abi.ChainEpoch, error)
InitialBalance() (abi.TokenAmount, error)
Threshold() (uint64, error)
Signers() ([]address.Address, error)
ForEachPendingTxn(func(id int64, txn Transaction) error) error
PendingTxnChanged(State) (bool, error)
transactions() (adt.Map, error)
decodeTransaction(val *cbg.Deferred) (Transaction, error)
2020-09-15 21:44:03 +00:00
}
2021-05-06 04:17:35 +00:00
type Transaction = msig4.Transaction
var Methods = builtin4.MethodsMultisig
func Message(version actors.Version, from address.Address) MessageBuilder {
switch version {
2021-04-30 15:51:24 +00:00
case actors.Version0:
return message0{from}
2021-04-30 15:51:24 +00:00
case actors.Version2:
return message2{message0{from}}
2021-04-30 15:51:24 +00:00
case actors.Version3:
return message3{message0{from}}
2021-04-30 15:51:24 +00:00
case actors.Version4:
return message4{message0{from}}
default:
panic(fmt.Sprintf("unsupported actors version: %d", version))
}
}
type MessageBuilder interface {
// Create a new multisig with the specified parameters.
Create(signers []address.Address, threshold uint64,
vestingStart, vestingDuration abi.ChainEpoch,
initialAmount abi.TokenAmount) (*types.Message, error)
// Propose a transaction to the given multisig.
Propose(msig, target address.Address, amt abi.TokenAmount,
method abi.MethodNum, params []byte) (*types.Message, error)
// Approve a multisig transaction. The "hash" is optional.
Approve(msig address.Address, txID uint64, hash *ProposalHashData) (*types.Message, error)
// Cancel a multisig transaction. The "hash" is optional.
Cancel(msig address.Address, txID uint64, hash *ProposalHashData) (*types.Message, error)
}
// this type is the same between v0 and v2
type ProposalHashData = msig4.ProposalHashData
type ProposeReturn = msig4.ProposeReturn
type ProposeParams = msig4.ProposeParams
func txnParams(id uint64, data *ProposalHashData) ([]byte, error) {
params := msig4.TxnIDParams{ID: msig4.TxnID(id)}
if data != nil {
if data.Requester.Protocol() != address.ID {
return nil, xerrors.Errorf("proposer address must be an ID address, was %s", data.Requester)
}
if data.Value.Sign() == -1 {
return nil, xerrors.Errorf("proposal value must be non-negative, was %s", data.Value)
}
if data.To == address.Undef {
return nil, xerrors.Errorf("proposed destination address must be set")
}
pser, err := data.Serialize()
if err != nil {
return nil, err
}
hash := blake2b.Sum256(pser)
params.ProposalHash = hash[:]
}
return actors.SerializeParams(&params)
}