Repository navigation
Fix multi-backend signature decoding during restore #438
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| // Copyright 2026 - See NOTICE file for copyright holders. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package channel_test | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
|
|
||
| _ "perun.network/go-perun/backend/sim/wallet" | ||
| "perun.network/go-perun/channel" | ||
| ctest "perun.network/go-perun/channel/test" | ||
| "perun.network/go-perun/wallet" | ||
| wallettest "perun.network/go-perun/wallet/test" | ||
| pkgtest "polycry.pt/poly-go/test" | ||
| ) | ||
|
|
||
| func TestTransactionDecDecodeUsesParticipantBackends(t *testing.T) { | ||
| rng := pkgtest.Prng(t) | ||
| accs, addrs := wallettest.NewRandomAccounts(rng, 2, channel.TestBackendID) | ||
| params := ctest.NewRandomParams(rng, ctest.WithParts(addrs)) | ||
| state := ctest.NewRandomState(rng, ctest.WithID(params.ID()), ctest.WithNumParts(len(addrs))) | ||
|
|
||
| sigs := make([]wallet.Sig, len(addrs)) | ||
| for i := range addrs { | ||
| sig, err := channel.Sign(accs[i][channel.TestBackendID], state, channel.TestBackendID) | ||
| require.NoError(t, err) | ||
| sigs[i] = sig | ||
| } | ||
|
|
||
| var encoded bytes.Buffer | ||
| original := channel.Transaction{State: state, Sigs: sigs} | ||
| require.NoError(t, original.Encode(&encoded)) | ||
|
|
||
| var decoded channel.Transaction | ||
| require.NoError(t, (channel.TransactionDec{Tx: &decoded, Parts: addrs}).Decode(&encoded)) | ||
| require.Equal(t, original, decoded) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -47,13 +47,19 @@ const bitsPerByte = 8 | |
|
|
||
| // SigDec is a helper type to decode signatures. | ||
| type SigDec struct { | ||
| Sig *Sig | ||
| BackendID int | ||
| Sig *Sig | ||
| // BackendID optionally selects the backend-specific signature decoder. If it | ||
| // is nil, decoding falls back to the global multi-backend decoder. | ||
| BackendID *BackendID | ||
| } | ||
|
Comment on lines
49
to
54
|
||
|
|
||
| // Decode decodes a single signature. | ||
| func (s SigDec) Decode(r io.Reader) (err error) { | ||
| *s.Sig, err = DecodeSig(r) | ||
| if s.BackendID == nil { | ||
| *s.Sig, err = DecodeSig(r) | ||
| return err | ||
| } | ||
| *s.Sig, err = decodeSigForBackend(r, *s.BackendID) | ||
| return err | ||
| } | ||
|
|
||
|
|
@@ -86,6 +92,28 @@ func EncodeSparseSigs(w io.Writer, sigs []Sig) error { | |
|
|
||
| // DecodeSparseSigs decodes a collection of signatures in the form (mask, sig, ...). | ||
| func DecodeSparseSigs(r io.Reader, sigs *[]Sig) (err error) { | ||
| return decodeSparseSigs(r, sigs, func(r io.Reader, _ int) (Sig, error) { | ||
| return DecodeSig(r) | ||
| }) | ||
| } | ||
|
|
||
| // DecodeSparseSigsForParts decodes a sparse signature collection using the | ||
| // participant backend IDs when they are known. If a participant exposes zero or | ||
| // multiple backend IDs, it falls back to the global decoder. | ||
| func DecodeSparseSigsForParts(r io.Reader, sigs *[]Sig, parts []map[BackendID]Address) (err error) { | ||
| if len(*sigs) != len(parts) { | ||
| return errors.Errorf("signature/participant count mismatch: %d != %d", len(*sigs), len(parts)) | ||
| } | ||
|
|
||
| return decodeSparseSigs(r, sigs, func(r io.Reader, sigIdx int) (Sig, error) { | ||
| if backendID, ok := SingleBackendID(parts[sigIdx]); ok { | ||
| return decodeSigForBackend(r, backendID) | ||
| } | ||
| return DecodeSig(r) | ||
| }) | ||
| } | ||
|
|
||
| func decodeSparseSigs(r io.Reader, sigs *[]Sig, decoder func(io.Reader, int) (Sig, error)) (err error) { | ||
| masklen := int(math.Ceil(float64(len(*sigs)) / float64(bitsPerByte))) | ||
| mask := make([]uint8, masklen) | ||
|
|
||
|
|
@@ -100,11 +128,11 @@ func DecodeSparseSigs(r io.Reader, sigs *[]Sig) (err error) { | |
| for bitIdx := 0; bitIdx < bitsPerByte && sigIdx < len(*sigs); bitIdx, sigIdx = bitIdx+1, sigIdx+1 { | ||
| if ((mask[maskIdx] >> bitIdx) % binaryModulo) == 0 { | ||
| (*sigs)[sigIdx] = nil | ||
| } else { | ||
| (*sigs)[sigIdx], err = DecodeSig(r) | ||
| if err != nil { | ||
| return errors.WithMessagef(err, "decoding signature %d", sigIdx) | ||
| } | ||
| continue | ||
| } | ||
| (*sigs)[sigIdx], err = decoder(r, sigIdx) | ||
| if err != nil { | ||
| return errors.WithMessagef(err, "decoding signature %d", sigIdx) | ||
| } | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
readNextValueusesstrings.HasSuffix(actual, key)to validate the iterator key. This can incorrectly accept unexpected keys that merely end with the expected key (e.g.,...:mycurrentwould pass forkey="current"), weakening the new guard.Consider additionally checking that the byte immediately preceding the suffix is the channel DB separator (":"), or otherwise validating the exact key format produced by
channelDB(...)+":"+key.