Fix deserialization of script version in UTXOSet deserialization (#1395)

* Initalize protoUTXOSetIterator with index = -1

* Handle error when failed to deserialize Script version

* Add support for (de)serialization of (u)int16

* Log the error when converting it into ErrMalformedUTXO
This commit is contained in:
Svarog 2021-01-11 15:23:27 +02:00 committed by GitHub
parent 434cf45112
commit c7deda41c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 3 deletions

View File

@ -161,7 +161,7 @@ func (p *protoUTXOSetIterator) Get() (outpoint *externalapi.DomainOutpoint, utxo
entry, outpoint, err := utxo.DeserializeUTXO(p.utxoSet.Utxos[p.index].EntryOutpointPair)
if err != nil {
if serialization.IsMalformedError(err) {
return nil, nil, errors.Wrap(ruleerrors.ErrMalformedUTXO, "malformed utxo")
return nil, nil, errors.Wrapf(ruleerrors.ErrMalformedUTXO, "malformed utxo: %s", err)
}
return nil, nil, err
}
@ -170,5 +170,5 @@ func (p *protoUTXOSetIterator) Get() (outpoint *externalapi.DomainOutpoint, utxo
}
func protoUTXOSetToReadOnlyUTXOSetIterator(protoUTXOSet *utxoserialization.ProtoUTXOSet) model.ReadOnlyUTXOSetIterator {
return &protoUTXOSetIterator{utxoSet: protoUTXOSet}
return &protoUTXOSetIterator{utxoSet: protoUTXOSet, index: -1}
}

View File

@ -18,6 +18,12 @@ func WriteElement(w io.Writer, element interface{}) error {
// Attempt to write the element based on the concrete type via fast
// type assertions first.
switch e := element.(type) {
case int16:
err := binaryserializer.PutUint16(w, uint16(e))
if err != nil {
return err
}
return nil
case uint16:
err := binaryserializer.PutUint16(w, e)
if err != nil {
@ -121,6 +127,21 @@ func ReadElement(r io.Reader, element interface{}) error {
// Attempt to read the element based on the concrete type via fast
// type assertions first.
switch e := element.(type) {
case *int16:
rv, err := binaryserializer.Uint16(r)
if err != nil {
return err
}
*e = int16(rv)
return nil
case *uint16:
rv, err := binaryserializer.Uint16(r)
if err != nil {
return err
}
*e = rv
return nil
case *int32:
rv, err := binaryserializer.Uint32(r)
if err != nil {

View File

@ -114,7 +114,10 @@ func deserializeUTXOEntry(r io.Reader) (externalapi.UTXOEntry, error) {
}
var version uint16
err = serialization.ReadElement(r, version)
err = serialization.ReadElement(r, &version)
if err != nil {
return nil, err
}
var scriptPubKeyLen uint64
err = serialization.ReadElement(r, &scriptPubKeyLen)
if err != nil {