Skip to content

Commit e17a6ca

Browse files
Dan Lainedboehm-avalabsrkuris
authored
x/sync -- use for sending Range Proofs (ava-labs#1537)
Co-authored-by: dboehm-avalabs <david.boehm@avalabs.org> Co-authored-by: Ron Kuris <ron.kuris@avalabs.org>
1 parent d77e409 commit e17a6ca

File tree

11 files changed

+368
-345
lines changed

11 files changed

+368
-345
lines changed

proto/pb/sync/sync.pb.go

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

proto/sync/sync.proto

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ message KeyChange {
4747

4848
// SerializedPath is the serialized representation of a path.
4949
message SerializedPath {
50-
uint32 nibble_length = 1;
50+
uint64 nibble_length = 1;
5151
bytes value = 2;
5252
}
5353

utils/hashing/hashing.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package hashing
55

66
import (
77
"crypto/sha256"
8+
"errors"
89
"fmt"
910
"io"
1011

@@ -16,6 +17,8 @@ const (
1617
AddrLen = ripemd160.Size
1718
)
1819

20+
var ErrInvalidHashLen = errors.New("invalid hash length")
21+
1922
// Hash256 A 256 bit long hash value.
2023
type Hash256 = [HashLen]byte
2124

@@ -85,7 +88,7 @@ func Checksum(bytes []byte, length int) []byte {
8588
func ToHash256(bytes []byte) (Hash256, error) {
8689
hash := Hash256{}
8790
if bytesLen := len(bytes); bytesLen != HashLen {
88-
return hash, fmt.Errorf("expected 32 bytes but got %d", bytesLen)
91+
return hash, fmt.Errorf("%w: expected 32 bytes but got %d", ErrInvalidHashLen, bytesLen)
8992
}
9093
copy(hash[:], bytes)
9194
return hash, nil
@@ -94,7 +97,7 @@ func ToHash256(bytes []byte) (Hash256, error) {
9497
func ToHash160(bytes []byte) (Hash160, error) {
9598
hash := Hash160{}
9699
if bytesLen := len(bytes); bytesLen != ripemd160.Size {
97-
return hash, fmt.Errorf("expected 20 bytes but got %d", bytesLen)
100+
return hash, fmt.Errorf("%w: expected 20 bytes but got %d", ErrInvalidHashLen, bytesLen)
98101
}
99102
copy(hash[:], bytes)
100103
return hash, nil

x/merkledb/codec.go

-109
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ type EncoderDecoder interface {
7474
type Encoder interface {
7575
EncodeProof(version uint16, p *Proof) ([]byte, error)
7676
EncodeChangeProof(version uint16, p *ChangeProof) ([]byte, error)
77-
EncodeRangeProof(version uint16, p *RangeProof) ([]byte, error)
7877

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

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

164-
func (c *codecImpl) EncodeRangeProof(version uint16, proof *RangeProof) ([]byte, error) {
165-
if proof == nil {
166-
return nil, errEncodeNil
167-
}
168-
169-
if version != codecVersion {
170-
return nil, fmt.Errorf("%w: %d", errUnknownVersion, version)
171-
}
172-
173-
buf := &bytes.Buffer{}
174-
if err := c.encodeInt(buf, int(version)); err != nil {
175-
return nil, err
176-
}
177-
if err := c.encodeProofPath(buf, proof.StartProof); err != nil {
178-
return nil, err
179-
}
180-
if err := c.encodeProofPath(buf, proof.EndProof); err != nil {
181-
return nil, err
182-
}
183-
if err := c.encodeInt(buf, len(proof.KeyValues)); err != nil {
184-
return nil, err
185-
}
186-
for _, kv := range proof.KeyValues {
187-
if err := c.encodeKeyValue(kv, buf); err != nil {
188-
return nil, err
189-
}
190-
}
191-
192-
return buf.Bytes(), nil
193-
}
194-
195162
func (c *codecImpl) encodeDBNode(version uint16, n *dbNode) ([]byte, error) {
196163
if n == nil {
197164
return nil, errEncodeNil
@@ -356,54 +323,6 @@ func (c *codecImpl) DecodeChangeProof(b []byte, proof *ChangeProof) (uint16, err
356323
return codecVersion, nil
357324
}
358325

359-
func (c *codecImpl) DecodeRangeProof(b []byte, proof *RangeProof) (uint16, error) {
360-
if proof == nil {
361-
return 0, errDecodeNil
362-
}
363-
if minRangeProofLen > len(b) {
364-
return 0, io.ErrUnexpectedEOF
365-
}
366-
367-
var (
368-
src = bytes.NewReader(b)
369-
err error
370-
)
371-
gotCodecVersion, err := c.decodeInt(src)
372-
if err != nil {
373-
return 0, err
374-
}
375-
if codecVersion != gotCodecVersion {
376-
return 0, fmt.Errorf("%w: %d", errInvalidCodecVersion, gotCodecVersion)
377-
}
378-
if proof.StartProof, err = c.decodeProofPath(src); err != nil {
379-
return 0, err
380-
}
381-
if proof.EndProof, err = c.decodeProofPath(src); err != nil {
382-
return 0, err
383-
}
384-
385-
numKeyValues, err := c.decodeInt(src)
386-
if err != nil {
387-
return 0, err
388-
}
389-
if numKeyValues < 0 {
390-
return 0, errNegativeNumKeyValues
391-
}
392-
if numKeyValues > src.Len()/minKeyValueLen {
393-
return 0, io.ErrUnexpectedEOF
394-
}
395-
proof.KeyValues = make([]KeyValue, numKeyValues)
396-
for i := range proof.KeyValues {
397-
if proof.KeyValues[i], err = c.decodeKeyValue(src); err != nil {
398-
return 0, err
399-
}
400-
}
401-
if src.Len() != 0 {
402-
return 0, errExtraSpace
403-
}
404-
return codecVersion, nil
405-
}
406-
407326
func (c *codecImpl) decodeDBNode(b []byte, n *dbNode) (uint16, error) {
408327
if n == nil {
409328
return 0, errDecodeNil
@@ -491,24 +410,6 @@ func (c *codecImpl) decodeKeyChange(src *bytes.Reader) (KeyChange, error) {
491410
return result, nil
492411
}
493412

494-
func (c *codecImpl) decodeKeyValue(src *bytes.Reader) (KeyValue, error) {
495-
if minKeyValueLen > src.Len() {
496-
return KeyValue{}, io.ErrUnexpectedEOF
497-
}
498-
499-
var (
500-
result KeyValue
501-
err error
502-
)
503-
if result.Key, err = c.decodeByteSlice(src); err != nil {
504-
return result, err
505-
}
506-
if result.Value, err = c.decodeByteSlice(src); err != nil {
507-
return result, err
508-
}
509-
return result, nil
510-
}
511-
512413
func (c *codecImpl) encodeKeyChange(kv KeyChange, dst io.Writer) error {
513414
if err := c.encodeByteSlice(dst, kv.Key); err != nil {
514415
return err
@@ -519,16 +420,6 @@ func (c *codecImpl) encodeKeyChange(kv KeyChange, dst io.Writer) error {
519420
return nil
520421
}
521422

522-
func (c *codecImpl) encodeKeyValue(kv KeyValue, dst io.Writer) error {
523-
if err := c.encodeByteSlice(dst, kv.Key); err != nil {
524-
return err
525-
}
526-
if err := c.encodeByteSlice(dst, kv.Value); err != nil {
527-
return err
528-
}
529-
return nil
530-
}
531-
532423
func (*codecImpl) encodeBool(dst io.Writer, value bool) error {
533424
bytesValue := falseBytes
534425
if value {

0 commit comments

Comments
 (0)