Add Flag for SSZ Encoding (#3256)

* add flag and enum

* change help message

* linter

* add flag

* add comment

* one more comment

* fix panic

* preston's comments

* add panic
This commit is contained in:
Nishant Das
2019-08-21 22:03:48 +05:30
committed by Preston Van Loon
parent acb20e269c
commit 79e57e8e8e
8 changed files with 28 additions and 2 deletions

View File

@@ -39,6 +39,7 @@ var appFlags = []cli.Flag{
cmd.P2PMaxPeers,
cmd.P2PPrivKey,
cmd.P2PWhitelist,
cmd.P2PEncoding,
cmd.DataDirFlag,
cmd.VerbosityFlag,
cmd.EnableTracingFlag,

View File

@@ -215,6 +215,7 @@ func (b *BeaconNode) registerP2P(ctx *cli.Context) error {
MaxPeers: ctx.GlobalUint(cmd.P2PMaxPeers.Name),
WhitelistCIDR: ctx.GlobalString(cmd.P2PWhitelist.Name),
EnableUPnP: ctx.GlobalBool(cmd.EnableUPnPFlag.Name),
Encoding: ctx.GlobalString(cmd.P2PEncoding.Name),
})
if err != nil {
return err

View File

@@ -25,6 +25,9 @@ func TestService_Broadcast(t *testing.T) {
p := &Service{
host: p1.Host,
pubsub: p1.PubSub(),
cfg: &Config{
Encoding: "ssz",
},
}
msg := &testpb.TestSimpleMessage{

View File

@@ -14,4 +14,5 @@ type Config struct {
MaxPeers uint
WhitelistCIDR string
EnableUPnP bool
Encoding string
}

View File

@@ -6,6 +6,12 @@ import (
"github.com/gogo/protobuf/proto"
)
// Defines the different encoding formats
const (
SSZ = "ssz" // SSZ is SSZ only.
SSZSnappy = "ssz-snappy" // SSZSnappy is SSZ with snappy compression.
)
// NetworkEncoding represents an encoder compatible with Ethereum 2.0 p2p.
type NetworkEncoding interface {
// Decode reads bytes from the reader and decodes it to the provided message.

View File

@@ -105,8 +105,15 @@ func (s *Service) Status() error {
// Encoding returns the configured networking encoding.
func (s *Service) Encoding() encoder.NetworkEncoding {
// TODO(3147): Return based on flag value
return &encoder.SszNetworkEncoder{}
encoding := s.cfg.Encoding
switch encoding {
case encoder.SSZ:
return &encoder.SszNetworkEncoder{}
case encoder.SSZSnappy:
return &encoder.SszNetworkEncoder{UseSnappyCompression: true}
default:
panic("Invalid Network Encoding Flag Provided")
}
}
// PubSub returns the p2p pubsub framework.

View File

@@ -94,6 +94,7 @@ var appHelpFlagGroups = []flagGroup{
cmd.P2PWhitelist,
cmd.StaticPeers,
cmd.EnableUPnPFlag,
cmd.P2PEncoding,
},
},
{

View File

@@ -107,6 +107,12 @@ var (
"would whitelist connections to peers on your local network only. The default " +
"is to accept all connections.",
}
// P2PEncoding defines the encoding format for p2p messages.
P2PEncoding = cli.StringFlag{
Name: "p2p-encoding",
Usage: "The encoding format of messages sent over the wire. The default is 0, which represents ssz",
Value: "ssz",
}
// ClearDB tells the beacon node to remove any previously stored data at the data directory.
ClearDB = cli.BoolFlag{
Name: "clear-db",