Skip to content

Commit

Permalink
fix: Use node representation for Block (#2746)
Browse files Browse the repository at this point in the history
## Relevant issue(s)

Resolves #2745

## Description

This PR ensures that the node being passed to the link system is in it's
representation form. This will allow the use of optional fields.

Node that the 2 added `block` unit tests are there to cover when the
optional `isEncrypted` field will be added to `Block` ipld schema.
  • Loading branch information
fredcarle authored Jun 21, 2024
1 parent c1b612e commit 77af4fc
Show file tree
Hide file tree
Showing 26 changed files with 240 additions and 183 deletions.
2 changes: 2 additions & 0 deletions docs/data_format_changes/i2746-use-node-representation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Use node representation
To enable the use of optional IPLD schema fields, we change to using the node representation when saving with the link system.
15 changes: 9 additions & 6 deletions internal/core/block/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
// Schema is the IPLD schema type that represents a `Block`.
var (
Schema schema.Type
SchemaPrototype schema.TypedPrototype
SchemaPrototype ipld.NodePrototype
)

func init() {
Expand All @@ -49,7 +49,7 @@ type schemaDefinition interface {
IPLDSchemaBytes() []byte
}

func mustSetSchema(schemas ...schemaDefinition) (schema.Type, schema.TypedPrototype) {
func mustSetSchema(schemas ...schemaDefinition) (schema.Type, ipld.NodePrototype) {
schemaBytes := make([][]byte, 0, len(schemas))
for _, s := range schemas {
schemaBytes = append(schemaBytes, s.IPLDSchemaBytes())
Expand All @@ -66,7 +66,7 @@ func mustSetSchema(schemas ...schemaDefinition) (schema.Type, schema.TypedProtot
// If [Block] and `blockSchematype` do not match, this will panic.
proto := bindnode.Prototype(&Block{}, blockSchemaType)

return blockSchemaType, proto
return blockSchemaType, proto.Representation()
}

// DAGLink represents a link to another object in a DAG.
Expand Down Expand Up @@ -201,9 +201,9 @@ func (block *Block) Unmarshal(b []byte) error {
return nil
}

// GenerateNode generates an IPLD node from the block.
// GenerateNode generates an IPLD node from the block in its representation form.
func (block *Block) GenerateNode() (node ipld.Node) {
return bindnode.Wrap(block, Schema)
return bindnode.Wrap(block, Schema).Representation()
}

// GetLinkByName returns the link by name. It will return false if the link does not exist.
Expand All @@ -219,11 +219,14 @@ func (block *Block) GetLinkByName(name string) (cidlink.Link, bool) {
// GenerateLink generates a cid link for the block.
func (block *Block) GenerateLink() (cidlink.Link, error) {
node := bindnode.Wrap(block, Schema)
return GetLinkFromNode(node)
return GetLinkFromNode(node.Representation())
}

// GetLinkFromNode returns the cid link from the node.
func GetLinkFromNode(node ipld.Node) (cidlink.Link, error) {
if typedNode, ok := node.(schema.TypedNode); ok {
node = typedNode.Representation()
}
lsys := cidlink.DefaultLinkSystem()
link, err := lsys.ComputeLink(GetLinkPrototype(), node)
if err != nil {
Expand Down
64 changes: 58 additions & 6 deletions internal/core/block/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/ipld/go-ipld-prime/linking"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
"github.com/ipld/go-ipld-prime/node/basicnode"
"github.com/ipld/go-ipld-prime/node/bindnode"
"github.com/ipld/go-ipld-prime/storage/memstore"
"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -107,7 +108,11 @@ func generateBlocks(lsys *linking.LinkSystem) (cidlink.Link, error) {
},
},
}
compositeUpdateBlockLink, err := lsys.Store(ipld.LinkContext{}, GetLinkPrototype(), compositeUpdateBlock.GenerateNode())
compositeUpdateBlockLink, err := lsys.Store(
ipld.LinkContext{},
GetLinkPrototype(),
compositeUpdateBlock.GenerateNode(),
)
if err != nil {
return cidlink.Link{}, err
}
Expand All @@ -124,8 +129,7 @@ func TestBlock(t *testing.T) {
rootLink, err := generateBlocks(&lsys)
require.NoError(t, err)

proto := SchemaPrototype.Representation()
nd, err := lsys.Load(ipld.LinkContext{}, rootLink, proto)
nd, err := lsys.Load(ipld.LinkContext{}, rootLink, SchemaPrototype)
require.NoError(t, err)

block, err := GetFromNode(nd)
Expand All @@ -139,7 +143,7 @@ func TestBlock(t *testing.T) {

require.Equal(t, block, newBlock)

newNode := block.GenerateNode()
newNode := bindnode.Wrap(block, Schema)
require.Equal(t, nd, newNode)

link, err := block.GenerateLink()
Expand All @@ -165,8 +169,7 @@ func TestBlockDeltaPriority(t *testing.T) {
rootLink, err := generateBlocks(&lsys)
require.NoError(t, err)

proto := SchemaPrototype.Representation()
nd, err := lsys.Load(ipld.LinkContext{}, rootLink, proto)
nd, err := lsys.Load(ipld.LinkContext{}, rootLink, SchemaPrototype)
require.NoError(t, err)

block, err := GetFromNode(nd)
Expand All @@ -176,3 +179,52 @@ func TestBlockDeltaPriority(t *testing.T) {
// which results in a priority of 2.
require.Equal(t, uint64(2), block.Delta.GetPriority())
}

func TestBlockMarshal_IsEncryptedNotSet_ShouldNotContainIsEcryptedField(t *testing.T) {
lsys := cidlink.DefaultLinkSystem()
store := memstore.Store{}
lsys.SetReadStorage(&store)
lsys.SetWriteStorage(&store)

fieldBlock := Block{
Delta: crdt.CRDT{
LWWRegDelta: &crdt.LWWRegDelta{
DocID: []byte("docID"),
FieldName: "name",
Priority: 1,
SchemaVersionID: "schemaVersionID",
Data: []byte("John"),
},
},
}

b, err := fieldBlock.Marshal()
require.NoError(t, err)
require.NotContains(t, string(b), "isEncrypted")
}

func TestBlockMarshal_IsEncryptedNotSetWithLinkSystem_ShouldLoadWithNoError(t *testing.T) {
lsys := cidlink.DefaultLinkSystem()
store := memstore.Store{}
lsys.SetReadStorage(&store)
lsys.SetWriteStorage(&store)

fieldBlock := Block{
Delta: crdt.CRDT{
LWWRegDelta: &crdt.LWWRegDelta{
DocID: []byte("docID"),
FieldName: "name",
Priority: 1,
SchemaVersionID: "schemaVersionID",
Data: []byte("John"),
},
},
}
fieldBlockLink, err := lsys.Store(ipld.LinkContext{}, GetLinkPrototype(), fieldBlock.GenerateNode())
require.NoError(t, err)

nd, err := lsys.Load(ipld.LinkContext{}, fieldBlockLink, SchemaPrototype)
require.NoError(t, err)
_, err = GetFromNode(nd)
require.NoError(t, err)
}
2 changes: 1 addition & 1 deletion internal/db/merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func TestMerge_DualBranchWithOneIncomplete_CouldNotFindCID(t *testing.T) {
Cid: compInfo3.link.Cid,
SchemaRoot: col.SchemaRoot(),
})
require.ErrorContains(t, err, "could not find bafyreichk7jctbxhrodk5au3r4c4iqm627d4fi2cii2beseu4h6caoiwla")
require.ErrorContains(t, err, "could not find bafyreifi4sa4auy4uk6psoljwuzqepgwqzsjk3h6p2xjdtsty7bdjz4uzm")

// Verify the document was created with the expected values
doc, err := col.Get(ctx, docID, false)
Expand Down
2 changes: 1 addition & 1 deletion net/sync_dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func syncDAG(ctx context.Context, bserv blockservice.BlockService, block *corebl
//
// any errors encountered during preload are ignored
preloader := func(pctx preload.PreloadContext, l preload.Link) {
go lsys.Load(linking.LinkContext{Ctx: pctx.Ctx}, l.Link, basicnode.Prototype.Any) //nolint:errcheck
go lsys.Load(linking.LinkContext{Ctx: pctx.Ctx}, l.Link, coreblock.SchemaPrototype) //nolint:errcheck
}
config := traversal.Config{
Ctx: ctx,
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/events/simple/with_update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@ func TestEventsSimpleWithUpdate(t *testing.T) {
ExpectedUpdates: []testUtils.ExpectedUpdate{
{
DocID: immutable.Some(docID1),
Cid: immutable.Some("bafyreifvrmwmlwtglxe3afki36spu6d5qs6vvza57kxs4giyi53r5vbbnu"),
Cid: immutable.Some("bafyreih5kmftjua6lihlm7lwohamezecomnwgxv6jtowfnrrfdev43lquq"),
},
{
DocID: immutable.Some(docID2),
},
{
DocID: immutable.Some(docID1),
Cid: immutable.Some("bafyreihfijpchdbc6fb3klay3a2ktcwav7mse6udbxpauslwzsmn6qczna"),
Cid: immutable.Some("bafyreifzav4o7q4sljthu2vks3idyd67hg34llnyv44ii6pstal2woc65q"),
},
},
}
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/mutation/create/with_version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestMutationCreate_ReturnsVersionCID(t *testing.T) {
{
"_version": []map[string]any{
{
"cid": "bafyreicceacb554vtciciumodqmz6vmnfvr6uod2hfhnwujmfqx5pgq3fi",
"cid": "bafyreia5ph2hvwebdsxe7m2f6bwuq7ngwxzqp7esiuzjihtcz2jswma6xu",
},
},
},
Expand Down
44 changes: 22 additions & 22 deletions tests/integration/query/commits/simple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ func TestQueryCommits(t *testing.T) {
}`,
Results: []map[string]any{
{
"cid": "bafyreietqxguz3xlady4gfaqnbeamwsnrwfkufykkpprxej7a77ba7siay",
"cid": "bafyreifzyy7bmpx2eywj4lznxzrzrvh6vrz6l7bhthkpexdq3wtho3vz6i",
},
{
"cid": "bafyreidksmyoo6txzmcygby6quhdkzymlqoaxpg75ehlxjdneotjzbih6y",
"cid": "bafyreic2sba5sffkfnt32wfeoaw4qsqozjb5acwwtouxuzllb3aymjwute",
},
{
"cid": "bafyreichxrfyhajs7rzp3wh5f2zrmt3zkjqan5dmxoy4qno5ozy7omzfpq",
"cid": "bafyreihv7jqe32wsuff5vwzlp7izoo6pqg6kgqf5edknp3mqm3344gu35q",
},
},
},
Expand Down Expand Up @@ -79,22 +79,22 @@ func TestQueryCommitsMultipleDocs(t *testing.T) {
}`,
Results: []map[string]any{
{
"cid": "bafyreiazgtllwk7znzuapv3fsukzhpekqqjjvgv4fzypkfp7mljfabie3q",
"cid": "bafyreihpasbgxcoxmzv5bp6euq3lbaoh5y5wjbbgfthtxqs3nppk36kebq",
},
{
"cid": "bafyreicbr2jo7y4d6773q66kxvzq4k3jss2rw5ysr3co2mjdhcdyiz7buq",
"cid": "bafyreihe3jydldbt7mvkiae6asrchdxajzkxwid6syi436nmrpcqhwt7xa",
},
{
"cid": "bafyreihmvuytwy5ofcm5bqyazxwnquksutxvybznavmw23vddb7nooh6pq",
"cid": "bafyreihb5eo3luqoojztdmxtg3tdpvm6pc64mkyrzlefbdauker5qlnop4",
},
{
"cid": "bafyreietqxguz3xlady4gfaqnbeamwsnrwfkufykkpprxej7a77ba7siay",
"cid": "bafyreifzyy7bmpx2eywj4lznxzrzrvh6vrz6l7bhthkpexdq3wtho3vz6i",
},
{
"cid": "bafyreidksmyoo6txzmcygby6quhdkzymlqoaxpg75ehlxjdneotjzbih6y",
"cid": "bafyreic2sba5sffkfnt32wfeoaw4qsqozjb5acwwtouxuzllb3aymjwute",
},
{
"cid": "bafyreichxrfyhajs7rzp3wh5f2zrmt3zkjqan5dmxoy4qno5ozy7omzfpq",
"cid": "bafyreihv7jqe32wsuff5vwzlp7izoo6pqg6kgqf5edknp3mqm3344gu35q",
},
},
},
Expand Down Expand Up @@ -125,15 +125,15 @@ func TestQueryCommitsWithSchemaVersionIDField(t *testing.T) {
}`,
Results: []map[string]any{
{
"cid": "bafyreietqxguz3xlady4gfaqnbeamwsnrwfkufykkpprxej7a77ba7siay",
"cid": "bafyreifzyy7bmpx2eywj4lznxzrzrvh6vrz6l7bhthkpexdq3wtho3vz6i",
"schemaVersionId": "bafkreicprhqxzlw3akyssz2v6pifwfueavp7jq2yj3dghapi3qcq6achs4",
},
{
"cid": "bafyreidksmyoo6txzmcygby6quhdkzymlqoaxpg75ehlxjdneotjzbih6y",
"cid": "bafyreic2sba5sffkfnt32wfeoaw4qsqozjb5acwwtouxuzllb3aymjwute",
"schemaVersionId": "bafkreicprhqxzlw3akyssz2v6pifwfueavp7jq2yj3dghapi3qcq6achs4",
},
{
"cid": "bafyreichxrfyhajs7rzp3wh5f2zrmt3zkjqan5dmxoy4qno5ozy7omzfpq",
"cid": "bafyreihv7jqe32wsuff5vwzlp7izoo6pqg6kgqf5edknp3mqm3344gu35q",
"schemaVersionId": "bafkreicprhqxzlw3akyssz2v6pifwfueavp7jq2yj3dghapi3qcq6achs4",
},
},
Expand Down Expand Up @@ -349,7 +349,7 @@ func TestQuery_CommitsWithAllFieldsWithUpdate_NoError(t *testing.T) {
`,
Results: []map[string]any{
{
"cid": "bafyreigurfgpfvcm4uzqxjf4ur3xegxbebn6yoogjrvyaw6x7d2ji6igim",
"cid": "bafyreiay56ley5dvsptso37fsonfcrtbuphwlfhi67d2y52vzzexba6vua",
"collectionID": int64(1),
"delta": testUtils.CBORValue(22),
"docID": "bae-c9fb0fa4-1195-589c-aa54-e68333fb90b3",
Expand All @@ -358,13 +358,13 @@ func TestQuery_CommitsWithAllFieldsWithUpdate_NoError(t *testing.T) {
"height": int64(2),
"links": []map[string]any{
{
"cid": "bafyreietqxguz3xlady4gfaqnbeamwsnrwfkufykkpprxej7a77ba7siay",
"cid": "bafyreifzyy7bmpx2eywj4lznxzrzrvh6vrz6l7bhthkpexdq3wtho3vz6i",
"name": "_head",
},
},
},
{
"cid": "bafyreietqxguz3xlady4gfaqnbeamwsnrwfkufykkpprxej7a77ba7siay",
"cid": "bafyreifzyy7bmpx2eywj4lznxzrzrvh6vrz6l7bhthkpexdq3wtho3vz6i",
"collectionID": int64(1),
"delta": testUtils.CBORValue(21),
"docID": "bae-c9fb0fa4-1195-589c-aa54-e68333fb90b3",
Expand All @@ -374,7 +374,7 @@ func TestQuery_CommitsWithAllFieldsWithUpdate_NoError(t *testing.T) {
"links": []map[string]any{},
},
{
"cid": "bafyreidksmyoo6txzmcygby6quhdkzymlqoaxpg75ehlxjdneotjzbih6y",
"cid": "bafyreic2sba5sffkfnt32wfeoaw4qsqozjb5acwwtouxuzllb3aymjwute",
"collectionID": int64(1),
"delta": testUtils.CBORValue("John"),
"docID": "bae-c9fb0fa4-1195-589c-aa54-e68333fb90b3",
Expand All @@ -384,7 +384,7 @@ func TestQuery_CommitsWithAllFieldsWithUpdate_NoError(t *testing.T) {
"links": []map[string]any{},
},
{
"cid": "bafyreif632ewkphjjwxcthemgbkgtm25faw22mvw7eienu5gnazrao33ba",
"cid": "bafyreicsavx5oblk6asfoqyssz4ge2gf5ekfouvi7o6l7adly275op5oje",
"collectionID": int64(1),
"delta": nil,
"docID": "bae-c9fb0fa4-1195-589c-aa54-e68333fb90b3",
Expand All @@ -393,17 +393,17 @@ func TestQuery_CommitsWithAllFieldsWithUpdate_NoError(t *testing.T) {
"height": int64(2),
"links": []map[string]any{
{
"cid": "bafyreichxrfyhajs7rzp3wh5f2zrmt3zkjqan5dmxoy4qno5ozy7omzfpq",
"cid": "bafyreihv7jqe32wsuff5vwzlp7izoo6pqg6kgqf5edknp3mqm3344gu35q",
"name": "_head",
},
{
"cid": "bafyreigurfgpfvcm4uzqxjf4ur3xegxbebn6yoogjrvyaw6x7d2ji6igim",
"cid": "bafyreiay56ley5dvsptso37fsonfcrtbuphwlfhi67d2y52vzzexba6vua",
"name": "age",
},
},
},
{
"cid": "bafyreichxrfyhajs7rzp3wh5f2zrmt3zkjqan5dmxoy4qno5ozy7omzfpq",
"cid": "bafyreihv7jqe32wsuff5vwzlp7izoo6pqg6kgqf5edknp3mqm3344gu35q",
"collectionID": int64(1),
"delta": nil,
"docID": "bae-c9fb0fa4-1195-589c-aa54-e68333fb90b3",
Expand All @@ -412,11 +412,11 @@ func TestQuery_CommitsWithAllFieldsWithUpdate_NoError(t *testing.T) {
"height": int64(1),
"links": []map[string]any{
{
"cid": "bafyreidksmyoo6txzmcygby6quhdkzymlqoaxpg75ehlxjdneotjzbih6y",
"cid": "bafyreic2sba5sffkfnt32wfeoaw4qsqozjb5acwwtouxuzllb3aymjwute",
"name": "name",
},
{
"cid": "bafyreietqxguz3xlady4gfaqnbeamwsnrwfkufykkpprxej7a77ba7siay",
"cid": "bafyreifzyy7bmpx2eywj4lznxzrzrvh6vrz6l7bhthkpexdq3wtho3vz6i",
"name": "age",
},
},
Expand Down
8 changes: 4 additions & 4 deletions tests/integration/query/commits/with_cid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ func TestQueryCommitsWithCid(t *testing.T) {
testUtils.Request{
Request: `query {
commits(
cid: "bafyreichxrfyhajs7rzp3wh5f2zrmt3zkjqan5dmxoy4qno5ozy7omzfpq"
cid: "bafyreihv7jqe32wsuff5vwzlp7izoo6pqg6kgqf5edknp3mqm3344gu35q"
) {
cid
}
}`,
Results: []map[string]any{
{
"cid": "bafyreichxrfyhajs7rzp3wh5f2zrmt3zkjqan5dmxoy4qno5ozy7omzfpq",
"cid": "bafyreihv7jqe32wsuff5vwzlp7izoo6pqg6kgqf5edknp3mqm3344gu35q",
},
},
},
Expand All @@ -71,14 +71,14 @@ func TestQueryCommitsWithCidForFieldCommit(t *testing.T) {
testUtils.Request{
Request: `query {
commits(
cid: "bafyreichxrfyhajs7rzp3wh5f2zrmt3zkjqan5dmxoy4qno5ozy7omzfpq"
cid: "bafyreihv7jqe32wsuff5vwzlp7izoo6pqg6kgqf5edknp3mqm3344gu35q"
) {
cid
}
}`,
Results: []map[string]any{
{
"cid": "bafyreichxrfyhajs7rzp3wh5f2zrmt3zkjqan5dmxoy4qno5ozy7omzfpq",
"cid": "bafyreihv7jqe32wsuff5vwzlp7izoo6pqg6kgqf5edknp3mqm3344gu35q",
},
},
},
Expand Down
Loading

0 comments on commit 77af4fc

Please sign in to comment.