diff --git a/chain/call.go b/chain/call.go new file mode 100644 index 000000000..31fd98310 --- /dev/null +++ b/chain/call.go @@ -0,0 +1,49 @@ +package chain + +import ( + "context" + + "github.com/filecoin-project/go-lotus/chain/actors" + "github.com/filecoin-project/go-lotus/chain/store" + "github.com/filecoin-project/go-lotus/chain/types" + "github.com/filecoin-project/go-lotus/chain/vm" + "golang.org/x/xerrors" +) + +func Call(ctx context.Context, cs *store.ChainStore, msg *types.Message, ts *types.TipSet) (*types.MessageReceipt, error) { + if ts == nil { + ts = cs.GetHeaviestTipSet() + } + state, err := cs.TipSetState(ts.Cids()) + if err != nil { + return nil, err + } + + vmi, err := vm.NewVM(state, ts.Height(), ts.Blocks()[0].Miner, cs) + if err != nil { + return nil, xerrors.Errorf("failed to set up vm: %w", err) + } + + if msg.GasLimit == types.EmptyInt { + msg.GasLimit = types.NewInt(10000000000) + } + if msg.GasPrice == types.EmptyInt { + msg.GasPrice = types.NewInt(0) + } + if msg.Value == types.EmptyInt { + msg.Value = types.NewInt(0) + } + if msg.Params == nil { + msg.Params, err = actors.SerializeParams(struct{}{}) + if err != nil { + return nil, err + } + } + + // TODO: maybe just use the invoker directly? + ret, err := vmi.ApplyMessage(ctx, msg) + if ret.ActorErr != nil { + log.Warnf("chain call failed: %s", ret.ActorErr) + } + return &ret.MessageReceipt, err +} diff --git a/go.mod b/go.mod index 24b63d594..265818c2c 100644 --- a/go.mod +++ b/go.mod @@ -61,6 +61,7 @@ require ( github.com/multiformats/go-multihash v0.0.6 github.com/pkg/errors v0.8.1 github.com/polydawn/refmt v0.0.0-20190809202753-05966cbd336a + github.com/prometheus/common v0.2.0 github.com/smartystreets/assertions v1.0.1 // indirect github.com/smartystreets/goconvey v0.0.0-20190710185942-9d28bd7c0945 // indirect github.com/stretchr/testify v1.3.0 diff --git a/go.sum b/go.sum index 8f4946451..f099e195f 100644 --- a/go.sum +++ b/go.sum @@ -19,7 +19,9 @@ github.com/Stebalien/go-bitfield v0.0.0-20180330043415-076a62f9ce6e/go.mod h1:3o github.com/Stebalien/go-bitfield v0.0.1 h1:X3kbSSPUaJK60wV2hjOPZwmpljr6VGCqdq4cBLhbQBo= github.com/Stebalien/go-bitfield v0.0.1/go.mod h1:GNjFpasyUVkHMsfEOk8EFLJ9syQ6SI+XWrX9Wf2XH0s= github.com/aead/siphash v1.0.1/go.mod h1:Nywa3cDsYNNK3gaciGTWPwHt0wlpNV15vwmswBAUSII= +github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc h1:cAKDfWh5VpdgMhJosfJnn5/FoN2SRZ4p7fJNX58YPaU= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= +github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf h1:qet1QNfXsQxTZqLG4oE62mJzwPIB8+Tee4RNCL9ulrY= github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= github.com/apache/thrift v0.12.0 h1:pODnxUFNcjP9UTLZGTdeh+j16A8lJbRvD3rOtrk/7bs= github.com/apache/thrift v0.12.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ= @@ -549,6 +551,7 @@ github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXP github.com/prometheus/client_golang v0.9.3-0.20190127221311-3c4408c8b829/go.mod h1:p2iRAGwDERtqlqzRXnrOVns+ignqQo//hLXqYxZYVNs= github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo= github.com/prometheus/client_model v0.0.0-20190115171406-56726106282f/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo= +github.com/prometheus/common v0.2.0 h1:kUZDBDTdBVBYBj5Tmh2NZLlF60mfjA27rM34b+cVwNU= github.com/prometheus/common v0.2.0/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4= github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= github.com/prometheus/procfs v0.0.0-20190117184657-bf6a532e95b1/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= diff --git a/node/impl/full.go b/node/impl/full.go index d2521b5fc..c71a29b05 100644 --- a/node/impl/full.go +++ b/node/impl/full.go @@ -3,9 +3,10 @@ package impl import ( "context" "fmt" - "github.com/filecoin-project/go-lotus/lib/bufbstore" "strconv" + "github.com/filecoin-project/go-lotus/lib/bufbstore" + "github.com/filecoin-project/go-lotus/api" "github.com/filecoin-project/go-lotus/chain" "github.com/filecoin-project/go-lotus/chain/actors" @@ -15,7 +16,6 @@ import ( "github.com/filecoin-project/go-lotus/chain/state" "github.com/filecoin-project/go-lotus/chain/store" "github.com/filecoin-project/go-lotus/chain/types" - "github.com/filecoin-project/go-lotus/chain/vm" "github.com/filecoin-project/go-lotus/chain/wallet" "github.com/filecoin-project/go-lotus/miner" "github.com/filecoin-project/go-lotus/node/client" @@ -157,41 +157,7 @@ func (a *FullNodeAPI) ChainGetBlockReceipts(ctx context.Context, bcid cid.Cid) ( } func (a *FullNodeAPI) ChainCall(ctx context.Context, msg *types.Message, ts *types.TipSet) (*types.MessageReceipt, error) { - if ts == nil { - ts = a.Chain.GetHeaviestTipSet() - } - state, err := a.Chain.TipSetState(ts.Cids()) - if err != nil { - return nil, err - } - - vmi, err := vm.NewVM(state, ts.Height(), ts.Blocks()[0].Miner, a.Chain) - if err != nil { - return nil, xerrors.Errorf("failed to set up vm: %w", err) - } - - if msg.GasLimit == types.EmptyInt { - msg.GasLimit = types.NewInt(10000000000) - } - if msg.GasPrice == types.EmptyInt { - msg.GasPrice = types.NewInt(0) - } - if msg.Value == types.EmptyInt { - msg.Value = types.NewInt(0) - } - if msg.Params == nil { - msg.Params, err = actors.SerializeParams(struct{}{}) - if err != nil { - return nil, err - } - } - - // TODO: maybe just use the invoker directly? - ret, err := vmi.ApplyMessage(ctx, msg) - if ret.ActorErr != nil { - log.Warnf("chain call failed: %s", ret.ActorErr) - } - return &ret.MessageReceipt, err + return chain.Call(ctx, a.Chain, msg, ts) } func (a *FullNodeAPI) stateForTs(ts *types.TipSet) (*state.StateTree, error) { diff --git a/node/modules/paych.go b/node/modules/paych.go index a032a2992..4050d858a 100644 --- a/node/modules/paych.go +++ b/node/modules/paych.go @@ -1,7 +1,6 @@ package modules import ( - "github.com/filecoin-project/go-lotus/api" "github.com/filecoin-project/go-lotus/chain/store" "github.com/filecoin-project/go-lotus/node/modules/dtypes" "github.com/filecoin-project/go-lotus/paych" @@ -12,7 +11,5 @@ func PaychStore(ds dtypes.MetadataDS) *paych.Store { } func PaymentChannelManager(chain *store.ChainStore, store *paych.Store) (*paych.Manager, error) { - var api api.FullNode - panic("i need a full node api. what do i do") - return paych.NewManager(api, chain, store), nil + return paych.NewManager(chain, store), nil } diff --git a/paych/paych.go b/paych/paych.go index 7fbfe1b95..611c1128f 100644 --- a/paych/paych.go +++ b/paych/paych.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/filecoin-project/go-lotus/chain" "github.com/filecoin-project/go-lotus/chain/actors" "github.com/filecoin-project/go-lotus/chain/address" "github.com/filecoin-project/go-lotus/chain/state" @@ -13,19 +14,13 @@ import ( hamt "github.com/ipfs/go-hamt-ipld" ) -type paychMgrApi interface { - ChainCall(ctx context.Context, msg *types.Message, ts *types.TipSet) (*types.MessageReceipt, error) -} - type Manager struct { chain *store.ChainStore - api paychMgrApi store *Store } -func NewManager(api paychMgrApi, chain *store.ChainStore, pchstore *Store) *Manager { +func NewManager(chain *store.ChainStore, pchstore *Store) *Manager { return &Manager{ - api: api, chain: chain, store: pchstore, } @@ -128,7 +123,7 @@ func (pm *Manager) CheckVoucherSpendable(ctx context.Context, ch address.Address return false, err } - ret, err := pm.api.ChainCall(ctx, &types.Message{ + ret, err := chain.Call(ctx, pm.chain, &types.Message{ From: owner, To: ch, Method: actors.PCAMethods.UpdateChannelState, @@ -171,7 +166,7 @@ func (pm *Manager) loadPaychState(ctx context.Context, ch address.Address) (*typ } func (pm *Manager) getPaychOwner(ctx context.Context, ch address.Address) (address.Address, error) { - ret, err := pm.api.ChainCall(ctx, &types.Message{ + ret, err := chain.Call(ctx, pm.chain, &types.Message{ From: ch, To: ch, Method: actors.PCAMethods.GetOwner,