Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,25 @@ jobs:
go run .
docker stop ganache

- name: Multiledger Payment Channel
working-directory: multiledger-channel
env:
KEY_DEPLOYER: "0x79ea8f62d97bc0591a4224c1725fca6b00de5b2cea286fe2e0bb35c5e76be46e"
KEY_ALICE: "0x1af2e950272dd403de7a5760d41c6e44d92b6d02797e51810795ff03cc2cda4f"
KEY_BOB: "0xf63d7d8e930bccd74e93cf5662fde2c28fd8be95edb70c73f1bdd863d07f412e"
BALANCE: "10000000000000000000"
CHAIN_A_PORT: "8545"
CHAIN_B_PORT: "8546"
CHAIN_A_ID: "1337"
CHAIN_B_ID: "1338"
run: |
docker run --rm --name ganache-a --detach --publish $CHAIN_A_PORT:$CHAIN_A_PORT ${{ env.ganache-image }} --port $CHAIN_A_PORT --chain.chainId $CHAIN_A_ID --wallet.accounts $KEY_DEPLOYER,$BALANCE $KEY_ALICE,$BALANCE $KEY_BOB,$BALANCE --b=1
docker run --rm --name ganache-b --detach --publish $CHAIN_B_PORT:$CHAIN_B_PORT ${{ env.ganache-image }} --port $CHAIN_B_PORT --chain.chainId $CHAIN_B_ID --wallet.accounts $KEY_DEPLOYER,$BALANCE $KEY_ALICE,$BALANCE $KEY_BOB,$BALANCE --b=1
sleep 5
go run .
docker stop ganache-a
docker stop ganache-b

- name: Payment Channel XLM
working-directory: payment-channel-xlm
run: |
Expand Down Expand Up @@ -106,7 +125,7 @@ jobs:
./stopdfx.sh || true # stop any running dfx
./startdeploy.sh

sleep 15
sleep 10
go run ./

./stopdfx.sh || true
Expand Down
4 changes: 2 additions & 2 deletions multiledger-channel/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ KEY_ALICE=0x1af2e950272dd403de7a5760d41c6e44d92b6d02797e51810795ff03cc2cda4f
KEY_BOB=0xf63d7d8e930bccd74e93cf5662fde2c28fd8be95edb70c73f1bdd863d07f412e
BALANCE=100000000000000000000000

ganache --h 127.0.0.1 --port 8545 --chain.chainId 1337 --wallet.accounts $KEY_DEPLOYER,$BALANCE $KEY_ALICE,$BALANCE $KEY_BOB,$BALANCE
ganache --h 127.0.0.1 --port 8545 --chain.chainId 1337 --wallet.accounts $KEY_DEPLOYER,$BALANCE $KEY_ALICE,$BALANCE $KEY_BOB,$BALANCE --b=5
```
Open up a second terminal and start the second chain:
```sh
Expand All @@ -17,7 +17,7 @@ KEY_ALICE=0x1af2e950272dd403de7a5760d41c6e44d92b6d02797e51810795ff03cc2cda4f
KEY_BOB=0xf63d7d8e930bccd74e93cf5662fde2c28fd8be95edb70c73f1bdd863d07f412e
BALANCE=100000000000000000000000

