Skip to content

Commit 4720727

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 47b6793 commit 4720727

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
@@ -65,8 +70,7 @@ func (c *Capability) DecodeBinary(br *io.BinReader) {
6570
case TCPServer, WSServer:
6671
c.Data = &Server{}
6772
default:
68-
br.Err = errors.New("unknown node capability type")
69-
return
73+
c.Data = &Unknown{}
7074
}
7175
c.Data.DecodeBinary(br)
7276
}
@@ -111,3 +115,19 @@ func (s *Server) DecodeBinary(br *io.BinReader) {
111115
func (s *Server) EncodeBinary(bw *io.BinWriter) {
112116
bw.WriteU16LE(s.Port)
113117
}
118+
119+
// Unknown represents an unknown capability with some data. Other nodes can
120+
// decode it even if they can't interpret it. This is not expected to be used
121+
// for sending data directly (proper new types should be used), but it allows
122+
// for easier protocol extensibility (old nodes won't reject new capabilities).
123+
type Unknown []byte
124+
125+
// DecodeBinary implements io.Serializable.
126+
func (u *Unknown) DecodeBinary(br *io.BinReader) {
127+
*u = br.ReadVarBytes()
128+
}
129+
130+
// EncodeBinary implements io.Serializable.
131+
func (u *Unknown) EncodeBinary(bw *io.BinWriter) {
132+
bw.WriteVarBytes(*u)
133+
}
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)