Skip to content

Commit 498c233

Browse files
authored
feat: account for stake deregistration in validation rules (#940)
Also, because you cannot re-register a registered stake key or de-register and unregistered stake key, we can always apply the key deposit/refund for one of those certs and do not need to query Signed-off-by: Aurora Gaffney <[email protected]>
1 parent e3c9893 commit 498c233

File tree

9 files changed

+72
-150
lines changed

9 files changed

+72
-150
lines changed

ledger/allegra/rules_test.go

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -505,8 +505,6 @@ func TestUtxoValidateValueNotConservedUtxo(t *testing.T) {
505505
var testInputAmount uint64 = 555666777
506506
var testFee uint64 = 123456
507507
var testStakeDeposit uint64 = 2_000_000
508-
var testStakeCred1 = []byte{0x01, 0x23, 0x45}
509-
var testStakeCred2 = []byte{0xab, 0xcd, 0xef}
510508
testOutputExactAmount := testInputAmount - testFee
511509
testOutputUnderAmount := testOutputExactAmount - 999
512510
testOutputOverAmount := testOutputExactAmount + 999
@@ -535,13 +533,6 @@ func TestUtxoValidateValueNotConservedUtxo(t *testing.T) {
535533
},
536534
},
537535
},
538-
MockStakeRegistration: []common.StakeRegistrationCertificate{
539-
{
540-
StakeRegistration: common.StakeCredential{
541-
Credential: testStakeCred2,
542-
},
543-
},
544-
},
545536
}
546537
testSlot := uint64(0)
547538
testProtocolParams := &allegra.AllegraProtocolParameters{
@@ -568,18 +559,16 @@ func TestUtxoValidateValueNotConservedUtxo(t *testing.T) {
568559
}
569560
},
570561
)
571-
// First stake registration
562+
// Stake registration
572563
t.Run(
573-
"first stake registration",
564+
"stake registration",
574565
func(t *testing.T) {
575566
testTx.Body.TxOutputs[0].OutputAmount = testOutputExactAmount - testStakeDeposit
576567
testTx.Body.TxCertificates = []common.CertificateWrapper{
577568
{
578569
Type: common.CertificateTypeStakeRegistration,
579570
Certificate: &common.StakeRegistrationCertificate{
580-
StakeRegistration: common.StakeCredential{
581-
Credential: testStakeCred1,
582-
},
571+
StakeRegistration: common.StakeCredential{},
583572
},
584573
},
585574
}
@@ -597,18 +586,16 @@ func TestUtxoValidateValueNotConservedUtxo(t *testing.T) {
597586
}
598587
},
599588
)
600-
// Second stake registration
589+
// Stake deregistration
601590
t.Run(
602-
"second stake registration",
591+
"stake deregistration",
603592
func(t *testing.T) {
604-
testTx.Body.TxOutputs[0].OutputAmount = testOutputExactAmount
593+
testTx.Body.TxOutputs[0].OutputAmount = testOutputExactAmount + testStakeDeposit
605594
testTx.Body.TxCertificates = []common.CertificateWrapper{
606595
{
607596
Type: common.CertificateTypeStakeRegistration,
608-
Certificate: &common.StakeRegistrationCertificate{
609-
StakeRegistration: common.StakeCredential{
610-
Credential: testStakeCred2,
611-
},
597+
Certificate: &common.StakeDeregistrationCertificate{
598+
StakeDeregistration: common.StakeCredential{},
612599
},
613600
},
614601
}

ledger/alonzo/rules_test.go

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -520,8 +520,6 @@ func TestUtxoValidateValueNotConservedUtxo(t *testing.T) {
520520
var testInputAmount uint64 = 555666777
521521
var testFee uint64 = 123456
522522
var testStakeDeposit uint64 = 2_000_000
523-
var testStakeCred1 = []byte{0x01, 0x23, 0x45}
524-
var testStakeCred2 = []byte{0xab, 0xcd, 0xef}
525523
testOutputExactAmount := testInputAmount - testFee
526524
testOutputUnderAmount := testOutputExactAmount - 999
527525
testOutputOverAmount := testOutputExactAmount + 999
@@ -554,13 +552,6 @@ func TestUtxoValidateValueNotConservedUtxo(t *testing.T) {
554552
},
555553
},
556554
},
557-
MockStakeRegistration: []common.StakeRegistrationCertificate{
558-
{
559-
StakeRegistration: common.StakeCredential{
560-
Credential: testStakeCred2,
561-
},
562-
},
563-
},
564555
}
565556
testSlot := uint64(0)
566557
testProtocolParams := &alonzo.AlonzoProtocolParameters{
@@ -591,18 +582,16 @@ func TestUtxoValidateValueNotConservedUtxo(t *testing.T) {
591582
}
592583
},
593584
)
594-
// First stake registration
585+
// Stake registration
595586
t.Run(
596-
"first stake registration",
587+
"stake registration",
597588
func(t *testing.T) {
598589
testTx.Body.TxOutputs[0].OutputAmount.Amount = testOutputExactAmount - testStakeDeposit
599590
testTx.Body.TxCertificates = []common.CertificateWrapper{
600591
{
601592
Type: common.CertificateTypeStakeRegistration,
602593
Certificate: &common.StakeRegistrationCertificate{
603-
StakeRegistration: common.StakeCredential{
604-
Credential: testStakeCred1,
605-
},
594+
StakeRegistration: common.StakeCredential{},
606595
},
607596
},
608597
}
@@ -620,18 +609,16 @@ func TestUtxoValidateValueNotConservedUtxo(t *testing.T) {
620609
}
621610
},
622611
)
623-
// Second stake registration
612+
// Stake deregistration
624613
t.Run(
625-
"second stake registration",
614+
"stake deregistration",
626615
func(t *testing.T) {
627-
testTx.Body.TxOutputs[0].OutputAmount.Amount = testOutputExactAmount
616+
testTx.Body.TxOutputs[0].OutputAmount.Amount = testOutputExactAmount + testStakeDeposit
628617
testTx.Body.TxCertificates = []common.CertificateWrapper{
629618
{
630619
Type: common.CertificateTypeStakeRegistration,
631-
Certificate: &common.StakeRegistrationCertificate{
632-
StakeRegistration: common.StakeCredential{
633-
Credential: testStakeCred2,
634-
},
620+
Certificate: &common.StakeDeregistrationCertificate{
621+
StakeDeregistration: common.StakeCredential{},
635622
},
636623
},
637624
}

ledger/babbage/rules.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ func UtxoValidateValueNotConservedUtxo(tx common.Transaction, slot uint64, ls co
182182
return errors.New("pparams are not expected type")
183183
}
184184
// Calculate consumed value
185-
// consumed = value from input(s) + withdrawals + refunds(?)
185+
// consumed = value from input(s) + withdrawals + refunds
186186
var consumedValue uint64
187187
for _, tmpInput := range tx.Inputs() {
188188
tmpUtxo, err := ls.UtxoById(tmpInput)
@@ -195,6 +195,12 @@ func UtxoValidateValueNotConservedUtxo(tx common.Transaction, slot uint64, ls co
195195
for _, tmpWithdrawalAmount := range tx.Withdrawals() {
196196
consumedValue += tmpWithdrawalAmount
197197
}
198+
for _, cert := range tx.Certificates() {
199+
switch cert.(type) {
200+
case *common.StakeDeregistrationCertificate:
201+
consumedValue += uint64(tmpPparams.KeyDeposit)
202+
}
203+
}
198204
// Calculate produced value
199205
// produced = value from output(s) + fee + deposits
200206
var producedValue uint64
@@ -213,13 +219,7 @@ func UtxoValidateValueNotConservedUtxo(tx common.Transaction, slot uint64, ls co
213219
producedValue += uint64(tmpPparams.PoolDeposit)
214220
}
215221
case *common.StakeRegistrationCertificate:
216-
certs, err := ls.StakeRegistration(tmpCert.StakeRegistration.Credential)
217-
if err != nil {
218-
return err
219-
}
220-
if len(certs) == 0 {
221-
producedValue += uint64(tmpPparams.KeyDeposit)
222-
}
222+
producedValue += uint64(tmpPparams.KeyDeposit)
223223
}
224224
}
225225
if consumedValue == producedValue {

ledger/babbage/rules_test.go

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -523,8 +523,6 @@ func TestUtxoValidateValueNotConservedUtxo(t *testing.T) {
523523
var testInputAmount uint64 = 555666777
524524
var testFee uint64 = 123456
525525
var testStakeDeposit uint64 = 2_000_000
526-
var testStakeCred1 = []byte{0x01, 0x23, 0x45}
527-
var testStakeCred2 = []byte{0xab, 0xcd, 0xef}
528526
testOutputExactAmount := testInputAmount - testFee
529527
testOutputUnderAmount := testOutputExactAmount - 999
530528
testOutputOverAmount := testOutputExactAmount + 999
@@ -559,13 +557,6 @@ func TestUtxoValidateValueNotConservedUtxo(t *testing.T) {
559557
},
560558
},
561559
},
562-
MockStakeRegistration: []common.StakeRegistrationCertificate{
563-
{
564-
StakeRegistration: common.StakeCredential{
565-
Credential: testStakeCred2,
566-
},
567-
},
568-
},
569560
}
570561
testSlot := uint64(0)
571562
testProtocolParams := &babbage.BabbageProtocolParameters{
@@ -590,18 +581,16 @@ func TestUtxoValidateValueNotConservedUtxo(t *testing.T) {
590581
}
591582
},
592583
)
593-
// First stake registration
584+
// Stake registration
594585
t.Run(
595-
"first stake registration",
586+
"stake registration",
596587
func(t *testing.T) {
597588
testTx.Body.TxOutputs[0].OutputAmount.Amount = testOutputExactAmount - testStakeDeposit
598589
testTx.Body.TxCertificates = []common.CertificateWrapper{
599590
{
600591
Type: common.CertificateTypeStakeRegistration,
601592
Certificate: &common.StakeRegistrationCertificate{
602-
StakeRegistration: common.StakeCredential{
603-
Credential: testStakeCred1,
604-
},
593+
StakeRegistration: common.StakeCredential{},
605594
},
606595
},
607596
}
@@ -619,18 +608,16 @@ func TestUtxoValidateValueNotConservedUtxo(t *testing.T) {
619608
}
620609
},
621610
)
622-
// Second stake registration
611+
// Stake deregistration
623612
t.Run(
624-
"second stake registration",
613+
"stake deregistration",
625614
func(t *testing.T) {
626-
testTx.Body.TxOutputs[0].OutputAmount.Amount = testOutputExactAmount
615+
testTx.Body.TxOutputs[0].OutputAmount.Amount = testOutputExactAmount + testStakeDeposit
627616
testTx.Body.TxCertificates = []common.CertificateWrapper{
628617
{
629618
Type: common.CertificateTypeStakeRegistration,
630-
Certificate: &common.StakeRegistrationCertificate{
631-
StakeRegistration: common.StakeCredential{
632-
Credential: testStakeCred2,
633-
},
619+
Certificate: &common.StakeDeregistrationCertificate{
620+
StakeDeregistration: common.StakeCredential{},
634621
},
635622
},
636623
}

ledger/conway/rules.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ func UtxoValidateValueNotConservedUtxo(tx common.Transaction, slot uint64, ls co
178178
return errors.New("pparams are not expected type")
179179
}
180180
// Calculate consumed value
181-
// consumed = value from input(s) + withdrawals + refunds(?)
181+
// consumed = value from input(s) + withdrawals + refunds
182182
var consumedValue uint64
183183
for _, tmpInput := range tx.Inputs() {
184184
tmpUtxo, err := ls.UtxoById(tmpInput)
@@ -191,6 +191,12 @@ func UtxoValidateValueNotConservedUtxo(tx common.Transaction, slot uint64, ls co
191191
for _, tmpWithdrawalAmount := range tx.Withdrawals() {
192192
consumedValue += tmpWithdrawalAmount
193193
}
194+
for _, cert := range tx.Certificates() {
195+
switch cert.(type) {
196+
case *common.StakeDeregistrationCertificate:
197+
consumedValue += uint64(tmpPparams.KeyDeposit)
198+
}
199+
}
194200
// Calculate produced value
195201
// produced = value from output(s) + fee + deposits
196202
var producedValue uint64
@@ -209,13 +215,7 @@ func UtxoValidateValueNotConservedUtxo(tx common.Transaction, slot uint64, ls co
209215
producedValue += uint64(tmpPparams.PoolDeposit)
210216
}
211217
case *common.StakeRegistrationCertificate:
212-
certs, err := ls.StakeRegistration(tmpCert.StakeRegistration.Credential)
213-
if err != nil {
214-
return err
215-
}
216-
if len(certs) == 0 {
217-
producedValue += uint64(tmpPparams.KeyDeposit)
218-
}
218+
producedValue += uint64(tmpPparams.KeyDeposit)
219219
}
220220
}
221221
if consumedValue == producedValue {

ledger/conway/rules_test.go

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -524,8 +524,6 @@ func TestUtxoValidateValueNotConservedUtxo(t *testing.T) {
524524
var testInputAmount uint64 = 555666777
525525
var testFee uint64 = 123456
526526
var testStakeDeposit uint64 = 2_000_000
527-
var testStakeCred1 = []byte{0x01, 0x23, 0x45}
528-
var testStakeCred2 = []byte{0xab, 0xcd, 0xef}
529527
testOutputExactAmount := testInputAmount - testFee
530528
testOutputUnderAmount := testOutputExactAmount - 999
531529
testOutputOverAmount := testOutputExactAmount + 999
@@ -562,13 +560,6 @@ func TestUtxoValidateValueNotConservedUtxo(t *testing.T) {
562560
},
563561
},
564562
},
565-
MockStakeRegistration: []common.StakeRegistrationCertificate{
566-
{
567-
StakeRegistration: common.StakeCredential{
568-
Credential: testStakeCred2,
569-
},
570-
},
571-
},
572563
}
573564
testSlot := uint64(0)
574565
testProtocolParams := &conway.ConwayProtocolParameters{
@@ -593,18 +584,16 @@ func TestUtxoValidateValueNotConservedUtxo(t *testing.T) {
593584
}
594585
},
595586
)
596-
// First stake registration
587+
// Stake registration
597588
t.Run(
598-
"first stake registration",
589+
"stake registration",
599590
func(t *testing.T) {
600591
testTx.Body.TxOutputs[0].OutputAmount.Amount = testOutputExactAmount - testStakeDeposit
601592
testTx.Body.TxCertificates = []common.CertificateWrapper{
602593
{
603594
Type: common.CertificateTypeStakeRegistration,
604595
Certificate: &common.StakeRegistrationCertificate{
605-
StakeRegistration: common.StakeCredential{
606-
Credential: testStakeCred1,
607-
},
596+
StakeRegistration: common.StakeCredential{},
608597
},
609598
},
610599
}
@@ -622,18 +611,16 @@ func TestUtxoValidateValueNotConservedUtxo(t *testing.T) {
622611
}
623612
},
624613
)
625-
// Second stake registration
614+
// Stake deregistration
626615
t.Run(
627-
"second stake registration",
616+
"stake deregistration",
628617
func(t *testing.T) {
629-
testTx.Body.TxOutputs[0].OutputAmount.Amount = testOutputExactAmount
618+
testTx.Body.TxOutputs[0].OutputAmount.Amount = testOutputExactAmount + testStakeDeposit
630619
testTx.Body.TxCertificates = []common.CertificateWrapper{
631620
{
632621
Type: common.CertificateTypeStakeRegistration,
633-
Certificate: &common.StakeRegistrationCertificate{
634-
StakeRegistration: common.StakeCredential{
635-
Credential: testStakeCred2,
636-
},
622+
Certificate: &common.StakeDeregistrationCertificate{
623+
StakeDeregistration: common.StakeCredential{},
637624
},
638625
},
639626
}

0 commit comments

Comments
 (0)