ipld-eth-server/pkg/payments/payments_manager.go

154 lines
4.0 KiB
Go
Raw Normal View History

// 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
}