Skip to content

Commit

Permalink
Add tests for block backend impl
Browse files Browse the repository at this point in the history
  • Loading branch information
dimartiro committed Feb 14, 2025
1 parent 0911e24 commit f4772e0
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 7 deletions.
7 changes: 3 additions & 4 deletions internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -477,10 +477,9 @@ func (c *Client[H, Hasher, N, E, Header]) Block(hash H) (*generic.SignedBlock[N,
}

if header != nil && body != nil {
return &generic.SignedBlock[N, H, Hasher, E]{
Block: generic.NewBlock[N, H, Hasher](*header, body),
Justifications: justifications,
}, nil
return generic.NewSignedBlock(
generic.NewBlock[N, H, Hasher](*header, body), justifications,
), nil
}

return nil, nil
Expand Down
102 changes: 100 additions & 2 deletions internal/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,10 +341,12 @@ func TestPreCommitActions(t *testing.T) {
}

func TestHeaderBackendImplementation(t *testing.T) {
backendMock := mocks.NewBackend[hash.H256, uint64, runtime.BlakeTwo256, *generic.Header[uint64, hash.H256, runtime.BlakeTwo256],
backendMock := mocks.NewBackend[hash.H256, uint64, runtime.BlakeTwo256,
*generic.Header[uint64, hash.H256, runtime.BlakeTwo256],
runtime.OpaqueExtrinsic](t)

blockchainMock := mocks.NewBlockchainBackend[hash.H256, uint64, *generic.Header[uint64, hash.H256, runtime.BlakeTwo256], runtime.OpaqueExtrinsic](t)
blockchainMock := mocks.NewBlockchainBackend[hash.H256, uint64,
*generic.Header[uint64, hash.H256, runtime.BlakeTwo256], runtime.OpaqueExtrinsic](t)

expectedHeader := generic.NewHeader[uint64, hash.H256, runtime.BlakeTwo256](
1,
Expand Down Expand Up @@ -429,3 +431,99 @@ func TestHeaderBackendImplementation(t *testing.T) {
require.NotNil(t, blockNumber)
require.Equal(t, expectedNumber, *blockNumber)
}

func TestBlockBackendImplementation(t *testing.T) {
backendMock := mocks.NewBackend[hash.H256, uint64, runtime.BlakeTwo256,
*generic.Header[uint64, hash.H256, runtime.BlakeTwo256],
runtime.OpaqueExtrinsic](t)

blockchainMock := mocks.NewBlockchainBackend[hash.H256, uint64,
*generic.Header[uint64, hash.H256, runtime.BlakeTwo256], runtime.OpaqueExtrinsic](t)

c := New(backendMock)

expectedHeader := generic.NewHeader[uint64, hash.H256, runtime.BlakeTwo256](
1,
hash.H256("extrinsicsroot"),
hash.H256("stateroot"),
hash.H256("parent"),
runtime.Digest{},
)
expectedHash := expectedHeader.Hash()
expectedNumber := expectedHeader.Number()

blockchainMock.EXPECT().Header(expectedHash).Return(&expectedHeader, nil)
blockchainMock.EXPECT().Hash(expectedNumber).Return(&expectedHash, nil)

expectedExtrinsics := []runtime.OpaqueExtrinsic{}
blockchainMock.EXPECT().Body(expectedHash).Return(expectedExtrinsics, nil)

expectedStatus := blockchain.BlockStatusInChain
blockchainMock.EXPECT().Status(expectedHash).Return(expectedStatus, nil)

expectedIndexedExtrinsics := [][]byte{
[]byte("extrinsic1"),
[]byte("extrinsic2"),
}

blockchainMock.EXPECT().BlockIndexedBody(expectedHash).Return(expectedIndexedExtrinsics, nil)

var expectedJustifications runtime.Justifications = nil
blockchainMock.EXPECT().Justifications(expectedHash).Return(expectedJustifications, nil)

expectedIndexedTransaction := []byte("transaction1")
blockchainMock.EXPECT().IndexedTransaction(expectedHash).Return(expectedIndexedTransaction, nil)
blockchainMock.EXPECT().HasIndexedTransaction(expectedHash).Return(true, nil)

backendMock.EXPECT().RequiresFullSync().Return(true)
backendMock.EXPECT().Blockchain().Return(blockchainMock)

// Get BlockBody
extrinsics, err := c.BlockBody(expectedHash)
require.NoError(t, err)
require.Equal(t, expectedExtrinsics, extrinsics)

// Get BlockIndexedBody
indexedBody, err := c.BlockIndexedBody(expectedHash)
require.NoError(t, err)
require.Equal(t, expectedIndexedExtrinsics, indexedBody)

// Get Block
expectedBlock := generic.NewSignedBlock(
generic.NewBlock[uint64, hash.H256, runtime.BlakeTwo256](expectedHeader, expectedExtrinsics), nil,
)
block, err := c.Block(expectedHash)
require.NoError(t, err)
require.Equal(t, expectedBlock, block)

// Get BlockStatus
blockStatus, err := c.BlockStatus(expectedHash)
require.NoError(t, err)
require.Equal(t, expectedStatus, blockStatus)

// Get Justifications
justifications, err := c.Justifications(expectedHash)
require.NoError(t, err)
require.Equal(t, expectedJustifications, justifications)

// Get BlockHash
blockHash, err := c.BlockHash(expectedNumber)
require.NoError(t, err)
require.NotNil(t, blockHash)
require.Equal(t, expectedHash, *blockHash)

// Get IndexedTransaction
indexedTransaction, err := c.IndexedTransaction(expectedHash)
require.NoError(t, err)
require.Equal(t, expectedIndexedTransaction, indexedTransaction)

// HasIndexedTransactions
has, err := c.HasIndexedTransaction(expectedHash)
require.NoError(t, err)
require.True(t, has)

// RequiresFullSync
requiresFullSync := c.RequiresFullSync()
require.NoError(t, err)
require.True(t, requiresFullSync)
}
2 changes: 1 addition & 1 deletion internal/primitives/blockchain/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type BlockBackend[H runtime.Hash, N runtime.Number, Header runtime.Header[N, H],
BlockStatus(hash H) (BlockStatus, error)

// Justification gets block justifications for the block with the given hash.
Justification(hash H) (runtime.Justifications, error)
Justifications(hash H) (runtime.Justifications, error)

// BlockHash gets block hash by number.
BlockHash(number N) (*H, error)
Expand Down
8 changes: 8 additions & 0 deletions internal/primitives/runtime/generic/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,13 @@ type SignedBlock[N runtime.Number, H runtime.Hash, Hasher runtime.Hasher[H], E r
Justifications runtime.Justifications
}

func NewSignedBlock[N runtime.Number, H runtime.Hash, Hasher runtime.Hasher[H], E runtime.Extrinsic](
block Block[N, H, Hasher, E], justifications runtime.Justifications) *SignedBlock[N, H, Hasher, E] {
return &SignedBlock[N, H, Hasher, E]{
Block: block,
Justifications: justifications,
}
}

var _ runtime.Block[uint, hash.H256, runtime.OpaqueExtrinsic] = Block[uint, hash.H256,
runtime.BlakeTwo256, runtime.OpaqueExtrinsic]{}

0 comments on commit f4772e0

Please sign in to comment.