Skip to content

Commit e585ccb

Browse files
authored
refactor: use variable slot for 'Utxo' validation rules (#884)
Signed-off-by: Aurora Gaffney <[email protected]>
1 parent c5930c6 commit e585ccb

File tree

3 files changed

+41
-61
lines changed

3 files changed

+41
-61
lines changed

ledger/common/rules.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@
1414

1515
package common
1616

17-
type UtxoValidationRuleFunc func(Transaction, LedgerState, TipState, ProtocolParameters) error
17+
type UtxoValidationRuleFunc func(Transaction, uint64, LedgerState, ProtocolParameters) error

ledger/shelley/rules.go

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,31 +35,27 @@ var UtxoValidationRules = []common.UtxoValidationRuleFunc{
3535
}
3636

3737
// UtxoValidateTimeToLive ensures that the current tip slot is not after the specified TTL value
38-
func UtxoValidateTimeToLive(tx common.Transaction, ls common.LedgerState, ts common.TipState, pp common.ProtocolParameters) error {
39-
tip, err := ts.Tip()
40-
if err != nil {
41-
return err
42-
}
38+
func UtxoValidateTimeToLive(tx common.Transaction, slot uint64, ls common.LedgerState, pp common.ProtocolParameters) error {
4339
ttl := tx.TTL()
44-
if ttl == 0 || ttl >= tip.Point.Slot {
40+
if ttl == 0 || ttl >= slot {
4541
return nil
4642
}
4743
return ExpiredUtxoError{
4844
Ttl: ttl,
49-
Slot: tip.Point.Slot,
45+
Slot: slot,
5046
}
5147
}
5248

5349
// UtxoValidateInputSetEmptyUtxo ensures that the input set is not empty
54-
func UtxoValidateInputSetEmptyUtxo(tx common.Transaction, ls common.LedgerState, ts common.TipState, pp common.ProtocolParameters) error {
50+
func UtxoValidateInputSetEmptyUtxo(tx common.Transaction, slot uint64, ls common.LedgerState, pp common.ProtocolParameters) error {
5551
if len(tx.Inputs()) > 0 {
5652
return nil
5753
}
5854
return InputSetEmptyUtxoError{}
5955
}
6056

6157
// UtxoValidateFeeTooSmallUtxo ensures that the fee is at least the calculated minimum
62-
func UtxoValidateFeeTooSmallUtxo(tx common.Transaction, ls common.LedgerState, ts common.TipState, pp common.ProtocolParameters) error {
58+
func UtxoValidateFeeTooSmallUtxo(tx common.Transaction, slot uint64, ls common.LedgerState, pp common.ProtocolParameters) error {
6359
minFee, err := MinFeeTx(tx, pp)
6460
if err != nil {
6561
return err
@@ -74,7 +70,7 @@ func UtxoValidateFeeTooSmallUtxo(tx common.Transaction, ls common.LedgerState, t
7470
}
7571

7672
// UtxoValidateBadInputsUtxo ensures that all inputs are present in the ledger state (have not been spent)
77-
func UtxoValidateBadInputsUtxo(tx common.Transaction, ls common.LedgerState, ts common.TipState, pp common.ProtocolParameters) error {
73+
func UtxoValidateBadInputsUtxo(tx common.Transaction, slot uint64, ls common.LedgerState, pp common.ProtocolParameters) error {
7874
var badInputs []common.TransactionInput
7975
for _, tmpInput := range tx.Inputs() {
8076
_, err := ls.UtxoById(tmpInput)
@@ -91,7 +87,7 @@ func UtxoValidateBadInputsUtxo(tx common.Transaction, ls common.LedgerState, ts
9187
}
9288

9389
// UtxoValidateWrongNetwork ensures that all output addresses use the correct network ID
94-
func UtxoValidateWrongNetwork(tx common.Transaction, ls common.LedgerState, ts common.TipState, pp common.ProtocolParameters) error {
90+
func UtxoValidateWrongNetwork(tx common.Transaction, slot uint64, ls common.LedgerState, pp common.ProtocolParameters) error {
9591
networkId := ls.NetworkId()
9692
var badAddrs []common.Address
9793
for _, tmpOutput := range tx.Outputs() {
@@ -111,7 +107,7 @@ func UtxoValidateWrongNetwork(tx common.Transaction, ls common.LedgerState, ts c
111107
}
112108

113109
// UtxoValidateWrongNetworkWithdrawal ensures that all withdrawal addresses use the correct network ID
114-
func UtxoValidateWrongNetworkWithdrawal(tx common.Transaction, ls common.LedgerState, ts common.TipState, pp common.ProtocolParameters) error {
110+
func UtxoValidateWrongNetworkWithdrawal(tx common.Transaction, slot uint64, ls common.LedgerState, pp common.ProtocolParameters) error {
115111
networkId := ls.NetworkId()
116112
var badAddrs []common.Address
117113
for addr := range tx.Withdrawals() {
@@ -130,7 +126,7 @@ func UtxoValidateWrongNetworkWithdrawal(tx common.Transaction, ls common.LedgerS
130126
}
131127

132128
// UtxoValidateValueNotConservedUtxo ensures that the consumed value equals the produced value
133-
func UtxoValidateValueNotConservedUtxo(tx common.Transaction, ls common.LedgerState, ts common.TipState, pp common.ProtocolParameters) error {
129+
func UtxoValidateValueNotConservedUtxo(tx common.Transaction, slot uint64, ls common.LedgerState, pp common.ProtocolParameters) error {
134130
// Calculate consumed value
135131
// consumed = value from input(s) + withdrawals + refunds(?)
136132
var consumedValue uint64
@@ -162,7 +158,7 @@ func UtxoValidateValueNotConservedUtxo(tx common.Transaction, ls common.LedgerSt
162158
}
163159

164160
// UtxoValidateOutputTooSmallUtxo ensures that outputs have at least the minimum value
165-
func UtxoValidateOutputTooSmallUtxo(tx common.Transaction, ls common.LedgerState, ts common.TipState, pp common.ProtocolParameters) error {
161+
func UtxoValidateOutputTooSmallUtxo(tx common.Transaction, slot uint64, ls common.LedgerState, pp common.ProtocolParameters) error {
166162
minCoin, err := MinCoinTxOut(tx, pp)
167163
if err != nil {
168164
return err
@@ -182,7 +178,7 @@ func UtxoValidateOutputTooSmallUtxo(tx common.Transaction, ls common.LedgerState
182178
}
183179

184180
// UtxoValidateOutputBootAddrAttrsTooBig ensures that bootstrap (Byron) addresses don't have attributes that are too large
185-
func UtxoValidateOutputBootAddrAttrsTooBig(tx common.Transaction, ls common.LedgerState, ts common.TipState, pp common.ProtocolParameters) error {
181+
func UtxoValidateOutputBootAddrAttrsTooBig(tx common.Transaction, slot uint64, ls common.LedgerState, pp common.ProtocolParameters) error {
186182
var badOutputs []common.TransactionOutput
187183
for _, tmpOutput := range tx.Outputs() {
188184
addr := tmpOutput.Address()
@@ -208,7 +204,7 @@ func UtxoValidateOutputBootAddrAttrsTooBig(tx common.Transaction, ls common.Ledg
208204
}
209205

210206
// UtxoValidateMaxTxSizeUtxo ensures that a transaction does not exceed the max size
211-
func UtxoValidateMaxTxSizeUtxo(tx common.Transaction, ls common.LedgerState, ts common.TipState, pp common.ProtocolParameters) error {
207+
func UtxoValidateMaxTxSizeUtxo(tx common.Transaction, slot uint64, ls common.LedgerState, pp common.ProtocolParameters) error {
212208
tmpTx, ok := tx.(*ShelleyTransaction)
213209
if !ok {
214210
return fmt.Errorf("transaction is not expected type")

0 commit comments

Comments
 (0)