2022-10-08 14:15:44 +00:00
|
|
|
/*
|
|
|
|
Copyright 2022 Erigon-Lightclient contributors
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-09-30 17:07:13 +00:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
2022-10-10 18:14:07 +00:00
|
|
|
"github.com/ledgerwatch/erigon/cmd/lightclient/cltypes"
|
2022-10-06 12:34:39 +00:00
|
|
|
"github.com/ledgerwatch/erigon/cmd/lightclient/sentinel/communication"
|
2022-09-30 17:07:13 +00:00
|
|
|
)
|
|
|
|
|
2022-10-25 17:30:11 +00:00
|
|
|
// Type safe handlers which all have access to the original stream & decompressed data.
|
|
|
|
// Since packets are just structs, they can be resent with no issue
|
|
|
|
|
|
|
|
func (c *ConsensusHandlers) pingHandler(ctx *communication.StreamContext, _ *communication.EmptyPacket) error {
|
|
|
|
return ctx.Codec.WritePacket(&cltypes.Ping{
|
|
|
|
Id: c.metadata.SeqNumber,
|
|
|
|
}, SuccessfulResponsePrefix)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ConsensusHandlers) goodbyeHandler(ctx *communication.StreamContext, _ *communication.EmptyPacket) error {
|
|
|
|
// From the spec, these are the valid goodbye numbers. Start with just
|
|
|
|
// sending 1, but we should think about when the others need to be sent.
|
|
|
|
// 1: Client shut down.
|
|
|
|
// 2: Irrelevant network.
|
|
|
|
// 3: Fault/error.
|
|
|
|
return ctx.Codec.WritePacket(&cltypes.Ping{
|
|
|
|
Id: 1,
|
|
|
|
}, SuccessfulResponsePrefix)
|
2022-09-30 17:07:13 +00:00
|
|
|
}
|
|
|
|
|
2022-10-09 18:28:49 +00:00
|
|
|
func (c *ConsensusHandlers) metadataV1Handler(ctx *communication.StreamContext, _ *communication.EmptyPacket) error {
|
2022-10-10 18:14:07 +00:00
|
|
|
return ctx.Codec.WritePacket(&cltypes.MetadataV1{
|
2022-10-09 18:28:49 +00:00
|
|
|
SeqNumber: c.metadata.SeqNumber,
|
|
|
|
Attnets: c.metadata.Attnets,
|
2022-10-25 17:30:11 +00:00
|
|
|
}, SuccessfulResponsePrefix)
|
2022-10-09 18:28:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ConsensusHandlers) metadataV2Handler(ctx *communication.StreamContext, _ *communication.EmptyPacket) error {
|
2022-10-25 17:30:11 +00:00
|
|
|
return ctx.Codec.WritePacket(c.metadata, SuccessfulResponsePrefix)
|
2022-09-30 17:07:13 +00:00
|
|
|
}
|
|
|
|
|
2022-10-07 16:40:18 +00:00
|
|
|
// TODO: Actually respond with proper status
|
2022-10-25 17:30:11 +00:00
|
|
|
func (c *ConsensusHandlers) statusHandler(ctx *communication.StreamContext, dat *cltypes.Status) error {
|
|
|
|
return ctx.Codec.WritePacket(dat, SuccessfulResponsePrefix)
|
2022-09-30 17:07:13 +00:00
|
|
|
}
|