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

Fix: check auth changes before processing block #4336

Merged
merged 8 commits into from
Dec 13, 2024
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
29 changes: 10 additions & 19 deletions dot/sync/block_importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,20 +104,7 @@ func (b *blockImporter) importBlock(bd *types.BlockData, origin BlockOrigin) (im
// or the index of the block data that errored on failure.
func (b *blockImporter) processBlockData(blockData types.BlockData, origin BlockOrigin) error {
if blockData.Header != nil {
var (
hasJustification = blockData.Justification != nil && len(*blockData.Justification) > 0
round uint64
setID uint64
)

if hasJustification {
var err error
round, setID, err = b.finalityGadget.VerifyBlockJustification(
blockData.Header.Hash(), blockData.Header.Number, *blockData.Justification)
if err != nil {
return fmt.Errorf("verifying justification: %w", err)
}
}
header := blockData.Header

if blockData.Body != nil {
err := b.processBlockDataWithHeaderAndBody(blockData, origin)
dimartiro marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -126,18 +113,22 @@ func (b *blockImporter) processBlockData(blockData types.BlockData, origin Block
}
}

if hasJustification {
header := blockData.Header
err := b.blockState.SetFinalisedHash(header.Hash(), round, setID)
if blockData.Justification != nil && len(*blockData.Justification) > 0 {
round, setID, err := b.finalityGadget.VerifyBlockJustification(
header.Hash(), header.Number, *blockData.Justification)
if err != nil {
return fmt.Errorf("verifying justification for block %s: %w", header.Hash().String(), err)
}

err = b.blockState.SetFinalisedHash(header.Hash(), round, setID)
if err != nil {
return fmt.Errorf("setting finalised hash: %w", err)
}

err = b.blockState.SetJustification(header.Hash(), *blockData.Justification)
if err != nil {
return fmt.Errorf("setting justification for block number %d: %w", header.Number, err)
}

return nil
}
}
err := b.blockState.CompareAndSetBlockData(&blockData)
Expand Down
2 changes: 1 addition & 1 deletion dot/types/digest.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type DigestItemValues interface {

func newDigestItem[Value DigestItemValues](value Value) DigestItem {
item := DigestItem{}
setDigestItem[Value](&item, value)
setDigestItem(&item, value)
return item
}

Expand Down
11 changes: 11 additions & 0 deletions internal/client/consensus/grandpa/justification_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,17 @@ func TestJustificationEncoding(t *testing.T) {
require.Equal(t, expected, justification.Justification)
}

func TestDecodeJustificationFromWestendBlock2323464(t *testing.T) {
haikoschol marked this conversation as resolved.
Show resolved Hide resolved
//nolint:lll
encodedJustification := []byte{
101, 9, 0, 0, 0, 0, 0, 0, 160, 39, 234, 163, 193, 179, 177, 30, 36, 107, 194, 191, 221, 25, 90, 49, 196, 99, 44, 212, 88, 240, 35, 236, 228, 218, 193, 52, 63, 124, 13, 205, 8, 116, 35, 0, 44, 61, 255, 19, 108, 231, 232, 166, 4, 100, 186, 92, 153, 57, 80, 3, 56, 96, 6, 5, 68, 151, 191, 177, 72, 88, 198, 80, 161, 92, 202, 155, 243, 9, 116, 35, 0, 198, 201, 2, 183, 193, 46, 175, 3, 86, 192, 71, 8, 140, 159, 134, 111, 60, 19, 179, 232, 166, 52, 13, 83, 131, 67, 154, 255, 169, 77, 46, 164, 243, 101, 198, 120, 44, 34, 193, 252, 78, 116, 191, 19, 234, 3, 12, 73, 160, 191, 117, 195, 117, 189, 255, 154, 245, 228, 244, 56, 186, 175, 62, 5, 7, 217, 82, 218, 242, 208, 226, 97, 110, 83, 68, 166, 207, 249, 137, 163, 252, 197, 167, 154, 87, 153, 25, 140, 21, 255, 28, 6, 197, 26, 18, 128, 160, 39, 234, 163, 193, 179, 177, 30, 36, 107, 194, 191, 221, 25, 90, 49, 196, 99, 44, 212, 88, 240, 35, 236, 228, 218, 193, 52, 63, 124, 13, 205, 8, 116, 35, 0, 176, 249, 251, 175, 109, 52, 88, 104, 18, 58, 131, 78, 60, 254, 31, 223, 108, 15, 5, 172, 89, 74, 87, 71, 224, 95, 205, 14, 140, 187, 209, 168, 191, 202, 222, 118, 236, 59, 136, 204, 25, 155, 54, 195, 41, 185, 185, 25, 220, 59, 102, 243, 78, 196, 207, 151, 235, 88, 18, 254, 235, 223, 213, 10, 22, 157, 169, 111, 232, 137, 254, 25, 242, 233, 70, 60, 76, 183, 48, 179, 52, 115, 165, 97, 240, 161, 93, 85, 129, 202, 124, 82, 54, 42, 37, 45, 160, 39, 234, 163, 193, 179, 177, 30, 36, 107, 194, 191, 221, 25, 90, 49, 196, 99, 44, 212, 88, 240, 35, 236, 228, 218, 193, 52, 63, 124, 13, 205, 8, 116, 35, 0, 99, 120, 106, 150, 120, 143, 157, 182, 211, 188, 22, 39, 229, 213, 243, 215, 167, 136, 65, 250, 142, 19, 253, 68, 145, 41, 107, 23, 75, 214, 26, 218, 250, 117, 61, 126, 142, 16, 100, 37, 91, 52, 80, 139, 174, 188, 9, 16, 164, 118, 236, 64, 25, 60, 149, 184, 41, 98, 22, 130, 153, 247, 130, 14, 38, 165, 0, 130, 236, 99, 74, 108, 27, 250, 196, 212, 157, 16, 5, 85, 237, 246, 19, 223, 112, 61, 138, 10, 206, 62, 76, 149, 116, 94, 166, 153, 61, 255, 19, 108, 231, 232, 166, 4, 100, 186, 92, 153, 57, 80, 3, 56, 96, 6, 5, 68, 151, 191, 177, 72, 88, 198, 80, 161, 92, 202, 155, 243, 9, 116, 35, 0, 213, 174, 223, 48, 80, 151, 122, 6, 20, 150, 237, 141, 18, 241, 40, 29, 108, 125, 140, 66, 118, 219, 205, 152, 120, 254, 234, 104, 178, 115, 48, 11, 203, 253, 199, 30, 145, 96, 190, 248, 119, 153, 70, 212, 109, 128, 253, 128, 218, 102, 237, 172, 20, 67, 150, 223, 33, 18, 180, 177, 236, 90, 246, 8, 49, 218, 231, 151, 187, 172, 14, 39, 185, 1, 53, 92, 201, 4, 70, 54, 157, 205, 21, 216, 249, 101, 147, 59, 87, 114, 62, 227, 137, 103, 13, 85, 160, 39, 234, 163, 193, 179, 177, 30, 36, 107, 194, 191, 221, 25, 90, 49, 196, 99, 44, 212, 88, 240, 35, 236, 228, 218, 193, 52, 63, 124, 13, 205, 8, 116, 35, 0, 152, 158, 83, 232, 15, 138, 209, 77, 64, 107, 167, 37, 241, 208, 225, 167, 139, 4, 11, 198, 31, 54, 199, 4, 9, 37, 83, 81, 181, 3, 137, 105, 106, 8, 122, 244, 145, 242, 86, 170, 97, 164, 75, 149, 40, 184, 255, 144, 175, 167, 34, 239, 142, 209, 188, 254, 254, 172, 98, 151, 204, 104, 195, 3, 64, 75, 49, 166, 102, 51, 68, 198, 140, 126, 124, 42, 28, 244, 167, 103, 245, 58, 82, 95, 224, 34, 248, 13, 162, 131, 210, 200, 187, 19, 104, 109, 61, 255, 19, 108, 231, 232, 166, 4, 100, 186, 92, 153, 57, 80, 3, 56, 96, 6, 5, 68, 151, 191, 177, 72, 88, 198, 80, 161, 92, 202, 155, 243, 9, 116, 35, 0, 125, 196, 88, 9, 14, 244, 127, 43, 91, 8, 3, 111, 60, 100, 68, 214, 232, 192, 227, 100, 3, 85, 96, 123, 39, 218, 144, 8, 105, 89, 91, 81, 247, 144, 105, 157, 52, 137, 232, 1, 147, 140, 235, 159, 111, 171, 42, 118, 132, 34, 246, 112, 201, 39, 14, 11, 246, 240, 61, 96, 179, 74, 214, 13, 90, 240, 22, 123, 223, 44, 19, 81, 145, 248, 255, 177, 85, 199, 95, 9, 123, 133, 115, 69, 104, 121, 209, 216, 156, 81, 148, 80, 144, 230, 69, 231, 160, 39, 234, 163, 193, 179, 177, 30, 36, 107, 194, 191, 221, 25, 90, 49, 196, 99, 44, 212, 88, 240, 35, 236, 228, 218, 193, 52, 63, 124, 13, 205, 8, 116, 35, 0, 24, 143, 150, 165, 148, 107, 121, 215, 2, 248, 43, 157, 205, 237, 232, 215, 38, 4, 255, 171, 250, 235, 168, 112, 179, 227, 162, 65, 57, 103, 181, 78, 62, 138, 122, 170, 209, 197, 249, 15, 58, 60, 127, 131, 148, 23, 73, 104, 154, 68, 110, 187, 187, 81, 35, 7, 112, 40, 131, 72, 252, 159, 21, 0, 93, 5, 197, 56, 70, 126, 210, 89, 242, 82, 11, 86, 244, 159, 245, 136, 50, 204, 196, 64, 138, 105, 178, 138, 18, 187, 39, 63, 59, 65, 159, 44, 61, 255, 19, 108, 231, 232, 166, 4, 100, 186, 92, 153, 57, 80, 3, 56, 96, 6, 5, 68, 151, 191, 177, 72, 88, 198, 80, 161, 92, 202, 155, 243, 9, 116, 35, 0, 8, 223, 196, 18, 152, 147, 171, 52, 220, 14, 7, 73, 92, 31, 114, 102, 49, 225, 193, 77, 208, 80, 122, 181, 225, 249, 241, 231, 111, 108, 168, 32, 195, 46, 167, 253, 161, 55, 210, 186, 77, 178, 44, 110, 183, 255, 177, 239, 51, 54, 7, 210, 244, 140, 118, 238, 166, 204, 115, 22, 91, 40, 200, 12, 198, 220, 66, 100, 134, 46, 17, 156, 132, 171, 46, 163, 191, 78, 170, 157, 123, 225, 4, 216, 141, 127, 62, 224, 142, 9, 139, 224, 1, 181, 171, 248, 160, 39, 234, 163, 193, 179, 177, 30, 36, 107, 194, 191, 221, 25, 90, 49, 196, 99, 44, 212, 88, 240, 35, 236, 228, 218, 193, 52, 63, 124, 13, 205, 8, 116, 35, 0, 242, 86, 164, 127, 87, 169, 203, 130, 87, 220, 232, 163, 152, 74, 168, 1, 148, 28, 130, 172, 224, 187, 175, 110, 168, 210, 32, 192, 13, 239, 253, 128, 242, 61, 11, 147, 177, 71, 167, 232, 9, 164, 5, 55, 201, 23, 241, 38, 11, 183, 108, 222, 31, 205, 143, 241, 250, 113, 150, 153, 233, 157, 177, 2, 202, 111, 218, 104, 65, 158, 55, 67, 81, 194, 252, 131, 20, 178, 177, 182, 54, 147, 35, 144, 162, 50, 36, 210, 60, 48, 25, 122, 32, 236, 76, 178, 61, 255, 19, 108, 231, 232, 166, 4, 100, 186, 92, 153, 57, 80, 3, 56, 96, 6, 5, 68, 151, 191, 177, 72, 88, 198, 80, 161, 92, 202, 155, 243, 9, 116, 35, 0, 92, 247, 208, 78, 40, 206, 67, 51, 161, 33, 242, 20, 96, 34, 29, 153, 173, 107, 81, 107, 174, 81, 70, 243, 152, 34, 56, 210, 233, 60, 104, 208, 249, 218, 76, 246, 143, 141, 145, 59, 93, 208, 11, 89, 238, 251, 144, 58, 21, 242, 241, 146, 191, 53, 87, 152, 2, 72, 147, 31, 112, 116, 86, 15, 203, 185, 105, 7, 239, 188, 63, 248, 237, 145, 209, 113, 202, 38, 101, 138, 44, 57, 5, 137, 2, 117, 55, 94, 218, 147, 126, 180, 86, 129, 98, 126, 61, 255, 19, 108, 231, 232, 166, 4, 100, 186, 92, 153, 57, 80, 3, 56, 96, 6, 5, 68, 151, 191, 177, 72, 88, 198, 80, 161, 92, 202, 155, 243, 9, 116, 35, 0, 204, 187, 70, 137, 74, 136, 59, 59, 152, 243, 175, 33, 2, 120, 172, 158, 205, 10, 95, 114, 45, 98, 202, 56, 6, 164, 248, 213, 112, 43, 254, 76, 1, 145, 133, 70, 249, 18, 164, 216, 44, 65, 107, 2, 252, 250, 196, 133, 81, 133, 128, 94, 88, 14, 82, 34, 40, 21, 53, 173, 146, 129, 123, 9, 207, 44, 219, 247, 170, 156, 134, 196, 172, 58, 196, 93, 17, 2, 49, 213, 206, 34, 254, 84, 173, 21, 246, 247, 139, 134, 102, 195, 145, 222, 62, 120, 4, 160, 39, 234, 163, 193, 179, 177, 30, 36, 107, 194, 191, 221, 25, 90, 49, 196, 99, 44, 212, 88, 240, 35, 236, 228, 218, 193, 52, 63, 124, 13, 205, 38, 208, 141, 0, 141, 209, 83, 5, 194, 62, 249, 11, 242, 116, 184, 9, 131, 144, 52, 213, 192, 28, 99, 214, 166, 50, 99, 255, 194, 162, 132, 89, 224, 209, 215, 18, 175, 115, 178, 154, 62, 213, 23, 253, 41, 157, 129, 228, 37, 15, 202, 20, 16, 146, 91, 190, 175, 232, 194, 152, 111, 207, 21, 204, 183, 86, 44, 6, 8, 6, 66, 65, 66, 69, 52, 2, 4, 0, 0, 0, 208, 224, 230, 15, 0, 0, 0, 0, 5, 66, 65, 66, 69, 1, 1, 50, 255, 71, 85, 126, 223, 168, 216, 206, 184, 250, 72, 32, 198, 203, 75, 199, 72, 221, 129, 46, 148, 138, 198, 131, 87, 171, 222, 179, 77, 17, 67, 253, 235, 143, 215, 161, 94, 64, 52, 22, 176, 169, 99, 87, 111, 63, 212, 235, 51, 27, 29, 171, 103, 11, 175, 236, 95, 31, 146, 12, 171, 224, 136,
}

justification, err := DecodeJustification[hash.H256, uint32, runtime.BlakeTwo256](encodedJustification)
require.NoError(t, err)
require.Equal(t, uint32(2323464), justification.Target().Number)
}

func TestDecodeGrandpaJustificationVerifyFinalizes(t *testing.T) {
var a hash.H256 = "a\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" //nolint:lll

Expand Down
94 changes: 80 additions & 14 deletions internal/primitives/runtime/digest.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@

package runtime

import (
"fmt"

"github.com/ChainSafe/gossamer/pkg/scale"
)

// Digest item that is able to encode/decode 'system' digest items and
// provide opaque access to other items.
type DigestItemTypes interface {
Expand All @@ -11,12 +17,82 @@ type DigestItemTypes interface {

// Digest item that is able to encode/decode 'system' digest items and
// provide opaque access to other items.
// TODO: implement this as scale.VaryingDataType
type DigestItem any
type DigestItem struct {
haikoschol marked this conversation as resolved.
Show resolved Hide resolved
inner any
}

// NewDigestItem is constructor for DigestItem
func NewDigestItem[T DigestItemTypes](item T) DigestItem {
return NewDigestItem(item)
func NewDigestItem[T DigestItemTypes](value T) DigestItem {
item := DigestItem{}
setDigestItem(&item, value)
return item
}

func setDigestItem[Value DigestItemTypes](mvdt *DigestItem, value Value) {
mvdt.inner = value
}

func (mvdt *DigestItem) SetValue(value any) (err error) {
switch value := value.(type) {
case PreRuntime:
setDigestItem(mvdt, value)
return
case Consensus:
setDigestItem(mvdt, value)
return
case Seal:
setDigestItem(mvdt, value)
return
case RuntimeEnvironmentUpdated:
setDigestItem(mvdt, value)
return
case Other:
setDigestItem(mvdt, value)
return
default:
return fmt.Errorf("unsupported type")
}
}

func (mvdt DigestItem) IndexValue() (index uint, value any, err error) {
switch mvdt.inner.(type) {
case Other:
return 0, mvdt.inner, nil
case Consensus:
return 4, mvdt.inner, nil
case Seal:
return 5, mvdt.inner, nil
case PreRuntime:
return 6, mvdt.inner, nil
case RuntimeEnvironmentUpdated:
return 8, mvdt.inner, nil
}
return 0, nil, scale.ErrUnsupportedVaryingDataTypeValue
}

func (mvdt DigestItem) Value() (value any, err error) {
_, value, err = mvdt.IndexValue()
return
}

func (mvdt DigestItem) ValueAt(index uint) (value any, err error) {
switch index {
case 0:
return Other{}, nil
case 4:
return Consensus{}, nil
case 5:
return Seal{}, nil
case 6:
return PreRuntime{}, nil
case 8:
return RuntimeEnvironmentUpdated{}, nil
}
return nil, scale.ErrUnknownVaryingDataTypeValue
}

func (mvdt DigestItem) String() string {
return fmt.Sprintf("%s", mvdt.inner)
}

// A pre-runtime digest.
Expand Down Expand Up @@ -68,13 +144,3 @@ type Digest struct {
func (d *Digest) Push(item DigestItem) {
d.Logs = append(d.Logs, item)
}

// Pop a digest item.
func (d *Digest) Pop() DigestItem {
if len(d.Logs) == 0 {
return nil
}
item := d.Logs[len(d.Logs)-1]
d.Logs = d.Logs[:len(d.Logs)-1]
return item
}
31 changes: 31 additions & 0 deletions internal/primitives/runtime/digest_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2024 ChainSafe Systems (ON)
// SPDX-License-Identifier: LGPL-3.0-only

package runtime

import (
"testing"

"github.com/ChainSafe/gossamer/pkg/scale"
"github.com/stretchr/testify/require"
)

func TestEncodeDecodeDigest(t *testing.T) {
digest := Digest{
Logs: []DigestItem{
NewDigestItem(PreRuntime{
ConsensusEngineID: ConsensusEngineID{'F', 'R', 'N', 'K'},
Bytes: []byte("test"),
}),
},
}

encoded, err := scale.Marshal(digest)
require.NoError(t, err)

d := Digest{}
err = scale.Unmarshal(encoded, &d)
require.NoError(t, err)

require.Equal(t, digest, d)
}
39 changes: 39 additions & 0 deletions internal/primitives/runtime/generic/header_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2024 ChainSafe Systems (ON)
// SPDX-License-Identifier: LGPL-3.0-only

package generic

import (
"testing"

"github.com/ChainSafe/gossamer/internal/primitives/core/hash"
"github.com/ChainSafe/gossamer/internal/primitives/runtime"
"github.com/ChainSafe/gossamer/pkg/scale"
"github.com/stretchr/testify/require"
)

func TestEncodeDecodeHeader(t *testing.T) {
header := NewHeader[uint64, hash.H256, runtime.BlakeTwo256](
100,
hash.H256(""),
hash.H256(""),
hash.H256(""),
runtime.Digest{
Logs: []runtime.DigestItem{
runtime.NewDigestItem(runtime.PreRuntime{
ConsensusEngineID: runtime.ConsensusEngineID{'F', 'R', 'N', 'K'},
Bytes: []byte("test"),
}),
},
},
)

encoded, err := scale.Marshal(header)
require.NoError(t, err)

h := &Header[uint64, hash.H256, runtime.BlakeTwo256]{}
err = scale.Unmarshal(encoded, h)
require.NoError(t, err)

require.Equal(t, header, h)
}
15 changes: 7 additions & 8 deletions lib/grandpa/warp_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/ChainSafe/gossamer/dot/types"
consensus_grandpa "github.com/ChainSafe/gossamer/internal/client/consensus/grandpa"
"github.com/ChainSafe/gossamer/internal/primitives/consensus/grandpa"
primitives "github.com/ChainSafe/gossamer/internal/primitives/consensus/grandpa"
"github.com/ChainSafe/gossamer/internal/primitives/consensus/grandpa/app"
"github.com/ChainSafe/gossamer/internal/primitives/core/hash"
"github.com/ChainSafe/gossamer/internal/primitives/runtime"
Expand Down Expand Up @@ -80,7 +79,7 @@ func (w *WarpSyncProof) lastProofBlockNumber() uint64 {
// If we are able to verify all the fragments, then the warp proof is valid.
func (w *WarpSyncProof) verify(
setId grandpa.SetID,
authorities primitives.AuthorityList,
authorities grandpa.AuthorityList,
hardForks map[string]SetIdAuthorityList,
) (*SetIdAuthorityList, error) {
currentSetId := setId
Expand Down Expand Up @@ -141,7 +140,7 @@ func NewWarpSyncProofProvider(blockState BlockState, grandpaState GrandpaState)

type SetIdAuthorityList struct {
grandpa.SetID
primitives.AuthorityList
grandpa.AuthorityList
}

// Generate build a warp sync encoded proof starting from the given block hash
Expand Down Expand Up @@ -254,7 +253,7 @@ func (p *WarpSyncProofProvider) Generate(start common.Hash) ([]byte, error) {
func (p *WarpSyncProofProvider) Verify(
encodedProof []byte,
setId grandpa.SetID,
authorities primitives.AuthorityList,
authorities grandpa.AuthorityList,
) (*network.WarpSyncVerificationResult, error) {
var proof WarpSyncProof
err := scale.Unmarshal(encodedProof, &proof)
Expand Down Expand Up @@ -313,10 +312,10 @@ func findScheduledChange(
return nil, nil
}

func grandpaAuthoritiesRawToAuthorities(adr []types.GrandpaAuthoritiesRaw) (primitives.AuthorityList, error) {
ad := make([]primitives.AuthorityIDWeight, len(adr))
func grandpaAuthoritiesRawToAuthorities(adr []types.GrandpaAuthoritiesRaw) (grandpa.AuthorityList, error) {
ad := make([]grandpa.AuthorityIDWeight, len(adr))
for i, r := range adr {
ad[i] = primitives.AuthorityIDWeight{}
ad[i] = grandpa.AuthorityIDWeight{}

key, err := ed25519.NewPublicKey(r.Key[:])
if err != nil {
Expand All @@ -330,7 +329,7 @@ func grandpaAuthoritiesRawToAuthorities(adr []types.GrandpaAuthoritiesRaw) (prim
}

ad[i].AuthorityID = pkey
ad[i].AuthorityWeight = primitives.AuthorityWeight(r.ID)
ad[i].AuthorityWeight = grandpa.AuthorityWeight(r.ID)
}

return ad, nil
Expand Down
42 changes: 38 additions & 4 deletions lib/grandpa/warp_sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,11 +307,45 @@ func genericHeadersList(t *testing.T, headers []*types.Header) []runtime.Header[
if header == nil {
continue
}
newHeader := generic.Header[uint64, hash.H256, runtime.BlakeTwo256]{}
newHeader.SetParentHash(hash.H256(header.ParentHash.String()))
newHeader.SetNumber(uint64(header.Number))
newHeader.DigestMut().Push(header.Digest)

generic.NewHeader[uint64, hash.H256, runtime.BlakeTwo256](
uint64(header.Number),
hash.H256(string(header.ExtrinsicsRoot.ToBytes())),
hash.H256(string(header.StateRoot.ToBytes())),
hash.H256(string(header.ParentHash.ToBytes())),
mapDigest(t, header.Digest),
)
}

return headerList
}

func mapDigest(t *testing.T, digest types.Digest) runtime.Digest {
newDigest := runtime.Digest{}
for _, log := range digest {
value, err := log.Value()
require.NoError(t, err)

switch v := value.(type) {
case types.PreRuntimeDigest:
newDigest.Push(runtime.NewDigestItem(runtime.PreRuntime{
ConsensusEngineID: runtime.ConsensusEngineID(v.ConsensusEngineID),
Bytes: v.Data,
}))
case types.ConsensusDigest:
newDigest.Push(runtime.NewDigestItem(runtime.Consensus{
ConsensusEngineID: runtime.ConsensusEngineID(v.ConsensusEngineID),
Bytes: v.Data,
}))
case types.SealDigest:
newDigest.Push(runtime.NewDigestItem(runtime.Seal{
ConsensusEngineID: runtime.ConsensusEngineID(v.ConsensusEngineID),
Bytes: v.Data,
}))
case types.RuntimeEnvironmentUpdated:
newDigest.Push(runtime.NewDigestItem(runtime.RuntimeEnvironmentUpdated{}))
}
}

return newDigest
}
Loading