Add a PaymentsManager with Nitro node initialization

This commit is contained in:
Prathamesh Musale 2023-10-10 14:51:52 +05:30
parent 72f5007a72
commit 233bffd4a1
5 changed files with 161 additions and 61 deletions

View File

@ -26,7 +26,7 @@ import (
"time" "time"
"github.com/cerc-io/ipld-eth-server/v5/pkg/log" "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/ethereum/go-ethereum/rpc"
"github.com/mailgun/groupcache/v2" "github.com/mailgun/groupcache/v2"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -98,11 +98,8 @@ func serve() {
// TODO: Create required config for Nitro node // TODO: Create required config for Nitro node
// TODO: Create a new Nitro node using the config paymentsManager, _ := payments.NewPaymentsManager(true)
nitro, _ := nitro.NewNitroNode() paymentsManager.Start(wg)
// TODO: Start the Nitro node, pass wg
nitro.Start(wg)
shutdown := make(chan os.Signal, 1) shutdown := make(chan os.Signal, 1)
signal.Notify(shutdown, os.Interrupt) signal.Notify(shutdown, os.Interrupt)
@ -111,9 +108,7 @@ func serve() {
graphQL.Stop() graphQL.Stop()
} }
server.Stop() server.Stop()
paymentsManager.Stop()
// TODO: Stop nitro node
nitro.Stop()
wg.Wait() wg.Wait()
} }

4
go.mod
View File

@ -8,7 +8,7 @@ require (
github.com/cerc-io/ipfs-ethdb/v5 v5.0.0-alpha 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/ipld-eth-statedb v0.0.5-alpha
github.com/cerc-io/plugeth-statediff v0.1.1 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/google/uuid v1.3.0
github.com/graph-gophers/graphql-go v1.3.0 github.com/graph-gophers/graphql-go v1.3.0
github.com/ipfs/go-cid v0.4.1 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 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

4
go.sum
View File

@ -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/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 h1:yJ/HVlfKpnD3LdYP03AHyTvbm3BpPiz2oZiOeReJRdU=
github.com/ceramicnetwork/go-dag-jose v0.1.0/go.mod h1:qYA1nYt0X8u4XoMAVoOV3upUVKtrxy/I670Dg5F0wjI= 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.6 h1:rtOsOFPnz1bX6Z/Ejxv19PuDEMFxXElvGpTx5pbBkXU=
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/go.mod h1:Cc6AgGm/Ou9P6vdssCPRDfrpb9iKIhY2hiPcWX4aOrw=
github.com/cespare/cp v0.1.0 h1:SE+dxFebS7Iik5LK0tsi1k9ZCxEaFX4AjQmoyA+1dJk= 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/cp v0.1.0/go.mod h1:SOGHArjBr4JWaSDEVpWpo/hNg6RoKrls6Oh40hiwW+s=
github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko= github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=

View File

@ -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 <http://www.gnu.org/licenses/>.
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
}

View File

@ -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 <http://www.gnu.org/licenses/>.
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
}