Merge pull request #55 from ava-labs/ping-pong

Added ping pong messages
This commit is contained in:
Stephen Buttolph 2020-06-16 23:27:19 -04:00 committed by GitHub
commit be5957875c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 236 additions and 23 deletions

View File

@ -50,6 +50,122 @@ func (c *Config) init() error {
// Hard coded genesis constants
var (
EverestConfig = Config{
MintAddresses: []string{
"95YUFjhDG892VePMzpwKF9JzewGKvGRi3",
},
FundedAddresses: []string{
"9uKvvA7E35QCwLvAaohXTCfFejbf3Rv17",
"JLrYNMYXANGj43BfWXBxMMAEenUBp1Sbn",
"7TUTzwrU6nbZtWHjTHEpdneUvjKBxb3EM",
"77mPUXBdQKwQpPoX6rckCZGLGGdkuG1G6",
"4gGWdFZ4Gax1B466YKXyKRRpWLb42Afdt",
"CKTkzAPsRxCreyiDTnjGxLmjMarxF28fi",
"4ABm9gFHVtsNdcKSd1xsacFkGneSgzpaa",
"DpL8PTsrjtLzv5J8LL3D2A6YcnCTqrNH9",
"ZdhZv6oZrmXLyFDy6ovXAu6VxmbTsT2h",
"6cesTteH62Y5mLoDBUASaBvCXuL2AthL",
},
StakerIDs: []string{
"LQwRLm4cbJ7T2kxcxp4uXCU5XD8DFrE1C",
"hArafGhY2HFTbwaaVh1CSCUCUCiJ2Vfb",
"2m38qc95mhHXtrhjyGbe7r2NhniqHHJRB",
"4QBwET5o8kUhvt9xArhir4d3R25CtmZho",
"NpagUxt6KQiwPch9Sd4osv8kD1TZnkjdk",
},
EVMBytes: []byte{
0x7b, 0x22, 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x67,
0x22, 0x3a, 0x7b, 0x22, 0x63, 0x68, 0x61, 0x69,
0x6e, 0x49, 0x64, 0x22, 0x3a, 0x34, 0x33, 0x31,
0x31, 0x30, 0x2c, 0x22, 0x68, 0x6f, 0x6d, 0x65,
0x73, 0x74, 0x65, 0x61, 0x64, 0x42, 0x6c, 0x6f,
0x63, 0x6b, 0x22, 0x3a, 0x30, 0x2c, 0x22, 0x64,
0x61, 0x6f, 0x46, 0x6f, 0x72, 0x6b, 0x42, 0x6c,
0x6f, 0x63, 0x6b, 0x22, 0x3a, 0x30, 0x2c, 0x22,
0x64, 0x61, 0x6f, 0x46, 0x6f, 0x72, 0x6b, 0x53,
0x75, 0x70, 0x70, 0x6f, 0x72, 0x74, 0x22, 0x3a,
0x74, 0x72, 0x75, 0x65, 0x2c, 0x22, 0x65, 0x69,
0x70, 0x31, 0x35, 0x30, 0x42, 0x6c, 0x6f, 0x63,
0x6b, 0x22, 0x3a, 0x30, 0x2c, 0x22, 0x65, 0x69,
0x70, 0x31, 0x35, 0x30, 0x48, 0x61, 0x73, 0x68,
0x22, 0x3a, 0x22, 0x30, 0x78, 0x32, 0x30, 0x38,
0x36, 0x37, 0x39, 0x39, 0x61, 0x65, 0x65, 0x62,
0x65, 0x61, 0x65, 0x31, 0x33, 0x35, 0x63, 0x32,
0x34, 0x36, 0x63, 0x36, 0x35, 0x30, 0x32, 0x31,
0x63, 0x38, 0x32, 0x62, 0x34, 0x65, 0x31, 0x35,
0x61, 0x32, 0x63, 0x34, 0x35, 0x31, 0x33, 0x34,
0x30, 0x39, 0x39, 0x33, 0x61, 0x61, 0x63, 0x66,
0x64, 0x32, 0x37, 0x35, 0x31, 0x38, 0x38, 0x36,
0x35, 0x31, 0x34, 0x66, 0x30, 0x22, 0x2c, 0x22,
0x65, 0x69, 0x70, 0x31, 0x35, 0x35, 0x42, 0x6c,
0x6f, 0x63, 0x6b, 0x22, 0x3a, 0x30, 0x2c, 0x22,
0x65, 0x69, 0x70, 0x31, 0x35, 0x38, 0x42, 0x6c,
0x6f, 0x63, 0x6b, 0x22, 0x3a, 0x30, 0x2c, 0x22,
0x62, 0x79, 0x7a, 0x61, 0x6e, 0x74, 0x69, 0x75,
0x6d, 0x42, 0x6c, 0x6f, 0x63, 0x6b, 0x22, 0x3a,
0x30, 0x2c, 0x22, 0x63, 0x6f, 0x6e, 0x73, 0x74,
0x61, 0x6e, 0x74, 0x69, 0x6e, 0x6f, 0x70, 0x6c,
0x65, 0x42, 0x6c, 0x6f, 0x63, 0x6b, 0x22, 0x3a,
0x30, 0x2c, 0x22, 0x70, 0x65, 0x74, 0x65, 0x72,
0x73, 0x62, 0x75, 0x72, 0x67, 0x42, 0x6c, 0x6f,
0x63, 0x6b, 0x22, 0x3a, 0x30, 0x7d, 0x2c, 0x22,
0x6e, 0x6f, 0x6e, 0x63, 0x65, 0x22, 0x3a, 0x22,
0x30, 0x78, 0x30, 0x22, 0x2c, 0x22, 0x74, 0x69,
0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x22,
0x3a, 0x22, 0x30, 0x78, 0x30, 0x22, 0x2c, 0x22,
0x65, 0x78, 0x74, 0x72, 0x61, 0x44, 0x61, 0x74,
0x61, 0x22, 0x3a, 0x22, 0x30, 0x78, 0x30, 0x30,
0x22, 0x2c, 0x22, 0x67, 0x61, 0x73, 0x4c, 0x69,
0x6d, 0x69, 0x74, 0x22, 0x3a, 0x22, 0x30, 0x78,
0x35, 0x66, 0x35, 0x65, 0x31, 0x30, 0x30, 0x22,
0x2c, 0x22, 0x64, 0x69, 0x66, 0x66, 0x69, 0x63,
0x75, 0x6c, 0x74, 0x79, 0x22, 0x3a, 0x22, 0x30,
0x78, 0x30, 0x22, 0x2c, 0x22, 0x6d, 0x69, 0x78,
0x48, 0x61, 0x73, 0x68, 0x22, 0x3a, 0x22, 0x30,
0x78, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
0x30, 0x22, 0x2c, 0x22, 0x63, 0x6f, 0x69, 0x6e,
0x62, 0x61, 0x73, 0x65, 0x22, 0x3a, 0x22, 0x30,
0x78, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
0x30, 0x22, 0x2c, 0x22, 0x61, 0x6c, 0x6c, 0x6f,
0x63, 0x22, 0x3a, 0x7b, 0x22, 0x35, 0x37, 0x32,
0x66, 0x34, 0x64, 0x38, 0x30, 0x66, 0x31, 0x30,
0x66, 0x36, 0x36, 0x33, 0x62, 0x35, 0x30, 0x34,
0x39, 0x66, 0x37, 0x38, 0x39, 0x35, 0x34, 0x36,
0x66, 0x32, 0x35, 0x66, 0x37, 0x30, 0x62, 0x62,
0x36, 0x32, 0x61, 0x37, 0x66, 0x22, 0x3a, 0x7b,
0x22, 0x62, 0x61, 0x6c, 0x61, 0x6e, 0x63, 0x65,
0x22, 0x3a, 0x22, 0x30, 0x78, 0x33, 0x33, 0x62,
0x32, 0x65, 0x33, 0x63, 0x39, 0x66, 0x64, 0x30,
0x38, 0x30, 0x34, 0x30, 0x30, 0x30, 0x30, 0x30,
0x30, 0x30, 0x30, 0x30, 0x22, 0x7d, 0x7d, 0x2c,
0x22, 0x6e, 0x75, 0x6d, 0x62, 0x65, 0x72, 0x22,
0x3a, 0x22, 0x30, 0x78, 0x30, 0x22, 0x2c, 0x22,
0x67, 0x61, 0x73, 0x55, 0x73, 0x65, 0x64, 0x22,
0x3a, 0x22, 0x30, 0x78, 0x30, 0x22, 0x2c, 0x22,
0x70, 0x61, 0x72, 0x65, 0x6e, 0x74, 0x48, 0x61,
0x73, 0x68, 0x22, 0x3a, 0x22, 0x30, 0x78, 0x30,
0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x22,
0x7d,
},
}
DenaliConfig = Config{
MintAddresses: []string{
"95YUFjhDG892VePMzpwKF9JzewGKvGRi3",
@ -393,6 +509,8 @@ var (
// GetConfig ...
func GetConfig(networkID uint32) *Config {
switch networkID {
case EverestID:
return &EverestConfig
case DenaliID:
return &DenaliConfig
case CascadeID:

View File

@ -23,8 +23,11 @@ func TestNetworkName(t *testing.T) {
if name := NetworkName(DenaliID); name != DenaliName {
t.Fatalf("NetworkID was incorrectly named. Result: %s ; Expected: %s", name, DenaliName)
}
if name := NetworkName(TestnetID); name != DenaliName {
t.Fatalf("NetworkID was incorrectly named. Result: %s ; Expected: %s", name, DenaliName)
if name := NetworkName(EverestID); name != EverestName {
t.Fatalf("NetworkID was incorrectly named. Result: %s ; Expected: %s", name, EverestName)
}
if name := NetworkName(TestnetID); name != EverestName {
t.Fatalf("NetworkID was incorrectly named. Result: %s ; Expected: %s", name, EverestName)
}
if name := NetworkName(4294967295); name != "network-4294967295" {
t.Fatalf("NetworkID was incorrectly named. Result: %s ; Expected: %s", name, "network-4294967295")

View File

@ -16,13 +16,15 @@ var (
MainnetID uint32 = 1
CascadeID uint32 = 2
DenaliID uint32 = 3
EverestID uint32 = 4
TestnetID uint32 = 3
TestnetID uint32 = 4
LocalID uint32 = 12345
MainnetName = "mainnet"
CascadeName = "cascade"
DenaliName = "denali"
EverestName = "everest"
TestnetName = "testnet"
LocalName = "local"
@ -31,6 +33,7 @@ var (
MainnetID: MainnetName,
CascadeID: CascadeName,
DenaliID: DenaliName,
EverestID: EverestName,
LocalID: LocalName,
}
@ -38,6 +41,7 @@ var (
MainnetName: MainnetID,
CascadeName: CascadeID,
DenaliName: DenaliID,
EverestName: EverestID,
TestnetName: TestnetID,
LocalName: LocalID,

View File

@ -30,7 +30,7 @@ import (
)
const (
dbVersion = "v0.5.0"
dbVersion = "v0.6.0"
)
// Results of parsing the CLI

View File

@ -33,6 +33,12 @@ func (m Builder) PeerList(ipDescs []utils.IPDesc) (Msg, error) {
return m.Pack(PeerList, map[Field]interface{}{Peers: ipDescs})
}
// Ping message
func (m Builder) Ping() (Msg, error) { return m.Pack(Ping, nil) }
// Pong message
func (m Builder) Pong() (Msg, error) { return m.Pack(Pong, nil) }
// GetAcceptedFrontier message
func (m Builder) GetAcceptedFrontier(chainID ids.ID, requestID uint32) (Msg, error) {
return m.Pack(GetAcceptedFrontier, map[Field]interface{}{

View File

@ -132,6 +132,10 @@ func (op Op) String() string {
return "get_peerlist"
case PeerList:
return "peerlist"
case Ping:
return "ping"
case Pong:
return "pong"
case GetAcceptedFrontier:
return "get_accepted_frontier"
case AcceptedFrontier:
@ -166,22 +170,21 @@ const (
Version
GetPeerList
PeerList
Ping
Pong
// Bootstrapping:
GetAcceptedFrontier
AcceptedFrontier
GetAccepted
Accepted
GetAncestors
MultiPut
// Consensus:
Get
Put
PushQuery
PullQuery
Chits
// Bootstrapping:
// TODO: Move GetAncestors and MultiPut with the rest of the bootstrapping
// commands when we do non-backwards compatible upgrade
GetAncestors
MultiPut
)
// Defines the messages that can be sent/received with this network
@ -192,6 +195,8 @@ var (
Version: {NetworkID, NodeID, MyTime, IP, VersionStr},
GetPeerList: {},
PeerList: {Peers},
Ping: {},
Pong: {},
// Bootstrapping:
GetAcceptedFrontier: {ChainID, RequestID},
AcceptedFrontier: {ChainID, RequestID, ContainerIDs},

View File

@ -54,6 +54,7 @@ type metrics struct {
getVersion, version,
getPeerlist, peerlist,
ping, pong,
getAcceptedFrontier, acceptedFrontier,
getAccepted, accepted,
get, getAncestors, put, multiPut,
@ -78,6 +79,8 @@ func (m *metrics) initialize(registerer prometheus.Registerer) error {
errs.Add(m.version.initialize(Version, registerer))
errs.Add(m.getPeerlist.initialize(GetPeerList, registerer))
errs.Add(m.peerlist.initialize(PeerList, registerer))
errs.Add(m.ping.initialize(Ping, registerer))
errs.Add(m.pong.initialize(Pong, registerer))
errs.Add(m.getAcceptedFrontier.initialize(GetAcceptedFrontier, registerer))
errs.Add(m.acceptedFrontier.initialize(AcceptedFrontier, registerer))
errs.Add(m.getAccepted.initialize(GetAccepted, registerer))
@ -103,6 +106,10 @@ func (m *metrics) message(msgType Op) *messageMetrics {
return &m.getPeerlist
case PeerList:
return &m.peerlist
case Ping:
return &m.ping
case Pong:
return &m.pong
case GetAcceptedFrontier:
return &m.getAcceptedFrontier
case AcceptedFrontier:

View File

@ -43,6 +43,8 @@ const (
defaultGetVersionTimeout = 2 * time.Second
defaultAllowPrivateIPs = true
defaultGossipSize = 50
defaultPingPongTimeout = time.Minute
defaultPingFrequency = 3 * defaultPingPongTimeout / 4
// Request ID used when sending a Put message to gossip an accepted container
// (ie not sent in response to a Get)
@ -123,6 +125,8 @@ type network struct {
getVersionTimeout time.Duration
allowPrivateIPs bool
gossipSize int
pingPongTimeout time.Duration
pingFrequency time.Duration
executor timer.Executor
@ -184,6 +188,8 @@ func NewDefaultNetwork(
defaultGetVersionTimeout,
defaultAllowPrivateIPs,
defaultGossipSize,
defaultPingPongTimeout,
defaultPingFrequency,
)
}
@ -215,6 +221,8 @@ func NewNetwork(
getVersionTimeout time.Duration,
allowPrivateIPs bool,
gossipSize int,
pingPongTimeout time.Duration,
pingFrequency time.Duration,
) Network {
net := &network{
log: log,
@ -243,6 +251,8 @@ func NewNetwork(
getVersionTimeout: getVersionTimeout,
allowPrivateIPs: allowPrivateIPs,
gossipSize: gossipSize,
pingPongTimeout: pingPongTimeout,
pingFrequency: pingFrequency,
disconnectedIPs: make(map[string]struct{}),
connectedIPs: make(map[string]struct{}),

View File

@ -64,6 +64,24 @@ func (p *peer) Start() {
// Initially send the version to the peer
go p.Version()
go p.requestVersion()
go p.sendPings()
}
func (p *peer) sendPings() {
t := time.NewTicker(p.net.pingFrequency)
defer t.Stop()
for range t.C {
p.net.stateLock.Lock()
closed := p.closed
p.net.stateLock.Unlock()
if closed {
return
}
p.Ping()
}
}
// request the version from the peer until we get the version from them
@ -80,6 +98,7 @@ func (p *peer) requestVersion() {
if connected || closed {
return
}
p.GetVersion()
}
}
@ -88,6 +107,11 @@ func (p *peer) requestVersion() {
func (p *peer) ReadMessages() {
defer p.Close()
if err := p.conn.SetReadDeadline(p.net.clock.Time().Add(p.net.pingPongTimeout)); err != nil {
p.net.log.Verbo("error on setting the connection read timeout %s", err)
return
}
pendingBuffer := wrappers.Packer{}
readBuffer := make([]byte, 1<<10)
for {
@ -218,7 +242,15 @@ func (p *peer) send(msg Msg) bool {
// assumes the stateLock is not held
func (p *peer) handle(msg Msg) {
p.net.heartbeat()
atomic.StoreInt64(&p.lastReceived, p.net.clock.Time().Unix())
currentTime := p.net.clock.Time()
atomic.StoreInt64(&p.lastReceived, currentTime.Unix())
if err := p.conn.SetReadDeadline(currentTime.Add(p.net.pingPongTimeout)); err != nil {
p.net.log.Verbo("error on setting the connection read timeout %s, closing the connection", err)
p.Close()
return
}
op := msg.Op()
msgMetrics := p.net.message(op)
@ -235,6 +267,12 @@ func (p *peer) handle(msg Msg) {
case GetVersion:
p.getVersion(msg)
return
case Ping:
p.ping(msg)
return
case Pong:
p.pong(msg)
return
}
if !p.connected {
p.net.log.Debug("dropping message from %s because the connection hasn't been established yet", p.id)
@ -318,6 +356,12 @@ func (p *peer) GetPeerList() {
p.Send(msg)
}
// assumes the stateLock is not held
func (p *peer) SendPeerList() {
ips := p.net.validatorIPs()
p.PeerList(ips)
}
// assumes the stateLock is not held
func (p *peer) PeerList(peers []utils.IPDesc) {
msg, err := p.net.b.PeerList(peers)
@ -326,7 +370,28 @@ func (p *peer) PeerList(peers []utils.IPDesc) {
return
}
p.Send(msg)
return
}
// assumes the stateLock is not held
func (p *peer) Ping() {
msg, err := p.net.b.Ping()
p.net.log.AssertNoError(err)
if p.Send(msg) {
p.net.ping.numSent.Inc()
} else {
p.net.ping.numFailed.Inc()
}
}
// assumes the stateLock is not held
func (p *peer) Pong() {
msg, err := p.net.b.Pong()
p.net.log.AssertNoError(err)
if p.Send(msg) {
p.net.pong.numSent.Inc()
} else {
p.net.pong.numFailed.Inc()
}
}
// assumes the stateLock is not held
@ -458,17 +523,6 @@ func (p *peer) version(msg Msg) {
p.net.connected(p)
}
// assumes the stateLock is not held
func (p *peer) SendPeerList() {
ips := p.net.validatorIPs()
reply, err := p.net.b.PeerList(ips)
if err != nil {
p.net.log.Warn("failed to send PeerList message due to %s", err)
return
}
p.Send(reply)
}
// assumes the stateLock is not held
func (p *peer) getPeerList(_ Msg) { p.SendPeerList() }
@ -488,6 +542,12 @@ func (p *peer) peerList(msg Msg) {
p.net.stateLock.Unlock()
}
// assumes the stateLock is not held
func (p *peer) ping(_ Msg) { p.Pong() }
// assumes the stateLock is not held
func (p *peer) pong(_ Msg) {}
// assumes the stateLock is not held
func (p *peer) getAcceptedFrontier(msg Msg) {
chainID, err := ids.ToID(msg.Get(ChainID).([]byte))

View File

@ -56,7 +56,7 @@ var (
genesisHashKey = []byte("genesisID")
// Version is the version of this code
Version = version.NewDefaultVersion("avalanche", 0, 5, 5)
Version = version.NewDefaultVersion("avalanche", 0, 6, 0)
versionParser = version.NewDefaultParser()
)