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

Use go-perun event and timeout types #11

Merged
merged 3 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
36 changes: 20 additions & 16 deletions channel/adjudicator.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 PolyCrypt GmbH
// Copyright 2024 PolyCrypt GmbH
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -32,27 +32,31 @@ import (

var ErrChannelAlreadyClosed = errors.New("channel is already closed")

var DefaultChallengeDuration = time.Duration(20) * time.Second

type Adjudicator struct {
log log.Embedding
StellarClient *client.Client
acc *wallet.Account
assetAddr xdr.ScAddress
perunAddr xdr.ScAddress
maxIters int
pollingInterval time.Duration
challengeDuration *time.Duration
log log.Embedding
StellarClient *client.Client
acc *wallet.Account
assetAddr xdr.ScAddress
perunAddr xdr.ScAddress
maxIters int
pollingInterval time.Duration
}

// NewAdjudicator returns a new Adjudicator.

func NewAdjudicator(acc *wallet.Account, stellarClient *client.Client, perunID xdr.ScAddress, assetID xdr.ScAddress) *Adjudicator {
return &Adjudicator{
StellarClient: stellarClient,
acc: acc,
perunAddr: perunID,
assetAddr: assetID,
maxIters: MaxIterationsUntilAbort,
pollingInterval: DefaultPollingInterval,
log: log.MakeEmbedding(log.Default()),
challengeDuration: &DefaultChallengeDuration,
StellarClient: stellarClient,
acc: acc,
perunAddr: perunID,
assetAddr: assetID,
maxIters: MaxIterationsUntilAbort,
pollingInterval: DefaultPollingInterval,
log: log.MakeEmbedding(log.Default()),
}
}

Expand All @@ -68,7 +72,7 @@ func (a *Adjudicator) Subscribe(ctx context.Context, cid pchannel.ID) (pchannel.
c := a.StellarClient
perunAddr := a.GetPerunAddr()
assetAddr := a.GetAssetAddr()
return NewAdjudicatorSub(ctx, cid, c, perunAddr, assetAddr)
return NewAdjudicatorSub(ctx, cid, c, perunAddr, assetAddr, a.challengeDuration)
}

func (a *Adjudicator) Withdraw(ctx context.Context, req pchannel.AdjudicatorReq, smap pchannel.StateMap) error {
Expand Down
74 changes: 29 additions & 45 deletions channel/adjudicator_sub.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 PolyCrypt GmbH
// Copyright 2024 PolyCrypt GmbH
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -32,44 +32,37 @@ const (
DefaultSubscriptionPollingInterval = time.Duration(5) * time.Second
)

type AdjEvent interface {
// Sets the necessary event data from the channel information
EventDataFromChannel(cchanState wire.Channel, timestamp uint64) error
ID() pchannel.ID
Timeout() pchannel.Timeout
Version() event.Version
Tstamp() uint64
}

// AdjudicatorSub implements the AdjudicatorSubscription interface.
type AdjEventSub struct {
stellarClient *client.Client
chanControl wire.Control
cid pchannel.ID
perunAddr xdr.ScAddress
assetAddr xdr.ScAddress
events chan AdjEvent
subErrors chan error
err error
cancel context.CancelFunc
closer *pkgsync.Closer
pollInterval time.Duration
log log.Embedding
challengeDuration *time.Duration
stellarClient *client.Client
chanControl wire.Control
cid pchannel.ID
perunAddr xdr.ScAddress
assetAddr xdr.ScAddress
events chan event.PerunEvent
subErrors chan error
err error
cancel context.CancelFunc
closer *pkgsync.Closer
pollInterval time.Duration
log log.Embedding
}

func NewAdjudicatorSub(ctx context.Context, cid pchannel.ID, stellarClient *client.Client, perunAddr xdr.ScAddress, assetAddr xdr.ScAddress) (*AdjEventSub, error) {
func NewAdjudicatorSub(ctx context.Context, cid pchannel.ID, stellarClient *client.Client, perunAddr xdr.ScAddress, assetAddr xdr.ScAddress, challengeDuration *time.Duration) (*AdjEventSub, error) {

sub := &AdjEventSub{
stellarClient: stellarClient,
chanControl: wire.Control{},
cid: cid,
perunAddr: perunAddr,
assetAddr: assetAddr,
events: make(chan AdjEvent, DefaultBufferSize),
subErrors: make(chan error, 1),
pollInterval: DefaultSubscriptionPollingInterval,
closer: new(pkgsync.Closer),
log: log.MakeEmbedding(log.Default()),
challengeDuration: challengeDuration,
stellarClient: stellarClient,
chanControl: wire.Control{},
cid: cid,
perunAddr: perunAddr,
assetAddr: assetAddr,
events: make(chan event.PerunEvent, DefaultBufferSize),
subErrors: make(chan error, 1),
pollInterval: DefaultSubscriptionPollingInterval,
closer: new(pkgsync.Closer),
log: log.MakeEmbedding(log.Default()),
}

ctx, sub.cancel = context.WithCancel(ctx)
Expand Down Expand Up @@ -121,16 +114,16 @@ polling:
continue polling

} else {
s.log.Log().Debug("Event detected, evaluating events...")
s.log.Log().Debugf("Found event: %v", adjEvent)
s.log.Log().Debug("Contract event detected, evaluating...")
s.log.Log().Debugf("Found contract event: %v", adjEvent)
s.events <- adjEvent
return
}
}
}
}

