From 76aba989e79c5b86c8d2948befc0a68ed1fa65d0 Mon Sep 17 00:00:00 2001 From: Prathamesh Musale Date: Tue, 10 Oct 2023 14:51:52 +0530 Subject: [PATCH] Add a PaymentsManager with Nitro node initialization --- cmd/serve.go | 13 +-- go.mod | 11 ++- go.sum | 30 ++++++ pkg/nitro/node.go | 48 ---------- pkg/payments/payments_manager.go | 153 +++++++++++++++++++++++++++++++ 5 files changed, 197 insertions(+), 58 deletions(-) delete mode 100644 pkg/nitro/node.go create mode 100644 pkg/payments/payments_manager.go 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 feb9346a..c8d5e0f6 100644 --- a/go.mod +++ b/go.mod @@ -23,6 +23,7 @@ require ( github.com/sirupsen/logrus v1.9.0 github.com/spf13/cobra v1.4.0 github.com/spf13/viper v1.11.0 + github.com/statechannels/go-nitro v0.1.1 gorm.io/driver/postgres v1.3.7 gorm.io/gorm v1.23.5 ) @@ -230,6 +231,14 @@ require ( github.com/subosito/gotenv v1.2.0 // indirect github.com/syndtr/goleveldb v1.0.1-0.20220614013038-64ee5596c38a // indirect github.com/thoas/go-funk v0.9.3 // indirect + github.com/tidwall/btree v1.6.0 // indirect + github.com/tidwall/buntdb v1.2.10 // indirect + github.com/tidwall/gjson v1.14.4 // indirect + github.com/tidwall/grect v0.1.4 // indirect + github.com/tidwall/match v1.1.1 // indirect + github.com/tidwall/pretty v1.2.1 // indirect + github.com/tidwall/rtred v0.1.2 // indirect + github.com/tidwall/tinyqueue v0.1.1 // indirect github.com/tklauser/go-sysconf v0.3.11 // indirect github.com/tklauser/numcpus v0.6.1 // indirect github.com/tyler-smith/go-bip39 v1.1.0 // indirect @@ -292,4 +301,4 @@ replace ( github.com/cerc-io/ipfs-ethdb/v5 => github.com/cerc-io/ipfs-ethdb/v5 v5.0.1-alpha.0.20231013070931-0b1a36562a28 ) -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.7 diff --git a/go.sum b/go.sum index e5ceef13..397bf025 100644 --- a/go.sum +++ b/go.sum @@ -91,12 +91,16 @@ github.com/blang/semver/v4 v4.0.0/go.mod h1:IbckMUScFkM3pff0VJDNKRiT6TG/YpiHIM2y github.com/bradfitz/go-smtpd v0.0.0-20170404230938-deb6d6237625/go.mod h1:HYsPBTaaSFSlLx/70C2HPIMNZpVV8+vt/A+FMnYP11g= github.com/btcsuite/btcd v0.0.0-20190824003749-130ea5bddde3/go.mod h1:3J08xEfcugPacsc34/LKRU2yO7YmuT8yt28J8k2+rrI= github.com/btcsuite/btcd v0.20.1-beta/go.mod h1:wVuoA8VJLEcwgqHBwHmzLRazpKxTv13Px/pDuV7OomQ= +github.com/btcsuite/btcd v0.22.0-beta h1:LTDpDKUM5EeOFBPM8IXpinEcmZ6FWfNZbE3lfrfdnWo= +github.com/btcsuite/btcd v0.22.0-beta/go.mod h1:9n5ntfhhHQBIhUvlhDvD3Qg6fRUj4jkN0VB8L8svzOA= github.com/btcsuite/btcd/btcec/v2 v2.3.2 h1:5n0X6hX0Zk+6omWcihdYvdAlGf2DfasC0GMf7DClJ3U= github.com/btcsuite/btcd/btcec/v2 v2.3.2/go.mod h1:zYzJ8etWJQIv1Ogk7OzpWjowwOdXY1W/17j2MW85J04= github.com/btcsuite/btcd/chaincfg/chainhash v1.0.2 h1:KdUfX2zKommPRa+PD0sWZUyXe9w277ABlgELO7H04IM= github.com/btcsuite/btcd/chaincfg/chainhash v1.0.2/go.mod h1:7SFka0XMvUgj3hfZtydOrQY2mwhPclbT2snogU7SQQc= github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f/go.mod h1:TdznJufoqS23FtqVCzL0ZqgP5MqXbb4fg/WgDys70nA= github.com/btcsuite/btcutil v0.0.0-20190425235716-9e5f4b9a998d/go.mod h1:+5NJ2+qvTyV9exUAL/rxXi3DcLg2Ts+ymUAY5y4NvMg= +github.com/btcsuite/btcutil v1.0.3-0.20201208143702-a53e38424cce h1:YtWJF7RHm2pYCvA5t0RPmAaLUhREsKuKd+SLhxFbFeQ= +github.com/btcsuite/btcutil v1.0.3-0.20201208143702-a53e38424cce/go.mod h1:0DVlHczLPewLcPGEIeUEzfOJhqGPQ0mJJRDBtD307+o= github.com/btcsuite/go-socks v0.0.0-20170105172521-4720035b7bfd/go.mod h1:HHNXQzUsZCxOoE+CPiyCTO6x34Zs86zZUiwtpXoGdtg= github.com/btcsuite/goleveldb v0.0.0-20160330041536-7834afc9e8cd/go.mod h1:F+uVaaLLH7j4eDXPRvw78tMflu7Ie2bzYOH4Y8rRKBY= github.com/btcsuite/snappy-go v0.0.0-20151229074030-0bdef8d06723/go.mod h1:8woku9dyThutzjeg+3xrA5iCpBRH8XEEg3lh6TiUghc= @@ -110,6 +114,8 @@ github.com/ceramicnetwork/go-dag-jose v0.1.0 h1:yJ/HVlfKpnD3LdYP03AHyTvbm3BpPiz2 github.com/ceramicnetwork/go-dag-jose v0.1.0/go.mod h1:qYA1nYt0X8u4XoMAVoOV3upUVKtrxy/I670Dg5F0wjI= github.com/cerc-io/eth-ipfs-state-validator/v5 v5.1.1-alpha.0.20231013075659-56aa03028c43 h1:pkGCN+VWo5Qmu4iDjA7noGrE6wM8VOVeX1Mn6ucYhPg= github.com/cerc-io/eth-ipfs-state-validator/v5 v5.1.1-alpha.0.20231013075659-56aa03028c43/go.mod h1:snThUFpyCrpZhTuz3HibJRLL2XaS+lKNsM3XAE0gB/4= +github.com/cerc-io/go-nitro v0.1.1-ts-port-0.1.7 h1:moqgKEUH9EtnyBgEQH65JrD8Q94abj+r6zGT6BJsU90= +github.com/cerc-io/go-nitro v0.1.1-ts-port-0.1.7/go.mod h1:gkKL37JcSo54ybLTI6VJRmP75bWEu9i1kc9RYmQLp+I= github.com/cerc-io/ipfs-ethdb/v5 v5.0.1-alpha.0.20231013070931-0b1a36562a28 h1:5FXtMuZXTIXjjzzLdqgyzx9pjD22FB5os2vXayRn+BQ= github.com/cerc-io/ipfs-ethdb/v5 v5.0.1-alpha.0.20231013070931-0b1a36562a28/go.mod h1:W1C6qTXGsPcsK1HKUYPsXmBORjO2ekdm+101sJkpdNI= github.com/cespare/cp v0.1.0 h1:SE+dxFebS7Iik5LK0tsi1k9ZCxEaFX4AjQmoyA+1dJk= @@ -789,6 +795,8 @@ github.com/miekg/dns v1.1.41/go.mod h1:p6aan82bvRIyn+zDIv9xYNUpwa73JcSh9BKwknJys github.com/miekg/dns v1.1.43/go.mod h1:+evo5L0630/F6ca/Z9+GAqzhjGyn8/c+TBaOyfEl0V4= github.com/miekg/dns v1.1.55 h1:GoQ4hpsj0nFLYe+bWiCToyrBEJXkQfOOIvFGFy0lEgo= github.com/miekg/dns v1.1.55/go.mod h1:uInx36IzPl7FYnDcMeVWxj9byh7DutNykX4G9Sj60FY= +github.com/miguelmota/go-ethereum-hdwallet v0.1.1 h1:zdXGlHao7idpCBjEGTXThVAtMKs+IxAgivZ75xqkWK0= +github.com/miguelmota/go-ethereum-hdwallet v0.1.1/go.mod h1:f9m9uXokAHA6WNoYOPjj4AqjJS5pquQRiYYj/XSyPYc= github.com/mikioh/tcp v0.0.0-20190314235350-803a9b46060c h1:bzE/A84HN25pxAuk9Eej1Kz9OUelF97nAc82bDquQI8= github.com/mikioh/tcp v0.0.0-20190314235350-803a9b46060c/go.mod h1:0SQS9kMwD2VsyFEB++InYyBJroV/FRmBgcydeSUcJms= github.com/mikioh/tcpinfo v0.0.0-20190314235526-30a79bb1804b h1:z78hV3sbSMAUoyUMM0I83AUIT6Hu17AWfgjzIbtrYFc= @@ -1052,6 +1060,28 @@ github.com/syndtr/goleveldb v1.0.1-0.20220614013038-64ee5596c38a/go.mod h1:RRCYJ github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07/go.mod h1:kDXzergiv9cbyO7IOYJZWg1U88JhDg3PB6klq9Hg2pA= github.com/thoas/go-funk v0.9.3 h1:7+nAEx3kn5ZJcnDm2Bh23N2yOtweO14bi//dvRtgLpw= github.com/thoas/go-funk v0.9.3/go.mod h1:+IWnUfUmFO1+WVYQWQtIJHeRRdaIyyYglZN7xzUPe4Q= +github.com/tidwall/assert v0.1.0 h1:aWcKyRBUAdLoVebxo95N7+YZVTFF/ASTr7BN4sLP6XI= +github.com/tidwall/assert v0.1.0/go.mod h1:QLYtGyeqse53vuELQheYl9dngGCJQ+mTtlxcktb+Kj8= +github.com/tidwall/btree v1.6.0 h1:LDZfKfQIBHGHWSwckhXI0RPSXzlo+KYdjK7FWSqOzzg= +github.com/tidwall/btree v1.6.0/go.mod h1:twD9XRA5jj9VUQGELzDO4HPQTNJsoWWfYEL+EUQ2cKY= +github.com/tidwall/buntdb v1.2.10 h1:U/ebfkmYPBnyiNZIirUiWFcxA/mgzjbKlyPynFsPtyM= +github.com/tidwall/buntdb v1.2.10/go.mod h1:lZZrZUWzlyDJKlLQ6DKAy53LnG7m5kHyrEHvvcDmBpU= +github.com/tidwall/gjson v1.12.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= +github.com/tidwall/gjson v1.14.4 h1:uo0p8EbA09J7RQaflQ1aBRffTR7xedD2bcIVSYxLnkM= +github.com/tidwall/gjson v1.14.4/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= +github.com/tidwall/grect v0.1.4 h1:dA3oIgNgWdSspFzn1kS4S/RDpZFLrIxAZOdJKjYapOg= +github.com/tidwall/grect v0.1.4/go.mod h1:9FBsaYRaR0Tcy4UwefBX/UDcDcDy9V5jUcxHzv2jd5Q= +github.com/tidwall/lotsa v1.0.2 h1:dNVBH5MErdaQ/xd9s769R31/n2dXavsQ0Yf4TMEHHw8= +github.com/tidwall/lotsa v1.0.2/go.mod h1:X6NiU+4yHA3fE3Puvpnn1XMDrFZrE9JO2/w+UMuqgR8= +github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= +github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= +github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= +github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4= +github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= +github.com/tidwall/rtred v0.1.2 h1:exmoQtOLvDoO8ud++6LwVsAMTu0KPzLTUrMln8u1yu8= +github.com/tidwall/rtred v0.1.2/go.mod h1:hd69WNXQ5RP9vHd7dqekAz+RIdtfBogmglkZSRxCHFQ= +github.com/tidwall/tinyqueue v0.1.1 h1:SpNEvEggbpyN5DIReaJ2/1ndroY8iyEGxPYxoSaymYE= +github.com/tidwall/tinyqueue v0.1.1/go.mod h1:O/QNHwrnjqr6IHItYrzoHAKYhBkLI67Q096fQP5zMYw= github.com/tj/assert v0.0.3 h1:Df/BlaZ20mq6kuai7f5z2TvPFiwC3xaWJSDQNiIS3Rk= github.com/tj/assert v0.0.3/go.mod h1:Ne6X72Q+TB1AteidzQncjw9PabbMp4PBMZ1k+vd1Pvk= github.com/tklauser/go-sysconf v0.3.11 h1:89WgdJhk5SNwJfu+GKyYveZ4IaJ7xAkecBo+KdJV0CM= 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 +}