From fdfb864f61886f26c29fdb243b103e8f66208cf7 Mon Sep 17 00:00:00 2001 From: Steffen Rattay Date: Mon, 9 Jan 2023 14:44:24 +0100 Subject: [PATCH 1/8] 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 56941978210c9ac189fec0c0ae0242b47d54e134 Mon Sep 17 00:00:00 2001 From: Steffen Rattay Date: Tue, 10 Jan 2023 17:05:32 +0100 Subject: [PATCH 2/8] 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 643e286c518c7bed9aa4e210bd612d90f29e0115 Mon Sep 17 00:00:00 2001 From: Ilja von Hoessle Date: Tue, 8 Aug 2023 10:35:54 +0200 Subject: [PATCH 3/8] 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 1f0c6d3204ef8fe6fd2174064b00a31fa0155d74 Mon Sep 17 00:00:00 2001 From: Ilja von Hoessle Date: Mon, 15 Jan 2024 12:33:13 +0100 Subject: [PATCH 4/8] 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 40f6013bfedb413d7e5943d65c7e045c2db02832 Mon Sep 17 00:00:00 2001 From: Ilja von Hoessle Date: Mon, 15 Jan 2024 12:42:11 +0100 Subject: [PATCH 5/8] 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 e08ee0bfe1474f46ba2cc87a6f50b8fefb68571d Mon Sep 17 00:00:00 2001 From: Minh Huy Tran Date: Tue, 6 Feb 2024 12:51:59 +0100 Subject: [PATCH 6/8] 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) + }) } From 0b444cb3a6e712f83167bb48373857b7f250fbd4 Mon Sep 17 00:00:00 2001 From: Jan Bormet Date: Fri, 2 Feb 2024 13:06:32 +0100 Subject: [PATCH 7/8] 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 1476ec69c..5154d5d50 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 35ed06164e8d39e03ad4e0414dd51dd024cc108b Mon Sep 17 00:00:00 2001 From: Sophia Koehler Date: Tue, 20 Feb 2024 14:31:11 +0100 Subject: [PATCH 8/8] Revert: Remove settle secondary (#351) Chore: Add author for this commit. Signed-off-by: Sophia Koehler chore: update NOTICE. Signed-off-by: Sophia Koehler --- MAINTAINERS.md | 1 + NOTICE | 1 + channel/adjudicator.go | 14 ++++++++++---- channel/machine.go | 8 ++++++-- client/adjudicate.go | 7 ++++--- client/test/bob.go | 2 +- client/test/channel.go | 10 +++++++++- client/test/fund.go | 4 ++-- client/test/multiledger_dispute.go | 5 +++-- client/test/multiledger_happy.go | 5 +++-- client/test/persistence.go | 2 +- client/test/progression.go | 4 ++-- client/test/subchannel.go | 2 +- client/test/virtualchannel.go | 4 ++-- watcher/local/watcher.go | 5 +++-- 15 files changed, 49 insertions(+), 25 deletions(-) diff --git a/MAINTAINERS.md b/MAINTAINERS.md index 6a026a2fa..7607b4961 100644 --- a/MAINTAINERS.md +++ b/MAINTAINERS.md @@ -10,6 +10,7 @@ | 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 | +| Sophia Koehler | [@sophia1ch](https://githun.com/sophia1ch) | sophia#3072 | ## Emeritus Maintainers diff --git a/NOTICE b/NOTICE index 5154d5d50..7b42b10f0 100644 --- a/NOTICE +++ b/NOTICE @@ -44,6 +44,7 @@ PolyCrypt GmbH Ilja von Hoessle Jens Winkle Minh Huy Tran + Sophia Koehler Robert Bosch GmbH Manoranjith diff --git a/channel/adjudicator.go b/channel/adjudicator.go index 1751a5469..ddfd1a7b0 100644 --- a/channel/adjudicator.go +++ b/channel/adjudicator.go @@ -103,11 +103,17 @@ type ( // An AdjudicatorReq collects all necessary information to make calls to the // adjudicator. + // + // If the Secondary flag is set to true, it is assumed that this is an + // on-chain request that is executed by the other channel participants as well + // and the Adjudicator backend may run an optimized on-chain transaction + // protocol, possibly saving unnecessary double sending of transactions. AdjudicatorReq struct { - Params *Params - Acc wallet.Account - Tx Transaction - Idx Index // Always the own index + Params *Params + Acc wallet.Account + Tx Transaction + Idx Index // Always the own index + Secondary bool // Optimized secondary call protocol } // SignedState represents a signed channel state including parameters. diff --git a/channel/machine.go b/channel/machine.go index 7d836b2a5..0175a2067 100644 --- a/channel/machine.go +++ b/channel/machine.go @@ -249,6 +249,9 @@ func (m *machine) CurrentTX() Transaction { // AdjudicatorReq returns the adjudicator request for the current channel // transaction (the current state together with all participants' signatures on // it). +// +// The Secondary flag is left as false. Set it manually after creating the +// request if you want to use optimized sencondary adjudication logic. func (m *machine) AdjudicatorReq() AdjudicatorReq { return AdjudicatorReq{ Params: &m.params, @@ -335,8 +338,9 @@ func (m *machine) EnableFinal() error { } // enableStaged checks that -// 1. the current phase is `expected.From` and -// 2. all signatures of the staging transactions have been set. +// 1. the current phase is `expected.From` and +// 2. all signatures of the staging transactions have been set. +// // If successful, the staging transaction is promoted to be the current // transaction. If not, an error is returned. func (m *machine) enableStaged(expected PhaseTransition) error { diff --git a/client/adjudicate.go b/client/adjudicate.go index 31cff0431..7cdd616f2 100644 --- a/client/adjudicate.go +++ b/client/adjudicate.go @@ -242,7 +242,7 @@ func (c *Channel) ForceUpdate(ctx context.Context, updater func(*channel.State)) // to be mined. // Returns ChainNotReachableError if the connection to the blockchain network // fails when sending a transaction to / reading from the blockchain. -func (c *Channel) Settle(ctx context.Context) (err error) { +func (c *Channel) Settle(ctx context.Context, secondary bool) (err error) { if !c.State().IsFinal { err := c.ensureRegistered(ctx) if err != nil { @@ -268,7 +268,7 @@ func (c *Channel) Settle(ctx context.Context) (err error) { } // Withdraw. - err = c.withdraw(ctx) + err = c.withdraw(ctx, secondary) if err != nil { return } @@ -303,7 +303,7 @@ func (c *Channel) Settle(ctx context.Context) (err error) { return nil } -func (c *Channel) withdraw(ctx context.Context) error { +func (c *Channel) withdraw(ctx context.Context, secondary bool) error { switch { case c.IsLedgerChannel(): subStates, err := c.subChannelStateMap() @@ -311,6 +311,7 @@ func (c *Channel) withdraw(ctx context.Context) error { return errors.WithMessage(err, "creating sub-channel state map") } req := c.machine.AdjudicatorReq() + req.Secondary = secondary if err := c.adjudicator.Withdraw(ctx, req, subStates); err != nil { return errors.WithMessage(err, "calling Withdraw") } diff --git a/client/test/bob.go b/client/test/bob.go index b554cde8b..de215f6bf 100644 --- a/client/test/bob.go +++ b/client/test/bob.go @@ -57,7 +57,7 @@ func (r *Bob) exec(_cfg ExecConfig, ch *paymentChannel, propHandler *acceptNextP ch.sendFinal() // 4th Settle channel - ch.settle() + ch.settleSecondary() // 4th final stage r.waitStage() diff --git a/client/test/channel.go b/client/test/channel.go index a0b0187a4..ff023626b 100644 --- a/client/test/channel.go +++ b/client/test/channel.go @@ -168,10 +168,18 @@ func (ch *paymentChannel) recvFinal() { } func (ch *paymentChannel) settle() { + ch.settleImpl(false) +} + +func (ch *paymentChannel) settleSecondary() { + ch.settleImpl(true) +} + +func (ch *paymentChannel) settleImpl(secondary bool) { ctx, cancel := context.WithTimeout(context.Background(), ch.r.timeout) defer cancel() - ch.r.RequireNoError(ch.Settle(ctx)) + ch.r.RequireNoError(ch.Settle(ctx, secondary)) ch.assertBals(ch.State()) if ch.IsSubChannel() { diff --git a/client/test/fund.go b/client/test/fund.go index 355553efd..76a15900d 100644 --- a/client/test/fund.go +++ b/client/test/fund.go @@ -129,7 +129,7 @@ func runFredFridaTest( require.IsType(t, &client.ChannelFundingError{}, err) require.NotNil(t, chFrida) // Frida settles the channel. - require.NoError(t, chFrida.Settle(ctx)) + require.NoError(t, chFrida.Settle(ctx, false)) // Fred gets the channel and settles it afterwards. chFred := <-chsFred @@ -141,7 +141,7 @@ func runFredFridaTest( require.NoError(t, ctx.Err()) } // Fred settles the channel. - require.NoError(t, chFred.Settle(ctx)) + require.NoError(t, chFred.Settle(ctx, false)) // Test the final balances. balancesAfter := channel.Balances{ diff --git a/client/test/multiledger_dispute.go b/client/test/multiledger_dispute.go index 0d6febc3b..b153f4435 100644 --- a/client/test/multiledger_dispute.go +++ b/client/test/multiledger_dispute.go @@ -31,6 +31,7 @@ import ( // TestMultiLedgerDispute runs an end-to-end test of the multi-ledger // functionality in the dispute case for the implementation specified in the // test setup. +// //nolint:revive // test.Test... stutters but this is OK in this special case. func TestMultiLedgerDispute( ctx context.Context, @@ -127,9 +128,9 @@ func TestMultiLedgerDispute( require.NoError(err) // Settle. - err = chAliceBob.Settle(ctx) + err = chAliceBob.Settle(ctx, false) require.NoError(err) - err = chBobAlice.Settle(ctx) + err = chBobAlice.Settle(ctx, false) require.NoError(err) // Close the channels. diff --git a/client/test/multiledger_happy.go b/client/test/multiledger_happy.go index f8958c63b..9ec8c021b 100644 --- a/client/test/multiledger_happy.go +++ b/client/test/multiledger_happy.go @@ -27,6 +27,7 @@ import ( // TestMultiLedgerHappy runs an end-to-end test of the multi-ledger // functionality in the optimistic case for the implementation specified in the // test setup. +// //nolint:revive // test.Test... stutters but this is OK in this special case. func TestMultiLedgerHappy(ctx context.Context, t *testing.T, mlt MultiLedgerSetup, challengeDuration uint64) { require := require.New(t) @@ -90,8 +91,8 @@ func TestMultiLedgerHappy(ctx context.Context, t *testing.T, mlt MultiLedgerSetu require.NoError(err) // Close channel. - err = chAliceBob.Settle(ctx) + err = chAliceBob.Settle(ctx, false) require.NoError(err) - err = chBobAlice.Settle(ctx) + err = chBobAlice.Settle(ctx, false) require.NoError(err) } diff --git a/client/test/persistence.go b/client/test/persistence.go index 726119e8d..d44a12fda 100644 --- a/client/test/persistence.go +++ b/client/test/persistence.go @@ -125,7 +125,7 @@ func (r *Petra) Execute(cfg ExecConfig) { // 6. Finalize restored channel ch.recvFinal() - ch.settle() + ch.settleSecondary() r.RequireNoError(r.Close()) } diff --git a/client/test/progression.go b/client/test/progression.go index 33c399b1e..be498dd9c 100644 --- a/client/test/progression.go +++ b/client/test/progression.go @@ -116,7 +116,7 @@ func (r *Paul) exec(_cfg ExecConfig, ch *paymentChannel) { r.waitStage() // withdraw - r.RequireNoError(ch.Settle(ctx)) + r.RequireNoError(ch.Settle(ctx, false)) } // ----------------- BEGIN PAULA ----------------- @@ -181,5 +181,5 @@ func (r *Paula) exec(_cfg ExecConfig, ch *paymentChannel, _ *acceptNextPropHandl r.RequireNoErrorf(e.Timeout().Wait(ctx), "waiting for progression timeout") // withdraw - r.RequireNoError(ch.Settle(ctx)) + r.RequireNoError(ch.Settle(ctx, true)) } diff --git a/client/test/subchannel.go b/client/test/subchannel.go index 247c39394..3480751d5 100644 --- a/client/test/subchannel.go +++ b/client/test/subchannel.go @@ -209,7 +209,7 @@ func (r *Tim) exec(_cfg ExecConfig, ledgerChannel *paymentChannel, propHandler * finalizeAndSettle := func(ch *paymentChannel) { ch.recvFinal() - ch.settle() + ch.settleSecondary() } for i, ch := range subChannels { diff --git a/client/test/virtualchannel.go b/client/test/virtualchannel.go index 31ceec45b..5d1449160 100644 --- a/client/test/virtualchannel.go +++ b/client/test/virtualchannel.go @@ -56,7 +56,7 @@ func TestVirtualChannelOptimistic( //nolint:revive // test.Test... stutters but success.Add(len(chs)) for _, ch := range chs { go func(ch *client.Channel) { - err := ch.Settle(ctx) + err := ch.Settle(ctx, false) if err != nil { vct.errs <- err return @@ -102,7 +102,7 @@ func TestVirtualChannelDispute( //nolint:revive // test.Test... stutters but OK // Settle the channels in a random order. for _, i := range rand.Perm(len(chs)) { - err := chs[i].Settle(ctx) + err := chs[i].Settle(ctx, false) assert.NoErrorf(err, "settle channel: %d", i) } diff --git a/watcher/local/watcher.go b/watcher/local/watcher.go index 97dfdfb8d..b74de505f 100644 --- a/watcher/local/watcher.go +++ b/watcher/local/watcher.go @@ -439,8 +439,9 @@ func makeSignedState(params *channel.Params, tx channel.Transaction) channel.Sig func makeAdjudicatorReq(params *channel.Params, tx channel.Transaction) channel.AdjudicatorReq { return channel.AdjudicatorReq{ - Params: params, - Tx: tx, + Params: params, + Tx: tx, + Secondary: false, } }