gecko/network/commands.go

149 lines
3.7 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
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
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
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"
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"
}
}
// Public commands that may be sent between stakers
const (
// Handshake:
2020-05-17 20:47:43 -07:00
GetVersion uint8 = iota
2020-03-10 12:20:34 -07:00
Version
GetPeerList
PeerList
// Bootstrapping:
GetAcceptedFrontier
AcceptedFrontier
GetAccepted
Accepted
// Consensus:
Get
Put
PushQuery
PullQuery
Chits
)
// Defines the messages that can be sent/received with this network
var (
2020-05-17 20:47:43 -07:00
Messages = map[uint8][]Field{
2020-03-10 12:20:34 -07:00
// Handshake:
GetVersion: []Field{},
2020-04-18 17:34:19 -07:00
Version: []Field{NetworkID, 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},
// 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},
}
)