Browse Source

pkg: expose p2p functions (#6627)

## Description

Expose p2p functions for use in the sdk. 

These functions could also be copied over to the sdk. I dont have a preference of which is better.
pull/6642/head
Marko 3 years ago
committed by GitHub
parent
commit
0e9bec1b53
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 76 additions and 43 deletions
  1. +2
    -0
      CHANGELOG_PENDING.md
  2. +2
    -2
      cmd/tendermint/commands/gen_node_key.go
  3. +1
    -2
      cmd/tendermint/commands/init.go
  4. +2
    -4
      cmd/tendermint/commands/show_node_id.go
  5. +2
    -3
      cmd/tendermint/commands/testnet.go
  6. +39
    -0
      config/config.go
  7. +1
    -1
      internal/p2p/mock/peer.go
  8. +1
    -1
      internal/p2p/peer_set_test.go
  9. +2
    -2
      internal/p2p/switch.go
  10. +1
    -1
      internal/p2p/test_util.go
  11. +4
    -4
      node/node.go
  12. +1
    -2
      node/node_test.go
  13. +1
    -2
      node/public.go
  14. +4
    -4
      node/setup.go
  15. +1
    -2
      test/e2e/runner/setup.go
  16. +4
    -5
      types/node_key.go
  17. +8
    -8
      types/node_key_test.go

+ 2
- 0
CHANGELOG_PENDING.md View File

@ -66,6 +66,8 @@ Friendly reminder: We have a [bug bounty program](https://hackerone.com/tendermi
Some core types have been kept in the `mempool` package such as `TxCache` and it's implementations, the `Mempool` interface itself
and `TxInfo`. (@alexanderbez)
- [crypto/sr25519] \#6526 Do not re-execute the Ed25519-style key derivation step when doing signing and verification. The derivation is now done once and only once. This breaks `sr25519.GenPrivKeyFromSecret` output compatibility. (@Yawning)
- [types] \#6627 Move `NodeKey` to types to make the type public.
- [config] \#6627 Extend `config` to contain methods `LoadNodeKeyID` and `LoadorGenNodeKeyID`
- Blockchain Protocol


+ 2
- 2
cmd/tendermint/commands/gen_node_key.go View File

@ -5,8 +5,8 @@ import (
"github.com/spf13/cobra"
"github.com/tendermint/tendermint/internal/p2p"
tmjson "github.com/tendermint/tendermint/libs/json"
"github.com/tendermint/tendermint/types"
)
// GenNodeKeyCmd allows the generation of a node key. It prints JSON-encoded
@ -20,7 +20,7 @@ var GenNodeKeyCmd = &cobra.Command{
}
func genNodeKey(cmd *cobra.Command, args []string) error {
nodeKey := p2p.GenNodeKey()
nodeKey := types.GenNodeKey()
bz, err := tmjson.Marshal(nodeKey)
if err != nil {


+ 1
- 2
cmd/tendermint/commands/init.go View File

@ -8,7 +8,6 @@ import (
"github.com/spf13/cobra"
cfg "github.com/tendermint/tendermint/config"
"github.com/tendermint/tendermint/internal/p2p"
tmos "github.com/tendermint/tendermint/libs/os"
tmrand "github.com/tendermint/tendermint/libs/rand"
tmtime "github.com/tendermint/tendermint/libs/time"
@ -76,7 +75,7 @@ func initFilesWithConfig(config *cfg.Config) error {
if tmos.FileExists(nodeKeyFile) {
logger.Info("Found node key", "path", nodeKeyFile)
} else {
if _, err := p2p.LoadOrGenNodeKey(nodeKeyFile); err != nil {
if _, err := types.LoadOrGenNodeKey(nodeKeyFile); err != nil {
return err
}
logger.Info("Generated node key", "path", nodeKeyFile)


+ 2
- 4
cmd/tendermint/commands/show_node_id.go View File

@ -4,8 +4,6 @@ import (
"fmt"
"github.com/spf13/cobra"
"github.com/tendermint/tendermint/internal/p2p"
)
// ShowNodeIDCmd dumps node's ID to the standard output.
@ -18,11 +16,11 @@ var ShowNodeIDCmd = &cobra.Command{
}
func showNodeID(cmd *cobra.Command, args []string) error {
nodeKey, err := p2p.LoadNodeKey(config.NodeKeyFile())
nodeKeyID, err := config.LoadNodeKeyID()
if err != nil {
return err
}
fmt.Println(nodeKey.ID)
fmt.Println(nodeKeyID)
return nil
}

+ 2
- 3
cmd/tendermint/commands/testnet.go View File

@ -12,7 +12,6 @@ import (
"github.com/spf13/viper"
cfg "github.com/tendermint/tendermint/config"
"github.com/tendermint/tendermint/internal/p2p"
"github.com/tendermint/tendermint/libs/bytes"
tmrand "github.com/tendermint/tendermint/libs/rand"
tmtime "github.com/tendermint/tendermint/libs/time"
@ -274,11 +273,11 @@ func persistentPeersArray(config *cfg.Config) ([]string, error) {
for i := 0; i < nValidators+nNonValidators; i++ {
nodeDir := filepath.Join(outputDir, fmt.Sprintf("%s%d", nodeDirPrefix, i))
config.SetRoot(nodeDir)
nodeKey, err := p2p.LoadNodeKey(config.NodeKeyFile())
nodeKey, err := config.LoadNodeKeyID()
if err != nil {
return []string{}, err
}
peers[i] = nodeKey.ID.AddressString(fmt.Sprintf("%s:%d", hostnameOrIP(i), p2pPort))
peers[i] = nodeKey.AddressString(fmt.Sprintf("%s:%d", hostnameOrIP(i), p2pPort))
}
return peers, nil
}


+ 39
- 0
config/config.go View File

@ -4,12 +4,16 @@ import (
"encoding/hex"
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"time"
tmjson "github.com/tendermint/tendermint/libs/json"
"github.com/tendermint/tendermint/libs/log"
tmos "github.com/tendermint/tendermint/libs/os"
"github.com/tendermint/tendermint/types"
)
const (
@ -282,6 +286,41 @@ func (cfg BaseConfig) NodeKeyFile() string {
return rootify(cfg.NodeKey, cfg.RootDir)
}
// LoadNodeKey loads NodeKey located in filePath.
func (cfg BaseConfig) LoadNodeKeyID() (types.NodeID, error) {
jsonBytes, err := ioutil.ReadFile(cfg.NodeKeyFile())
if err != nil {
return "", err
}
nodeKey := types.NodeKey{}
err = tmjson.Unmarshal(jsonBytes, &nodeKey)
if err != nil {
return "", err
}
nodeKey.ID = types.NodeIDFromPubKey(nodeKey.PubKey())
return nodeKey.ID, nil
}
// LoadOrGenNodeKey attempts to load the NodeKey from the given filePath. If
// the file does not exist, it generates and saves a new NodeKey.
func (cfg BaseConfig) LoadOrGenNodeKeyID() (types.NodeID, error) {
if tmos.FileExists(cfg.NodeKeyFile()) {
nodeKey, err := cfg.LoadNodeKeyID()
if err != nil {
return "", err
}
return nodeKey, nil
}
nodeKey := types.GenNodeKey()
if err := nodeKey.SaveAs(cfg.NodeKeyFile()); err != nil {
return "", err
}
return nodeKey.ID, nil
}
// DBDir returns the full path to the database directory
func (cfg BaseConfig) DBDir() string {
return rootify(cfg.DBPath, cfg.RootDir)


+ 1
- 1
internal/p2p/mock/peer.go View File

@ -27,7 +27,7 @@ func NewPeer(ip net.IP) *Peer {
} else {
netAddr = types.NewNetAddressIPPort(ip, 26656)
}
nodeKey := p2p.GenNodeKey()
nodeKey := types.GenNodeKey()
netAddr.ID = nodeKey.ID
mp := &Peer{
ip: ip,


+ 1
- 1
internal/p2p/peer_set_test.go View File

@ -38,7 +38,7 @@ func newMockPeer(ip net.IP) *mockPeer {
if ip == nil {
ip = net.IP{127, 0, 0, 1}
}
nodeKey := GenNodeKey()
nodeKey := types.GenNodeKey()
return &mockPeer{
ip: ip,
id: nodeKey.ID,


+ 2
- 2
internal/p2p/switch.go View File

@ -104,7 +104,7 @@ type Switch struct {
dialing *cmap.CMap
reconnecting *cmap.CMap
nodeInfo types.NodeInfo // our node info
nodeKey NodeKey // our node privkey
nodeKey types.NodeKey // our node privkey
addrBook AddrBook
// peers addresses with whom we'll maintain constant connection
persistentPeersAddrs []*NetAddress
@ -254,7 +254,7 @@ func (sw *Switch) NodeInfo() types.NodeInfo {
// SetNodeKey sets the switch's private key for authenticated encryption.
// NOTE: Not goroutine safe.
func (sw *Switch) SetNodeKey(nodeKey NodeKey) {
func (sw *Switch) SetNodeKey(nodeKey types.NodeKey) {
sw.nodeKey = nodeKey
}


+ 1
- 1
internal/p2p/test_util.go View File

@ -170,7 +170,7 @@ func MakeSwitch(
opts ...SwitchOption,
) *Switch {
nodeKey := GenNodeKey()
nodeKey := types.GenNodeKey()
nodeInfo := testNodeInfo(nodeKey.ID, fmt.Sprintf("node%d", i))
addr, err := types.NewNetAddressString(
nodeKey.ID.AddressString(nodeInfo.ListenAddr),


+ 4
- 4
node/node.go View File

@ -59,7 +59,7 @@ type nodeImpl struct {
router *p2p.Router
addrBook pex.AddrBook // known peers
nodeInfo types.NodeInfo
nodeKey p2p.NodeKey // our node privkey
nodeKey types.NodeKey // our node privkey
isListening bool
// services
@ -89,7 +89,7 @@ type nodeImpl struct {
// PrivValidator, ClientCreator, GenesisDoc, and DBProvider.
// It implements NodeProvider.
func newDefaultNode(config *cfg.Config, logger log.Logger) (service.Service, error) {
nodeKey, err := p2p.LoadOrGenNodeKey(config.NodeKeyFile())
nodeKey, err := types.LoadOrGenNodeKey(config.NodeKeyFile())
if err != nil {
return nil, fmt.Errorf("failed to load or gen node key %s: %w", config.NodeKeyFile(), err)
}
@ -126,7 +126,7 @@ func newDefaultNode(config *cfg.Config, logger log.Logger) (service.Service, err
// makeNode returns a new, ready to go, Tendermint Node.
func makeNode(config *cfg.Config,
privValidator types.PrivValidator,
nodeKey p2p.NodeKey,
nodeKey types.NodeKey,
clientCreator proxy.ClientCreator,
genesisDocProvider genesisDocProvider,
dbProvider cfg.DBProvider,
@ -458,7 +458,7 @@ func makeNode(config *cfg.Config,
// makeSeedNode returns a new seed node, containing only p2p, pex reactor
func makeSeedNode(config *cfg.Config,
dbProvider cfg.DBProvider,
nodeKey p2p.NodeKey,
nodeKey types.NodeKey,
genesisDocProvider genesisDocProvider,
logger log.Logger,
) (service.Service, error) {


+ 1
- 2
node/node_test.go View File

@ -24,7 +24,6 @@ import (
"github.com/tendermint/tendermint/internal/evidence"
"github.com/tendermint/tendermint/internal/mempool"
mempoolv0 "github.com/tendermint/tendermint/internal/mempool/v0"
"github.com/tendermint/tendermint/internal/p2p"
"github.com/tendermint/tendermint/internal/test/factory"
"github.com/tendermint/tendermint/libs/log"
tmrand "github.com/tendermint/tendermint/libs/rand"
@ -486,7 +485,7 @@ func TestNodeNewSeedNode(t *testing.T) {
config.Mode = cfg.ModeSeed
defer os.RemoveAll(config.RootDir)
nodeKey, err := p2p.LoadOrGenNodeKey(config.NodeKeyFile())
nodeKey, err := types.LoadOrGenNodeKey(config.NodeKeyFile())
require.NoError(t, err)
ns, err := makeSeedNode(config,


+ 1
- 2
node/public.go View File

@ -5,7 +5,6 @@ import (
"fmt"
"github.com/tendermint/tendermint/config"
"github.com/tendermint/tendermint/internal/p2p"
"github.com/tendermint/tendermint/libs/log"
"github.com/tendermint/tendermint/libs/service"
"github.com/tendermint/tendermint/privval"
@ -32,7 +31,7 @@ func New(conf *config.Config,
cf proxy.ClientCreator,
gen *types.GenesisDoc,
) (service.Service, error) {
nodeKey, err := p2p.LoadOrGenNodeKey(conf.NodeKeyFile())
nodeKey, err := types.LoadOrGenNodeKey(conf.NodeKeyFile())
if err != nil {
return nil, fmt.Errorf("failed to load or gen node key %s: %w", conf.NodeKeyFile(), err)
}


+ 4
- 4
node/setup.go View File

@ -566,7 +566,7 @@ func createSwitch(
evidenceReactor *p2p.ReactorShim,
proxyApp proxy.AppConns,
nodeInfo types.NodeInfo,
nodeKey p2p.NodeKey,
nodeKey types.NodeKey,
p2pLogger log.Logger,
) *p2p.Switch {
@ -644,7 +644,7 @@ func createSwitch(
}
func createAddrBookAndSetOnSwitch(config *cfg.Config, sw *p2p.Switch,
p2pLogger log.Logger, nodeKey p2p.NodeKey) (pex.AddrBook, error) {
p2pLogger log.Logger, nodeKey types.NodeKey) (pex.AddrBook, error) {
addrBook := pex.NewAddrBook(config.P2P.AddrBookFile(), config.P2P.AddrBookStrict)
addrBook.SetLogger(p2pLogger.With("book", config.P2P.AddrBookFile()))
@ -709,7 +709,7 @@ func createPEXReactorV2(
func makeNodeInfo(
config *cfg.Config,
nodeKey p2p.NodeKey,
nodeKey types.NodeKey,
eventSinks []indexer.EventSink,
genDoc *types.GenesisDoc,
state sm.State,
@ -778,7 +778,7 @@ func makeNodeInfo(
func makeSeedNodeInfo(
config *cfg.Config,
nodeKey p2p.NodeKey,
nodeKey types.NodeKey,
genDoc *types.GenesisDoc,
state sm.State,
) (types.NodeInfo, error) {


+ 1
- 2
test/e2e/runner/setup.go View File

@ -20,7 +20,6 @@ import (
"github.com/tendermint/tendermint/config"
"github.com/tendermint/tendermint/crypto/ed25519"
"github.com/tendermint/tendermint/internal/p2p"
"github.com/tendermint/tendermint/privval"
e2e "github.com/tendermint/tendermint/test/e2e/pkg"
"github.com/tendermint/tendermint/types"
@ -106,7 +105,7 @@ func Setup(testnet *e2e.Testnet) error {
return err
}
err = (&p2p.NodeKey{PrivKey: node.NodeKey}).SaveAs(filepath.Join(nodeDir, "config", "node_key.json"))
err = (&types.NodeKey{PrivKey: node.NodeKey}).SaveAs(filepath.Join(nodeDir, "config", "node_key.json"))
if err != nil {
return err
}


internal/p2p/key.go → types/node_key.go View File


internal/p2p/key_test.go → types/node_key_test.go View File


Loading…
Cancel
Save