Skip to content

Commit

Permalink
simplify to just completed objectives for now
Browse files Browse the repository at this point in the history
  • Loading branch information
lalexgap committed Feb 7, 2022
1 parent 6c43b70 commit 33f46a7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 23 deletions.
25 changes: 8 additions & 17 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,19 @@ import (
"github.com/statechannels/go-nitro/types"
)

// ChannelFundedEvents is returned in a channel when a state channel has been successfly funded.
type ChannelFundedEvent struct {
ChannelId types.Destination
ObjectiveId protocols.ObjectiveId
}

// Client provides the interface for the consuming application
type Client struct {
engine engine.Engine // The core business logic of the client
Address *types.Address
allChannelFundedEvents chan ChannelFundedEvent
engine engine.Engine // The core business logic of the client
Address *types.Address
completedObjectives chan protocols.ObjectiveId
}

// New is the constructor for a Client. It accepts a messaging service, a chain service, and a store as injected dependencies.
func New(messageService messageservice.MessageService, chainservice chainservice.ChainService, store store.Store, logDestination io.Writer) Client {
c := Client{}
c.Address = store.GetAddress()
c.engine = engine.New(messageService, chainservice, store, logDestination)
c.allChannelFundedEvents = make(chan ChannelFundedEvent, 100)
c.completedObjectives = make(chan protocols.ObjectiveId, 100)

// Start the engine in a go routine
go c.engine.Run()
Expand All @@ -52,11 +46,8 @@ func (c *Client) handleEngineEvents() {
for update := range c.engine.ToApi() {

for _, completed := range update.CompletedObjectives {
// TODO: We're assuming the first channel id is the one we're interested in.
channelId := completed.Channels()[0]
event := ChannelFundedEvent{ObjectiveId: completed.Id(), ChannelId: channelId}

c.allChannelFundedEvents <- event
c.completedObjectives <- completed.Id()

}

Expand All @@ -65,9 +56,9 @@ func (c *Client) handleEngineEvents() {

// Begin API

// ChannelFunded returns a channel that receives a ChannelFundedEvent whenever a state channel has been fully funded.
func (c *Client) ChannelFunded() <-chan ChannelFundedEvent {
return c.allChannelFundedEvents
// CompletedObjectives returns a chan that receives a objective id whenever that objective is completed
func (c *Client) CompletedObjectives() <-chan protocols.ObjectiveId {
return c.completedObjectives
}

// CreateDirectChannel creates a directly funded channel with the given counterparty
Expand Down
12 changes: 6 additions & 6 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,16 @@ func TestNew(t *testing.T) {
}}

id := clientA.CreateDirectChannel(b, types.Address{}, types.Bytes{}, outcome, big.NewInt(0))
got := <-clientA.ChannelFunded()
got := <-clientA.CompletedObjectives()

if got.ObjectiveId != id {
t.Errorf("expected completed objective with id %v, but got %v", id, got.ObjectiveId)
if got != id {
t.Errorf("expected completed objective with id %v, but got %v", id, got)
}

gotFromB := <-clientB.ChannelFunded()
gotFromB := <-clientB.CompletedObjectives()

if gotFromB.ObjectiveId != id {
t.Errorf("expected completed objective with id %v, but got %v", id, got.ObjectiveId)
if gotFromB != id {
t.Errorf("expected completed objective with id %v, but got %v", id, gotFromB)
}

}

0 comments on commit 33f46a7

Please sign in to comment.