Skip to content

Commit 1db2458

Browse files
committed
capability: allow to decode unknown capabilities
It's more symmetric than the C# counterpart, but the essence is the same: we can accept capabilities we can't interpret. Refs. neo-project/neo#3639 Signed-off-by: Roman Khimov <roman@nspcc.ru>
1 parent 9540608 commit 1db2458

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed

pkg/network/capability/capability.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,13 @@ import (
66
"github.com/nspcc-dev/neo-go/pkg/io"
77
)
88

9-
// MaxCapabilities is the maximum number of capabilities per payload.
10-
const MaxCapabilities = 32
9+
const (
10+
// MaxCapabilities is the maximum number of capabilities per payload.
11+
MaxCapabilities = 32
12+
13+
// MaxDataSize is the maximum size of capability payload.
14+
MaxDataSize = 1024
15+
)
1116

1217
// Capabilities is a list of Capability.
1318
type Capabilities []Capability
@@ -64,8 +69,7 @@ func (c *Capability) DecodeBinary(br *io.BinReader) {
6469
case TCPServer, WSServer:
6570
c.Data = &Server{}
6671
default:
67-
br.Err = errors.New("unknown node capability type")
68-
return
72+
c.Data = &Unknown{}
6973
}
7074
c.Data.DecodeBinary(br)
7175
}
@@ -110,3 +114,19 @@ func (s *Server) DecodeBinary(br *io.BinReader) {
110114
func (s *Server) EncodeBinary(bw *io.BinWriter) {
111115
bw.WriteU16LE(s.Port)
112116
}
117+
118+
// Unknown represents an unknown capability with some data. Other nodes can
119+
// decode it even if they can't interpret it. This is not expected to be used
120+
// for sending data directly (proper new types should be used), but it allows
121+
// for easier protocol extensibility (old nodes won't reject new capabilities).
122+
type Unknown []byte
123+
124+
// DecodeBinary implements io.Serializable.
125+
func (u *Unknown) DecodeBinary(br *io.BinReader) {
126+
*u = br.ReadVarBytes()
127+
}
128+
129+
// EncodeBinary implements io.Serializable.
130+
func (u *Unknown) EncodeBinary(bw *io.BinWriter) {
131+
bw.WriteVarBytes(*u)
132+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package capability
2+
3+
import (
4+
"testing"
5+
6+
"github.com/nspcc-dev/neo-go/internal/testserdes"
7+
)
8+
9+
func TestUnknownEncodeDecode(t *testing.T) {
10+
var (
11+
u = Unknown{0x55, 0xaa}
12+
ud Unknown
13+
)
14+
testserdes.EncodeDecodeBinary(t, &u, &ud)
15+
}

pkg/network/payload/version_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,20 @@ func TestVersionEncodeDecode(t *testing.T) {
2929
Port: wsPort,
3030
},
3131
},
32+
{
33+
Type: 0xff,
34+
Data: &capability.Unknown{},
35+
},
3236
{
3337
Type: capability.FullNode,
3438
Data: &capability.Node{
3539
StartHeight: height,
3640
},
3741
},
42+
{
43+
Type: 0xf0,
44+
Data: &capability.Unknown{0x55, 0xaa},
45+
},
3846
}
3947

4048
version := NewVersion(magic, id, useragent, capabilities)

0 commit comments

Comments
 (0)