Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make field length optional for maps #97

Merged
merged 1 commit into from
Mar 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
language: go
go_import_path: github.com/centrifuge/precise-proofs
go:
- 1.11.x
- tip
- 1.15.x

env:
matrix:
- GO111MODULE=on

install:
- go get -u github.com/golang/dep/...
- dep ensure
- go mod tidy
- go mod vendor

script:
- go test -race -coverprofile=coverage.txt -covermode=atomic -v ./proofs
Expand Down
98 changes: 0 additions & 98 deletions Gopkg.lock

This file was deleted.

43 changes: 0 additions & 43 deletions Gopkg.toml

This file was deleted.

6 changes: 4 additions & 2 deletions examples/wasm/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build wasm

package main

import (
Expand All @@ -7,9 +9,9 @@ import (
"strings"
"syscall/js"

"github.com/centrifuge/precise-proofs/examples/documents"
documentspb "github.com/centrifuge/precise-proofs/examples/documents"
"github.com/centrifuge/precise-proofs/proofs"
"github.com/centrifuge/precise-proofs/proofs/proto"
proofspb "github.com/centrifuge/precise-proofs/proofs/proto"
"github.com/golang/protobuf/jsonpb"
)

Expand Down
16 changes: 16 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module github.com/centrifuge/precise-proofs

go 1.15

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/golang/protobuf v1.3.1
github.com/pkg/errors v0.8.0
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.2.2
github.com/xsleonard/go-merkle v1.1.0
golang.org/x/crypto v0.0.0-20190530122614-20be4c3c3ed5
golang.org/x/sys v0.0.0-20190531073156-46560c3f3c0a // indirect
)

replace github.com/xsleonard/go-merkle v1.1.0 => github.com/centrifuge/go-merkle v0.0.0-20190727075423-0ac78bbbc01b
21 changes: 21 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
github.com/centrifuge/go-merkle v0.0.0-20190727075423-0ac78bbbc01b h1:TPvvMcGAc3TVBVgQ4XYYEWTXxYls8YuylZ8JzrVxPzc=
github.com/centrifuge/go-merkle v0.0.0-20190727075423-0ac78bbbc01b/go.mod h1:0voJY6Qzxvr2S0LeDSFQiCnJzGq5gORg2SwCmn8602I=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/protobuf v1.3.1 h1:YF8+flBXS5eO826T4nzqPrxfhQThhXl0YzfuUPu4SBg=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190530122614-20be4c3c3ed5 h1:8dUaAV7K4uHsF56JQWkprecIQKdPHtR9jCHF5nB8uzc=
golang.org/x/crypto v0.0.0-20190530122614-20be4c3c3ed5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190531073156-46560c3f3c0a h1:ndj5BW7+tkr8XVsbkZpft/pgCRU+L76ePImF2dpCwek=
golang.org/x/sys v0.0.0-20190531073156-46560c3f3c0a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
40 changes: 36 additions & 4 deletions proofs/flatten.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"sort"
"strings"

"github.com/centrifuge/precise-proofs/proofs/proto"
proofspb "github.com/centrifuge/precise-proofs/proofs/proto"
"github.com/golang/protobuf/descriptor"
"github.com/golang/protobuf/proto"
godescriptor "github.com/golang/protobuf/protoc-gen-go/descriptor"
Expand Down Expand Up @@ -254,7 +254,9 @@ func (f *messageFlattener) handleValue(prop Property, value reflect.Value, salts
// Handle each value of the map
for _, k := range value.MapKeys() {
keyLength := getKeyLengthFrom(outerFieldDescriptor)

if keyLength == 0 {
keyLength = fetchLengthFromInterface(k)
}
elemProp, err := prop.MapElemProp(k.Interface(), keyLength)
if err != nil {
return errors.Wrapf(err, "failed to create elem prop for %q", k)
Expand Down Expand Up @@ -296,6 +298,18 @@ func (f *messageFlattener) handleValue(prop Property, value reflect.Value, salts
return nil
}

func fetchLengthFromInterface(k reflect.Value) uint64 {
switch k.Kind() {
case reflect.Interface:
v := reflect.ValueOf(k.Interface())
return fetchLengthFromInterface(v)
case reflect.Array, reflect.Slice, reflect.String, reflect.Chan, reflect.Map:
return uint64(k.Len())
}

return 0
}

func (f *messageFlattener) appendLeaf(prop Property, value []byte, salt []byte, readablePropertyLengthSuffix string, hash []byte, hashed bool) {
leaf := LeafNode{
Property: prop,
Expand Down Expand Up @@ -429,13 +443,31 @@ func sliceToMap(value reflect.Value, mappingKey string, keyLength uint64) (refle
// since we know the key length, we convert each
// []byte to [keyLength]byte before using it as a key
keyType = reflect.ArrayOf(int(keyLength), reflect.TypeOf(byte(0)))

// if key length is 0
// then we set the map type as interface{}
// individual key type will b declared based on the length of each value
if keyLength == 0 {
m := make(map[interface{}]bool)
r := reflect.TypeOf(m)
keyType = r.Key()
}

extractKeyByteSlice := extractKey
extractKey = func(v reflect.Value) (reflect.Value, error) {
bs, _ := extractKeyByteSlice(v)
if uint64(bs.Len()) != keyLength {
// if key length is not set or 0,
// then take the key length of key
kt := keyType
if keyLength == 0 {
kt = reflect.ArrayOf(int(uint64(bs.Len())), reflect.TypeOf(byte(0)))
}

if keyLength != 0 && uint64(bs.Len()) != keyLength {
return reflect.Value{}, errors.Errorf("could not use %x as mapping_key - does not have length %d", bs, keyLength)
}
ba := reflect.New(keyType)

ba := reflect.New(kt)
reflect.Copy(ba.Elem(), bs)
return ba.Elem(), nil
}
Expand Down
3 changes: 1 addition & 2 deletions proofs/property.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ import (
"strconv"
"strings"

proofspb "github.com/centrifuge/precise-proofs/proofs/proto"
"github.com/pkg/errors"

"github.com/centrifuge/precise-proofs/proofs/proto"
)

// Property uniquely identifies a LeafNode
Expand Down