Skip to content

Commit f4772e0

Browse files
committed
Add tests for block backend impl
1 parent 0911e24 commit f4772e0

File tree

4 files changed

+112
-7
lines changed

4 files changed

+112
-7
lines changed

internal/client/client.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -477,10 +477,9 @@ func (c *Client[H, Hasher, N, E, Header]) Block(hash H) (*generic.SignedBlock[N,
477477
}
478478

479479
if header != nil && body != nil {
480-
return &generic.SignedBlock[N, H, Hasher, E]{
481-
Block: generic.NewBlock[N, H, Hasher](*header, body),
482-
Justifications: justifications,
483-
}, nil
480+
return generic.NewSignedBlock(
481+
generic.NewBlock[N, H, Hasher](*header, body), justifications,
482+
), nil
484483
}
485484

486485
return nil, nil

internal/client/client_test.go

Lines changed: 100 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,10 +341,12 @@ func TestPreCommitActions(t *testing.T) {
341341
}
342342

343343
func TestHeaderBackendImplementation(t *testing.T) {
344-
backendMock := mocks.NewBackend[hash.H256, uint64, runtime.BlakeTwo256, *generic.Header[uint64, hash.H256, runtime.BlakeTwo256],
344+
backendMock := mocks.NewBackend[hash.H256, uint64, runtime.BlakeTwo256,
345+
*generic.Header[uint64, hash.H256, runtime.BlakeTwo256],
345346
runtime.OpaqueExtrinsic](t)
346347

347-
blockchainMock := mocks.NewBlockchainBackend[hash.H256, uint64, *generic.Header[uint64, hash.H256, runtime.BlakeTwo256], runtime.OpaqueExtrinsic](t)
348+
blockchainMock := mocks.NewBlockchainBackend[hash.H256, uint64,
349+
*generic.Header[uint64, hash.H256, runtime.BlakeTwo256], runtime.OpaqueExtrinsic](t)
348350

349351
expectedHeader := generic.NewHeader[uint64, hash.H256, runtime.BlakeTwo256](
350352
1,
@@ -429,3 +431,99 @@ func TestHeaderBackendImplementation(t *testing.T) {
429431
require.NotNil(t, blockNumber)
430432
require.Equal(t, expectedNumber, *blockNumber)
431433
}
434+
435+
func TestBlockBackendImplementation(t *testing.T) {
436+
backendMock := mocks.NewBackend[hash.H256, uint64, runtime.BlakeTwo256,
437+
*generic.Header[uint64, hash.H256, runtime.BlakeTwo256],
438+
runtime.OpaqueExtrinsic](t)
439+
440+
blockchainMock := mocks.NewBlockchainBackend[hash.H256, uint64,
441+
*generic.Header[uint64, hash.H256, runtime.BlakeTwo256], runtime.OpaqueExtrinsic](t)
442+
443+
c := New(backendMock)
444+
445+
expectedHeader := generic.NewHeader[uint64, hash.H256, runtime.BlakeTwo256](
446+
1,
447+
hash.H256("extrinsicsroot"),
448+
hash.H256("stateroot"),
449+
hash.H256("parent"),
450+
runtime.Digest{},
451+
)
452+
expectedHash := expectedHeader.Hash()
453+
expectedNumber := expectedHeader.Number()
454+
455+
blockchainMock.EXPECT().Header(expectedHash).Return(&expectedHeader, nil)
456+
blockchainMock.EXPECT().Hash(expectedNumber).Return(&expectedHash, nil)
457+
458+
expectedExtrinsics := []runtime.OpaqueExtrinsic{}
459+
blockchainMock.EXPECT().Body(expectedHash).Return(expectedExtrinsics, nil)
460+
461+
expectedStatus := blockchain.BlockStatusInChain
462+
blockchainMock.EXPECT().Status(expectedHash).Return(expectedStatus, nil)
463+
464+
expectedIndexedExtrinsics := [][]byte{
465+
[]byte("extrinsic1"),
466+
[]byte("extrinsic2"),
467+
}
468+
469+
blockchainMock.EXPECT().BlockIndexedBody(expectedHash).Return(expectedIndexedExtrinsics, nil)
470+
471+
var expectedJustifications runtime.Justifications = nil
472+
blockchainMock.EXPECT().Justifications(expectedHash).Return(expectedJustifications, nil)
473+
474+
expectedIndexedTransaction := []byte("transaction1")
475+
blockchainMock.EXPECT().IndexedTransaction(expectedHash).Return(expectedIndexedTransaction, nil)
476+
blockchainMock.EXPECT().HasIndexedTransaction(expectedHash).Return(true, nil)
477+
478+
backendMock.EXPECT().RequiresFullSync().Return(true)
479+
backendMock.EXPECT().Blockchain().Return(blockchainMock)
480+
481+
// Get BlockBody
482+
extrinsics, err := c.BlockBody(expectedHash)
483+
require.NoError(t, err)
484+
require.Equal(t, expectedExtrinsics, extrinsics)
485+
486+
// Get BlockIndexedBody
487+
indexedBody, err := c.BlockIndexedBody(expectedHash)
488+
require.NoError(t, err)
489+
require.Equal(t, expectedIndexedExtrinsics, indexedBody)
490+
491+
// Get Block
492+
expectedBlock := generic.NewSignedBlock(
493+
generic.NewBlock[uint64, hash.H256, runtime.BlakeTwo256](expectedHeader, expectedExtrinsics), nil,
494+
)
495+
block, err := c.Block(expectedHash)
496+
require.NoError(t, err)
497+
require.Equal(t, expectedBlock, block)
498+
499+
// Get BlockStatus
500+
blockStatus, err := c.BlockStatus(expectedHash)
501+
require.NoError(t, err)
502+
require.Equal(t, expectedStatus, blockStatus)
503+
504+
// Get Justifications
505+
justifications, err := c.Justifications(expectedHash)
506+
require.NoError(t, err)
507+
require.Equal(t, expectedJustifications, justifications)
508+
509+
// Get BlockHash
510+
blockHash, err := c.BlockHash(expectedNumber)
511+
require.NoError(t, err)
512+
require.NotNil(t, blockHash)
513+
require.Equal(t, expectedHash, *blockHash)
514+
515+
// Get IndexedTransaction
516+
indexedTransaction, err := c.IndexedTransaction(expectedHash)
517+
require.NoError(t, err)
518+
require.Equal(t, expectedIndexedTransaction, indexedTransaction)
519+
520+
// HasIndexedTransactions
521+
has, err := c.HasIndexedTransaction(expectedHash)
522+
require.NoError(t, err)
523+
require.True(t, has)
524+
525+
// RequiresFullSync
526+
requiresFullSync := c.RequiresFullSync()
527+
require.NoError(t, err)
528+
require.True(t, requiresFullSync)
529+
}

internal/primitives/blockchain/backend.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ type BlockBackend[H runtime.Hash, N runtime.Number, Header runtime.Header[N, H],
2626
BlockStatus(hash H) (BlockStatus, error)
2727

2828
// Justification gets block justifications for the block with the given hash.
29-
Justification(hash H) (runtime.Justifications, error)
29+
Justifications(hash H) (runtime.Justifications, error)
3030

3131
// BlockHash gets block hash by number.
3232
BlockHash(number N) (*H, error)

internal/primitives/runtime/generic/block.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,5 +88,13 @@ type SignedBlock[N runtime.Number, H runtime.Hash, Hasher runtime.Hasher[H], E r
8888
Justifications runtime.Justifications
8989
}
9090

91+
func NewSignedBlock[N runtime.Number, H runtime.Hash, Hasher runtime.Hasher[H], E runtime.Extrinsic](
92+
block Block[N, H, Hasher, E], justifications runtime.Justifications) *SignedBlock[N, H, Hasher, E] {
93+
return &SignedBlock[N, H, Hasher, E]{
94+
Block: block,
95+
Justifications: justifications,
96+
}
97+
}
98+
9199
var _ runtime.Block[uint, hash.H256, runtime.OpaqueExtrinsic] = Block[uint, hash.H256,
92100
runtime.BlakeTwo256, runtime.OpaqueExtrinsic]{}

0 commit comments

Comments
 (0)