55 lines
1.4 KiB
Go
55 lines
1.4 KiB
Go
package genesis
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/filecoin-project/go-address"
|
|
"github.com/filecoin-project/specs-actors/actors/abi"
|
|
cbg "github.com/whyrusleeping/cbor-gen"
|
|
"golang.org/x/xerrors"
|
|
|
|
"github.com/filecoin-project/lotus/chain/actors"
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
"github.com/filecoin-project/lotus/chain/vm"
|
|
)
|
|
|
|
func mustEnc(i cbg.CBORMarshaler) []byte {
|
|
enc, err := actors.SerializeParams(i)
|
|
if err != nil {
|
|
panic(err) // ok
|
|
}
|
|
return enc
|
|
}
|
|
|
|
func doExec(ctx context.Context, vm *vm.VM, to, from address.Address, method abi.MethodNum, params []byte) ([]byte, error) {
|
|
return doExecValue(ctx, vm, to, from, types.NewInt(0), method, params)
|
|
}
|
|
|
|
func doExecValue(ctx context.Context, vm *vm.VM, to, from address.Address, value types.BigInt, method abi.MethodNum, params []byte) ([]byte, error) {
|
|
act, err := vm.StateTree().GetActor(from)
|
|
if err != nil {
|
|
return nil, xerrors.Errorf("doExec failed to get from actor: %w", err)
|
|
}
|
|
|
|
ret, err := vm.ApplyMessage(context.TODO(), &types.Message{
|
|
To: to,
|
|
From: from,
|
|
Method: method,
|
|
Params: params,
|
|
GasLimit: types.NewInt(1000000),
|
|
GasPrice: types.NewInt(0),
|
|
Value: value,
|
|
Nonce: act.Nonce,
|
|
})
|
|
if err != nil {
|
|
return nil, xerrors.Errorf("doExec apply message failed: %w", err)
|
|
}
|
|
|
|
if ret.ExitCode != 0 {
|
|
return nil, fmt.Errorf("failed to call method: %s", ret.ActorErr)
|
|
}
|
|
|
|
return ret.Return, nil
|
|
}
|