Skip to content
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

Remove channelID map #413

Merged
merged 47 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
66fee75
feat(wallet, channel): Change backend to map of backends
sophia1ch Aug 21, 2024
28685ea
fix(multi): Add AssetIDKey struct to fix the map lookups for funders …
sophia1ch Aug 28, 2024
ef11de9
fix(all): Use map[int]ID instead of id for each backend.
sophia1ch Sep 4, 2024
ab8e60b
fix(all): Add map of IDs
sophia1ch Sep 4, 2024
13eeddf
fix(channel/backend): Revert change in verify.
sophia1ch Sep 5, 2024
8181cb0
fix(Adjudicator): Change to singular channel ID in events.
sophia1ch Sep 11, 2024
794298a
fix(client/test): Generalize role test.
sophia1ch Sep 11, 2024
969c6a7
feat(channel): Add backend field check.
sophia1ch Sep 20, 2024
10c9b28
fix(subchannel_dispute): Generalize event watcher.
sophia1ch Sep 25, 2024
3c1e261
feat: Asset Address function to handle encoding in backends.
sophia1ch Oct 1, 2024
a695d14
feat(apps/payment, backend/sim, channel/test, client/test, log, walle…
sophia1ch Oct 15, 2024
8589eb8
fix(asset.go, allocation.go): Adjust Address() to return byte[] inste…
sophia1ch Oct 29, 2024
39095c1
chore: Remove unnecessary logs
sophia1ch Nov 6, 2024
98e11ea
fix: Remove repeated assetIDs in LedgerID array
sophia1ch Nov 27, 2024
46de2d9
chore: update go.sum, gofmt some files
sophia1ch Dec 18, 2024
dac83b9
Merge branch 'main' into feat-mult-backends
sophia1ch Dec 18, 2024
5e5bfb3
fix(wallet/backend.go): Return just one error instead of concatenatio…
sophia1ch Dec 19, 2024
70036df
fix(golangci.yml): Exclude generated file from linter
sophia1ch Dec 19, 2024
19a792f
fix(ci): Use go1.18 for the any type in proto
sophia1ch Dec 19, 2024
fd8bf8a
fix(ci): Use glinter 1.45 which supports go1.18
sophia1ch Dec 19, 2024
98b900b
chore(all): Add nolint flags for conversions
sophia1ch Dec 19, 2024
2f24516
chore(all): remove unnecessary flags
sophia1ch Dec 19, 2024
8622a49
chore(all): remove unnecessary flags
sophia1ch Dec 19, 2024
caa8d4c
chore(all): remove unnecessary flags
sophia1ch Dec 19, 2024
b272564
chore(all): remove unnecessary flags
sophia1ch Dec 19, 2024
2c079f1
chore(all): remove unnecessary flags
sophia1ch Dec 19, 2024
ced8fb5
chore(all): remove unnecessary flags
sophia1ch Dec 19, 2024
056e726
chore(all): remove unnecessary flags
sophia1ch Dec 19, 2024
d73ca03
chore(all): Rename and add comments to exported functions
sophia1ch Dec 19, 2024
2d98975
chore: Rename variables
sophia1ch Dec 23, 2024
d463e82
fix(params, address): Fix index allocation
sophia1ch Dec 23, 2024
81fe219
Revert "fix(all): Add map of IDs"
sophia1ch Jan 7, 2025
3ead503
fix(backend.go): Remove unnecessary errors.join()
sophia1ch Jan 14, 2025
f46b0c5
fix: remove unnecessary else statement, add linter flag
sophia1ch Jan 14, 2025
7169c29
refactor(/apps/payment, /sim/wallet, /channel, /client, /wallet, /wir…
sophia1ch Jan 20, 2025
d899ea0
chore: Update proto binding, remove IDMap
sophia1ch Jan 20, 2025
c1fe0e2
refactor: Name TestBackendID, rename multiLedgerID to LedgerBackendID
sophia1ch Jan 20, 2025
a6d4a4a
refactor: Fix comment on exported function
sophia1ch Jan 20, 2025
5219a0b
chore(all): Update license dates
sophia1ch Jan 28, 2025
07bf588
Merge branch 'refs/heads/feat-mult-backends' into remove-id-map
sophia1ch Jan 28, 2025
a404ea6
chore(backendtest): Rename to fix import issues
sophia1ch Jan 28, 2025
18c4a4a
Merge branch 'refs/heads/feat-mult-backends' into remove-id-map
sophia1ch Jan 28, 2025
21d0af4
fix(allocation_test): Remove channelID map from merge
sophia1ch Jan 28, 2025
180da66
Merge branch 'refs/heads/upstream-main' into remove-id-map
sophia1ch Feb 4, 2025
f33dbaf
fix(all): Fix errors from merge
sophia1ch Feb 4, 2025
56f6528
chore(client_role_test): format file
sophia1ch Feb 4, 2025
6a6add4
chore: fix merge errors
sophia1ch Feb 4, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions backend/sim/wallet/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ func NewRandomAddress(rng io.Reader) *Address {

// NewRandomAddresses creates a new address using the randomness
// provided by rng.
func NewRandomAddresses(rng io.Reader) map[int]wallet.Address {
func NewRandomAddresses(rng io.Reader) map[wallet.BackendID]wallet.Address {
privateKey, err := ecdsa.GenerateKey(curve, rng)
if err != nil {
log.Panicf("Creation of account failed with error", err)
}

return map[int]wallet.Address{channel.TestBackendID: &Address{
return map[wallet.BackendID]wallet.Address{channel.TestBackendID: &Address{
Curve: privateKey.Curve,
X: privateKey.X,
Y: privateKey.Y,
Expand Down
9 changes: 4 additions & 5 deletions channel/adjudicator.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ type (
// subscription should be closed by calling Close on the subscription after
// the channel is closed.
EventSubscriber interface {
Subscribe(context.Context, map[wallet.BackendID]ID) (AdjudicatorSubscription, error)
Subscribe(context.Context, ID) (AdjudicatorSubscription, error)
}

// An AdjudicatorReq collects all necessary information to make calls to the
Expand Down Expand Up @@ -204,7 +204,7 @@ type (
}

// StateMap represents a channel state tree.
StateMap map[string]*State
StateMap map[ID]*State
)

// NewProgressReq creates a new ProgressReq object.
Expand Down Expand Up @@ -302,13 +302,12 @@ func (t *TimeTimeout) String() string {

// MakeStateMap creates a new StateMap object.
func MakeStateMap() StateMap {
return make(map[string]*State)
return make(map[ID]*State)
}

// Add adds the given states to the state map.
func (m StateMap) Add(states ...*State) {
for _, s := range states {
key := IDKey(s.ID)
m[key] = s
m[s.ID] = s
}
}
14 changes: 7 additions & 7 deletions channel/allocation.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ type (
// The size of the balances slice must be of the same size as the assets slice
// of the channel Params.
SubAlloc struct {
ID map[wallet.BackendID]ID
ID ID
Bals []Bal
IndexMap []Index // Maps participant indices of the sub-channel to participant indices of the parent channel.
}
Expand Down Expand Up @@ -552,7 +552,7 @@ func (b Balances) Sum() []Bal {
}

// NewSubAlloc creates a new sub-allocation.
func NewSubAlloc(id map[wallet.BackendID]ID, bals []Bal, indexMap []Index) *SubAlloc {
func NewSubAlloc(id ID, bals []Bal, indexMap []Index) *SubAlloc {
if indexMap == nil {
indexMap = []Index{}
}
Expand All @@ -561,9 +561,9 @@ func NewSubAlloc(id map[wallet.BackendID]ID, bals []Bal, indexMap []Index) *SubA

// SubAlloc tries to return the sub-allocation for the given subchannel.
// The second return value indicates success.
func (a Allocation) SubAlloc(subchannel map[wallet.BackendID]ID) (subAlloc SubAlloc, ok bool) {
func (a Allocation) SubAlloc(subchannel ID) (subAlloc SubAlloc, ok bool) {
for _, subAlloc = range a.Locked {
if EqualIDs(subAlloc.ID, subchannel) {
if subAlloc.ID == subchannel {
ok = true
return
}
Expand Down Expand Up @@ -667,7 +667,7 @@ func (s SubAlloc) Encode(w io.Writer) error {
err, "invalid sub-allocations cannot be encoded, got %v", s)
}
// encode ID and dimension
if err := perunio.Encode(w, IDMap(s.ID), Index(len(s.Bals))); err != nil {
if err := perunio.Encode(w, s.ID, Index(len(s.Bals))); err != nil {
return errors.WithMessagef(
err, "encoding sub-allocation ID or dimension, id %v", s.ID)
}
Expand Down Expand Up @@ -696,7 +696,7 @@ func (s SubAlloc) Encode(w io.Writer) error {
func (s *SubAlloc) Decode(r io.Reader) error {
var numAssets Index
// decode ID and dimension
if err := perunio.Decode(r, (*IDMap)(&s.ID), &numAssets); err != nil {
if err := perunio.Decode(r, &s.ID, &numAssets); err != nil {
return errors.WithMessage(err, "decoding sub-allocation ID or dimension")
}
if numAssets > MaxNumAssets {
Expand Down Expand Up @@ -731,7 +731,7 @@ func (s *SubAlloc) Equal(t *SubAlloc) error {
if s == t {
return nil
}
if !EqualIDs(s.ID, t.ID) {
if s.ID != t.ID {
return errors.New("different ID")
}
if !s.BalancesEqual(t.Bals) {
Expand Down
18 changes: 9 additions & 9 deletions channel/allocation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,12 +291,12 @@ func TestAllocationValidLimits(t *testing.T) {

for i := range allocation.Locked {
allocation.Locked[i] = *channel.NewSubAlloc(
map[wallet.BackendID]channel.ID{channel.TestBackendID: {byte(i), byte(i >> 8), byte(i >> 16), byte(i >> 24)}},
channel.ID{byte(i), byte(i >> 8), byte(i >> 16), byte(i >> 24)},
make([]channel.Bal, x.numAssets),
nil,
)
allocation.Locked[i] = *channel.NewSubAlloc(
map[wallet.BackendID]channel.ID{channel.TestBackendID: {byte(i), byte(i >> 8), byte(i >> 16), byte(i >> 24)}},
channel.ID{byte(i), byte(i >> 8), byte(i >> 16), byte(i >> 24)},
make([]channel.Bal, x.numAssets),
nil,
)
Expand Down Expand Up @@ -387,7 +387,7 @@ func TestAllocation_Sum(t *testing.T) {

{
"single asset/one participants/one locked",
*test.NewRandomAllocation(rng, test.WithNumAssets(1), test.WithNumParts(1), test.WithLocked(*channel.NewSubAlloc(map[wallet.BackendID]channel.ID{}, []channel.Bal{big.NewInt(2)}, nil)), test.WithBalancesInRange(big.NewInt(1), big.NewInt(1))),
*test.NewRandomAllocation(rng, test.WithNumAssets(1), test.WithNumParts(1), test.WithLocked(*channel.NewSubAlloc(channel.ID{}, []channel.Bal{big.NewInt(2)}, nil)), test.WithBalancesInRange(big.NewInt(1), big.NewInt(1))),
[]channel.Bal{big.NewInt(3)},
},

Expand Down Expand Up @@ -500,7 +500,7 @@ func TestAllocation_Valid(t *testing.T) {
{big.NewInt(64), big.NewInt(128)},
},
Locked: []channel.SubAlloc{
*channel.NewSubAlloc(map[wallet.BackendID]channel.ID{channel.TestBackendID: channel.Zero}, []channel.Bal{big.NewInt(4)}, nil),
*channel.NewSubAlloc(channel.Zero, []channel.Bal{big.NewInt(4)}, nil),
},
},
false,
Expand All @@ -516,7 +516,7 @@ func TestAllocation_Valid(t *testing.T) {
{big.NewInt(64), big.NewInt(128)},
},
Locked: []channel.SubAlloc{
*channel.NewSubAlloc(map[wallet.BackendID]channel.ID{channel.TestBackendID: channel.Zero}, []channel.Bal{big.NewInt(-1)}, nil),
*channel.NewSubAlloc(channel.Zero, []channel.Bal{big.NewInt(-1)}, nil),
},
},
false,
Expand Down Expand Up @@ -544,7 +544,7 @@ func TestAllocation_Valid(t *testing.T) {
{big.NewInt(2), big.NewInt(16)},
},
Locked: []channel.SubAlloc{
*channel.NewSubAlloc(map[wallet.BackendID]channel.ID{channel.TestBackendID: channel.Zero}, []channel.Bal{big.NewInt(4), big.NewInt(-1)}, nil),
*channel.NewSubAlloc(channel.Zero, []channel.Bal{big.NewInt(4), big.NewInt(-1)}, nil),
},
},
false,
Expand All @@ -564,9 +564,9 @@ func TestAllocation_Valid(t *testing.T) {
// suballocation serialization.
func TestSuballocSerialization(t *testing.T) {
ss := []perunio.Serializer{
channel.NewSubAlloc(map[wallet.BackendID]channel.ID{channel.TestBackendID: {2}}, []channel.Bal{}, nil),
channel.NewSubAlloc(map[wallet.BackendID]channel.ID{channel.TestBackendID: {3}}, []channel.Bal{big.NewInt(0)}, nil),
channel.NewSubAlloc(map[wallet.BackendID]channel.ID{channel.TestBackendID: {4}}, []channel.Bal{big.NewInt(5), big.NewInt(1 << 62)}, nil),
channel.NewSubAlloc(channel.ID{2}, []channel.Bal{}, nil),
channel.NewSubAlloc(channel.ID{3}, []channel.Bal{big.NewInt(0)}, nil),
channel.NewSubAlloc(channel.ID{4}, []channel.Bal{big.NewInt(5), big.NewInt(1 << 62)}, nil),
}

peruniotest.GenericSerializerTest(t, ss...)
Expand Down
21 changes: 13 additions & 8 deletions channel/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,21 @@ func SetBackend(b Backend, id int) {
}

// CalcID calculates the CalcID.
func CalcID(p *Params) (map[wallet.BackendID]ID, error) {
id := make(map[wallet.BackendID]ID)
var err error
for i := range p.Parts[0] {
id[i], err = backend[i].CalcID(p)
if err != nil {
return nil, err
func CalcID(p *Params) (ID, error) {
var lastErr error
for _, b := range backend {
id, err := b.CalcID(p)
if err == nil {
return id, nil
}
lastErr = err
}
return id, nil

if lastErr != nil {
return ID{}, lastErr
}

return ID{}, errors.New("no valid ID found")
}

// Sign creates a signature from the account a on state s.
Expand Down
16 changes: 7 additions & 9 deletions channel/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,24 @@ package channel
import (
"fmt"

"perun.network/go-perun/wallet"

"github.com/pkg/errors"
)

type (
// StateTransitionError happens in case of an invalid channel state transition.
StateTransitionError struct {
ID map[wallet.BackendID]ID
ID ID
}

// ActionError happens if an invalid action is applied to a channel state.
ActionError struct {
ID map[wallet.BackendID]ID
ID ID
}

// PhaseTransitionError happens in case of an invalid channel machine phase
// transition.
PhaseTransitionError struct {
ID map[wallet.BackendID]ID
ID ID
current Phase
PhaseTransition
}
Expand All @@ -58,20 +56,20 @@ func (e *PhaseTransitionError) Error() string {
}

// NewStateTransitionError creates a new StateTransitionError.
func NewStateTransitionError(id map[wallet.BackendID]ID, msg string) error {
func NewStateTransitionError(id ID, msg string) error {
return errors.Wrap(&StateTransitionError{
ID: id,
}, msg)
}

// NewActionError creates a new ActionError.
func NewActionError(id map[wallet.BackendID]ID, msg string) error {
func NewActionError(id ID, msg string) error {
return errors.Wrap(&ActionError{
ID: id,
}, msg)
}

func newPhaseTransitionError(id map[wallet.BackendID]ID, current Phase, expected PhaseTransition, msg string) error {
func newPhaseTransitionError(id ID, current Phase, expected PhaseTransition, msg string) error {
return errors.Wrap(&PhaseTransitionError{
ID: id,
current: current,
Expand All @@ -80,7 +78,7 @@ func newPhaseTransitionError(id map[wallet.BackendID]ID, current Phase, expected
}

func newPhaseTransitionErrorf(
id map[wallet.BackendID]ID,
id ID,
current Phase,
expected PhaseTransition,
format string,
Expand Down
10 changes: 4 additions & 6 deletions channel/errors_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,19 @@ import (
"errors"
"testing"

"perun.network/go-perun/wallet"

"github.com/stretchr/testify/assert"
)

func TestTransitionErrors(t *testing.T) {
assert.False(t, IsStateTransitionError(errors.New("No StateTransitionError")))
assert.True(t, IsStateTransitionError(NewStateTransitionError(map[wallet.BackendID]ID{TestBackendID: Zero}, "A StateTransitionError")))
assert.True(t, IsStateTransitionError(NewStateTransitionError(Zero, "A StateTransitionError")))

assert.False(t, IsActionError(errors.New("No ActionError")))
assert.True(t, IsActionError(NewActionError(map[wallet.BackendID]ID{TestBackendID: Zero}, "An ActionError")))
assert.True(t, IsActionError(NewActionError(Zero, "An ActionError")))

assert.False(t, IsPhaseTransitionError(errors.New("No PhaseTransitionError")))
assert.True(t, IsPhaseTransitionError(newPhaseTransitionError(
map[wallet.BackendID]ID{TestBackendID: Zero}, InitActing, PhaseTransition{InitActing, InitActing}, "A PhaseTransitionError")))
Zero, InitActing, PhaseTransition{InitActing, InitActing}, "A PhaseTransitionError")))
assert.True(t, IsPhaseTransitionError(newPhaseTransitionErrorf(
map[wallet.BackendID]ID{TestBackendID: Zero}, InitActing, PhaseTransition{InitActing, InitActing}, "A %s", "PhaseTransitionError")))
Zero, InitActing, PhaseTransition{InitActing, InitActing}, "A %s", "PhaseTransitionError")))
}
16 changes: 8 additions & 8 deletions channel/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ type (
// needed for persistence. The ID, Idx and Params only need to be persisted
// once per channel as they stay constant during a channel's lifetime.
Source interface {
ID() map[wallet.BackendID]ID // ID is the channel ID of this source. It is the same as Params().ID().
Idx() Index // Idx is the own index in the channel.
Params() *Params // Params are the channel parameters.
StagingTX() Transaction // StagingTX is the staged transaction (State+incomplete list of sigs).
CurrentTX() Transaction // CurrentTX is the current transaction (State+complete list of sigs).
Phase() Phase // Phase is the phase in which the channel is currently in.
ID() ID // ID is the channel ID of this source. It is the same as Params().ID().
Idx() Index // Idx is the own index in the channel.
Params() *Params // Params are the channel parameters.
StagingTX() Transaction // StagingTX is the staged transaction (State+incomplete list of sigs).
CurrentTX() Transaction // CurrentTX is the current transaction (State+complete list of sigs).
Phase() Phase // Phase is the phase in which the channel is currently in.
}
)

Expand Down Expand Up @@ -169,7 +169,7 @@ func restoreMachine(acc map[wallet.BackendID]wallet.Account, source Source) (*ma
}

// ID returns the channel id.
func (m *machine) ID() map[wallet.BackendID]ID {
func (m *machine) ID() ID {
return m.params.ID()
}

Expand Down Expand Up @@ -488,7 +488,7 @@ func (m *machine) expect(tr PhaseTransition) error {
// A StateMachine will additionally check the validity of the app-specific
// transition whereas an ActionMachine checks each Action as being valid.
func (m *machine) ValidTransition(to *State) error {
if !EqualIDs(to.ID, m.params.id) {
if to.ID != m.params.id {
return errors.New("new state's ID doesn't match")
}

Expand Down
6 changes: 2 additions & 4 deletions channel/mock_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import (
"encoding/binary"
"fmt"

"perun.network/go-perun/wallet"

"github.com/pkg/errors"
)

Expand Down Expand Up @@ -170,9 +168,9 @@ func (a MockApp) execMockOp(op *MockOp) error {
case OpErr:
return errors.New("MockOp: runtime error")
case OpTransitionErr:
return NewStateTransitionError(map[wallet.BackendID]ID{}, "MockOp: state transition error")
return NewStateTransitionError(ID{}, "MockOp: state transition error")
case OpActionErr:
return NewActionError(map[wallet.BackendID]ID{}, "MockOp: action error")
return NewActionError(ID{}, "MockOp: action error")
case OpPanic:
panic("MockOp: panic")
case OpValid:
Expand Down
4 changes: 1 addition & 3 deletions channel/mock_app_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ package channel
import (
"testing"

"perun.network/go-perun/wallet"

"github.com/stretchr/testify/assert"

wiretest "perun.network/go-perun/wire/test"
Expand Down Expand Up @@ -143,5 +141,5 @@ func MockActionAppTest(t *testing.T, app MockApp) {
}

func createState(op MockOp) *State {
return &State{ID: map[wallet.BackendID]ID{}, Version: 0, Allocation: Allocation{}, Data: NewMockOp(op), IsFinal: false}
return &State{ID: ID{}, Version: 0, Allocation: Allocation{}, Data: NewMockOp(op), IsFinal: false}
}
3 changes: 1 addition & 2 deletions channel/multi/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ import (
"context"

"perun.network/go-perun/channel"
"perun.network/go-perun/wallet"
)

// Subscribe creates a new multi-ledger AdjudicatorSubscription.
func (a *Adjudicator) Subscribe(ctx context.Context, chID map[wallet.BackendID]channel.ID) (channel.AdjudicatorSubscription, error) {
func (a *Adjudicator) Subscribe(ctx context.Context, chID channel.ID) (channel.AdjudicatorSubscription, error) {
asub := &AdjudicatorSubscription{
events: make(chan channel.AdjudicatorEvent),
errors: make(chan error),
Expand Down
Loading