Skip to content

Commit 209b1b5

Browse files
authored
feat: Mary era 'Utxo' validation rules (#888)
Fixes #877 Signed-off-by: Aurora Gaffney <[email protected]>
1 parent 094b4bc commit 209b1b5

File tree

3 files changed

+1092
-0
lines changed

3 files changed

+1092
-0
lines changed

ledger/mary/errors.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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 mary
16+
17+
import (
18+
"fmt"
19+
"strings"
20+
21+
"github.com/blinklabs-io/gouroboros/ledger/common"
22+
)
23+
24+
type OutputTooBigUtxoError struct {
25+
Outputs []common.TransactionOutput
26+
}
27+
28+
func (e OutputTooBigUtxoError) Error() string {
29+
tmpOutputs := make([]string, 0, len(e.Outputs))
30+
for idx, tmpOutput := range e.Outputs {
31+
tmpOutputs[idx] = fmt.Sprintf("%#v", tmpOutput)
32+
}
33+
return fmt.Sprintf(
34+
"output value too large: %s",
35+
strings.Join(tmpOutputs, ", "),
36+
)
37+
}

ledger/mary/rules.go

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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 mary
16+
17+
import (
18+
"fmt"
19+
20+
"github.com/blinklabs-io/gouroboros/cbor"
21+
"github.com/blinklabs-io/gouroboros/ledger/allegra"
22+
"github.com/blinklabs-io/gouroboros/ledger/common"
23+
"github.com/blinklabs-io/gouroboros/ledger/shelley"
24+
)
25+
26+
const (
27+
maxTransactionOutputValueSize = 4000
28+
)
29+
30+
var UtxoValidationRules = []common.UtxoValidationRuleFunc{
31+
UtxoValidateOutsideValidityIntervalUtxo,
32+
UtxoValidateInputSetEmptyUtxo,
33+
UtxoValidateFeeTooSmallUtxo,
34+
UtxoValidateBadInputsUtxo,
35+
UtxoValidateWrongNetwork,
36+
UtxoValidateWrongNetworkWithdrawal,
37+
UtxoValidateValueNotConservedUtxo,
38+
UtxoValidateOutputTooSmallUtxo,
39+
UtxoValidateOutputTooBigUtxo,
40+
UtxoValidateOutputBootAddrAttrsTooBig,
41+
UtxoValidateMaxTxSizeUtxo,
42+
}
43+
44+
// UtxoValidateOutputTooBigUtxo ensures that transaction output values are not too large
45+
func UtxoValidateOutputTooBigUtxo(tx common.Transaction, slot uint64, _ common.LedgerState, _ common.ProtocolParameters) error {
46+
var badOutputs []common.TransactionOutput
47+
for _, txOutput := range tx.Outputs() {
48+
tmpOutput, ok := txOutput.(*MaryTransactionOutput)
49+
if !ok {
50+
return fmt.Errorf("transaction output is not expected type")
51+
}
52+
outputValBytes, err := cbor.Encode(tmpOutput.OutputAmount)
53+
if err != nil {
54+
return err
55+
}
56+
if len(outputValBytes) <= maxTransactionOutputValueSize {
57+
continue
58+
}
59+
badOutputs = append(badOutputs, tmpOutput)
60+
}
61+
if len(badOutputs) == 0 {
62+
return nil
63+
}
64+
return OutputTooBigUtxoError{
65+
Outputs: badOutputs,
66+
}
67+
}
68+
69+
func UtxoValidateOutsideValidityIntervalUtxo(tx common.Transaction, slot uint64, ls common.LedgerState, pp common.ProtocolParameters) error {
70+
return allegra.UtxoValidateOutsideValidityIntervalUtxo(tx, slot, ls, pp)
71+
}
72+
73+
func UtxoValidateInputSetEmptyUtxo(tx common.Transaction, slot uint64, ls common.LedgerState, pp common.ProtocolParameters) error {
74+
return shelley.UtxoValidateInputSetEmptyUtxo(tx, slot, ls, pp)
75+
}
76+
77+
func UtxoValidateFeeTooSmallUtxo(tx common.Transaction, slot uint64, ls common.LedgerState, pp common.ProtocolParameters) error {
78+
tmpPparams, ok := pp.(*MaryProtocolParameters)
79+
if !ok {
80+
return fmt.Errorf("pparams are not expected type")
81+
}
82+
return shelley.UtxoValidateFeeTooSmallUtxo(tx, slot, ls, &tmpPparams.ShelleyProtocolParameters)
83+
}
84+
85+
func UtxoValidateBadInputsUtxo(tx common.Transaction, slot uint64, ls common.LedgerState, pp common.ProtocolParameters) error {
86+
return shelley.UtxoValidateBadInputsUtxo(tx, slot, ls, pp)
87+
}
88+
89+
func UtxoValidateWrongNetwork(tx common.Transaction, slot uint64, ls common.LedgerState, pp common.ProtocolParameters) error {
90+
return shelley.UtxoValidateWrongNetwork(tx, slot, ls, pp)
91+
}
92+
93+
func UtxoValidateWrongNetworkWithdrawal(tx common.Transaction, slot uint64, ls common.LedgerState, pp common.ProtocolParameters) error {
94+
return shelley.UtxoValidateWrongNetworkWithdrawal(tx, slot, ls, pp)
95+
}
96+
97+
func UtxoValidateValueNotConservedUtxo(tx common.Transaction, slot uint64, ls common.LedgerState, pp common.ProtocolParameters) error {
98+
tmpPparams, ok := pp.(*MaryProtocolParameters)
99+
if !ok {
100+
return fmt.Errorf("pparams are not expected type")
101+
}
102+
return shelley.UtxoValidateValueNotConservedUtxo(tx, slot, ls, &tmpPparams.ShelleyProtocolParameters)
103+
}
104+
105+
func UtxoValidateOutputTooSmallUtxo(tx common.Transaction, slot uint64, ls common.LedgerState, pp common.ProtocolParameters) error {
106+
tmpPparams, ok := pp.(*MaryProtocolParameters)
107+
if !ok {
108+
return fmt.Errorf("pparams are not expected type")
109+
}
110+
return shelley.UtxoValidateOutputTooSmallUtxo(tx, slot, ls, &tmpPparams.ShelleyProtocolParameters)
111+
}
112+
113+
func UtxoValidateOutputBootAddrAttrsTooBig(tx common.Transaction, slot uint64, ls common.LedgerState, pp common.ProtocolParameters) error {
114+
return shelley.UtxoValidateOutputBootAddrAttrsTooBig(tx, slot, ls, pp)
115+
}
116+
117+
func UtxoValidateMaxTxSizeUtxo(tx common.Transaction, slot uint64, ls common.LedgerState, pp common.ProtocolParameters) error {
118+
tmpPparams, ok := pp.(*MaryProtocolParameters)
119+
if !ok {
120+
return fmt.Errorf("pparams are not expected type")
121+
}
122+
return shelley.UtxoValidateMaxTxSizeUtxo(tx, slot, ls, &tmpPparams.ShelleyProtocolParameters)
123+
}

0 commit comments

Comments
 (0)