Skip to content

Commit

Permalink
add: ckb payment example
Browse files Browse the repository at this point in the history
Signed-off-by: Hendrik Amler <hendrik@perun.network>
  • Loading branch information
tinnendo committed Oct 25, 2024
1 parent c60eb98 commit 4057575
Show file tree
Hide file tree
Showing 234 changed files with 48,636 additions and 196 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,39 @@ jobs:
go run .
docker stop ganache
- name: Payment Channel CKB
working-directory: payment-channel-ckb
run: |
sudo apt-get update
sudo apt-get install -y jq sed gawk tmux tmuxp expect make
curl -LO https://github.com/nervosnetwork/ckb/releases/download/v0.109.0/ckb_v0.109.0_x86_64-unknown-linux-gnu.tar.gz
tar -xzf ckb_v0.109.0_x86_64-unknown-linux-gnu.tar.gz
sudo cp ckb_v0.109.0_x86_64-unknown-linux-gnu/ckb /usr/local/bin/
curl -LO https://github.com/nervosnetwork/ckb-cli/releases/download/v1.4.0/ckb-cli_v1.4.0_x86_64-unknown-linux-gnu.tar.gz
tar -xzf ckb-cli_v1.4.0_x86_64-unknown-linux-gnu.tar.gz
sudo cp ckb-cli_v1.4.0_x86_64-unknown-linux-gnu/ckb-cli /usr/local/bin/
curl -LO https://github.com/nervosnetwork/capsule/releases/download/v0.9.2/capsule_v0.9.2_x86_64-linux.tar.gz
tar -xzf capsule_v0.9.2_x86_64-linux.tar.gz
sudo cp capsule_v0.9.2_x86_64-linux/capsule /usr/local/bin/
cd ./devnet
chmod +x setup-devnet.sh print_accounts.sh deploy_contracts.sh sudt_helper.sh
./setup-devnet.sh
ckb run > /dev/null 2>&1 &
sleep 3
./print_accounts.sh
sleep 6
expect fund_accounts.expect
sleep 10
./deploy_contracts.sh
sleep 15
./sudt_helper.sh fund
sleep 10
./sudt_helper.sh balances
sleep 30
cd ..
go run main.go
- name: Payment Channel ETH
working-directory: payment-channel
env:
Expand Down
Binary file added payment-channel-ckb/__debug_bin2729964150
Binary file not shown.
29 changes: 18 additions & 11 deletions payment-channel-ckb/client/balances.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
// Copyright 2024 PolyCrypt GmbH
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package client

