diff --git a/dot/node.go b/dot/node.go index 67aac2ef48..8b0352eb03 100644 --- a/dot/node.go +++ b/dot/node.go @@ -157,7 +157,7 @@ func (nodeBuilder) initNode(config *cfg.Config) error { } // create genesis block from trie - header, err := t.GenesisBlock() + header, err := runtime.GenesisBlockFromTrie(t) if err != nil { return fmt.Errorf("failed to create genesis block from trie: %w", err) } diff --git a/dot/node_integration_test.go b/dot/node_integration_test.go index 469f2d9c0f..441b1b34e8 100644 --- a/dot/node_integration_test.go +++ b/dot/node_integration_test.go @@ -102,7 +102,7 @@ func TestNewNode(t *testing.T) { return nil, fmt.Errorf("failed to create trie from genesis: %w", err) } // create genesis block from trie - header, err := trie.GenesisBlock() + header, err := runtime.GenesisBlockFromTrie(trie) if err != nil { return nil, fmt.Errorf("failed to create genesis block from trie: %w", err) } diff --git a/lib/runtime/genesis.go b/lib/runtime/genesis.go index 76a7aa19a2..a3c2990e07 100644 --- a/lib/runtime/genesis.go +++ b/lib/runtime/genesis.go @@ -7,6 +7,8 @@ import ( "errors" "fmt" + "github.com/ChainSafe/gossamer/dot/types" + "github.com/ChainSafe/gossamer/lib/common" "github.com/ChainSafe/gossamer/lib/genesis" "github.com/ChainSafe/gossamer/pkg/trie" in_memory_trie "github.com/ChainSafe/gossamer/pkg/trie/inmemory" @@ -33,3 +35,17 @@ func NewTrieFromGenesis(gen genesis.Genesis) (tr trie.Trie, err error) { return tr, nil } + +func GenesisBlockFromTrie(t trie.Trie) (genesisHeader types.Header, err error) { + rootHash, err := t.Hash() + if err != nil { + return genesisHeader, fmt.Errorf("root hashing trie: %w", err) + } + + parentHash := common.Hash{0} + extrinsicRoot := trie.EmptyHash + const blockNumber = 0 + digest := types.NewDigest() + genesisHeader = *types.NewHeader(parentHash, rootHash, extrinsicRoot, blockNumber, digest) + return genesisHeader, nil +} diff --git a/pkg/trie/inmemory/genesis.go b/pkg/trie/inmemory/genesis.go deleted file mode 100644 index ee4002192d..0000000000 --- a/pkg/trie/inmemory/genesis.go +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright 2022 ChainSafe Systems (ON) -// SPDX-License-Identifier: LGPL-3.0-only - -package inmemory - -import ( - "fmt" - - "github.com/ChainSafe/gossamer/dot/types" - "github.com/ChainSafe/gossamer/lib/common" - "github.com/ChainSafe/gossamer/pkg/trie" -) - -// GenesisBlock creates a genesis block from the trie. -func (t *InMemoryTrie) GenesisBlock() (genesisHeader types.Header, err error) { - rootHash, err := t.Hash() - if err != nil { - return genesisHeader, fmt.Errorf("root hashing trie: %w", err) - } - - parentHash := common.Hash{0} - extrinsicRoot := trie.EmptyHash - const blockNumber = 0 - digest := types.NewDigest() - genesisHeader = *types.NewHeader(parentHash, rootHash, extrinsicRoot, blockNumber, digest) - return genesisHeader, nil -} diff --git a/pkg/trie/inmemory/genesis_test.go b/pkg/trie/inmemory/genesis_test.go deleted file mode 100644 index 04d7188da2..0000000000 --- a/pkg/trie/inmemory/genesis_test.go +++ /dev/null @@ -1,74 +0,0 @@ -// Copyright 2022 ChainSafe Systems (ON) -// SPDX-License-Identifier: LGPL-3.0-only - -package inmemory - -import ( - "testing" - - "github.com/ChainSafe/gossamer/dot/types" - "github.com/ChainSafe/gossamer/lib/common" - "github.com/ChainSafe/gossamer/pkg/trie" - "github.com/ChainSafe/gossamer/pkg/trie/node" - "github.com/stretchr/testify/assert" -) - -func Test_InMemoryTrie_GenesisBlock(t *testing.T) { - t.Parallel() - - withHash := func(header types.Header) types.Header { - header.Hash() - return header - } - - testCases := map[string]struct { - trie InMemoryTrie - genesisHeader types.Header - errSentinel error - errMessage string - }{ - "empty_trie": { - genesisHeader: withHash(types.Header{ - ParentHash: common.Hash{0}, - StateRoot: trie.EmptyHash, - ExtrinsicsRoot: trie.EmptyHash, - Digest: types.NewDigest(), - }), - }, - "non_empty_trie": { - trie: InMemoryTrie{ - root: &node.Node{ - PartialKey: []byte{1, 2, 3}, - StorageValue: []byte{4, 5, 6}, - }, - }, - genesisHeader: withHash(types.Header{ - ParentHash: common.Hash{0}, - StateRoot: common.Hash{ - 0x25, 0xc1, 0x86, 0xd4, 0x5b, 0xc9, 0x1d, 0x9f, - 0xf5, 0xfd, 0x29, 0xd3, 0x29, 0x8a, 0xa3, 0x63, - 0x83, 0xf3, 0x2d, 0x14, 0xa8, 0xbd, 0xde, 0xc9, - 0x7b, 0x57, 0x92, 0x78, 0x67, 0xfc, 0x8a, 0xfa}, - ExtrinsicsRoot: trie.EmptyHash, - Digest: types.NewDigest(), - }), - }, - } - - for name, testCase := range testCases { - testCase := testCase - t.Run(name, func(t *testing.T) { - t.Parallel() - - trie := testCase.trie - - genesisHeader, err := trie.GenesisBlock() - - assert.ErrorIs(t, err, testCase.errSentinel) - if testCase.errSentinel != nil { - assert.EqualError(t, err, testCase.errMessage) - } - assert.Equal(t, testCase.genesisHeader, genesisHeader) - }) - } -} diff --git a/pkg/trie/trie.go b/pkg/trie/trie.go index 6f3404dfaa..b4bb005b7e 100644 --- a/pkg/trie/trie.go +++ b/pkg/trie/trie.go @@ -6,7 +6,6 @@ package trie import ( "fmt" - "github.com/ChainSafe/gossamer/dot/types" "github.com/ChainSafe/gossamer/lib/common" "github.com/ChainSafe/gossamer/pkg/trie/tracking" ) @@ -53,7 +52,6 @@ type Versioned interface { type Hashable interface { MustHash() common.Hash Hash() (common.Hash, error) - GenesisBlock() (genesisHeader types.Header, err error) } type Trie interface {