Skip to content

Commit f7a551f

Browse files
authored
Merge pull request #14 from lazyledger/tzdybal/p2p/bootstrapping
P2P bootstrapping
2 parents 363cec3 + bb1da59 commit f7a551f

14 files changed

Lines changed: 857 additions & 14 deletions

File tree

‎abci/client/client.go‎

Lines changed: 0 additions & 11 deletions
This file was deleted.

‎config/config.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package config
2+
3+
type NodeConfig struct {
4+
P2P P2PConfig
5+
}

‎config/p2p.go‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package config
2+
3+
type P2PConfig struct {
4+
ListenAddress string // Address to listen for incoming connections
5+
Seeds string // Comma separated list of seed nodes to connect to
6+
}

‎conv/config.go‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package conv
2+
3+
import (
4+
tmcfg "github.com/lazyledger/lazyledger-core/config"
5+
6+
"github.com/lazyledger/optimint/config"
7+
)
8+
9+
// GetNodeConfig translates Tendermint's configuration into Optimint configuration.
10+
//
11+
// This method only translates configuration, and doesn't verify it. If some option is missing in Tendermint's
12+
// config, it's skipped during translation.
13+
func GetNodeConfig(cfg *tmcfg.Config) config.NodeConfig {
14+
nodeConf := config.NodeConfig{}
15+
16+
if cfg != nil {
17+
if cfg.P2P != nil {
18+
nodeConf.P2P.ListenAddress = cfg.P2P.ListenAddress
19+
nodeConf.P2P.Seeds = cfg.P2P.Seeds
20+
}
21+
}
22+
23+
return nodeConf
24+
}

‎conv/config_test.go‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package conv
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
8+
tmcfg "github.com/lazyledger/lazyledger-core/config"
9+
"github.com/lazyledger/optimint/config"
10+
)
11+
12+
func TestGetNodeConfig(t *testing.T) {
13+
t.Parallel()
14+
15+
cases := []struct {
16+
name string
17+
input *tmcfg.Config
18+
expected config.NodeConfig
19+
}{
20+
{"empty", nil, config.NodeConfig{}},
21+
{"Seeds", &tmcfg.Config{P2P: &tmcfg.P2PConfig{Seeds: "seeds"}}, config.NodeConfig{P2P: config.P2PConfig{Seeds: "seeds"}}},
22+
{"ListenAddress", &tmcfg.Config{P2P: &tmcfg.P2PConfig{ListenAddress: "127.0.0.1:7676"}}, config.NodeConfig{P2P: config.P2PConfig{ListenAddress: "127.0.0.1:7676"}}},
23+
}
24+
25+
for _, c := range cases {
26+
t.Run(c.name, func(t *testing.T) {
27+
actual := GetNodeConfig(c.input)
28+
assert.Equal(t, c.expected, actual)
29+
})
30+
}
31+
}

‎conv/crypto.go‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package conv
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
7+
"github.com/lazyledger/lazyledger-core/p2p"
8+
"github.com/libp2p/go-libp2p-core/crypto"
9+
)
10+
11+
var ErrNilKey = errors.New("key can't be nil")
12+
var ErrUnsupportedKeyType = errors.New("unsupported key type")
13+
14+
func GetNodeKey(nodeKey *p2p.NodeKey) (crypto.PrivKey, error) {
15+
if nodeKey == nil || nodeKey.PrivKey == nil {
16+
return nil, ErrNilKey
17+
}
18+
switch nodeKey.PrivKey.Type() {
19+
case "ed25519":
20+
privKey, err := crypto.UnmarshalEd25519PrivateKey(nodeKey.PrivKey.Bytes())
21+
if err != nil {
22+
return nil, fmt.Errorf("node private key unmarshaling error: %w", err)
23+
}
24+
return privKey, nil
25+
default:
26+
return nil, ErrUnsupportedKeyType
27+
}
28+
}

‎conv/crypto_test.go‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package conv
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
"github.com/stretchr/testify/require"
8+
9+
"github.com/lazyledger/lazyledger-core/crypto/secp256k1"
10+
"github.com/lazyledger/lazyledger-core/p2p"
11+
pb "github.com/libp2p/go-libp2p-core/crypto/pb"
12+
)
13+
14+
func TestGetNodeKey(t *testing.T) {
15+
t.Parallel()
16+
17+
valid := p2p.GenNodeKey()
18+
invalid := p2p.NodeKey{
19+
PrivKey: secp256k1.GenPrivKey(),
20+
}
21+
22+
cases := []struct {
23+
name string
24+
input *p2p.NodeKey
25+
expectedType pb.KeyType
26+
err error
27+
}{
28+
{"nil", nil, pb.KeyType(-1), ErrNilKey},
29+
{"empty", &p2p.NodeKey{}, pb.KeyType(-1), ErrNilKey},
30+
{"invalid", &invalid, pb.KeyType(-1), ErrUnsupportedKeyType},
31+
{"valid", &valid, pb.KeyType_Ed25519, nil},
32+
}
33+
34+
for _, c := range cases {
35+
t.Run(c.name, func(t *testing.T) {
36+
actual, err := GetNodeKey(c.input)
37+
if c.err != nil {
38+
assert.Nil(t, actual)
39+
assert.Error(t, err)
40+
assert.ErrorIs(t, c.err, err)
41+
} else {
42+
require.NotNil(t, actual)
43+
assert.NoError(t, err)
44+
assert.Equal(t, c.expectedType, actual.Type())
45+
}
46+
})
47+
}
48+
}

‎go.mod‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,11 @@ module github.com/lazyledger/optimint
22

33
go 1.15
44

5-
require github.com/lazyledger/lazyledger-core v0.0.0-20210219190522-0eccfb24e2aa
5+
require (
6+
github.com/lazyledger/lazyledger-core v0.0.0-20210219190522-0eccfb24e2aa
7+
github.com/libp2p/go-libp2p v0.13.0 // indirect
8+
github.com/libp2p/go-libp2p-core v0.8.5
9+
github.com/multiformats/go-multiaddr v0.3.1 // indirect
10+
github.com/multiformats/go-multihash v0.0.14 // indirect
11+
github.com/stretchr/testify v1.7.0
12+
)

‎go.sum‎

Lines changed: 395 additions & 0 deletions
Large diffs are not rendered by default.

‎log/logger.go‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package log
2+
3+
// Logger interface is compatible with Tendermint logger
4+
type Logger interface {
5+
Debug(msg string, keyvals ...interface{})
6+
Info(msg string, keyvals ...interface{})
7+
Error(msg string, keyvals ...interface{})
8+
}

0 commit comments

Comments
 (0)