300 lines
9.8 KiB
Go
300 lines
9.8 KiB
Go
// 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"
|
|
"unicode/utf8"
|
|
|
|
coreerr "dappco.re/go/core/log"
|
|
|
|
"dappco.re/go/core/blockchain/config"
|
|
"dappco.re/go/core/blockchain/types"
|
|
"dappco.re/go/core/blockchain/wire"
|
|
)
|
|
|
|
// ValidateTransaction performs semantic validation on a regular (non-coinbase)
|
|
// transaction. Checks are ordered to match the C++ validate_tx_semantic().
|
|
func ValidateTransaction(tx *types.Transaction, txBlob []byte, forks []config.HardFork, height uint64) error {
|
|
hf4Active := config.IsHardForkActive(forks, config.HF4Zarcanum, height)
|
|
hf5Active := config.IsHardForkActive(forks, config.HF5, height)
|
|
|
|
// 0. Transaction version.
|
|
if err := checkTxVersion(tx, forks, height); err != nil {
|
|
return err
|
|
}
|
|
|
|
// 1. Blob size.
|
|
if uint64(len(txBlob)) >= config.MaxTransactionBlobSize {
|
|
return coreerr.E("ValidateTransaction", fmt.Sprintf("%d bytes", len(txBlob)), ErrTxTooLarge)
|
|
}
|
|
|
|
// 2. Input count.
|
|
if len(tx.Vin) == 0 {
|
|
return ErrNoInputs
|
|
}
|
|
if uint64(len(tx.Vin)) > config.TxMaxAllowedInputs {
|
|
return coreerr.E("ValidateTransaction", fmt.Sprintf("%d", len(tx.Vin)), ErrTooManyInputs)
|
|
}
|
|
|
|
hf1Active := config.IsHardForkActive(forks, config.HF1, height)
|
|
|
|
// 3. Input types — TxInputGenesis not allowed in regular transactions.
|
|
if err := checkInputTypes(tx, hf1Active, hf4Active); err != nil {
|
|
return err
|
|
}
|
|
|
|
// 4. Output validation.
|
|
if err := checkOutputs(tx, hf1Active, hf4Active); err != nil {
|
|
return err
|
|
}
|
|
|
|
// 4a. HF5 asset operation validation inside extra.
|
|
if err := checkAssetOperations(tx.Extra, hf5Active); err != nil {
|
|
return err
|
|
}
|
|
|
|
// 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).
|
|
if !hf4Active {
|
|
if _, err := TxFee(tx); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// checkTxVersion validates that the transaction version is appropriate for the
|
|
// current hardfork era.
|
|
//
|
|
// 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.
|
|
func checkTxVersion(tx *types.Transaction, forks []config.HardFork, height uint64) error {
|
|
hf5Active := config.IsHardForkActive(forks, config.HF5, height)
|
|
hf4Active := config.IsHardForkActive(forks, config.HF4Zarcanum, height)
|
|
currentFork := config.VersionAtHeight(forks, height)
|
|
|
|
var expectedVersion uint64
|
|
switch {
|
|
case hf5Active:
|
|
expectedVersion = types.VersionPostHF5
|
|
case hf4Active:
|
|
expectedVersion = types.VersionPostHF4
|
|
default:
|
|
expectedVersion = types.VersionPreHF4
|
|
}
|
|
|
|
if tx.Version != expectedVersion {
|
|
return coreerr.E("checkTxVersion",
|
|
fmt.Sprintf("version %d invalid at height %d (expected %d)", tx.Version, height, expectedVersion),
|
|
ErrTxVersionInvalid)
|
|
}
|
|
|
|
if tx.Version >= types.VersionPostHF5 && tx.HardforkID != currentFork {
|
|
return coreerr.E("checkTxVersion",
|
|
fmt.Sprintf("hardfork id %d does not match active fork %d at height %d", tx.HardforkID, currentFork, height),
|
|
ErrTxVersionInvalid)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func checkInputTypes(tx *types.Transaction, hf1Active, hf4Active bool) error {
|
|
for _, vin := range tx.Vin {
|
|
switch vin.(type) {
|
|
case types.TxInputToKey:
|
|
// Always valid.
|
|
case types.TxInputGenesis:
|
|
return coreerr.E("checkInputTypes", "txin_gen in regular transaction", ErrInvalidInputType)
|
|
case types.TxInputHTLC, types.TxInputMultisig:
|
|
// HTLC and multisig inputs require at least HF1.
|
|
if !hf1Active {
|
|
return coreerr.E("checkInputTypes", fmt.Sprintf("tag %d pre-HF1", vin.InputType()), ErrInvalidInputType)
|
|
}
|
|
default:
|
|
// Future types (ZC) — accept if HF4+.
|
|
if !hf4Active {
|
|
return coreerr.E("checkInputTypes", fmt.Sprintf("tag %d pre-HF4", vin.InputType()), ErrInvalidInputType)
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func checkOutputs(tx *types.Transaction, hf1Active, hf4Active bool) error {
|
|
if len(tx.Vout) == 0 {
|
|
return ErrNoOutputs
|
|
}
|
|
|
|
if hf4Active && uint64(len(tx.Vout)) < config.TxMinAllowedOutputs {
|
|
return coreerr.E("checkOutputs", fmt.Sprintf("%d (min %d)", len(tx.Vout), config.TxMinAllowedOutputs), ErrTooFewOutputs)
|
|
}
|
|
|
|
if uint64(len(tx.Vout)) > config.TxMaxAllowedOutputs {
|
|
return coreerr.E("checkOutputs", fmt.Sprintf("%d", len(tx.Vout)), ErrTooManyOutputs)
|
|
}
|
|
|
|
for i, vout := range tx.Vout {
|
|
switch o := vout.(type) {
|
|
case types.TxOutputBare:
|
|
if o.Amount == 0 {
|
|
return coreerr.E("checkOutputs", fmt.Sprintf("output %d has zero amount", i), ErrInvalidOutput)
|
|
}
|
|
// Only known transparent output targets are accepted.
|
|
switch o.Target.(type) {
|
|
case types.TxOutToKey:
|
|
case types.TxOutHTLC, types.TxOutMultisig:
|
|
if !hf1Active {
|
|
return coreerr.E("checkOutputs", fmt.Sprintf("output %d: HTLC/multisig target pre-HF1", i), ErrInvalidOutput)
|
|
}
|
|
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)
|
|
}
|
|
case types.TxOutputZarcanum:
|
|
// Validated by proof verification.
|
|
default:
|
|
return coreerr.E("checkOutputs", fmt.Sprintf("output %d: unsupported output type %T", i, vout), ErrInvalidOutput)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func checkKeyImages(tx *types.Transaction) error {
|
|
seen := make(map[types.KeyImage]struct{})
|
|
for _, vin := range tx.Vin {
|
|
var ki types.KeyImage
|
|
switch v := vin.(type) {
|
|
case types.TxInputToKey:
|
|
ki = v.KeyImage
|
|
case types.TxInputHTLC:
|
|
ki = v.KeyImage
|
|
default:
|
|
continue
|
|
}
|
|
if _, exists := seen[ki]; exists {
|
|
return coreerr.E("checkKeyImages", ki.String(), ErrDuplicateKeyImage)
|
|
}
|
|
seen[ki] = struct{}{}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func checkAssetOperations(extra []byte, hf5Active bool) error {
|
|
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
|
|
}
|
|
if !hf5Active {
|
|
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)
|
|
}
|
|
if err := validateAssetDescriptorOperation(op); err != nil {
|
|
return coreerr.E("checkAssetOperations", fmt.Sprintf("extra[%d]", i), err)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func validateAssetDescriptorOperation(op types.AssetDescriptorOperation) error {
|
|
switch op.Version {
|
|
case 0, 1:
|
|
default:
|
|
return coreerr.E("validateAssetDescriptorOperation", fmt.Sprintf("unsupported version %d", op.Version), ErrInvalidExtra)
|
|
}
|
|
|
|
switch op.OperationType {
|
|
case types.AssetOpRegister:
|
|
if !op.AssetID.IsZero() {
|
|
return coreerr.E("validateAssetDescriptorOperation", "register operation must not carry asset id", ErrInvalidExtra)
|
|
}
|
|
if op.Descriptor == nil {
|
|
return coreerr.E("validateAssetDescriptorOperation", "register operation missing descriptor", ErrInvalidExtra)
|
|
}
|
|
if err := validateAssetDescriptorBase(*op.Descriptor); err != nil {
|
|
return err
|
|
}
|
|
if op.AmountToEmit != 0 || op.AmountToBurn != 0 {
|
|
return coreerr.E("validateAssetDescriptorOperation", "register operation must not include emission or burn amounts", ErrInvalidExtra)
|
|
}
|
|
case types.AssetOpEmit, types.AssetOpUpdate, types.AssetOpBurn, types.AssetOpPublicBurn:
|
|
if op.AssetID.IsZero() {
|
|
return coreerr.E("validateAssetDescriptorOperation", "operation must carry asset id", ErrInvalidExtra)
|
|
}
|
|
if op.OperationType == types.AssetOpUpdate && op.Descriptor == nil {
|
|
return coreerr.E("validateAssetDescriptorOperation", "update operation missing descriptor", ErrInvalidExtra)
|
|
}
|
|
if op.OperationType == types.AssetOpEmit && op.AmountToEmit == 0 {
|
|
return coreerr.E("validateAssetDescriptorOperation", "emit operation has zero amount", ErrInvalidExtra)
|
|
}
|
|
if (op.OperationType == types.AssetOpBurn || op.OperationType == types.AssetOpPublicBurn) && op.AmountToBurn == 0 {
|
|
return coreerr.E("validateAssetDescriptorOperation", "burn operation has zero amount", ErrInvalidExtra)
|
|
}
|
|
if op.OperationType == types.AssetOpUpdate && op.Descriptor != nil {
|
|
if err := validateAssetDescriptorBase(*op.Descriptor); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
default:
|
|
return coreerr.E("validateAssetDescriptorOperation", fmt.Sprintf("unsupported operation type %d", op.OperationType), ErrInvalidExtra)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func validateAssetDescriptorBase(base types.AssetDescriptorBase) error {
|
|
tickerLen := utf8.RuneCountInString(base.Ticker)
|
|
fullNameLen := utf8.RuneCountInString(base.FullName)
|
|
|
|
if base.TotalMaxSupply == 0 {
|
|
return coreerr.E("validateAssetDescriptorBase", "total max supply must be non-zero", ErrInvalidExtra)
|
|
}
|
|
if base.CurrentSupply > base.TotalMaxSupply {
|
|
return coreerr.E("validateAssetDescriptorBase", fmt.Sprintf("current supply %d exceeds max supply %d", base.CurrentSupply, base.TotalMaxSupply), ErrInvalidExtra)
|
|
}
|
|
if tickerLen == 0 || tickerLen > 6 {
|
|
return coreerr.E("validateAssetDescriptorBase", fmt.Sprintf("ticker length %d out of range", tickerLen), ErrInvalidExtra)
|
|
}
|
|
if fullNameLen == 0 || fullNameLen > 64 {
|
|
return coreerr.E("validateAssetDescriptorBase", fmt.Sprintf("full name length %d out of range", fullNameLen), ErrInvalidExtra)
|
|
}
|
|
if base.OwnerKey.IsZero() {
|
|
return coreerr.E("validateAssetDescriptorBase", "owner key must be non-zero", ErrInvalidExtra)
|
|
}
|
|
|
|
return nil
|
|
}
|