import (
Expand Down Expand Up @@ -29,7 +43,7 @@ func sudtBalanceExtractor(cell *indexer.LiveCell) *big.Int {
}

func (p *PaymentClient) PollBalances() {
defer log.Println("PollBalances: stopped")
//defer log.Println("PollBalances: stopped")
pollingInterval := time.Second
searchKey := &indexer.SearchKey{
Script: address.AsParticipant(p.Account.Address()).PaymentScript,
Expand All @@ -38,7 +52,7 @@ func (p *PaymentClient) PollBalances() {
Filter: nil,
WithData: true,
}
log.Println("PollBalances")
//log.Println("PollBalances")
updateBalance := func() {
ctx, _ := context.WithTimeout(context.Background(), pollingInterval)

Expand Down Expand Up @@ -70,18 +84,11 @@ func (p *PaymentClient) PollBalances() {
}
}
updateBalance()
//return "CKbytes: " + p.balance.String() + ", SUDT: " + p.sudtBalance.String()
/*
// Poll the balance every 5 seconds.
for {
updateBalance()
time.Sleep(pollingInterval)
}
*/

}

func FormatBalance(ckbBal, sudtBal *big.Int) string {
log.Printf("balances: ckb = %s || sudt = %s", ckbBal.String(), sudtBal.String())
//log.Printf("balances: ckb = %s || sudt = %s", ckbBal.String(), sudtBal.String())
balCKByte, _ := ShannonToCKByte(ckbBal).Float64()
return fmt.Sprintf("[green]%s\t[yellow]%s[white]",
strconv.FormatFloat(balCKByte, 'f', 2, 64)+" CKByte",
Expand Down
37 changes: 17 additions & 20 deletions payment-channel-ckb/client/channel.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
// Copyright 2024 PolyCrypt GmbH
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package client

import (
"context"
"fmt"
"math/big"

"perun.network/go-perun/channel"
Expand Down Expand Up @@ -34,25 +47,11 @@ func (c PaymentChannel) SendPayment(amounts map[channel.Asset]float64) {
peer := 1 - actor
//fmt.Println("Send payment handler called")
for a, amount := range amounts {
fmt.Println(a)
fmt.Println(amount)
//fmt.Println(a)
//fmt.Println(amount)
if amount < 0 {
continue
}
/*
switch a := a.(type) {
case *asset.Asset:
if a.IsCKBytes {
fmt.Println("inside condition isCKBytes")
shannonAmount := CKByteToShannon(big.NewFloat(amount))
state.Allocation.TransferBalance(actor, peer, a, shannonAmount)
} else {
fmt.Println("inside if conditional !isCKBytes")
intAmount := new(big.Int).SetUint64(uint64(amount))
state.Allocation.TransferBalance(actor, peer, a, intAmount)
}
}
*/

shannonAmount := CKByteToShannon(big.NewFloat(amount))
state.Allocation.TransferBalance(actor, peer, a, shannonAmount)
Expand All @@ -63,9 +62,7 @@ func (c PaymentChannel) SendPayment(amounts map[channel.Asset]float64) {
if err != nil {
panic(err)
}
if err != nil {
panic(err) // We panic on error to keep the code simple.
}

}

// Settle settles the payment channel and withdraws the funds.
Expand Down
46 changes: 20 additions & 26 deletions payment-channel-ckb/client/client.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
// Copyright 2024 PolyCrypt GmbH
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package client

import (
"context"
"fmt"
"log"
"math/big"

"github.com/decred/dcrd/dcrec/secp256k1/v4"
Expand Down Expand Up @@ -70,18 +83,7 @@ func NewPaymentClient(
if err != nil {
return nil, err
}
/*
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 Down Expand Up @@ -130,14 +132,6 @@ func (p *PaymentClient) GetSudtBalance() *big.Int {
return new(big.Int).Set(p.sudtBalance)
}

// TODO: Remove as probably not required
/*
func (p *PaymentClient) NotifyAllBalance(ckbBal int64) string {
str := FormatBalance(new(big.Int).SetInt64(ckbBal), p.GetSudtBalance())
return str
}
*/

// GetBalances retrieves the current balances of the client.
func (p *PaymentClient) GetBalances() string {
p.PollBalances()
Expand All @@ -149,7 +143,7 @@ func (p *PaymentClient) OpenChannel(peer wire.Address, peerID string, amounts ma
// 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")
//log.Println("OpenChannel called")
participants := []wire.Address{p.WireAddress(), peer}
p.net.Dialer.Register(peer, peerID)

Expand All @@ -162,7 +156,7 @@ func (p *PaymentClient) OpenChannel(peer wire.Address, peerID string, amounts ma

// We create an initial allocation which defines the starting balances.
initAlloc := gpchannel.NewAllocation(2, assets...)
log.Println(initAlloc.Assets)
//log.Println(initAlloc.Assets)
for a, amount := range amounts {
switch a := a.(type) {
case *asset.Asset:
Expand All @@ -183,7 +177,7 @@ func (p *PaymentClient) OpenChannel(peer wire.Address, peerID string, amounts ma
}

}
log.Println("Created Allocation")
//log.Println("Created Allocation")

// Prepare the channel proposal by defining the channel parameters.
challengeDuration := uint64(10) // On-chain challenge duration in seconds.
Expand All @@ -197,20 +191,20 @@ func (p *PaymentClient) OpenChannel(peer wire.Address, peerID string, amounts ma
panic(err)
}

log.Println("Created Proposal")
//log.Println("Created Proposal")

// Send the proposal.
ch, err := p.PerunClient.ProposeChannel(context.TODO(), proposal)
if err != nil {
panic(err)
}

log.Println("Sent Channel")
//log.Println("Sent Channel")

// Start the on-chain event watcher. It automatically handles disputes.
p.startWatching(ch)

log.Println("Started Watching")
//log.Println("Started Watching")

//p.Channel = newPaymentChannel(ch, assets)
return newPaymentChannel(ch, assets)
Expand Down
14 changes: 14 additions & 0 deletions payment-channel-ckb/client/handle.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
// Copyright 2024 PolyCrypt GmbH
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package client

import (
Expand Down
14 changes: 14 additions & 0 deletions payment-channel-ckb/client/util.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
// Copyright 2024 PolyCrypt GmbH
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package client

import (
Expand Down
14 changes: 14 additions & 0 deletions payment-channel-ckb/deployment/deployment.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
// Copyright 2024 PolyCrypt GmbH
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package deployment

import (
Expand Down
17 changes: 16 additions & 1 deletion payment-channel-ckb/deployment/keys.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
// Copyright 2024 PolyCrypt GmbH
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package deployment

import (
"encoding/hex"
"fmt"
"github.com/decred/dcrd/dcrec/secp256k1/v4"
"io"
"os"
"strings"

"github.com/decred/dcrd/dcrec/secp256k1/v4"
)

func GetKey(path string) (*secp256k1.PrivateKey, error) {
Expand Down
14 changes: 14 additions & 0 deletions payment-channel-ckb/deployment/system_scripts.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
// Copyright 2024 PolyCrypt GmbH
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package deployment

import (
Expand Down
16 changes: 15 additions & 1 deletion payment-channel-ckb/deployment/system_scripts_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
// Copyright 2024 PolyCrypt GmbH
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package deployment_test

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/require"
"perun.network/perun-ckb-demo/deployment"
"perun.network/perun-examples/payment-channel-ckb/deployment"
)

func TestSystemScripts(t *testing.T) {
Expand Down
Loading

0 comments on commit 4057575

Please sign in to comment.