diff --git a/cmd/serve.go b/cmd/serve.go index 8a4f110a..c529d6c0 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -26,7 +26,7 @@ import ( "time" "github.com/cerc-io/ipld-eth-server/v5/pkg/log" - "github.com/cerc-io/ipld-eth-server/v5/pkg/nitro" + "github.com/cerc-io/ipld-eth-server/v5/pkg/payments" "github.com/ethereum/go-ethereum/rpc" "github.com/mailgun/groupcache/v2" "github.com/spf13/cobra" @@ -98,11 +98,8 @@ func serve() { // TODO: Create required config for Nitro node - // TODO: Create a new Nitro node using the config - nitro, _ := nitro.NewNitroNode() - - // TODO: Start the Nitro node, pass wg - nitro.Start(wg) + paymentsManager, _ := payments.NewPaymentsManager(true) + paymentsManager.Start(wg) shutdown := make(chan os.Signal, 1) signal.Notify(shutdown, os.Interrupt) @@ -111,9 +108,7 @@ func serve() { graphQL.Stop() } server.Stop() - - // TODO: Stop nitro node - nitro.Stop() + paymentsManager.Stop() wg.Wait() } diff --git a/go.mod b/go.mod index 277dcaa1..84bda243 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,7 @@ require ( github.com/cerc-io/ipfs-ethdb/v5 v5.0.0-alpha github.com/cerc-io/ipld-eth-statedb v0.0.5-alpha github.com/cerc-io/plugeth-statediff v0.1.1 - github.com/ethereum/go-ethereum v1.12.0 // TODO: Investigate + github.com/ethereum/go-ethereum v1.12.0 // TODO: Investigate github.com/google/uuid v1.3.0 github.com/graph-gophers/graphql-go v1.3.0 github.com/ipfs/go-cid v0.4.1 @@ -299,4 +299,4 @@ replace ( github.com/cerc-io/ipfs-ethdb/v5 => /home/prathamesh/deepstack/ipfs-ethdb ) -replace github.com/statechannels/go-nitro v0.1.1 => github.com/cerc-io/go-nitro v0.1.1-ts-port-0.1.5 +replace github.com/statechannels/go-nitro v0.1.1 => github.com/cerc-io/go-nitro v0.1.1-ts-port-0.1.6 diff --git a/go.sum b/go.sum index 120b3bf2..cef443e7 100644 --- a/go.sum +++ b/go.sum @@ -112,8 +112,8 @@ github.com/cenkalti/backoff/v4 v4.2.1/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyY github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/ceramicnetwork/go-dag-jose v0.1.0 h1:yJ/HVlfKpnD3LdYP03AHyTvbm3BpPiz2oZiOeReJRdU= github.com/ceramicnetwork/go-dag-jose v0.1.0/go.mod h1:qYA1nYt0X8u4XoMAVoOV3upUVKtrxy/I670Dg5F0wjI= -github.com/cerc-io/go-nitro v0.1.1-ts-port-0.1.5 h1:Htdvri3Mc407d0dGBwQKQjwIc1o45rVOC1XLHL7wLFM= -github.com/cerc-io/go-nitro v0.1.1-ts-port-0.1.5/go.mod h1:Cc6AgGm/Ou9P6vdssCPRDfrpb9iKIhY2hiPcWX4aOrw= +github.com/cerc-io/go-nitro v0.1.1-ts-port-0.1.6 h1:rtOsOFPnz1bX6Z/Ejxv19PuDEMFxXElvGpTx5pbBkXU= +github.com/cerc-io/go-nitro v0.1.1-ts-port-0.1.6/go.mod h1:Cc6AgGm/Ou9P6vdssCPRDfrpb9iKIhY2hiPcWX4aOrw= github.com/cespare/cp v0.1.0 h1:SE+dxFebS7Iik5LK0tsi1k9ZCxEaFX4AjQmoyA+1dJk= github.com/cespare/cp v0.1.0/go.mod h1:SOGHArjBr4JWaSDEVpWpo/hNg6RoKrls6Oh40hiwW+s= github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko= diff --git a/pkg/nitro/node.go b/pkg/nitro/node.go deleted file mode 100644 index 8ca98430..00000000 --- a/pkg/nitro/node.go +++ /dev/null @@ -1,48 +0,0 @@ -// VulcanizeDB -// Copyright © 2023 Vulcanize - -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. - -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Affero General Public License for more details. - -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - -package nitro - -import ( - "sync" -) - -// TODO: Implement -// Struct representing the in-process Nitro node -type NitroNode struct { - // Used to signal shutdown of the node - QuitChan chan bool -} - -func NewNitroNode() (NitroNode, error) { - // TODO: Implement - return NitroNode{}, nil -} - -func (n *NitroNode) Start(wg *sync.WaitGroup) { - // TODO: Implement - go func() { - wg.Add(1) - defer wg.Done() - <-n.QuitChan - }() -} - -func (n *NitroNode) Stop() error { - // TODO: Implement - close(n.QuitChan) - return nil -} diff --git a/pkg/payments/payments_manager.go b/pkg/payments/payments_manager.go new file mode 100644 index 00000000..1b6f3697 --- /dev/null +++ b/pkg/payments/payments_manager.go @@ -0,0 +1,153 @@ +// VulcanizeDB +// Copyright © 2023 Vulcanize + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. + +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +package payments + +import ( + "sync" + + "github.com/cerc-io/ipld-eth-server/v5/pkg/log" + "github.com/ethereum/go-ethereum/common" + "github.com/statechannels/go-nitro/node/engine" + "github.com/statechannels/go-nitro/node/engine/chainservice" + "github.com/statechannels/go-nitro/node/engine/store" + + nitroNode "github.com/statechannels/go-nitro/node" + p2pms "github.com/statechannels/go-nitro/node/engine/messageservice/p2p-message-service" +) + +// TODO: Implement +// Struct representing payments manager +// Maintains either an in-process or communication with an out-of-process Nitro node +type PaymentsManager struct { + // Whether to run an in-process Nitro node + runInProcessNitroNode bool + + // In-process Nitro node; nil when runInProcessNitroNode is false + nitroNode *nitroNode.Node + + // Used to signal shutdown of the service + quitChan chan bool +} + +func NewPaymentsManager(runInProcessNitroNode bool) (PaymentsManager, error) { + // TODO: Implement + var err error + + pm := PaymentsManager{runInProcessNitroNode: runInProcessNitroNode} + + if runInProcessNitroNode { + pm.nitroNode, err = initializeNitroNode() + if err != nil { + return PaymentsManager{}, err + } + } + + return pm, nil +} + +func (pm *PaymentsManager) Start(wg *sync.WaitGroup) { + // TODO: Implement + go func() { + wg.Add(1) + defer wg.Done() + <-pm.quitChan + }() +} + +func (pm *PaymentsManager) Stop() error { + // TODO: Implement + close(pm.quitChan) + return nil +} + +func initializeNitroNode() (*nitroNode.Node, error) { + // TODO: Configure + pkString := "" + useDurableStore := true + durableStoreFolder := "./data/nitro-store" + msgPort := 3005 + wsMsgPort := 5005 + chainUrl := "ws://127.0.0.1:8546" + chainStartBlock := uint64(0) + chainPk := "" + naAddress := "" + vpaAddress := "" + caAddress := "" + + chainAuthToken := "" + publicIp := "0.0.0.0" + + chainOpts := chainservice.ChainOpts{ + ChainUrl: chainUrl, + ChainStartBlock: chainStartBlock, + ChainAuthToken: chainAuthToken, + ChainPk: chainPk, + NaAddress: common.HexToAddress(naAddress), + VpaAddress: common.HexToAddress(vpaAddress), + CaAddress: common.HexToAddress(caAddress), + } + + storeOpts := store.StoreOpts{ + PkBytes: common.Hex2Bytes(pkString), + UseDurableStore: useDurableStore, + DurableStoreFolder: durableStoreFolder, + } + + peerSlice := []string{} + + messageOpts := p2pms.MessageOpts{ + PkBytes: common.Hex2Bytes(pkString), + TcpPort: msgPort, + WsMsgPort: wsMsgPort, + BootPeers: peerSlice, + PublicIp: publicIp, + } + + ourStore, err := store.NewStore(storeOpts) + if err != nil { + return nil, err + } + + log.Info("Initializing message service", "tcp port", messageOpts.TcpPort, "web socket port", messageOpts.WsMsgPort) + messageOpts.SCAddr = *ourStore.GetAddress() + messageService := p2pms.NewMessageService(messageOpts) + + // Compare chainOpts.ChainStartBlock to lastBlockNum seen in store. The larger of the two + // gets passed as an argument when creating NewEthChainService + storeBlockNum, err := ourStore.GetLastBlockNumSeen() + if err != nil { + return nil, err + } + if storeBlockNum > chainOpts.ChainStartBlock { + chainOpts.ChainStartBlock = storeBlockNum + } + + log.Info("Initializing chain service...") + ourChain, err := chainservice.NewEthChainService(chainOpts) + if err != nil { + return nil, err + } + + node := nitroNode.New( + messageService, + ourChain, + ourStore, + &engine.PermissivePolicy{}, + ) + + return &node, nil +}