Skip to content

Commit

Permalink
examples/go-integration: Add ChannelProposal
Browse files Browse the repository at this point in the history
- Go: Change go-side to accept any incomming channel (and a wallet
  related fix)
- Rust: Uncomment channel proposal and receive of accept message
- Rust: Change order of values in encoded assets (Asset.MarshalBinary),
  see hyperledger-labs/perun-eth-backend#36 for
  why this wasn't correct beforehand. Additionally, this PR is required
  for this example to work correctly, as go-perun will throw away all
  proposed channels because the Asset list isn't
  deserialized/unmarshaled correctly.

Signed-off-by: Jens <jens@perun.network>
  • Loading branch information
DragonDev1906 committed Jan 5, 2023
1 parent 92563c4 commit 763900b
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 17 deletions.
4 changes: 2 additions & 2 deletions examples/go-integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ fn main() {
participant: addr,
};
// Propose new channel and wait for responses
// let mut channel = client.propose_channel(prop);
// bus.recv_envelope();
let mut channel = client.propose_channel(prop);
bus.recv_envelope();
// match bus.rx.recv().unwrap() {
// ParticipantMessage::ProposalAccepted(msg) => {
// channel.participant_accepted(1, msg).unwrap();
Expand Down
43 changes: 36 additions & 7 deletions examples/go-integration/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"crypto/rand"
"math/big"
"os"
"os/signal"
Expand All @@ -16,10 +17,12 @@ import (
"github.com/ethereum/go-ethereum/params"
ethchannel "github.com/perun-network/perun-eth-backend/channel"
phd "github.com/perun-network/perun-eth-backend/wallet/hd"
"github.com/sirupsen/logrus"
"perun.network/go-perun/channel"
"perun.network/go-perun/client"
perunlogrus "perun.network/go-perun/log/logrus"
"perun.network/go-perun/wallet"
"perun.network/go-perun/watcher/local"
"perun.network/go-perun/wire"
wirenet "perun.network/go-perun/wire/net"
"perun.network/go-perun/wire/net/simple"
"perun.network/go-perun/wire/protobuf"
Expand All @@ -44,6 +47,8 @@ func ToWei(value int64, denomination string) *big.Int {
}

func main() {
perunlogrus.Set(logrus.TraceLevel, &logrus.TextFormatter{})

w := NewSimpleWallet()
account := w.GenerateNewAccount()
deployer_account := w.GenerateNewAccount()
Expand Down Expand Up @@ -91,9 +96,9 @@ func main() {
account.Address,
account,
)
perunID := wire.NewAddress()
perunID := simple.NewAddress("Bob")
bus := wirenet.NewBus(
simple.NewAccount(simple.NewAddress("Bob")),
simple.NewAccount(perunID),
simple.NewTCPDialer(time.Minute),
protobuf.Serializer(),
)
Expand All @@ -110,8 +115,13 @@ func main() {
if err != nil {
panic(err)
}

var proposalHandler client.ProposalHandler = ProposalHandler{}
bob_account, err := wallet.NewAccount()
if err != nil {
panic(err)
}
var proposalHandler client.ProposalHandler = ProposalHandler{
addr: bob_account.Address(),
}
var updateHandler client.UpdateHandler = UpdateHandler{}

listener, err := simple.NewTCPListener("127.0.0.1:1337")
Expand All @@ -133,11 +143,30 @@ func main() {
println("Done")
}

type ProposalHandler struct{}
type ProposalHandler struct {
addr wallet.Address
}

// HandleProposal implements client.ProposalHandler
func (ProposalHandler) HandleProposal(proposal client.ChannelProposal, res *client.ProposalResponder) {
func (ph ProposalHandler) HandleProposal(proposal client.ChannelProposal, res *client.ProposalResponder) {
println("HandleProposal(): ", proposal, res)

var nonce_share [32]byte
_, err := rand.Read(nonce_share[:])
if err != nil {
panic(err)
}

_, err = res.Accept(context.Background(), &client.LedgerChannelProposalAccMsg{
BaseChannelProposalAcc: client.BaseChannelProposalAcc{
ProposalID: proposal.Base().ProposalID,
NonceShare: nonce_share,
},
Participant: ph.addr,
})
if err != nil {
panic(err)
}
}

type UpdateHandler struct{}
Expand Down
9 changes: 7 additions & 2 deletions examples/go-integration/simple_wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func NewSimpleWallet() *SimpleWallet {
type SimpleWallet struct {
accounts []accounts.Account
keys map[common.Address]*ecdsa.PrivateKey
derived map[string]accounts.Account
}

var _ accounts.Wallet = (*SimpleWallet)(nil)
Expand Down Expand Up @@ -60,8 +61,12 @@ func (w *SimpleWallet) Contains(account accounts.Account) bool {
}

// Derive implements accounts.Wallet
func (*SimpleWallet) Derive(path accounts.DerivationPath, pin bool) (accounts.Account, error) {
panic("unimplemented")
func (w *SimpleWallet) Derive(path accounts.DerivationPath, pin bool) (accounts.Account, error) {
acc, ok := w.derived[path.String()]
if !ok {
acc = w.GenerateNewAccount()
}
return acc, nil
}

// Open implements accounts.Wallet
Expand Down
12 changes: 7 additions & 5 deletions src/channel/fixed_size_payment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,20 +192,22 @@ impl<const A: usize, const P: usize> From<Allocation<A, P>> for perunwire::Alloc
assets: value
.assets
.map(|a| {
// go-perun currently uses `encoding/binary` in go and
// manually adds the length of each field.
let mut b = vec![];
b.append(&mut 20u16.to_le_bytes().to_vec()); // Length of asset holder (address)
b.append(&mut a.holder.0.to_vec());

// go-perun uses less bytes, as it strips away some leading
// zeroes, which this implementation does not (for
// simplicity). However this should still be understandable
// by go-perun.
b.append(&mut 32u16.to_le_bytes().to_vec());
b.extend_from_slice(&32u16.to_le_bytes());
let mut buf = [0u8; 32];
a.chain_id.to_big_endian(&mut buf);
b.extend_from_slice(&buf);

// go-perun currently uses `encoding/binary` in go and
// manually adds the length of each field.
b.extend_from_slice(&20u16.to_le_bytes()); // Length of asset holder (address)
b.extend_from_slice(&a.holder.0);

b
})
.to_vec(),
Expand Down
2 changes: 1 addition & 1 deletion src/channel/proposal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ impl From<LedgerChannelProposal> for perunwire::LedgerChannelProposalMsg {
funding_agreement: Some(value.funding_agreement.into()),
}),
participant: value.participant.0.to_vec(),
peers: vec![], // TODO: This is not empty!
peers: vec!["Alice".as_bytes().to_vec(), "Bob".as_bytes().to_vec()], // TODO: Use real data
}
}
}
Expand Down

0 comments on commit 763900b

Please sign in to comment.