mirror of
https://github.com/luxfi/proto.git
synced 2026-10-30 15:21:09 +00:00
- Rename InitCtx → InitRuntime throughout - Update imports for new package versions - Update utxo/secp256k1fx usage - Update consensus context references
98 lines
2.4 KiB
Go
98 lines
2.4 KiB
Go
// Copyright (C) 2019-2025, Lux Industries, Inc. All rights reserved.
|
|
// See the file LICENSE for licensing terms.
|
|
|
|
package txs
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"github.com/luxfi/runtime"
|
|
|
|
"github.com/luxfi/ids"
|
|
"github.com/luxfi/math/set"
|
|
lux "github.com/luxfi/utxo"
|
|
"github.com/luxfi/utxo/secp256k1fx"
|
|
)
|
|
|
|
var (
|
|
_ UnsignedTx = (*ImportTx)(nil)
|
|
|
|
errNoImportInputs = errors.New("tx has no imported inputs")
|
|
)
|
|
|
|
// ImportTx is an unsigned importTx
|
|
type ImportTx struct {
|
|
BaseTx `serialize:"true"`
|
|
|
|
// Which chain to consume the funds from
|
|
SourceChain ids.ID `serialize:"true" json:"sourceChain"`
|
|
|
|
// Inputs that consume UTXOs produced on the chain
|
|
ImportedInputs []*lux.TransferableInput `serialize:"true" json:"importedInputs"`
|
|
}
|
|
|
|
// InitRuntime sets the FxID fields in the inputs and outputs of this
|
|
// [ImportTx]. Also sets the [ctx] to the given [vm.ctx] so that
|
|
// the addresses can be json marshalled into human readable format
|
|
func (tx *ImportTx) InitRuntime(rt *runtime.Runtime) {
|
|
tx.BaseTx.InitRuntime(rt)
|
|
for _, in := range tx.ImportedInputs {
|
|
in.FxID = secp256k1fx.ID
|
|
}
|
|
}
|
|
|
|
// InputUTXOs returns the UTXOIDs of the imported funds
|
|
func (tx *ImportTx) InputUTXOs() set.Set[ids.ID] {
|
|
set := set.NewSet[ids.ID](len(tx.ImportedInputs))
|
|
for _, in := range tx.ImportedInputs {
|
|
set.Add(in.InputID())
|
|
}
|
|
return set
|
|
}
|
|
|
|
func (tx *ImportTx) InputIDs() set.Set[ids.ID] {
|
|
inputs := tx.BaseTx.InputIDs()
|
|
atomicInputs := tx.InputUTXOs()
|
|
return inputs.Union(atomicInputs)
|
|
}
|
|
|
|
// SyntacticVerify this transaction is well-formed
|
|
func (tx *ImportTx) SyntacticVerify(rt *runtime.Runtime) error {
|
|
switch {
|
|
case tx == nil:
|
|
return ErrNilTx
|
|
case tx.SyntacticallyVerified: // already passed syntactic verification
|
|
return nil
|
|
case len(tx.ImportedInputs) == 0:
|
|
return errNoImportInputs
|
|
}
|
|
|
|
if err := tx.BaseTx.SyntacticVerify(rt); err != nil {
|
|
return err
|
|
}
|
|
|
|
for _, in := range tx.ImportedInputs {
|
|
if err := in.Verify(); err != nil {
|
|
return fmt.Errorf("input failed verification: %w", err)
|
|
}
|
|
}
|
|
if !isSortedAndUniqueByCompare(tx.ImportedInputs) {
|
|
return errInputsNotSortedUnique
|
|
}
|
|
|
|
tx.SyntacticallyVerified = true
|
|
return nil
|
|
}
|
|
|
|
func (tx *ImportTx) Visit(visitor Visitor) error {
|
|
return visitor.ImportTx(tx)
|
|
}
|
|
|
|
// InitializeWithContext initializes the transaction with consensus context
|
|
func (tx *ImportTx) InitializeWithContext(ctx context.Context) error {
|
|
// Initialize any context-dependent fields here
|
|
return nil
|
|
}
|