Skip to content

Commit

Permalink
x/sync -- use for sending Range Proofs (ava-labs#1537)
Browse files Browse the repository at this point in the history
Co-authored-by: dboehm-avalabs <david.boehm@avalabs.org>
Co-authored-by: Ron Kuris <ron.kuris@avalabs.org>
  • Loading branch information
3 people authored May 31, 2023
1 parent d77e409 commit e17a6ca
Show file tree
Hide file tree
Showing 11 changed files with 368 additions and 345 deletions.
6 changes: 3 additions & 3 deletions proto/pb/sync/sync.pb.go

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

2 changes: 1 addition & 1 deletion proto/sync/sync.proto
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ message KeyChange {

// SerializedPath is the serialized representation of a path.
message SerializedPath {
uint32 nibble_length = 1;
uint64 nibble_length = 1;
bytes value = 2;
}

Expand Down
7 changes: 5 additions & 2 deletions utils/hashing/hashing.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package hashing

import (
"crypto/sha256"
"errors"
"fmt"
"io"

Expand All @@ -16,6 +17,8 @@ const (
AddrLen = ripemd160.Size
)

var ErrInvalidHashLen = errors.New("invalid hash length")

// Hash256 A 256 bit long hash value.
type Hash256 = [HashLen]byte

Expand Down Expand Up @@ -85,7 +88,7 @@ func Checksum(bytes []byte, length int) []byte {
func ToHash256(bytes []byte) (Hash256, error) {
hash := Hash256{}
if bytesLen := len(bytes); bytesLen != HashLen {
return hash, fmt.Errorf("expected 32 bytes but got %d", bytesLen)
return hash, fmt.Errorf("%w: expected 32 bytes but got %d", ErrInvalidHashLen, bytesLen)
}
copy(hash[:], bytes)
return hash, nil
Expand All @@ -94,7 +97,7 @@ func ToHash256(bytes []byte) (Hash256, error) {
func ToHash160(bytes []byte) (Hash160, error) {
hash := Hash160{}
if bytesLen := len(bytes); bytesLen != ripemd160.Size {
return hash, fmt.Errorf("expected 20 bytes but got %d", bytesLen)
return hash, fmt.Errorf("%w: expected 20 bytes but got %d", ErrInvalidHashLen, bytesLen)
}
copy(hash[:], bytes)
return hash, nil
Expand Down
109 changes: 0 additions & 109 deletions x/merkledb/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ type EncoderDecoder interface {
type Encoder interface {
EncodeProof(version uint16, p *Proof) ([]byte, error)
EncodeChangeProof(version uint16, p *ChangeProof) ([]byte, error)
EncodeRangeProof(version uint16, p *RangeProof) ([]byte, error)

encodeDBNode(version uint16, n *dbNode) ([]byte, error)
encodeHashValues(version uint16, hv *hashValues) ([]byte, error)
Expand All @@ -83,7 +82,6 @@ type Encoder interface {
type Decoder interface {
DecodeProof(bytes []byte, p *Proof) (uint16, error)
DecodeChangeProof(bytes []byte, p *ChangeProof) (uint16, error)
DecodeRangeProof(bytes []byte, p *RangeProof) (uint16, error)

decodeDBNode(bytes []byte, n *dbNode) (uint16, error)
}
Expand Down Expand Up @@ -161,37 +159,6 @@ func (c *codecImpl) EncodeChangeProof(version uint16, proof *ChangeProof) ([]byt
return buf.Bytes(), nil
}

func (c *codecImpl) EncodeRangeProof(version uint16, proof *RangeProof) ([]byte, error) {
if proof == nil {
return nil, errEncodeNil
}

if version != codecVersion {
return nil, fmt.Errorf("%w: %d", errUnknownVersion, version)
}

buf := &bytes.Buffer{}
if err := c.encodeInt(buf, int(version)); err != nil {
return nil, err
}
if err := c.encodeProofPath(buf, proof.StartProof); err != nil {
return nil, err
}
if err := c.encodeProofPath(buf, proof.EndProof); err != nil {
return nil, err
}
if err := c.encodeInt(buf, len(proof.KeyValues)); err != nil {
return nil, err
}
for _, kv := range proof.KeyValues {
if err := c.encodeKeyValue(kv, buf); err != nil {
return nil, err
}
}

return buf.Bytes(), nil
}

func (c *codecImpl) encodeDBNode(version uint16, n *dbNode) ([]byte, error) {
if n == nil {
return nil, errEncodeNil
Expand Down Expand Up @@ -356,54 +323,6 @@ func (c *codecImpl) DecodeChangeProof(b []byte, proof *ChangeProof) (uint16, err
return codecVersion, nil
}

func (c *codecImpl) DecodeRangeProof(b []byte, proof *RangeProof) (uint16, error) {
if proof == nil {
return 0, errDecodeNil
}
if minRangeProofLen > len(b) {
return 0, io.ErrUnexpectedEOF
}

var (
src = bytes.NewReader(b)
err error
)
gotCodecVersion, err := c.decodeInt(src)
if err != nil {
return 0, err
}
if codecVersion != gotCodecVersion {
return 0, fmt.Errorf("%w: %d", errInvalidCodecVersion, gotCodecVersion)
}
if proof.StartProof, err = c.decodeProofPath(src); err != nil {
return 0, err
}
if proof.EndProof, err = c.decodeProofPath(src); err != nil {
return 0, err
}

numKeyValues, err := c.decodeInt(src)
if err != nil {
return 0, err
}
if numKeyValues < 0 {
return 0, errNegativeNumKeyValues
}
if numKeyValues > src.Len()/minKeyValueLen {
return 0, io.ErrUnexpectedEOF
}
proof.KeyValues = make([]KeyValue, numKeyValues)
for i := range proof.KeyValues {
if proof.KeyValues[i], err = c.decodeKeyValue(src); err != nil {
return 0, err
}
}
if src.Len() != 0 {
return 0, errExtraSpace
}
return codecVersion, nil
}

func (c *codecImpl) decodeDBNode(b []byte, n *dbNode) (uint16, error) {
if n == nil {
return 0, errDecodeNil
Expand Down Expand Up @@ -491,24 +410,6 @@ func (c *codecImpl) decodeKeyChange(src *bytes.Reader) (KeyChange, error) {
return result, nil
}

func (c *codecImpl) decodeKeyValue(src *bytes.Reader) (KeyValue, error) {
if minKeyValueLen > src.Len() {
return KeyValue{}, io.ErrUnexpectedEOF
}

var (
result KeyValue
err error
)
if result.Key, err = c.decodeByteSlice(src); err != nil {
return result, err
}
if result.Value, err = c.decodeByteSlice(src); err != nil {
return result, err
}
return result, nil
}

func (c *codecImpl) encodeKeyChange(kv KeyChange, dst io.Writer) error {
if err := c.encodeByteSlice(dst, kv.Key); err != nil {
return err
Expand All @@ -519,16 +420,6 @@ func (c *codecImpl) encodeKeyChange(kv KeyChange, dst io.Writer) error {
return nil
}

func (c *codecImpl) encodeKeyValue(kv KeyValue, dst io.Writer) error {
if err := c.encodeByteSlice(dst, kv.Key); err != nil {
return err
}
if err := c.encodeByteSlice(dst, kv.Value); err != nil {
return err
}
return nil
}

func (*codecImpl) encodeBool(dst io.Writer, value bool) error {
bytesValue := falseBytes
if value {
Expand Down
Loading

0 comments on commit e17a6ca

Please sign in to comment.