Skip to content
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
11 changes: 10 additions & 1 deletion ledger/allegra/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,16 @@ func UtxoValidateValueNotConservedUtxo(
ls common.LedgerState,
pp common.ProtocolParameters,
) error {
return shelley.UtxoValidateValueNotConservedUtxo(tx, slot, ls, pp)
tmpPparams, ok := pp.(*AllegraProtocolParameters)
if !ok {
return errors.New("pparams are not expected type")
}
return shelley.UtxoValidateValueNotConservedUtxo(
tx,
slot,
ls,
&tmpPparams.ShelleyProtocolParameters,
)
}

func UtxoValidateOutputTooSmallUtxo(
Expand Down
32 changes: 31 additions & 1 deletion ledger/allegra/rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,7 @@ func TestUtxoValidateValueNotConservedUtxo(t *testing.T) {
testInputTxId := "d228b482a1aae768e4a796380f49e021d9c21f70d3c12cb186b188dedfc0ee22"
var testInputAmount uint64 = 555666777
var testFee uint64 = 123456
var testStakeDeposit uint64 = 2_000_000
testOutputExactAmount := testInputAmount - testFee
testOutputUnderAmount := testOutputExactAmount - 999
testOutputOverAmount := testOutputExactAmount + 999
Expand Down Expand Up @@ -558,7 +559,11 @@ func TestUtxoValidateValueNotConservedUtxo(t *testing.T) {
},
}
testSlot := uint64(0)
testProtocolParams := &allegra.AllegraProtocolParameters{}
testProtocolParams := &allegra.AllegraProtocolParameters{
ShelleyProtocolParameters: shelley.ShelleyProtocolParameters{
KeyDeposit: uint(testStakeDeposit),
},
}
// Exact amount
t.Run(
"exact amount",
Expand All @@ -578,6 +583,31 @@ func TestUtxoValidateValueNotConservedUtxo(t *testing.T) {
}
},
)
// Stake registration
t.Run(
"stake registration",
func(t *testing.T) {
testTx.Body.TxOutputs[0].OutputAmount = testOutputExactAmount - testStakeDeposit
testTx.Body.TxCertificates = []common.CertificateWrapper{
{
Type: common.CertificateTypeStakeRegistration,
Certificate: &common.StakeRegistrationCertificate{},
},
}
err := allegra.UtxoValidateValueNotConservedUtxo(
testTx,
testSlot,
testLedgerState,
testProtocolParams,
)
if err != nil {
t.Errorf(
"UtxoValidateValueNotConservedUtxo should succeed when inputs and outputs are balanced\n got error: %v",
err,
)
}
},
)
// Output too low
t.Run(
"output too low",
Expand Down
6 changes: 5 additions & 1 deletion ledger/alonzo/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,11 @@ func UtxoValidateBadInputsUtxo(tx common.Transaction, slot uint64, ls common.Led
}

func UtxoValidateValueNotConservedUtxo(tx common.Transaction, slot uint64, ls common.LedgerState, pp common.ProtocolParameters) error {
return shelley.UtxoValidateValueNotConservedUtxo(tx, slot, ls, pp)
tmpPparams, ok := pp.(*AlonzoProtocolParameters)
if !ok {
return errors.New("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 {
Expand Down
36 changes: 35 additions & 1 deletion ledger/alonzo/rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,7 @@ func TestUtxoValidateValueNotConservedUtxo(t *testing.T) {
testInputTxId := "d228b482a1aae768e4a796380f49e021d9c21f70d3c12cb186b188dedfc0ee22"
var testInputAmount uint64 = 555666777
var testFee uint64 = 123456
var testStakeDeposit uint64 = 2_000_000
testOutputExactAmount := testInputAmount - testFee
testOutputUnderAmount := testOutputExactAmount - 999
testOutputOverAmount := testOutputExactAmount + 999
Expand Down Expand Up @@ -575,7 +576,15 @@ func TestUtxoValidateValueNotConservedUtxo(t *testing.T) {
},
}
testSlot := uint64(0)
testProtocolParams := &alonzo.AlonzoProtocolParameters{}
testProtocolParams := &alonzo.AlonzoProtocolParameters{
MaryProtocolParameters: mary.MaryProtocolParameters{
AllegraProtocolParameters: allegra.AllegraProtocolParameters{
ShelleyProtocolParameters: shelley.ShelleyProtocolParameters{
KeyDeposit: uint(testStakeDeposit),
},
},
},
}
// Exact amount
t.Run(
"exact amount",
Expand All @@ -595,6 +604,31 @@ func TestUtxoValidateValueNotConservedUtxo(t *testing.T) {
}
},
)
// Stake registration
t.Run(
"stake registration",
func(t *testing.T) {
testTx.Body.TxOutputs[0].OutputAmount.Amount = testOutputExactAmount - testStakeDeposit
testTx.Body.TxCertificates = []common.CertificateWrapper{
{
Type: common.CertificateTypeStakeRegistration,
Certificate: &common.StakeRegistrationCertificate{},
},
}
err := alonzo.UtxoValidateValueNotConservedUtxo(
testTx,
testSlot,
testLedgerState,
testProtocolParams,
)
if err != nil {
t.Errorf(
"UtxoValidateValueNotConservedUtxo should succeed when inputs and outputs are balanced\n got error: %v",
err,
)
}
},
)
// Output too low
t.Run(
"output too low",
Expand Down
41 changes: 40 additions & 1 deletion ledger/babbage/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,46 @@ func UtxoValidateBadInputsUtxo(tx common.Transaction, slot uint64, ls common.Led
}

func UtxoValidateValueNotConservedUtxo(tx common.Transaction, slot uint64, ls common.LedgerState, pp common.ProtocolParameters) error {
return shelley.UtxoValidateValueNotConservedUtxo(tx, slot, ls, pp)
tmpPparams, ok := pp.(*BabbageProtocolParameters)
if !ok {
return errors.New("pparams are not expected type")
}
// Calculate consumed value
// consumed = value from input(s) + withdrawals + refunds(?)
var consumedValue uint64
for _, tmpInput := range tx.Inputs() {
tmpUtxo, err := ls.UtxoById(tmpInput)
// Ignore errors fetching the UTxO and exclude it from calculations
if err != nil {
continue
}
consumedValue += tmpUtxo.Output.Amount()
}
for _, tmpWithdrawalAmount := range tx.Withdrawals() {
consumedValue += tmpWithdrawalAmount
}
// Calculate produced value
// produced = value from output(s) + fee + deposits
var producedValue uint64
for _, tmpOutput := range tx.Outputs() {
producedValue += tmpOutput.Amount()
}
producedValue += tx.Fee()
for _, cert := range tx.Certificates() {
switch cert.(type) {
case *common.PoolRegistrationCertificate:
producedValue += uint64(tmpPparams.PoolDeposit)
case *common.StakeRegistrationCertificate:
producedValue += uint64(tmpPparams.KeyDeposit)
}
}
if consumedValue == producedValue {
return nil
}
return shelley.ValueNotConservedUtxoError{
Consumed: consumedValue,
Produced: producedValue,
}
}

func UtxoValidateOutputTooSmallUtxo(tx common.Transaction, slot uint64, ls common.LedgerState, pp common.ProtocolParameters) error {
Expand Down
30 changes: 29 additions & 1 deletion ledger/babbage/rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,7 @@ func TestUtxoValidateValueNotConservedUtxo(t *testing.T) {
testInputTxId := "d228b482a1aae768e4a796380f49e021d9c21f70d3c12cb186b188dedfc0ee22"
var testInputAmount uint64 = 555666777
var testFee uint64 = 123456
var testStakeDeposit uint64 = 2_000_000
testOutputExactAmount := testInputAmount - testFee
testOutputUnderAmount := testOutputExactAmount - 999
testOutputOverAmount := testOutputExactAmount + 999
Expand Down Expand Up @@ -580,7 +581,9 @@ func TestUtxoValidateValueNotConservedUtxo(t *testing.T) {
},
}
testSlot := uint64(0)
testProtocolParams := &babbage.BabbageProtocolParameters{}
testProtocolParams := &babbage.BabbageProtocolParameters{
KeyDeposit: uint(testStakeDeposit),
}
// Exact amount
t.Run(
"exact amount",
Expand All @@ -600,6 +603,31 @@ func TestUtxoValidateValueNotConservedUtxo(t *testing.T) {
}
},
)
// Stake registration
t.Run(
"stake registration",
func(t *testing.T) {
testTx.Body.TxOutputs[0].OutputAmount.Amount = testOutputExactAmount - testStakeDeposit
testTx.Body.TxCertificates = []common.CertificateWrapper{
{
Type: common.CertificateTypeStakeRegistration,
Certificate: &common.StakeRegistrationCertificate{},
},
}
err := babbage.UtxoValidateValueNotConservedUtxo(
testTx,
testSlot,
testLedgerState,
testProtocolParams,
)
if err != nil {
t.Errorf(
"UtxoValidateValueNotConservedUtxo should succeed when inputs and outputs are balanced\n got error: %v",
err,
)
}
},
)
// Output too low
t.Run(
"output too low",
Expand Down
41 changes: 40 additions & 1 deletion ledger/conway/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,46 @@ func UtxoValidateBadInputsUtxo(tx common.Transaction, slot uint64, ls common.Led
}

func UtxoValidateValueNotConservedUtxo(tx common.Transaction, slot uint64, ls common.LedgerState, pp common.ProtocolParameters) error {
return shelley.UtxoValidateValueNotConservedUtxo(tx, slot, ls, pp)
tmpPparams, ok := pp.(*ConwayProtocolParameters)
if !ok {
return errors.New("pparams are not expected type")
}
// Calculate consumed value
// consumed = value from input(s) + withdrawals + refunds(?)
var consumedValue uint64
for _, tmpInput := range tx.Inputs() {
tmpUtxo, err := ls.UtxoById(tmpInput)
// Ignore errors fetching the UTxO and exclude it from calculations
if err != nil {
continue
}
consumedValue += tmpUtxo.Output.Amount()
}
for _, tmpWithdrawalAmount := range tx.Withdrawals() {
consumedValue += tmpWithdrawalAmount
}
// Calculate produced value
// produced = value from output(s) + fee + deposits
var producedValue uint64
for _, tmpOutput := range tx.Outputs() {
producedValue += tmpOutput.Amount()
}
producedValue += tx.Fee()
for _, cert := range tx.Certificates() {
switch cert.(type) {
case *common.PoolRegistrationCertificate:
producedValue += uint64(tmpPparams.PoolDeposit)
case *common.StakeRegistrationCertificate:
producedValue += uint64(tmpPparams.KeyDeposit)
}
}
if consumedValue == producedValue {
return nil
}
return shelley.ValueNotConservedUtxoError{
Consumed: consumedValue,
Produced: producedValue,
}
}

func UtxoValidateOutputTooSmallUtxo(tx common.Transaction, slot uint64, ls common.LedgerState, pp common.ProtocolParameters) error {
Expand Down
30 changes: 29 additions & 1 deletion ledger/conway/rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,7 @@ func TestUtxoValidateValueNotConservedUtxo(t *testing.T) {
testInputTxId := "d228b482a1aae768e4a796380f49e021d9c21f70d3c12cb186b188dedfc0ee22"
var testInputAmount uint64 = 555666777
var testFee uint64 = 123456
var testStakeDeposit uint64 = 2_000_000
testOutputExactAmount := testInputAmount - testFee
testOutputUnderAmount := testOutputExactAmount - 999
testOutputOverAmount := testOutputExactAmount + 999
Expand Down Expand Up @@ -583,7 +584,9 @@ func TestUtxoValidateValueNotConservedUtxo(t *testing.T) {
},
}
testSlot := uint64(0)
testProtocolParams := &conway.ConwayProtocolParameters{}
testProtocolParams := &conway.ConwayProtocolParameters{
KeyDeposit: uint(testStakeDeposit),
}
// Exact amount
t.Run(
"exact amount",
Expand All @@ -603,6 +606,31 @@ func TestUtxoValidateValueNotConservedUtxo(t *testing.T) {
}
},
)
// Stake registration
t.Run(
"stake registration",
func(t *testing.T) {
testTx.Body.TxOutputs[0].OutputAmount.Amount = testOutputExactAmount - testStakeDeposit
testTx.Body.TxCertificates = []common.CertificateWrapper{
{
Type: common.CertificateTypeStakeRegistration,
Certificate: &common.StakeRegistrationCertificate{},
},
}
err := conway.UtxoValidateValueNotConservedUtxo(
testTx,
testSlot,
testLedgerState,
testProtocolParams,
)
if err != nil {
t.Errorf(
"UtxoValidateValueNotConservedUtxo should succeed when inputs and outputs are balanced\n got error: %v",
err,
)
}
},
)
// Output too low
t.Run(
"output too low",
Expand Down
11 changes: 10 additions & 1 deletion ledger/mary/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,16 @@ func UtxoValidateValueNotConservedUtxo(
ls common.LedgerState,
pp common.ProtocolParameters,
) error {
return shelley.UtxoValidateValueNotConservedUtxo(tx, slot, ls, pp)
tmpPparams, ok := pp.(*MaryProtocolParameters)
if !ok {
return errors.New("pparams are not expected type")
}
return shelley.UtxoValidateValueNotConservedUtxo(
tx,
slot,
ls,
&tmpPparams.ShelleyProtocolParameters,
)
}

func UtxoValidateOutputTooSmallUtxo(
Expand Down
Loading
Loading