Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Mary era 'Utxo' validation rules #888

Merged
merged 1 commit into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions ledger/mary/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2025 Blink Labs Software
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package mary

import (
"fmt"
"strings"

"github.com/blinklabs-io/gouroboros/ledger/common"
)

type OutputTooBigUtxoError struct {
Outputs []common.TransactionOutput
}

func (e OutputTooBigUtxoError) Error() string {
tmpOutputs := make([]string, 0, len(e.Outputs))
for idx, tmpOutput := range e.Outputs {
tmpOutputs[idx] = fmt.Sprintf("%#v", tmpOutput)
}
return fmt.Sprintf(
"output value too large: %s",
strings.Join(tmpOutputs, ", "),
)
}
123 changes: 123 additions & 0 deletions ledger/mary/rules.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// Copyright 2025 Blink Labs Software
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package mary

import (
"fmt"

"github.com/blinklabs-io/gouroboros/cbor"
"github.com/blinklabs-io/gouroboros/ledger/allegra"
"github.com/blinklabs-io/gouroboros/ledger/common"
"github.com/blinklabs-io/gouroboros/ledger/shelley"
)

const (
maxTransactionOutputValueSize = 4000
)

var UtxoValidationRules = []common.UtxoValidationRuleFunc{
UtxoValidateOutsideValidityIntervalUtxo,
UtxoValidateInputSetEmptyUtxo,
UtxoValidateFeeTooSmallUtxo,
UtxoValidateBadInputsUtxo,
UtxoValidateWrongNetwork,
UtxoValidateWrongNetworkWithdrawal,
UtxoValidateValueNotConservedUtxo,
UtxoValidateOutputTooSmallUtxo,
UtxoValidateOutputTooBigUtxo,
UtxoValidateOutputBootAddrAttrsTooBig,
UtxoValidateMaxTxSizeUtxo,
}

// UtxoValidateOutputTooBigUtxo ensures that transaction output values are not too large
func UtxoValidateOutputTooBigUtxo(tx common.Transaction, slot uint64, _ common.LedgerState, _ common.ProtocolParameters) error {
var badOutputs []common.TransactionOutput
for _, txOutput := range tx.Outputs() {
tmpOutput, ok := txOutput.(*MaryTransactionOutput)
if !ok {
return fmt.Errorf("transaction output is not expected type")
}
outputValBytes, err := cbor.Encode(tmpOutput.OutputAmount)
if err != nil {
return err
}
if len(outputValBytes) <= maxTransactionOutputValueSize {
continue
}
badOutputs = append(badOutputs, tmpOutput)
}
if len(badOutputs) == 0 {
return nil
}
return OutputTooBigUtxoError{
Outputs: badOutputs,
}
}

func UtxoValidateOutsideValidityIntervalUtxo(tx common.Transaction, slot uint64, ls common.LedgerState, pp common.ProtocolParameters) error {
return allegra.UtxoValidateOutsideValidityIntervalUtxo(tx, slot, ls, pp)
}

func UtxoValidateInputSetEmptyUtxo(tx common.Transaction, slot uint64, ls common.LedgerState, pp common.ProtocolParameters) error {
return shelley.UtxoValidateInputSetEmptyUtxo(tx, slot, ls, pp)
}

func UtxoValidateFeeTooSmallUtxo(tx common.Transaction, slot uint64, ls common.LedgerState, pp common.ProtocolParameters) error {
tmpPparams, ok := pp.(*MaryProtocolParameters)
if !ok {
return fmt.Errorf("pparams are not expected type")
}
return shelley.UtxoValidateFeeTooSmallUtxo(tx, slot, ls, &tmpPparams.ShelleyProtocolParameters)
}

func UtxoValidateBadInputsUtxo(tx common.Transaction, slot uint64, ls common.LedgerState, pp common.ProtocolParameters) error {
return shelley.UtxoValidateBadInputsUtxo(tx, slot, ls, pp)
}

func UtxoValidateWrongNetwork(tx common.Transaction, slot uint64, ls common.LedgerState, pp common.ProtocolParameters) error {
return shelley.UtxoValidateWrongNetwork(tx, slot, ls, pp)
}

func UtxoValidateWrongNetworkWithdrawal(tx common.Transaction, slot uint64, ls common.LedgerState, pp common.ProtocolParameters) error {
return shelley.UtxoValidateWrongNetworkWithdrawal(tx, slot, ls, pp)
}

func UtxoValidateValueNotConservedUtxo(tx common.Transaction, slot uint64, ls common.LedgerState, pp common.ProtocolParameters) error {
tmpPparams, ok := pp.(*MaryProtocolParameters)
if !ok {
return fmt.Errorf("pparams are not expected type")
}
return shelley.UtxoValidateValueNotConservedUtxo(tx, slot, ls, &tmpPparams.ShelleyProtocolParameters)
}

func UtxoValidateOutputTooSmallUtxo(tx common.Transaction, slot uint64, ls common.LedgerState, pp common.ProtocolParameters) error {
tmpPparams, ok := pp.(*MaryProtocolParameters)
if !ok {
return fmt.Errorf("pparams are not expected type")
}
return shelley.UtxoValidateOutputTooSmallUtxo(tx, slot, ls, &tmpPparams.ShelleyProtocolParameters)
}

func UtxoValidateOutputBootAddrAttrsTooBig(tx common.Transaction, slot uint64, ls common.LedgerState, pp common.ProtocolParameters) error {
return shelley.UtxoValidateOutputBootAddrAttrsTooBig(tx, slot, ls, pp)
}

func UtxoValidateMaxTxSizeUtxo(tx common.Transaction, slot uint64, ls common.LedgerState, pp common.ProtocolParameters) error {
tmpPparams, ok := pp.(*MaryProtocolParameters)
if !ok {
return fmt.Errorf("pparams are not expected type")
}
return shelley.UtxoValidateMaxTxSizeUtxo(tx, slot, ls, &tmpPparams.ShelleyProtocolParameters)
}
Loading
Loading