Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(lib/genesis): less dependencies on other packages #2795

Merged
merged 16 commits into from
Sep 20, 2022

Conversation

qdm12
Copy link
Contributor

@qdm12 qdm12 commented Sep 1, 2022

Changes

TLDR: clear up lib/genesis cycle dependency hell 🔥 on lib/runtime/wasmer and lib/trie

That's gonna look like it's useless changes, but I actually hated my day working on this and it's really needed 😄

The reason is #2774 needs to run wasmer code to extract the state version from the runtime code in the genesis mapping.
And as it is, it makes lib/genesis import lib/runtime/wasmer which opens a whole can of worms of circular dependencies.

Anyway this fixes it and moves functions around such that lib/genesis does not import lib/runtime/wasmer nor lib/trie anymore.

  • Remove all utils/test helping global variables from lib/genesis and inline them where needed
  • duplicate test-only code helping functions where they are needed in helpers_test.go and unexport them. That way the Go production API is reduced and the lib/genesis imports are less. I know it's tempting to dedup the code, but when it comes to test helping functions we should 100% duplicate to avoid cycle dependency hell 👿
  • Change test helping functions to return genesis, trie and block header as values instead of pointers. This is part of my movement to not use pointers unless required for performance reasons (stack is better) and safety (no nil check needed).
  • Move lib/genesis's NewGenesisBlockFromTrie function as lib/trie Trie method GenesisBlock() (same thing, less genesis dependencies)
  • Move lib/genesis's NewTrieFromGenesis function to lib/runtime/wasmer since it will need in chore(all): version trie calls #2774 to use wasmer functions.
  • Move key constants from lib/runtime/constants.go to lib/genesis/constants.go

Tests

go test -tags integration github.com/ChainSafe/gossamer

Issues

Slow boat towards #2418

Primary Reviewer

@EclesioMeloJunior

@@ -144,7 +167,7 @@ func getGssmrRuntimeCode(t *testing.T) (code []byte) {
gssmrGenesis, err := genesis.NewGenesisFromJSONRaw(path)
require.NoError(t, err)

trie, err := genesis.NewTrieFromGenesis(gssmrGenesis)
trie, err := wasmer.NewTrieFromGenesis(*gssmrGenesis)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is moved to wasmer since it will need to run wasmer code to get the state version from the runtime code contained in the genesis.

dot/node.go Show resolved Hide resolved
dot/rpc/helpers_test.go Show resolved Hide resolved
@@ -16,8 +16,7 @@ import (
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/crypto"
"github.com/ChainSafe/gossamer/lib/runtime"
"github.com/ChainSafe/gossamer/lib/trie"
"github.com/ChainSafe/gossamer/lib/runtime/constants"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New subpackage containing just a few hex trie keys, to avoid cycle dependency hell. Also an example why we need moare subpackages.

Copy link
Member

@EclesioMeloJunior EclesioMeloJunior Sep 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@qdm12 the constants package you introduce here just contains some constants that are only used by production code while initializing the node and building the Genesis struct, I would suggest moving those constants to lib/genesis since they are specifically keys at the genesis file

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are are used by both wasmer and genesis packages (and wasmer imports genesis). We can thus either:

  • keep it as is in a separate dependency-free package
  • duplicate the constants in both wasmer and genesis

But it feels a bit better to have them in that constants package, what do you think?

Copy link
Contributor Author

@qdm12 qdm12 Sep 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to lib/genesis/keys.go

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also these constants are used by some tests in the wasmer package, not just production code.

lib/runtime/wasmer/genesis_test.go Show resolved Hide resolved
lib/trie/genesis.go Show resolved Hide resolved
@qdm12 qdm12 marked this pull request as ready for review September 2, 2022 21:40
@qdm12 qdm12 force-pushed the qdm12/genesis/less-dependencies branch from 1a6b2ee to 227f81e Compare September 6, 2022 15:38
Copy link
Contributor

@jimjbrettj jimjbrettj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice stuff!

@qdm12 qdm12 force-pushed the qdm12/genesis/less-dependencies branch from 227f81e to bcb4229 Compare September 7, 2022 23:44
dot/rpc/helpers_test.go Outdated Show resolved Hide resolved
@qdm12 qdm12 force-pushed the qdm12/genesis/less-dependencies branch from d5eed9a to 95ba320 Compare September 12, 2022 19:39
@qdm12 qdm12 force-pushed the qdm12/genesis/less-dependencies branch 2 times, most recently from f155179 to edd1f99 Compare September 16, 2022 12:29
dot/helpers_test.go Show resolved Hide resolved
lib/babe/helpers_test.go Outdated Show resolved Hide resolved
lib/runtime/wasmer/genesis.go Show resolved Hide resolved
@qdm12 qdm12 force-pushed the qdm12/genesis/less-dependencies branch from 81a9da0 to 357cabe Compare September 20, 2022 14:33
@qdm12 qdm12 force-pushed the qdm12/genesis/less-dependencies branch from 357cabe to 6b0f0eb Compare September 20, 2022 14:39
Copy link
Member

@EclesioMeloJunior EclesioMeloJunior left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@qdm12 qdm12 merged commit 1e38c35 into development Sep 20, 2022
@qdm12 qdm12 deleted the qdm12/genesis/less-dependencies branch September 20, 2022 15:14
@github-actions
Copy link

🎉 This PR is included in version 0.7.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants