gecko/network/commands.go

201 lines
4.8 KiB
Go
Raw Normal View History

2020-03-10 12:20:34 -07:00
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
2020-05-17 20:47:43 -07:00
package network
2020-03-10 12:20:34 -07:00
import (
"github.com/ava-labs/gecko/utils/wrappers"
)
// Field that may be packed into a message
type Field uint32
// Fields that may be packed. These values are not sent over the wire.
const (
VersionStr Field = iota // Used in handshake
NetworkID // Used in handshake
2020-05-23 23:00:38 -07:00
NodeID // Used in handshake
2020-03-10 12:20:34 -07:00
MyTime // Used in handshake
2020-04-18 17:34:19 -07:00
IP // Used in handshake
2020-03-10 12:20:34 -07:00
Peers // Used in handshake
ChainID // Used for dispatching
RequestID // Used for all messages
ContainerID // Used for querying
ContainerBytes // Used for gossiping
ContainerIDs // Used for querying
)
// Packer returns the packer function that can be used to pack this field.
func (f Field) Packer() func(*wrappers.Packer, interface{}) {
switch f {
case VersionStr:
return wrappers.TryPackStr
case NetworkID:
return wrappers.TryPackInt
2020-05-23 23:00:38 -07:00
case NodeID:
return wrappers.TryPackInt
2020-03-10 12:20:34 -07:00
case MyTime:
return wrappers.TryPackLong
2020-04-18 17:34:19 -07:00
case IP:
return wrappers.TryPackIP
2020-03-10 12:20:34 -07:00
case Peers:
return wrappers.TryPackIPList
case ChainID: // TODO: This will be shortened to use a modified varint spec
return wrappers.TryPackHash
case RequestID:
return wrappers.TryPackInt
case ContainerID:
return wrappers.TryPackHash
case ContainerBytes:
return wrappers.TryPackBytes
case ContainerIDs:
return wrappers.TryPackHashes
default:
return nil
}
}
// Unpacker returns the unpacker function that can be used to unpack this field.
func (f Field) Unpacker() func(*wrappers.Packer) interface{} {
switch f {
case VersionStr:
return wrappers.TryUnpackStr
case NetworkID:
return wrappers.TryUnpackInt
2020-05-23 23:00:38 -07:00
case NodeID:
return wrappers.TryUnpackInt
2020-03-10 12:20:34 -07:00
case MyTime:
return wrappers.TryUnpackLong
2020-04-18 17:34:19 -07:00
case IP:
return wrappers.TryUnpackIP
2020-03-10 12:20:34 -07:00
case Peers:
return wrappers.TryUnpackIPList
case ChainID: // TODO: This will be shortened to use a modified varint spec
return wrappers.TryUnpackHash
case RequestID:
return wrappers.TryUnpackInt
case ContainerID:
return wrappers.TryUnpackHash
case ContainerBytes:
return wrappers.TryUnpackBytes
case ContainerIDs:
return wrappers.TryUnpackHashes
default:
return nil
}
}
func (f Field) String() string {
switch f {
case VersionStr:
return "VersionStr"
case NetworkID:
return "NetworkID"
2020-05-23 23:00:38 -07:00
case NodeID:
return "NodeID"
2020-03-10 12:20:34 -07:00
case MyTime:
return "MyTime"
2020-04-18 17:34:19 -07:00
case IP:
return "IP"
2020-03-10 12:20:34 -07:00
case Peers:
return "Peers"
case ChainID:
return "ChainID"
case ContainerID:
return "ContainerID"
case ContainerBytes:
return "Container Bytes"
case ContainerIDs:
return "Container IDs"
default:
return "Unknown Field"
}
}
2020-05-25 13:02:03 -07:00
// Op is an opcode
type Op byte
func (op Op) String() string {
switch op {
case GetVersion:
return "get_version"
case Version:
return "version"
case GetPeerList:
return "get_peerlist"
case PeerList:
return "peerlist"
case GetAcceptedFrontier:
return "get_accepted_frontier"
case AcceptedFrontier:
return "accepted_frontier"
case GetAccepted:
return "get_accepted"
case Accepted:
return "accepted"
case Get:
return "get"
case GetAncestors:
return "get_ancestors"
2020-05-25 13:02:03 -07:00
case Put:
return "put"
case PutAncestor:
return "put_ancestor"
2020-05-25 13:02:03 -07:00
case PushQuery:
return "push_query"
case PullQuery:
return "pull_query"
case Chits:
return "chits"
default:
return "Unknown Op"
}
}
2020-03-10 12:20:34 -07:00
// Public commands that may be sent between stakers
const (
// Handshake:
2020-05-25 13:02:03 -07:00
GetVersion Op = iota
2020-03-10 12:20:34 -07:00
Version
GetPeerList
PeerList
// Bootstrapping:
GetAcceptedFrontier
AcceptedFrontier
GetAccepted
Accepted
// Consensus:
Get
Put
PushQuery
PullQuery
Chits
// Bootstrapping
GetAncestors
PutAncestor
2020-03-10 12:20:34 -07:00
)
// Defines the messages that can be sent/received with this network
var (
2020-05-25 13:02:03 -07:00
Messages = map[Op][]Field{
2020-03-10 12:20:34 -07:00
// Handshake:
GetVersion: []Field{},
2020-05-23 23:00:38 -07:00
Version: []Field{NetworkID, NodeID, MyTime, IP, VersionStr},
2020-03-10 12:20:34 -07:00
GetPeerList: []Field{},
PeerList: []Field{Peers},
// Bootstrapping:
GetAcceptedFrontier: []Field{ChainID, RequestID},
AcceptedFrontier: []Field{ChainID, RequestID, ContainerIDs},
GetAccepted: []Field{ChainID, RequestID, ContainerIDs},
Accepted: []Field{ChainID, RequestID, ContainerIDs},
GetAncestors: []Field{ChainID, RequestID, ContainerID},
PutAncestor: []Field{ChainID, RequestID, ContainerID, ContainerBytes},
2020-03-10 12:20:34 -07:00
// Consensus:
Get: []Field{ChainID, RequestID, ContainerID},
Put: []Field{ChainID, RequestID, ContainerID, ContainerBytes},
PushQuery: []Field{ChainID, RequestID, ContainerID, ContainerBytes},
PullQuery: []Field{ChainID, RequestID, ContainerID},
Chits: []Field{ChainID, RequestID, ContainerIDs},
}
)