2019-10-17 04:53:25 +00:00
|
|
|
package validation
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-11-22 16:20:56 +00:00
|
|
|
"errors"
|
2019-10-17 04:53:25 +00:00
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/chain/actors"
|
|
|
|
"github.com/filecoin-project/lotus/chain/address"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/chain-validation/pkg/chain"
|
|
|
|
"github.com/filecoin-project/chain-validation/pkg/state"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Signer interface {
|
|
|
|
Sign(ctx context.Context, addr address.Address, msg []byte) (*types.Signature, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type MessageFactory struct {
|
|
|
|
signer Signer
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ chain.MessageFactory = &MessageFactory{}
|
|
|
|
|
|
|
|
func NewMessageFactory(signer Signer) *MessageFactory {
|
|
|
|
return &MessageFactory{signer}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mf *MessageFactory) MakeMessage(from, to state.Address, method chain.MethodID, nonce uint64, value, gasPrice state.AttoFIL, gasLimit state.GasUnit, params ...interface{}) (interface{}, error) {
|
|
|
|
fromDec, err := address.NewFromBytes([]byte(from))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
toDec, err := address.NewFromBytes([]byte(to))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
valueDec := types.BigInt{value}
|
|
|
|
paramsDec, err := state.EncodeValues(params...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if int(method) >= len(methods) {
|
|
|
|
return nil, errors.Errorf("No method name for method %v", method)
|
|
|
|
}
|
|
|
|
methodId := methods[method]
|
|
|
|
msg := &types.Message{
|
|
|
|
toDec,
|
|
|
|
fromDec,
|
|
|
|
nonce,
|
|
|
|
valueDec,
|
|
|
|
types.BigInt{gasPrice},
|
|
|
|
types.NewInt(uint64(gasLimit)),
|
|
|
|
methodId,
|
|
|
|
paramsDec,
|
|
|
|
}
|
|
|
|
|
|
|
|
return msg, nil
|
|
|
|
}
|
|
|
|
|
2019-11-13 14:48:57 +00:00
|
|
|
func (mf *MessageFactory) FromSingletonAddress(addr state.SingletonActorID) state.Address {
|
2019-10-17 04:53:25 +00:00
|
|
|
return fromSingletonAddress(addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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{
|
|
|
|
chain.NoMethod: 0,
|
|
|
|
chain.InitExec: actors.IAMethods.Exec,
|
|
|
|
|
2019-11-13 14:48:57 +00:00
|
|
|
chain.StoragePowerConstructor: actors.SPAMethods.Constructor,
|
2019-10-17 04:53:25 +00:00
|
|
|
chain.StoragePowerCreateStorageMiner: actors.SPAMethods.CreateStorageMiner,
|
2019-11-13 14:48:57 +00:00
|
|
|
chain.StoragePowerUpdatePower: actors.SPAMethods.UpdateStorage,
|
2019-10-17 04:53:25 +00:00
|
|
|
|
2019-11-13 14:48:57 +00:00
|
|
|
chain.StorageMinerUpdatePeerID: actors.MAMethods.UpdatePeerID,
|
|
|
|
chain.StorageMinerGetOwner: actors.MAMethods.GetOwner,
|
|
|
|
chain.StorageMinerGetPower: actors.MAMethods.GetPower,
|
2019-10-17 04:53:25 +00:00
|
|
|
chain.StorageMinerGetWorkerAddr: actors.MAMethods.GetWorkerAddr,
|
2019-11-13 14:48:57 +00:00
|
|
|
chain.StorageMinerGetPeerID: actors.MAMethods.GetPeerID,
|
2019-10-17 04:53:25 +00:00
|
|
|
chain.StorageMinerGetSectorSize: actors.MAMethods.GetSectorSize,
|
|
|
|
// More to follow...
|
|
|
|
}
|