Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dot/types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Block struct {
}

// NewBlockFromGeneric returns a new Block from a generic block
func NewBlockFromGeneric[N runtime.Number, H runtime.Hash, E runtime.Extrinsic](gb runtime.Block[N, H, E]) (
func NewBlockFromGeneric[N runtime.Number, H runtime.Hash, E runtime.Extrinsic, Header runtime.Header[N, H]](gb runtime.Block[H, N, E, Header]) (
*Block, error) {
header, err := NewHeaderFromGeneric(gb.Header())
if err != nil {
Expand Down
14 changes: 7 additions & 7 deletions dot/types/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,19 +154,19 @@ func TestNewBlockFromGeneric(t *testing.T) {

digest := runtime.Digest{
Logs: []runtime.DigestItem{
runtime.NewDigestItem(runtime.Consensus{
runtime.DigestItemConsensus{
ConsensusEngineID: runtime.ConsensusEngineID{'B', 'E', 'E', 'F'},
Bytes: []byte("test"),
}),
runtime.NewDigestItem(runtime.Seal{
},
runtime.DigestItemSeal{
ConsensusEngineID: runtime.ConsensusEngineID{'S', 'E', 'A', 'L'},
Bytes: []byte("test"),
}),
runtime.NewDigestItem(runtime.PreRuntime{
},
runtime.DigestItemPreRuntime{
ConsensusEngineID: runtime.ConsensusEngineID{'B', 'A', 'B', 'E'},
Bytes: []byte("test"),
}),
runtime.NewDigestItem(runtime.RuntimeEnvironmentUpdated{}),
},
runtime.DigestItemRuntimeEnvironmentUpdated{},
},
}

Expand Down
17 changes: 6 additions & 11 deletions dot/types/digest.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,36 +103,31 @@ type Digest []DigestItem
func NewDigestFromGeneric(gd runtime.Digest) (Digest, error) {
newDigest := Digest{}
for _, log := range gd.Logs {
value, err := log.Value()
if err != nil {
return nil, err
}

var digest any

switch v := value.(type) {
case runtime.PreRuntime:
switch v := log.(type) {
case runtime.DigestItemPreRuntime:
digest = PreRuntimeDigest{
ConsensusEngineID: ConsensusEngineID(v.ConsensusEngineID),
Data: v.Bytes,
}
case runtime.Consensus:
case runtime.DigestItemConsensus:
digest = ConsensusDigest{
ConsensusEngineID: ConsensusEngineID(v.ConsensusEngineID),
Data: v.Bytes,
}
case runtime.Seal:
case runtime.DigestItemSeal:
digest = SealDigest{
ConsensusEngineID: ConsensusEngineID(v.ConsensusEngineID),
Data: v.Bytes,
}
case runtime.RuntimeEnvironmentUpdated:
case runtime.DigestItemRuntimeEnvironmentUpdated:
digest = RuntimeEnvironmentUpdated{}
default:
return nil, fmt.Errorf("unsupported type")
}

err = newDigest.Add(digest)
err := newDigest.Add(digest)
if err != nil {
return nil, err
}
Expand Down
14 changes: 7 additions & 7 deletions dot/types/digest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,19 +249,19 @@ func TestSealDigest(t *testing.T) {
func TestFromGenericDigest(t *testing.T) {
digest := runtime.Digest{
Logs: []runtime.DigestItem{
runtime.NewDigestItem(runtime.Consensus{
runtime.DigestItemConsensus{
ConsensusEngineID: runtime.ConsensusEngineID{'B', 'E', 'E', 'F'},
Bytes: []byte("test"),
}),
runtime.NewDigestItem(runtime.Seal{
},
runtime.DigestItemSeal{
ConsensusEngineID: runtime.ConsensusEngineID{'S', 'E', 'A', 'L'},
Bytes: []byte("test"),
}),
runtime.NewDigestItem(runtime.PreRuntime{
},
runtime.DigestItemPreRuntime{
ConsensusEngineID: runtime.ConsensusEngineID{'B', 'A', 'B', 'E'},
Bytes: []byte("test"),
}),
runtime.NewDigestItem(runtime.RuntimeEnvironmentUpdated{}),
},
runtime.DigestItemRuntimeEnvironmentUpdated{},
},
}

Expand Down
14 changes: 7 additions & 7 deletions dot/types/header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,19 +114,19 @@ func TestFromGenericHeader(t *testing.T) {
t.Run("successful_conversion", func(t *testing.T) {
digest := runtime.Digest{
Logs: []runtime.DigestItem{
runtime.NewDigestItem(runtime.Consensus{
runtime.DigestItemConsensus{
ConsensusEngineID: runtime.ConsensusEngineID{'B', 'E', 'E', 'F'},
Bytes: []byte("test"),
}),
runtime.NewDigestItem(runtime.Seal{
},
runtime.DigestItemSeal{
ConsensusEngineID: runtime.ConsensusEngineID{'S', 'E', 'A', 'L'},
Bytes: []byte("test"),
}),
runtime.NewDigestItem(runtime.PreRuntime{
},
runtime.DigestItemPreRuntime{
ConsensusEngineID: runtime.ConsensusEngineID{'B', 'A', 'B', 'E'},
Bytes: []byte("test"),
}),
runtime.NewDigestItem(runtime.RuntimeEnvironmentUpdated{}),
},
runtime.DigestItemRuntimeEnvironmentUpdated{},
},
}

Expand Down
6 changes: 3 additions & 3 deletions internal/client/adapter/client_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (ca *ClientAdapter[H, Hasher, N, E, Header]) BestBlock() (*types.Block, err
return nil, nil
}

return types.NewBlockFromGeneric(signedBlock.Block)
return types.NewBlockFromGeneric[N, H, E](signedBlock.Block)
}

func (ca *ClientAdapter[H, Hasher, N, E, Header]) BestBlockHash() common.Hash {
Expand Down Expand Up @@ -144,7 +144,7 @@ func (ca *ClientAdapter[H, Hasher, N, E, Header]) GetBlockByHash(bhash common.Ha
return nil, nil
}

return types.NewBlockFromGeneric(block.Block)
return types.NewBlockFromGeneric[N, H, E](block.Block)
}

func (ca *ClientAdapter[H, Hasher, N, E, Header]) GetBlockByNumber(blockNumber uint) (*types.Block, error) {
Expand All @@ -166,7 +166,7 @@ func (ca *ClientAdapter[H, Hasher, N, E, Header]) GetBlockByNumber(blockNumber u
return nil, nil
}

return types.NewBlockFromGeneric(signedBlock.Block)
return types.NewBlockFromGeneric[N, H, E](signedBlock.Block)
}

// GetFinalisedHeader is unimplemented
Expand Down
4 changes: 2 additions & 2 deletions internal/client/adapter/client_adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func TestBlockOps(t *testing.T) {
client, _, adapter := setupTest(t)

t.Run("best_block_ok", func(t *testing.T) {
expectedBlock, err := types.NewBlockFromGeneric(signedBlock.Block)
expectedBlock, err := types.NewBlockFromGeneric[Number, Hash, Extrinsic](signedBlock.Block)
require.NoError(t, err)

client.EXPECT().Info().Return(blockchainInfo)
Expand Down Expand Up @@ -326,7 +326,7 @@ func TestGetBlockByNumber(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, block)

expectedBlock, err := types.NewBlockFromGeneric(block)
expectedBlock, err := types.NewBlockFromGeneric[Number, Hash, Extrinsic](block)
require.NoError(t, err)

require.Equal(t, expectedBlock, returnedBlock)
Expand Down
14 changes: 7 additions & 7 deletions internal/client/adapter/mocks/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

109 changes: 105 additions & 4 deletions internal/client/api/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/ChainSafe/gossamer/internal/primitives/blockchain"
"github.com/ChainSafe/gossamer/internal/primitives/consensus/common"
"github.com/ChainSafe/gossamer/internal/primitives/core/offchain"
"github.com/ChainSafe/gossamer/internal/primitives/kv"
"github.com/ChainSafe/gossamer/internal/primitives/runtime"
statemachine "github.com/ChainSafe/gossamer/internal/primitives/state-machine"
"github.com/ChainSafe/gossamer/internal/primitives/state-machine/overlayedchanges"
Expand Down Expand Up @@ -237,10 +238,7 @@ type Finalizer[
}

// KeyValue is used in [AuxStore.InsertAux]. Key and Value should not be nil.
type KeyValue struct {
Key []byte
Value []byte
}
type KeyValue = kv.KeyValue

// AuxStore provides access to an auxiliary database.
//
Expand All @@ -256,6 +254,109 @@ type AuxStore interface {
GetAux(key []byte) ([]byte, error)
}

// / An `Iterator` that iterates keys in a given block under a prefix.
// pub struct KeysIter<State, Block>
// where
//
// State: StateBackend<HashFor<Block>>,
// Block: BlockT,
//
// {
// inner: <State as StateBackend<HashFor<Block>>>::RawIter,
// state: State,
// }
type KeysIter[H runtime.Hash, N runtime.Number, Hasher runtime.Hasher[H]] struct {
inner statemachine.StorageIterator[H, Hasher]
state statemachine.Backend[H, Hasher]
}

// / An `Iterator` that iterates keys and values in a given block under a prefix.
// pub struct PairsIter<State, Block>
// where
//
// State: StateBackend<HashFor<Block>>,
// Block: BlockT,
//
// {
// inner: <State as StateBackend<HashFor<Block>>>::RawIter,
// state: State,
// }
type PairsIter[H runtime.Hash, N runtime.Number, Hasher runtime.Hasher[H]] struct {
inner statemachine.StorageIterator[H, Hasher]
state statemachine.Backend[H, Hasher]
}

// / Provides access to storage primitives
// pub trait StorageProvider<Block: BlockT, B: Backend<Block>> {
type StorageProvider[H runtime.Hash, N runtime.Number, Hasher runtime.Hasher[H]] interface {
/// Given a block's `Hash` and a key, return the value under the key in that block.
// fn storage(
// &self,
// hash: Block::Hash,
// key: &StorageKey,
// ) -> sp_blockchain::Result<Option<StorageData>>;
Storage(hash H, key storage.StorageKey) (*storage.StorageData, error)

/// Given a block's `Hash` and a key, return the value under the hash in that block.
// fn storage_hash(
// &self,
// hash: Block::Hash,
// key: &StorageKey,
// ) -> sp_blockchain::Result<Option<Block::Hash>>;
StorageHash(hash H, key storage.StorageKey) (*H, error)

/// Given a block's `Hash` and a key prefix, returns a `KeysIter` iterates matching storage
/// keys in that block.
// fn storage_keys(
// &self,
// hash: Block::Hash,
// prefix: Option<&StorageKey>,
// start_key: Option<&StorageKey>,
// ) -> sp_blockchain::Result<KeysIter<B::State, Block>>;
StorageKeys(hash H, prefix *storage.StorageKey, startKey *storage.StorageKey) (KeysIter[H, N, Hasher], error)

/// Given a block's `Hash` and a key prefix, returns an iterator over the storage keys and
/// values in that block.
// fn storage_pairs(
// &self,
// hash: <Block as BlockT>::Hash,
// prefix: Option<&StorageKey>,
// start_key: Option<&StorageKey>,
// ) -> sp_blockchain::Result<PairsIter<B::State, Block>>;
StoragePairs(hash H, prefix *storage.StorageKey, startKey *storage.StorageKey) (PairsIter[H, N, Hasher], error)

/// Given a block's `Hash`, a key and a child storage key, return the value under the key in
/// that block.
// fn child_storage(
// &self,
// hash: Block::Hash,
// child_info: &ChildInfo,
// key: &StorageKey,
// ) -> sp_blockchain::Result<Option<StorageData>>;
ChildStorage(hash H, childInfo storage.ChildInfo, key storage.StorageKey) (*storage.StorageData, error)

// /// Given a block's `Hash` and a key `prefix` and a child storage key,
// /// returns a `KeysIter` that iterates matching storage keys in that block.
// fn child_storage_keys(
// &self,
// hash: Block::Hash,
// child_info: ChildInfo,
// prefix: Option<&StorageKey>,
// start_key: Option<&StorageKey>,
// ) -> sp_blockchain::Result<KeysIter<B::State, Block>>;
ChildStorageKeys(hash H, childInfo storage.ChildInfo, prefix *storage.StorageKey, startKey *storage.StorageKey) (KeysIter[H, N, Hasher], error)

// /// Given a block's `Hash`, a key and a child storage key, return the hash under the key in that
// /// block.
// fn child_storage_hash(
// &self,
// hash: Block::Hash,
// child_info: &ChildInfo,
// key: &StorageKey,
// ) -> sp_blockchain::Result<Option<Block::Hash>>;
ChildStorageHash(hash H, childInfo storage.ChildInfo, key storage.StorageKey) (*H, error)
}

// Backend is the client backend.
//
// Manages the data layer.
Expand Down
Loading
Loading