2018-11-26 19:05:18 +03:00
|
|
|
// Copyright 2018 The go-ethereum Authors
|
2016-08-29 22:18:00 +03:00
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package swarm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2017-03-22 20:20:33 +03:00
|
|
|
"context"
|
2016-08-29 22:18:00 +03:00
|
|
|
"crypto/ecdsa"
|
2019-04-10 17:50:58 +03:00
|
|
|
"errors"
|
2016-08-29 22:18:00 +03:00
|
|
|
"fmt"
|
2018-07-13 18:40:28 +03:00
|
|
|
"io"
|
2017-12-01 12:32:14 +03:00
|
|
|
"math/big"
|
2017-05-22 09:56:40 +03:00
|
|
|
"net"
|
2018-06-20 15:06:27 +03:00
|
|
|
"path/filepath"
|
2017-12-13 12:23:11 +03:00
|
|
|
"strings"
|
2017-12-01 12:32:14 +03:00
|
|
|
"time"
|
2017-12-13 12:23:11 +03:00
|
|
|
"unicode"
|
2016-08-29 22:18:00 +03:00
|
|
|
|
2019-04-10 17:50:58 +03:00
|
|
|
"github.com/ethereum/go-ethereum/swarm/chunk"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/swarm/storage/feed"
|
|
|
|
"github.com/ethereum/go-ethereum/swarm/storage/localstore"
|
|
|
|
|
2016-08-29 22:18:00 +03:00
|
|
|
"github.com/ethereum/go-ethereum/accounts/abi/bind"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/contracts/chequebook"
|
|
|
|
"github.com/ethereum/go-ethereum/contracts/ens"
|
2017-04-13 12:06:19 +03:00
|
|
|
"github.com/ethereum/go-ethereum/ethclient"
|
2018-02-23 16:19:59 +03:00
|
|
|
"github.com/ethereum/go-ethereum/metrics"
|
2016-08-29 22:18:00 +03:00
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
2018-06-20 15:06:27 +03:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/protocols"
|
2017-12-01 12:32:14 +03:00
|
|
|
"github.com/ethereum/go-ethereum/params"
|
2016-08-29 22:18:00 +03:00
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
|
|
|
"github.com/ethereum/go-ethereum/swarm/api"
|
|
|
|
httpapi "github.com/ethereum/go-ethereum/swarm/api/http"
|
2017-04-12 03:06:02 +03:00
|
|
|
"github.com/ethereum/go-ethereum/swarm/fuse"
|
2018-06-20 15:06:27 +03:00
|
|
|
"github.com/ethereum/go-ethereum/swarm/log"
|
2016-08-29 22:18:00 +03:00
|
|
|
"github.com/ethereum/go-ethereum/swarm/network"
|
2018-06-20 15:06:27 +03:00
|
|
|
"github.com/ethereum/go-ethereum/swarm/network/stream"
|
|
|
|
"github.com/ethereum/go-ethereum/swarm/pss"
|
|
|
|
"github.com/ethereum/go-ethereum/swarm/state"
|
2016-08-29 22:18:00 +03:00
|
|
|
"github.com/ethereum/go-ethereum/swarm/storage"
|
2018-06-20 15:06:27 +03:00
|
|
|
"github.com/ethereum/go-ethereum/swarm/storage/mock"
|
2018-11-16 01:41:19 +03:00
|
|
|
"github.com/ethereum/go-ethereum/swarm/swap"
|
2018-07-13 18:40:28 +03:00
|
|
|
"github.com/ethereum/go-ethereum/swarm/tracing"
|
2016-08-29 22:18:00 +03:00
|
|
|
)
|
|
|
|
|
2018-02-23 16:19:59 +03:00
|
|
|
var (
|
|
|
|
updateGaugesPeriod = 5 * time.Second
|
|
|
|
startCounter = metrics.NewRegisteredCounter("stack,start", nil)
|
|
|
|
stopCounter = metrics.NewRegisteredCounter("stack,stop", nil)
|
|
|
|
uptimeGauge = metrics.NewRegisteredGauge("stack.uptime", nil)
|
2018-06-20 15:06:27 +03:00
|
|
|
requestsCacheGauge = metrics.NewRegisteredGauge("storage.cache.requests.size", nil)
|
2018-02-23 16:19:59 +03:00
|
|
|
)
|
|
|
|
|
2016-08-29 22:18:00 +03:00
|
|
|
// the swarm stack
|
|
|
|
type Swarm struct {
|
2018-11-26 19:05:18 +03:00
|
|
|
config *api.Config // swarm configuration
|
|
|
|
api *api.API // high level api layer (fs/manifest)
|
|
|
|
dns api.Resolver // DNS registrar
|
|
|
|
fileStore *storage.FileStore // distributed preimage archive, the local API to the storage with document level storage/retrieval support
|
|
|
|
streamer *stream.Registry
|
|
|
|
bzz *network.Bzz // the logistic manager
|
|
|
|
backend chequebook.Backend // simple blockchain Backend
|
|
|
|
privateKey *ecdsa.PrivateKey
|
|
|
|
netStore *storage.NetStore
|
|
|
|
sfs *fuse.SwarmFS // need this to cleanup all the active mounts on node exit
|
|
|
|
ps *pss.Pss
|
|
|
|
swap *swap.Swap
|
|
|
|
stateStore *state.DBStore
|
|
|
|
accountingMetrics *protocols.AccountingMetrics
|
2019-02-13 10:15:03 +03:00
|
|
|
cleanupFuncs []func() error
|
2018-07-13 18:40:28 +03:00
|
|
|
|
|
|
|
tracerClose io.Closer
|
2016-08-29 22:18:00 +03:00
|
|
|
}
|
|
|
|
|
2019-01-24 14:02:18 +03:00
|
|
|
// NewSwarm creates a new swarm service instance
|
2016-08-29 22:18:00 +03:00
|
|
|
// implements node.Service
|
2018-06-20 15:06:27 +03:00
|
|
|
// If mockStore is not nil, it will be used as the storage for chunk data.
|
|
|
|
// MockStore should be used only for testing.
|
|
|
|
func NewSwarm(config *api.Config, mockStore *mock.NodeStore) (self *Swarm, err error) {
|
|
|
|
if bytes.Equal(common.FromHex(config.PublicKey), storage.ZeroAddr) {
|
2016-08-29 22:18:00 +03:00
|
|
|
return nil, fmt.Errorf("empty public key")
|
|
|
|
}
|
2018-06-20 15:06:27 +03:00
|
|
|
if bytes.Equal(common.FromHex(config.BzzKey), storage.ZeroAddr) {
|
2016-08-29 22:18:00 +03:00
|
|
|
return nil, fmt.Errorf("empty bzz key")
|
|
|
|
}
|
|
|
|
|
2018-06-20 15:06:27 +03:00
|
|
|
var backend chequebook.Backend
|
|
|
|
if config.SwapAPI != "" && config.SwapEnabled {
|
|
|
|
log.Info("connecting to SWAP API", "url", config.SwapAPI)
|
|
|
|
backend, err = ethclient.Dial(config.SwapAPI)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error connecting to SWAP API %s: %s", config.SwapAPI, err)
|
|
|
|
}
|
2016-08-29 22:18:00 +03:00
|
|
|
}
|
|
|
|
|
2018-06-20 15:06:27 +03:00
|
|
|
self = &Swarm{
|
2019-02-13 10:15:03 +03:00
|
|
|
config: config,
|
|
|
|
backend: backend,
|
|
|
|
privateKey: config.ShiftPrivateKey(),
|
|
|
|
cleanupFuncs: []func() error{},
|
2016-08-29 22:18:00 +03:00
|
|
|
}
|
2018-08-21 11:34:10 +03:00
|
|
|
log.Debug("Setting up Swarm service components")
|
2016-08-29 22:18:00 +03:00
|
|
|
|
2018-06-20 15:06:27 +03:00
|
|
|
config.HiveParams.Discovery = true
|
2016-08-29 22:18:00 +03:00
|
|
|
|
2018-06-20 15:06:27 +03:00
|
|
|
bzzconfig := &network.BzzConfig{
|
2019-01-24 14:02:18 +03:00
|
|
|
NetworkID: config.NetworkID,
|
|
|
|
OverlayAddr: common.FromHex(config.BzzKey),
|
|
|
|
HiveParams: config.HiveParams,
|
|
|
|
LightNode: config.LightNodeEnabled,
|
|
|
|
BootnodeMode: config.BootnodeMode,
|
2018-06-20 15:06:27 +03:00
|
|
|
}
|
2016-08-29 22:18:00 +03:00
|
|
|
|
2018-11-26 19:05:18 +03:00
|
|
|
self.stateStore, err = state.NewDBStore(filepath.Join(config.Path, "state-store.db"))
|
2018-06-20 15:06:27 +03:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2016-08-29 22:18:00 +03:00
|
|
|
|
2018-06-20 15:06:27 +03:00
|
|
|
// set up high level api
|
|
|
|
var resolver *api.MultiResolver
|
2017-12-18 18:22:39 +03:00
|
|
|
if len(config.EnsAPIs) > 0 {
|
|
|
|
opts := []api.MultiResolverOption{}
|
|
|
|
for _, c := range config.EnsAPIs {
|
|
|
|
tld, endpoint, addr := parseEnsAPIAddress(c)
|
2018-06-20 15:06:27 +03:00
|
|
|
r, err := newEnsClient(endpoint, addr, config, self.privateKey)
|
2017-12-01 12:32:14 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-12-18 18:22:39 +03:00
|
|
|
opts = append(opts, api.MultiResolverOptionWithResolver(r, tld))
|
2018-06-20 15:06:27 +03:00
|
|
|
|
2017-12-01 12:32:14 +03:00
|
|
|
}
|
2018-06-20 15:06:27 +03:00
|
|
|
resolver = api.NewMultiResolver(opts...)
|
|
|
|
self.dns = resolver
|
|
|
|
}
|
2019-04-10 17:50:58 +03:00
|
|
|
// check that we are not in the old database schema
|
|
|
|
// if so - fail and exit
|
|
|
|
isLegacy := localstore.IsLegacyDatabase(config.ChunkDbPath)
|
2018-06-20 15:06:27 +03:00
|
|
|
|
2019-04-10 17:50:58 +03:00
|
|
|
if isLegacy {
|
|
|
|
return nil, errors.New("Legacy database format detected! Please read the migration announcement at: https://github.com/ethersphere/go-ethereum/wiki/Swarm-v0.4-local-store-migration")
|
|
|
|
}
|
|
|
|
|
|
|
|
var feedsHandler *feed.Handler
|
|
|
|
fhParams := &feed.HandlerParams{}
|
|
|
|
|
|
|
|
feedsHandler = feed.NewHandler(fhParams)
|
|
|
|
|
|
|
|
localStore, err := localstore.New(config.ChunkDbPath, config.BaseKey, &localstore.Options{
|
|
|
|
MockStore: mockStore,
|
|
|
|
Capacity: config.DbCapacity,
|
|
|
|
})
|
2018-06-20 15:06:27 +03:00
|
|
|
if err != nil {
|
2018-09-13 12:42:19 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
2019-04-10 17:50:58 +03:00
|
|
|
lstore := chunk.NewValidatorStore(
|
|
|
|
localStore,
|
|
|
|
storage.NewContentAddressValidator(storage.MakeHashFunc(storage.DefaultHash)),
|
|
|
|
feedsHandler,
|
|
|
|
)
|
2018-09-13 12:42:19 +03:00
|
|
|
|
|
|
|
self.netStore, err = storage.NewNetStore(lstore, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2018-06-20 15:06:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
to := network.NewKademlia(
|
|
|
|
common.FromHex(config.BzzKey),
|
|
|
|
network.NewKadParams(),
|
|
|
|
)
|
2018-09-13 12:42:19 +03:00
|
|
|
delivery := stream.NewDelivery(to, self.netStore)
|
|
|
|
self.netStore.NewNetFetcherFunc = network.NewFetcherFactory(delivery.RequestFromPeers, config.DeliverySkipCheck).New
|
2018-06-20 15:06:27 +03:00
|
|
|
|
2019-04-10 17:50:58 +03:00
|
|
|
feedsHandler.SetStore(self.netStore)
|
|
|
|
|
2018-11-16 01:41:19 +03:00
|
|
|
if config.SwapEnabled {
|
|
|
|
balancesStore, err := state.NewDBStore(filepath.Join(config.Path, "balances.db"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
self.swap = swap.New(balancesStore)
|
2018-11-26 19:05:18 +03:00
|
|
|
self.accountingMetrics = protocols.SetupAccountingMetrics(10*time.Second, filepath.Join(config.Path, "metrics.db"))
|
2018-11-16 01:41:19 +03:00
|
|
|
}
|
|
|
|
|
2019-03-22 07:55:47 +03:00
|
|
|
nodeID := config.Enode.ID()
|
2018-11-07 02:04:18 +03:00
|
|
|
|
|
|
|
syncing := stream.SyncingAutoSubscribe
|
|
|
|
if !config.SyncEnabled || config.LightNodeEnabled {
|
|
|
|
syncing = stream.SyncingDisabled
|
|
|
|
}
|
|
|
|
|
2018-10-17 20:22:37 +03:00
|
|
|
registryOptions := &stream.RegistryOptions{
|
all: new p2p node representation (#17643)
Package p2p/enode provides a generalized representation of p2p nodes
which can contain arbitrary information in key/value pairs. It is also
the new home for the node database. The "v4" identity scheme is also
moved here from p2p/enr to remove the dependency on Ethereum crypto from
that package.
Record signature handling is changed significantly. The identity scheme
registry is removed and acceptable schemes must be passed to any method
that needs identity. This means records must now be validated explicitly
after decoding.
The enode API is designed to make signature handling easy and safe: most
APIs around the codebase work with enode.Node, which is a wrapper around
a valid record. Going from enr.Record to enode.Node requires a valid
signature.
* p2p/discover: port to p2p/enode
This ports the discovery code to the new node representation in
p2p/enode. The wire protocol is unchanged, this can be considered a
refactoring change. The Kademlia table can now deal with nodes using an
arbitrary identity scheme. This requires a few incompatible API changes:
- Table.Lookup is not available anymore. It used to take a public key
as argument because v4 protocol requires one. Its replacement is
LookupRandom.
- Table.Resolve takes *enode.Node instead of NodeID. This is also for
v4 protocol compatibility because nodes cannot be looked up by ID
alone.
- Types Node and NodeID are gone. Further commits in the series will be
fixes all over the the codebase to deal with those removals.
* p2p: port to p2p/enode and discovery changes
This adapts package p2p to the changes in p2p/discover. All uses of
discover.Node and discover.NodeID are replaced by their equivalents from
p2p/enode.
New API is added to retrieve the enode.Node instance of a peer. The
behavior of Server.Self with discovery disabled is improved. It now
tries much harder to report a working IP address, falling back to
127.0.0.1 if no suitable address can be determined through other means.
These changes were needed for tests of other packages later in the
series.
* p2p/simulations, p2p/testing: port to p2p/enode
No surprises here, mostly replacements of discover.Node, discover.NodeID
with their new equivalents. The 'interesting' API changes are:
- testing.ProtocolSession tracks complete nodes, not just their IDs.
- adapters.NodeConfig has a new method to create a complete node.
These changes were needed to make swarm tests work.
Note that the NodeID change makes the code incompatible with old
simulation snapshots.
* whisper/whisperv5, whisper/whisperv6: port to p2p/enode
This port was easy because whisper uses []byte for node IDs and
URL strings in the API.
* eth: port to p2p/enode
Again, easy to port because eth uses strings for node IDs and doesn't
care about node information in any way.
* les: port to p2p/enode
Apart from replacing discover.NodeID with enode.ID, most changes are in
the server pool code. It now deals with complete nodes instead
of (Pubkey, IP, Port) triples. The database format is unchanged for now,
but we should probably change it to use the node database later.
* node: port to p2p/enode
This change simply replaces discover.Node and discover.NodeID with their
new equivalents.
* swarm/network: port to p2p/enode
Swarm has its own node address representation, BzzAddr, containing both
an overlay address (the hash of a secp256k1 public key) and an underlay
address (enode:// URL).
There are no changes to the BzzAddr format in this commit, but certain
operations such as creating a BzzAddr from a node ID are now impossible
because node IDs aren't public keys anymore.
Most swarm-related changes in the series remove uses of
NewAddrFromNodeID, replacing it with NewAddr which takes a complete node
as argument. ToOverlayAddr is removed because we can just use the node
ID directly.
2018-09-25 01:59:00 +03:00
|
|
|
SkipCheck: config.DeliverySkipCheck,
|
2018-11-07 02:04:18 +03:00
|
|
|
Syncing: syncing,
|
2018-06-20 15:06:27 +03:00
|
|
|
SyncUpdateDelay: config.SyncUpdateDelay,
|
2018-09-24 18:40:22 +03:00
|
|
|
MaxPeerServers: config.MaxStreamPeerServers,
|
2018-10-17 20:22:37 +03:00
|
|
|
}
|
2018-11-26 19:05:18 +03:00
|
|
|
self.streamer = stream.NewRegistry(nodeID, delivery, self.netStore, self.stateStore, registryOptions, self.swap)
|
2018-06-20 15:06:27 +03:00
|
|
|
|
|
|
|
// Swarm Hash Merklised Chunking for Arbitrary-length Document/File storage
|
2018-09-13 12:42:19 +03:00
|
|
|
self.fileStore = storage.NewFileStore(self.netStore, self.config.FileStoreParams)
|
2018-06-20 15:06:27 +03:00
|
|
|
|
2018-08-21 11:34:10 +03:00
|
|
|
log.Debug("Setup local storage")
|
2018-06-20 15:06:27 +03:00
|
|
|
|
2018-11-26 19:05:18 +03:00
|
|
|
self.bzz = network.NewBzz(bzzconfig, to, self.stateStore, self.streamer.GetSpec(), self.streamer.Run)
|
2018-06-20 15:06:27 +03:00
|
|
|
|
|
|
|
// Pss = postal service over swarm (devp2p over bzz)
|
|
|
|
self.ps, err = pss.NewPss(to, config.Pss)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if pss.IsActiveHandshake {
|
|
|
|
pss.SetHandshakeController(self.ps, pss.NewHandshakeParams())
|
2016-08-29 22:18:00 +03:00
|
|
|
}
|
|
|
|
|
2018-09-30 10:43:10 +03:00
|
|
|
self.api = api.NewAPI(self.fileStore, self.dns, feedsHandler, self.privateKey)
|
2016-08-29 22:18:00 +03:00
|
|
|
|
2017-04-12 03:06:02 +03:00
|
|
|
self.sfs = fuse.NewSwarmFS(self.api)
|
2018-08-21 11:34:10 +03:00
|
|
|
log.Debug("Initialized FUSE filesystem")
|
2017-03-23 16:56:06 +03:00
|
|
|
|
2016-08-29 22:18:00 +03:00
|
|
|
return self, nil
|
|
|
|
}
|
|
|
|
|
2017-12-13 12:23:11 +03:00
|
|
|
// parseEnsAPIAddress parses string according to format
|
|
|
|
// [tld:][contract-addr@]url and returns ENSClientConfig structure
|
|
|
|
// with endpoint, contract address and TLD.
|
|
|
|
func parseEnsAPIAddress(s string) (tld, endpoint string, addr common.Address) {
|
|
|
|
isAllLetterString := func(s string) bool {
|
|
|
|
for _, r := range s {
|
|
|
|
if !unicode.IsLetter(r) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
endpoint = s
|
|
|
|
if i := strings.Index(endpoint, ":"); i > 0 {
|
|
|
|
if isAllLetterString(endpoint[:i]) && len(endpoint) > i+2 && endpoint[i+1:i+3] != "//" {
|
|
|
|
tld = endpoint[:i]
|
|
|
|
endpoint = endpoint[i+1:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if i := strings.Index(endpoint, "@"); i > 0 {
|
|
|
|
addr = common.HexToAddress(endpoint[:i])
|
|
|
|
endpoint = endpoint[i+1:]
|
|
|
|
}
|
|
|
|
return
|
2017-12-01 12:32:14 +03:00
|
|
|
}
|
|
|
|
|
2018-06-20 15:06:27 +03:00
|
|
|
// ensClient provides functionality for api.ResolveValidator
|
|
|
|
type ensClient struct {
|
|
|
|
*ens.ENS
|
|
|
|
*ethclient.Client
|
|
|
|
}
|
|
|
|
|
2017-12-01 12:32:14 +03:00
|
|
|
// newEnsClient creates a new ENS client for that is a consumer of
|
|
|
|
// a ENS API on a specific endpoint. It is used as a helper function
|
|
|
|
// for creating multiple resolvers in NewSwarm function.
|
2018-06-20 15:06:27 +03:00
|
|
|
func newEnsClient(endpoint string, addr common.Address, config *api.Config, privkey *ecdsa.PrivateKey) (*ensClient, error) {
|
2017-12-01 12:32:14 +03:00
|
|
|
log.Info("connecting to ENS API", "url", endpoint)
|
|
|
|
client, err := rpc.Dial(endpoint)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error connecting to ENS API %s: %s", endpoint, err)
|
|
|
|
}
|
2018-06-20 15:06:27 +03:00
|
|
|
ethClient := ethclient.NewClient(client)
|
2017-12-01 12:32:14 +03:00
|
|
|
|
|
|
|
ensRoot := config.EnsRoot
|
2017-12-13 12:23:11 +03:00
|
|
|
if addr != (common.Address{}) {
|
|
|
|
ensRoot = addr
|
2017-12-01 12:32:14 +03:00
|
|
|
} else {
|
|
|
|
a, err := detectEnsAddr(client)
|
|
|
|
if err == nil {
|
|
|
|
ensRoot = a
|
|
|
|
} else {
|
|
|
|
log.Warn(fmt.Sprintf("could not determine ENS contract address, using default %s", ensRoot), "err", err)
|
|
|
|
}
|
|
|
|
}
|
2018-06-20 15:06:27 +03:00
|
|
|
transactOpts := bind.NewKeyedTransactor(privkey)
|
|
|
|
dns, err := ens.NewENS(transactOpts, ensRoot, ethClient)
|
2017-12-01 12:32:14 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
log.Debug(fmt.Sprintf("-> Swarm Domain Name Registrar %v @ address %v", endpoint, ensRoot.Hex()))
|
2018-06-20 15:06:27 +03:00
|
|
|
return &ensClient{
|
|
|
|
ENS: dns,
|
|
|
|
Client: ethClient,
|
|
|
|
}, err
|
2017-12-01 12:32:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// detectEnsAddr determines the ENS contract address by getting both the
|
|
|
|
// version and genesis hash using the client and matching them to either
|
|
|
|
// mainnet or testnet addresses
|
|
|
|
func detectEnsAddr(client *rpc.Client) (common.Address, error) {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
var version string
|
|
|
|
if err := client.CallContext(ctx, &version, "net_version"); err != nil {
|
|
|
|
return common.Address{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
block, err := ethclient.NewClient(client).BlockByNumber(ctx, big.NewInt(0))
|
|
|
|
if err != nil {
|
|
|
|
return common.Address{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
|
|
|
|
case version == "1" && block.Hash() == params.MainnetGenesisHash:
|
|
|
|
log.Info("using Mainnet ENS contract address", "addr", ens.MainNetAddress)
|
|
|
|
return ens.MainNetAddress, nil
|
|
|
|
|
|
|
|
case version == "3" && block.Hash() == params.TestnetGenesisHash:
|
|
|
|
log.Info("using Testnet ENS contract address", "addr", ens.TestNetAddress)
|
|
|
|
return ens.TestNetAddress, nil
|
|
|
|
|
|
|
|
default:
|
|
|
|
return common.Address{}, fmt.Errorf("unknown version and genesis hash: %s %s", version, block.Hash())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-29 22:18:00 +03:00
|
|
|
/*
|
|
|
|
Start is called when the stack is started
|
|
|
|
* starts the network kademlia hive peer management
|
|
|
|
* (starts netStore level 0 api)
|
|
|
|
* starts DPA level 1 api (chunking -> store/retrieve requests)
|
|
|
|
* (starts level 2 api)
|
|
|
|
* starts http proxy server
|
|
|
|
* registers url scheme handlers for bzz, etc
|
|
|
|
* TODO: start subservices like sword, swear, swarmdns
|
|
|
|
*/
|
|
|
|
// implements the node.Service interface
|
2019-02-18 18:44:50 +03:00
|
|
|
func (s *Swarm) Start(srv *p2p.Server) error {
|
2019-02-13 10:15:03 +03:00
|
|
|
startTime := time.Now()
|
2018-06-20 15:06:27 +03:00
|
|
|
|
2019-02-18 18:44:50 +03:00
|
|
|
s.tracerClose = tracing.Closer
|
2018-07-13 18:40:28 +03:00
|
|
|
|
2018-06-20 15:06:27 +03:00
|
|
|
// update uaddr to correct enode
|
2019-02-18 18:44:50 +03:00
|
|
|
newaddr := s.bzz.UpdateLocalAddr([]byte(srv.Self().String()))
|
2018-08-21 11:34:10 +03:00
|
|
|
log.Info("Updated bzz local addr", "oaddr", fmt.Sprintf("%x", newaddr.OAddr), "uaddr", fmt.Sprintf("%s", newaddr.UAddr))
|
2016-08-29 22:18:00 +03:00
|
|
|
// set chequebook
|
2018-11-16 01:41:19 +03:00
|
|
|
//TODO: Currently if swap is enabled and no chequebook (or inexistent) contract is provided, the node would crash.
|
|
|
|
//Once we integrate back the contracts, this check MUST be revisited
|
2019-02-18 18:44:50 +03:00
|
|
|
if s.config.SwapEnabled && s.config.SwapAPI != "" {
|
2016-08-29 22:18:00 +03:00
|
|
|
ctx := context.Background() // The initial setup has no deadline.
|
2019-02-18 18:44:50 +03:00
|
|
|
err := s.SetChequebook(ctx)
|
2016-08-29 22:18:00 +03:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Unable to set chequebook for SWAP: %v", err)
|
|
|
|
}
|
2019-02-18 18:44:50 +03:00
|
|
|
log.Debug(fmt.Sprintf("-> cheque book for SWAP: %v", s.config.Swap.Chequebook()))
|
2016-08-29 22:18:00 +03:00
|
|
|
} else {
|
2017-02-22 15:10:07 +03:00
|
|
|
log.Debug(fmt.Sprintf("SWAP disabled: no cheque book set"))
|
2016-08-29 22:18:00 +03:00
|
|
|
}
|
|
|
|
|
2018-08-21 11:34:10 +03:00
|
|
|
log.Info("Starting bzz service")
|
2016-08-29 22:18:00 +03:00
|
|
|
|
2019-02-18 18:44:50 +03:00
|
|
|
err := s.bzz.Start(srv)
|
2018-06-20 15:06:27 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Error("bzz failed", "err", err)
|
|
|
|
return err
|
|
|
|
}
|
2019-02-18 18:44:50 +03:00
|
|
|
log.Info("Swarm network started", "bzzaddr", fmt.Sprintf("%x", s.bzz.Hive.BaseAddr()))
|
2018-06-20 15:06:27 +03:00
|
|
|
|
2019-02-18 18:44:50 +03:00
|
|
|
if s.ps != nil {
|
|
|
|
s.ps.Start(srv)
|
2018-06-20 15:06:27 +03:00
|
|
|
}
|
2016-08-29 22:18:00 +03:00
|
|
|
|
|
|
|
// start swarm http proxy server
|
2019-02-18 18:44:50 +03:00
|
|
|
if s.config.Port != "" {
|
|
|
|
addr := net.JoinHostPort(s.config.ListenAddr, s.config.Port)
|
|
|
|
server := httpapi.NewServer(s.api, s.config.Cors)
|
2018-07-12 16:42:45 +03:00
|
|
|
|
2019-02-18 18:44:50 +03:00
|
|
|
if s.config.Cors != "" {
|
|
|
|
log.Debug("Swarm HTTP proxy CORS headers", "allowedOrigins", s.config.Cors)
|
2018-08-21 11:34:10 +03:00
|
|
|
}
|
2018-06-20 15:06:27 +03:00
|
|
|
|
2019-02-18 18:44:50 +03:00
|
|
|
log.Debug("Starting Swarm HTTP proxy", "port", s.config.Port)
|
2018-08-21 11:34:10 +03:00
|
|
|
go func() {
|
|
|
|
err := server.ListenAndServe(addr)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Could not start Swarm HTTP proxy", "err", err.Error())
|
|
|
|
}
|
|
|
|
}()
|
2017-01-05 13:57:41 +03:00
|
|
|
}
|
|
|
|
|
2019-02-13 10:15:03 +03:00
|
|
|
doneC := make(chan struct{})
|
2018-02-23 16:19:59 +03:00
|
|
|
|
2019-02-18 18:44:50 +03:00
|
|
|
s.cleanupFuncs = append(s.cleanupFuncs, func() error {
|
2019-02-13 10:15:03 +03:00
|
|
|
close(doneC)
|
|
|
|
return nil
|
|
|
|
})
|
2016-08-29 22:18:00 +03:00
|
|
|
|
2019-02-13 10:15:03 +03:00
|
|
|
go func(time.Time) {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-time.After(updateGaugesPeriod):
|
|
|
|
uptimeGauge.Update(time.Since(startTime).Nanoseconds())
|
2019-02-18 18:44:50 +03:00
|
|
|
requestsCacheGauge.Update(int64(s.netStore.RequestsCacheLen()))
|
2019-02-13 10:15:03 +03:00
|
|
|
case <-doneC:
|
|
|
|
return
|
|
|
|
}
|
2018-02-23 16:19:59 +03:00
|
|
|
}
|
2019-02-13 10:15:03 +03:00
|
|
|
}(startTime)
|
2018-02-23 16:19:59 +03:00
|
|
|
|
2019-02-13 10:15:03 +03:00
|
|
|
startCounter.Inc(1)
|
2019-02-18 18:44:50 +03:00
|
|
|
s.streamer.Start(srv)
|
2019-02-13 10:15:03 +03:00
|
|
|
return nil
|
2018-02-23 16:19:59 +03:00
|
|
|
}
|
|
|
|
|
2016-08-29 22:18:00 +03:00
|
|
|
// implements the node.Service interface
|
|
|
|
// stops all component services.
|
2019-02-18 18:44:50 +03:00
|
|
|
func (s *Swarm) Stop() error {
|
|
|
|
if s.tracerClose != nil {
|
|
|
|
err := s.tracerClose.Close()
|
2019-02-20 16:50:37 +03:00
|
|
|
tracing.FinishSpans()
|
2018-07-13 18:40:28 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-18 18:44:50 +03:00
|
|
|
if s.ps != nil {
|
|
|
|
s.ps.Stop()
|
2018-06-20 15:06:27 +03:00
|
|
|
}
|
2019-02-18 18:44:50 +03:00
|
|
|
if ch := s.config.Swap.Chequebook(); ch != nil {
|
2016-08-29 22:18:00 +03:00
|
|
|
ch.Stop()
|
|
|
|
ch.Save()
|
|
|
|
}
|
2019-02-18 18:44:50 +03:00
|
|
|
if s.swap != nil {
|
|
|
|
s.swap.Close()
|
2018-11-26 19:05:18 +03:00
|
|
|
}
|
2019-02-18 18:44:50 +03:00
|
|
|
if s.accountingMetrics != nil {
|
|
|
|
s.accountingMetrics.Close()
|
2018-11-26 19:05:18 +03:00
|
|
|
}
|
2019-02-18 18:44:50 +03:00
|
|
|
if s.netStore != nil {
|
|
|
|
s.netStore.Close()
|
2017-02-08 20:01:12 +03:00
|
|
|
}
|
2019-02-18 18:44:50 +03:00
|
|
|
s.sfs.Stop()
|
2018-02-23 16:19:59 +03:00
|
|
|
stopCounter.Inc(1)
|
2019-02-18 18:44:50 +03:00
|
|
|
s.streamer.Stop()
|
2018-11-26 19:05:18 +03:00
|
|
|
|
2019-02-18 18:44:50 +03:00
|
|
|
err := s.bzz.Stop()
|
|
|
|
if s.stateStore != nil {
|
|
|
|
s.stateStore.Close()
|
2018-11-26 19:05:18 +03:00
|
|
|
}
|
2019-02-13 10:15:03 +03:00
|
|
|
|
2019-02-18 18:44:50 +03:00
|
|
|
for _, cleanF := range s.cleanupFuncs {
|
2019-02-13 10:15:03 +03:00
|
|
|
err = cleanF()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("encountered an error while running cleanup function", "err", err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2018-11-26 19:05:18 +03:00
|
|
|
return err
|
2016-08-29 22:18:00 +03:00
|
|
|
}
|
|
|
|
|
2019-01-24 14:02:18 +03:00
|
|
|
// Protocols implements the node.Service interface
|
|
|
|
func (s *Swarm) Protocols() (protos []p2p.Protocol) {
|
|
|
|
if s.config.BootnodeMode {
|
|
|
|
protos = append(protos, s.bzz.Protocols()...)
|
|
|
|
} else {
|
|
|
|
protos = append(protos, s.bzz.Protocols()...)
|
2018-06-20 15:06:27 +03:00
|
|
|
|
2019-01-24 14:02:18 +03:00
|
|
|
if s.ps != nil {
|
|
|
|
protos = append(protos, s.ps.Protocols()...)
|
|
|
|
}
|
2018-06-20 15:06:27 +03:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-08-29 22:18:00 +03:00
|
|
|
// implements node.Service
|
2018-06-20 15:06:27 +03:00
|
|
|
// APIs returns the RPC API descriptors the Swarm implementation offers
|
2019-02-18 18:44:50 +03:00
|
|
|
func (s *Swarm) APIs() []rpc.API {
|
2018-06-20 15:06:27 +03:00
|
|
|
|
|
|
|
apis := []rpc.API{
|
2016-08-29 22:18:00 +03:00
|
|
|
// public APIs
|
|
|
|
{
|
|
|
|
Namespace: "bzz",
|
2018-06-20 15:06:27 +03:00
|
|
|
Version: "3.0",
|
2019-02-18 18:44:50 +03:00
|
|
|
Service: &Info{s.config, chequebook.ContractParams},
|
2016-08-29 22:18:00 +03:00
|
|
|
Public: true,
|
|
|
|
},
|
|
|
|
// admin APIs
|
|
|
|
{
|
|
|
|
Namespace: "bzz",
|
2018-06-20 15:06:27 +03:00
|
|
|
Version: "3.0",
|
2019-02-18 18:44:50 +03:00
|
|
|
Service: api.NewInspector(s.api, s.bzz.Hive, s.netStore),
|
2016-08-29 22:18:00 +03:00
|
|
|
Public: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Namespace: "chequebook",
|
|
|
|
Version: chequebook.Version,
|
2019-02-18 18:44:50 +03:00
|
|
|
Service: chequebook.NewAPI(s.config.Swap.Chequebook),
|
2016-08-29 22:18:00 +03:00
|
|
|
Public: false,
|
|
|
|
},
|
2017-03-23 16:56:06 +03:00
|
|
|
{
|
|
|
|
Namespace: "swarmfs",
|
2019-02-24 14:39:23 +03:00
|
|
|
Version: fuse.SwarmFSVersion,
|
2019-02-18 18:44:50 +03:00
|
|
|
Service: s.sfs,
|
2017-03-23 16:56:06 +03:00
|
|
|
Public: false,
|
|
|
|
},
|
2018-12-22 08:04:03 +03:00
|
|
|
{
|
|
|
|
Namespace: "accounting",
|
|
|
|
Version: protocols.AccountingVersion,
|
2019-02-18 18:44:50 +03:00
|
|
|
Service: protocols.NewAccountingApi(s.accountingMetrics),
|
2018-12-22 08:04:03 +03:00
|
|
|
Public: false,
|
|
|
|
},
|
2016-08-29 22:18:00 +03:00
|
|
|
}
|
2018-06-20 15:06:27 +03:00
|
|
|
|
2019-02-18 18:44:50 +03:00
|
|
|
apis = append(apis, s.bzz.APIs()...)
|
2018-06-20 15:06:27 +03:00
|
|
|
|
2019-03-20 23:30:34 +03:00
|
|
|
apis = append(apis, s.streamer.APIs()...)
|
|
|
|
|
2019-02-18 18:44:50 +03:00
|
|
|
if s.ps != nil {
|
|
|
|
apis = append(apis, s.ps.APIs()...)
|
2018-06-20 15:06:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return apis
|
2016-08-29 22:18:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetChequebook ensures that the local checquebook is set up on chain.
|
2019-02-18 18:44:50 +03:00
|
|
|
func (s *Swarm) SetChequebook(ctx context.Context) error {
|
|
|
|
err := s.config.Swap.SetChequebook(ctx, s.backend, s.config.Path)
|
2016-08-29 22:18:00 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-18 18:44:50 +03:00
|
|
|
log.Info(fmt.Sprintf("new chequebook set (%v): saving config file, resetting all connections in the hive", s.config.Swap.Contract.Hex()))
|
2016-08-29 22:18:00 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-18 18:44:50 +03:00
|
|
|
// RegisterPssProtocol adds a devp2p protocol to the swarm node's Pss instance
|
|
|
|
func (s *Swarm) RegisterPssProtocol(topic *pss.Topic, spec *protocols.Spec, targetprotocol *p2p.Protocol, options *pss.ProtocolParams) (*pss.Protocol, error) {
|
|
|
|
return pss.RegisterProtocol(s.ps, topic, spec, targetprotocol, options)
|
|
|
|
}
|
|
|
|
|
2016-08-29 22:18:00 +03:00
|
|
|
// serialisable info about swarm
|
|
|
|
type Info struct {
|
|
|
|
*api.Config
|
|
|
|
*chequebook.Params
|
|
|
|
}
|
|
|
|
|
2019-02-18 18:44:50 +03:00
|
|
|
func (s *Info) Info() *Info {
|
|
|
|
return s
|
2016-08-29 22:18:00 +03:00
|
|
|
}
|