cmd/swarm, p2p, swarm: Enable ENR in binary/execadapter (#19309)
* cmd/swarm, p2p, swarm: Enable ENR in binary/execadapter * cmd/p2p/swarm: Remove comments + config.Enode nomarshal * p2p/simulations: Remove superfluous error check * p2p/simulation: Move init enode comment * swarm/api: Check error in config test * swarm, p2p/simulations, cmd/swarm: Use nodekey in binary record sign * cmd/swarm: Make nodekey available for swarm api config
This commit is contained in:
@ -29,7 +29,6 @@ import (
|
||||
"github.com/ethereum/go-ethereum/crypto"
|
||||
"github.com/ethereum/go-ethereum/node"
|
||||
"github.com/ethereum/go-ethereum/p2p/enode"
|
||||
"github.com/ethereum/go-ethereum/swarm/log"
|
||||
"github.com/ethereum/go-ethereum/swarm/network"
|
||||
"github.com/ethereum/go-ethereum/swarm/pss"
|
||||
"github.com/ethereum/go-ethereum/swarm/services/swap"
|
||||
@ -58,7 +57,7 @@ type Config struct {
|
||||
Port string
|
||||
PublicKey string
|
||||
BzzKey string
|
||||
NodeID string
|
||||
Enode *enode.Node `toml:"-"`
|
||||
NetworkID uint64
|
||||
SwapEnabled bool
|
||||
SyncEnabled bool
|
||||
@ -104,33 +103,38 @@ func NewConfig() (c *Config) {
|
||||
|
||||
//some config params need to be initialized after the complete
|
||||
//config building phase is completed (e.g. due to overriding flags)
|
||||
func (c *Config) Init(prvKey *ecdsa.PrivateKey) {
|
||||
func (c *Config) Init(prvKey *ecdsa.PrivateKey, nodeKey *ecdsa.PrivateKey) error {
|
||||
|
||||
address := crypto.PubkeyToAddress(prvKey.PublicKey)
|
||||
c.Path = filepath.Join(c.Path, "bzz-"+common.Bytes2Hex(address.Bytes()))
|
||||
err := os.MkdirAll(c.Path, os.ModePerm)
|
||||
// create swarm dir and record key
|
||||
err := c.createAndSetPath(c.Path, prvKey)
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("Error creating root swarm data directory: %v", err))
|
||||
return
|
||||
return fmt.Errorf("Error creating root swarm data directory: %v", err)
|
||||
}
|
||||
c.setKey(prvKey)
|
||||
|
||||
// create the new enode record
|
||||
// signed with the ephemeral node key
|
||||
enodeParams := &network.EnodeParams{
|
||||
PrivateKey: prvKey,
|
||||
EnodeKey: nodeKey,
|
||||
Lightnode: c.LightNodeEnabled,
|
||||
Bootnode: c.BootnodeMode,
|
||||
}
|
||||
c.Enode, err = network.NewEnode(enodeParams)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error creating enode: %v", err)
|
||||
}
|
||||
|
||||
pubkey := crypto.FromECDSAPub(&prvKey.PublicKey)
|
||||
pubkeyhex := common.ToHex(pubkey)
|
||||
keyhex := hexutil.Encode(network.PrivateKeyToBzzKey(prvKey))
|
||||
|
||||
c.PublicKey = pubkeyhex
|
||||
c.BzzKey = keyhex
|
||||
c.NodeID = enode.PubkeyToIDV4(&prvKey.PublicKey).String()
|
||||
|
||||
// initialize components that depend on the swarm instance's private key
|
||||
if c.SwapEnabled {
|
||||
c.Swap.Init(c.Contract, prvKey)
|
||||
}
|
||||
|
||||
c.privateKey = prvKey
|
||||
c.LocalStoreParams.Init(c.Path)
|
||||
c.LocalStoreParams.BaseKey = common.FromHex(keyhex)
|
||||
c.LocalStoreParams.BaseKey = common.FromHex(c.BzzKey)
|
||||
|
||||
c.Pss = c.Pss.WithPrivateKey(c.privateKey)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Config) ShiftPrivateKey() (privKey *ecdsa.PrivateKey) {
|
||||
@ -140,3 +144,25 @@ func (c *Config) ShiftPrivateKey() (privKey *ecdsa.PrivateKey) {
|
||||
}
|
||||
return privKey
|
||||
}
|
||||
|
||||
func (c *Config) setKey(prvKey *ecdsa.PrivateKey) {
|
||||
bzzkeybytes := network.PrivateKeyToBzzKey(prvKey)
|
||||
pubkey := crypto.FromECDSAPub(&prvKey.PublicKey)
|
||||
pubkeyhex := hexutil.Encode(pubkey)
|
||||
keyhex := hexutil.Encode(bzzkeybytes)
|
||||
|
||||
c.privateKey = prvKey
|
||||
c.PublicKey = pubkeyhex
|
||||
c.BzzKey = keyhex
|
||||
}
|
||||
|
||||
func (c *Config) createAndSetPath(datadirPath string, prvKey *ecdsa.PrivateKey) error {
|
||||
address := crypto.PubkeyToAddress(prvKey.PublicKey)
|
||||
bzzdirPath := filepath.Join(datadirPath, "bzz-"+common.Bytes2Hex(address.Bytes()))
|
||||
err := os.MkdirAll(bzzdirPath, os.ModePerm)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c.Path = bzzdirPath
|
||||
return nil
|
||||
}
|
||||
|
@ -27,11 +27,16 @@ import (
|
||||
func TestConfig(t *testing.T) {
|
||||
|
||||
var hexprvkey = "65138b2aa745041b372153550584587da326ab440576b2a1191dd95cee30039c"
|
||||
var hexnodekey = "75138b2aa745041b372153550584587da326ab440576b2a1191dd95cee30039c"
|
||||
|
||||
prvkey, err := crypto.HexToECDSA(hexprvkey)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to load private key: %v", err)
|
||||
}
|
||||
nodekey, err := crypto.HexToECDSA(hexnodekey)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to load private key: %v", err)
|
||||
}
|
||||
|
||||
one := NewConfig()
|
||||
two := NewConfig()
|
||||
@ -41,7 +46,10 @@ func TestConfig(t *testing.T) {
|
||||
t.Fatal("Two default configs are not equal")
|
||||
}
|
||||
|
||||
one.Init(prvkey)
|
||||
err = one.Init(prvkey, nodekey)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
//the init function should set the following fields
|
||||
if one.BzzKey == "" {
|
||||
|
@ -7,6 +7,7 @@ import (
|
||||
|
||||
"github.com/ethereum/go-ethereum/crypto"
|
||||
"github.com/ethereum/go-ethereum/p2p/enode"
|
||||
"github.com/ethereum/go-ethereum/p2p/enr"
|
||||
)
|
||||
|
||||
// BzzAddr implements the PeerAddr interface
|
||||
@ -68,3 +69,37 @@ func PrivateKeyToBzzKey(prvKey *ecdsa.PrivateKey) []byte {
|
||||
pubkeyBytes := crypto.FromECDSAPub(&prvKey.PublicKey)
|
||||
return crypto.Keccak256Hash(pubkeyBytes).Bytes()
|
||||
}
|
||||
|
||||
type EnodeParams struct {
|
||||
PrivateKey *ecdsa.PrivateKey
|
||||
EnodeKey *ecdsa.PrivateKey
|
||||
Lightnode bool
|
||||
Bootnode bool
|
||||
}
|
||||
|
||||
func NewEnodeRecord(params *EnodeParams) (*enr.Record, error) {
|
||||
|
||||
if params.PrivateKey == nil {
|
||||
return nil, fmt.Errorf("all param private keys must be defined")
|
||||
}
|
||||
|
||||
bzzkeybytes := PrivateKeyToBzzKey(params.PrivateKey)
|
||||
|
||||
var record enr.Record
|
||||
record.Set(NewENRAddrEntry(bzzkeybytes))
|
||||
record.Set(ENRLightNodeEntry(params.Lightnode))
|
||||
record.Set(ENRBootNodeEntry(params.Bootnode))
|
||||
return &record, nil
|
||||
}
|
||||
|
||||
func NewEnode(params *EnodeParams) (*enode.Node, error) {
|
||||
record, err := NewEnodeRecord(params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = enode.SignV4(record, params.EnodeKey)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("ENR create fail: %v", err)
|
||||
}
|
||||
return enode.New(enode.V4ID{}, record)
|
||||
}
|
||||
|
@ -102,16 +102,16 @@ func (s *Simulation) AddNode(opts ...AddNodeOption) (id enode.ID, err error) {
|
||||
// most importantly the bzz overlay address
|
||||
//
|
||||
// for now we have no way of setting bootnodes or lightnodes in sims
|
||||
// so we just set them as false
|
||||
// so we just let them be set to false
|
||||
// they should perhaps be possible to override them with AddNodeOption
|
||||
bzzKey := network.PrivateKeyToBzzKey(conf.PrivateKey)
|
||||
bzzAddr := network.NewENRAddrEntry(bzzKey)
|
||||
|
||||
var lightnode network.ENRLightNodeEntry
|
||||
var bootnode network.ENRBootNodeEntry
|
||||
conf.Record.Set(bzzAddr)
|
||||
conf.Record.Set(&lightnode)
|
||||
conf.Record.Set(&bootnode)
|
||||
enodeParams := &network.EnodeParams{
|
||||
PrivateKey: conf.PrivateKey,
|
||||
}
|
||||
record, err := network.NewEnodeRecord(enodeParams)
|
||||
if err != nil {
|
||||
return enode.ID{}, err
|
||||
}
|
||||
conf.Record = *record
|
||||
|
||||
// Add the bzz address to the node config
|
||||
node, err := s.Net.NewNodeWithConfig(conf)
|
||||
|
@ -311,8 +311,12 @@ func testSwarmNetwork(t *testing.T, o *testSwarmNetworkOptions, steps ...testSwa
|
||||
if err != nil {
|
||||
return nil, cleanup, err
|
||||
}
|
||||
nodekey, err := crypto.GenerateKey()
|
||||
if err != nil {
|
||||
return nil, cleanup, err
|
||||
}
|
||||
|
||||
config.Init(privkey)
|
||||
config.Init(privkey, nodekey)
|
||||
config.DeliverySkipCheck = o.SkipCheck
|
||||
config.Port = ""
|
||||
|
||||
|
@ -36,7 +36,6 @@ import (
|
||||
"github.com/ethereum/go-ethereum/ethclient"
|
||||
"github.com/ethereum/go-ethereum/metrics"
|
||||
"github.com/ethereum/go-ethereum/p2p"
|
||||
"github.com/ethereum/go-ethereum/p2p/enode"
|
||||
"github.com/ethereum/go-ethereum/p2p/protocols"
|
||||
"github.com/ethereum/go-ethereum/params"
|
||||
"github.com/ethereum/go-ethereum/rpc"
|
||||
@ -171,10 +170,7 @@ func NewSwarm(config *api.Config, mockStore *mock.NodeStore) (self *Swarm, err e
|
||||
self.accountingMetrics = protocols.SetupAccountingMetrics(10*time.Second, filepath.Join(config.Path, "metrics.db"))
|
||||
}
|
||||
|
||||
var nodeID enode.ID
|
||||
if err := nodeID.UnmarshalText([]byte(config.NodeID)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
nodeID := config.Enode.ID()
|
||||
|
||||
syncing := stream.SyncingAutoSubscribe
|
||||
if !config.SyncEnabled || config.LightNodeEnabled {
|
||||
|
@ -170,8 +170,12 @@ func TestNewSwarm(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
nodekey, err := crypto.GenerateKey()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
config.Init(privkey)
|
||||
config.Init(privkey, nodekey)
|
||||
|
||||
if tc.configure != nil {
|
||||
tc.configure(config)
|
||||
@ -307,8 +311,12 @@ func TestLocalStoreAndRetrieve(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
nodekey, err := crypto.GenerateKey()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
config.Init(privkey)
|
||||
config.Init(privkey, nodekey)
|
||||
|
||||
swarm, err := NewSwarm(config, nil)
|
||||
if err != nil {
|
||||
|
Reference in New Issue
Block a user