From 763900bc9beb05248f6a7b05761ef8ad33d56522 Mon Sep 17 00:00:00 2001 From: Jens Date: Thu, 5 Jan 2023 16:05:39 +0100 Subject: [PATCH] examples/go-integration: Add ChannelProposal - 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 https://github.com/hyperledger-labs/perun-eth-backend/pull/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 --- examples/go-integration.rs | 4 +-- examples/go-integration/main.go | 43 ++++++++++++++++++++---- examples/go-integration/simple_wallet.go | 9 +++-- src/channel/fixed_size_payment.rs | 12 ++++--- src/channel/proposal.rs | 2 +- 5 files changed, 53 insertions(+), 17 deletions(-) diff --git a/examples/go-integration.rs b/examples/go-integration.rs index e9fbe42..cd3eba0 100644 --- a/examples/go-integration.rs +++ b/examples/go-integration.rs @@ -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(); diff --git a/examples/go-integration/main.go b/examples/go-integration/main.go index de44643..822cac2 100644 --- a/examples/go-integration/main.go +++ b/examples/go-integration/main.go @@ -2,6 +2,7 @@ package main import ( "context" + "crypto/rand" "math/big" "os" "os/signal" @@ -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" @@ -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() @@ -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(), ) @@ -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") @@ -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{} diff --git a/examples/go-integration/simple_wallet.go b/examples/go-integration/simple_wallet.go index 078006c..49d33df 100644 --- a/examples/go-integration/simple_wallet.go +++ b/examples/go-integration/simple_wallet.go @@ -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) @@ -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 diff --git a/src/channel/fixed_size_payment.rs b/src/channel/fixed_size_payment.rs index cbcbfec..c57ec42 100644 --- a/src/channel/fixed_size_payment.rs +++ b/src/channel/fixed_size_payment.rs @@ -192,20 +192,22 @@ impl From> 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(), diff --git a/src/channel/proposal.rs b/src/channel/proposal.rs index bca29ce..0411034 100644 --- a/src/channel/proposal.rs +++ b/src/channel/proposal.rs @@ -93,7 +93,7 @@ impl From 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 } } }