Skip to content

Commit 88272fb

Browse files
authored
feat(ledger): Add a method to get the Certificate type to the interface (#971)
Signed-off-by: Akhil Repala <[email protected]>
1 parent e70e43f commit 88272fb

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed

ledger/common/certs.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ type Certificate interface {
117117
isCertificate()
118118
Cbor() []byte
119119
Utxorpc() *utxorpc.Certificate
120+
Type() uint
120121
}
121122

122123
const (
@@ -177,6 +178,10 @@ func (c *StakeRegistrationCertificate) Utxorpc() *utxorpc.Certificate {
177178
}
178179
}
179180

181+
func (c *StakeRegistrationCertificate) Type() uint {
182+
return c.CertType
183+
}
184+
180185
type StakeDeregistrationCertificate struct {
181186
cbor.StructAsArray
182187
cbor.DecodeStoreCbor
@@ -198,6 +203,10 @@ func (c *StakeDeregistrationCertificate) Utxorpc() *utxorpc.Certificate {
198203
}
199204
}
200205

206+
func (c *StakeDeregistrationCertificate) Type() uint {
207+
return c.CertType
208+
}
209+
201210
type StakeDelegationCertificate struct {
202211
cbor.StructAsArray
203212
cbor.DecodeStoreCbor
@@ -223,6 +232,10 @@ func (c *StakeDelegationCertificate) Utxorpc() *utxorpc.Certificate {
223232
}
224233
}
225234

235+
func (c *StakeDelegationCertificate) Type() uint {
236+
return c.CertType
237+
}
238+
226239
type (
227240
PoolKeyHash Blake2b224
228241
PoolMetadataHash Blake2b256
@@ -373,6 +386,10 @@ func (c *PoolRegistrationCertificate) Utxorpc() *utxorpc.Certificate {
373386
}
374387
}
375388

389+
func (c *PoolRegistrationCertificate) Type() uint {
390+
return c.CertType
391+
}
392+
376393
type PoolRetirementCertificate struct {
377394
cbor.StructAsArray
378395
cbor.DecodeStoreCbor
@@ -398,6 +415,10 @@ func (c *PoolRetirementCertificate) Utxorpc() *utxorpc.Certificate {
398415
}
399416
}
400417

418+
func (c *PoolRetirementCertificate) Type() uint {
419+
return c.CertType
420+
}
421+
401422
type GenesisKeyDelegationCertificate struct {
402423
cbor.StructAsArray
403424
cbor.DecodeStoreCbor
@@ -425,6 +446,10 @@ func (c *GenesisKeyDelegationCertificate) Utxorpc() *utxorpc.Certificate {
425446
}
426447
}
427448

449+
func (c *GenesisKeyDelegationCertificate) Type() uint {
450+
return c.CertType
451+
}
452+
428453
type MirSource int32
429454

430455
const (
@@ -508,6 +533,10 @@ func (c *MoveInstantaneousRewardsCertificate) Utxorpc() *utxorpc.Certificate {
508533
}
509534
}
510535

536+
func (c *MoveInstantaneousRewardsCertificate) Type() uint {
537+
return c.CertType
538+
}
539+
511540
type RegistrationCertificate struct {
512541
cbor.StructAsArray
513542
cbor.DecodeStoreCbor
@@ -529,6 +558,10 @@ func (c *RegistrationCertificate) Utxorpc() *utxorpc.Certificate {
529558
return nil
530559
}
531560

561+
func (c *RegistrationCertificate) Type() uint {
562+
return c.CertType
563+
}
564+
532565
type DeregistrationCertificate struct {
533566
cbor.StructAsArray
534567
cbor.DecodeStoreCbor
@@ -550,6 +583,10 @@ func (c *DeregistrationCertificate) Utxorpc() *utxorpc.Certificate {
550583
return nil
551584
}
552585

586+
func (c *DeregistrationCertificate) Type() uint {
587+
return c.CertType
588+
}
589+
553590
type VoteDelegationCertificate struct {
554591
cbor.StructAsArray
555592
cbor.DecodeStoreCbor
@@ -571,6 +608,10 @@ func (c *VoteDelegationCertificate) Utxorpc() *utxorpc.Certificate {
571608
return nil
572609
}
573610

611+
func (c *VoteDelegationCertificate) Type() uint {
612+
return c.CertType
613+
}
614+
574615
type StakeVoteDelegationCertificate struct {
575616
cbor.StructAsArray
576617
cbor.DecodeStoreCbor
@@ -593,6 +634,10 @@ func (c *StakeVoteDelegationCertificate) Utxorpc() *utxorpc.Certificate {
593634
return nil
594635
}
595636

637+
func (c *StakeVoteDelegationCertificate) Type() uint {
638+
return c.CertType
639+
}
640+
596641
type StakeRegistrationDelegationCertificate struct {
597642
cbor.StructAsArray
598643
cbor.DecodeStoreCbor
@@ -615,6 +660,10 @@ func (c *StakeRegistrationDelegationCertificate) Utxorpc() *utxorpc.Certificate
615660
return nil
616661
}
617662

663+
func (c *StakeRegistrationDelegationCertificate) Type() uint {
664+
return c.CertType
665+
}
666+
618667
type VoteRegistrationDelegationCertificate struct {
619668
cbor.StructAsArray
620669
cbor.DecodeStoreCbor
@@ -637,6 +686,10 @@ func (c *VoteRegistrationDelegationCertificate) Utxorpc() *utxorpc.Certificate {
637686
return nil
638687
}
639688

689+
func (c *VoteRegistrationDelegationCertificate) Type() uint {
690+
return c.CertType
691+
}
692+
640693
type StakeVoteRegistrationDelegationCertificate struct {
641694
cbor.StructAsArray
642695
cbor.DecodeStoreCbor
@@ -660,6 +713,10 @@ func (c *StakeVoteRegistrationDelegationCertificate) Utxorpc() *utxorpc.Certific
660713
return nil
661714
}
662715

716+
func (c *StakeVoteRegistrationDelegationCertificate) Type() uint {
717+
return c.CertType
718+
}
719+
663720
type AuthCommitteeHotCertificate struct {
664721
cbor.StructAsArray
665722
cbor.DecodeStoreCbor
@@ -681,6 +738,10 @@ func (c *AuthCommitteeHotCertificate) Utxorpc() *utxorpc.Certificate {
681738
return nil
682739
}
683740

741+
func (c *AuthCommitteeHotCertificate) Type() uint {
742+
return c.CertType
743+
}
744+
684745
type ResignCommitteeColdCertificate struct {
685746
cbor.StructAsArray
686747
cbor.DecodeStoreCbor
@@ -702,6 +763,10 @@ func (c *ResignCommitteeColdCertificate) Utxorpc() *utxorpc.Certificate {
702763
return nil
703764
}
704765

766+
func (c *ResignCommitteeColdCertificate) Type() uint {
767+
return c.CertType
768+
}
769+
705770
type RegistrationDrepCertificate struct {
706771
cbor.StructAsArray
707772
cbor.DecodeStoreCbor
@@ -724,6 +789,10 @@ func (c *RegistrationDrepCertificate) Utxorpc() *utxorpc.Certificate {
724789
return nil
725790
}
726791

792+
func (c *RegistrationDrepCertificate) Type() uint {
793+
return c.CertType
794+
}
795+
727796
type DeregistrationDrepCertificate struct {
728797
cbor.StructAsArray
729798
cbor.DecodeStoreCbor
@@ -745,6 +814,10 @@ func (c *DeregistrationDrepCertificate) Utxorpc() *utxorpc.Certificate {
745814
return nil
746815
}
747816

817+
func (c *DeregistrationDrepCertificate) Type() uint {
818+
return c.CertType
819+
}
820+
748821
type UpdateDrepCertificate struct {
749822
cbor.StructAsArray
750823
cbor.DecodeStoreCbor
@@ -765,3 +838,7 @@ func (c *UpdateDrepCertificate) Utxorpc() *utxorpc.Certificate {
765838
// TODO (#850)
766839
return nil
767840
}
841+
842+
func (c *UpdateDrepCertificate) Type() uint {
843+
return c.CertType
844+
}

ledger/common/common_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,3 +243,42 @@ func TestBlake2b224_String(t *testing.T) {
243243
t.Errorf("expected %s but got %s", expected, hash.String())
244244
}
245245
}
246+
247+
func TestCertificateTypeMethods(t *testing.T) {
248+
tests := []struct {
249+
name string
250+
cert Certificate
251+
expected uint
252+
}{
253+
{"StakeRegistration", &StakeRegistrationCertificate{CertType: CertificateTypeStakeRegistration}, CertificateTypeStakeRegistration},
254+
{"StakeDeregistration", &StakeDeregistrationCertificate{CertType: CertificateTypeStakeDeregistration}, CertificateTypeStakeDeregistration},
255+
{"StakeDelegation", &StakeDelegationCertificate{CertType: CertificateTypeStakeDelegation}, CertificateTypeStakeDelegation},
256+
{"PoolRegistration", &PoolRegistrationCertificate{CertType: CertificateTypePoolRegistration}, CertificateTypePoolRegistration},
257+
{"PoolRetirement", &PoolRetirementCertificate{CertType: CertificateTypePoolRetirement}, CertificateTypePoolRetirement},
258+
{"GenesisKeyDelegation", &GenesisKeyDelegationCertificate{CertType: CertificateTypeGenesisKeyDelegation}, CertificateTypeGenesisKeyDelegation},
259+
{"MoveInstantaneousRewards", &MoveInstantaneousRewardsCertificate{CertType: CertificateTypeMoveInstantaneousRewards}, CertificateTypeMoveInstantaneousRewards},
260+
{"Registration", &RegistrationCertificate{CertType: CertificateTypeRegistration}, CertificateTypeRegistration},
261+
{"Deregistration", &DeregistrationCertificate{CertType: CertificateTypeDeregistration}, CertificateTypeDeregistration},
262+
{"VoteDelegation", &VoteDelegationCertificate{CertType: CertificateTypeVoteDelegation}, CertificateTypeVoteDelegation},
263+
{"StakeVoteDelegation", &StakeVoteDelegationCertificate{CertType: CertificateTypeStakeVoteDelegation}, CertificateTypeStakeVoteDelegation},
264+
{"StakeRegistrationDelegation", &StakeRegistrationDelegationCertificate{CertType: CertificateTypeStakeRegistrationDelegation}, CertificateTypeStakeRegistrationDelegation},
265+
{"VoteRegistrationDelegation", &VoteRegistrationDelegationCertificate{CertType: CertificateTypeVoteRegistrationDelegation}, CertificateTypeVoteRegistrationDelegation},
266+
{"StakeVoteRegistrationDelegation", &StakeVoteRegistrationDelegationCertificate{CertType: CertificateTypeStakeVoteRegistrationDelegation}, CertificateTypeStakeVoteRegistrationDelegation},
267+
{"AuthCommitteeHot", &AuthCommitteeHotCertificate{CertType: CertificateTypeAuthCommitteeHot}, CertificateTypeAuthCommitteeHot},
268+
{"ResignCommitteeCold", &ResignCommitteeColdCertificate{CertType: CertificateTypeResignCommitteeCold}, CertificateTypeResignCommitteeCold},
269+
{"RegistrationDrep", &RegistrationDrepCertificate{CertType: CertificateTypeRegistrationDrep}, CertificateTypeRegistrationDrep},
270+
{"DeregistrationDrep", &DeregistrationDrepCertificate{CertType: CertificateTypeDeregistrationDrep}, CertificateTypeDeregistrationDrep},
271+
{"UpdateDrep", &UpdateDrepCertificate{CertType: CertificateTypeUpdateDrep}, CertificateTypeUpdateDrep},
272+
}
273+
274+
for _, tt := range tests {
275+
t.Run(tt.name, func(t *testing.T) {
276+
got := tt.cert.Type()
277+
if got != tt.expected {
278+
t.Errorf("FAIL: %s -> Type() = %d, expected = %d", tt.name, got, tt.expected)
279+
} else {
280+
t.Logf("PASS: %s -> Type() = %d, expected = %d \n", tt.name, got, tt.expected)
281+
}
282+
})
283+
}
284+
}

0 commit comments

Comments
 (0)