2019-10-17 04:53:25 +00:00
|
|
|
package validation
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-11-26 21:58:43 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
|
|
|
vchain "github.com/filecoin-project/chain-validation/pkg/chain"
|
|
|
|
vactors "github.com/filecoin-project/chain-validation/pkg/state/actors"
|
|
|
|
vaddress "github.com/filecoin-project/chain-validation/pkg/state/address"
|
|
|
|
vtypes "github.com/filecoin-project/chain-validation/pkg/state/types"
|
|
|
|
|
2020-02-06 19:57:13 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/builtin"
|
2019-12-19 20:13:17 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
2020-02-06 19:57:13 +00:00
|
|
|
|
2019-10-17 04:53:25 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Signer interface {
|
2019-11-26 21:58:43 +00:00
|
|
|
Sign(ctx context.Context, addr vaddress.Address, msg []byte) (*types.Signature, error)
|
2019-10-17 04:53:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type MessageFactory struct {
|
|
|
|
signer Signer
|
|
|
|
}
|
|
|
|
|
2019-11-26 21:58:43 +00:00
|
|
|
var _ vchain.MessageFactory = &MessageFactory{}
|
2019-10-17 04:53:25 +00:00
|
|
|
|
|
|
|
func NewMessageFactory(signer Signer) *MessageFactory {
|
|
|
|
return &MessageFactory{signer}
|
|
|
|
}
|
|
|
|
|
2019-11-26 21:58:43 +00:00
|
|
|
func (mf *MessageFactory) MakeMessage(from, to vaddress.Address, method vchain.MethodID, nonce uint64, value, gasPrice vtypes.BigInt, gasLimit vtypes.GasUnit, params []byte) (interface{}, error) {
|
|
|
|
fromDec, err := address.NewFromBytes(from.Bytes())
|
2019-10-17 04:53:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-11-26 21:58:43 +00:00
|
|
|
toDec, err := address.NewFromBytes(to.Bytes())
|
2019-10-17 04:53:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-11-26 21:58:43 +00:00
|
|
|
valueDec := types.BigInt{value.Int}
|
2019-10-17 04:53:25 +00:00
|
|
|
|
|
|
|
if int(method) >= len(methods) {
|
2019-11-22 18:31:15 +00:00
|
|
|
return nil, xerrors.Errorf("No method name for method %v", method)
|
2019-10-17 04:53:25 +00:00
|
|
|
}
|
|
|
|
methodId := methods[method]
|
|
|
|
msg := &types.Message{
|
|
|
|
toDec,
|
|
|
|
fromDec,
|
|
|
|
nonce,
|
|
|
|
valueDec,
|
2019-11-26 21:58:43 +00:00
|
|
|
types.BigInt{gasPrice.Int},
|
2019-10-17 04:53:25 +00:00
|
|
|
types.NewInt(uint64(gasLimit)),
|
|
|
|
methodId,
|
2019-11-26 21:58:43 +00:00
|
|
|
params,
|
2019-10-17 04:53:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return msg, nil
|
|
|
|
}
|
|
|
|
|
2019-11-26 21:58:43 +00:00
|
|
|
func (mf *MessageFactory) FromSingletonAddress(addr vactors.SingletonActorID) vaddress.Address {
|
2019-10-17 04:53:25 +00:00
|
|
|
return fromSingletonAddress(addr)
|
|
|
|
}
|
|
|
|
|
2019-11-26 21:58:43 +00:00
|
|
|
func (mf *MessageFactory) FromActorCodeCid(code vactors.ActorCodeID) cid.Cid {
|
|
|
|
return fromActorCode(code)
|
|
|
|
}
|
|
|
|
|
2019-10-17 04:53:25 +00:00
|
|
|
// Maps method enumeration values to method names.
|
|
|
|
// This will change to a mapping to method ids when method dispatch is updated to use integers.
|
|
|
|
var methods = []uint64{
|
2019-11-26 21:58:43 +00:00
|
|
|
vchain.NoMethod: 0,
|
|
|
|
vchain.InitExec: actors.IAMethods.Exec,
|
|
|
|
|
|
|
|
vchain.StoragePowerConstructor: actors.SPAMethods.Constructor,
|
|
|
|
vchain.StoragePowerCreateStorageMiner: actors.SPAMethods.CreateStorageMiner,
|
|
|
|
vchain.StoragePowerUpdatePower: actors.SPAMethods.UpdateStorage,
|
|
|
|
|
|
|
|
vchain.StorageMinerUpdatePeerID: actors.MAMethods.UpdatePeerID,
|
|
|
|
vchain.StorageMinerGetOwner: actors.MAMethods.GetOwner,
|
|
|
|
vchain.StorageMinerGetPower: actors.MAMethods.GetPower,
|
|
|
|
vchain.StorageMinerGetWorkerAddr: actors.MAMethods.GetWorkerAddr,
|
|
|
|
vchain.StorageMinerGetPeerID: actors.MAMethods.GetPeerID,
|
|
|
|
vchain.StorageMinerGetSectorSize: actors.MAMethods.GetSectorSize,
|
2019-12-09 17:55:24 +00:00
|
|
|
|
2020-02-06 19:57:13 +00:00
|
|
|
vchain.MultiSigConstructor: uint64(builtin.MethodsMultisig.Constructor),
|
|
|
|
vchain.MultiSigPropose: uint64(builtin.MethodsMultisig.Propose),
|
|
|
|
vchain.MultiSigApprove: uint64(builtin.MethodsMultisig.Approve),
|
|
|
|
vchain.MultiSigCancel: uint64(builtin.MethodsMultisig.Cancel),
|
|
|
|
vchain.MultiSigClearCompleted: uint64(builtin.MethodsMultisig.ClearCompleted),
|
|
|
|
vchain.MultiSigAddSigner: uint64(builtin.MethodsMultisig.AddSigner),
|
|
|
|
vchain.MultiSigRemoveSigner: uint64(builtin.MethodsMultisig.RemoveSigner),
|
|
|
|
vchain.MultiSigSwapSigner: uint64(builtin.MethodsMultisig.SwapSigner),
|
|
|
|
vchain.MultiSigChangeRequirement: uint64(builtin.MethodsMultisig.ChangeNumApprovalsThreshold),
|
2019-10-17 04:53:25 +00:00
|
|
|
// More to follow...
|
|
|
|
}
|