From 926c80ff1d178f1aed514d9bb712736180dfc837 Mon Sep 17 00:00:00 2001 From: Steffen Rattay Date: Mon, 9 Jan 2023 14:44:24 +0100 Subject: [PATCH 1/7] wire.proto: use absolute module path This allows including `wire.proto` in other projects and generates the correct include paths in .proto modules including it. Signed-off-by: Steffen Rattay --- wire/protobuf/wire.proto | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wire/protobuf/wire.proto b/wire/protobuf/wire.proto index 1d5d450dd..b5bd68052 100644 --- a/wire/protobuf/wire.proto +++ b/wire/protobuf/wire.proto @@ -18,7 +18,7 @@ syntax = "proto3"; package perunwire; -option go_package = "../protobuf"; +option go_package = "perun.network/go-perun/wire/protobuf"; // Envelope encapsulates a message with the routing information. That is the // the sender and the intended receiver. From 75d0bed3ebb268b8634b4e6c3bffe1c3fa17a65c Mon Sep 17 00:00:00 2001 From: Steffen Rattay Date: Tue, 10 Jan 2023 17:05:32 +0100 Subject: [PATCH 2/7] Expose protobuf converters Signed-off-by: Steffen Rattay --- wire/protobuf/proposalmsgs.go | 7 ++++--- wire/protobuf/syncmsgs.go | 2 +- wire/protobuf/updatemsgs.go | 19 +++++++++++-------- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/wire/protobuf/proposalmsgs.go b/wire/protobuf/proposalmsgs.go index 84321cd32..76a3fcce7 100644 --- a/wire/protobuf/proposalmsgs.go +++ b/wire/protobuf/proposalmsgs.go @@ -150,7 +150,7 @@ func toBaseChannelProposal(protoProp *BaseChannelProposal) (prop client.BaseChan if err != nil { return prop, errors.WithMessage(err, "init bals") } - prop.FundingAgreement = toBalances(protoProp.FundingAgreement) + prop.FundingAgreement = ToBalances(protoProp.FundingAgreement) if err != nil { return prop, errors.WithMessage(err, "funding agreement") } @@ -214,11 +214,12 @@ func toAllocation(protoAlloc *Allocation) (alloc *channel.Allocation, err error) return nil, errors.WithMessagef(err, "%d'th sub alloc", i) } } - alloc.Balances = toBalances(protoAlloc.Balances) + alloc.Balances = ToBalances(protoAlloc.Balances) return alloc, nil } -func toBalances(protoBalances *Balances) (balances channel.Balances) { +// ToBalances parses protobuf balances. +func ToBalances(protoBalances *Balances) (balances channel.Balances) { balances = make([][]channel.Bal, len(protoBalances.Balances)) for i := range protoBalances.Balances { balances[i] = toBalance(protoBalances.Balances[i]) diff --git a/wire/protobuf/syncmsgs.go b/wire/protobuf/syncmsgs.go index f1cdb37b2..409a3ad12 100644 --- a/wire/protobuf/syncmsgs.go +++ b/wire/protobuf/syncmsgs.go @@ -29,7 +29,7 @@ func toChannelSyncMsg(protoEnvMsg *Envelope_ChannelSyncMsg) (msg *client.Channel msg.CurrentTX.Sigs[i] = make([]byte, len(protoMsg.CurrentTx.Sigs[i])) copy(msg.CurrentTX.Sigs[i], protoMsg.CurrentTx.Sigs[i]) } - msg.CurrentTX.State, err = toState(protoMsg.CurrentTx.State) + msg.CurrentTX.State, err = ToState(protoMsg.CurrentTx.State) return msg, err } diff --git a/wire/protobuf/updatemsgs.go b/wire/protobuf/updatemsgs.go index 8aa6ace9a..dd672a60f 100644 --- a/wire/protobuf/updatemsgs.go +++ b/wire/protobuf/updatemsgs.go @@ -35,7 +35,7 @@ func toVirtualChannelFundingProposalMsg(protoEnvMsg *Envelope_VirtualChannelFund protoMsg := protoEnvMsg.VirtualChannelFundingProposalMsg msg = &client.VirtualChannelFundingProposalMsg{} - msg.Initial, err = toSignedState(protoMsg.Initial) + msg.Initial, err = ToSignedState(protoMsg.Initial) if err != nil { return nil, errors.WithMessage(err, "initial state") } @@ -54,7 +54,7 @@ func toVirtualChannelSettlementProposalMsg(protoEnvMsg *Envelope_VirtualChannelS protoMsg := protoEnvMsg.VirtualChannelSettlementProposalMsg msg = &client.VirtualChannelSettlementProposalMsg{} - msg.Final, err = toSignedState(protoMsg.Final) + msg.Final, err = ToSignedState(protoMsg.Final) if err != nil { return nil, errors.WithMessage(err, "final state") } @@ -90,12 +90,13 @@ func toChannelUpdate(protoUpdate *ChannelUpdateMsg) (update client.ChannelUpdate update.ActorIdx = channel.Index(protoUpdate.ChannelUpdate.ActorIdx) update.Sig = make([]byte, len(protoUpdate.Sig)) copy(update.Sig, protoUpdate.Sig) - update.State, err = toState(protoUpdate.ChannelUpdate.State) + update.State, err = ToState(protoUpdate.ChannelUpdate.State) return update, err } -func toSignedState(protoSignedState *SignedState) (signedState channel.SignedState, err error) { - signedState.Params, err = toParams(protoSignedState.Params) +// ToSignedState parses protobuf signed states. +func ToSignedState(protoSignedState *SignedState) (signedState channel.SignedState, err error) { + signedState.Params, err = ToParams(protoSignedState.Params) if err != nil { return signedState, err } @@ -104,11 +105,12 @@ func toSignedState(protoSignedState *SignedState) (signedState channel.SignedSta signedState.Sigs[i] = make([]byte, len(protoSignedState.Sigs[i])) copy(signedState.Sigs[i], protoSignedState.Sigs[i]) } - signedState.State, err = toState(protoSignedState.State) + signedState.State, err = ToState(protoSignedState.State) return signedState, err } -func toParams(protoParams *Params) (*channel.Params, error) { +// ToParams parses protobuf params. +func ToParams(protoParams *Params) (*channel.Params, error) { app, err := toApp(protoParams.App) if err != nil { return nil, err @@ -128,7 +130,8 @@ func toParams(protoParams *Params) (*channel.Params, error) { return params, nil } -func toState(protoState *State) (state *channel.State, err error) { +// ToState parses protobuf states. +func ToState(protoState *State) (state *channel.State, err error) { state = &channel.State{} copy(state.ID[:], protoState.Id) state.Version = protoState.Version From d78dbd41d1603f0fe54e380a7f2996b0aa4e81ca Mon Sep 17 00:00:00 2001 From: Ilja von Hoessle Date: Tue, 8 Aug 2023 10:35:54 +0200 Subject: [PATCH 3/7] README: Update backend compatibility list and link the backend repositories Signed-off-by: Ilja von Hoessle --- NOTICE | 1 + README.md | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/NOTICE b/NOTICE index cb4c69c2e..ee352f945 100644 --- a/NOTICE +++ b/NOTICE @@ -40,6 +40,7 @@ PolyCrypt GmbH Steffen Rattay Sebastian Stammler Oliver Tale-Yazdi + Ilja von Hoessle Robert Bosch GmbH Manoranjith diff --git a/README.md b/README.md index c120bcffd..763c03963 100644 --- a/README.md +++ b/README.md @@ -74,7 +74,10 @@ There are multiple **blockchain backends** available. A backend is automatically - **Ethereum.** The Ethereum backend is available at [perun-eth-backend](https://github.com/perun-network/perun-eth-backend/). - **Polkadot.** The Polkadot backend is available at [perun-polkadot-backend](https://github.com/perun-network/perun-polkadot-backend). - **Cosmos.** The Cosmos backend is available at [perun-cosmwasm-backend](https://github.com/hyperledger-labs/perun-cosmwasm-backend). -- **Fabric** The Hyperledger Fabric backend is available at [perun-fabric](https://github.com/perun-network/perun-fabric). +- **Cardano.** The Cardano backend is available at [perun-cardano-backend](https://github.com/perun-network/perun-cardano-backend). +- **NERVOS.** The NERVOS backend is available at [perun-ckb-backend](https://github.com/perun-network/perun-ckb-backend). +- **Dfinity.** The Dfinity Internet Computer backend is available at [perun-icp-backend](https://github.com/perun-network/perun-icp-backend). +- **Fabric.** The Hyperledger Fabric backend is available at [perun-fabric](https://github.com/perun-network/perun-fabric). - **SimBackend.** The SimBackend represents an ideal blockchain backend (`backend/sim`) implementation that can be used for testing. **Logging and networking** capabilities can also be injected by the user. From 2ab892f84d7df33a5f958b813bace933ee5ac1f9 Mon Sep 17 00:00:00 2001 From: Ilja von Hoessle Date: Mon, 15 Jan 2024 12:33:13 +0100 Subject: [PATCH 4/7] doc: Add MAINTAINERS.md file, add Jens to NOTICE Signed-off-by: Ilja von Hoessle --- MAINTAINERS.md | 28 ++++++++++++++++++++++++++++ NOTICE | 3 ++- 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 MAINTAINERS.md diff --git a/MAINTAINERS.md b/MAINTAINERS.md new file mode 100644 index 000000000..ae2651f35 --- /dev/null +++ b/MAINTAINERS.md @@ -0,0 +1,28 @@ +# Maintainers + +## Active Maintainers +| Name | Github | [Discord][_chat_url] | +|-------------------|-----------|----------------| +| Hendrik Amler | [@tinnendo](https://github.com/tinnendo) | hendrik#5345 | +| Jan Bormet | [@janbormet](https://github.com/janbormet) | _.pants | +| Philipp-Florens Lehwalder | [@cryptphil](https://github.com/cryptphil) | cryptphil | +| Steffen Rattay | [@rmbrt](https://github.com/rmbrt) | rmbrt | +| Ilja von Hoessle | [@iljabvh](https://github.com/DragonDev1906) | iljabvh | +| Jens Winkle | [@DragonDev1906](https://github.com/DragonDev1906) | jens#4601 | + +## Emeritus Maintainers + +| Name | GitHub | [Discord][_chat_url] | +|-------------------|-----------|----------------------| +| Christoph Conrads | | | +| Norbert Dzikowski | [@ndzik](https://github.com/ndzik) | lambdax.1 | +| Matthias Geihs | [@matthiasgeihs](https://github.com/matthiasgeihs) | mpn#9737 | +| Luigi Iandolo | | | +| Manoranjith | [@manoranjith](https://github.com/manoranjith) | manoranjith#8124 | +| Sebastian Stammler | [@sebastianst](https://github.com/sebastianst) | | +| Anagha Sukumaran| [@anagha-ks](https://github.com/anagha-ks) | | +| Oliver Tale-Yazdi | [@ggwpez](https://github.com/ggwpez) | | +| Marius van der Wijden | | | + + +[_chat_url]: https://discord.com/channels/817445017680609340/834052993258225715 diff --git a/NOTICE b/NOTICE index ee352f945..371455648 100644 --- a/NOTICE +++ b/NOTICE @@ -41,7 +41,8 @@ PolyCrypt GmbH Sebastian Stammler Oliver Tale-Yazdi Ilja von Hoessle - + Jens Winkle + Robert Bosch GmbH Manoranjith Anagha Sukumaran From 3aff708e34da108ae6fc50675178205fbbbd0f7b Mon Sep 17 00:00:00 2001 From: Ilja von Hoessle Date: Mon, 15 Jan 2024 12:42:11 +0100 Subject: [PATCH 5/7] doc(MAINTAINERS.md): Fix iljabvh Github handle Signed-off-by: Ilja von Hoessle --- MAINTAINERS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MAINTAINERS.md b/MAINTAINERS.md index ae2651f35..818dd7af7 100644 --- a/MAINTAINERS.md +++ b/MAINTAINERS.md @@ -7,7 +7,7 @@ | Jan Bormet | [@janbormet](https://github.com/janbormet) | _.pants | | Philipp-Florens Lehwalder | [@cryptphil](https://github.com/cryptphil) | cryptphil | | Steffen Rattay | [@rmbrt](https://github.com/rmbrt) | rmbrt | -| Ilja von Hoessle | [@iljabvh](https://github.com/DragonDev1906) | iljabvh | +| Ilja von Hoessle | [@iljabvh](https://github.com/iljabvh) | iljabvh | | Jens Winkle | [@DragonDev1906](https://github.com/DragonDev1906) | jens#4601 | ## Emeritus Maintainers From 5a2e94b59f2545b7677c6fdc2d56ee1b119fa530 Mon Sep 17 00:00:00 2001 From: Jan Bormet Date: Fri, 2 Feb 2024 13:06:32 +0100 Subject: [PATCH 6/7] wire/protobuf: Add conversion functions between protbuf types and go-perun types Signed-off-by: Jan Bormet --- NOTICE | 1 + wire/protobuf/proposalmsgs.go | 203 +++++++++++++++++++++------------- wire/protobuf/serializer.go | 48 ++++---- wire/protobuf/syncmsgs.go | 2 +- wire/protobuf/updatemsgs.go | 97 +++++++++------- 5 files changed, 209 insertions(+), 142 deletions(-) diff --git a/NOTICE b/NOTICE index 371455648..f541ac1cd 100644 --- a/NOTICE +++ b/NOTICE @@ -34,6 +34,7 @@ Chair of Applied Cryptography, Technische Universität Darmstadt, Germany Marius van der Wijden PolyCrypt GmbH + Jan Bormet Norbert Dzikowski Matthias Geihs Philipp-Florens Lehwalder diff --git a/wire/protobuf/proposalmsgs.go b/wire/protobuf/proposalmsgs.go index 76a3fcce7..af1b6b44b 100644 --- a/wire/protobuf/proposalmsgs.go +++ b/wire/protobuf/proposalmsgs.go @@ -26,40 +26,45 @@ import ( "perun.network/go-perun/wire" ) -func toLedgerChannelProposalMsg(protoEnvMsg *Envelope_LedgerChannelProposalMsg) (msg *client.LedgerChannelProposalMsg, err error) { +// ToLedgerChannelProposalMsg converts a protobuf Envelope_LedgerChannelProposalMsg to a client +// LedgerChannelProposalMsg. +func ToLedgerChannelProposalMsg(protoEnvMsg *Envelope_LedgerChannelProposalMsg) (msg *client.LedgerChannelProposalMsg, err error) { protoMsg := protoEnvMsg.LedgerChannelProposalMsg msg = &client.LedgerChannelProposalMsg{} - msg.BaseChannelProposal, err = toBaseChannelProposal(protoMsg.BaseChannelProposal) + msg.BaseChannelProposal, err = ToBaseChannelProposal(protoMsg.BaseChannelProposal) if err != nil { return nil, err } - msg.Participant, err = toWalletAddr(protoMsg.Participant) + msg.Participant, err = ToWalletAddr(protoMsg.Participant) if err != nil { return nil, errors.WithMessage(err, "participant address") } - msg.Peers, err = toWireAddrs(protoMsg.Peers) + msg.Peers, err = ToWireAddrs(protoMsg.Peers) return msg, errors.WithMessage(err, "peers") } -func toSubChannelProposalMsg(protoEnvMsg *Envelope_SubChannelProposalMsg) (msg *client.SubChannelProposalMsg, err error) { +// ToSubChannelProposalMsg converts a protobuf Envelope_SubChannelProposalMsg to a client SubChannelProposalMsg. +func ToSubChannelProposalMsg(protoEnvMsg *Envelope_SubChannelProposalMsg) (msg *client.SubChannelProposalMsg, err error) { protoMsg := protoEnvMsg.SubChannelProposalMsg msg = &client.SubChannelProposalMsg{} copy(msg.Parent[:], protoMsg.Parent) - msg.BaseChannelProposal, err = toBaseChannelProposal(protoMsg.BaseChannelProposal) + msg.BaseChannelProposal, err = ToBaseChannelProposal(protoMsg.BaseChannelProposal) return msg, err } -func toVirtualChannelProposalMsg(protoEnvMsg *Envelope_VirtualChannelProposalMsg) (msg *client.VirtualChannelProposalMsg, err error) { +// ToVirtualChannelProposalMsg converts a protobuf Envelope_VirtualChannelProposalMsg to a client +// VirtualChannelProposalMsg. +func ToVirtualChannelProposalMsg(protoEnvMsg *Envelope_VirtualChannelProposalMsg) (msg *client.VirtualChannelProposalMsg, err error) { protoMsg := protoEnvMsg.VirtualChannelProposalMsg msg = &client.VirtualChannelProposalMsg{} - msg.BaseChannelProposal, err = toBaseChannelProposal(protoMsg.BaseChannelProposal) + msg.BaseChannelProposal, err = ToBaseChannelProposal(protoMsg.BaseChannelProposal) if err != nil { return nil, err } - msg.Proposer, err = toWalletAddr(protoMsg.Proposer) + msg.Proposer, err = ToWalletAddr(protoMsg.Proposer) if err != nil { return nil, errors.WithMessage(err, "proposer") } @@ -69,42 +74,49 @@ func toVirtualChannelProposalMsg(protoEnvMsg *Envelope_VirtualChannelProposalMsg } msg.IndexMaps = make([][]channel.Index, len(protoMsg.IndexMaps)) for i := range protoMsg.IndexMaps { - msg.IndexMaps[i], err = toIndexMap(protoMsg.IndexMaps[i].IndexMap) + msg.IndexMaps[i], err = ToIndexMap(protoMsg.IndexMaps[i].IndexMap) if err != nil { return nil, err } } - msg.Peers, err = toWireAddrs(protoMsg.Peers) + msg.Peers, err = ToWireAddrs(protoMsg.Peers) return msg, errors.WithMessage(err, "peers") } -func toLedgerChannelProposalAccMsg(protoEnvMsg *Envelope_LedgerChannelProposalAccMsg) (msg *client.LedgerChannelProposalAccMsg, err error) { +// ToLedgerChannelProposalAccMsg converts a protobuf Envelope_LedgerChannelProposalAccMsg to a client +// LedgerChannelProposalAccMsg. +func ToLedgerChannelProposalAccMsg(protoEnvMsg *Envelope_LedgerChannelProposalAccMsg) (msg *client.LedgerChannelProposalAccMsg, err error) { protoMsg := protoEnvMsg.LedgerChannelProposalAccMsg msg = &client.LedgerChannelProposalAccMsg{} - msg.BaseChannelProposalAcc = toBaseChannelProposalAcc(protoMsg.BaseChannelProposalAcc) - msg.Participant, err = toWalletAddr(protoMsg.Participant) + msg.BaseChannelProposalAcc = ToBaseChannelProposalAcc(protoMsg.BaseChannelProposalAcc) + msg.Participant, err = ToWalletAddr(protoMsg.Participant) return msg, errors.WithMessage(err, "participant") } -func toSubChannelProposalAccMsg(protoEnvMsg *Envelope_SubChannelProposalAccMsg) (msg *client.SubChannelProposalAccMsg) { +// ToSubChannelProposalAccMsg converts a protobuf Envelope_SubChannelProposalAccMsg to a client +// SubChannelProposalAccMsg. +func ToSubChannelProposalAccMsg(protoEnvMsg *Envelope_SubChannelProposalAccMsg) (msg *client.SubChannelProposalAccMsg) { protoMsg := protoEnvMsg.SubChannelProposalAccMsg msg = &client.SubChannelProposalAccMsg{} - msg.BaseChannelProposalAcc = toBaseChannelProposalAcc(protoMsg.BaseChannelProposalAcc) + msg.BaseChannelProposalAcc = ToBaseChannelProposalAcc(protoMsg.BaseChannelProposalAcc) return msg } -func toVirtualChannelProposalAccMsg(protoEnvMsg *Envelope_VirtualChannelProposalAccMsg) (msg *client.VirtualChannelProposalAccMsg, err error) { +// ToVirtualChannelProposalAccMsg converts a protobuf Envelope_VirtualChannelProposalAccMsg to a client +// VirtualChannelProposalAccMsg. +func ToVirtualChannelProposalAccMsg(protoEnvMsg *Envelope_VirtualChannelProposalAccMsg) (msg *client.VirtualChannelProposalAccMsg, err error) { protoMsg := protoEnvMsg.VirtualChannelProposalAccMsg msg = &client.VirtualChannelProposalAccMsg{} - msg.BaseChannelProposalAcc = toBaseChannelProposalAcc(protoMsg.BaseChannelProposalAcc) - msg.Responder, err = toWalletAddr(protoMsg.Responder) + msg.BaseChannelProposalAcc = ToBaseChannelProposalAcc(protoMsg.BaseChannelProposalAcc) + msg.Responder, err = ToWalletAddr(protoMsg.Responder) return msg, errors.WithMessage(err, "responder") } -func toChannelProposalRejMsg(protoEnvMsg *Envelope_ChannelProposalRejMsg) (msg *client.ChannelProposalRejMsg) { +// ToChannelProposalRejMsg converts a protobuf Envelope_ChannelProposalRejMsg to a client ChannelProposalRejMsg. +func ToChannelProposalRejMsg(protoEnvMsg *Envelope_ChannelProposalRejMsg) (msg *client.ChannelProposalRejMsg) { protoMsg := protoEnvMsg.ChannelProposalRejMsg msg = &client.ChannelProposalRejMsg{} @@ -113,12 +125,14 @@ func toChannelProposalRejMsg(protoEnvMsg *Envelope_ChannelProposalRejMsg) (msg * return msg } -func toWalletAddr(protoAddr []byte) (wallet.Address, error) { +// ToWalletAddr converts a protobuf wallet address to a wallet.Address. +func ToWalletAddr(protoAddr []byte) (wallet.Address, error) { addr := wallet.NewAddress() return addr, addr.UnmarshalBinary(protoAddr) } -func toWalletAddrs(protoAddrs [][]byte) ([]wallet.Address, error) { +// ToWalletAddrs converts protobuf wallet addresses to a slice of wallet.Address. +func ToWalletAddrs(protoAddrs [][]byte) ([]wallet.Address, error) { addrs := make([]wallet.Address, len(protoAddrs)) for i := range protoAddrs { addrs[i] = wallet.NewAddress() @@ -130,7 +144,8 @@ func toWalletAddrs(protoAddrs [][]byte) ([]wallet.Address, error) { return addrs, nil } -func toWireAddrs(protoAddrs [][]byte) ([]wire.Address, error) { +// ToWireAddrs converts protobuf wire addresses to a slice of wire.Address. +func ToWireAddrs(protoAddrs [][]byte) ([]wire.Address, error) { addrs := make([]wire.Address, len(protoAddrs)) for i := range protoAddrs { addrs[i] = wire.NewAddress() @@ -142,11 +157,12 @@ func toWireAddrs(protoAddrs [][]byte) ([]wire.Address, error) { return addrs, nil } -func toBaseChannelProposal(protoProp *BaseChannelProposal) (prop client.BaseChannelProposal, err error) { +// ToBaseChannelProposal converts a protobuf BaseChannelProposal to a client BaseChannelProposal. +func ToBaseChannelProposal(protoProp *BaseChannelProposal) (prop client.BaseChannelProposal, err error) { prop.ChallengeDuration = protoProp.ChallengeDuration copy(prop.ProposalID[:], protoProp.ProposalId) copy(prop.NonceShare[:], protoProp.NonceShare) - prop.InitBals, err = toAllocation(protoProp.InitBals) + prop.InitBals, err = ToAllocation(protoProp.InitBals) if err != nil { return prop, errors.WithMessage(err, "init bals") } @@ -154,17 +170,19 @@ func toBaseChannelProposal(protoProp *BaseChannelProposal) (prop client.BaseChan if err != nil { return prop, errors.WithMessage(err, "funding agreement") } - prop.App, prop.InitData, err = toAppAndData(protoProp.App, protoProp.InitData) + prop.App, prop.InitData, err = ToAppAndData(protoProp.App, protoProp.InitData) return prop, err } -func toBaseChannelProposalAcc(protoPropAcc *BaseChannelProposalAcc) (propAcc client.BaseChannelProposalAcc) { +// ToBaseChannelProposalAcc converts a protobuf BaseChannelProposalAcc to a client BaseChannelProposalAcc. +func ToBaseChannelProposalAcc(protoPropAcc *BaseChannelProposalAcc) (propAcc client.BaseChannelProposalAcc) { copy(propAcc.ProposalID[:], protoPropAcc.ProposalId) copy(propAcc.NonceShare[:], protoPropAcc.NonceShare) return } -func toApp(protoApp []byte) (app channel.App, err error) { +// ToApp converts a protobuf app to a channel.App. +func ToApp(protoApp []byte) (app channel.App, err error) { if len(protoApp) == 0 { app = channel.NoApp() return app, nil @@ -178,7 +196,8 @@ func toApp(protoApp []byte) (app channel.App, err error) { return app, err } -func toAppAndData(protoApp, protoData []byte) (app channel.App, data channel.Data, err error) { +// ToAppAndData converts protobuf app and data to a channel.App and channel.Data. +func ToAppAndData(protoApp, protoData []byte) (app channel.App, data channel.Data, err error) { if len(protoApp) == 0 { app = channel.NoApp() data = channel.NoData() @@ -197,7 +216,8 @@ func toAppAndData(protoApp, protoData []byte) (app channel.App, data channel.Dat return app, data, data.UnmarshalBinary(protoData) } -func toAllocation(protoAlloc *Allocation) (alloc *channel.Allocation, err error) { +// ToAllocation converts a protobuf allocation to a channel.Allocation. +func ToAllocation(protoAlloc *Allocation) (alloc *channel.Allocation, err error) { alloc = &channel.Allocation{} alloc.Assets = make([]channel.Asset, len(protoAlloc.Assets)) for i := range protoAlloc.Assets { @@ -209,7 +229,7 @@ func toAllocation(protoAlloc *Allocation) (alloc *channel.Allocation, err error) } alloc.Locked = make([]channel.SubAlloc, len(protoAlloc.Locked)) for i := range protoAlloc.Locked { - alloc.Locked[i], err = toSubAlloc(protoAlloc.Locked[i]) + alloc.Locked[i], err = ToSubAlloc(protoAlloc.Locked[i]) if err != nil { return nil, errors.WithMessagef(err, "%d'th sub alloc", i) } @@ -218,16 +238,17 @@ func toAllocation(protoAlloc *Allocation) (alloc *channel.Allocation, err error) return alloc, nil } -// ToBalances parses protobuf balances. +// ToBalances converts a protobuf Balances to a channel.Balances. func ToBalances(protoBalances *Balances) (balances channel.Balances) { balances = make([][]channel.Bal, len(protoBalances.Balances)) for i := range protoBalances.Balances { - balances[i] = toBalance(protoBalances.Balances[i]) + balances[i] = ToBalance(protoBalances.Balances[i]) } return balances } -func toBalance(protoBalance *Balance) (balance []channel.Bal) { +// ToBalance converts a protobuf Balance to a channel.Bal. +func ToBalance(protoBalance *Balance) (balance []channel.Bal) { balance = make([]channel.Bal, len(protoBalance.Balance)) for j := range protoBalance.Balance { balance[j] = new(big.Int).SetBytes(protoBalance.Balance[j]) @@ -235,18 +256,20 @@ func toBalance(protoBalance *Balance) (balance []channel.Bal) { return balance } -func toSubAlloc(protoSubAlloc *SubAlloc) (subAlloc channel.SubAlloc, err error) { +// ToSubAlloc converts a protobuf SubAlloc to a channel.SubAlloc. +func ToSubAlloc(protoSubAlloc *SubAlloc) (subAlloc channel.SubAlloc, err error) { subAlloc = channel.SubAlloc{} - subAlloc.Bals = toBalance(protoSubAlloc.Bals) + subAlloc.Bals = ToBalance(protoSubAlloc.Bals) if len(protoSubAlloc.Id) != len(subAlloc.ID) { return subAlloc, errors.New("sub alloc id has incorrect length") } copy(subAlloc.ID[:], protoSubAlloc.Id) - subAlloc.IndexMap, err = toIndexMap(protoSubAlloc.IndexMap.IndexMap) + subAlloc.IndexMap, err = ToIndexMap(protoSubAlloc.IndexMap.IndexMap) return subAlloc, err } -func toIndexMap(protoIndexMap []uint32) (indexMap []channel.Index, err error) { +// ToIndexMap converts a protobuf IndexMap to a channel.IndexMap. +func ToIndexMap(protoIndexMap []uint32) (indexMap []channel.Index, err error) { indexMap = make([]channel.Index, len(protoIndexMap)) for i := range protoIndexMap { if protoIndexMap[i] > math.MaxUint16 { @@ -257,35 +280,40 @@ func toIndexMap(protoIndexMap []uint32) (indexMap []channel.Index, err error) { return indexMap, nil } -func fromLedgerChannelProposalMsg(msg *client.LedgerChannelProposalMsg) (_ *Envelope_LedgerChannelProposalMsg, err error) { +// FromLedgerChannelProposalMsg converts a client LedgerChannelProposalMsg to a protobuf +// Envelope_LedgerChannelProposalMsg. +func FromLedgerChannelProposalMsg(msg *client.LedgerChannelProposalMsg) (_ *Envelope_LedgerChannelProposalMsg, err error) { protoMsg := &LedgerChannelProposalMsg{} - protoMsg.BaseChannelProposal, err = fromBaseChannelProposal(msg.BaseChannelProposal) + protoMsg.BaseChannelProposal, err = FromBaseChannelProposal(msg.BaseChannelProposal) if err != nil { return nil, err } - protoMsg.Participant, err = fromWalletAddr(msg.Participant) + protoMsg.Participant, err = FromWalletAddr(msg.Participant) if err != nil { return nil, errors.WithMessage(err, "participant address") } - protoMsg.Peers, err = fromWireAddrs(msg.Peers) + protoMsg.Peers, err = FromWireAddrs(msg.Peers) return &Envelope_LedgerChannelProposalMsg{protoMsg}, errors.WithMessage(err, "peers") } -func fromSubChannelProposalMsg(msg *client.SubChannelProposalMsg) (_ *Envelope_SubChannelProposalMsg, err error) { +// FromSubChannelProposalMsg converts a client SubChannelProposalMsg to a protobuf Envelope_SubChannelProposalMsg. +func FromSubChannelProposalMsg(msg *client.SubChannelProposalMsg) (_ *Envelope_SubChannelProposalMsg, err error) { protoMsg := &SubChannelProposalMsg{} protoMsg.Parent = make([]byte, len(msg.Parent)) copy(protoMsg.Parent, msg.Parent[:]) - protoMsg.BaseChannelProposal, err = fromBaseChannelProposal(msg.BaseChannelProposal) + protoMsg.BaseChannelProposal, err = FromBaseChannelProposal(msg.BaseChannelProposal) return &Envelope_SubChannelProposalMsg{protoMsg}, err } -func fromVirtualChannelProposalMsg(msg *client.VirtualChannelProposalMsg) (_ *Envelope_VirtualChannelProposalMsg, err error) { +// FromVirtualChannelProposalMsg converts a client VirtualChannelProposalMsg to a protobuf +// Envelope_VirtualChannelProposalMsg. +func FromVirtualChannelProposalMsg(msg *client.VirtualChannelProposalMsg) (_ *Envelope_VirtualChannelProposalMsg, err error) { protoMsg := &VirtualChannelProposalMsg{} - protoMsg.BaseChannelProposal, err = fromBaseChannelProposal(msg.BaseChannelProposal) + protoMsg.BaseChannelProposal, err = FromBaseChannelProposal(msg.BaseChannelProposal) if err != nil { return nil, err } - protoMsg.Proposer, err = fromWalletAddr(msg.Proposer) + protoMsg.Proposer, err = FromWalletAddr(msg.Proposer) if err != nil { return nil, err } @@ -296,33 +324,40 @@ func fromVirtualChannelProposalMsg(msg *client.VirtualChannelProposalMsg) (_ *En } protoMsg.IndexMaps = make([]*IndexMap, len(msg.IndexMaps)) for i := range msg.IndexMaps { - protoMsg.IndexMaps[i] = &IndexMap{IndexMap: fromIndexMap(msg.IndexMaps[i])} + protoMsg.IndexMaps[i] = &IndexMap{IndexMap: FromIndexMap(msg.IndexMaps[i])} } - protoMsg.Peers, err = fromWireAddrs(msg.Peers) + protoMsg.Peers, err = FromWireAddrs(msg.Peers) return &Envelope_VirtualChannelProposalMsg{protoMsg}, errors.WithMessage(err, "peers") } -func fromLedgerChannelProposalAccMsg(msg *client.LedgerChannelProposalAccMsg) (_ *Envelope_LedgerChannelProposalAccMsg, err error) { +// FromLedgerChannelProposalAccMsg converts a client LedgerChannelProposalAccMsg to a protobuf +// Envelope_LedgerChannelProposalAccMsg. +func FromLedgerChannelProposalAccMsg(msg *client.LedgerChannelProposalAccMsg) (_ *Envelope_LedgerChannelProposalAccMsg, err error) { protoMsg := &LedgerChannelProposalAccMsg{} - protoMsg.BaseChannelProposalAcc = fromBaseChannelProposalAcc(msg.BaseChannelProposalAcc) - protoMsg.Participant, err = fromWalletAddr(msg.Participant) + protoMsg.BaseChannelProposalAcc = FromBaseChannelProposalAcc(msg.BaseChannelProposalAcc) + protoMsg.Participant, err = FromWalletAddr(msg.Participant) return &Envelope_LedgerChannelProposalAccMsg{protoMsg}, errors.WithMessage(err, "participant") } -func fromSubChannelProposalAccMsg(msg *client.SubChannelProposalAccMsg) (_ *Envelope_SubChannelProposalAccMsg) { +// FromSubChannelProposalAccMsg converts a client SubChannelProposalAccMsg to a protobuf +// Envelope_SubChannelProposalAccMsg. +func FromSubChannelProposalAccMsg(msg *client.SubChannelProposalAccMsg) (_ *Envelope_SubChannelProposalAccMsg) { protoMsg := &SubChannelProposalAccMsg{} - protoMsg.BaseChannelProposalAcc = fromBaseChannelProposalAcc(msg.BaseChannelProposalAcc) + protoMsg.BaseChannelProposalAcc = FromBaseChannelProposalAcc(msg.BaseChannelProposalAcc) return &Envelope_SubChannelProposalAccMsg{protoMsg} } -func fromVirtualChannelProposalAccMsg(msg *client.VirtualChannelProposalAccMsg) (_ *Envelope_VirtualChannelProposalAccMsg, err error) { +// FromVirtualChannelProposalAccMsg converts a client VirtualChannelProposalAccMsg to a protobuf +// Envelope_VirtualChannelProposalAccMsg. +func FromVirtualChannelProposalAccMsg(msg *client.VirtualChannelProposalAccMsg) (_ *Envelope_VirtualChannelProposalAccMsg, err error) { protoMsg := &VirtualChannelProposalAccMsg{} - protoMsg.BaseChannelProposalAcc = fromBaseChannelProposalAcc(msg.BaseChannelProposalAcc) - protoMsg.Responder, err = fromWalletAddr(msg.Responder) + protoMsg.BaseChannelProposalAcc = FromBaseChannelProposalAcc(msg.BaseChannelProposalAcc) + protoMsg.Responder, err = FromWalletAddr(msg.Responder) return &Envelope_VirtualChannelProposalAccMsg{protoMsg}, errors.WithMessage(err, "responder") } -func fromChannelProposalRejMsg(msg *client.ChannelProposalRejMsg) (_ *Envelope_ChannelProposalRejMsg) { +// FromChannelProposalRejMsg converts a client ChannelProposalRejMsg to a protobuf Envelope_ChannelProposalRejMsg. +func FromChannelProposalRejMsg(msg *client.ChannelProposalRejMsg) (_ *Envelope_ChannelProposalRejMsg) { protoMsg := &ChannelProposalRejMsg{} protoMsg.ProposalId = make([]byte, len(msg.ProposalID)) copy(protoMsg.ProposalId, msg.ProposalID[:]) @@ -330,11 +365,13 @@ func fromChannelProposalRejMsg(msg *client.ChannelProposalRejMsg) (_ *Envelope_C return &Envelope_ChannelProposalRejMsg{protoMsg} } -func fromWalletAddr(addr wallet.Address) ([]byte, error) { +// FromWalletAddr converts a wallet.Address to a protobuf wallet address. +func FromWalletAddr(addr wallet.Address) ([]byte, error) { return addr.MarshalBinary() } -func fromWalletAddrs(addrs []wallet.Address) (protoAddrs [][]byte, err error) { +// FromWalletAddrs converts a slice of wallet.Address to protobuf wallet addresses. +func FromWalletAddrs(addrs []wallet.Address) (protoAddrs [][]byte, err error) { protoAddrs = make([][]byte, len(addrs)) for i := range addrs { protoAddrs[i], err = addrs[i].MarshalBinary() @@ -345,7 +382,8 @@ func fromWalletAddrs(addrs []wallet.Address) (protoAddrs [][]byte, err error) { return protoAddrs, nil } -func fromWireAddrs(addrs []wire.Address) (protoAddrs [][]byte, err error) { +// FromWireAddrs converts a slice of wire.Address to protobuf wire addresses. +func FromWireAddrs(addrs []wire.Address) (protoAddrs [][]byte, err error) { protoAddrs = make([][]byte, len(addrs)) for i := range addrs { protoAddrs[i], err = addrs[i].MarshalBinary() @@ -356,7 +394,8 @@ func fromWireAddrs(addrs []wire.Address) (protoAddrs [][]byte, err error) { return protoAddrs, nil } -func fromBaseChannelProposal(prop client.BaseChannelProposal) (protoProp *BaseChannelProposal, err error) { +// FromBaseChannelProposal converts a client BaseChannelProposal to a protobuf BaseChannelProposal. +func FromBaseChannelProposal(prop client.BaseChannelProposal) (protoProp *BaseChannelProposal, err error) { protoProp = &BaseChannelProposal{} protoProp.ProposalId = make([]byte, len(prop.ProposalID)) @@ -367,19 +406,20 @@ func fromBaseChannelProposal(prop client.BaseChannelProposal) (protoProp *BaseCh protoProp.ChallengeDuration = prop.ChallengeDuration - protoProp.InitBals, err = fromAllocation(*prop.InitBals) + protoProp.InitBals, err = FromAllocation(*prop.InitBals) if err != nil { return nil, errors.WithMessage(err, "init bals") } - protoProp.FundingAgreement, err = fromBalances(prop.FundingAgreement) + protoProp.FundingAgreement, err = FromBalances(prop.FundingAgreement) if err != nil { return nil, errors.WithMessage(err, "funding agreement") } - protoProp.App, protoProp.InitData, err = fromAppAndData(prop.App, prop.InitData) + protoProp.App, protoProp.InitData, err = FromAppAndData(prop.App, prop.InitData) return protoProp, err } -func fromBaseChannelProposalAcc(propAcc client.BaseChannelProposalAcc) (protoPropAcc *BaseChannelProposalAcc) { +// FromBaseChannelProposalAcc converts a client BaseChannelProposalAcc to a protobuf BaseChannelProposalAcc. +func FromBaseChannelProposalAcc(propAcc client.BaseChannelProposalAcc) (protoPropAcc *BaseChannelProposalAcc) { protoPropAcc = &BaseChannelProposalAcc{} protoPropAcc.ProposalId = make([]byte, len(propAcc.ProposalID)) protoPropAcc.NonceShare = make([]byte, len(propAcc.NonceShare)) @@ -388,7 +428,8 @@ func fromBaseChannelProposalAcc(propAcc client.BaseChannelProposalAcc) (protoPro return protoPropAcc } -func fromApp(app channel.App) (protoApp []byte, err error) { +// FromApp converts a channel.App to a protobuf app. +func FromApp(app channel.App) (protoApp []byte, err error) { if channel.IsNoApp(app) { return []byte{}, nil } @@ -396,7 +437,8 @@ func fromApp(app channel.App) (protoApp []byte, err error) { return protoApp, err } -func fromAppAndData(app channel.App, data channel.Data) (protoApp, protoData []byte, err error) { +// FromAppAndData converts channel.App and channel.Data to protobuf app and data. +func FromAppAndData(app channel.App, data channel.Data) (protoApp, protoData []byte, err error) { if channel.IsNoApp(app) { return []byte{}, []byte{}, nil } @@ -408,7 +450,8 @@ func fromAppAndData(app channel.App, data channel.Data) (protoApp, protoData []b return protoApp, protoData, err } -func fromAllocation(alloc channel.Allocation) (protoAlloc *Allocation, err error) { +// FromAllocation converts a channel.Allocation to a protobuf Allocation. +func FromAllocation(alloc channel.Allocation) (protoAlloc *Allocation, err error) { protoAlloc = &Allocation{} protoAlloc.Assets = make([][]byte, len(alloc.Assets)) for i := range alloc.Assets { @@ -419,21 +462,22 @@ func fromAllocation(alloc channel.Allocation) (protoAlloc *Allocation, err error } locked := make([]*SubAlloc, len(alloc.Locked)) for i := range alloc.Locked { - locked[i], err = fromSubAlloc(alloc.Locked[i]) + locked[i], err = FromSubAlloc(alloc.Locked[i]) if err != nil { return nil, errors.WithMessagef(err, "%d'th sub alloc", i) } } - protoAlloc.Balances, err = fromBalances(alloc.Balances) + protoAlloc.Balances, err = FromBalances(alloc.Balances) return protoAlloc, err } -func fromBalances(balances channel.Balances) (protoBalances *Balances, err error) { +// FromBalances converts a channel.Balances to a protobuf Balances. +func FromBalances(balances channel.Balances) (protoBalances *Balances, err error) { protoBalances = &Balances{ Balances: make([]*Balance, len(balances)), } for i := range balances { - protoBalances.Balances[i], err = fromBalance(balances[i]) + protoBalances.Balances[i], err = FromBalance(balances[i]) if err != nil { return nil, errors.WithMessagef(err, "%d'th balance", i) } @@ -441,7 +485,8 @@ func fromBalances(balances channel.Balances) (protoBalances *Balances, err error return protoBalances, nil } -func fromBalance(balance []channel.Bal) (protoBalance *Balance, err error) { +// FromBalance converts a slice of channel.Bal to a protobuf Balance. +func FromBalance(balance []channel.Bal) (protoBalance *Balance, err error) { protoBalance = &Balance{ Balance: make([][]byte, len(balance)), } @@ -457,16 +502,18 @@ func fromBalance(balance []channel.Bal) (protoBalance *Balance, err error) { return protoBalance, nil } -func fromSubAlloc(subAlloc channel.SubAlloc) (protoSubAlloc *SubAlloc, err error) { +// FromSubAlloc converts a channel.SubAlloc to a protobuf SubAlloc. +func FromSubAlloc(subAlloc channel.SubAlloc) (protoSubAlloc *SubAlloc, err error) { protoSubAlloc = &SubAlloc{} protoSubAlloc.Id = make([]byte, len(subAlloc.ID)) copy(protoSubAlloc.Id, subAlloc.ID[:]) - protoSubAlloc.IndexMap = &IndexMap{IndexMap: fromIndexMap(subAlloc.IndexMap)} - protoSubAlloc.Bals, err = fromBalance(subAlloc.Bals) + protoSubAlloc.IndexMap = &IndexMap{IndexMap: FromIndexMap(subAlloc.IndexMap)} + protoSubAlloc.Bals, err = FromBalance(subAlloc.Bals) return protoSubAlloc, err } -func fromIndexMap(indexMap []channel.Index) (protoIndexMap []uint32) { +// FromIndexMap converts a channel.IndexMap to a protobuf index map. +func FromIndexMap(indexMap []channel.Index) (protoIndexMap []uint32) { protoIndexMap = make([]uint32, len(indexMap)) for i := range indexMap { protoIndexMap[i] = uint32(indexMap[i]) diff --git a/wire/protobuf/serializer.go b/wire/protobuf/serializer.go index d5b7222c7..cd7b284f5 100644 --- a/wire/protobuf/serializer.go +++ b/wire/protobuf/serializer.go @@ -47,29 +47,29 @@ func (serializer) Encode(w io.Writer, env *wire.Envelope) (err error) { //nolint case *wire.AuthResponseMsg: protoEnv.Msg = &Envelope_AuthResponseMsg{} case *client.LedgerChannelProposalMsg: - protoEnv.Msg, err = fromLedgerChannelProposalMsg(msg) + protoEnv.Msg, err = FromLedgerChannelProposalMsg(msg) case *client.SubChannelProposalMsg: - protoEnv.Msg, err = fromSubChannelProposalMsg(msg) + protoEnv.Msg, err = FromSubChannelProposalMsg(msg) case *client.VirtualChannelProposalMsg: - protoEnv.Msg, err = fromVirtualChannelProposalMsg(msg) + protoEnv.Msg, err = FromVirtualChannelProposalMsg(msg) case *client.LedgerChannelProposalAccMsg: - protoEnv.Msg, err = fromLedgerChannelProposalAccMsg(msg) + protoEnv.Msg, err = FromLedgerChannelProposalAccMsg(msg) case *client.SubChannelProposalAccMsg: - protoEnv.Msg = fromSubChannelProposalAccMsg(msg) + protoEnv.Msg = FromSubChannelProposalAccMsg(msg) case *client.VirtualChannelProposalAccMsg: - protoEnv.Msg, err = fromVirtualChannelProposalAccMsg(msg) + protoEnv.Msg, err = FromVirtualChannelProposalAccMsg(msg) case *client.ChannelProposalRejMsg: - protoEnv.Msg = fromChannelProposalRejMsg(msg) + protoEnv.Msg = FromChannelProposalRejMsg(msg) case *client.ChannelUpdateMsg: - protoEnv.Msg, err = fromChannelUpdateMsg(msg) + protoEnv.Msg, err = FromChannelUpdateMsg(msg) case *client.VirtualChannelFundingProposalMsg: - protoEnv.Msg, err = fromVirtualChannelFundingProposalMsg(msg) + protoEnv.Msg, err = FromVirtualChannelFundingProposalMsg(msg) case *client.VirtualChannelSettlementProposalMsg: - protoEnv.Msg, err = fromVirtualChannelSettlementProposalMsg(msg) + protoEnv.Msg, err = FromVirtualChannelSettlementProposalMsg(msg) case *client.ChannelUpdateAccMsg: - protoEnv.Msg = fromChannelUpdateAccMsg(msg) + protoEnv.Msg = FromChannelUpdateAccMsg(msg) case *client.ChannelUpdateRejMsg: - protoEnv.Msg = fromChannelUpdateRejMsg(msg) + protoEnv.Msg = FromChannelUpdateRejMsg(msg) case *client.ChannelSyncMsg: protoEnv.Msg, err = fromChannelSyncMsg(msg) default: @@ -133,29 +133,29 @@ func (serializer) Decode(r io.Reader) (env *wire.Envelope, err error) { //nolint case *Envelope_AuthResponseMsg: env.Msg = &wire.AuthResponseMsg{} case *Envelope_LedgerChannelProposalMsg: - env.Msg, err = toLedgerChannelProposalMsg(protoMsg) + env.Msg, err = ToLedgerChannelProposalMsg(protoMsg) case *Envelope_SubChannelProposalMsg: - env.Msg, err = toSubChannelProposalMsg(protoMsg) + env.Msg, err = ToSubChannelProposalMsg(protoMsg) case *Envelope_VirtualChannelProposalMsg: - env.Msg, err = toVirtualChannelProposalMsg(protoMsg) + env.Msg, err = ToVirtualChannelProposalMsg(protoMsg) case *Envelope_LedgerChannelProposalAccMsg: - env.Msg, err = toLedgerChannelProposalAccMsg(protoMsg) + env.Msg, err = ToLedgerChannelProposalAccMsg(protoMsg) case *Envelope_SubChannelProposalAccMsg: - env.Msg = toSubChannelProposalAccMsg(protoMsg) + env.Msg = ToSubChannelProposalAccMsg(protoMsg) case *Envelope_VirtualChannelProposalAccMsg: - env.Msg, err = toVirtualChannelProposalAccMsg(protoMsg) + env.Msg, err = ToVirtualChannelProposalAccMsg(protoMsg) case *Envelope_ChannelProposalRejMsg: - env.Msg = toChannelProposalRejMsg(protoMsg) + env.Msg = ToChannelProposalRejMsg(protoMsg) case *Envelope_ChannelUpdateMsg: - env.Msg, err = toChannelUpdateMsg(protoMsg) + env.Msg, err = ToChannelUpdateMsg(protoMsg) case *Envelope_VirtualChannelFundingProposalMsg: - env.Msg, err = toVirtualChannelFundingProposalMsg(protoMsg) + env.Msg, err = ToVirtualChannelFundingProposalMsg(protoMsg) case *Envelope_VirtualChannelSettlementProposalMsg: - env.Msg, err = toVirtualChannelSettlementProposalMsg(protoMsg) + env.Msg, err = ToVirtualChannelSettlementProposalMsg(protoMsg) case *Envelope_ChannelUpdateAccMsg: - env.Msg = toChannelUpdateAccMsg(protoMsg) + env.Msg = ToChannelUpdateAccMsg(protoMsg) case *Envelope_ChannelUpdateRejMsg: - env.Msg = toChannelUpdateRejMsg(protoMsg) + env.Msg = ToChannelUpdateRejMsg(protoMsg) case *Envelope_ChannelSyncMsg: env.Msg, err = toChannelSyncMsg(protoMsg) default: diff --git a/wire/protobuf/syncmsgs.go b/wire/protobuf/syncmsgs.go index 409a3ad12..fb94a5b19 100644 --- a/wire/protobuf/syncmsgs.go +++ b/wire/protobuf/syncmsgs.go @@ -43,6 +43,6 @@ func fromChannelSyncMsg(msg *client.ChannelSyncMsg) (_ *Envelope_ChannelSyncMsg, protoMsg.CurrentTx.Sigs[i] = make([]byte, len(msg.CurrentTX.Sigs[i])) copy(protoMsg.CurrentTx.Sigs[i], msg.CurrentTX.Sigs[i]) } - protoMsg.CurrentTx.State, err = fromState(msg.CurrentTX.State) + protoMsg.CurrentTx.State, err = FromState(msg.CurrentTX.State) return &Envelope_ChannelSyncMsg{protoMsg}, err } diff --git a/wire/protobuf/updatemsgs.go b/wire/protobuf/updatemsgs.go index dd672a60f..dc30b00ce 100644 --- a/wire/protobuf/updatemsgs.go +++ b/wire/protobuf/updatemsgs.go @@ -23,12 +23,15 @@ import ( "perun.network/go-perun/client" ) -func toChannelUpdateMsg(protoEnvMsg *Envelope_ChannelUpdateMsg) (*client.ChannelUpdateMsg, error) { - update, err := toChannelUpdate(protoEnvMsg.ChannelUpdateMsg) +// ToChannelUpdateMsg converts a protobuf Envelope_ChannelUpdateMsg to a client.ChannelUpdateMsg. +func ToChannelUpdateMsg(protoEnvMsg *Envelope_ChannelUpdateMsg) (*client.ChannelUpdateMsg, error) { + update, err := ToChannelUpdate(protoEnvMsg.ChannelUpdateMsg) return &update, err } -func toVirtualChannelFundingProposalMsg(protoEnvMsg *Envelope_VirtualChannelFundingProposalMsg) ( +// ToVirtualChannelFundingProposalMsg converts a protobuf Envelope_VirtualChannelFundingProposalMsg to a +// client.VirtualChannelFundingProposalMsg. +func ToVirtualChannelFundingProposalMsg(protoEnvMsg *Envelope_VirtualChannelFundingProposalMsg) ( msg *client.VirtualChannelFundingProposalMsg, err error, ) { @@ -39,15 +42,17 @@ func toVirtualChannelFundingProposalMsg(protoEnvMsg *Envelope_VirtualChannelFund if err != nil { return nil, errors.WithMessage(err, "initial state") } - msg.IndexMap, err = toIndexMap(protoMsg.IndexMap.IndexMap) + msg.IndexMap, err = ToIndexMap(protoMsg.IndexMap.IndexMap) if err != nil { return nil, err } - msg.ChannelUpdateMsg, err = toChannelUpdate(protoMsg.ChannelUpdateMsg) + msg.ChannelUpdateMsg, err = ToChannelUpdate(protoMsg.ChannelUpdateMsg) return msg, err } -func toVirtualChannelSettlementProposalMsg(protoEnvMsg *Envelope_VirtualChannelSettlementProposalMsg) ( +// ToVirtualChannelSettlementProposalMsg converts a protobuf Envelope_VirtualChannelSettlementProposalMsg to a +// client.VirtualChannelSettlementProposalMsg. +func ToVirtualChannelSettlementProposalMsg(protoEnvMsg *Envelope_VirtualChannelSettlementProposalMsg) ( msg *client.VirtualChannelSettlementProposalMsg, err error, ) { @@ -58,11 +63,12 @@ func toVirtualChannelSettlementProposalMsg(protoEnvMsg *Envelope_VirtualChannelS if err != nil { return nil, errors.WithMessage(err, "final state") } - msg.ChannelUpdateMsg, err = toChannelUpdate(protoMsg.ChannelUpdateMsg) + msg.ChannelUpdateMsg, err = ToChannelUpdate(protoMsg.ChannelUpdateMsg) return msg, err } -func toChannelUpdateAccMsg(protoEnvMsg *Envelope_ChannelUpdateAccMsg) (msg *client.ChannelUpdateAccMsg) { +// ToChannelUpdateAccMsg converts a protobuf Envelope_ChannelUpdateAccMsg to a client.ChannelUpdateAccMsg. +func ToChannelUpdateAccMsg(protoEnvMsg *Envelope_ChannelUpdateAccMsg) (msg *client.ChannelUpdateAccMsg) { protoMsg := protoEnvMsg.ChannelUpdateAccMsg msg = &client.ChannelUpdateAccMsg{} @@ -73,7 +79,8 @@ func toChannelUpdateAccMsg(protoEnvMsg *Envelope_ChannelUpdateAccMsg) (msg *clie return msg } -func toChannelUpdateRejMsg(protoEnvMsg *Envelope_ChannelUpdateRejMsg) (msg *client.ChannelUpdateRejMsg) { +// ToChannelUpdateRejMsg converts a protobuf Envelope_ChannelUpdateRejMsg to a client.ChannelUpdateRejMsg. +func ToChannelUpdateRejMsg(protoEnvMsg *Envelope_ChannelUpdateRejMsg) (msg *client.ChannelUpdateRejMsg) { protoMsg := protoEnvMsg.ChannelUpdateRejMsg msg = &client.ChannelUpdateRejMsg{} @@ -83,7 +90,8 @@ func toChannelUpdateRejMsg(protoEnvMsg *Envelope_ChannelUpdateRejMsg) (msg *clie return msg } -func toChannelUpdate(protoUpdate *ChannelUpdateMsg) (update client.ChannelUpdateMsg, err error) { +// ToChannelUpdate parses protobuf channel updates. +func ToChannelUpdate(protoUpdate *ChannelUpdateMsg) (update client.ChannelUpdateMsg, err error) { if protoUpdate.ChannelUpdate.ActorIdx > math.MaxUint16 { return update, errors.New("actor index is invalid") } @@ -94,7 +102,7 @@ func toChannelUpdate(protoUpdate *ChannelUpdateMsg) (update client.ChannelUpdate return update, err } -// ToSignedState parses protobuf signed states. +// ToSignedState converts a protobuf SignedState to a channel.SignedState. func ToSignedState(protoSignedState *SignedState) (signedState channel.SignedState, err error) { signedState.Params, err = ToParams(protoSignedState.Params) if err != nil { @@ -109,13 +117,13 @@ func ToSignedState(protoSignedState *SignedState) (signedState channel.SignedSta return signedState, err } -// ToParams parses protobuf params. +// ToParams converts a protobuf Params to a channel.Params. func ToParams(protoParams *Params) (*channel.Params, error) { - app, err := toApp(protoParams.App) + app, err := ToApp(protoParams.App) if err != nil { return nil, err } - parts, err := toWalletAddrs(protoParams.Parts) + parts, err := ToWalletAddrs(protoParams.Parts) if err != nil { return nil, errors.WithMessage(err, "parts") } @@ -130,56 +138,62 @@ func ToParams(protoParams *Params) (*channel.Params, error) { return params, nil } -// ToState parses protobuf states. +// ToState converts a protobuf State to a channel.State. func ToState(protoState *State) (state *channel.State, err error) { state = &channel.State{} copy(state.ID[:], protoState.Id) state.Version = protoState.Version state.IsFinal = protoState.IsFinal - allocation, err := toAllocation(protoState.Allocation) + allocation, err := ToAllocation(protoState.Allocation) if err != nil { return nil, errors.WithMessage(err, "allocation") } state.Allocation = *allocation - state.App, state.Data, err = toAppAndData(protoState.App, protoState.Data) + state.App, state.Data, err = ToAppAndData(protoState.App, protoState.Data) return state, err } -func fromChannelUpdateMsg(msg *client.ChannelUpdateMsg) (*Envelope_ChannelUpdateMsg, error) { - protoMsg, err := fromChannelUpdate(msg) +// FromChannelUpdateMsg converts a client.ChannelUpdateMsg to a protobuf Envelope_ChannelUpdateMsg. +func FromChannelUpdateMsg(msg *client.ChannelUpdateMsg) (*Envelope_ChannelUpdateMsg, error) { + protoMsg, err := FromChannelUpdate(msg) return &Envelope_ChannelUpdateMsg{protoMsg}, err } -func fromVirtualChannelFundingProposalMsg(msg *client.VirtualChannelFundingProposalMsg) ( +// FromVirtualChannelFundingProposalMsg converts a client.VirtualChannelFundingProposalMsg to a protobuf +// Envelope_VirtualChannelFundingProposalMsg. +func FromVirtualChannelFundingProposalMsg(msg *client.VirtualChannelFundingProposalMsg) ( _ *Envelope_VirtualChannelFundingProposalMsg, err error, ) { protoMsg := &VirtualChannelFundingProposalMsg{} - protoMsg.Initial, err = fromSignedState(&msg.Initial) + protoMsg.Initial, err = FromSignedState(&msg.Initial) if err != nil { return nil, errors.WithMessage(err, "initial state") } - protoMsg.IndexMap = &IndexMap{IndexMap: fromIndexMap(msg.IndexMap)} - protoMsg.ChannelUpdateMsg, err = fromChannelUpdate(&msg.ChannelUpdateMsg) + protoMsg.IndexMap = &IndexMap{IndexMap: FromIndexMap(msg.IndexMap)} + protoMsg.ChannelUpdateMsg, err = FromChannelUpdate(&msg.ChannelUpdateMsg) return &Envelope_VirtualChannelFundingProposalMsg{protoMsg}, err } -func fromVirtualChannelSettlementProposalMsg(msg *client.VirtualChannelSettlementProposalMsg) ( +// FromVirtualChannelSettlementProposalMsg converts a client.VirtualChannelSettlementProposalMsg to a protobuf +// Envelope_VirtualChannelSettlementProposalMsg. +func FromVirtualChannelSettlementProposalMsg(msg *client.VirtualChannelSettlementProposalMsg) ( _ *Envelope_VirtualChannelSettlementProposalMsg, err error, ) { protoMsg := &VirtualChannelSettlementProposalMsg{} - protoMsg.ChannelUpdateMsg, err = fromChannelUpdate(&msg.ChannelUpdateMsg) + protoMsg.ChannelUpdateMsg, err = FromChannelUpdate(&msg.ChannelUpdateMsg) if err != nil { return nil, err } - protoMsg.Final, err = fromSignedState(&msg.Final) + protoMsg.Final, err = FromSignedState(&msg.Final) return &Envelope_VirtualChannelSettlementProposalMsg{protoMsg}, err } -func fromChannelUpdateAccMsg(msg *client.ChannelUpdateAccMsg) *Envelope_ChannelUpdateAccMsg { +// FromChannelUpdateAccMsg converts a client.ChannelUpdateAccMsg to a protobuf Envelope_ChannelUpdateAccMsg. +func FromChannelUpdateAccMsg(msg *client.ChannelUpdateAccMsg) *Envelope_ChannelUpdateAccMsg { protoMsg := &ChannelUpdateAccMsg{} protoMsg.ChannelId = make([]byte, len(msg.ChannelID)) @@ -190,7 +204,8 @@ func fromChannelUpdateAccMsg(msg *client.ChannelUpdateAccMsg) *Envelope_ChannelU return &Envelope_ChannelUpdateAccMsg{protoMsg} } -func fromChannelUpdateRejMsg(msg *client.ChannelUpdateRejMsg) *Envelope_ChannelUpdateRejMsg { +// FromChannelUpdateRejMsg converts a client.ChannelUpdateRejMsg to a protobuf Envelope_ChannelUpdateRejMsg. +func FromChannelUpdateRejMsg(msg *client.ChannelUpdateRejMsg) *Envelope_ChannelUpdateRejMsg { protoMsg := &ChannelUpdateRejMsg{} protoMsg.ChannelId = make([]byte, len(msg.ChannelID)) copy(protoMsg.ChannelId, msg.ChannelID[:]) @@ -199,58 +214,62 @@ func fromChannelUpdateRejMsg(msg *client.ChannelUpdateRejMsg) *Envelope_ChannelU return &Envelope_ChannelUpdateRejMsg{protoMsg} } -func fromChannelUpdate(update *client.ChannelUpdateMsg) (protoUpdate *ChannelUpdateMsg, err error) { +// FromChannelUpdate converts a client.ChannelUpdateMsg to a protobuf ChannelUpdateMsg. +func FromChannelUpdate(update *client.ChannelUpdateMsg) (protoUpdate *ChannelUpdateMsg, err error) { protoUpdate = &ChannelUpdateMsg{} protoUpdate.ChannelUpdate = &ChannelUpdate{} protoUpdate.ChannelUpdate.ActorIdx = uint32(update.ChannelUpdate.ActorIdx) protoUpdate.Sig = make([]byte, len(update.Sig)) copy(protoUpdate.Sig, update.Sig) - protoUpdate.ChannelUpdate.State, err = fromState(update.ChannelUpdate.State) + protoUpdate.ChannelUpdate.State, err = FromState(update.ChannelUpdate.State) return protoUpdate, err } -func fromSignedState(signedState *channel.SignedState) (protoSignedState *SignedState, err error) { +// FromSignedState converts a channel.SignedState to a protobuf SignedState. +func FromSignedState(signedState *channel.SignedState) (protoSignedState *SignedState, err error) { protoSignedState = &SignedState{} protoSignedState.Sigs = make([][]byte, len(signedState.Sigs)) for i := range signedState.Sigs { protoSignedState.Sigs[i] = make([]byte, len(signedState.Sigs[i])) copy(protoSignedState.Sigs[i], signedState.Sigs[i]) } - protoSignedState.Params, err = fromParams(signedState.Params) + protoSignedState.Params, err = FromParams(signedState.Params) if err != nil { return nil, err } - protoSignedState.State, err = fromState(signedState.State) + protoSignedState.State, err = FromState(signedState.State) return protoSignedState, err } -func fromParams(params *channel.Params) (protoParams *Params, err error) { +// FromParams converts a channel.Params to a protobuf Params. +func FromParams(params *channel.Params) (protoParams *Params, err error) { protoParams = &Params{} protoParams.Nonce = params.Nonce.Bytes() protoParams.ChallengeDuration = params.ChallengeDuration protoParams.LedgerChannel = params.LedgerChannel protoParams.VirtualChannel = params.VirtualChannel - protoParams.Parts, err = fromWalletAddrs(params.Parts) + protoParams.Parts, err = FromWalletAddrs(params.Parts) if err != nil { return nil, errors.WithMessage(err, "parts") } - protoParams.App, err = fromApp(params.App) + protoParams.App, err = FromApp(params.App) return protoParams, err } -func fromState(state *channel.State) (protoState *State, err error) { +// FromState converts a channel.State to a protobuf State. +func FromState(state *channel.State) (protoState *State, err error) { protoState = &State{} protoState.Id = make([]byte, len(state.ID)) copy(protoState.Id, state.ID[:]) protoState.Version = state.Version protoState.IsFinal = state.IsFinal - protoState.Allocation, err = fromAllocation(state.Allocation) + protoState.Allocation, err = FromAllocation(state.Allocation) if err != nil { return nil, errors.WithMessage(err, "allocation") } - protoState.App, protoState.Data, err = fromAppAndData(state.App, state.Data) + protoState.App, protoState.Data, err = FromAppAndData(state.App, state.Data) return protoState, err } From b89b2c3cb30e712c4d21cdbfb2d1b1486e71961a Mon Sep 17 00:00:00 2001 From: Minh Huy Tran Date: Tue, 6 Feb 2024 12:51:59 +0100 Subject: [PATCH 7/7] fix(internal_tests): fix failing unit tests Signed-off-by: Minh Huy Tran --- MAINTAINERS.md | 1 + NOTICE | 1 + channel/test/app_randomizer.go | 2 +- channel/test/app_randomizer_internal_test.go | 4 ++-- channel/test/mock_app_randomizer.go | 15 ++++++++++++++- go.mod | 1 + go.sum | 2 ++ wire/net/simple/init.go | 13 ++++++++++++- 8 files changed, 34 insertions(+), 5 deletions(-) diff --git a/MAINTAINERS.md b/MAINTAINERS.md index 818dd7af7..6a026a2fa 100644 --- a/MAINTAINERS.md +++ b/MAINTAINERS.md @@ -9,6 +9,7 @@ | Steffen Rattay | [@rmbrt](https://github.com/rmbrt) | rmbrt | | Ilja von Hoessle | [@iljabvh](https://github.com/iljabvh) | iljabvh | | Jens Winkle | [@DragonDev1906](https://github.com/DragonDev1906) | jens#4601 | +| Minh Huy Tran | [@NhoxxKienn](https://github.com/NhoxxKienn) | NhoxxKienn | ## Emeritus Maintainers diff --git a/NOTICE b/NOTICE index 371455648..1476ec69c 100644 --- a/NOTICE +++ b/NOTICE @@ -42,6 +42,7 @@ PolyCrypt GmbH Oliver Tale-Yazdi Ilja von Hoessle Jens Winkle + Minh Huy Tran Robert Bosch GmbH Manoranjith diff --git a/channel/test/app_randomizer.go b/channel/test/app_randomizer.go index 3cc69a1cf..ae7442b20 100644 --- a/channel/test/app_randomizer.go +++ b/channel/test/app_randomizer.go @@ -27,7 +27,7 @@ type AppRandomizer interface { NewRandomData(*rand.Rand) channel.Data } -var appRandomizer AppRandomizer = &MockAppRandomizer{} +var appRandomizer AppRandomizer = NewMockAppRandomizer() // isAppRandomizerSet tracks whether the AppRandomizer was already set // with `SetAppRandomizer`. diff --git a/channel/test/app_randomizer_internal_test.go b/channel/test/app_randomizer_internal_test.go index 64d7b9bb8..3416841a2 100644 --- a/channel/test/app_randomizer_internal_test.go +++ b/channel/test/app_randomizer_internal_test.go @@ -29,13 +29,13 @@ func TestAppRandomizerSet(t *testing.T) { assert.False(t, isAppRandomizerSet, "isAppRandomizerSet should be defaulted to false") old := appRandomizer - assert.NotPanics(t, func() { SetAppRandomizer(&MockAppRandomizer{}) }, "first SetAppRandomizer() should work") + assert.NotPanics(t, func() { SetAppRandomizer(NewMockAppRandomizer()) }, "first SetAppRandomizer() should work") assert.True(t, isAppRandomizerSet, "isAppRandomizerSet should be true") assert.NotNil(t, appRandomizer, "appRandomizer should not be nil") assert.False(t, old == appRandomizer, "appRandomizer should have changed") old = appRandomizer - assert.Panics(t, func() { SetAppRandomizer(&MockAppRandomizer{}) }, "second SetAppRandomizer() should panic") + assert.Panics(t, func() { SetAppRandomizer(NewMockAppRandomizer()) }, "second SetAppRandomizer() should panic") assert.True(t, isAppRandomizerSet, "isAppRandomizerSet should be true") assert.NotNil(t, appRandomizer, "appRandomizer should not be nil") assert.True(t, old == appRandomizer, "appRandomizer should not have changed") diff --git a/channel/test/mock_app_randomizer.go b/channel/test/mock_app_randomizer.go index a03a9cff0..0e20c2493 100644 --- a/channel/test/mock_app_randomizer.go +++ b/channel/test/mock_app_randomizer.go @@ -17,11 +17,19 @@ package test import ( "math/rand" + "github.com/google/uuid" "perun.network/go-perun/channel" ) // MockAppRandomizer implements the AppRandomizer interface. -type MockAppRandomizer struct{} +type MockAppRandomizer struct { + id uuid.UUID // Unique identifier for each instance +} + +// NewMockAppRandomizer creates a new instance of MockAppRandomizer with a unique identifier. +func NewMockAppRandomizer() *MockAppRandomizer { + return &MockAppRandomizer{id: uuid.New()} +} // NewRandomApp creates a new MockApp with a random address. func (MockAppRandomizer) NewRandomApp(rng *rand.Rand) channel.App { @@ -32,3 +40,8 @@ func (MockAppRandomizer) NewRandomApp(rng *rand.Rand) channel.App { func (MockAppRandomizer) NewRandomData(rng *rand.Rand) channel.Data { return channel.NewMockOp(channel.MockOp(rng.Uint64())) } + +// Equal returns false for any comparison, ensuring two MockAppRandomizers are always considered different. +func (m *MockAppRandomizer) Equal(other *MockAppRandomizer) bool { + return m.id != other.id +} diff --git a/go.mod b/go.mod index 5d66b4575..a765acf82 100644 --- a/go.mod +++ b/go.mod @@ -17,6 +17,7 @@ require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/golang/snappy v0.0.4 // indirect github.com/google/go-cmp v0.5.4 // indirect + github.com/google/uuid v1.6.0 github.com/kr/text v0.2.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/stretchr/objx v0.1.1 // indirect diff --git a/go.sum b/go.sum index 3e51d0b9f..80c0f338c 100644 --- a/go.sum +++ b/go.sum @@ -19,6 +19,8 @@ github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.4 h1:L8R9j+yAqZuZjsqh/z+F1NCffTKKLShY6zXTItVIZ8M= github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI= diff --git a/wire/net/simple/init.go b/wire/net/simple/init.go index 29af22ab2..cce04c416 100644 --- a/wire/net/simple/init.go +++ b/wire/net/simple/init.go @@ -14,10 +14,21 @@ package simple -import "perun.network/go-perun/wire" +import ( + "math/rand" + + "perun.network/go-perun/wire" + "perun.network/go-perun/wire/test" +) func init() { wire.SetNewAddressFunc(func() wire.Address { return NewAddress("") }) + test.SetNewRandomAddress(func(rng *rand.Rand) wire.Address { + return NewRandomAddress(rng) + }) + test.SetNewRandomAccount(func(rng *rand.Rand) wire.Account { + return NewRandomAccount(rng) + }) }