Skip to content

Commit

Permalink
🚧 SubAlloc with participant index map
Browse files Browse the repository at this point in the history
Signed-off-by: Matthias Geihs <[email protected]>
  • Loading branch information
matthiasgeihs committed May 23, 2021
1 parent ea3e6de commit 9c316fd
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 32 deletions.
4 changes: 2 additions & 2 deletions backend/ethereum/channel/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func ToEthParams(p *channel.Params) adjudicator.ChannelParams {
func ToEthState(s *channel.State) adjudicator.ChannelState {
locked := make([]adjudicator.ChannelSubAlloc, len(s.Locked))
for i, sub := range s.Locked {
locked[i] = adjudicator.ChannelSubAlloc{ID: sub.ID, Balances: sub.Bals}
locked[i] = adjudicator.ChannelSubAlloc{ID: sub.ID, Balances: sub.Bals, Participants: []uint16{0: 0, 1: 1}}
}
outcome := adjudicator.ChannelAllocation{
Assets: assetToCommonAddresses(s.Allocation.Assets),
Expand Down Expand Up @@ -229,7 +229,7 @@ func pwToCommonAddresses(addr []wallet.Address) []common.Address {
func FromEthState(app channel.App, s *adjudicator.ChannelState) channel.State {
locked := make([]channel.SubAlloc, len(s.Outcome.Locked))
for i, sub := range s.Outcome.Locked {
locked[i] = channel.SubAlloc{ID: sub.ID, Bals: sub.Balances}
locked[i] = *channel.NewSubAlloc(sub.ID, sub.Balances)
}
alloc := channel.Allocation{
Assets: fromEthAssets(s.Outcome.Assets),
Expand Down
16 changes: 2 additions & 14 deletions backend/ethereum/channel/conclude_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,9 @@ func TestAdjudicator_ConcludeWithSubChannels(t *testing.T) {
)
// create channels
var (
ledgerChannel = makeRandomChannel(rng)
subChannels = makeRandomChannels(rng, maxCountSubChannels, makeRandomChannel)
subSubChannels = makeRandomChannels(rng, maxCountSubSubChannels, makeRandomChannel)
ledgerChannel = makeRandomChannel(rng)
subChannels = makeRandomChannels(rng, maxCountSubChannels, makeRandomChannel)
)
// update sub-channel locked funds
parentChannel := randomChannel(rng, subChannels)
for _, c := range subSubChannels {
parentChannel.state.AddSubAlloc(*c.state.ToSubAlloc())
}
// update ledger channel locked funds
for _, c := range subChannels {
ledgerChannel.state.AddSubAlloc(*c.state.ToSubAlloc())
Expand All @@ -141,7 +135,6 @@ func TestAdjudicator_ConcludeWithSubChannels(t *testing.T) {

// 1. register channels

require.NoError(register(ctx, adj, accounts, subSubChannels...))
require.NoError(register(ctx, adj, accounts, subChannels...))
require.NoError(register(ctx, adj, accounts, ledgerChannel))

Expand All @@ -157,7 +150,6 @@ func TestAdjudicator_ConcludeWithSubChannels(t *testing.T) {
subStates := channel.MakeStateMap()
addSubStates(subStates, ledgerChannel)
addSubStates(subStates, subChannels...)
addSubStates(subStates, subSubChannels...)

assert.NoError(withdraw(ctx, adj, accounts, ledgerChannel, subStates))
}
Expand Down Expand Up @@ -188,10 +180,6 @@ func makeRandomChannels(rng *rand.Rand, maxCount int, makeRandomChannel func(rng
return channels
}

func randomChannel(rng *rand.Rand, channels []paramsAndState) *paramsAndState {
return &channels[rng.Intn(len(channels))]
}

func fund(ctx context.Context, funders []*ethchannel.Funder, c paramsAndState) error {
errg := errors.NewGatherer()
for i, funder := range funders {
Expand Down
35 changes: 31 additions & 4 deletions channel/allocation.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,9 @@ type (
// The size of the balances slice must be of the same size as the assets slice
// of the channel Params.
SubAlloc struct {
ID ID
Bals []Bal
ID ID
Bals []Bal
Parts []Index
}

// Bal is a single asset's balance.
Expand Down Expand Up @@ -442,7 +443,7 @@ func (b Balances) Sum() []Bal {

// NewSubAlloc creates a new sub-allocation.
func NewSubAlloc(id ID, bals []Bal) *SubAlloc {
return &SubAlloc{ID: id, Bals: bals}
return &SubAlloc{ID: id, Bals: bals, Parts: []uint16{0: 0, 1: 1}}
}

// SubAlloc tries to return the sub-allocation for the given subchannel.
Expand Down Expand Up @@ -548,6 +549,17 @@ func (s SubAlloc) Encode(w io.Writer) error {
err, "encoding balance of participant %d", i)
}
}
// encode parts
if err := perunio.Encode(w, Index(len(s.Parts))); err != nil {
return errors.WithMessage(
err, "encoding participants length")
}
for i, part := range s.Parts {
if err := perunio.Encode(w, part); err != nil {
return errors.WithMessagef(
err, "encoding participant index %d", i)
}
}

return nil
}
Expand All @@ -568,7 +580,22 @@ func (s *SubAlloc) Decode(r io.Reader) error {
for i := range s.Bals {
if err := perunio.Decode(r, &s.Bals[i]); err != nil {
return errors.WithMessagef(
err, "encoding participant balance %d", i)
err, "decoding participant balance %d", i)
}
}
// decode parts
var numParts Index
if err := perunio.Decode(r, &numParts); err != nil {
return errors.WithMessage(err, "decoding sub-allocation participants length")
}
if numParts > MaxNumParts {
return errors.Errorf("numParts too big, got: %d max: %d", numParts, MaxNumParts)
}
s.Parts = make([]Index, numParts)
for i := range s.Parts {
if err := perunio.Decode(r, &s.Parts[i]); err != nil {
return errors.WithMessagef(
err, "decoding participant index %d", i)
}
}

Expand Down
25 changes: 15 additions & 10 deletions channel/allocation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,14 @@ func TestAllocationValidLimits(t *testing.T) {
}

for i := range allocation.Locked {
allocation.Locked[i] = channel.SubAlloc{
ID: channel.ID{byte(i), byte(i >> 8), byte(i >> 16), byte(i >> 24)},
Bals: make([]channel.Bal, x.numAssets)}
allocation.Locked[i] = *channel.NewSubAlloc(
channel.ID{byte(i), byte(i >> 8), byte(i >> 16), byte(i >> 24)},
make([]channel.Bal, x.numAssets),
)
allocation.Locked[i] = *channel.NewSubAlloc(
channel.ID{byte(i), byte(i >> 8), byte(i >> 16), byte(i >> 24)},
make([]channel.Bal, x.numAssets),
)

for j := range allocation.Locked[i].Bals {
bal := big.NewInt(int64(x.numAssets)*int64(i) + int64(j) + 1)
Expand Down Expand Up @@ -372,7 +377,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.SubAlloc{Bals: []channel.Bal{big.NewInt(2)}}), 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)})), test.WithBalancesInRange(big.NewInt(1), big.NewInt(1))),
[]channel.Bal{big.NewInt(3)},
},

Expand Down Expand Up @@ -485,7 +490,7 @@ func TestAllocation_Valid(t *testing.T) {
{big.NewInt(64), big.NewInt(128)},
},
Locked: []channel.SubAlloc{
{channel.Zero, []channel.Bal{big.NewInt(4)}},
*channel.NewSubAlloc(channel.Zero, []channel.Bal{big.NewInt(4)}),
},
},
false,
Expand All @@ -501,7 +506,7 @@ func TestAllocation_Valid(t *testing.T) {
{big.NewInt(64), big.NewInt(128)},
},
Locked: []channel.SubAlloc{
{channel.Zero, []channel.Bal{big.NewInt(-1)}},
*channel.NewSubAlloc(channel.Zero, []channel.Bal{big.NewInt(-1)}),
},
},
false,
Expand Down Expand Up @@ -529,7 +534,7 @@ func TestAllocation_Valid(t *testing.T) {
{big.NewInt(2), big.NewInt(16)},
},
Locked: []channel.SubAlloc{
{channel.Zero, []channel.Bal{big.NewInt(4), big.NewInt(-1)}},
*channel.NewSubAlloc(channel.Zero, []channel.Bal{big.NewInt(4), big.NewInt(-1)}),
},
},
false,
Expand All @@ -549,9 +554,9 @@ func TestAllocation_Valid(t *testing.T) {
// suballocation serialization.
func TestSuballocSerialization(t *testing.T) {
ss := []perunio.Serializer{
&channel.SubAlloc{channel.ID{2}, []channel.Bal{}},
&channel.SubAlloc{channel.ID{3}, []channel.Bal{big.NewInt(0)}},
&channel.SubAlloc{channel.ID{4}, []channel.Bal{big.NewInt(5), big.NewInt(1 << 62)}},
channel.NewSubAlloc(channel.ID{2}, []channel.Bal{}),
channel.NewSubAlloc(channel.ID{3}, []channel.Bal{big.NewInt(0)}),
channel.NewSubAlloc(channel.ID{4}, []channel.Bal{big.NewInt(5), big.NewInt(1 << 62)}),
}

iotest.GenericSerializerTest(t, ss...)
Expand Down
2 changes: 1 addition & 1 deletion channel/test/randomizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func NewRandomSubAlloc(rng *rand.Rand, opts ...RandomOpt) *channel.SubAlloc {
bals = NewRandomBals(rng, opt.NumAssets(rng), opt)
}

return &channel.SubAlloc{ID: id, Bals: bals}
return channel.NewSubAlloc(id, bals)
}

// NewRandomParamsAndState generates a new random `channel.Params` and `channel.State`.
Expand Down
2 changes: 1 addition & 1 deletion client/subchannel.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (c *Channel) withdrawSubChannel(ctx context.Context, sub *Channel) error {

func (c *Channel) registerSubChannelFunding(id channel.ID, alloc []channel.Bal) {
filter := func(cu ChannelUpdate) bool {
expected := channel.SubAlloc{ID: id, Bals: alloc}
expected := *channel.NewSubAlloc(id, alloc)
_, containedBefore := c.machine.State().SubAlloc(expected.ID)
subAlloc, containedAfter := cu.State.SubAlloc(expected.ID)
return !containedBefore && containedAfter && expected.Equal(&subAlloc) == nil
Expand Down

0 comments on commit 9c316fd

Please sign in to comment.