From abe14fc6cb39d7e1d0e9276582b7193d967e48d2 Mon Sep 17 00:00:00 2001 From: Diego Date: Thu, 21 Nov 2024 20:49:26 +0700 Subject: [PATCH 1/8] Fix: check auth changes before processing block --- dot/sync/block_importer.go | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/dot/sync/block_importer.go b/dot/sync/block_importer.go index 3e6ab4898f..395113c8a4 100644 --- a/dot/sync/block_importer.go +++ b/dot/sync/block_importer.go @@ -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) @@ -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) From 539444f022e8b33d110be6dd11582e97a8566620 Mon Sep 17 00:00:00 2001 From: Diego Date: Mon, 25 Nov 2024 20:44:19 +0900 Subject: [PATCH 2/8] Fix unmarshal generic header --- .../client/consensus/grandpa/justification_test.go | 10 ++++++++++ internal/primitives/runtime/generic/header.go | 6 ++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/internal/client/consensus/grandpa/justification_test.go b/internal/client/consensus/grandpa/justification_test.go index 868094c9cc..895713f2ec 100644 --- a/internal/client/consensus/grandpa/justification_test.go +++ b/internal/client/consensus/grandpa/justification_test.go @@ -81,6 +81,16 @@ func TestJustificationEncoding(t *testing.T) { require.Equal(t, expected, justification.Justification) } +func TestDecodeJustificationFromWestendBlock2323464(t *testing.T) { + 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 diff --git a/internal/primitives/runtime/generic/header.go b/internal/primitives/runtime/generic/header.go index ef10c50ab4..b1194e1b35 100644 --- a/internal/primitives/runtime/generic/header.go +++ b/internal/primitives/runtime/generic/header.go @@ -94,11 +94,13 @@ func (h Header[N, H, Hasher]) MarshalSCALE() ([]byte, error) { // UnmarshalSCALE implements custom SCALE decoding. func (h *Header[N, H, Hasher]) UnmarshalSCALE(r io.Reader) error { var header encodingHelper[H] - decoder := scale.NewDecoder(r) - err := decoder.Decode(&header) + var data []byte + _, err := r.Read(data) if err != nil { return err } + scale.Unmarshal(data, &header) + h.parentHash = header.ParentHash h.number = N(header.Number) h.stateRoot = header.StateRoot From eabb68c721d56ba180f71a83eb07469d48d5a61b Mon Sep 17 00:00:00 2001 From: Diego Date: Fri, 29 Nov 2024 22:10:27 +0900 Subject: [PATCH 3/8] Add more tests --- .../consensus/grandpa/justification_test.go | 1 + internal/primitives/runtime/digest_test.go | 28 +++++++++++++++ internal/primitives/runtime/generic/header.go | 5 ++- .../primitives/runtime/generic/header_test.go | 36 +++++++++++++++++++ 4 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 internal/primitives/runtime/digest_test.go create mode 100644 internal/primitives/runtime/generic/header_test.go diff --git a/internal/client/consensus/grandpa/justification_test.go b/internal/client/consensus/grandpa/justification_test.go index 895713f2ec..f9be0ced07 100644 --- a/internal/client/consensus/grandpa/justification_test.go +++ b/internal/client/consensus/grandpa/justification_test.go @@ -82,6 +82,7 @@ func TestJustificationEncoding(t *testing.T) { } func TestDecodeJustificationFromWestendBlock2323464(t *testing.T) { + //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, } diff --git a/internal/primitives/runtime/digest_test.go b/internal/primitives/runtime/digest_test.go new file mode 100644 index 0000000000..a311ba0668 --- /dev/null +++ b/internal/primitives/runtime/digest_test.go @@ -0,0 +1,28 @@ +package runtime + +import ( + "testing" + + "github.com/ChainSafe/gossamer/pkg/scale" + "github.com/stretchr/testify/require" +) + +func TestEncodeDecodeDigest(t *testing.T) { + digest := Digest{ + Logs: []DigestItem{ + 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) +} diff --git a/internal/primitives/runtime/generic/header.go b/internal/primitives/runtime/generic/header.go index b1194e1b35..b249198f79 100644 --- a/internal/primitives/runtime/generic/header.go +++ b/internal/primitives/runtime/generic/header.go @@ -94,12 +94,11 @@ func (h Header[N, H, Hasher]) MarshalSCALE() ([]byte, error) { // UnmarshalSCALE implements custom SCALE decoding. func (h *Header[N, H, Hasher]) UnmarshalSCALE(r io.Reader) error { var header encodingHelper[H] - var data []byte - _, err := r.Read(data) + decoder := scale.NewDecoder(r) + err := decoder.Decode(&header) if err != nil { return err } - scale.Unmarshal(data, &header) h.parentHash = header.ParentHash h.number = N(header.Number) diff --git a/internal/primitives/runtime/generic/header_test.go b/internal/primitives/runtime/generic/header_test.go new file mode 100644 index 0000000000..319aeb3134 --- /dev/null +++ b/internal/primitives/runtime/generic/header_test.go @@ -0,0 +1,36 @@ +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.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) +} From afb60d7ffe0cc78cec54b6eb01aca4a7522cebf6 Mon Sep 17 00:00:00 2001 From: Diego Date: Fri, 29 Nov 2024 22:37:27 +0900 Subject: [PATCH 4/8] Implement vdt for runtime DigestItem --- dot/types/digest.go | 2 +- internal/primitives/runtime/digest.go | 94 ++++++++++++++++--- internal/primitives/runtime/digest_test.go | 8 +- .../primitives/runtime/generic/header_test.go | 4 +- lib/grandpa/warp_sync_test.go | 1 + 5 files changed, 88 insertions(+), 21 deletions(-) diff --git a/dot/types/digest.go b/dot/types/digest.go index d18753b701..06931011bb 100644 --- a/dot/types/digest.go +++ b/dot/types/digest.go @@ -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 } diff --git a/internal/primitives/runtime/digest.go b/internal/primitives/runtime/digest.go index 7ed6358c60..09eb081c6f 100644 --- a/internal/primitives/runtime/digest.go +++ b/internal/primitives/runtime/digest.go @@ -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 { @@ -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 { + 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. @@ -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 -} diff --git a/internal/primitives/runtime/digest_test.go b/internal/primitives/runtime/digest_test.go index a311ba0668..fb10cb2c2d 100644 --- a/internal/primitives/runtime/digest_test.go +++ b/internal/primitives/runtime/digest_test.go @@ -10,18 +10,18 @@ import ( func TestEncodeDecodeDigest(t *testing.T) { digest := Digest{ Logs: []DigestItem{ - PreRuntime{ + 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) + d := Digest{} + err = scale.Unmarshal(encoded, &d) require.NoError(t, err) require.Equal(t, digest, d) diff --git a/internal/primitives/runtime/generic/header_test.go b/internal/primitives/runtime/generic/header_test.go index 319aeb3134..87ea4c5554 100644 --- a/internal/primitives/runtime/generic/header_test.go +++ b/internal/primitives/runtime/generic/header_test.go @@ -17,10 +17,10 @@ func TestEncodeDecodeHeader(t *testing.T) { hash.H256(""), runtime.Digest{ Logs: []runtime.DigestItem{ - runtime.PreRuntime{ + runtime.NewDigestItem(runtime.PreRuntime{ ConsensusEngineID: runtime.ConsensusEngineID{'F', 'R', 'N', 'K'}, Bytes: []byte("test"), - }, + }), }, }, ) diff --git a/lib/grandpa/warp_sync_test.go b/lib/grandpa/warp_sync_test.go index 11ad44ee49..c4b09091be 100644 --- a/lib/grandpa/warp_sync_test.go +++ b/lib/grandpa/warp_sync_test.go @@ -310,6 +310,7 @@ func genericHeadersList(t *testing.T, headers []*types.Header) []runtime.Header[ 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) } From 7d2ab3123e74db3a7e07b6fab621056884555a65 Mon Sep 17 00:00:00 2001 From: Diego Date: Fri, 29 Nov 2024 22:40:31 +0900 Subject: [PATCH 5/8] Dedup grandpa import in warp sync --- lib/grandpa/warp_sync.go | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/lib/grandpa/warp_sync.go b/lib/grandpa/warp_sync.go index 7b8c3d5122..0774433aed 100644 --- a/lib/grandpa/warp_sync.go +++ b/lib/grandpa/warp_sync.go @@ -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" @@ -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 @@ -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 @@ -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) @@ -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 { @@ -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 From 4dbf1520c69a29839ff2d2764fe2bfe0f6a42b59 Mon Sep 17 00:00:00 2001 From: Diego Date: Fri, 29 Nov 2024 23:51:47 +0900 Subject: [PATCH 6/8] Fix tests --- lib/grandpa/warp_sync_test.go | 41 +++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/lib/grandpa/warp_sync_test.go b/lib/grandpa/warp_sync_test.go index c4b09091be..8f26c2e9a5 100644 --- a/lib/grandpa/warp_sync_test.go +++ b/lib/grandpa/warp_sync_test.go @@ -307,12 +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 +} From f3449072077e99ca6387d1aab0dab5c72de901bd Mon Sep 17 00:00:00 2001 From: Diego Date: Fri, 29 Nov 2024 23:54:10 +0900 Subject: [PATCH 7/8] Remove unnecesary space --- internal/primitives/runtime/generic/header.go | 1 - 1 file changed, 1 deletion(-) diff --git a/internal/primitives/runtime/generic/header.go b/internal/primitives/runtime/generic/header.go index b249198f79..ef10c50ab4 100644 --- a/internal/primitives/runtime/generic/header.go +++ b/internal/primitives/runtime/generic/header.go @@ -99,7 +99,6 @@ func (h *Header[N, H, Hasher]) UnmarshalSCALE(r io.Reader) error { if err != nil { return err } - h.parentHash = header.ParentHash h.number = N(header.Number) h.stateRoot = header.StateRoot From 0d9111aba1a8998491e066c6b044173a2341e7e2 Mon Sep 17 00:00:00 2001 From: Diego Date: Fri, 29 Nov 2024 23:56:55 +0900 Subject: [PATCH 8/8] Add missing licenses --- internal/primitives/runtime/digest_test.go | 3 +++ internal/primitives/runtime/generic/header_test.go | 3 +++ 2 files changed, 6 insertions(+) diff --git a/internal/primitives/runtime/digest_test.go b/internal/primitives/runtime/digest_test.go index fb10cb2c2d..e8cd90aa55 100644 --- a/internal/primitives/runtime/digest_test.go +++ b/internal/primitives/runtime/digest_test.go @@ -1,3 +1,6 @@ +// Copyright 2024 ChainSafe Systems (ON) +// SPDX-License-Identifier: LGPL-3.0-only + package runtime import ( diff --git a/internal/primitives/runtime/generic/header_test.go b/internal/primitives/runtime/generic/header_test.go index 87ea4c5554..d259ac46d7 100644 --- a/internal/primitives/runtime/generic/header_test.go +++ b/internal/primitives/runtime/generic/header_test.go @@ -1,3 +1,6 @@ +// Copyright 2024 ChainSafe Systems (ON) +// SPDX-License-Identifier: LGPL-3.0-only + package generic import (