maticnetwork / heimdall

Validator node for Polygon PoS
https://polygon.technology/
GNU General Public License v3.0
267 stars 181 forks source link

Add Error Handling in GetBridgeDBInstance Function #1196

Open vinayak0035 opened 3 weeks ago

vinayak0035 commented 3 weeks ago

Summary

Implement error handling in the GetBridgeDBInstance function to handle cases where the bridgeDB object is nil.

Problem Definition

Currently, the GetBridgeDBInstance function does not handle scenarios where bridgeDB may be nil. If the database fails to open for any reason, returning a nil object can lead to potential runtime panics or errors when the returned database instance is used. Adding error handling will help prevent these issues and improve the robustness of the code.

Benefits of including this feature include:

Proposal

Modify the GetBridgeDBInstance function to include error handling. The implementation could look like this:

https://github.com/maticnetwork/heimdall/blob/master/bridge/setu/util/db.go#L16

func GetBridgeDBInstance(filePath string) *leveldb.DB {
    bridgeDBOnce.Do(func() {
        var err error
        bridgeDB, err = leveldb.OpenFile(filePath, nil)
        if err != nil {
            log.Fatalln("Error in Bor Opening Database", err.Error())
        }
    })
    return bridgeDB
}

This change ensures that the function returns an error if the database fails to open, allowing the caller to handle it appropriately.


For Admin Use

Raneet10 commented 2 weeks ago

Hey @vinayak0035 thanks for flagging this! Could you maybe open a PR for this ?

vinayak0035 commented 2 weeks ago

@Raneet10 PR: https://github.com/maticnetwork/heimdall/pull/1198

vinayak0035 commented 1 week ago

@Raneet10 @pratikspatil024 New PR: https://github.com/maticnetwork/heimdall/pull/1201