Skip to content

Commit

Permalink
cmd, p2p, trie: load p2p data from genesis (#369)
Browse files Browse the repository at this point in the history
* added ProtocolId to p2p config, set it in p2p.NewService
* changed the previous global ProtocolPrefix to DefaultProtocolId which is used if the protocol ID in the config is empty
* added updating of the p2p config in makeNode to use the p2p info that was loaded from genesis
  • Loading branch information
noot committed Nov 5, 2019
1 parent 8fefce8 commit baa3bb8
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 31 deletions.
26 changes: 13 additions & 13 deletions cmd/gossamer/configcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"unicode"

"github.com/ChainSafe/gossamer/cmd/utils"
"github.com/ChainSafe/gossamer/common"
cfg "github.com/ChainSafe/gossamer/config"
"github.com/ChainSafe/gossamer/core"
"github.com/ChainSafe/gossamer/dot"
Expand All @@ -49,8 +50,6 @@ func makeNode(ctx *cli.Context) (*dot.Dot, *cfg.Config, error) {

var srvcs []services.Service

log.Info("🕸\t Starting gossamer...", "datadir", fig.Global.DataDir)

// DB: Create database dir and initialize stateDB and blockDB
dbSrv, err := polkadb.NewDbService(fig.Global.DataDir)
if err != nil {
Expand All @@ -71,10 +70,18 @@ func makeNode(ctx *cli.Context) (*dot.Dot, *cfg.Config, error) {
return nil, nil, fmt.Errorf("error loading state and runtime: %s", err)
}

// load extra genesis data from DB
gendata, err := state.Db().LoadGenesisData()
if err != nil {
return nil, nil, err
}

log.Info("🕸\t Configuring node...", "datadir", fig.Global.DataDir, "protocolID", string(gendata.ProtocolId), "bootnodes", fig.P2p.BootstrapNodes)

// TODO: BABE

// P2P
p2pSrvc, msgChan := createP2PService(fig)
p2pSrvc, msgChan := createP2PService(fig, gendata)
srvcs = append(srvcs, p2pSrvc)

// core.Service
Expand All @@ -88,14 +95,6 @@ func makeNode(ctx *cli.Context) (*dot.Dot, *cfg.Config, error) {
// RPC
rpcSrvr := startRpc(ctx, fig.Rpc, apiSrvc)

// load extra genesis data from DB
gendata, err := state.Db().LoadGenesisData()
if err != nil {
return nil, nil, err
}

log.Debug("genesisdata", "data", gendata)

return dot.NewDot(string(gendata.Name), srvcs, rpcSrvr), fig, nil
}

Expand Down Expand Up @@ -183,14 +182,15 @@ func setP2pConfig(ctx *cli.Context, fig *cfg.P2pCfg) {
}

// createP2PService starts a p2p network layer from provided config
func createP2PService(fig *cfg.Config) (*p2p.Service, chan []byte) {
func createP2PService(fig *cfg.Config, gendata *trie.Genesis) (*p2p.Service, chan []byte) {
config := p2p.Config{
BootstrapNodes: fig.P2p.BootstrapNodes,
BootstrapNodes: append(fig.P2p.BootstrapNodes, common.BytesToStringArray(gendata.Bootnodes)...),
Port: fig.P2p.Port,
RandSeed: 0,
NoBootstrap: fig.P2p.NoBootstrap,
NoMdns: fig.P2p.NoMdns,
DataDir: fig.Global.DataDir,
ProtocolId: string(gendata.ProtocolId),
}

msgChan := make(chan []byte)
Expand Down
7 changes: 6 additions & 1 deletion cmd/gossamer/configcmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/ChainSafe/gossamer/config/genesis"
"github.com/ChainSafe/gossamer/internal/api"
"github.com/ChainSafe/gossamer/polkadb"
"github.com/ChainSafe/gossamer/trie"
log "github.com/ChainSafe/log15"
"github.com/urfave/cli"
)
Expand Down Expand Up @@ -244,7 +245,11 @@ func TestSetGlobalConfig(t *testing.T) {
}

func TestCreateP2PService(t *testing.T) {
srv, _ := createP2PService(cfg.DefaultConfig())
gendata := &trie.Genesis{
ProtocolId: []byte("gossamer"),
}

srv, _ := createP2PService(cfg.DefaultConfig(), gendata)

if srv == nil {
t.Fatalf("failed to create p2p service")
Expand Down
5 changes: 3 additions & 2 deletions cmd/gossamer/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"reflect"
"testing"

"github.com/ChainSafe/gossamer/common"
"github.com/ChainSafe/gossamer/config/genesis"
"github.com/ChainSafe/gossamer/core"
"github.com/ChainSafe/gossamer/dot"
Expand Down Expand Up @@ -62,11 +63,11 @@ func TestStoreGenesisInfo(t *testing.T) {
expected := &trie.Genesis{
Name: []byte(tmpGenesis.Name),
Id: []byte(tmpGenesis.Id),
Bootnodes: tmpGenesis.Bootnodes,
ProtocolId: []byte(tmpGenesis.ProtocolId),
Bootnodes: common.StringArrayToBytes(tmpGenesis.Bootnodes),
}

if reflect.DeepEqual(gendata, expected) {
if !reflect.DeepEqual(gendata, expected) {
t.Fatalf("Fail to get genesis data: got %s expected %s", gendata, expected)
}
}
Expand Down
18 changes: 18 additions & 0 deletions common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,24 @@ const (
HashLength = 32
)

// StringArrayToBytes turns an array of strings into an array of byte arrays
func StringArrayToBytes(in []string) [][]byte {
b := [][]byte{}
for _, str := range in {
b = append(b, []byte(str))
}
return b
}

// BytesToStringArray turns an array of byte arrays into an array strings
func BytesToStringArray(in [][]byte) []string {
strs := []string{}
for _, b := range in {
strs = append(strs, string(b))
}
return strs
}

// HexToBytes turns a 0x prefixed hex string into a byte slice
func HexToBytes(in string) ([]byte, error) {
if strings.Compare(in[:2], "0x") != 0 {
Expand Down
4 changes: 2 additions & 2 deletions genesis.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions p2p/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ const KeyFile = "node.key"
type Config struct {
// Peers used for bootstrapping
BootstrapNodes []string
// Protocol ID for network messages
ProtocolId string
// Listening port
Port uint32
// If 0, random host ID will be generated; If non-0, deterministic ID will be produced, keys will not be loaded from data dir
Expand Down
4 changes: 2 additions & 2 deletions p2p/connmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ func (_ ConnManager) Unprotect(peer.ID, string) bool { return false }
func (_ ConnManager) Close() error { return nil }

func OpenedStream(n net.Network, s net.Stream) {
if string(s.Protocol()) == ProtocolPrefix {
if s.Protocol() == DefaultProtocolId {
log.Info("opened stream", "peer", s.Conn().RemotePeer(), "protocol", s.Protocol())
}
}

func ClosedStream(n net.Network, s net.Stream) {
if string(s.Protocol()) == ProtocolPrefix {
if s.Protocol() == DefaultProtocolId {
log.Info("closed stream", "peer", s.Conn().RemotePeer(), "protocol", s.Protocol())
}
}
20 changes: 14 additions & 6 deletions p2p/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
libp2phost "github.com/libp2p/go-libp2p-core/host"
net "github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/protocol"
kaddht "github.com/libp2p/go-libp2p-kad-dht"

rhost "github.com/libp2p/go-libp2p/p2p/host/routed"
Expand All @@ -23,7 +24,7 @@ import (
"github.com/libp2p/go-libp2p/p2p/discovery"
)

const ProtocolPrefix = "/substrate/dot/2"
const DefaultProtocolId = protocol.ID("/gossamer/dot/0")
const mdnsPeriod = time.Minute

// host is a wrapper around libp2p's host.host
Expand All @@ -37,6 +38,7 @@ type host struct {
noBootstrap bool
noMdns bool
mdns discovery.Service
protocolId protocol.ID
}

func newHost(ctx context.Context, cfg *Config) (*host, error) {
Expand All @@ -50,6 +52,11 @@ func newHost(ctx context.Context, cfg *Config) (*host, error) {
return nil, err
}

protocolId := protocol.ID(cfg.ProtocolId)
if protocolId == "" {
protocolId = DefaultProtocolId
}

dstore := dsync.MutexWrap(ds.NewMapDatastore())
dht := kaddht.NewDHT(ctx, h, dstore)

Expand Down Expand Up @@ -79,6 +86,7 @@ func newHost(ctx context.Context, cfg *Config) (*host, error) {
dht: dht,
dhtConfig: dhtConfig,
bootnodes: bootstrapNodes,
protocolId: protocolId,
noBootstrap: cfg.NoBootstrap,
noMdns: cfg.NoMdns,
}, nil
Expand All @@ -102,7 +110,7 @@ func (h *host) bootstrap() {

func (h *host) startMdns() {
if !h.noMdns {
mdns, err := discovery.NewMdnsService(h.ctx, h.h, mdnsPeriod, ProtocolPrefix)
mdns, err := discovery.NewMdnsService(h.ctx, h.h, mdnsPeriod, string(h.protocolId))
if err != nil {
log.Error("error starting MDNS", "err", err)
}
Expand All @@ -122,21 +130,21 @@ func (h *host) logAddrs() {
}

func (h *host) registerStreamHandler(handler func(net.Stream)) {
h.h.SetStreamHandler(ProtocolPrefix, handler)
h.h.SetStreamHandler(h.protocolId, handler)
}

func (h *host) connect(addrInfo peer.AddrInfo) (err error) {
err = h.h.Connect(h.ctx, addrInfo)
return err
}

// getExistingStream gets an existing stream for a peer that uses ProtocolPrefix
// getExistingStream gets an existing stream for a peer that uses the host's protocolId
func (h *host) getExistingStream(p peer.ID) net.Stream {
conns := h.h.Network().ConnsToPeer(p)
for _, conn := range conns {
streams := conn.GetStreams()
for _, stream := range streams {
if stream.Protocol() == ProtocolPrefix {
if stream.Protocol() == h.protocolId {
return stream
}
}
Expand All @@ -151,7 +159,7 @@ func (h *host) send(peer core.PeerAddrInfo, msg []byte) (err error) {

stream := h.getExistingStream(peer.ID)
if stream == nil {
stream, err = h.h.NewStream(h.ctx, peer.ID, ProtocolPrefix)
stream, err = h.h.NewStream(h.ctx, peer.ID, h.protocolId)
log.Debug("opening new stream ", "to", peer.ID)
if err != nil {
log.Error("failed to open stream", "error", err)
Expand Down
1 change: 1 addition & 0 deletions polkadb/dbService.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,6 @@ func (s *DbService) Stop() error {
if err != nil {
return err
}

return nil
}
9 changes: 6 additions & 3 deletions trie/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,16 @@ type Genesis struct {
Name []byte
Id []byte
ProtocolId []byte
Bootnodes []string
Bootnodes [][]byte
}

func NewGenesisFromData(gen *genesis.Genesis) *Genesis {
bnodes := common.StringArrayToBytes(gen.Bootnodes)
return &Genesis{
Name: []byte(gen.Name),
Id: []byte(gen.Id),
ProtocolId: []byte(gen.ProtocolId),
Bootnodes: gen.Bootnodes,
Bootnodes: bnodes,
}
}

Expand All @@ -107,7 +108,9 @@ func (db *Database) LoadGenesisData() (*Genesis, error) {
return nil, err
}

data, err := scale.Decode(enc, &Genesis{})
data, err := scale.Decode(enc, &Genesis{
Bootnodes: [][]byte{{}},
})
if err != nil {
return nil, err
}
Expand Down
3 changes: 1 addition & 2 deletions trie/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ func TestStoreAndLoadGenesisData(t *testing.T) {
Name: []byte("gossamer"),
Id: []byte("gossamer"),
ProtocolId: []byte("gossamer"),
Bootnodes: nil,
Bootnodes: [][]byte{[]byte("noot")},
}

err = trie.db.StoreGenesisData(expected)
Expand All @@ -225,5 +225,4 @@ func TestStoreAndLoadGenesisData(t *testing.T) {
if !reflect.DeepEqual(gen, expected) {
t.Fatalf("Fail: got %v expected %v", gen, expected)
}

}

0 comments on commit baa3bb8

Please sign in to comment.