Skip to content

Commit

Permalink
asset+proof: rename and move var bytes funcs for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
guggero committed Oct 5, 2023
1 parent 3c3ed30 commit e955d55
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 33 deletions.
46 changes: 34 additions & 12 deletions asset/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,22 @@ func VarIntDecoder(r io.Reader, val any, buf *[8]byte, l uint64) error {
return tlv.NewTypeForDecodingErr(val, "uint64", 8, l)
}

func VarBytesEncoder(w io.Writer, val any, buf *[8]byte) error {
func DVarBytesWithLimit(limit uint64) tlv.Decoder {
return func(r io.Reader, val interface{}, _ *[8]byte, l uint64) error {
if l > limit {
return tlv.ErrRecordTooLarge
}

if b, ok := val.(*[]byte); ok {
*b = make([]byte, l)
_, err := io.ReadFull(r, *b)
return err
}
return tlv.NewTypeForDecodingErr(val, "[]byte", l, l)
}
}

func InlineVarBytesEncoder(w io.Writer, val any, buf *[8]byte) error {
if t, ok := val.(*[]byte); ok {
if err := tlv.WriteVarInt(w, uint64(len(*t)), buf); err != nil {
return err
Expand All @@ -55,7 +70,9 @@ func VarBytesEncoder(w io.Writer, val any, buf *[8]byte) error {
return tlv.NewTypeForEncodingErr(val, "[]byte")
}

func VarBytesDecoder(r io.Reader, val any, buf *[8]byte, maxLen uint64) error {
func InlineVarBytesDecoder(r io.Reader, val any, buf *[8]byte,
maxLen uint64) error {

if typ, ok := val.(*[]byte); ok {
bytesLen, err := tlv.ReadVarInt(r, buf)
if err != nil {
Expand Down Expand Up @@ -268,7 +285,7 @@ func GenesisEncoder(w io.Writer, val any, buf *[8]byte) error {
return err
}
tagBytes := []byte(t.Tag)
if err := VarBytesEncoder(w, &tagBytes, buf); err != nil {
if err := InlineVarBytesEncoder(w, &tagBytes, buf); err != nil {
return err
}
if err := tlv.EBytes32(w, &t.MetaHash, buf); err != nil {
Expand All @@ -290,7 +307,7 @@ func GenesisDecoder(r io.Reader, val any, buf *[8]byte, _ uint64) error {
return err
}
var tag []byte
err = VarBytesDecoder(r, &tag, buf, MaxAssetNameLength)
err = InlineVarBytesDecoder(r, &tag, buf, MaxAssetNameLength)
if err != nil {
return err
}
Expand Down Expand Up @@ -354,7 +371,8 @@ func TxWitnessEncoder(w io.Writer, val any, buf *[8]byte) error {
}
for _, part := range *t {
part := part
if err := VarBytesEncoder(w, &part, buf); err != nil {
err := InlineVarBytesEncoder(w, &part, buf)
if err != nil {
return err
}
}
Expand All @@ -380,7 +398,9 @@ func TxWitnessDecoder(r io.Reader, val any, buf *[8]byte, _ uint64) error {
witness := make(wire.TxWitness, 0, numItems)
for i := uint64(0); i < numItems; i++ {
var item []byte
err = VarBytesDecoder(r, &item, buf, math.MaxUint16)
err = InlineVarBytesDecoder(
r, &item, buf, math.MaxUint16,
)
if err != nil {
return err
}
Expand All @@ -403,7 +423,7 @@ func WitnessEncoder(w io.Writer, val any, buf *[8]byte) error {
return err
}
streamBytes := streamBuf.Bytes()
err := VarBytesEncoder(w, &streamBytes, buf)
err := InlineVarBytesEncoder(w, &streamBytes, buf)
if err != nil {
return err
}
Expand Down Expand Up @@ -431,7 +451,7 @@ func WitnessDecoder(r io.Reader, val any, buf *[8]byte, _ uint64) error {
*typ = make([]Witness, 0, numItems)
for i := uint64(0); i < numItems; i++ {
var streamBytes []byte
err = VarBytesDecoder(
err = InlineVarBytesDecoder(
r, &streamBytes, buf, math.MaxUint16,
)
if err != nil {
Expand All @@ -457,15 +477,16 @@ func SplitCommitmentEncoder(w io.Writer, val any, buf *[8]byte) error {
return err
}
proofBytes := proof.Bytes()
if err := VarBytesEncoder(w, &proofBytes, buf); err != nil {
err := InlineVarBytesEncoder(w, &proofBytes, buf)
if err != nil {
return err
}
var rootAsset bytes.Buffer
if err := (*t).RootAsset.Encode(&rootAsset); err != nil {
return err
}
rootAssetBytes := rootAsset.Bytes()
return VarBytesEncoder(w, &rootAssetBytes, buf)
return InlineVarBytesEncoder(w, &rootAssetBytes, buf)
}
return tlv.NewTypeForEncodingErr(val, "*SplitCommitment")
}
Expand All @@ -477,7 +498,8 @@ func SplitCommitmentDecoder(r io.Reader, val any, buf *[8]byte, l uint64) error

if typ, ok := val.(**SplitCommitment); ok {
var proofBytes []byte
if err := VarBytesDecoder(r, &proofBytes, buf, l); err != nil {
err := InlineVarBytesDecoder(r, &proofBytes, buf, l)
if err != nil {
return err
}

Expand All @@ -487,7 +509,7 @@ func SplitCommitmentDecoder(r io.Reader, val any, buf *[8]byte, l uint64) error
}

var rootAssetBytes []byte
err := VarBytesDecoder(r, &rootAssetBytes, buf, l)
err = InlineVarBytesDecoder(r, &rootAssetBytes, buf, l)
if err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions proof/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func TaprootProofsEncoder(w io.Writer, val any, buf *[8]byte) error {
return err
}
proofBytes := proofBuf.Bytes()
err := asset.VarBytesEncoder(w, &proofBytes, buf)
err := asset.InlineVarBytesEncoder(w, &proofBytes, buf)
if err != nil {
return err
}
Expand All @@ -208,7 +208,7 @@ func TaprootProofsDecoder(r io.Reader, val any, buf *[8]byte, _ uint64) error {
proofs := make([]TaprootProof, 0, numProofs)
for i := uint64(0); i < numProofs; i++ {
var proofBytes []byte
err := asset.VarBytesDecoder(
err := asset.InlineVarBytesDecoder(
r, &proofBytes, buf, MaxTaprootProofSizeBytes,
)
if err != nil {
Expand Down Expand Up @@ -239,7 +239,7 @@ func AdditionalInputsEncoder(w io.Writer, val any, buf *[8]byte) error {
return err
}
inputFileBytes := inputFileBuf.Bytes()
err := asset.VarBytesEncoder(w, &inputFileBytes, buf)
err := asset.InlineVarBytesEncoder(w, &inputFileBytes, buf)
if err != nil {
return err
}
Expand Down Expand Up @@ -270,7 +270,7 @@ func AdditionalInputsDecoder(r io.Reader, val any, buf *[8]byte, l uint64) error
inputFiles := make([]File, 0, numInputs)
for i := uint64(0); i < numInputs; i++ {
var inputFileBytes []byte
err := asset.VarBytesDecoder(
err := asset.InlineVarBytesDecoder(
r, &inputFileBytes, buf, FileMaxSizeBytes,
)
if err != nil {
Expand Down
18 changes: 1 addition & 17 deletions proof/records.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package proof

import (
"bytes"
"io"

"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/wire"
Expand Down Expand Up @@ -302,25 +301,10 @@ func MetaRevealDataRecord(data *[]byte) tlv.Record {
}
return tlv.MakeDynamicRecord(
MetaRevealDataType, data, sizeFunc, tlv.EVarBytes,
DVarBytesWithLimit(MetaDataMaxSizeBytes),
asset.DVarBytesWithLimit(MetaDataMaxSizeBytes),
)
}

func DVarBytesWithLimit(limit uint64) tlv.Decoder {
return func(r io.Reader, val interface{}, _ *[8]byte, l uint64) error {
if l > limit {
return tlv.ErrRecordTooLarge
}

if b, ok := val.(*[]byte); ok {
*b = make([]byte, l)
_, err := io.ReadFull(r, *b)
return err
}
return tlv.NewTypeForDecodingErr(val, "[]byte", l, l)
}
}

func GenesisRevealRecord(genesis **asset.Genesis) tlv.Record {
recordSize := func() uint64 {
var (
Expand Down

0 comments on commit e955d55

Please sign in to comment.