Skip to content

Commit

Permalink
mst: expose NodeData and TreeEntry types
Browse files Browse the repository at this point in the history
  • Loading branch information
devinivy committed Dec 20, 2024
1 parent b125248 commit 1041a88
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 25 deletions.
22 changes: 11 additions & 11 deletions mst/cbor_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions mst/mst.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,20 +105,20 @@ func checkTreeInvariant(ents []nodeEntry) {
// the CBOR codec.
func CBORTypes() []reflect.Type {
return []reflect.Type{
reflect.TypeOf(nodeData{}),
reflect.TypeOf(treeEntry{}),
reflect.TypeOf(NodeData{}),
reflect.TypeOf(TreeEntry{}),
}
}

// MST tree node as gets serialized to CBOR. Note that the CBOR fields are all
// single-character.
type nodeData struct {
type NodeData struct {
Left *cid.Cid `cborgen:"l"` // [nullable] pointer to lower-level subtree to the "left" of this path/key
Entries []treeEntry `cborgen:"e"` // ordered list of entries at this node
Entries []TreeEntry `cborgen:"e"` // ordered list of entries at this node
}

// treeEntry are elements of nodeData's Entries.
type treeEntry struct {
// TreeEntry are elements of NodeData's Entries.
type TreeEntry struct {
PrefixLen int64 `cborgen:"p"` // count of characters shared with previous path/key in tree
KeySuffix []byte `cborgen:"k"` // remaining part of path/key (appended to "previous key")
Val cid.Cid `cborgen:"v"` // CID pointer at this path/key
Expand Down Expand Up @@ -189,7 +189,7 @@ func (mst *MerkleSearchTree) getEntries(ctx context.Context) ([]nodeEntry, error
// otherwise this is a virtual/pointer struct and we need to hydrate from
// blockstore before returning entries
if mst.pointer != cid.Undef {
var nd nodeData
var nd NodeData
if err := mst.cst.Get(ctx, mst.pointer, &nd); err != nil {
return nil, err
}
Expand All @@ -210,7 +210,7 @@ func (mst *MerkleSearchTree) getEntries(ctx context.Context) ([]nodeEntry, error
}

// golang-specific helper that calls in to deserializeNodeData
func entriesFromNodeData(ctx context.Context, nd *nodeData, cst cbor.IpldStore) ([]nodeEntry, error) {
func entriesFromNodeData(ctx context.Context, nd *NodeData, cst cbor.IpldStore) ([]nodeEntry, error) {
layer := -1
if len(nd.Entries) > 0 {
// NOTE(bnewbold): can compute the layer on the first KeySuffix, because for the first entry that field is a complete key
Expand Down
4 changes: 2 additions & 2 deletions mst/mst_interop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,9 @@ func TestManualNode(t *testing.T) {
t.Fatal(err)
}

simple_nd := nodeData{
simple_nd := NodeData{
Left: nil,
Entries: []treeEntry{
Entries: []TreeEntry{
{
PrefixLen: 0,
KeySuffix: []byte("com.example.record/3jqfcqzm3fo2j"),
Expand Down
8 changes: 4 additions & 4 deletions mst/mst_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func layerForEntries(entries []nodeEntry) int {
}

// Typescript: deserializeNodeData(storage, data, layer)
func deserializeNodeData(ctx context.Context, cst cbor.IpldStore, nd *nodeData, layer int) ([]nodeEntry, error) {
func deserializeNodeData(ctx context.Context, cst cbor.IpldStore, nd *NodeData, layer int) ([]nodeEntry, error) {
entries := []nodeEntry{}
if nd.Left != nil {
// Note: like Typescript, this is actually a lazy load
Expand Down Expand Up @@ -111,8 +111,8 @@ func deserializeNodeData(ctx context.Context, cst cbor.IpldStore, nd *nodeData,
}

// Typescript: serializeNodeData(entries) -> NodeData
func serializeNodeData(entries []nodeEntry) (*nodeData, error) {
var data nodeData
func serializeNodeData(entries []nodeEntry) (*NodeData, error) {
var data NodeData

i := 0
if len(entries) > 0 && entries[0].isTree() {
Expand Down Expand Up @@ -157,7 +157,7 @@ func serializeNodeData(entries []nodeEntry) (*nodeData, error) {
}

prefixLen := countPrefixLen(lastKey, leaf.Key)
data.Entries = append(data.Entries, treeEntry{
data.Entries = append(data.Entries, TreeEntry{
PrefixLen: int64(prefixLen),
KeySuffix: []byte(leaf.Key)[prefixLen:],
Val: leaf.Val,
Expand Down

0 comments on commit 1041a88

Please sign in to comment.