-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support invocations as effects (#30)
This PR allows completed effects to be included in a receipt. Similar to [`Ran`](https://github.com/storacha/go-ucanto/blob/main/core/invocation/ran/ran.go), an effect is now a union type - a link OR an invocation. This allows a location commitment to be returned from a `blob/accept` invocation. 🤔 Not sure I agree with using effects for this, but this is how it is done currently.
- Loading branch information
Showing
7 changed files
with
225 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package fx | ||
|
||
import ( | ||
"github.com/storacha/go-ucanto/core/invocation" | ||
"github.com/storacha/go-ucanto/ucan" | ||
) | ||
|
||
type Effects interface { | ||
Fork() []Effect | ||
Join() Effect | ||
} | ||
|
||
type effects struct { | ||
fork []Effect | ||
join Effect | ||
} | ||
|
||
func (fx effects) Fork() []Effect { | ||
return fx.fork | ||
} | ||
|
||
func (fx effects) Join() Effect { | ||
return fx.join | ||
} | ||
|
||
// Option is an option configuring effects. | ||
type Option func(fx *effects) error | ||
|
||
// WithFork configures the forks for the receipt. | ||
func WithFork(forks ...Effect) Option { | ||
return func(fx *effects) error { | ||
fx.fork = forks | ||
return nil | ||
} | ||
} | ||
|
||
// WithJoin configures the join for the receipt. | ||
func WithJoin(join Effect) Option { | ||
return func(fx *effects) error { | ||
fx.join = join | ||
return nil | ||
} | ||
} | ||
|
||
func NewEffects(opts ...Option) Effects { | ||
var fx effects | ||
for _, opt := range opts { | ||
opt(&fx) | ||
} | ||
return fx | ||
} | ||
|
||
// Effect is either an invocation or a link to one. | ||
type Effect struct { | ||
invocation invocation.Invocation | ||
link ucan.Link | ||
} | ||
|
||
// Invocation returns the invocation if it is available. | ||
func (e Effect) Invocation() (invocation.Invocation, bool) { | ||
return e.invocation, e.invocation != nil | ||
} | ||
|
||
// Link returns the invocation root link. | ||
func (e Effect) Link() ucan.Link { | ||
if e.invocation != nil { | ||
return e.invocation.Link() | ||
} | ||
return e.link | ||
} | ||
|
||
func FromLink(link ucan.Link) Effect { | ||
return Effect{nil, link} | ||
} | ||
|
||
func FromInvocation(invocation invocation.Invocation) Effect { | ||
return Effect{invocation, nil} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package receipt | ||
|
||
import ( | ||
"slices" | ||
"testing" | ||
|
||
"github.com/storacha/go-ucanto/core/invocation" | ||
"github.com/storacha/go-ucanto/core/invocation/ran" | ||
"github.com/storacha/go-ucanto/core/ipld" | ||
"github.com/storacha/go-ucanto/core/receipt/fx" | ||
"github.com/storacha/go-ucanto/core/result" | ||
"github.com/storacha/go-ucanto/core/result/ok" | ||
"github.com/storacha/go-ucanto/testing/fixtures" | ||
"github.com/storacha/go-ucanto/testing/helpers" | ||
"github.com/storacha/go-ucanto/ucan" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestEffects(t *testing.T) { | ||
ran := ran.FromLink(helpers.RandomCID()) | ||
out := result.Ok[ok.Unit, ipld.Builder](ok.Unit{}) | ||
|
||
t.Run("as links", func(t *testing.T) { | ||
f0 := fx.FromLink(helpers.RandomCID()) | ||
f1 := fx.FromLink(helpers.RandomCID()) | ||
j := fx.FromLink(helpers.RandomCID()) | ||
|
||
receipt, err := Issue(fixtures.Alice, out, ran, WithFork(f0, f1), WithJoin(j)) | ||
require.NoError(t, err) | ||
|
||
effects := receipt.Fx() | ||
require.True(t, slices.ContainsFunc(effects.Fork(), func(f fx.Effect) bool { | ||
return f.Link().String() == f0.Link().String() | ||
})) | ||
require.True(t, slices.ContainsFunc(effects.Fork(), func(f fx.Effect) bool { | ||
return f.Link().String() == f1.Link().String() | ||
})) | ||
require.Equal(t, effects.Join().Link(), j.Link()) | ||
}) | ||
|
||
t.Run("as invocations", func(t *testing.T) { | ||
i0, err := invocation.Invoke( | ||
fixtures.Alice, | ||
fixtures.Bob, | ||
ucan.NewCapability("fx/0", fixtures.Alice.DID().String(), ucan.NoCaveats{}), | ||
) | ||
require.NoError(t, err) | ||
i1, err := invocation.Invoke( | ||
fixtures.Alice, | ||
fixtures.Mallory, | ||
ucan.NewCapability("fx/1", fixtures.Alice.DID().String(), ucan.NoCaveats{}), | ||
) | ||
require.NoError(t, err) | ||
i2, err := invocation.Invoke( | ||
fixtures.Mallory, | ||
fixtures.Bob, | ||
ucan.NewCapability("fx/2", fixtures.Alice.DID().String(), ucan.NoCaveats{}), | ||
) | ||
require.NoError(t, err) | ||
|
||
f0 := fx.FromInvocation(i0) | ||
f1 := fx.FromInvocation(i1) | ||
j := fx.FromInvocation(i2) | ||
|
||
receipt, err := Issue(fixtures.Alice, out, ran, WithFork(f0, f1), WithJoin(j)) | ||
require.NoError(t, err) | ||
|
||
effects := receipt.Fx() | ||
require.True(t, slices.ContainsFunc(effects.Fork(), func(f fx.Effect) bool { | ||
return f.Link().String() == f0.Link().String() | ||
})) | ||
require.True(t, slices.ContainsFunc(effects.Fork(), func(f fx.Effect) bool { | ||
return f.Link().String() == f1.Link().String() | ||
})) | ||
require.Equal(t, effects.Join().Link(), j.Link()) | ||
|
||
for _, effect := range effects.Fork() { | ||
_, ok := effect.Invocation() | ||
require.True(t, ok) | ||
} | ||
|
||
_, ok := effects.Join().Invocation() | ||
require.True(t, ok) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.