-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
protocol: add capabilities to version payload
closes #871
- Loading branch information
1 parent
e6f617a
commit 902cfd5
Showing
14 changed files
with
267 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package capability | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/nspcc-dev/neo-go/pkg/io" | ||
) | ||
|
||
// MaxCapabilities is the maximum number of capabilities per payload | ||
const MaxCapabilities = 32 | ||
|
||
// Capability describes network service available for node | ||
type Capability struct { | ||
Type Type | ||
Data io.Serializable | ||
} | ||
|
||
// DecodeBinary implements Serializable interface. | ||
func (c *Capability) DecodeBinary(br *io.BinReader) { | ||
c.Type = Type(br.ReadB()) | ||
switch c.Type { | ||
case FullNode: | ||
c.Data = &Node{} | ||
case TCPServer, WSServer: | ||
c.Data = &Server{} | ||
default: | ||
br.Err = errors.New("unknown node capability type") | ||
} | ||
c.Data.DecodeBinary(br) | ||
} | ||
|
||
// EncodeBinary implements Serializable interface. | ||
func (c *Capability) EncodeBinary(bw *io.BinWriter) { | ||
bw.WriteB(byte(c.Type)) | ||
if c.Data == nil { | ||
bw.Err = errors.New("capability has no data") | ||
return | ||
} | ||
c.Data.EncodeBinary(bw) | ||
} | ||
|
||
// Node represents full node capability with start height | ||
type Node struct { | ||
StartHeight uint32 | ||
} | ||
|
||
// DecodeBinary implements Serializable interface. | ||
func (n *Node) DecodeBinary(br *io.BinReader) { | ||
n.StartHeight = br.ReadU32LE() | ||
} | ||
|
||
// EncodeBinary implements Serializable interface. | ||
func (n *Node) EncodeBinary(bw *io.BinWriter) { | ||
bw.WriteU32LE(n.StartHeight) | ||
} | ||
|
||
// Server represents TCP or WS server capability with port | ||
type Server struct { | ||
// Port is the port this server is listening on | ||
Port uint16 | ||
} | ||
|
||
// DecodeBinary implements Serializable interface. | ||
func (s *Server) DecodeBinary(br *io.BinReader) { | ||
s.Port = br.ReadU16LE() | ||
} | ||
|
||
// EncodeBinary implements Serializable interface. | ||
func (s *Server) EncodeBinary(bw *io.BinWriter) { | ||
bw.WriteU16LE(s.Port) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package capability | ||
|
||
// Type represents node capability type | ||
type Type byte | ||
|
||
const ( | ||
// TCPServer represents TCP node capability type | ||
TCPServer Type = 0x01 | ||
// WSServer represents WebSocket node capability type | ||
WSServer Type = 0x02 | ||
// FullNode represents full node capability type | ||
FullNode Type = 0x10 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.