Svarog 281944762d
[NOD-1500] Glue between domain and application (#1007)
* [NOD-1500] Added Domain type and Constructor

* [NOD-1500] Replaced dag+txpool with domain in flowContext

* [NOD-1500] Replaced dag+txpool with domain in flowContext

* [NOD-1500] Converters: domain objects from/to appmessage

* [NOD-1500] Convert hashes to DomainHashes in appmessages

* [NOD-1500] Remove references to daghash in dagconfig

* [NOD-1500] Fixed all appmessage usages of hashes

* [NOD-1500] Update all RPC to use domain

* [NOD-1500] Big chunk of protocol flows re-wired to domain

* [NOD-1500] Finished re-wiring all protocol flows to new Domain

* [NOD-1500] Fix some mempool and kaspaminer compilation errors

* [NOD-1500] Deleted util/{block,tx,daghash} and dbaccess

* [NOD-1500] util.CoinbaseTransactionIndex -> transactionhelper.CoinbaseTransactionIndex

* [NOD-1500] Fix txsigner

* [NOD-1500] Removed all references to util/subnetworkid

* [NOD-1500] Update RpcGetBlock related messages

* [NOD-1500] Many more compilation fixes

* [NOD-1500] Return full list of missing blocks for orphan resolution

* [NOD-1500] Fixed handshake

* [NOD-1500] Fixed flowcontext compilation

* [NOD-1500] Update users of StartIBDIfRequired to handle error

* [NOD-1500] Removed some more fields from RPC

* [NOD-1500] Fix the getBlockTemplate flow

* [NOD-1500] Fix HandleGetCurrentNetwork

* [NOD-1500] Remove redundant code

* [NOD-1500] Remove obsolete notifications

* [NOD-1500] Split MiningManager and Consensus to separate fields in Domain

* [NOD-1500] Update two wrong references to location of txscript

* [NOD-1500] Added comments

* [NOD-1500] Fix some tests

* [NOD-1500] Removed serialization logic from appmessage

* [NOD-1500] Rename database/serialization/messages.proto to dbobjects.proto

* [NOD-1500] Delete integration tests

* [NOD-1500] Remove txsort

* [NOD-1500] Fix tiny bug

* [NOD-1500] Remove rogue dependancy on bchd

* [NOD-1500] Some stylistic fixes
2020-11-08 11:55:54 +02:00

201 lines
5.9 KiB
Go

package consensusserialization
import (
"io"
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
"github.com/kaspanet/kaspad/domain/consensus/utils/hashes"
"github.com/kaspanet/kaspad/domain/consensus/utils/transactionhelper"
"github.com/kaspanet/kaspad/util/binaryserializer"
"github.com/pkg/errors"
)
// txEncoding is a bitmask defining which transaction fields we
// want to encode and which to ignore.
type txEncoding uint8
const (
txEncodingFull txEncoding = 0
// TODO: Consider if we need to ever exclude the payload, or use a different approach to partial nodes
// where we'll get rid of PayloadHash field, never exclude the payload when hashing, and provide
// partial nodes with their relevant block region with a merkle proof.
txEncodingExcludePayload txEncoding = 1 << iota
txEncodingExcludeSignatureScript
)
// TransactionHashForSigning hashes the transaction and the given hash type in a way that is intended for
// signatures.
func TransactionHashForSigning(tx *externalapi.DomainTransaction, hashType uint32) *externalapi.DomainHash {
// Encode the header and double sha256 everything prior to the number of
// transactions.
writer := hashes.NewHashWriter()
err := serializeTransaction(writer, tx, txEncodingExcludePayload)
if err != nil {
// It seems like this could only happen if the writer returned an error.
// and this writer should never return an error (no allocations or possible failures)
// the only non-writer error path here is unknown types in `WriteElement`
panic(errors.Wrap(err, "TransactionHashForSigning() failed. this should never fail for structurally-valid transactions"))
}
err = WriteElement(writer, hashType)
if err != nil {
panic(errors.Wrap(err, "this should never happen. SHA256's digest should never return an error"))
}
return writer.Finalize()
}
// TransactionHash returns the transaction hash.
func TransactionHash(tx *externalapi.DomainTransaction) *externalapi.DomainHash {
// Encode the header and double sha256 everything prior to the number of
// transactions.
writer := hashes.NewHashWriter()
err := serializeTransaction(writer, tx, txEncodingExcludePayload)
if err != nil {
// It seems like this could only happen if the writer returned an error.
// and this writer should never return an error (no allocations or possible failures)
// the only non-writer error path here is unknown types in `WriteElement`
panic(errors.Wrap(err, "TransactionHash() failed. this should never fail for structurally-valid transactions"))
}
return writer.Finalize()
}
// TransactionID generates the Hash for the transaction without the signature script and payload field.
func TransactionID(tx *externalapi.DomainTransaction) *externalapi.DomainTransactionID {
// Encode the transaction, replace signature script with zeroes, cut off
// payload and calculate double sha256 on the result.
var encodingFlags txEncoding
if !transactionhelper.IsCoinBase(tx) {
encodingFlags = txEncodingExcludeSignatureScript | txEncodingExcludePayload
}
writer := hashes.NewHashWriter()
err := serializeTransaction(writer, tx, encodingFlags)
if err != nil {
// this writer never return errors (no allocations or possible failures) so errors can only come from validity checks,
// and we assume we never construct malformed transactions.
panic(errors.Wrap(err, "TransactionID() failed. this should never fail for structurally-valid transactions"))
}
transactionID := externalapi.DomainTransactionID(*writer.Finalize())
return &transactionID
}
func serializeTransaction(w io.Writer, tx *externalapi.DomainTransaction, encodingFlags txEncoding) error {
err := binaryserializer.PutUint32(w, littleEndian, uint32(tx.Version))
if err != nil {
return err
}
count := uint64(len(tx.Inputs))
err = WriteElement(w, count)
if err != nil {
return err
}
for _, ti := range tx.Inputs {
err = writeTransactionInput(w, ti, encodingFlags)
if err != nil {
return err
}
}
count = uint64(len(tx.Outputs))
err = WriteElement(w, count)
if err != nil {
return err
}
for _, output := range tx.Outputs {
err = writeTxOut(w, output)
if err != nil {
return err
}
}
err = binaryserializer.PutUint64(w, littleEndian, tx.LockTime)
if err != nil {
return err
}
_, err = w.Write(tx.SubnetworkID[:])
if err != nil {
return err
}
err = binaryserializer.PutUint64(w, littleEndian, tx.Gas)
if err != nil {
return err
}
err = WriteElement(w, &tx.PayloadHash)
if err != nil {
return err
}
if encodingFlags&txEncodingExcludePayload != txEncodingExcludePayload {
err = writeVarBytes(w, tx.Payload)
if err != nil {
return err
}
} else {
err = writeVarBytes(w, []byte{})
if err != nil {
return err
}
}
return nil
}
// writeTransactionInput encodes ti to the kaspa protocol encoding for a transaction
// input to w.
func writeTransactionInput(w io.Writer, ti *externalapi.DomainTransactionInput, encodingFlags txEncoding) error {
err := writeOutpoint(w, &ti.PreviousOutpoint)
if err != nil {
return err
}
if encodingFlags&txEncodingExcludeSignatureScript != txEncodingExcludeSignatureScript {
err = writeVarBytes(w, ti.SignatureScript)
} else {
err = writeVarBytes(w, []byte{})
}
if err != nil {
return err
}
return binaryserializer.PutUint64(w, littleEndian, ti.Sequence)
}
func writeOutpoint(w io.Writer, outpoint *externalapi.DomainOutpoint) error {
_, err := w.Write(outpoint.TransactionID[:])
if err != nil {
return err
}
return binaryserializer.PutUint32(w, littleEndian, outpoint.Index)
}
func writeVarBytes(w io.Writer, data []byte) error {
dataLength := uint64(len(data))
err := WriteElement(w, dataLength)
if err != nil {
return err
}
_, err = w.Write(data)
return err
}
func writeTxOut(w io.Writer, to *externalapi.DomainTransactionOutput) error {
err := binaryserializer.PutUint64(w, littleEndian, to.Value)
if err != nil {
return err
}
return writeVarBytes(w, to.ScriptPublicKey)
}