-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement a basic event schema using IPLD (#69)
Implement a basic event schema using IPLD bindnode. Add an example that shows how to chain the events together and store them in a link system. Remove `replace` directive in go module pointing to local path. Run `go mod tidy` to clean up the go module. Fixes #65
- Loading branch information
Showing
6 changed files
with
131 additions
and
5 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,13 @@ | ||
package event | ||
|
||
import ( | ||
"github.com/ipld/go-ipld-prime" | ||
"github.com/libp2p/go-libp2p-core/peer" | ||
) | ||
|
||
type Event struct { | ||
Version string | ||
Previous ipld.Link | ||
Peer peer.ID | ||
Signature []byte | ||
} |
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 event_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/functionland/go-fula/event" | ||
"github.com/ipfs/go-cid" | ||
"github.com/ipld/go-ipld-prime" | ||
_ "github.com/ipld/go-ipld-prime/codec/dagcbor" | ||
cidlink "github.com/ipld/go-ipld-prime/linking/cid" | ||
"github.com/ipld/go-ipld-prime/node/bindnode" | ||
"github.com/ipld/go-ipld-prime/storage/memstore" | ||
"github.com/multiformats/go-multicodec" | ||
) | ||
|
||
var lp = cidlink.LinkPrototype{ | ||
Prefix: cid.Prefix{ | ||
Version: 1, | ||
Codec: uint64(multicodec.DagCbor), | ||
MhType: uint64(multicodec.Sha2_256), | ||
MhLength: -1, | ||
}, | ||
} | ||
|
||
// ExampleEventChain creates three events, chains them together and stores them via ipld.DefaultLinkSystem. | ||
func ExampleEventChain() { | ||
first := &event.Event{ | ||
Version: event.Version0, | ||
Peer: "1", | ||
Signature: []byte("sig1"), | ||
} | ||
second := &event.Event{ | ||
Version: event.Version0, | ||
Peer: "2", | ||
Signature: []byte("sig2"), | ||
} | ||
third := &event.Event{ | ||
Version: event.Version0, | ||
Peer: "3", | ||
Signature: []byte("sig3"), | ||
} | ||
|
||
ls := cidlink.DefaultLinkSystem() | ||
store := &memstore.Store{} | ||
ls.SetReadStorage(store) | ||
ls.SetWriteStorage(store) | ||
|
||
ctx := context.Background() | ||
|
||
n := bindnode.Wrap(first, event.Prototypes.Event.Type()) | ||
if l, err := ls.Store(ipld.LinkContext{Ctx: ctx}, lp, n); err != nil { | ||
panic(err) | ||
} else { | ||
second.Previous = l | ||
fmt.Printf("Link to 1st event: %s\n", l.String()) | ||
} | ||
|
||
n = bindnode.Wrap(second, event.Prototypes.Event.Type()) | ||
if l, err := ls.Store(ipld.LinkContext{Ctx: ctx}, lp, n); err != nil { | ||
panic(err) | ||
} else { | ||
third.Previous = l | ||
fmt.Printf("Link to 2nd event: %s\n", l.String()) | ||
} | ||
|
||
n = bindnode.Wrap(third, event.Prototypes.Event.Type()) | ||
if l, err := ls.Store(ipld.LinkContext{Ctx: ctx}, lp, n); err != nil { | ||
panic(err) | ||
} else { | ||
fmt.Printf("Link to 3rd event: %s\n", l.String()) | ||
} | ||
|
||
// output: | ||
// Link to 1st event: bafyreid3ehnrqi5bgyy73s42kevtcoa4tjol2rzwytnfk6222aict4hkam | ||
// Link to 2nd event: bafyreiaip6euzqqan5ujs322xe7ih653onk4lib2lsykeyvkd4uhcl6aea | ||
// Link to 3rd event: bafyreie7ycr7yqqprsn5k4zcuyvo6ap47dl2hwpea5dfo3owpdkj6ovtvm | ||
} |
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,29 @@ | ||
package event | ||
|
||
import ( | ||
_ "embed" | ||
"fmt" | ||
|
||
"github.com/ipld/go-ipld-prime" | ||
"github.com/ipld/go-ipld-prime/node/bindnode" | ||
"github.com/ipld/go-ipld-prime/schema" | ||
) | ||
|
||
const Version0 = "0" | ||
|
||
var ( | ||
Prototypes struct { | ||
Event schema.TypedPrototype | ||
} | ||
|
||
//go:embed schema.ipldsch | ||
schemaBytes []byte | ||
) | ||
|
||
func init() { | ||
typeSystem, err := ipld.LoadSchemaBytes(schemaBytes) | ||
if err != nil { | ||
panic(fmt.Errorf("cannot load schema: %w", err)) | ||
} | ||
Prototypes.Event = bindnode.Prototype((*Event)(nil), typeSystem.TypeByName("Event")) | ||
} |
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,6 @@ | ||
type Event struct { | ||
Version String | ||
Previous optional Link | ||
Peer String | ||
Signature Bytes | ||
} |
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