Skip to content

Commit a129a81

Browse files
committed
capability: add ArchivalNode, fix #3776
This will be used to differentiate full history nodes from state only. Currently it's just a definition, we can not use it safely on current networks because nodes will refuse this Version, so actual code using the attribute will be added in newer versions. Signed-off-by: Roman Khimov <roman@nspcc.ru>
1 parent a42f95e commit a129a81

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

pkg/network/capability/capability.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ type Capability struct {
6464
func (c *Capability) DecodeBinary(br *io.BinReader) {
6565
c.Type = Type(br.ReadB())
6666
switch c.Type {
67+
case ArchivalNode:
68+
c.Data = &Archival{}
6769
case FullNode:
6870
c.Data = &Node{}
6971
case TCPServer, WSServer:
@@ -115,6 +117,22 @@ func (s *Server) EncodeBinary(bw *io.BinWriter) {
115117
bw.WriteU16LE(s.Port)
116118
}
117119

120+
// Archival represents an archival node that stores all blocks.
121+
type Archival struct{}
122+
123+
// DecodeBinary implements io.Serializable.
124+
func (a *Archival) DecodeBinary(br *io.BinReader) {
125+
var zero = br.ReadB() // Zero-length byte array as per Unknown.
126+
if zero != 0 {
127+
br.Err = errors.New("archival capability with non-zero data")
128+
}
129+
}
130+
131+
// EncodeBinary implements io.Serializable.
132+
func (a *Archival) EncodeBinary(bw *io.BinWriter) {
133+
bw.WriteB(0)
134+
}
135+
118136
// Unknown represents an unknown capability with some data. Other nodes can
119137
// decode it even if they can't interpret it. This is not expected to be used
120138
// for sending data directly (proper new types should be used), but it allows

pkg/network/capability/capability_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"testing"
55

66
"github.com/nspcc-dev/neo-go/internal/testserdes"
7+
"github.com/stretchr/testify/require"
78
)
89

910
func TestUnknownEncodeDecode(t *testing.T) {
@@ -13,3 +14,14 @@ func TestUnknownEncodeDecode(t *testing.T) {
1314
)
1415
testserdes.EncodeDecodeBinary(t, &u, &ud)
1516
}
17+
18+
func TestArchivalEncodeDecode(t *testing.T) {
19+
var (
20+
a = Archival{}
21+
ad Archival
22+
)
23+
testserdes.EncodeDecodeBinary(t, &a, &ad)
24+
25+
var bad = []byte{0x02, 0x55, 0xaa} // Two-byte var-encoded string.
26+
require.Error(t, testserdes.DecodeBinary(bad, &ad))
27+
}

pkg/network/capability/type.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ const (
1010
WSServer Type = 0x02
1111
// FullNode represents a node that has complete current state.
1212
FullNode Type = 0x10
13+
// ArchivalNode represents a node that stores full block history.
14+
// These nodes can be used for P2P synchronization from genesis
15+
// (FullNode can cut the tail and may not respond to requests for
16+
// old (wrt MaxTraceableBlocks) blocks).
17+
ArchivalNode Type = 0x11
1318

1419
// 0xf0-0xff are reserved for private experiments.
1520
)

pkg/network/payload/version_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ func TestVersionEncodeDecode(t *testing.T) {
2929
Port: wsPort,
3030
},
3131
},
32+
{
33+
Type: capability.ArchivalNode,
34+
Data: &capability.Archival{},
35+
},
3236
{
3337
Type: 0xff,
3438
Data: &capability.Unknown{},

0 commit comments

Comments
 (0)