Skip to content

Commit

Permalink
feat: add restore function to paymentClient && chore: cleanup
Browse files Browse the repository at this point in the history
- add Restore method to PaymentClient in client.go
- update .gitignore and remove unecessary .idea files
  • Loading branch information
ravi0131 committed Jul 9, 2024
1 parent 19d47e6 commit a889f41
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 39 deletions.
1 change: 1 addition & 0 deletions payment-channel-ckb/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ devnet/ckb-miner.toml
devnet/ckb.toml
devnet/default.db-options
devnet/system_scripts
.vscode
8 changes: 0 additions & 8 deletions payment-channel-ckb/.idea/.gitignore

This file was deleted.

8 changes: 0 additions & 8 deletions payment-channel-ckb/.idea/modules.xml

This file was deleted.

9 changes: 0 additions & 9 deletions payment-channel-ckb/.idea/payment-channel-ckb.iml

This file was deleted.

6 changes: 0 additions & 6 deletions payment-channel-ckb/.idea/vcs.xml

This file was deleted.

38 changes: 30 additions & 8 deletions payment-channel/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@ package client
import (
"context"
"fmt"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/common"
"github.com/pkg/errors"
"math/big"

ethchannel "perun.network/go-perun/backend/ethereum/channel"
ethwallet "perun.network/go-perun/backend/ethereum/wallet"
swallet "perun.network/go-perun/backend/ethereum/wallet/simple"
"perun.network/go-perun/channel/persistence"
"perun.network/go-perun/watcher/local"

"perun.network/go-perun/channel"
"perun.network/go-perun/client"
"perun.network/go-perun/wallet"
"perun.network/go-perun/watcher/local"
"perun.network/go-perun/wire"

"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/common"
"github.com/pkg/errors"
)

const (
Expand All @@ -54,6 +54,7 @@ func SetupPaymentClient(
chainID uint64, // chainID is the identifier of the blockchain.
adjudicator common.Address, // adjudicator is the address of the adjudicator.
asset ethwallet.Address, // asset is the address of the asset holder for our payment channels.
pr persistence.PersistRestorer,
) (*PaymentClient, error) {
// Create Ethereum client and contract backend.
cb, err := CreateContractBackend(nodeURL, chainID, w)
Expand All @@ -74,11 +75,11 @@ func SetupPaymentClient(
// Setup funder.
funder := ethchannel.NewFunder(cb)
dep := ethchannel.NewETHDepositor()
ethAcc := accounts.Account{Address: acc}
ethAcc := accounts.Account{Address: acc} // Account to be used for funding transactions
funder.RegisterAsset(asset, dep, ethAcc)

// Setup adjudicator.
adj := ethchannel.NewAdjudicator(cb, adjudicator, acc, ethAcc)
adj := ethchannel.NewAdjudicator(cb, adjudicator, acc, ethAcc) // acc is the address of the account that will receive the payout

// Setup dispute watcher.
watcher, err := local.NewWatcher(adj)
Expand All @@ -89,6 +90,7 @@ func SetupPaymentClient(
// Setup Perun client.
waddr := ethwallet.AsWalletAddr(acc)
perunClient, err := client.New(waddr, bus, funder, adj, w, watcher)
perunClient.EnablePersistence(pr)
if err != nil {
return nil, errors.WithMessage(err, "creating client")
}
Expand Down Expand Up @@ -162,3 +164,23 @@ func (c *PaymentClient) AcceptedChannel() *PaymentChannel {
func (c *PaymentClient) Shutdown() {
c.perunClient.Close()
}

func (c *PaymentClient) Restore() []*PaymentChannel {
var restoredChannels []*client.Channel

c.perunClient.OnNewChannel(func(ch *client.Channel) {
restoredChannels = append(restoredChannels, ch)
})

err := c.perunClient.Restore(context.TODO())
if err != nil {
fmt.Println("Error restoring channels")
}

paymentChannels := make([]*PaymentChannel, len(restoredChannels))
for i, ch := range restoredChannels {
paymentChannels[i] = newPaymentChannel(ch, c.currency)
}

return paymentChannels
}

0 comments on commit a889f41

Please sign in to comment.