-
Notifications
You must be signed in to change notification settings - Fork 264
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Removes Amino and adds varint utility-functions instead, keeping the same serialized format. Partial fix for #242. The last vestige of Amino is in the proof handling, namely `AbsenceOp` and `ValueOp`, which are structs encoded/decoded with Amino.
- Loading branch information
1 parent
4f7cdd7
commit 5d5829f
Showing
9 changed files
with
300 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package iavl | ||
|
||
import ( | ||
"encoding/binary" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"math/bits" | ||
) | ||
|
||
// decodeBytes decodes a varint length-prefixed byte slice, returning it along with the number | ||
// of input bytes read. | ||
func decodeBytes(bz []byte) ([]byte, int, error) { | ||
size, n, err := decodeUvarint(bz) | ||
if err != nil { | ||
return nil, n, err | ||
} | ||
if int(size) < 0 { | ||
return nil, n, fmt.Errorf("invalid negative length %v decoding []byte", size) | ||
} | ||
if len(bz) < n+int(size) { | ||
return nil, n, fmt.Errorf("insufficient bytes decoding []byte of length %v", size) | ||
} | ||
bz2 := make([]byte, size) | ||
copy(bz2, bz[n:n+int(size)]) | ||
n += int(size) | ||
return bz2, n, nil | ||
} | ||
|
||
// decodeUvarint decodes a varint-encoded unsigned integer from a byte slice, returning it and the | ||
// number of bytes decoded. | ||
func decodeUvarint(bz []byte) (uint64, int, error) { | ||
u, n := binary.Uvarint(bz) | ||
if n == 0 { | ||
// buf too small | ||
return u, n, errors.New("buffer too small") | ||
} else if n < 0 { | ||
// value larger than 64 bits (overflow) | ||
// and -n is the number of bytes read | ||
n = -n | ||
return u, n, errors.New("EOF decoding uvarint") | ||
} | ||
return u, n, nil | ||
} | ||
|
||
// decodeVarint decodes a varint-encoded integer from a byte slice, returning it and the number of | ||
// bytes decoded. | ||
func decodeVarint(bz []byte) (int64, int, error) { | ||
i, n := binary.Varint(bz) | ||
if n == 0 { | ||
return i, n, errors.New("buffer too small") | ||
} else if n < 0 { | ||
// value larger than 64 bits (overflow) | ||
// and -n is the number of bytes read | ||
n = -n | ||
return i, n, errors.New("EOF decoding varint") | ||
} | ||
return i, n, nil | ||
} | ||
|
||
// encodeBytes writes a varint length-prefixed byte slice to the writer. | ||
func encodeBytes(w io.Writer, bz []byte) error { | ||
err := encodeUvarint(w, uint64(len(bz))) | ||
if err != nil { | ||
return err | ||
} | ||
_, err = w.Write(bz) | ||
return err | ||
} | ||
|
||
// encodeBytesSize returns the byte size of the given slice including length-prefixing. | ||
func encodeBytesSize(bz []byte) int { | ||
return encodeUvarintSize(uint64(len(bz))) + len(bz) | ||
} | ||
|
||
// encodeUvarint writes a varint-encoded unsigned integer to an io.Writer. | ||
func encodeUvarint(w io.Writer, u uint64) error { | ||
var buf [binary.MaxVarintLen64]byte | ||
n := binary.PutUvarint(buf[:], u) | ||
_, err := w.Write(buf[0:n]) | ||
return err | ||
} | ||
|
||
// encodeUvarintSize returns the byte size of the given integer as a varint. | ||
func encodeUvarintSize(u uint64) int { | ||
if u == 0 { | ||
return 1 | ||
} | ||
return (bits.Len64(u) + 6) / 7 | ||
} | ||
|
||
// encodeVarint writes a varint-encoded integer to an io.Writer. | ||
func encodeVarint(w io.Writer, i int64) error { | ||
var buf [binary.MaxVarintLen64]byte | ||
n := binary.PutVarint(buf[:], i) | ||
_, err := w.Write(buf[0:n]) | ||
return err | ||
} | ||
|
||
// encodeVarintSize returns the byte size of the given integer as a varint. | ||
func encodeVarintSize(i int64) int { | ||
var buf [binary.MaxVarintLen64]byte | ||
return binary.PutVarint(buf[:], i) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.