func DifferencesInControls(controlCurr, controlNext wire.Control) (AdjEvent, error) {
func DifferencesInControls(controlCurr, controlNext wire.Control) (event.PerunEvent, error) {

if controlCurr.FundedA != controlNext.FundedA {
if controlCurr.FundedA {
Expand Down Expand Up @@ -187,12 +180,3 @@ func DifferencesInControls(controlCurr, controlNext wire.Control) (AdjEvent, err

return nil, nil
}

func IdenticalControls(controlCurr, controlNext wire.Control) bool {
return controlCurr.FundedA == controlNext.FundedA &&
controlCurr.FundedB == controlNext.FundedB &&
controlCurr.Closed == controlNext.Closed &&
controlCurr.WithdrawnA == controlNext.WithdrawnA &&
controlCurr.WithdrawnB == controlNext.WithdrawnB &&
controlCurr.Disputed == controlNext.Disputed
}
10 changes: 4 additions & 6 deletions channel/subscribe.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 PolyCrypt GmbH
// Copyright 2024 PolyCrypt GmbH
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -36,15 +36,13 @@ func (s *AdjEventSub) Next() pchannel.AdjudicatorEvent {
return nil
}

timestamp := ev.Tstamp()

switch e := ev.(type) {
case *event.DisputedEvent:
log.Println("DisputedEvent received")
dispEvent := pchannel.AdjudicatorEventBase{
VersionV: e.Version(),
IDV: e.ID(),
TimeoutV: MakeTimeout(timestamp),
TimeoutV: event.MakeTimeout(*s.challengeDuration),
}
ddn := &pchannel.RegisteredEvent{AdjudicatorEventBase: dispEvent, State: nil, Sigs: nil}
s.closer.Close()
Expand All @@ -56,7 +54,7 @@ func (s *AdjEventSub) Next() pchannel.AdjudicatorEvent {
conclEvent := pchannel.AdjudicatorEventBase{
VersionV: e.Version(),
IDV: e.ID(),
TimeoutV: MakeTimeout(timestamp),
TimeoutV: event.MakeTimeout(*s.challengeDuration),
}
ccn := &pchannel.ConcludedEvent{AdjudicatorEventBase: conclEvent}
s.closer.Close()
Expand All @@ -77,7 +75,7 @@ func (s *AdjEventSub) Close() error {
return nil
}

func (s *AdjEventSub) getEvents() <-chan AdjEvent {
func (s *AdjEventSub) getEvents() <-chan event.PerunEvent {
return s.events
}

Expand Down
102 changes: 0 additions & 102 deletions channel/timeout.go

This file was deleted.

16 changes: 16 additions & 0 deletions event/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2024 - 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 event provides the functionality to interpret events emitted from the Perun contract and to transform them into AdjudicatorEvent events.
package event
Loading
Loading