Skip to content

Commit

Permalink
wire discovery added
Browse files Browse the repository at this point in the history
  • Loading branch information
ravi0131 committed May 14, 2024
1 parent b9c23ff commit 19d47e6
Show file tree
Hide file tree
Showing 4 changed files with 575 additions and 38 deletions.
51 changes: 30 additions & 21 deletions payment-channel-ckb/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import (
"github.com/decred/dcrd/dcrec/secp256k1/v4"
"github.com/nervosnetwork/ckb-sdk-go/v2/rpc"
"github.com/nervosnetwork/ckb-sdk-go/v2/types"
"github.com/perun-network/perun-libp2p-wire/p2p"
"perun.network/go-perun/channel"
gpchannel "perun.network/go-perun/channel"
"perun.network/go-perun/channel/persistence"
"perun.network/go-perun/client"
gpwallet "perun.network/go-perun/wallet"
"perun.network/go-perun/watcher/local"
"perun.network/go-perun/wire"
"perun.network/go-perun/wire/net/simple"
"perun.network/perun-ckb-backend/backend"
"perun.network/perun-ckb-backend/channel/adjudicator"
"perun.network/perun-ckb-backend/channel/asset"
Expand All @@ -36,21 +36,23 @@ type PaymentClient struct {
wAddr wire.Address
Network types.Network
PerunClient *client.Client

channels chan *PaymentChannel
rpcClient rpc.Client
net *p2p.Net
channels chan *PaymentChannel
rpcClient rpc.Client
}

func NewPaymentClient(
name string,
network types.Network,
deployment backend.Deployment,
bus wire.Bus,
rpcUrl string,
account *wallet.Account,
key secp256k1.PrivateKey,
wallet *wallet.EphemeralWallet,
persistRestorer persistence.PersistRestorer,
wAddr wire.Address,
net *p2p.Net,

) (*PaymentClient, error) {
backendRPCClient, err := rpc.Dial(rpcUrl)
if err != nil {
Expand All @@ -68,8 +70,19 @@ func NewPaymentClient(
if err != nil {
return nil, err
}
wAddr := simple.NewAddress(account.Address().String())
perunClient, err := client.New(wAddr, bus, f, a, wallet, watcher)
/*
wireAcc := p2p.NewRandomAccount(rand.New(rand.NewSource(time.Now().UnixNano())))
net, err := p2p.NewP2PBus(wireAcc)
if err != nil {
panic(errors.Wrap(err, "creating p2p net"))
}
bus := net.Bus
listener := net.Listener
//wAddr := simple.NewAddress(account.Address().String())
wAddr := wireAcc.Address()
*/
perunClient, err := client.New(wAddr, net.Bus, f, a, wallet, watcher)
if err != nil {
return nil, err
}
Expand All @@ -89,6 +102,7 @@ func NewPaymentClient(
PerunClient: perunClient,
channels: make(chan *PaymentChannel, 1),
rpcClient: balanceRPC,
net: net,
}

//go p.PollBalances()
Expand All @@ -105,6 +119,11 @@ func (p *PaymentClient) WireAddress() wire.Address {
return p.wAddr
}

func (p *PaymentClient) PeerID() string {
walletAddr := p.wAddr.(*p2p.Address)
return walletAddr.ID.String()
}

func (p *PaymentClient) GetSudtBalance() *big.Int {
p.balanceMutex.Lock()
defer p.balanceMutex.Unlock()
Expand All @@ -126,12 +145,13 @@ func (p *PaymentClient) GetBalances() string {
}

// OpenChannel opens a new channel with the specified peer and funding.
func (p *PaymentClient) OpenChannel(peer wire.Address, amounts map[gpchannel.Asset]float64) *PaymentChannel {
func (p *PaymentClient) OpenChannel(peer wire.Address, peerID string, amounts map[gpchannel.Asset]float64) *PaymentChannel {
// We define the channel participants. The proposer always has index 0. Here
// we use the on-chain addresses as off-chain addresses, but we could also
// use different ones.
log.Println("OpenChannel called")
participants := []wire.Address{p.WireAddress(), peer}
p.net.Dialer.Register(peer, peerID)

assets := make([]gpchannel.Asset, len(amounts))
i := 0
Expand Down Expand Up @@ -214,22 +234,11 @@ func (p *PaymentClient) Shutdown() {
p.PerunClient.Close()
}

func (c *PaymentClient) Restore() []*PaymentChannel {
func (c *PaymentClient) Restore(peer wire.Address, peerID string) []*PaymentChannel {
var restoredChannels []*client.Channel

//c.net.Dialer.Register(peer, peerID)
//TODO: Remove this hack. Find why asset is not found upon restoring
c.PerunClient.OnNewChannel(func(ch *client.Channel) {
/*
state := ch.State().Clone()
ckbyte := asset.Asset{
IsCKBytes: true,
SUDT: nil,
}
//create a new allocation where asset type is defined
alloc := gpchannel.NewAllocation(2, ckbyte)
ckbBalances := state.Allocation.Balances[0]
alloc.SetAssetBalances(ckbyte, ckbBalances)
*/
restoredChannels = append(restoredChannels, ch)
})

Expand Down
75 changes: 72 additions & 3 deletions payment-channel-ckb/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ go 1.17
require (
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0
github.com/nervosnetwork/ckb-sdk-go/v2 v2.2.0
github.com/stretchr/testify v1.8.4
github.com/perun-network/perun-libp2p-wire v0.0.0-20240514121025-635388735967
github.com/stretchr/testify v1.9.0
perun.network/go-perun v0.10.7-0.20230808153546-74844191e56e
perun.network/perun-ckb-backend v0.0.0-20240514141411-35bdf3afa166
polycry.pt/poly-go v0.0.0-20220301085937-fb9d71b45a37
Expand All @@ -14,24 +15,92 @@ require (
require (
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/Pilatuz/bigz v1.2.1 // indirect
github.com/btcsuite/btcd v0.20.1-beta // indirect
github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect
github.com/btcsuite/btcd/chaincfg/chainhash v1.1.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/davidlazar/go-crypto v0.0.0-20170701192655-dcfb0a7ac018 // indirect
github.com/deckarep/golang-set/v2 v2.6.0 // indirect
github.com/ethereum/go-ethereum v1.13.10 // indirect
github.com/flynn/noise v0.0.0-20180327030543-2492fe189ae6 // indirect
github.com/go-ole/go-ole v1.3.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/google/gopacket v1.1.17 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/websocket v1.5.1 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/holiman/uint256 v1.2.4 // indirect
github.com/huin/goupnp v1.3.0 // indirect
github.com/ipfs/go-cid v0.0.7 // indirect
github.com/ipfs/go-ipfs-util v0.0.2 // indirect
github.com/ipfs/go-log v1.0.4 // indirect
github.com/ipfs/go-log/v2 v2.1.1 // indirect
github.com/jackpal/go-nat-pmp v1.0.2 // indirect
github.com/jbenet/go-temp-err-catcher v0.1.0 // indirect
github.com/jbenet/goprocess v0.1.4 // indirect
github.com/koron/go-ssdp v0.0.0-20191105050749-2e1c40ed0b5d // indirect
github.com/libp2p/go-addr-util v0.0.2 // indirect
github.com/libp2p/go-buffer-pool v0.0.2 // indirect
github.com/libp2p/go-conn-security-multistream v0.2.0 // indirect
github.com/libp2p/go-eventbus v0.2.1 // indirect
github.com/libp2p/go-flow-metrics v0.0.3 // indirect
github.com/libp2p/go-libp2p v0.13.0 // indirect
github.com/libp2p/go-libp2p-autonat v0.4.0 // indirect
github.com/libp2p/go-libp2p-blankhost v0.2.0 // indirect
github.com/libp2p/go-libp2p-circuit v0.4.0 // indirect
github.com/libp2p/go-libp2p-core v0.8.5 // indirect
github.com/libp2p/go-libp2p-discovery v0.5.0 // indirect
github.com/libp2p/go-libp2p-loggables v0.1.0 // indirect
github.com/libp2p/go-libp2p-mplex v0.4.1 // indirect
github.com/libp2p/go-libp2p-nat v0.0.6 // indirect
github.com/libp2p/go-libp2p-noise v0.1.1 // indirect
github.com/libp2p/go-libp2p-peerstore v0.2.6 // indirect
github.com/libp2p/go-libp2p-pnet v0.2.0 // indirect
github.com/libp2p/go-libp2p-swarm v0.4.0 // indirect
github.com/libp2p/go-libp2p-tls v0.1.3 // indirect
github.com/libp2p/go-libp2p-transport-upgrader v0.4.0 // indirect
github.com/libp2p/go-libp2p-yamux v0.5.1 // indirect
github.com/libp2p/go-mplex v0.3.0 // indirect
github.com/libp2p/go-msgio v0.0.6 // indirect
github.com/libp2p/go-nat v0.0.5 // indirect
github.com/libp2p/go-netroute v0.1.3 // indirect
github.com/libp2p/go-openssl v0.0.7 // indirect
github.com/libp2p/go-reuseport v0.0.2 // indirect
github.com/libp2p/go-reuseport-transport v0.0.4 // indirect
github.com/libp2p/go-sockaddr v0.0.2 // indirect
github.com/libp2p/go-stream-muxer-multistream v0.3.0 // indirect
github.com/libp2p/go-tcp-transport v0.2.1 // indirect
github.com/libp2p/go-ws-transport v0.4.0 // indirect
github.com/libp2p/go-yamux/v2 v2.0.0 // indirect
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1 // indirect
github.com/minio/sha256-simd v0.1.1 // indirect
github.com/mr-tron/base58 v1.2.0 // indirect
github.com/multiformats/go-base32 v0.0.3 // indirect
github.com/multiformats/go-base36 v0.1.0 // indirect
github.com/multiformats/go-multiaddr v0.3.3 // indirect
github.com/multiformats/go-multiaddr-dns v0.2.0 // indirect
github.com/multiformats/go-multiaddr-fmt v0.1.0 // indirect
github.com/multiformats/go-multiaddr-net v0.2.0 // indirect
github.com/multiformats/go-multibase v0.0.3 // indirect
github.com/multiformats/go-multihash v0.0.14 // indirect
github.com/multiformats/go-multistream v0.2.0 // indirect
github.com/multiformats/go-varint v0.0.6 // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.10.0 // indirect
github.com/shirou/gopsutil v3.21.11+incompatible // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/stretchr/objx v0.5.1 // indirect
github.com/spacemonkeygo/spacelog v0.0.0-20180420211403-2296661a0572 // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/tklauser/go-sysconf v0.3.13 // indirect
github.com/tklauser/numcpus v0.7.0 // indirect
github.com/whyrusleeping/multiaddr-filter v0.0.0-20160516205228-e903e4adabd7 // indirect
github.com/yusufpapurcu/wmi v1.2.3 // indirect
go.opencensus.io v0.22.4 // indirect
go.uber.org/atomic v1.6.0 // indirect
go.uber.org/multierr v1.5.0 // indirect
go.uber.org/zap v1.15.0 // indirect
golang.org/x/crypto v0.18.0 // indirect
golang.org/x/exp v0.0.0-20240112132812-db7319d0e0e3 // indirect
golang.org/x/mod v0.14.0 // indirect
Expand Down
Loading

0 comments on commit 19d47e6

Please sign in to comment.