lotus/tvx/chain/message_producer.go

95 lines
2.5 KiB
Go
Raw Normal View History

generate test-vectors based on tests from chain-validation project (#181) * Read a subset of filecoin state over the full node API * wip * import export wip * extract actors from message * generate car for any state * library for providing a pruned statetree * test vector schema draft + example 'message' class test vector. * message => messages test vector class. * fixup * wip * use lb.NewBlockstore with ID * fixup lotus-soup, and generate * fix deals * magic params * work on schema / export of test vector * fixup * wip deserialise state tree * pass at building a test case from a message * progress loading / serializing * recreation of ipld nodes * generation of vector creates json * kick off tvx tool. * wip * wip * retain init actor state. * initial test with printed out state * remove testing.T, but keep require and assert libraries * wip refactor state tree plucking. * simplified * removed factories * remove builder.Build ; remove interface - use concrete iface * comment out validateState * remove Validator * remove TestDriverBuilder * remove client * remove box * remove gen * remove factories * remove KeyManager interfafce * moved stuff around * remove ValidationConfig * extract randomness * extract config and key_manager * extract statewrapper * extract applier * rename factories to various * flatten chain-validation package * initial marshal of test vector * do not require schema package * fixup * run all messages tests * better names * run all messages tests * remove Indent setting from JSON encoder for now * refactor, and actually running successfully ;-) * remove irrelevant files; rename extract-msg command. * remove root CID from state_tree object in schema. * add tvx/lotus package; adjust .gitignore. * tidy up command flag management. * add comment. * remove validateState and trackState * remove xerrors * remove NewVM * remove commented out RootCID sets * enable more tests * add all `message_application` tests * delete all.json * update Message struct * fix message serialization * support multiple messages * gofmt * remove custom Message and SignedMessage types * update tests with gzip and adhere to new schema for compressed CAR * improved iface for Marshal * update Validation * remove test-suites and utils * better names for chain. methods * go mod tidy * remove top-level dummyT Co-authored-by: Will Scott <will@cypherpunk.email> Co-authored-by: Raúl Kripalani <raul@protocol.ai>
2020-08-05 17:40:09 +00:00
package chain
import (
address "github.com/filecoin-project/go-address"
abi_spec "github.com/filecoin-project/specs-actors/actors/abi"
big_spec "github.com/filecoin-project/specs-actors/actors/abi/big"
"github.com/filecoin-project/lotus/chain/types"
)
// The created messages are retained for subsequent export or evaluation in a VM.
type MessageProducer struct {
defaults msgOpts // Note non-pointer reference.
messages []*types.Message
}
// NewMessageProducer creates a new message producer, delegating message creation to `factory`.
func NewMessageProducer(defaultGasLimit int64, defaultGasPrice big_spec.Int) *MessageProducer {
return &MessageProducer{
defaults: msgOpts{
value: big_spec.Zero(),
gasLimit: defaultGasLimit,
gasPrice: defaultGasPrice,
},
}
}
// Messages returns a slice containing all messages created by the producer.
func (mp *MessageProducer) Messages() []*types.Message {
return mp.messages
}
// BuildFull creates and returns a single message.
func (mp *MessageProducer) BuildFull(from, to address.Address, method abi_spec.MethodNum, nonce uint64, value, gasPrice big_spec.Int, gasLimit int64, params []byte) *types.Message {
fm := &types.Message{
To: to,
From: from,
Nonce: nonce,
Value: value,
Method: method,
Params: params,
GasPrice: gasPrice,
GasLimit: gasLimit,
}
mp.messages = append(mp.messages, fm)
return fm
}
// Build creates and returns a single message, using default gas parameters unless modified by `opts`.
func (mp *MessageProducer) Build(from, to address.Address, method abi_spec.MethodNum, params []byte, opts ...MsgOpt) *types.Message {
values := mp.defaults
for _, opt := range opts {
opt(&values)
}
return mp.BuildFull(from, to, method, values.nonce, values.value, values.gasPrice, values.gasLimit, params)
}
// msgOpts specifies value and gas parameters for a message, supporting a functional options pattern
// for concise but customizable message construction.
type msgOpts struct {
nonce uint64
value big_spec.Int
gasPrice big_spec.Int
gasLimit int64
}
// MsgOpt is an option configuring message value or gas parameters.
type MsgOpt func(*msgOpts)
func Value(value big_spec.Int) MsgOpt {
return func(opts *msgOpts) {
opts.value = value
}
}
func Nonce(n uint64) MsgOpt {
return func(opts *msgOpts) {
opts.nonce = n
}
}
func GasLimit(limit int64) MsgOpt {
return func(opts *msgOpts) {
opts.gasLimit = limit
}
}
func GasPrice(price int64) MsgOpt {
return func(opts *msgOpts) {
opts.gasPrice = big_spec.NewInt(price)
}
}