ChainSafe / ChainSafe/gossamer
refactor: External functions/methods accept as a parameter types declared in the internal package
- Dominant language
- Go
- Stars
- 454
- Forks
- 144
- PR merge metrics
- No merged PRs in 30d
Description
## Issue summary
There are external functions that accept as a parameter type defined in the package internal. For example:
```go
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
```go
package main
import (
"github.com/ChainSafe/gossamer/dot/state"
)
func main() {
state.NewBlockState(...)
}
```
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:
```text
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
Contributor guide
Research direction
Start by auditing exported functions and methods in the dot/state package, including NewBlockState, for parameters or return values from internal packages. Review the related issue #3975 and trace the affected internal interfaces and types. Done means the public API no longer requires external clients to provide or consume internal types, with the intended modularity preserved.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go
- Domain
- backend-api-design
- Issue type
- Refactor
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100