|
| 1 | +// Copyright 2025 Blink Labs Software |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package common |
| 16 | + |
| 17 | +import ( |
| 18 | + "errors" |
| 19 | + "testing" |
| 20 | + "time" |
| 21 | + |
| 22 | + "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano" |
| 23 | +) |
| 24 | + |
| 25 | +func TestVerifyTransaction(t *testing.T) { |
| 26 | + // Mock transaction - we don't need a real one for this test |
| 27 | + var tx Transaction |
| 28 | + |
| 29 | + slot := uint64(1000) |
| 30 | + ledgerState := &mockLedgerStateRules{} |
| 31 | + protocolParams := &mockProtocolParamsRules{} |
| 32 | + |
| 33 | + t.Run("all_rules_pass", func(t *testing.T) { |
| 34 | + rules := []UtxoValidationRuleFunc{ |
| 35 | + func(Transaction, uint64, LedgerState, ProtocolParameters) error { return nil }, |
| 36 | + func(Transaction, uint64, LedgerState, ProtocolParameters) error { return nil }, |
| 37 | + func(Transaction, uint64, LedgerState, ProtocolParameters) error { return nil }, |
| 38 | + } |
| 39 | + |
| 40 | + err := VerifyTransaction(tx, slot, ledgerState, protocolParams, rules) |
| 41 | + if err != nil { |
| 42 | + t.Errorf("expected no error, got %v", err) |
| 43 | + } |
| 44 | + }) |
| 45 | + |
| 46 | + t.Run("first_rule_fails", func(t *testing.T) { |
| 47 | + expectedErr := errors.New("first rule failed") |
| 48 | + rules := []UtxoValidationRuleFunc{ |
| 49 | + func(Transaction, uint64, LedgerState, ProtocolParameters) error { return expectedErr }, |
| 50 | + func(Transaction, uint64, LedgerState, ProtocolParameters) error { return nil }, |
| 51 | + } |
| 52 | + |
| 53 | + err := VerifyTransaction(tx, slot, ledgerState, protocolParams, rules) |
| 54 | + if err != expectedErr { |
| 55 | + t.Errorf("expected error %v, got %v", expectedErr, err) |
| 56 | + } |
| 57 | + }) |
| 58 | + |
| 59 | + t.Run("middle_rule_fails", func(t *testing.T) { |
| 60 | + expectedErr := errors.New("middle rule failed") |
| 61 | + rules := []UtxoValidationRuleFunc{ |
| 62 | + func(Transaction, uint64, LedgerState, ProtocolParameters) error { return nil }, |
| 63 | + func(Transaction, uint64, LedgerState, ProtocolParameters) error { return expectedErr }, |
| 64 | + func(Transaction, uint64, LedgerState, ProtocolParameters) error { return nil }, |
| 65 | + } |
| 66 | + |
| 67 | + err := VerifyTransaction(tx, slot, ledgerState, protocolParams, rules) |
| 68 | + if err != expectedErr { |
| 69 | + t.Errorf("expected error %v, got %v", expectedErr, err) |
| 70 | + } |
| 71 | + }) |
| 72 | + |
| 73 | + t.Run("last_rule_fails", func(t *testing.T) { |
| 74 | + expectedErr := errors.New("last rule failed") |
| 75 | + rules := []UtxoValidationRuleFunc{ |
| 76 | + func(Transaction, uint64, LedgerState, ProtocolParameters) error { return nil }, |
| 77 | + func(Transaction, uint64, LedgerState, ProtocolParameters) error { return expectedErr }, |
| 78 | + } |
| 79 | + |
| 80 | + err := VerifyTransaction(tx, slot, ledgerState, protocolParams, rules) |
| 81 | + if err != expectedErr { |
| 82 | + t.Errorf("expected error %v, got %v", expectedErr, err) |
| 83 | + } |
| 84 | + }) |
| 85 | + |
| 86 | + t.Run("empty_rules", func(t *testing.T) { |
| 87 | + rules := []UtxoValidationRuleFunc{} |
| 88 | + |
| 89 | + err := VerifyTransaction(tx, slot, ledgerState, protocolParams, rules) |
| 90 | + if err != nil { |
| 91 | + t.Errorf("expected no error with empty rules, got %v", err) |
| 92 | + } |
| 93 | + }) |
| 94 | +} |
| 95 | + |
| 96 | +// Mock types for testing |
| 97 | +type mockLedgerStateRules struct{} |
| 98 | + |
| 99 | +func (m *mockLedgerStateRules) UtxoById( |
| 100 | + input TransactionInput, |
| 101 | +) (Utxo, error) { |
| 102 | + return Utxo{}, nil |
| 103 | +} |
| 104 | + |
| 105 | +func (m *mockLedgerStateRules) StakeRegistration( |
| 106 | + key []byte, |
| 107 | +) ([]StakeRegistrationCertificate, error) { |
| 108 | + return nil, nil |
| 109 | +} |
| 110 | + |
| 111 | +func (m *mockLedgerStateRules) SlotToTime( |
| 112 | + slot uint64, |
| 113 | +) (time.Time, error) { |
| 114 | + return time.Time{}, nil |
| 115 | +} |
| 116 | + |
| 117 | +func (m *mockLedgerStateRules) TimeToSlot( |
| 118 | + t time.Time, |
| 119 | +) (uint64, error) { |
| 120 | + return 0, nil |
| 121 | +} |
| 122 | + |
| 123 | +func (m *mockLedgerStateRules) PoolCurrentState( |
| 124 | + poolKeyHash PoolKeyHash, |
| 125 | +) (*PoolRegistrationCertificate, *uint64, error) { |
| 126 | + return nil, nil, nil |
| 127 | +} |
| 128 | + |
| 129 | +func (m *mockLedgerStateRules) CalculateRewards( |
| 130 | + pots AdaPots, |
| 131 | + snapshot RewardSnapshot, |
| 132 | + params RewardParameters, |
| 133 | +) (*RewardCalculationResult, error) { |
| 134 | + return nil, errors.New("not implemented") |
| 135 | +} |
| 136 | + |
| 137 | +func (m *mockLedgerStateRules) GetAdaPots() AdaPots { return AdaPots{} } |
| 138 | +func (m *mockLedgerStateRules) UpdateAdaPots(pots AdaPots) error { return nil } |
| 139 | + |
| 140 | +func (m *mockLedgerStateRules) GetRewardSnapshot( |
| 141 | + epoch uint64, |
| 142 | +) (RewardSnapshot, error) { |
| 143 | + return RewardSnapshot{}, nil |
| 144 | +} |
| 145 | +func (m *mockLedgerStateRules) NetworkId() uint { return 0 } |
| 146 | + |
| 147 | +type mockProtocolParamsRules struct{} |
| 148 | + |
| 149 | +func (m *mockProtocolParamsRules) Utxorpc() (*cardano.PParams, error) { return nil, nil } |
0 commit comments