2026-02-21 00:47:43 +00:00
|
|
|
// Copyright (c) 2017-2026 Lethean (https://lt.hn)
|
|
|
|
|
//
|
|
|
|
|
// Licensed under the European Union Public Licence (EUPL) version 1.2.
|
|
|
|
|
// SPDX-License-Identifier: EUPL-1.2
|
|
|
|
|
|
|
|
|
|
package consensus
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
|
2026-03-22 01:49:26 +00:00
|
|
|
coreerr "dappco.re/go/core/log"
|
2026-03-16 21:16:34 +00:00
|
|
|
|
2026-03-22 01:49:26 +00:00
|
|
|
"dappco.re/go/core/blockchain/config"
|
|
|
|
|
"dappco.re/go/core/blockchain/types"
|
2026-04-04 20:14:54 +00:00
|
|
|
"dappco.re/go/core/blockchain/wire"
|
2026-02-21 00:47:43 +00:00
|
|
|
)
|
|
|
|
|
|
2026-04-04 23:04:19 +00:00
|
|
|
type transactionForkState struct {
|
|
|
|
|
activeHardForkVersion uint8
|
|
|
|
|
hardForkOneActive bool
|
|
|
|
|
hardForkFourActive bool
|
|
|
|
|
hardForkFiveActive bool
|
2026-04-04 22:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
2026-04-04 23:04:19 +00:00
|
|
|
func newTransactionForkState(forks []config.HardFork, height uint64) transactionForkState {
|
|
|
|
|
return transactionForkState{
|
|
|
|
|
activeHardForkVersion: config.VersionAtHeight(forks, height),
|
|
|
|
|
hardForkOneActive: config.IsHardForkActive(forks, config.HF1, height),
|
|
|
|
|
hardForkFourActive: config.IsHardForkActive(forks, config.HF4Zarcanum, height),
|
|
|
|
|
hardForkFiveActive: config.IsHardForkActive(forks, config.HF5, height),
|
2026-04-04 22:38:20 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-21 00:47:43 +00:00
|
|
|
// ValidateTransaction performs semantic validation on a regular (non-coinbase)
|
|
|
|
|
// transaction. Checks are ordered to match the C++ validate_tx_semantic().
|
2026-04-05 07:36:31 +01:00
|
|
|
//
|
|
|
|
|
// consensus.ValidateTransaction(&tx, txBlob, config.MainnetForks, blockHeight)
|
2026-02-21 00:47:43 +00:00
|
|
|
func ValidateTransaction(tx *types.Transaction, txBlob []byte, forks []config.HardFork, height uint64) error {
|
2026-04-04 23:04:19 +00:00
|
|
|
state := newTransactionForkState(forks, height)
|
2026-02-21 00:47:43 +00:00
|
|
|
|
2026-03-16 21:32:33 +00:00
|
|
|
// 0. Transaction version.
|
2026-04-04 22:38:20 +00:00
|
|
|
if err := checkTxVersion(tx, state, height); err != nil {
|
2026-03-16 21:32:33 +00:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-21 00:47:43 +00:00
|
|
|
// 1. Blob size.
|
|
|
|
|
if uint64(len(txBlob)) >= config.MaxTransactionBlobSize {
|
2026-03-16 21:16:34 +00:00
|
|
|
return coreerr.E("ValidateTransaction", fmt.Sprintf("%d bytes", len(txBlob)), ErrTxTooLarge)
|
2026-02-21 00:47:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 2. Input count.
|
|
|
|
|
if len(tx.Vin) == 0 {
|
|
|
|
|
return ErrNoInputs
|
|
|
|
|
}
|
|
|
|
|
if uint64(len(tx.Vin)) > config.TxMaxAllowedInputs {
|
2026-03-16 21:16:34 +00:00
|
|
|
return coreerr.E("ValidateTransaction", fmt.Sprintf("%d", len(tx.Vin)), ErrTooManyInputs)
|
2026-02-21 00:47:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 3. Input types — TxInputGenesis not allowed in regular transactions.
|
2026-04-04 22:38:20 +00:00
|
|
|
if err := checkInputTypes(tx, state); err != nil {
|
2026-02-21 00:47:43 +00:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 4. Output validation.
|
2026-04-04 22:38:20 +00:00
|
|
|
if err := checkOutputs(tx, state); err != nil {
|
2026-02-21 00:47:43 +00:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-04 20:14:54 +00:00
|
|
|
// 4a. HF5 asset operation validation inside extra.
|
2026-04-04 23:04:19 +00:00
|
|
|
if err := checkAssetOperations(tx.Extra, state.hardForkFiveActive); err != nil {
|
2026-04-04 20:14:54 +00:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-21 00:47:43 +00:00
|
|
|
// 5. Money overflow.
|
|
|
|
|
if _, err := sumInputs(tx); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if _, err := sumOutputs(tx); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 6. Key image uniqueness.
|
|
|
|
|
if err := checkKeyImages(tx); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 7. Balance check (pre-HF4 only — post-HF4 uses commitment proofs).
|
2026-04-04 23:04:19 +00:00
|
|
|
if !state.hardForkFourActive {
|
2026-02-21 00:47:43 +00:00
|
|
|
if _, err := TxFee(tx); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-16 21:32:33 +00:00
|
|
|
// checkTxVersion validates that the transaction version is appropriate for the
|
|
|
|
|
// current hardfork era.
|
|
|
|
|
//
|
2026-04-04 20:04:24 +00:00
|
|
|
// Pre-HF4: regular transactions must use version 1.
|
|
|
|
|
// HF4 era: regular transactions must use version 2.
|
|
|
|
|
// HF5+: transaction version must be exactly version 3 and the embedded
|
|
|
|
|
// hardfork_id must match the active hardfork version.
|
2026-04-04 23:04:19 +00:00
|
|
|
func checkTxVersion(tx *types.Transaction, state transactionForkState, height uint64) error {
|
2026-04-04 20:04:24 +00:00
|
|
|
var expectedVersion uint64
|
|
|
|
|
switch {
|
2026-04-04 23:04:19 +00:00
|
|
|
case state.hardForkFiveActive:
|
2026-04-04 20:04:24 +00:00
|
|
|
expectedVersion = types.VersionPostHF5
|
2026-04-04 23:04:19 +00:00
|
|
|
case state.hardForkFourActive:
|
2026-04-04 20:04:24 +00:00
|
|
|
expectedVersion = types.VersionPostHF4
|
|
|
|
|
default:
|
|
|
|
|
expectedVersion = types.VersionPreHF4
|
2026-03-16 21:32:33 +00:00
|
|
|
}
|
|
|
|
|
|
2026-04-04 20:04:24 +00:00
|
|
|
if tx.Version != expectedVersion {
|
2026-03-16 21:32:33 +00:00
|
|
|
return coreerr.E("checkTxVersion",
|
2026-04-04 20:04:24 +00:00
|
|
|
fmt.Sprintf("version %d invalid at height %d (expected %d)", tx.Version, height, expectedVersion),
|
2026-03-16 21:32:33 +00:00
|
|
|
ErrTxVersionInvalid)
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-04 23:04:19 +00:00
|
|
|
if tx.Version >= types.VersionPostHF5 && tx.HardforkID != state.activeHardForkVersion {
|
2026-04-04 18:34:49 +00:00
|
|
|
return coreerr.E("checkTxVersion",
|
2026-04-04 23:04:19 +00:00
|
|
|
fmt.Sprintf("hardfork id %d does not match active fork %d at height %d", tx.HardforkID, state.activeHardForkVersion, height),
|
2026-04-04 18:34:49 +00:00
|
|
|
ErrTxVersionInvalid)
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-16 21:32:33 +00:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-04 23:04:19 +00:00
|
|
|
func checkInputTypes(tx *types.Transaction, state transactionForkState) error {
|
2026-02-21 00:47:43 +00:00
|
|
|
for _, vin := range tx.Vin {
|
|
|
|
|
switch vin.(type) {
|
|
|
|
|
case types.TxInputToKey:
|
|
|
|
|
// Always valid.
|
|
|
|
|
case types.TxInputGenesis:
|
2026-03-16 21:16:34 +00:00
|
|
|
return coreerr.E("checkInputTypes", "txin_gen in regular transaction", ErrInvalidInputType)
|
2026-03-16 21:32:33 +00:00
|
|
|
case types.TxInputHTLC, types.TxInputMultisig:
|
|
|
|
|
// HTLC and multisig inputs require at least HF1.
|
2026-04-04 23:04:19 +00:00
|
|
|
if !state.hardForkOneActive {
|
2026-03-16 21:32:33 +00:00
|
|
|
return coreerr.E("checkInputTypes", fmt.Sprintf("tag %d pre-HF1", vin.InputType()), ErrInvalidInputType)
|
|
|
|
|
}
|
2026-04-04 22:18:53 +00:00
|
|
|
case types.TxInputZC:
|
2026-04-04 23:04:19 +00:00
|
|
|
if !state.hardForkFourActive {
|
2026-03-16 21:16:34 +00:00
|
|
|
return coreerr.E("checkInputTypes", fmt.Sprintf("tag %d pre-HF4", vin.InputType()), ErrInvalidInputType)
|
2026-02-21 00:47:43 +00:00
|
|
|
}
|
2026-04-04 22:18:53 +00:00
|
|
|
default:
|
|
|
|
|
return coreerr.E("checkInputTypes", fmt.Sprintf("unsupported input type %T", vin), ErrInvalidInputType)
|
2026-02-21 00:47:43 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-04 23:04:19 +00:00
|
|
|
func checkOutputs(tx *types.Transaction, state transactionForkState) error {
|
2026-02-21 00:47:43 +00:00
|
|
|
if len(tx.Vout) == 0 {
|
|
|
|
|
return ErrNoOutputs
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-04 23:04:19 +00:00
|
|
|
if state.hardForkFourActive && uint64(len(tx.Vout)) < config.TxMinAllowedOutputs {
|
2026-03-16 21:16:34 +00:00
|
|
|
return coreerr.E("checkOutputs", fmt.Sprintf("%d (min %d)", len(tx.Vout), config.TxMinAllowedOutputs), ErrTooFewOutputs)
|
2026-02-21 00:47:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if uint64(len(tx.Vout)) > config.TxMaxAllowedOutputs {
|
2026-03-16 21:16:34 +00:00
|
|
|
return coreerr.E("checkOutputs", fmt.Sprintf("%d", len(tx.Vout)), ErrTooManyOutputs)
|
2026-02-21 00:47:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for i, vout := range tx.Vout {
|
|
|
|
|
switch o := vout.(type) {
|
|
|
|
|
case types.TxOutputBare:
|
|
|
|
|
if o.Amount == 0 {
|
2026-03-16 21:16:34 +00:00
|
|
|
return coreerr.E("checkOutputs", fmt.Sprintf("output %d has zero amount", i), ErrInvalidOutput)
|
2026-03-16 20:36:58 +00:00
|
|
|
}
|
2026-04-04 19:56:50 +00:00
|
|
|
// Only known transparent output targets are accepted.
|
2026-03-16 21:32:33 +00:00
|
|
|
switch o.Target.(type) {
|
2026-04-04 19:56:50 +00:00
|
|
|
case types.TxOutToKey:
|
2026-03-16 21:32:33 +00:00
|
|
|
case types.TxOutHTLC, types.TxOutMultisig:
|
2026-04-04 23:04:19 +00:00
|
|
|
if !state.hardForkOneActive {
|
2026-03-16 21:32:33 +00:00
|
|
|
return coreerr.E("checkOutputs", fmt.Sprintf("output %d: HTLC/multisig target pre-HF1", i), ErrInvalidOutput)
|
|
|
|
|
}
|
2026-04-04 19:56:50 +00:00
|
|
|
case nil:
|
|
|
|
|
return coreerr.E("checkOutputs", fmt.Sprintf("output %d: missing target", i), ErrInvalidOutput)
|
|
|
|
|
default:
|
|
|
|
|
return coreerr.E("checkOutputs", fmt.Sprintf("output %d: unsupported target %T", i, o.Target), ErrInvalidOutput)
|
2026-03-16 21:32:33 +00:00
|
|
|
}
|
2026-02-21 00:47:43 +00:00
|
|
|
case types.TxOutputZarcanum:
|
2026-04-04 23:04:19 +00:00
|
|
|
if !state.hardForkFourActive {
|
2026-04-04 22:18:53 +00:00
|
|
|
return coreerr.E("checkOutputs", fmt.Sprintf("output %d: Zarcanum output pre-HF4", i), ErrInvalidOutput)
|
|
|
|
|
}
|
2026-04-04 19:56:50 +00:00
|
|
|
default:
|
|
|
|
|
return coreerr.E("checkOutputs", fmt.Sprintf("output %d: unsupported output type %T", i, vout), ErrInvalidOutput)
|
2026-02-21 00:47:43 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func checkKeyImages(tx *types.Transaction) error {
|
|
|
|
|
seen := make(map[types.KeyImage]struct{})
|
|
|
|
|
for _, vin := range tx.Vin {
|
2026-03-16 21:32:33 +00:00
|
|
|
var ki types.KeyImage
|
|
|
|
|
switch v := vin.(type) {
|
|
|
|
|
case types.TxInputToKey:
|
|
|
|
|
ki = v.KeyImage
|
|
|
|
|
case types.TxInputHTLC:
|
|
|
|
|
ki = v.KeyImage
|
|
|
|
|
default:
|
2026-02-21 00:47:43 +00:00
|
|
|
continue
|
|
|
|
|
}
|
2026-03-16 21:32:33 +00:00
|
|
|
if _, exists := seen[ki]; exists {
|
|
|
|
|
return coreerr.E("checkKeyImages", ki.String(), ErrDuplicateKeyImage)
|
2026-02-21 00:47:43 +00:00
|
|
|
}
|
2026-03-16 21:32:33 +00:00
|
|
|
seen[ki] = struct{}{}
|
2026-02-21 00:47:43 +00:00
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2026-04-04 20:14:54 +00:00
|
|
|
|
2026-04-04 23:04:19 +00:00
|
|
|
func checkAssetOperations(extra []byte, hardForkFiveActive bool) error {
|
2026-04-04 20:14:54 +00:00
|
|
|
if len(extra) == 0 {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
elements, err := wire.DecodeVariantVector(extra)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return coreerr.E("checkAssetOperations", "parse extra", ErrInvalidExtra)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for i, elem := range elements {
|
|
|
|
|
if elem.Tag != types.AssetDescriptorOperationTag {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2026-04-04 23:04:19 +00:00
|
|
|
if !hardForkFiveActive {
|
2026-04-04 20:14:54 +00:00
|
|
|
return coreerr.E("checkAssetOperations", fmt.Sprintf("extra[%d]: asset descriptor operation pre-HF5", i), ErrInvalidExtra)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
op, err := wire.DecodeAssetDescriptorOperation(elem.Data)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return coreerr.E("checkAssetOperations", fmt.Sprintf("extra[%d]: decode asset descriptor operation", i), ErrInvalidExtra)
|
|
|
|
|
}
|
2026-04-04 22:09:17 +00:00
|
|
|
if err := op.Validate(); err != nil {
|
2026-04-04 20:14:54 +00:00
|
|
|
return coreerr.E("checkAssetOperations", fmt.Sprintf("extra[%d]", i), err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|