mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-05-20 13:56:45 +00:00

* '' * '' * '' * Changes genesis block version to 0. * a * a * All tests are done. * All tests passed for changed block version from int32 to uint16 * Adds validation of rejecting blocks with unknown versions. * Changes txn version from int32 to uint16. * . * Adds comments to exported functions. * Change functions name from ConvertFromRpcScriptPubKeyToRPCScriptPubKey to ConvertFromAppMsgRPCScriptPubKeyToRPCScriptPubKey and from ConvertFromRPCScriptPubKeyToRpcScriptPubKey to ConvertFromRPCScriptPubKeyToAppMsgRPCScriptPubKey * change comment to "ScriptPublicKey represents a Kaspad ScriptPublicKey" * delete part (tx.Version < 0) that cannot be exist on the if statement. * Revert protobuf version. * Fix a comment. * Fix a comment. * Rename a variable. * Rename a variable. * Remove a const. * Rename a type. * Rename a field. * Rename a field. * Remove commented-out code. * Remove dangerous nil case in DomainTransactionOutput.Clone(). * Remove a constant. * Fix a string. * Fix wrong totalScriptPubKeySize in transactionMassStandalonePart. * Remove a constant. * Remove an unused error. * Fix a serialization error. * Specify version types to be uint16 explicitly. * Use constants.ScriptPublicKeyVersion. * Fix a bad test. * Remove some whitespace. * Add a case to utxoEntry.Equal(). * Rename scriptPubKey to scriptPublicKey. * Remove a TODO. * Rename constants. * Rename a variable. * Add version to parseShortForm. Co-authored-by: tal <tal@daglabs.com> Co-authored-by: stasatdaglabs <stas@daglabs.com>
133 lines
3.2 KiB
Go
133 lines
3.2 KiB
Go
package utxo
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
|
|
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
|
"github.com/kaspanet/kaspad/domain/consensus/utils/serialization"
|
|
"github.com/kaspanet/kaspad/domain/consensus/utils/transactionid"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
// SerializeUTXO returns the byte-slice representation for given UTXOEntry-outpoint pair
|
|
func SerializeUTXO(entry externalapi.UTXOEntry, outpoint *externalapi.DomainOutpoint) ([]byte, error) {
|
|
w := &bytes.Buffer{}
|
|
|
|
err := serializeOutpoint(w, outpoint)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
err = serializeUTXOEntry(w, entry)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return w.Bytes(), nil
|
|
}
|
|
|
|
// DeserializeUTXO deserializes the given byte slice to UTXOEntry-outpoint pair
|
|
func DeserializeUTXO(utxoBytes []byte) (entry externalapi.UTXOEntry, outpoint *externalapi.DomainOutpoint, err error) {
|
|
r := bytes.NewReader(utxoBytes)
|
|
outpoint, err = deserializeOutpoint(r)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
entry, err = deserializeUTXOEntry(r)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
return entry, outpoint, nil
|
|
}
|
|
|
|
func serializeOutpoint(w io.Writer, outpoint *externalapi.DomainOutpoint) error {
|
|
_, err := w.Write(outpoint.TransactionID.ByteSlice())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = serialization.WriteElement(w, outpoint.Index)
|
|
if err != nil {
|
|
return errors.WithStack(err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func deserializeOutpoint(r io.Reader) (*externalapi.DomainOutpoint, error) {
|
|
transactionIDBytes := make([]byte, externalapi.DomainHashSize)
|
|
_, err := io.ReadFull(r, transactionIDBytes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
transactionID, err := transactionid.FromBytes(transactionIDBytes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var index uint32
|
|
err = serialization.ReadElement(r, &index)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &externalapi.DomainOutpoint{
|
|
TransactionID: *transactionID,
|
|
Index: index,
|
|
}, nil
|
|
}
|
|
|
|
func serializeUTXOEntry(w io.Writer, entry externalapi.UTXOEntry) error {
|
|
err := serialization.WriteElements(w, entry.BlockBlueScore(), entry.Amount(), entry.IsCoinbase())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = serialization.WriteElement(w, entry.ScriptPublicKey().Version)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
count := uint64(len(entry.ScriptPublicKey().Script))
|
|
err = serialization.WriteElement(w, count)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
_, err = w.Write(entry.ScriptPublicKey().Script)
|
|
if err != nil {
|
|
return errors.WithStack(err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func deserializeUTXOEntry(r io.Reader) (externalapi.UTXOEntry, error) {
|
|
var blockBlueScore uint64
|
|
var amount uint64
|
|
var isCoinbase bool
|
|
err := serialization.ReadElements(r, &blockBlueScore, &amount, &isCoinbase)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var version uint16
|
|
err = serialization.ReadElement(r, version)
|
|
var scriptPubKeyLen uint64
|
|
err = serialization.ReadElement(r, &scriptPubKeyLen)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
scriptPubKeyScript := make([]byte, scriptPubKeyLen)
|
|
_, err = io.ReadFull(r, scriptPubKeyScript)
|
|
if err != nil {
|
|
return nil, errors.WithStack(err)
|
|
}
|
|
scriptPubKey := externalapi.ScriptPublicKey{scriptPubKeyScript, version}
|
|
|
|
return NewUTXOEntry(amount, &scriptPubKey, isCoinbase, blockBlueScore), nil
|
|
}
|