1515package ledger
1616
1717import (
18+ "errors"
1819 "fmt"
1920
2021 "github.com/blinklabs-io/dingo/database"
22+ "github.com/blinklabs-io/dingo/ledger/eras"
2123 "github.com/blinklabs-io/gouroboros/ledger"
2224 lcommon "github.com/blinklabs-io/gouroboros/ledger/common"
2325 pcommon "github.com/blinklabs-io/gouroboros/protocol/common"
@@ -27,15 +29,15 @@ func (ls *LedgerState) processTransactionCertificates(
2729 txn * database.Txn ,
2830 blockPoint pcommon.Point ,
2931 certs []ledger.Certificate ,
32+ blockEraId uint ,
3033) error {
3134 var tmpCert lcommon.Certificate
3235 for _ , tmpCert = range certs {
33- certDeposit , err := ls .currentEra .CertDepositFunc (
34- tmpCert ,
35- ls .currentPParams ,
36- )
36+ // Calculate certificate deposits using the block's era functions
37+ // This ensures we use the correct era-specific logic instead of current era
38+ certDeposit , err := ls .calculateCertificateDeposit (tmpCert , blockEraId )
3739 if err != nil {
38- return fmt .Errorf ("failed load cert deposit func : %w" , err )
40+ return fmt .Errorf ("get certificate deposit: %w" , err )
3941 }
4042 switch cert := tmpCert .(type ) {
4143 case * lcommon.DeregistrationCertificate :
@@ -189,3 +191,36 @@ func (ls *LedgerState) processTransactionCertificates(
189191 }
190192 return nil
191193}
194+
195+ // calculateCertificateDeposit calculates the certificate deposit using the appropriate era's
196+ // certificate deposit function. This ensures we use the correct era-specific logic instead
197+ // of always using the current era, which may not match the block's era for historical data.
198+ func (ls * LedgerState ) calculateCertificateDeposit (
199+ cert lcommon.Certificate ,
200+ blockEraId uint ,
201+ ) (uint64 , error ) {
202+ // Get the era descriptor for this block
203+ blockEra := eras .GetEraById (blockEraId )
204+ if blockEra == nil {
205+ return 0 , fmt .Errorf ("unknown era ID %d" , blockEraId )
206+ }
207+
208+ // If this era doesn't support certificates (like Byron), return 0
209+ if blockEra .CertDepositFunc == nil {
210+ return 0 , nil
211+ }
212+
213+ // Use the block era's certificate deposit function with current protocol parameters
214+ certDeposit , err := blockEra .CertDepositFunc (cert , ls .currentPParams )
215+ if err != nil {
216+ // Handle era type mismatch - this can happen when processing historical blocks
217+ // with newer protocol parameters, or when the certificate type didn't exist
218+ // in that era
219+ if errors .Is (err , eras .ErrIncompatibleProtocolParams ) {
220+ return 0 , nil
221+ }
222+ return 0 , err
223+ }
224+
225+ return certDeposit , nil
226+ }
0 commit comments