Skip to content

Commit

Permalink
🔀 Merge pull request #114 from perun-network/prepare-82-virtual-channels
Browse files Browse the repository at this point in the history
Prepare 82 virtual channels
  • Loading branch information
ggwpez authored Jun 22, 2021
2 parents c7113e1 + c05d177 commit c50c75f
Show file tree
Hide file tree
Showing 17 changed files with 113 additions and 81 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
- name: Lint
uses: golangci/golangci-lint-action@v2
with:
version: v1.29
version: v1.40.1

tests:
name: Tests
Expand Down
2 changes: 1 addition & 1 deletion backend/ethereum/channel/backend.go
Original file line number Diff line number Diff line change
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
1 change: 0 additions & 1 deletion backend/ethereum/channel/register_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
pkgtest "perun.network/go-perun/pkg/test"
)

// nolint: dupl
func TestAdjudicator_MultipleRegisters(t *testing.T) {
t.Run("Register 1 party parallel", func(t *testing.T) { registerMultipleConcurrent(t, 1, true) })
t.Run("Register 2 party parallel", func(t *testing.T) { registerMultipleConcurrent(t, 2, true) })
Expand Down
20 changes: 12 additions & 8 deletions backend/ethereum/channel/withdraw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package channel_test

import (
"context"
"fmt"
"sync"
"testing"
"time"
Expand All @@ -31,15 +32,18 @@ import (
pkgtest "perun.network/go-perun/pkg/test"
)

// nolint: dupl
func TestAdjudicator_MultipleWithdraws_FinalState(t *testing.T) {
t.Run("Withdraw 1 party parallel", func(t *testing.T) { withdrawMultipleConcurrentFinal(t, 1, true) })
t.Run("Withdraw 2 party parallel", func(t *testing.T) { withdrawMultipleConcurrentFinal(t, 2, true) })
t.Run("Withdraw 5 party parallel", func(t *testing.T) { withdrawMultipleConcurrentFinal(t, 5, true) })
t.Run("Withdraw 10 party parallel", func(t *testing.T) { withdrawMultipleConcurrentFinal(t, 10, true) })
t.Run("Withdraw 1 party sequential", func(t *testing.T) { withdrawMultipleConcurrentFinal(t, 1, false) })
t.Run("Withdraw 2 party sequential", func(t *testing.T) { withdrawMultipleConcurrentFinal(t, 2, false) })
t.Run("Withdraw 5 party sequential", func(t *testing.T) { withdrawMultipleConcurrentFinal(t, 5, false) })
testParallel := func(n int) {
t.Run(fmt.Sprintf("Withdraw %d party parallel", n), func(t *testing.T) { withdrawMultipleConcurrentFinal(t, n, true) })
}
testSequential := func(n int) {
t.Run(fmt.Sprintf("Withdraw %d party sequential", n), func(t *testing.T) { withdrawMultipleConcurrentFinal(t, n, false) })
}

for _, n := range []int{1, 2, 5} {
testParallel(n)
testSequential(n)
}
}

func withdrawMultipleConcurrentFinal(t *testing.T, numParts int, parallel bool) {
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
3 changes: 2 additions & 1 deletion channel/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ const (
Progressed
Withdrawing
Withdrawn
LastPhase = Withdrawn
// LastPhase contains the value of the last phase. This is useful for testing.
LastPhase = int(Withdrawn)
)

func (p Phase) String() string {
Expand Down
3 changes: 3 additions & 0 deletions channel/persistence/keyvalue/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type channelCache struct {
peerChannels map[string]map[channel.ID]struct{} // Address -> Set<chID>
}

// nolint: unused
func (c *channelCache) addPeerChannel(addr wire.Address, chID channel.ID) {
c.mutex.Lock()
defer c.mutex.Unlock()
Expand All @@ -53,6 +54,7 @@ func (c *channelCache) addPeerChannel(addr wire.Address, chID channel.ID) {
}
}

// nolint: unused
func (c *channelCache) deleteChannel(id channel.ID) []wire.Address {
c.mutex.Lock()
defer c.mutex.Unlock()
Expand All @@ -71,6 +73,7 @@ func (c *channelCache) deleteChannel(id channel.ID) []wire.Address {
return peers
}

// nolint: unused
func (c *channelCache) clear() {
c.mutex.Lock()
defer c.mutex.Unlock()
Expand Down
2 changes: 1 addition & 1 deletion channel/persistence/test/persistrestorertest.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func GenericPersistRestorerTest(
chIndex := iterIdx
log.Error(subSeed)
seed := pkgtest.Seed("", subSeed, numChans, numPeers, chIndex, ch.ID())
rng := rand.New(rand.NewSource(seed))
rng := rand.New(rand.NewSource(seed)) // nolint: gosec

ch.Init(t, rng)
ch.SignAll(t)
Expand Down
4 changes: 2 additions & 2 deletions channel/test/randomizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func SetRandomizer(r Randomizer) {

// NewRandomPhase generates a random channel machine phase.
func NewRandomPhase(rng *rand.Rand) channel.Phase {
return channel.Phase(rng.Intn(int(channel.LastPhase) + 1))
return channel.Phase(rng.Intn(channel.LastPhase + 1))
}

// NewRandomAsset generates a new random `channel.Asset`.
Expand Down 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
File renamed without changes.
14 changes: 2 additions & 12 deletions client/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,7 @@ func (c *Client) validTwoPartyProposal(
ourIdx int,
peerAddr wallet.Address,
) error {
base := proposal.Base()
if err := base.Valid(); err != nil {
if err := proposal.Valid(); err != nil {
return err
}

Expand Down Expand Up @@ -371,15 +370,6 @@ func (c *Client) validChannelProposalAcc(
return errors.Errorf("mismatched proposal ID %b and accept ID %b", propID, accID)
}

if proposal.Type() == wire.SubChannelProposal {
subProp := proposal.(*SubChannelProposal)

_, ok := c.channels.Get(subProp.Parent)
if !ok {
return errors.New("parent channel does not exist")
}
}

return nil
}

Expand Down Expand Up @@ -602,7 +592,7 @@ type version1Cache struct {
type cachedUpdate struct {
uh UpdateHandler
p wire.Address
m *msgChannelUpdate
m ChannelUpdateProposal
}

// Error implements the error interface.
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
4 changes: 2 additions & 2 deletions client/test/carol.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ func (r *Carol) exec(_cfg ExecConfig, ch *paymentChannel, propHandler *acceptNex
// start watcher
go func() {
r.log.Info("Starting channel watcher.")
assert.NoError(ch.Watch(r))
r.log.Debug("Channel watcher returned.")
err := ch.Watch(r)
r.log.Infof("Channel watcher returned: %v", err)
}()

// 1st stage - channel controller set up
Expand Down
27 changes: 15 additions & 12 deletions client/test/progression.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/stretchr/testify/assert"

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

// ProgressionExecConfig contains config parameters for the progression test.
Expand All @@ -30,22 +31,22 @@ type ProgressionExecConfig struct {

// Watcher is a client that handles adjudicator events.
type Watcher struct {
t *testing.T
log.Logger
registered chan *channel.RegisteredEvent
progressed chan *channel.ProgressedEvent
}

func makeWatcher(t *testing.T) Watcher {
func makeWatcher(log log.Logger) Watcher {
return Watcher{
t: t,
Logger: log,
registered: make(chan *channel.RegisteredEvent),
progressed: make(chan *channel.ProgressedEvent),
}
}

// HandleAdjudicatorEvent is the callback for adjudicator event handling.
func (w *Watcher) HandleAdjudicatorEvent(e channel.AdjudicatorEvent) {
w.t.Logf("HandleAdjudicatorEvent: %v", e)
w.Infof("HandleAdjudicatorEvent: %v", e)
switch e := e.(type) {
case *channel.RegisteredEvent:
w.registered <- e
Expand All @@ -64,9 +65,10 @@ type Paul struct {

// NewPaul creates a new party that executes the Paul protocol.
func NewPaul(t *testing.T, setup RoleSetup) *Paul {
p := NewProposer(setup, t, 1)
return &Paul{
Proposer: *NewProposer(setup, t, 1),
Watcher: makeWatcher(t),
Proposer: *p,
Watcher: makeWatcher(p.log),
}
}

Expand All @@ -83,8 +85,8 @@ func (r *Paul) exec(_cfg ExecConfig, ch *paymentChannel) {
// start watcher
go func() {
r.log.Info("Starting channel watcher.")
assert.NoError(ch.Watch(r))
r.log.Debug("Channel watcher returned.")
err := ch.Watch(r)
r.log.Infof("Channel watcher returned: %v", err)
}()

r.waitStage() // wait for setup complete
Expand Down Expand Up @@ -130,9 +132,10 @@ type Paula struct {

// NewPaula creates a new party that executes the Paula protocol.
func NewPaula(t *testing.T, setup RoleSetup) *Paula {
r := NewResponder(setup, t, 1)
return &Paula{
Responder: *NewResponder(setup, t, 1),
Watcher: makeWatcher(t),
Responder: *r,
Watcher: makeWatcher(r.log),
}
}

Expand All @@ -149,8 +152,8 @@ func (r *Paula) exec(_cfg ExecConfig, ch *paymentChannel, _ *acceptNextPropHandl
// start watcher
go func() {
r.log.Info("Starting channel watcher.")
assert.NoError(ch.Watch(r))
r.log.Debug("Channel watcher returned.")
err := ch.Watch(r)
r.log.Infof("Channel watcher returned: %v", err)
}()

r.waitStage() // wait for setup complete
Expand Down
Loading

0 comments on commit c50c75f

Please sign in to comment.