ganache --h 127.0.0.1 --port 8546 --chain.chainId 1338 --wallet.accounts $KEY_DEPLOYER,$BALANCE $KEY_ALICE,$BALANCE $KEY_BOB,$BALANCE
ganache --h 127.0.0.1 --port 8546 --chain.chainId 1338 --wallet.accounts $KEY_DEPLOYER,$BALANCE $KEY_ALICE,$BALANCE $KEY_BOB,$BALANCE --b=5
```


Expand Down
41 changes: 25 additions & 16 deletions multiledger-channel/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
ethchannel "github.com/perun-network/perun-eth-backend/channel"
ethwallet "github.com/perun-network/perun-eth-backend/wallet"
swallet "github.com/perun-network/perun-eth-backend/wallet/simple"
ethwire "github.com/perun-network/perun-eth-backend/wire"

"perun.network/go-perun/channel"
"perun.network/go-perun/channel/multi"
Expand Down Expand Up @@ -50,10 +49,11 @@ type ChainConfig struct {

// SwapClient is a channel client for swaps.
type SwapClient struct {
perunClient *client.Client // The core Perun client.
account wallet.Address // The account we use for on-chain and off-chain transactions.
currencies [2]channel.Asset // The currencies of the different chains we support.
channels chan *SwapChannel // Accepted payment channels.
perunClient *client.Client // The core Perun client.
account map[wallet.BackendID]wallet.Address // The account we use for on-chain and off-chain transactions.
currencies [2]channel.Asset // The currencies of the different chains we support.
channels chan *SwapChannel // Accepted payment channels.
waddresss map[wallet.BackendID]wire.Address // The wire address of the client, used for off-chain communication.
}

// SetupSwapClient creates a new swap client.
Expand All @@ -62,6 +62,7 @@ func SetupSwapClient(
w *swallet.Wallet, // w is the wallet used for signing transactions.
acc common.Address, // acc is the address of the account to be used for signing transactions.
chains [2]ChainConfig, // chains represent the two chains the client should be able to use.
waddress wire.Address, // waddress is the wire address of the client, used for off-chain communication.
) (*SwapClient, error) {
// The multi-funder and multi-adjudicator will be registered with a funder /
// adjudicators for each chain.
Expand Down Expand Up @@ -93,19 +94,23 @@ func SetupSwapClient(
// Setup funder.
funder := ethchannel.NewFunder(cb)
// Register the asset on the funder.
dep := ethchannel.NewERC20Depositor(chain.Token)
dep := ethchannel.NewERC20Depositor(chain.Token, 100000)
ethAcc := accounts.Account{Address: acc}
funder.RegisterAsset(*assets[i].(*ethchannel.Asset), dep, ethAcc)
// We have to register the asset of the other chain too, but use a
// NoOpDepositor there since this funder can ignore it.
funder.RegisterAsset(*assets[1-i].(*ethchannel.Asset), ethchannel.NewNoOpDepositor(), ethAcc)

// assetID is the ID of the asset on the chain, which is used to
assetID := ethchannel.MakeLedgerBackendID(chain.ChainID.Int)

// Register the funder on the multi-funder.
multiFunder.RegisterFunder(chain.ChainID, funder)
multiFunder.RegisterFunder(assetID, funder)

// Setup adjudicator.
adj := ethchannel.NewAdjudicator(cb, chain.Adjudicator, acc, ethAcc)
adj := ethchannel.NewAdjudicator(cb, chain.Adjudicator, acc, ethAcc, 10000000)
// Register the adjudicator on the multi-adjudicator.
multiAdjudicator.RegisterAdjudicator(chain.ChainID, adj)
multiAdjudicator.RegisterAdjudicator(assetID, adj)
}

// Setup dispute watcher.
Expand All @@ -116,34 +121,38 @@ func SetupSwapClient(

// Setup Perun client.
walletAddr := ethwallet.AsWalletAddr(acc)
wireAddr := &ethwire.Address{Address: walletAddr}
perunClient, err := client.New(wireAddr, bus, multiFunder, multiAdjudicator, w, watcher)
addresses := map[wallet.BackendID]wire.Address{1: waddress}
ethWallet := map[wallet.BackendID]wallet.Wallet{1: w}
perunClient, err := client.New(addresses, bus, multiFunder, multiAdjudicator, ethWallet, watcher)
if err != nil {
return nil, errors.WithMessage(err, "creating client")
}

// Setup Accounts
account := map[wallet.BackendID]wallet.Address{1: walletAddr}

// Create client and start request handler.
c := &SwapClient{
perunClient: perunClient,
account: walletAddr,
account: account,
currencies: assets,
channels: make(chan *SwapChannel, 1),
waddresss: addresses,
}
go perunClient.Handle(c, c)

return c, nil
}

// OpenChannel opens a new channel with the specified peer and funding.
func (c *SwapClient) OpenChannel(peer wire.Address, balances channel.Balances) *SwapChannel {
func (c *SwapClient) OpenChannel(peer map[wallet.BackendID]wire.Address, balances channel.Balances) *SwapChannel {
// We define the channel participants. The proposer has always index 0. Here
// we use the on-chain addresses as off-chain addresses, but we could also
// use different ones.
wireAddr := &ethwire.Address{Address: c.account.(*ethwallet.Address)}
participants := []wire.Address{wireAddr, peer}
participants := []map[wallet.BackendID]wire.Address{c.waddresss, peer}

// We create an initial allocation which defines the starting balances.
initAlloc := channel.NewAllocation(2, c.currencies[0], c.currencies[1])
initAlloc := channel.NewAllocation(2, []wallet.BackendID{1, 1}, c.currencies[0], c.currencies[1])
initAlloc.Balances = balances

// Prepare the channel proposal by defining the channel parameters.
Expand Down
10 changes: 6 additions & 4 deletions multiledger-channel/client/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ import (
ethchannel "github.com/perun-network/perun-eth-backend/channel"
ethwallet "github.com/perun-network/perun-eth-backend/wallet"
swallet "github.com/perun-network/perun-eth-backend/wallet/simple"
"github.com/perun-network/perun-eth-backend/wire"

"perun.network/go-perun/wallet"
"perun.network/go-perun/wire"
)

// CreateContractBackend creates a new contract backend.
Expand All @@ -45,10 +47,10 @@ func CreateContractBackend(

// WalletAddress returns the wallet address of the client.
func (c *SwapClient) WalletAddress() common.Address {
return common.Address(*c.account.(*ethwallet.Address))
return common.Address(*c.account[1].(*ethwallet.Address))
}

// WireAddress returns the wire address of the client.
func (c *SwapClient) WireAddress() *wire.Address {
return &wire.Address{Address: ethwallet.AsWalletAddr(c.WalletAddress())}
func (c *SwapClient) WireAddress() map[wallet.BackendID]wire.Address {
return c.waddresss
}
146 changes: 126 additions & 20 deletions multiledger-channel/go.mod
Original file line number Diff line number Diff line change
@@ -1,34 +1,140 @@
module perun.network/perun-examples/multiledger-channel

go 1.20
go 1.23.0

toolchain go1.23.4

require (
github.com/ethereum/go-ethereum v1.10.12
github.com/perun-network/perun-eth-backend v0.1.0
github.com/ethereum/go-ethereum v1.15.11
github.com/perun-network/perun-eth-backend v0.6.0
github.com/perun-network/perun-libp2p-wire v1.0.1
github.com/pkg/errors v0.9.1
perun.network/go-perun v0.10.6
perun.network/go-perun v0.13.1-0.20250519120217-973104e7b38a
)

require (
github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6 // indirect
github.com/btcsuite/btcd v0.21.0-beta // indirect
github.com/Microsoft/go-winio v0.6.2 // indirect
github.com/StackExchange/wmi v1.2.1 // indirect
github.com/benbjohnson/clock v1.3.5 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bits-and-blooms/bitset v1.20.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/consensys/bavard v0.1.27 // indirect
github.com/consensys/gnark-crypto v0.16.0 // indirect
github.com/containerd/cgroups v1.1.0 // indirect
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
github.com/crate-crypto/go-eth-kzg v1.3.0 // indirect
github.com/crate-crypto/go-ipa v0.0.0-20240724233137-53bbb0ceb27a // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/deckarep/golang-set v0.0.0-20180603214616-504e848d77ea // indirect
github.com/go-ole/go-ole v1.2.1 // indirect
github.com/go-stack/stack v1.8.0 // indirect
github.com/davidlazar/go-crypto v0.0.0-20200604182044-b73af7476f6c // indirect
github.com/deckarep/golang-set/v2 v2.6.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.4.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/elastic/gosigar v0.14.3 // indirect
github.com/ethereum/c-kzg-4844/v2 v2.1.0 // indirect
github.com/ethereum/go-verkle v0.2.2 // indirect
github.com/flynn/noise v1.1.0 // indirect
github.com/francoispqt/gojay v1.2.13 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-ole/go-ole v1.3.0 // indirect
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
github.com/godbus/dbus/v5 v5.1.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/google/gopacket v1.1.19 // indirect
github.com/google/pprof v0.0.0-20250208200701-d0013a598941 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/gorilla/websocket v1.5.3 // indirect
github.com/holiman/uint256 v1.3.2 // indirect
github.com/huin/goupnp v1.3.0 // indirect
github.com/ipfs/go-cid v0.5.0 // indirect
github.com/ipfs/go-log/v2 v2.5.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/klauspost/compress v1.18.0 // indirect
github.com/klauspost/cpuid/v2 v2.2.10 // indirect
github.com/koron/go-ssdp v0.0.5 // indirect
github.com/libp2p/go-buffer-pool v0.1.0 // indirect
github.com/libp2p/go-flow-metrics v0.2.0 // indirect
github.com/libp2p/go-libp2p v0.41.0 // indirect
github.com/libp2p/go-libp2p-asn-util v0.4.1 // indirect
github.com/libp2p/go-msgio v0.3.0 // indirect
github.com/libp2p/go-netroute v0.2.2 // indirect
github.com/libp2p/go-reuseport v0.4.0 // indirect
github.com/libp2p/go-yamux/v5 v5.0.0 // indirect
github.com/marten-seemann/tcp v0.0.0-20210406111302-dfbc87cc63fd // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/miekg/dns v1.1.63 // indirect
github.com/mikioh/tcpinfo v0.0.0-20190314235526-30a79bb1804b // indirect
github.com/mikioh/tcpopt v0.0.0-20190314235656-172688c1accc // indirect
github.com/minio/sha256-simd v1.0.1 // indirect
github.com/mmcloughlin/addchain v0.4.0 // indirect
github.com/mr-tron/base58 v1.2.0 // indirect
github.com/multiformats/go-base32 v0.1.0 // indirect
github.com/multiformats/go-base36 v0.2.0 // indirect
github.com/multiformats/go-multiaddr v0.15.0 // indirect
github.com/multiformats/go-multiaddr-dns v0.4.1 // indirect
github.com/multiformats/go-multiaddr-fmt v0.1.0 // indirect
github.com/multiformats/go-multibase v0.2.0 // indirect
github.com/multiformats/go-multicodec v0.9.0 // indirect
github.com/multiformats/go-multihash v0.2.3 // indirect
github.com/multiformats/go-multistream v0.6.0 // indirect
github.com/multiformats/go-varint v0.0.7 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/onsi/ginkgo/v2 v2.22.2 // indirect
github.com/opencontainers/runtime-spec v1.2.0 // indirect
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 // indirect
github.com/pion/datachannel v1.5.10 // indirect
github.com/pion/dtls/v2 v2.2.12 // indirect
github.com/pion/dtls/v3 v3.0.4 // indirect
github.com/pion/ice/v4 v4.0.6 // indirect
github.com/pion/interceptor v0.1.37 // indirect
github.com/pion/logging v0.2.3 // indirect
github.com/pion/mdns/v2 v2.0.7 // indirect
github.com/pion/randutil v0.1.0 // indirect
github.com/pion/rtcp v1.2.15 // indirect
github.com/pion/rtp v1.8.11 // indirect
github.com/pion/sctp v1.8.36 // indirect
github.com/pion/sdp/v3 v3.0.10 // indirect
github.com/pion/srtp/v3 v3.0.4 // indirect
github.com/pion/stun v0.6.1 // indirect
github.com/pion/stun/v3 v3.0.0 // indirect
github.com/pion/transport/v2 v2.2.10 // indirect
github.com/pion/transport/v3 v3.0.7 // indirect
github.com/pion/turn/v4 v4.0.0 // indirect
github.com/pion/webrtc/v4 v4.0.10 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rjeczalik/notify v0.9.1 // indirect
github.com/prometheus/client_golang v1.21.0 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.62.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/quic-go/qpack v0.5.1 // indirect
github.com/quic-go/quic-go v0.50.0 // indirect
github.com/quic-go/webtransport-go v0.8.1-0.20241018022711-4ac2c9250e66 // indirect
github.com/raulk/go-watchdog v1.3.0 // indirect
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/stretchr/testify v1.7.0 // indirect
github.com/tklauser/go-sysconf v0.3.5 // indirect
github.com/tklauser/numcpus v0.2.2 // indirect
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2 // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
golang.org/x/sys v0.0.0-20210816183151-1e6c022a8912 // indirect
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect
github.com/stretchr/testify v1.10.0 // indirect
github.com/supranational/blst v0.3.14 // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
github.com/wlynxg/anet v0.0.5 // indirect
go.uber.org/dig v1.18.0 // indirect
go.uber.org/fx v1.23.0 // indirect
go.uber.org/mock v0.5.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.org/x/crypto v0.35.0 // indirect
golang.org/x/exp v0.0.0-20250218142911-aa4b98e5adaa // indirect
golang.org/x/mod v0.23.0 // indirect
golang.org/x/net v0.36.0 // indirect
golang.org/x/sync v0.11.0 // indirect
golang.org/x/sys v0.30.0 // indirect
golang.org/x/text v0.22.0 // indirect
golang.org/x/tools v0.30.0 // indirect
google.golang.org/protobuf v1.36.5 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
lukechampine.com/blake3 v1.4.0 // indirect
polycry.pt/poly-go v0.0.0-20220301085937-fb9d71b45a37 // indirect
rsc.io/tmplfunc v0.0.3 // indirect
)
Loading
Loading