Fix UTXO serialization errors (#1233)

This commit is contained in:
Ori Newman 2020-12-16 11:27:43 +02:00 committed by GitHub
parent f90d7d796a
commit 12379bedb6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 13 deletions

View File

@ -188,5 +188,5 @@ func ReadElements(r io.Reader, elements ...interface{}) error {
// IsMalformedError returns whether the error indicates a malformed data source
func IsMalformedError(err error) bool {
return errors.Is(err, io.ErrUnexpectedEOF) || errors.Is(err, errMalformed)
return errors.Is(err, io.ErrUnexpectedEOF) || errors.Is(err, io.EOF) || errors.Is(err, errMalformed)
}

View File

@ -10,8 +10,6 @@ import (
"github.com/pkg/errors"
)
const uint32Size = 4
// SerializeUTXO returns the byte-slice representation for given UTXOEntry-outpoint pair
func SerializeUTXO(entry externalapi.UTXOEntry, outpoint *externalapi.DomainOutpoint) ([]byte, error) {
w := &bytes.Buffer{}
@ -71,12 +69,6 @@ func deserializeOutpoint(r io.Reader) (*externalapi.DomainOutpoint, error) {
return nil, err
}
indexBytes := make([]byte, uint32Size)
_, err = io.ReadFull(r, indexBytes)
if err != nil {
return nil, err
}
var index uint32
err = serialization.ReadElement(r, &index)
if err != nil {
@ -113,19 +105,19 @@ func deserializeUTXOEntry(r io.Reader) (externalapi.UTXOEntry, error) {
var blockBlueScore uint64
var amount uint64
var isCoinbase bool
err := serialization.ReadElements(r, blockBlueScore, amount, isCoinbase)
err := serialization.ReadElements(r, &blockBlueScore, &amount, &isCoinbase)
if err != nil {
return nil, err
}
var scriptPubKeyLen int
err = serialization.ReadElement(r, scriptPubKeyLen)
var scriptPubKeyLen uint64
err = serialization.ReadElement(r, &scriptPubKeyLen)
if err != nil {
return nil, err
}
scriptPubKey := make([]byte, scriptPubKeyLen)
_, err = r.Read(scriptPubKey)
_, err = io.ReadFull(r, scriptPubKey)
if err != nil {
return nil, errors.WithStack(err)
}

View File

@ -2,6 +2,7 @@ package utxo
import (
"encoding/hex"
"reflect"
"testing"
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
@ -30,3 +31,38 @@ func Benchmark_serializeUTXO(b *testing.B) {
}
}
}
func Test_serializeUTXO(t *testing.T) {
scriptPublicKey, err := hex.DecodeString("76a914ad06dd6ddee55cbca9a9e3713bd7587509a3056488ac")
if err != nil {
t.Fatalf("Error decoding scriptPublicKey string: %s", err)
}
entry := NewUTXOEntry(5000000000, scriptPublicKey, false, 1432432)
outpoint := &externalapi.DomainOutpoint{
TransactionID: externalapi.DomainTransactionID{
0x16, 0x5e, 0x38, 0xe8, 0xb3, 0x91, 0x45, 0x95,
0xd9, 0xc6, 0x41, 0xf3, 0xb8, 0xee, 0xc2, 0xf3,
0x46, 0x11, 0x89, 0x6b, 0x82, 0x1a, 0x68, 0x3b,
0x7a, 0x4e, 0xde, 0xfe, 0x2c, 0x00, 0x00, 0x00,
},
Index: 0xffffffff,
}
serialized, err := SerializeUTXO(entry, outpoint)
if err != nil {
t.Fatalf("SerializeUTXO: %+v", err)
}
deserializedEntry, deserializedOutpoint, err := DeserializeUTXO(serialized)
if err != nil {
return
}
if !reflect.DeepEqual(deserializedEntry, entry) {
t.Fatalf("deserialized entry is not equal to the original")
}
if !reflect.DeepEqual(deserializedOutpoint, outpoint) {
t.Fatalf("deserialized outpoint is not equal to the original")
}
}