lotus/chain/validation/message.go

100 lines
3.4 KiB
Go
Raw Normal View History

package validation
import (
"context"
2020-02-23 07:49:15 +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"
"github.com/filecoin-project/go-address"
2020-02-23 07:49:15 +00:00
"github.com/filecoin-project/specs-actors/actors/abi"
2020-02-08 02:18:37 +00:00
"github.com/filecoin-project/specs-actors/actors/builtin"
2020-02-25 21:09:22 +00:00
"github.com/filecoin-project/specs-actors/actors/crypto"
2020-02-06 19:57:13 +00:00
"github.com/filecoin-project/lotus/chain/types"
)
type Signer interface {
2020-02-25 21:09:22 +00:00
Sign(ctx context.Context, addr vaddress.Address, msg []byte) (*crypto.Signature, error)
}
type MessageFactory struct {
signer Signer
}
var _ vchain.MessageFactory = &MessageFactory{}
func NewMessageFactory(signer Signer) *MessageFactory {
return &MessageFactory{signer}
}
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())
if err != nil {
return nil, err
}
toDec, err := address.NewFromBytes(to.Bytes())
if err != nil {
return nil, err
}
valueDec := types.BigInt{value.Int}
if int(method) >= len(methods) {
return nil, xerrors.Errorf("No method name for method %v", method)
}
methodId := methods[method]
msg := &types.Message{
toDec,
fromDec,
nonce,
valueDec,
types.BigInt{gasPrice.Int},
types.NewInt(uint64(gasLimit)),
2020-02-23 07:49:15 +00:00
abi.MethodNum(methodId),
params,
}
return msg, nil
}
func (mf *MessageFactory) FromSingletonAddress(addr vactors.SingletonActorID) vaddress.Address {
return fromSingletonAddress(addr)
}
func (mf *MessageFactory) FromActorCodeCid(code vactors.ActorCodeID) cid.Cid {
return fromActorCode(code)
}
// 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{
vchain.NoMethod: 0,
2020-02-23 07:49:15 +00:00
vchain.InitExec: uint64(builtin.MethodsInit.Exec),
vchain.StoragePowerConstructor: uint64(builtin.MethodsPower.Constructor),
vchain.StoragePowerCreateStorageMiner: uint64(builtin.MethodsPower.CreateMiner),
vchain.StorageMinerUpdatePeerID: 0, //actors.MAMethods.UpdatePeerID,
vchain.StorageMinerGetOwner: 0, //actors.MAMethods.GetOwner,
vchain.StorageMinerGetPower: 0, //actors.MAMethods.GetPower,
vchain.StorageMinerGetWorkerAddr: 0, //actors.MAMethods.GetWorkerAddr,
vchain.StorageMinerGetPeerID: 0, //actors.MAMethods.GetPeerID,
vchain.StorageMinerGetSectorSize: 0, //actors.MAMethods.GetSectorSize,
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),
// More to follow...
}