ChainSafe / gossamer

🕸️ Go Implementation of the Polkadot Host
https://chainsafe.github.io/gossamer
GNU Lesser General Public License v3.0
427 stars 110 forks source link

refactor: External functions/methods accept as a parameter types declared in the internal package #3973

Open EmilGeorgiev opened 4 months ago

EmilGeorgiev commented 4 months ago

Issue summary

There are external functions that accept as a parameter type defined in the package internal. For example:

func NewBlockState(db database.Database, trs *Tries, telemetry Telemetry) (*BlockState, error) {...}

Here NewBlockState is exported function from the package "dot/state" and it accept as a parameter database.Database which is interface defined in the package internal. Since the internal package restricts visibility to within the module, any external clients of your library will not be able to provide an instance of database.Database to NewBlockState.

This design makes the exported function effectively unusable for anyone outside the module, breaking the intended API contract. For example if I import Gossmer in my project, I will have access to the function NewBlockState but is not possible to set provide the first parameter. I can't implement the interface Database and provide the implementation as a parameter because the interface has methods that return types that are also defined in the internal package

package main

import (
    "github.com/ChainSafe/gossamer/dot/state"
)

func main() {
    state.NewBlockState(<HERE I CAN'T PROVIDE IMPLEMENTATION OF THE INTERFACE Database>...)
}   

If the goal is to prevent certain functions and methods from being used outside of the module while keeping some level of internal modularity, there are better approaches than exporting functions that require internal types like unexported functions or internal package.

For a good example we can see the package 'internal' in Go source code:

internal/
    abi/
    bisect/
    buildcfg/
    bytealg/
    ....

There is no public method or function from the standard Go packages that accept or return type from the 'internal' packages

Related issues:

3975

EmilGeorgiev commented 3 months ago

If you think this makes sense, can I take this issue?