Skip to content

Commit

Permalink
make field length optional for maps (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
vedhavyas authored Mar 19, 2021
1 parent 2ed0cc3 commit 93cd509
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 153 deletions.
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

0 comments on commit 93cd509

Please sign in to comment.