2020-05-04 19:29:26 +00:00
|
|
|
package full
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-07-21 00:17:53 +00:00
|
|
|
|
2020-09-07 03:49:10 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/big"
|
2020-05-04 19:29:26 +00:00
|
|
|
|
|
|
|
"github.com/filecoin-project/go-address"
|
2020-09-07 03:49:10 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
2020-05-04 19:29:26 +00:00
|
|
|
"github.com/filecoin-project/lotus/api"
|
|
|
|
"github.com/filecoin-project/lotus/chain/actors"
|
2020-09-30 19:30:02 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors/builtin/multisig"
|
2020-05-04 19:29:26 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2020-09-21 23:01:29 +00:00
|
|
|
|
2020-10-08 01:09:33 +00:00
|
|
|
multisig2 "github.com/filecoin-project/specs-actors/v2/actors/builtin/multisig"
|
2020-05-04 19:29:26 +00:00
|
|
|
|
|
|
|
"go.uber.org/fx"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type MsigAPI struct {
|
|
|
|
fx.In
|
|
|
|
|
2020-10-01 15:54:28 +00:00
|
|
|
StateAPI StateAPI
|
|
|
|
MpoolAPI MpoolAPI
|
2020-05-04 19:29:26 +00:00
|
|
|
}
|
|
|
|
|
2020-09-30 19:30:02 +00:00
|
|
|
func (a *MsigAPI) messageBuilder(ctx context.Context, from address.Address) (multisig.MessageBuilder, error) {
|
|
|
|
nver, err := a.StateAPI.StateNetworkVersion(ctx, types.EmptyTSK)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-05-04 19:29:26 +00:00
|
|
|
}
|
2021-08-10 17:07:30 +00:00
|
|
|
av, err := actors.VersionForNetwork(nver)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-04 19:29:26 +00:00
|
|
|
|
2021-08-10 17:07:30 +00:00
|
|
|
return multisig.Message(av, from), nil
|
2020-09-30 19:30:02 +00:00
|
|
|
}
|
2020-05-04 19:29:26 +00:00
|
|
|
|
2020-09-30 19:30:02 +00:00
|
|
|
// TODO: remove gp (gasPrice) from arguments
|
|
|
|
// TODO: Add "vesting start" to arguments.
|
2021-03-27 14:35:46 +00:00
|
|
|
func (a *MsigAPI) MsigCreate(ctx context.Context, req uint64, addrs []address.Address, duration abi.ChainEpoch, val types.BigInt, src address.Address, gp types.BigInt) (*api.MessagePrototype, error) {
|
2020-05-04 19:29:26 +00:00
|
|
|
|
2020-09-30 19:30:02 +00:00
|
|
|
mb, err := a.messageBuilder(ctx, src)
|
|
|
|
if err != nil {
|
2021-03-27 14:35:46 +00:00
|
|
|
return nil, err
|
2020-05-04 19:29:26 +00:00
|
|
|
}
|
|
|
|
|
2020-09-30 19:30:02 +00:00
|
|
|
msg, err := mb.Create(addrs, req, 0, duration, val)
|
|
|
|
if err != nil {
|
2021-03-27 14:35:46 +00:00
|
|
|
return nil, err
|
2020-05-04 19:29:26 +00:00
|
|
|
}
|
|
|
|
|
2021-03-27 14:35:46 +00:00
|
|
|
return &api.MessagePrototype{
|
|
|
|
Message: *msg,
|
|
|
|
ValidNonce: false,
|
|
|
|
}, nil
|
2020-05-04 19:29:26 +00:00
|
|
|
}
|
|
|
|
|
2021-03-27 14:35:46 +00:00
|
|
|
func (a *MsigAPI) MsigPropose(ctx context.Context, msig address.Address, to address.Address, amt types.BigInt, src address.Address, method uint64, params []byte) (*api.MessagePrototype, error) {
|
2020-05-04 19:29:26 +00:00
|
|
|
|
2020-09-30 19:30:02 +00:00
|
|
|
mb, err := a.messageBuilder(ctx, src)
|
|
|
|
if err != nil {
|
2021-03-27 14:35:46 +00:00
|
|
|
return nil, err
|
2020-05-04 19:29:26 +00:00
|
|
|
}
|
|
|
|
|
2020-09-30 19:30:02 +00:00
|
|
|
msg, err := mb.Propose(msig, to, amt, abi.MethodNum(method), params)
|
|
|
|
if err != nil {
|
2021-03-27 14:35:46 +00:00
|
|
|
return nil, xerrors.Errorf("failed to create proposal: %w", err)
|
2020-05-04 19:29:26 +00:00
|
|
|
}
|
|
|
|
|
2021-03-27 14:35:46 +00:00
|
|
|
return &api.MessagePrototype{
|
|
|
|
Message: *msg,
|
|
|
|
ValidNonce: false,
|
|
|
|
}, nil
|
2020-05-04 19:29:26 +00:00
|
|
|
}
|
|
|
|
|
2021-03-27 14:35:46 +00:00
|
|
|
func (a *MsigAPI) MsigAddPropose(ctx context.Context, msig address.Address, src address.Address, newAdd address.Address, inc bool) (*api.MessagePrototype, error) {
|
2020-09-06 05:52:30 +00:00
|
|
|
enc, actErr := serializeAddParams(newAdd, inc)
|
|
|
|
if actErr != nil {
|
2021-03-27 14:35:46 +00:00
|
|
|
return nil, actErr
|
2020-09-06 05:52:30 +00:00
|
|
|
}
|
|
|
|
|
2020-10-08 20:32:54 +00:00
|
|
|
return a.MsigPropose(ctx, msig, msig, big.Zero(), src, uint64(multisig.Methods.AddSigner), enc)
|
2020-09-06 05:52:30 +00:00
|
|
|
}
|
|
|
|
|
2021-03-27 14:35:46 +00:00
|
|
|
func (a *MsigAPI) MsigAddApprove(ctx context.Context, msig address.Address, src address.Address, txID uint64, proposer address.Address, newAdd address.Address, inc bool) (*api.MessagePrototype, error) {
|
2020-09-06 05:52:30 +00:00
|
|
|
enc, actErr := serializeAddParams(newAdd, inc)
|
|
|
|
if actErr != nil {
|
2021-03-27 14:35:46 +00:00
|
|
|
return nil, actErr
|
2020-09-06 05:52:30 +00:00
|
|
|
}
|
|
|
|
|
2020-10-08 20:32:54 +00:00
|
|
|
return a.MsigApproveTxnHash(ctx, msig, txID, proposer, msig, big.Zero(), src, uint64(multisig.Methods.AddSigner), enc)
|
2020-09-06 05:52:30 +00:00
|
|
|
}
|
|
|
|
|
2021-03-27 14:35:46 +00:00
|
|
|
func (a *MsigAPI) MsigAddCancel(ctx context.Context, msig address.Address, src address.Address, txID uint64, newAdd address.Address, inc bool) (*api.MessagePrototype, error) {
|
2020-09-06 05:52:30 +00:00
|
|
|
enc, actErr := serializeAddParams(newAdd, inc)
|
|
|
|
if actErr != nil {
|
2021-03-27 14:35:46 +00:00
|
|
|
return nil, actErr
|
2020-09-06 05:52:30 +00:00
|
|
|
}
|
|
|
|
|
2020-10-08 20:32:54 +00:00
|
|
|
return a.MsigCancel(ctx, msig, txID, msig, big.Zero(), src, uint64(multisig.Methods.AddSigner), enc)
|
2020-09-06 05:52:30 +00:00
|
|
|
}
|
|
|
|
|
2021-03-27 14:35:46 +00:00
|
|
|
func (a *MsigAPI) MsigSwapPropose(ctx context.Context, msig address.Address, src address.Address, oldAdd address.Address, newAdd address.Address) (*api.MessagePrototype, error) {
|
2020-07-16 00:55:27 +00:00
|
|
|
enc, actErr := serializeSwapParams(oldAdd, newAdd)
|
|
|
|
if actErr != nil {
|
2021-03-27 14:35:46 +00:00
|
|
|
return nil, actErr
|
2020-07-16 00:55:27 +00:00
|
|
|
}
|
|
|
|
|
2020-10-08 20:32:54 +00:00
|
|
|
return a.MsigPropose(ctx, msig, msig, big.Zero(), src, uint64(multisig.Methods.SwapSigner), enc)
|
2020-07-16 00:55:27 +00:00
|
|
|
}
|
|
|
|
|
2021-03-27 14:35:46 +00:00
|
|
|
func (a *MsigAPI) MsigSwapApprove(ctx context.Context, msig address.Address, src address.Address, txID uint64, proposer address.Address, oldAdd address.Address, newAdd address.Address) (*api.MessagePrototype, error) {
|
2020-07-16 00:55:27 +00:00
|
|
|
enc, actErr := serializeSwapParams(oldAdd, newAdd)
|
|
|
|
if actErr != nil {
|
2021-03-27 14:35:46 +00:00
|
|
|
return nil, actErr
|
2020-07-16 00:55:27 +00:00
|
|
|
}
|
|
|
|
|
2020-10-08 20:32:54 +00:00
|
|
|
return a.MsigApproveTxnHash(ctx, msig, txID, proposer, msig, big.Zero(), src, uint64(multisig.Methods.SwapSigner), enc)
|
2020-07-16 00:55:27 +00:00
|
|
|
}
|
|
|
|
|
2021-03-27 14:35:46 +00:00
|
|
|
func (a *MsigAPI) MsigSwapCancel(ctx context.Context, msig address.Address, src address.Address, txID uint64, oldAdd address.Address, newAdd address.Address) (*api.MessagePrototype, error) {
|
2020-07-16 00:55:27 +00:00
|
|
|
enc, actErr := serializeSwapParams(oldAdd, newAdd)
|
|
|
|
if actErr != nil {
|
2021-03-27 14:35:46 +00:00
|
|
|
return nil, actErr
|
2020-07-16 00:55:27 +00:00
|
|
|
}
|
|
|
|
|
2020-10-08 20:32:54 +00:00
|
|
|
return a.MsigCancel(ctx, msig, txID, msig, big.Zero(), src, uint64(multisig.Methods.SwapSigner), enc)
|
2020-07-16 00:55:27 +00:00
|
|
|
}
|
|
|
|
|
2021-03-27 14:35:46 +00:00
|
|
|
func (a *MsigAPI) MsigApprove(ctx context.Context, msig address.Address, txID uint64, src address.Address) (*api.MessagePrototype, error) {
|
2020-10-09 16:40:25 +00:00
|
|
|
return a.msigApproveOrCancelSimple(ctx, api.MsigApprove, msig, txID, src)
|
|
|
|
}
|
|
|
|
|
2021-03-27 14:35:46 +00:00
|
|
|
func (a *MsigAPI) MsigApproveTxnHash(ctx context.Context, msig address.Address, txID uint64, proposer address.Address, to address.Address, amt types.BigInt, src address.Address, method uint64, params []byte) (*api.MessagePrototype, error) {
|
2020-10-09 16:40:25 +00:00
|
|
|
return a.msigApproveOrCancelTxnHash(ctx, api.MsigApprove, msig, txID, proposer, to, amt, src, method, params)
|
2020-05-04 19:29:26 +00:00
|
|
|
}
|
|
|
|
|
2021-03-27 14:35:46 +00:00
|
|
|
func (a *MsigAPI) MsigCancel(ctx context.Context, msig address.Address, txID uint64, to address.Address, amt types.BigInt, src address.Address, method uint64, params []byte) (*api.MessagePrototype, error) {
|
2020-10-09 16:40:25 +00:00
|
|
|
return a.msigApproveOrCancelTxnHash(ctx, api.MsigCancel, msig, txID, src, to, amt, src, method, params)
|
2020-05-04 19:29:26 +00:00
|
|
|
}
|
|
|
|
|
2021-03-27 14:35:46 +00:00
|
|
|
func (a *MsigAPI) MsigRemoveSigner(ctx context.Context, msig address.Address, proposer address.Address, toRemove address.Address, decrease bool) (*api.MessagePrototype, error) {
|
2020-10-09 16:40:25 +00:00
|
|
|
enc, actErr := serializeRemoveParams(toRemove, decrease)
|
|
|
|
if actErr != nil {
|
2021-03-27 14:35:46 +00:00
|
|
|
return nil, actErr
|
2020-10-09 16:40:25 +00:00
|
|
|
}
|
|
|
|
|
2020-10-08 20:32:54 +00:00
|
|
|
return a.MsigPropose(ctx, msig, msig, types.NewInt(0), proposer, uint64(multisig.Methods.RemoveSigner), enc)
|
2020-10-09 16:40:25 +00:00
|
|
|
}
|
|
|
|
|
2021-03-27 14:35:46 +00:00
|
|
|
func (a *MsigAPI) msigApproveOrCancelSimple(ctx context.Context, operation api.MsigProposeResponse, msig address.Address, txID uint64, src address.Address) (*api.MessagePrototype, error) {
|
2020-10-09 16:40:25 +00:00
|
|
|
if msig == address.Undef {
|
2021-03-27 14:35:46 +00:00
|
|
|
return nil, xerrors.Errorf("must provide multisig address")
|
2020-10-09 16:40:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if src == address.Undef {
|
2021-03-27 14:35:46 +00:00
|
|
|
return nil, xerrors.Errorf("must provide source address")
|
2020-10-09 16:40:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mb, err := a.messageBuilder(ctx, src)
|
|
|
|
if err != nil {
|
2021-03-27 14:35:46 +00:00
|
|
|
return nil, err
|
2020-10-09 16:40:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var msg *types.Message
|
|
|
|
switch operation {
|
|
|
|
case api.MsigApprove:
|
|
|
|
msg, err = mb.Approve(msig, txID, nil)
|
|
|
|
case api.MsigCancel:
|
|
|
|
msg, err = mb.Cancel(msig, txID, nil)
|
|
|
|
default:
|
2021-03-27 14:35:46 +00:00
|
|
|
return nil, xerrors.Errorf("Invalid operation for msigApproveOrCancel")
|
2020-10-09 16:40:25 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
2021-03-27 14:35:46 +00:00
|
|
|
return nil, err
|
2020-10-09 16:40:25 +00:00
|
|
|
}
|
|
|
|
|
2021-03-27 14:35:46 +00:00
|
|
|
return &api.MessagePrototype{
|
|
|
|
Message: *msg,
|
|
|
|
ValidNonce: false,
|
|
|
|
}, nil
|
2020-10-09 16:40:25 +00:00
|
|
|
}
|
|
|
|
|
2021-03-27 14:35:46 +00:00
|
|
|
func (a *MsigAPI) msigApproveOrCancelTxnHash(ctx context.Context, operation api.MsigProposeResponse, msig address.Address, txID uint64, proposer address.Address, to address.Address, amt types.BigInt, src address.Address, method uint64, params []byte) (*api.MessagePrototype, error) {
|
2020-05-04 19:29:26 +00:00
|
|
|
if msig == address.Undef {
|
2021-03-27 14:35:46 +00:00
|
|
|
return nil, xerrors.Errorf("must provide multisig address")
|
2020-05-04 19:29:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if src == address.Undef {
|
2021-03-27 14:35:46 +00:00
|
|
|
return nil, xerrors.Errorf("must provide source address")
|
2020-05-04 19:29:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if proposer.Protocol() != address.ID {
|
2020-10-01 15:54:28 +00:00
|
|
|
proposerID, err := a.StateAPI.StateLookupID(ctx, proposer, types.EmptyTSK)
|
2020-05-04 19:29:26 +00:00
|
|
|
if err != nil {
|
2021-03-27 14:35:46 +00:00
|
|
|
return nil, err
|
2020-05-04 19:29:26 +00:00
|
|
|
}
|
|
|
|
proposer = proposerID
|
|
|
|
}
|
|
|
|
|
2020-09-30 19:30:02 +00:00
|
|
|
p := multisig.ProposalHashData{
|
2020-05-04 19:29:26 +00:00
|
|
|
Requester: proposer,
|
|
|
|
To: to,
|
|
|
|
Value: amt,
|
|
|
|
Method: abi.MethodNum(method),
|
|
|
|
Params: params,
|
|
|
|
}
|
|
|
|
|
2020-09-30 19:30:02 +00:00
|
|
|
mb, err := a.messageBuilder(ctx, src)
|
2020-05-04 19:29:26 +00:00
|
|
|
if err != nil {
|
2021-03-27 14:35:46 +00:00
|
|
|
return nil, err
|
2020-05-04 19:29:26 +00:00
|
|
|
}
|
|
|
|
|
2020-09-30 19:30:02 +00:00
|
|
|
var msg *types.Message
|
2020-05-04 19:29:26 +00:00
|
|
|
switch operation {
|
|
|
|
case api.MsigApprove:
|
2020-09-30 19:30:02 +00:00
|
|
|
msg, err = mb.Approve(msig, txID, &p)
|
2020-05-04 19:29:26 +00:00
|
|
|
case api.MsigCancel:
|
2020-09-30 19:30:02 +00:00
|
|
|
msg, err = mb.Cancel(msig, txID, &p)
|
2020-05-04 19:29:26 +00:00
|
|
|
default:
|
2021-03-27 14:35:46 +00:00
|
|
|
return nil, xerrors.Errorf("Invalid operation for msigApproveOrCancel")
|
2020-05-04 19:29:26 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
2021-03-27 14:35:46 +00:00
|
|
|
return nil, err
|
2020-05-04 19:29:26 +00:00
|
|
|
}
|
|
|
|
|
2021-03-27 14:35:46 +00:00
|
|
|
return &api.MessagePrototype{
|
|
|
|
Message: *msg,
|
|
|
|
ValidNonce: false,
|
|
|
|
}, nil
|
2020-05-04 19:29:26 +00:00
|
|
|
}
|
2020-07-16 00:55:27 +00:00
|
|
|
|
2020-09-06 05:52:30 +00:00
|
|
|
func serializeAddParams(new address.Address, inc bool) ([]byte, error) {
|
2020-10-08 01:09:33 +00:00
|
|
|
enc, actErr := actors.SerializeParams(&multisig2.AddSignerParams{
|
2020-09-06 05:52:30 +00:00
|
|
|
Signer: new,
|
|
|
|
Increase: inc,
|
|
|
|
})
|
|
|
|
if actErr != nil {
|
|
|
|
return nil, actErr
|
|
|
|
}
|
|
|
|
|
|
|
|
return enc, nil
|
|
|
|
}
|
|
|
|
|
2020-07-16 00:55:27 +00:00
|
|
|
func serializeSwapParams(old address.Address, new address.Address) ([]byte, error) {
|
2020-10-08 01:09:33 +00:00
|
|
|
enc, actErr := actors.SerializeParams(&multisig2.SwapSignerParams{
|
2020-07-16 00:55:27 +00:00
|
|
|
From: old,
|
|
|
|
To: new,
|
|
|
|
})
|
|
|
|
if actErr != nil {
|
|
|
|
return nil, actErr
|
|
|
|
}
|
|
|
|
|
|
|
|
return enc, nil
|
|
|
|
}
|
2020-10-09 16:40:25 +00:00
|
|
|
|
|
|
|
func serializeRemoveParams(rem address.Address, dec bool) ([]byte, error) {
|
2020-10-08 01:09:33 +00:00
|
|
|
enc, actErr := actors.SerializeParams(&multisig2.RemoveSignerParams{
|
2020-10-09 16:40:25 +00:00
|
|
|
Signer: rem,
|
|
|
|
Decrease: dec,
|
|
|
|
})
|
|
|
|
if actErr != nil {
|
|
|
|
return nil, actErr
|
|
|
|
}
|
|
|
|
|
|
|
|
return enc, nil
|
|
|
|
}
|