2016-08-29 14:18:00 -05:00
|
|
|
// Copyright 2016 The go-ethereum Authors
|
|
|
|
// 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 12:20:33 -05:00
|
|
|
"context"
|
2016-08-29 14:18:00 -05:00
|
|
|
"crypto/ecdsa"
|
|
|
|
"fmt"
|
2018-07-13 10:40:28 -05:00
|
|
|
"io"
|
2017-12-01 03:32:14 -06:00
|
|
|
"math/big"
|
2017-05-22 01:56:40 -05:00
|
|
|
"net"
|
2018-06-20 07:06:27 -05:00
|
|
|
"path/filepath"
|
2017-12-13 03:23:11 -06:00
|
|
|
"strings"
|
2017-12-01 03:32:14 -06:00
|
|
|
"time"
|
2017-12-13 03:23:11 -06:00
|
|
|
"unicode"
|
2016-08-29 14:18:00 -05: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 04:06:19 -05:00
|
|
|
"github.com/ethereum/go-ethereum/ethclient"
|
2018-02-23 07:19:59 -06:00
|
|
|
"github.com/ethereum/go-ethereum/metrics"
|
2016-08-29 14:18:00 -05:00
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p/discover"
|
2018-06-20 07:06:27 -05:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/protocols"
|
2017-12-01 03:32:14 -06:00
|
|
|
"github.com/ethereum/go-ethereum/params"
|
2016-08-29 14:18:00 -05: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-11 19:06:02 -05:00
|
|
|
"github.com/ethereum/go-ethereum/swarm/fuse"
|
2018-06-20 07:06:27 -05:00
|
|
|
"github.com/ethereum/go-ethereum/swarm/log"
|
2016-08-29 14:18:00 -05:00
|
|
|
"github.com/ethereum/go-ethereum/swarm/network"
|
2018-06-20 07:06:27 -05: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 14:18:00 -05:00
|
|
|
"github.com/ethereum/go-ethereum/swarm/storage"
|
2018-06-20 07:06:27 -05:00
|
|
|
"github.com/ethereum/go-ethereum/swarm/storage/mock"
|
|
|
|
"github.com/ethereum/go-ethereum/swarm/storage/mru"
|
2018-07-13 10:40:28 -05:00
|
|
|
"github.com/ethereum/go-ethereum/swarm/tracing"
|
2016-08-29 14:18:00 -05:00
|
|
|
)
|
|
|
|
|
2018-02-23 07:19:59 -06:00
|
|
|
var (
|
|
|
|
startTime time.Time
|
|
|
|
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 07:06:27 -05:00
|
|
|
requestsCacheGauge = metrics.NewRegisteredGauge("storage.cache.requests.size", nil)
|
2018-02-23 07:19:59 -06:00
|
|
|
)
|
|
|
|
|
2016-08-29 14:18:00 -05:00
|
|
|
// the swarm stack
|
|
|
|
type Swarm struct {
|
2018-06-20 07:06:27 -05: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
|
2016-08-29 14:18:00 -05:00
|
|
|
privateKey *ecdsa.PrivateKey
|
2017-01-05 04:57:41 -06:00
|
|
|
corsString string
|
2016-08-29 14:18:00 -05:00
|
|
|
swapEnabled bool
|
2017-04-06 17:22:22 -05:00
|
|
|
lstore *storage.LocalStore // local store, needs to store for releasing resources after node stopped
|
2017-04-11 19:06:02 -05:00
|
|
|
sfs *fuse.SwarmFS // need this to cleanup all the active mounts on node exit
|
2018-06-20 07:06:27 -05:00
|
|
|
ps *pss.Pss
|
2018-07-13 10:40:28 -05:00
|
|
|
|
|
|
|
tracerClose io.Closer
|
2016-08-29 14:18:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type SwarmAPI struct {
|
2018-06-20 07:06:27 -05:00
|
|
|
Api *api.API
|
2016-08-29 14:18:00 -05:00
|
|
|
Backend chequebook.Backend
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *Swarm) API() *SwarmAPI {
|
|
|
|
return &SwarmAPI{
|
|
|
|
Api: self.api,
|
|
|
|
Backend: self.backend,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// creates a new swarm service instance
|
|
|
|
// implements node.Service
|
2018-06-20 07:06:27 -05: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 14:18:00 -05:00
|
|
|
return nil, fmt.Errorf("empty public key")
|
|
|
|
}
|
2018-06-20 07:06:27 -05:00
|
|
|
if bytes.Equal(common.FromHex(config.BzzKey), storage.ZeroAddr) {
|
2016-08-29 14:18:00 -05:00
|
|
|
return nil, fmt.Errorf("empty bzz key")
|
|
|
|
}
|
|
|
|
|
2018-06-20 07:06:27 -05: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 14:18:00 -05:00
|
|
|
}
|
|
|
|
|
2018-06-20 07:06:27 -05:00
|
|
|
self = &Swarm{
|
|
|
|
config: config,
|
|
|
|
backend: backend,
|
|
|
|
privateKey: config.ShiftPrivateKey(),
|
2016-08-29 14:18:00 -05:00
|
|
|
}
|
2018-06-20 07:06:27 -05:00
|
|
|
log.Debug(fmt.Sprintf("Setting up Swarm service components"))
|
2016-08-29 14:18:00 -05:00
|
|
|
|
2018-06-20 07:06:27 -05:00
|
|
|
config.HiveParams.Discovery = true
|
2016-08-29 14:18:00 -05:00
|
|
|
|
2017-02-22 06:10:07 -06:00
|
|
|
log.Debug(fmt.Sprintf("-> swarm net store shared access layer to Swarm Chunk Store"))
|
2016-08-29 14:18:00 -05:00
|
|
|
|
2018-06-20 07:06:27 -05:00
|
|
|
nodeID, err := discover.HexID(config.NodeID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
addr := &network.BzzAddr{
|
|
|
|
OAddr: common.FromHex(config.BzzKey),
|
|
|
|
UAddr: []byte(discover.NewNode(nodeID, net.IP{127, 0, 0, 1}, 30303, 30303).String()),
|
|
|
|
}
|
|
|
|
|
|
|
|
bzzconfig := &network.BzzConfig{
|
|
|
|
NetworkID: config.NetworkID,
|
|
|
|
OverlayAddr: addr.OAddr,
|
|
|
|
UnderlayAddr: addr.UAddr,
|
|
|
|
HiveParams: config.HiveParams,
|
2018-08-07 08:34:11 -05:00
|
|
|
LightNode: config.LightNodeEnabled,
|
2018-06-20 07:06:27 -05:00
|
|
|
}
|
2016-08-29 14:18:00 -05:00
|
|
|
|
2018-06-20 07:06:27 -05:00
|
|
|
stateStore, err := state.NewDBStore(filepath.Join(config.Path, "state-store.db"))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2016-08-29 14:18:00 -05:00
|
|
|
|
2018-06-20 07:06:27 -05:00
|
|
|
// set up high level api
|
|
|
|
var resolver *api.MultiResolver
|
2017-12-18 09:22:39 -06:00
|
|
|
if len(config.EnsAPIs) > 0 {
|
|
|
|
opts := []api.MultiResolverOption{}
|
|
|
|
for _, c := range config.EnsAPIs {
|
|
|
|
tld, endpoint, addr := parseEnsAPIAddress(c)
|
2018-06-20 07:06:27 -05:00
|
|
|
r, err := newEnsClient(endpoint, addr, config, self.privateKey)
|
2017-12-01 03:32:14 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-12-18 09:22:39 -06:00
|
|
|
opts = append(opts, api.MultiResolverOptionWithResolver(r, tld))
|
2018-06-20 07:06:27 -05:00
|
|
|
|
2017-12-01 03:32:14 -06:00
|
|
|
}
|
2018-06-20 07:06:27 -05:00
|
|
|
resolver = api.NewMultiResolver(opts...)
|
|
|
|
self.dns = resolver
|
|
|
|
}
|
|
|
|
|
|
|
|
self.lstore, err = storage.NewLocalStore(config.LocalStoreParams, mockStore)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
db := storage.NewDBAPI(self.lstore)
|
|
|
|
to := network.NewKademlia(
|
|
|
|
common.FromHex(config.BzzKey),
|
|
|
|
network.NewKadParams(),
|
|
|
|
)
|
|
|
|
delivery := stream.NewDelivery(to, db)
|
|
|
|
|
|
|
|
self.streamer = stream.NewRegistry(addr, delivery, db, stateStore, &stream.RegistryOptions{
|
|
|
|
SkipCheck: config.DeliverySkipCheck,
|
|
|
|
DoSync: config.SyncEnabled,
|
|
|
|
DoRetrieve: true,
|
|
|
|
SyncUpdateDelay: config.SyncUpdateDelay,
|
|
|
|
})
|
|
|
|
|
|
|
|
// set up NetStore, the cloud storage local access layer
|
|
|
|
netStore := storage.NewNetStore(self.lstore, self.streamer.Retrieve)
|
|
|
|
// Swarm Hash Merklised Chunking for Arbitrary-length Document/File storage
|
|
|
|
self.fileStore = storage.NewFileStore(netStore, self.config.FileStoreParams)
|
|
|
|
|
|
|
|
var resourceHandler *mru.Handler
|
2018-07-21 14:49:36 -05:00
|
|
|
rhparams := &mru.HandlerParams{}
|
|
|
|
|
2018-08-14 09:03:56 -05:00
|
|
|
resourceHandler = mru.NewHandler(rhparams)
|
2018-06-20 07:06:27 -05:00
|
|
|
resourceHandler.SetStore(netStore)
|
|
|
|
|
2018-08-14 09:03:56 -05:00
|
|
|
self.lstore.Validators = []storage.ChunkValidator{
|
|
|
|
storage.NewContentAddressValidator(storage.MakeHashFunc(storage.DefaultHash)),
|
|
|
|
resourceHandler,
|
2018-06-20 07:06:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// setup local store
|
|
|
|
log.Debug(fmt.Sprintf("Set up local storage"))
|
|
|
|
|
|
|
|
self.bzz = network.NewBzz(bzzconfig, to, stateStore, stream.Spec, self.streamer.Run)
|
|
|
|
|
|
|
|
// 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 14:18:00 -05:00
|
|
|
}
|
|
|
|
|
2018-08-15 10:41:52 -05:00
|
|
|
self.api = api.NewAPI(self.fileStore, self.dns, resourceHandler, self.privateKey)
|
2016-08-29 14:18:00 -05:00
|
|
|
// Manifests for Smart Hosting
|
2017-02-22 06:10:07 -06:00
|
|
|
log.Debug(fmt.Sprintf("-> Web3 virtual server API"))
|
2016-08-29 14:18:00 -05:00
|
|
|
|
2017-04-11 19:06:02 -05:00
|
|
|
self.sfs = fuse.NewSwarmFS(self.api)
|
2017-03-23 08:56:06 -05:00
|
|
|
log.Debug("-> Initializing Fuse file system")
|
|
|
|
|
2016-08-29 14:18:00 -05:00
|
|
|
return self, nil
|
|
|
|
}
|
|
|
|
|
2017-12-13 03:23:11 -06: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 03:32:14 -06:00
|
|
|
}
|
|
|
|
|
2018-06-20 07:06:27 -05:00
|
|
|
// ensClient provides functionality for api.ResolveValidator
|
|
|
|
type ensClient struct {
|
|
|
|
*ens.ENS
|
|
|
|
*ethclient.Client
|
|
|
|
}
|
|
|
|
|
2017-12-01 03:32:14 -06: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 07:06:27 -05:00
|
|
|
func newEnsClient(endpoint string, addr common.Address, config *api.Config, privkey *ecdsa.PrivateKey) (*ensClient, error) {
|
2017-12-01 03:32:14 -06: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 07:06:27 -05:00
|
|
|
ethClient := ethclient.NewClient(client)
|
2017-12-01 03:32:14 -06:00
|
|
|
|
|
|
|
ensRoot := config.EnsRoot
|
2017-12-13 03:23:11 -06:00
|
|
|
if addr != (common.Address{}) {
|
|
|
|
ensRoot = addr
|
2017-12-01 03:32:14 -06: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 07:06:27 -05:00
|
|
|
transactOpts := bind.NewKeyedTransactor(privkey)
|
|
|
|
dns, err := ens.NewENS(transactOpts, ensRoot, ethClient)
|
2017-12-01 03:32:14 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
log.Debug(fmt.Sprintf("-> Swarm Domain Name Registrar %v @ address %v", endpoint, ensRoot.Hex()))
|
2018-06-20 07:06:27 -05:00
|
|
|
return &ensClient{
|
|
|
|
ENS: dns,
|
|
|
|
Client: ethClient,
|
|
|
|
}, err
|
2017-12-01 03:32:14 -06: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 14:18:00 -05: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
|
2017-05-22 01:56:40 -05:00
|
|
|
func (self *Swarm) Start(srv *p2p.Server) error {
|
2018-02-23 07:19:59 -06:00
|
|
|
startTime = time.Now()
|
2018-06-20 07:06:27 -05:00
|
|
|
|
2018-07-13 10:40:28 -05:00
|
|
|
self.tracerClose = tracing.Closer
|
|
|
|
|
2018-06-20 07:06:27 -05:00
|
|
|
// update uaddr to correct enode
|
|
|
|
newaddr := self.bzz.UpdateLocalAddr([]byte(srv.Self().String()))
|
|
|
|
log.Warn("Updated bzz local addr", "oaddr", fmt.Sprintf("%x", newaddr.OAddr), "uaddr", fmt.Sprintf("%s", newaddr.UAddr))
|
2016-08-29 14:18:00 -05:00
|
|
|
// set chequebook
|
2018-06-20 07:06:27 -05:00
|
|
|
if self.config.SwapEnabled {
|
2016-08-29 14:18:00 -05:00
|
|
|
ctx := context.Background() // The initial setup has no deadline.
|
|
|
|
err := self.SetChequebook(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Unable to set chequebook for SWAP: %v", err)
|
|
|
|
}
|
2017-02-22 06:10:07 -06:00
|
|
|
log.Debug(fmt.Sprintf("-> cheque book for SWAP: %v", self.config.Swap.Chequebook()))
|
2016-08-29 14:18:00 -05:00
|
|
|
} else {
|
2017-02-22 06:10:07 -06:00
|
|
|
log.Debug(fmt.Sprintf("SWAP disabled: no cheque book set"))
|
2016-08-29 14:18:00 -05:00
|
|
|
}
|
|
|
|
|
2017-02-22 06:10:07 -06:00
|
|
|
log.Warn(fmt.Sprintf("Starting Swarm service"))
|
2016-08-29 14:18:00 -05:00
|
|
|
|
2018-06-20 07:06:27 -05:00
|
|
|
err := self.bzz.Start(srv)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("bzz failed", "err", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Info(fmt.Sprintf("Swarm network started on bzz address: %x", self.bzz.Hive.Overlay.BaseAddr()))
|
|
|
|
|
|
|
|
if self.ps != nil {
|
|
|
|
self.ps.Start(srv)
|
|
|
|
log.Info("Pss started")
|
|
|
|
}
|
2016-08-29 14:18:00 -05:00
|
|
|
|
|
|
|
// start swarm http proxy server
|
|
|
|
if self.config.Port != "" {
|
2017-05-22 01:56:40 -05:00
|
|
|
addr := net.JoinHostPort(self.config.ListenAddr, self.config.Port)
|
2018-07-12 08:42:45 -05:00
|
|
|
server := httpapi.NewServer(self.api, self.config.Cors)
|
|
|
|
|
|
|
|
go server.ListenAndServe(addr)
|
2018-06-20 07:06:27 -05:00
|
|
|
}
|
2016-08-29 14:18:00 -05:00
|
|
|
|
2018-06-20 07:06:27 -05:00
|
|
|
log.Debug(fmt.Sprintf("Swarm http proxy started on port: %v", self.config.Port))
|
|
|
|
|
|
|
|
if self.config.Cors != "" {
|
|
|
|
log.Debug(fmt.Sprintf("Swarm http proxy started with corsdomain: %v", self.config.Cors))
|
2017-01-05 04:57:41 -06:00
|
|
|
}
|
|
|
|
|
2018-02-23 07:19:59 -06:00
|
|
|
self.periodicallyUpdateGauges()
|
|
|
|
|
|
|
|
startCounter.Inc(1)
|
2018-06-20 07:06:27 -05:00
|
|
|
self.streamer.Start(srv)
|
2016-08-29 14:18:00 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-02-23 07:19:59 -06:00
|
|
|
func (self *Swarm) periodicallyUpdateGauges() {
|
|
|
|
ticker := time.NewTicker(updateGaugesPeriod)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for range ticker.C {
|
|
|
|
self.updateGauges()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *Swarm) updateGauges() {
|
|
|
|
uptimeGauge.Update(time.Since(startTime).Nanoseconds())
|
2018-06-20 07:06:27 -05:00
|
|
|
requestsCacheGauge.Update(int64(self.lstore.RequestsCacheLen()))
|
2018-02-23 07:19:59 -06:00
|
|
|
}
|
|
|
|
|
2016-08-29 14:18:00 -05:00
|
|
|
// implements the node.Service interface
|
|
|
|
// stops all component services.
|
|
|
|
func (self *Swarm) Stop() error {
|
2018-07-13 10:40:28 -05:00
|
|
|
if self.tracerClose != nil {
|
|
|
|
err := self.tracerClose.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-20 07:06:27 -05:00
|
|
|
if self.ps != nil {
|
|
|
|
self.ps.Stop()
|
|
|
|
}
|
2016-08-29 14:18:00 -05:00
|
|
|
if ch := self.config.Swap.Chequebook(); ch != nil {
|
|
|
|
ch.Stop()
|
|
|
|
ch.Save()
|
|
|
|
}
|
2017-02-08 11:01:12 -06:00
|
|
|
|
|
|
|
if self.lstore != nil {
|
|
|
|
self.lstore.DbStore.Close()
|
|
|
|
}
|
2017-03-23 08:56:06 -05:00
|
|
|
self.sfs.Stop()
|
2018-02-23 07:19:59 -06:00
|
|
|
stopCounter.Inc(1)
|
2018-06-20 07:06:27 -05:00
|
|
|
self.streamer.Stop()
|
|
|
|
return self.bzz.Stop()
|
2016-08-29 14:18:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// implements the node.Service interface
|
2018-06-20 07:06:27 -05:00
|
|
|
func (self *Swarm) Protocols() (protos []p2p.Protocol) {
|
|
|
|
protos = append(protos, self.bzz.Protocols()...)
|
|
|
|
|
|
|
|
if self.ps != nil {
|
|
|
|
protos = append(protos, self.ps.Protocols()...)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *Swarm) RegisterPssProtocol(spec *protocols.Spec, targetprotocol *p2p.Protocol, options *pss.ProtocolParams) (*pss.Protocol, error) {
|
|
|
|
if !pss.IsActiveProtocol {
|
|
|
|
return nil, fmt.Errorf("Pss protocols not available (built with !nopssprotocol tag)")
|
2016-08-29 14:18:00 -05:00
|
|
|
}
|
2018-06-20 07:06:27 -05:00
|
|
|
topic := pss.ProtocolTopic(spec)
|
|
|
|
return pss.RegisterProtocol(self.ps, &topic, spec, targetprotocol, options)
|
2016-08-29 14:18:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// implements node.Service
|
2018-06-20 07:06:27 -05:00
|
|
|
// APIs returns the RPC API descriptors the Swarm implementation offers
|
2016-08-29 14:18:00 -05:00
|
|
|
func (self *Swarm) APIs() []rpc.API {
|
2018-06-20 07:06:27 -05:00
|
|
|
|
|
|
|
apis := []rpc.API{
|
2016-08-29 14:18:00 -05:00
|
|
|
// public APIs
|
|
|
|
{
|
|
|
|
Namespace: "bzz",
|
2018-06-20 07:06:27 -05:00
|
|
|
Version: "3.0",
|
2016-08-29 14:18:00 -05:00
|
|
|
Service: &Info{self.config, chequebook.ContractParams},
|
|
|
|
Public: true,
|
|
|
|
},
|
|
|
|
// admin APIs
|
|
|
|
{
|
|
|
|
Namespace: "bzz",
|
2018-06-20 07:06:27 -05:00
|
|
|
Version: "3.0",
|
|
|
|
Service: api.NewControl(self.api, self.bzz.Hive),
|
2016-08-29 14:18:00 -05:00
|
|
|
Public: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Namespace: "chequebook",
|
|
|
|
Version: chequebook.Version,
|
|
|
|
Service: chequebook.NewApi(self.config.Swap.Chequebook),
|
|
|
|
Public: false,
|
|
|
|
},
|
2017-03-23 08:56:06 -05:00
|
|
|
{
|
|
|
|
Namespace: "swarmfs",
|
2017-04-11 19:06:02 -05:00
|
|
|
Version: fuse.Swarmfs_Version,
|
2017-03-23 08:56:06 -05:00
|
|
|
Service: self.sfs,
|
|
|
|
Public: false,
|
|
|
|
},
|
2017-04-06 17:22:22 -05:00
|
|
|
// storage APIs
|
|
|
|
// DEPRECATED: Use the HTTP API instead
|
|
|
|
{
|
|
|
|
Namespace: "bzz",
|
|
|
|
Version: "0.1",
|
|
|
|
Service: api.NewStorage(self.api),
|
|
|
|
Public: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Namespace: "bzz",
|
|
|
|
Version: "0.1",
|
|
|
|
Service: api.NewFileSystem(self.api),
|
|
|
|
Public: false,
|
|
|
|
},
|
2016-08-29 14:18:00 -05:00
|
|
|
// {Namespace, Version, api.NewAdmin(self), false},
|
|
|
|
}
|
2018-06-20 07:06:27 -05:00
|
|
|
|
|
|
|
apis = append(apis, self.bzz.APIs()...)
|
|
|
|
|
|
|
|
if self.ps != nil {
|
|
|
|
apis = append(apis, self.ps.APIs()...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return apis
|
2016-08-29 14:18:00 -05:00
|
|
|
}
|
|
|
|
|
2018-06-20 07:06:27 -05:00
|
|
|
func (self *Swarm) Api() *api.API {
|
2016-08-29 14:18:00 -05:00
|
|
|
return self.api
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetChequebook ensures that the local checquebook is set up on chain.
|
|
|
|
func (self *Swarm) SetChequebook(ctx context.Context) error {
|
|
|
|
err := self.config.Swap.SetChequebook(ctx, self.backend, self.config.Path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-02-22 06:10:07 -06:00
|
|
|
log.Info(fmt.Sprintf("new chequebook set (%v): saving config file, resetting all connections in the hive", self.config.Swap.Contract.Hex()))
|
2016-08-29 14:18:00 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// serialisable info about swarm
|
|
|
|
type Info struct {
|
|
|
|
*api.Config
|
|
|
|
*chequebook.Params
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *Info) Info() *Info {
|
|
|
|
return self
|
|
|
|
}
|