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

[sovereign] Get extended header from storage #6554

Merged
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
45 changes: 45 additions & 0 deletions process/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,51 @@ func GetMetaHeaderFromStorage(
return hdr, nil
}

// GetExtendedShardHeaderFromStorage gets the extended shard header, which is associated with the given hash, from storage
func GetExtendedShardHeaderFromStorage(
hash []byte,
marshaller marshal.Marshalizer,
storageService dataRetriever.StorageService,
) (*block.ShardHeaderExtended, error) {
buffHdr, err := GetMarshalizedHeaderFromStorage(dataRetriever.ExtendedShardHeadersUnit, hash, marshaller, storageService)
if err != nil {
return nil, err
}

hdr := &block.ShardHeaderExtended{}
err = marshaller.Unmarshal(hdr, buffHdr)
if err != nil {
return nil, ErrUnmarshalWithoutSuccess
}

return hdr, nil
}

// GetExtendedHeaderFromStorageWithNonce method returns an extended block header from storage with a given nonce
func GetExtendedHeaderFromStorageWithNonce(
nonce uint64,
storageService dataRetriever.StorageService,
uint64Converter typeConverters.Uint64ByteSliceConverter,
marshaller marshal.Marshalizer,
) (*block.ShardHeaderExtended, []byte, error) {
hash, err := GetHeaderHashFromStorageWithNonce(
nonce,
storageService,
uint64Converter,
marshaller,
dataRetriever.ExtendedShardHeadersNonceHashDataUnit)
if err != nil {
return nil, nil, err
}

hdr, err := GetExtendedShardHeaderFromStorage(hash, marshaller, storageService)
if err != nil {
return nil, nil, err
}

return hdr, hash, nil
}

// GetMarshalizedHeaderFromStorage gets the marshalized header, which is associated with the given hash, from storage
func GetMarshalizedHeaderFromStorage(
blockUnit dataRetriever.UnitType,
Expand Down
99 changes: 99 additions & 0 deletions process/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2362,3 +2362,102 @@ func TestShardedCacheSearchMethod_ToString(t *testing.T) {
str := process.ShardedCacheSearchMethod(166).ToString()
assert.Equal(t, "unknown method 166", str)
}

func TestGetExtendedSharHeaderFromStorage(t *testing.T) {
t.Parallel()

hash := []byte("hash")
marshaller := &mock.MarshalizerMock{}

t.Run("should work", func(t *testing.T) {
expectedHeader := &block.ShardHeaderExtended{
Header: &block.HeaderV2{
Header: &block.Header{
Nonce: 4,
},
},
}

storageService := &storageStubs.ChainStorerStub{
GetStorerCalled: func(unitType dataRetriever.UnitType) (storage.Storer, error) {
return &storageStubs.StorerStub{
GetCalled: func(key []byte) ([]byte, error) {
if bytes.Equal(key, hash) {
return marshaller.Marshal(expectedHeader)
}

return nil, errors.New("error")
},
}, nil
},
}

header, err := process.GetExtendedShardHeaderFromStorage(hash, marshaller, storageService)
require.Nil(t, err)
require.Equal(t, expectedHeader, header)
})

t.Run("nil storage service, should return error", func(t *testing.T) {
header, err := process.GetExtendedShardHeaderFromStorage(hash, marshaller, nil)
require.Equal(t, process.ErrNilStorage, err)
require.Nil(t, header)
})

t.Run("cannot unmarshal, should return error", func(t *testing.T) {
storageService := &storageStubs.ChainStorerStub{
GetStorerCalled: func(unitType dataRetriever.UnitType) (storage.Storer, error) {
return &storageStubs.StorerStub{
GetCalled: func(key []byte) ([]byte, error) {
return nil, nil
},
}, nil
},
}

header, err := process.GetExtendedShardHeaderFromStorage(hash, marshaller, storageService)
require.Equal(t, process.ErrUnmarshalWithoutSuccess, err)
require.Nil(t, header)
})
}

func TestGetExtendedHeaderFromStorageWithNonce(t *testing.T) {
t.Parallel()

nonce := uint64(1)
hash := []byte("hash")
marshaller := &mock.MarshalizerMock{}
expectedHeader := &block.ShardHeaderExtended{
Header: &block.HeaderV2{
Header: &block.Header{
Nonce: 4,
},
},
}

t.Run("should work", func(t *testing.T) {
storageService, uint64Converter := initDefaultStorageServiceAndConverter(nonce, hash, expectedHeader)
header, headerHash, err := process.GetExtendedHeaderFromStorageWithNonce(
nonce,
storageService,
uint64Converter,
marshaller)

require.Nil(t, err)
require.Equal(t, hash, headerHash)
require.Equal(t, expectedHeader, header)
})

t.Run("cannot unmarshall, should return error", func(t *testing.T) {
marshaller.Fail = true
storageService, uint64Converter := initDefaultStorageServiceAndConverter(nonce, hash, expectedHeader)
header, headerHash, err := process.GetExtendedHeaderFromStorageWithNonce(
nonce,
storageService,
uint64Converter,
marshaller)

require.Equal(t, process.ErrUnmarshalWithoutSuccess, err)
require.Nil(t, headerHash)
require.Nil(t, header)
})
}
Loading