Skip to content

Commit

Permalink
Replace codegen with bindnode and add plain IPLD schema
Browse files Browse the repository at this point in the history
Define the HAMT schema in a dedicated ipldsch file and replace the
codegen types with go structs using IPLD bindnode.

Note the IPLD dependency is updated to the current head of main branch
to take advantage of the latest unreleased bindnode fixes.
  • Loading branch information
masih committed Jun 8, 2022
1 parent 65c763d commit 79518ad
Show file tree
Hide file tree
Showing 11 changed files with 776 additions and 8,948 deletions.
43 changes: 20 additions & 23 deletions builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
var _ ipld.NodePrototype = (*Prototype)(nil)

type Prototype struct {
BitWidth int
BucketSize int
BitWidth int64
BucketSize int64

// hashAlg requires an extra bool, because the zero value can't be used
// as the default behavior, since the code 0x00 is a valid multicodec
Expand Down Expand Up @@ -45,9 +45,9 @@ func (p FilecoinV3Prototype) NewBuilder() ipld.NodeBuilder {
var _ ipld.NodeBuilder = (*Builder)(nil)

type Builder struct {
bitWidth int
bitWidth int64
hashAlg multicodec.Code
bucketSize int
bucketSize int64

node *Node
}
Expand Down Expand Up @@ -103,11 +103,11 @@ func (b *Builder) BeginMap(sizeHint int64) (ipld.MapAssembler, error) {
default:
return nil, fmt.Errorf("unsupported hash algorithm: %x", b.hashAlg)
}
b.node._HashMapRoot = _HashMapRoot{
hashAlg: _Int{int64(b.hashAlg)},
bucketSize: _Int{int64(b.bucketSize)},
hamt: _HashMapNode{
_map: _Bytes{make([]byte, 1<<(b.bitWidth-3))},
b.node.HashMapRoot = HashMapRoot{
HashAlg: b.hashAlg,
BucketSize: b.bucketSize,
Hamt: HashMapNode{
Map: make([]byte, 1<<(b.bitWidth-3)),
},
}
return &assembler{node: b.node}, nil
Expand Down Expand Up @@ -146,11 +146,11 @@ func (a *assembler) Finish() error {
}

func (a *assembler) KeyPrototype() ipld.NodePrototype {
return _Bytes__Prototype{}
return basicnode.Prototype.Bytes
}

func (a *assembler) ValuePrototype(k string) ipld.NodePrototype {
return _Any__Prototype{}
return basicnode.Prototype.Any
}

type keyAssembler struct {
Expand Down Expand Up @@ -224,55 +224,54 @@ func (a valueAssembler) AssignNull() error {
}

func (a valueAssembler) AssignBool(b bool) error {
builder := _Any__ReprPrototype{}.NewBuilder()
builder := basicnode.Prototype.Any.NewBuilder()
if err := builder.AssignBool(b); err != nil {
return err
}
return a.AssignNode(builder.Build())
}

func (a valueAssembler) AssignInt(i int64) error {
builder := _Any__ReprPrototype{}.NewBuilder()
builder := basicnode.Prototype.Any.NewBuilder()
if err := builder.AssignInt(i); err != nil {
return err
}
return a.AssignNode(builder.Build())
}

func (a valueAssembler) AssignFloat(f float64) error {
builder := _Any__ReprPrototype{}.NewBuilder()
builder := basicnode.Prototype.Any.NewBuilder()
if err := builder.AssignFloat(f); err != nil {
return err
}
return a.AssignNode(builder.Build())
}

func (a valueAssembler) AssignString(s string) error {
builder := _Any__ReprPrototype{}.NewBuilder()
builder := basicnode.Prototype.Any.NewBuilder()
if err := builder.AssignString(s); err != nil {
return err
}
return a.AssignNode(builder.Build())
}

func (a valueAssembler) AssignBytes(b []byte) error {
builder := _Any__ReprPrototype{}.NewBuilder()
builder := basicnode.Prototype.Any.NewBuilder()
if err := builder.AssignBytes(b); err != nil {
return err
}
return a.AssignNode(builder.Build())
}

func (a valueAssembler) AssignLink(l ipld.Link) error {
builder := _Any__ReprPrototype{}.NewBuilder()
builder := basicnode.Prototype.Any.NewBuilder()
if err := builder.AssignLink(l); err != nil {
return err
}
return a.AssignNode(builder.Build())
}

func (a valueAssembler) AssignNode(v ipld.Node) error {
val := v.(*_Any)
func (a valueAssembler) AssignNode(val ipld.Node) error {

key := a.parent.assemblingKey
if a.parent.assemblingKey == nil {
Expand All @@ -281,10 +280,8 @@ func (a valueAssembler) AssignNode(v ipld.Node) error {
a.parent.assemblingKey = nil
node := a.parent.node
hash := node.hashKey(key)

return node.insertEntry(&node.hamt, node.bitWidth(), 0, hash, _BucketEntry{
_Bytes{key}, *val,
})
entry := BucketEntry{Key: key, Value: val}
return node.insertEntry(&node.Hamt, node.bitWidth(), 0, hash, entry)
}

func (valueAssembler) Prototype() ipld.NodePrototype {
Expand Down
3 changes: 2 additions & 1 deletion filecoin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
qt "github.com/frankban/quicktest"
hamt "github.com/ipld/go-ipld-adl-hamt"
"github.com/ipld/go-ipld-prime/codec/dagcbor"
"github.com/ipld/go-ipld-prime/schema"
cbg "github.com/whyrusleeping/cbor-gen"
)

Expand Down Expand Up @@ -63,7 +64,7 @@ func TestFilecoinBasic(t *testing.T) {

// TODO: can we do better than these type asserts?
node := builder.Build().(*hamt.Node)
nodeRepr := node.Substrate().(hamt.HashMapNode).Representation()
nodeRepr := node.Substrate().(schema.TypedNode).Representation()
buf.Reset()
qt.Assert(t, dagcbor.Encode(nodeRepr, buf), qt.IsNil)
enc := buf.Bytes()
Expand Down
9 changes: 6 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ require (
github.com/frankban/quicktest v1.14.3
github.com/google/go-cmp v0.5.8
github.com/ipfs/go-cid v0.2.0
github.com/ipld/go-ipld-prime v0.16.0
github.com/ipld/go-ipld-prime v0.16.1-0.20220607093021-c88f0b441e80
github.com/multiformats/go-multicodec v0.5.0
github.com/twmb/murmur3 v1.1.6
github.com/whyrusleeping/cbor-gen v0.0.0-20220514204315-f29c37e9c44c
)

require (
github.com/gopherjs/gopherjs v1.17.2 // indirect
github.com/jtolds/gls v4.20.0+incompatible // indirect
github.com/klauspost/cpuid/v2 v2.0.9 // indirect
github.com/kr/pretty v0.3.0 // indirect
github.com/kr/text v0.2.0 // indirect
Expand All @@ -26,9 +28,10 @@ require (
github.com/multiformats/go-varint v0.0.6 // indirect
github.com/polydawn/refmt v0.0.0-20201211092308-30ac6d18308e // indirect
github.com/rogpeppe/go-internal v1.6.1 // indirect
github.com/smartystreets/assertions v1.13.0 // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect
golang.org/x/crypto v0.0.0-20210506145944-38f3c27a63bf // indirect
golang.org/x/sys v0.0.0-20210309074719-68d13333faf2 // indirect
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 // indirect
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
lukechampine.com/blake3 v1.1.6 // indirect
)
Loading

0 comments on commit 79518ad

Please sign in to comment.