Skip to content

Commit 479821c

Browse files
committed
test: separate block sync summary parse tests into a new file
1 parent 2bf6393 commit 479821c

File tree

3 files changed

+56
-23
lines changed

3 files changed

+56
-23
lines changed

vms/evm/sync/message/block_sync_summary_parser.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,18 @@
44
package message
55

66
import (
7+
"errors"
78
"fmt"
89

910
"github.com/ava-labs/libevm/crypto"
1011

1112
"github.com/ava-labs/avalanchego/ids"
1213
)
1314

15+
// errInvalidBlockSyncSummary is returned when the provided bytes cannot be
16+
// parsed into a valid BlockSyncSummary.
17+
var errInvalidBlockSyncSummary = errors.New("invalid block sync summary")
18+
1419
type BlockSyncSummaryParser struct{}
1520

1621
func NewBlockSyncSummaryParser() *BlockSyncSummaryParser {
@@ -20,7 +25,7 @@ func NewBlockSyncSummaryParser() *BlockSyncSummaryParser {
2025
func (*BlockSyncSummaryParser) Parse(summaryBytes []byte, acceptImpl AcceptImplFn) (Syncable, error) {
2126
summary := BlockSyncSummary{}
2227
if _, err := Codec.Unmarshal(summaryBytes, &summary); err != nil {
23-
return nil, fmt.Errorf("failed to parse syncable summary: %w", err)
28+
return nil, fmt.Errorf("%w", errInvalidBlockSyncSummary)
2429
}
2530

2631
summary.bytes = summaryBytes
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright (C) 2019-2025, Ava Labs, Inc. All rights reserved.
2+
// See the file LICENSE for licensing terms.
3+
4+
package message
5+
6+
import (
7+
"context"
8+
"testing"
9+
10+
"github.com/ava-labs/libevm/common"
11+
"github.com/stretchr/testify/require"
12+
13+
"github.com/ava-labs/avalanchego/snow/engine/snowman/block"
14+
)
15+
16+
func TestBlockSyncSummaryParser_ParseValid(t *testing.T) {
17+
t.Parallel()
18+
19+
blockSyncSummary, err := NewBlockSyncSummary(common.Hash{1}, 2, common.Hash{3})
20+
require.NoError(t, err)
21+
22+
parser := NewBlockSyncSummaryParser()
23+
called := false
24+
acceptImplTest := func(Syncable) (block.StateSyncMode, error) {
25+
called = true
26+
return block.StateSyncSkipped, nil
27+
}
28+
s, err := parser.Parse(blockSyncSummary.Bytes(), acceptImplTest)
29+
require.NoError(t, err)
30+
require.Equal(t, blockSyncSummary.GetBlockHash(), s.GetBlockHash())
31+
require.Equal(t, blockSyncSummary.Height(), s.Height())
32+
require.Equal(t, blockSyncSummary.GetBlockRoot(), s.GetBlockRoot())
33+
require.Equal(t, blockSyncSummary.Bytes(), s.Bytes())
34+
35+
mode, err := s.Accept(context.Background())
36+
require.NoError(t, err)
37+
require.Equal(t, block.StateSyncSkipped, mode)
38+
require.True(t, called)
39+
}
40+
41+
func TestBlockSyncSummaryParser_ParseInvalid(t *testing.T) {
42+
t.Parallel()
43+
44+
parser := NewBlockSyncSummaryParser()
45+
_, err := parser.Parse([]byte("not-a-summary"), nil)
46+
require.ErrorIs(t, err, errInvalidBlockSyncSummary)
47+
}

vms/evm/sync/message/block_sync_summary_test.go

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,16 @@
44
package message
55

66
import (
7-
"context"
87
"encoding/base64"
98
"testing"
109

1110
"github.com/ava-labs/libevm/common"
1211
"github.com/stretchr/testify/require"
13-
14-
"github.com/ava-labs/avalanchego/snow/engine/snowman/block"
1512
)
1613

17-
func TestMarshalBlockSyncSummary(t *testing.T) {
14+
func TestBlockSyncSummary_MarshalGolden(t *testing.T) {
15+
t.Parallel()
16+
1817
blockSyncSummary, err := NewBlockSyncSummary(common.Hash{1}, 2, common.Hash{3})
1918
require.NoError(t, err)
2019

@@ -24,22 +23,4 @@ func TestMarshalBlockSyncSummary(t *testing.T) {
2423

2524
expectedBase64Bytes := "AAAAAAAAAAAAAgEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="
2625
require.Equal(t, expectedBase64Bytes, base64.StdEncoding.EncodeToString(blockSyncSummary.Bytes()))
27-
28-
parser := NewBlockSyncSummaryParser()
29-
called := false
30-
acceptImplTest := func(Syncable) (block.StateSyncMode, error) {
31-
called = true
32-
return block.StateSyncSkipped, nil
33-
}
34-
s, err := parser.Parse(blockSyncSummary.Bytes(), acceptImplTest)
35-
require.NoError(t, err)
36-
require.Equal(t, blockSyncSummary.GetBlockHash(), s.GetBlockHash())
37-
require.Equal(t, blockSyncSummary.Height(), s.Height())
38-
require.Equal(t, blockSyncSummary.GetBlockRoot(), s.GetBlockRoot())
39-
require.Equal(t, blockSyncSummary.Bytes(), s.Bytes())
40-
41-
mode, err := s.Accept(context.TODO())
42-
require.NoError(t, err)
43-
require.Equal(t, block.StateSyncSkipped, mode)
44-
require.True(t, called)
4526
}

0 commit comments

Comments
 (0)