github.com/vccomnet/occchain@v0.0.0-20181129092339-c57d4bab23fb/swarm/swarm.go (about)

     1  // Copyright 2016 The go-blockchain Authors
     2  // This file is part of the go-blockchain library.
     3  //
     4  // The go-blockchain library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-blockchain library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-blockchain library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package swarm
    18  
    19  import (
    20  	"bytes"
    21  	"context"
    22  	"crypto/ecdsa"
    23  	"fmt"
    24  	"io"
    25  	"math/big"
    26  	"net"
    27  	"path/filepath"
    28  	"strings"
    29  	"time"
    30  	"unicode"
    31  
    32  	"github.com/blockchain/go-blockchain/accounts/abi/bind"
    33  	"github.com/blockchain/go-blockchain/common"
    34  	"github.com/blockchain/go-blockchain/contracts/chequebook"
    35  	"github.com/blockchain/go-blockchain/contracts/ens"
    36  	"github.com/blockchain/go-blockchain/ethclient"
    37  	"github.com/blockchain/go-blockchain/metrics"
    38  	"github.com/blockchain/go-blockchain/p2p"
    39  	"github.com/blockchain/go-blockchain/p2p/enode"
    40  	"github.com/blockchain/go-blockchain/p2p/protocols"
    41  	"github.com/blockchain/go-blockchain/params"
    42  	"github.com/blockchain/go-blockchain/rpc"
    43  	"github.com/blockchain/go-blockchain/swarm/api"
    44  	httpapi "github.com/blockchain/go-blockchain/swarm/api/http"
    45  	"github.com/blockchain/go-blockchain/swarm/fuse"
    46  	"github.com/blockchain/go-blockchain/swarm/log"
    47  	"github.com/blockchain/go-blockchain/swarm/network"
    48  	"github.com/blockchain/go-blockchain/swarm/network/stream"
    49  	"github.com/blockchain/go-blockchain/swarm/pss"
    50  	"github.com/blockchain/go-blockchain/swarm/state"
    51  	"github.com/blockchain/go-blockchain/swarm/storage"
    52  	"github.com/blockchain/go-blockchain/swarm/storage/feed"
    53  	"github.com/blockchain/go-blockchain/swarm/storage/mock"
    54  	"github.com/blockchain/go-blockchain/swarm/tracing"
    55  )
    56  
    57  var (
    58  	startTime          time.Time
    59  	updateGaugesPeriod = 5 * time.Second
    60  	startCounter       = metrics.NewRegisteredCounter("stack,start", nil)
    61  	stopCounter        = metrics.NewRegisteredCounter("stack,stop", nil)
    62  	uptimeGauge        = metrics.NewRegisteredGauge("stack.uptime", nil)
    63  	requestsCacheGauge = metrics.NewRegisteredGauge("storage.cache.requests.size", nil)
    64  )
    65  
    66  // the swarm stack
    67  type Swarm struct {
    68  	config      *api.Config        // swarm configuration
    69  	api         *api.API           // high level api layer (fs/manifest)
    70  	dns         api.Resolver       // DNS registrar
    71  	fileStore   *storage.FileStore // distributed preimage archive, the local API to the storage with document level storage/retrieval support
    72  	streamer    *stream.Registry
    73  	bzz         *network.Bzz       // the logistic manager
    74  	backend     chequebook.Backend // simple blockchain Backend
    75  	privateKey  *ecdsa.PrivateKey
    76  	corsString  string
    77  	swapEnabled bool
    78  	netStore    *storage.NetStore
    79  	sfs         *fuse.SwarmFS // need this to cleanup all the active mounts on node exit
    80  	ps          *pss.Pss
    81  
    82  	tracerClose io.Closer
    83  }
    84  
    85  type SwarmAPI struct {
    86  	Api     *api.API
    87  	Backend chequebook.Backend
    88  }
    89  
    90  func (self *Swarm) API() *SwarmAPI {
    91  	return &SwarmAPI{
    92  		Api:     self.api,
    93  		Backend: self.backend,
    94  	}
    95  }
    96  
    97  // creates a new swarm service instance
    98  // implements node.Service
    99  // If mockStore is not nil, it will be used as the storage for chunk data.
   100  // MockStore should be used only for testing.
   101  func NewSwarm(config *api.Config, mockStore *mock.NodeStore) (self *Swarm, err error) {
   102  
   103  	if bytes.Equal(common.FromHex(config.PublicKey), storage.ZeroAddr) {
   104  		return nil, fmt.Errorf("empty public key")
   105  	}
   106  	if bytes.Equal(common.FromHex(config.BzzKey), storage.ZeroAddr) {
   107  		return nil, fmt.Errorf("empty bzz key")
   108  	}
   109  
   110  	var backend chequebook.Backend
   111  	if config.SwapAPI != "" && config.SwapEnabled {
   112  		log.Info("connecting to SWAP API", "url", config.SwapAPI)
   113  		backend, err = ethclient.Dial(config.SwapAPI)
   114  		if err != nil {
   115  			return nil, fmt.Errorf("error connecting to SWAP API %s: %s", config.SwapAPI, err)
   116  		}
   117  	}
   118  
   119  	self = &Swarm{
   120  		config:     config,
   121  		backend:    backend,
   122  		privateKey: config.ShiftPrivateKey(),
   123  	}
   124  	log.Debug("Setting up Swarm service components")
   125  
   126  	config.HiveParams.Discovery = true
   127  
   128  	bzzconfig := &network.BzzConfig{
   129  		NetworkID:   config.NetworkID,
   130  		OverlayAddr: common.FromHex(config.BzzKey),
   131  		HiveParams:  config.HiveParams,
   132  		LightNode:   config.LightNodeEnabled,
   133  	}
   134  
   135  	stateStore, err := state.NewDBStore(filepath.Join(config.Path, "state-store.db"))
   136  	if err != nil {
   137  		return
   138  	}
   139  
   140  	// set up high level api
   141  	var resolver *api.MultiResolver
   142  	if len(config.EnsAPIs) > 0 {
   143  		opts := []api.MultiResolverOption{}
   144  		for _, c := range config.EnsAPIs {
   145  			tld, endpoint, addr := parseEnsAPIAddress(c)
   146  			r, err := newEnsClient(endpoint, addr, config, self.privateKey)
   147  			if err != nil {
   148  				return nil, err
   149  			}
   150  			opts = append(opts, api.MultiResolverOptionWithResolver(r, tld))
   151  
   152  		}
   153  		resolver = api.NewMultiResolver(opts...)
   154  		self.dns = resolver
   155  	}
   156  
   157  	lstore, err := storage.NewLocalStore(config.LocalStoreParams, mockStore)
   158  	if err != nil {
   159  		return nil, err
   160  	}
   161  
   162  	self.netStore, err = storage.NewNetStore(lstore, nil)
   163  	if err != nil {
   164  		return nil, err
   165  	}
   166  
   167  	to := network.NewKademlia(
   168  		common.FromHex(config.BzzKey),
   169  		network.NewKadParams(),
   170  	)
   171  	delivery := stream.NewDelivery(to, self.netStore)
   172  	self.netStore.NewNetFetcherFunc = network.NewFetcherFactory(delivery.RequestFromPeers, config.DeliverySkipCheck).New
   173  
   174  	var nodeID enode.ID
   175  	if err := nodeID.UnmarshalText([]byte(config.NodeID)); err != nil {
   176  		return nil, err
   177  	}
   178  
   179  	syncing := stream.SyncingAutoSubscribe
   180  	if !config.SyncEnabled || config.LightNodeEnabled {
   181  		syncing = stream.SyncingDisabled
   182  	}
   183  
   184  	retrieval := stream.RetrievalEnabled
   185  	if config.LightNodeEnabled {
   186  		retrieval = stream.RetrievalClientOnly
   187  	}
   188  
   189  	registryOptions := &stream.RegistryOptions{
   190  		SkipCheck:       config.DeliverySkipCheck,
   191  		Syncing:         syncing,
   192  		Retrieval:       retrieval,
   193  		SyncUpdateDelay: config.SyncUpdateDelay,
   194  		MaxPeerServers:  config.MaxStreamPeerServers,
   195  	}
   196  	self.streamer = stream.NewRegistry(nodeID, delivery, self.netStore, stateStore, registryOptions)
   197  
   198  	// Swarm Hash Merklised Chunking for Arbitrary-length Document/File storage
   199  	self.fileStore = storage.NewFileStore(self.netStore, self.config.FileStoreParams)
   200  
   201  	var feedsHandler *feed.Handler
   202  	fhParams := &feed.HandlerParams{}
   203  
   204  	feedsHandler = feed.NewHandler(fhParams)
   205  	feedsHandler.SetStore(self.netStore)
   206  
   207  	lstore.Validators = []storage.ChunkValidator{
   208  		storage.NewContentAddressValidator(storage.MakeHashFunc(storage.DefaultHash)),
   209  		feedsHandler,
   210  	}
   211  
   212  	err = lstore.Migrate()
   213  	if err != nil {
   214  		return nil, err
   215  	}
   216  
   217  	log.Debug("Setup local storage")
   218  
   219  	self.bzz = network.NewBzz(bzzconfig, to, stateStore, stream.Spec, self.streamer.Run)
   220  
   221  	// Pss = postal service over swarm (devp2p over bzz)
   222  	self.ps, err = pss.NewPss(to, config.Pss)
   223  	if err != nil {
   224  		return nil, err
   225  	}
   226  	if pss.IsActiveHandshake {
   227  		pss.SetHandshakeController(self.ps, pss.NewHandshakeParams())
   228  	}
   229  
   230  	self.api = api.NewAPI(self.fileStore, self.dns, feedsHandler, self.privateKey)
   231  
   232  	self.sfs = fuse.NewSwarmFS(self.api)
   233  	log.Debug("Initialized FUSE filesystem")
   234  
   235  	return self, nil
   236  }
   237  
   238  // parseEnsAPIAddress parses string according to format
   239  // [tld:][contract-addr@]url and returns ENSClientConfig structure
   240  // with endpoint, contract address and TLD.
   241  func parseEnsAPIAddress(s string) (tld, endpoint string, addr common.Address) {
   242  	isAllLetterString := func(s string) bool {
   243  		for _, r := range s {
   244  			if !unicode.IsLetter(r) {
   245  				return false
   246  			}
   247  		}
   248  		return true
   249  	}
   250  	endpoint = s
   251  	if i := strings.Index(endpoint, ":"); i > 0 {
   252  		if isAllLetterString(endpoint[:i]) && len(endpoint) > i+2 && endpoint[i+1:i+3] != "//" {
   253  			tld = endpoint[:i]
   254  			endpoint = endpoint[i+1:]
   255  		}
   256  	}
   257  	if i := strings.Index(endpoint, "@"); i > 0 {
   258  		addr = common.HexToAddress(endpoint[:i])
   259  		endpoint = endpoint[i+1:]
   260  	}
   261  	return
   262  }
   263  
   264  // ensClient provides functionality for api.ResolveValidator
   265  type ensClient struct {
   266  	*ens.ENS
   267  	*ethclient.Client
   268  }
   269  
   270  // newEnsClient creates a new ENS client for that is a consumer of
   271  // a ENS API on a specific endpoint. It is used as a helper function
   272  // for creating multiple resolvers in NewSwarm function.
   273  func newEnsClient(endpoint string, addr common.Address, config *api.Config, privkey *ecdsa.PrivateKey) (*ensClient, error) {
   274  	log.Info("connecting to ENS API", "url", endpoint)
   275  	client, err := rpc.Dial(endpoint)
   276  	if err != nil {
   277  		return nil, fmt.Errorf("error connecting to ENS API %s: %s", endpoint, err)
   278  	}
   279  	ethClient := ethclient.NewClient(client)
   280  
   281  	ensRoot := config.EnsRoot
   282  	if addr != (common.Address{}) {
   283  		ensRoot = addr
   284  	} else {
   285  		a, err := detectEnsAddr(client)
   286  		if err == nil {
   287  			ensRoot = a
   288  		} else {
   289  			log.Warn(fmt.Sprintf("could not determine ENS contract address, using default %s", ensRoot), "err", err)
   290  		}
   291  	}
   292  	transactOpts := bind.NewKeyedTransactor(privkey)
   293  	dns, err := ens.NewENS(transactOpts, ensRoot, ethClient)
   294  	if err != nil {
   295  		return nil, err
   296  	}
   297  	log.Debug(fmt.Sprintf("-> Swarm Domain Name Registrar %v @ address %v", endpoint, ensRoot.Hex()))
   298  	return &ensClient{
   299  		ENS:    dns,
   300  		Client: ethClient,
   301  	}, err
   302  }
   303  
   304  // detectEnsAddr determines the ENS contract address by getting both the
   305  // version and genesis hash using the client and matching them to either
   306  // mainnet or testnet addresses
   307  func detectEnsAddr(client *rpc.Client) (common.Address, error) {
   308  	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
   309  	defer cancel()
   310  
   311  	var version string
   312  	if err := client.CallContext(ctx, &version, "net_version"); err != nil {
   313  		return common.Address{}, err
   314  	}
   315  
   316  	block, err := ethclient.NewClient(client).BlockByNumber(ctx, big.NewInt(0))
   317  	if err != nil {
   318  		return common.Address{}, err
   319  	}
   320  
   321  	switch {
   322  
   323  	case version == "1" && block.Hash() == params.MainnetGenesisHash:
   324  		log.Info("using Mainnet ENS contract address", "addr", ens.MainNetAddress)
   325  		return ens.MainNetAddress, nil
   326  
   327  	case version == "3" && block.Hash() == params.TestnetGenesisHash:
   328  		log.Info("using Testnet ENS contract address", "addr", ens.TestNetAddress)
   329  		return ens.TestNetAddress, nil
   330  
   331  	default:
   332  		return common.Address{}, fmt.Errorf("unknown version and genesis hash: %s %s", version, block.Hash())
   333  	}
   334  }
   335  
   336  /*
   337  Start is called when the stack is started
   338  * starts the network kademlia hive peer management
   339  * (starts netStore level 0 api)
   340  * starts DPA level 1 api (chunking -> store/retrieve requests)
   341  * (starts level 2 api)
   342  * starts http proxy server
   343  * registers url scheme handlers for bzz, etc
   344  * TODO: start subservices like sword, swear, swarmdns
   345  */
   346  // implements the node.Service interface
   347  func (self *Swarm) Start(srv *p2p.Server) error {
   348  	startTime = time.Now()
   349  
   350  	self.tracerClose = tracing.Closer
   351  
   352  	// update uaddr to correct enode
   353  	newaddr := self.bzz.UpdateLocalAddr([]byte(srv.Self().String()))
   354  	log.Info("Updated bzz local addr", "oaddr", fmt.Sprintf("%x", newaddr.OAddr), "uaddr", fmt.Sprintf("%s", newaddr.UAddr))
   355  	// set chequebook
   356  	if self.config.SwapEnabled {
   357  		ctx := context.Background() // The initial setup has no deadline.
   358  		err := self.SetChequebook(ctx)
   359  		if err != nil {
   360  			return fmt.Errorf("Unable to set chequebook for SWAP: %v", err)
   361  		}
   362  		log.Debug(fmt.Sprintf("-> cheque book for SWAP: %v", self.config.Swap.Chequebook()))
   363  	} else {
   364  		log.Debug(fmt.Sprintf("SWAP disabled: no cheque book set"))
   365  	}
   366  
   367  	log.Info("Starting bzz service")
   368  
   369  	err := self.bzz.Start(srv)
   370  	if err != nil {
   371  		log.Error("bzz failed", "err", err)
   372  		return err
   373  	}
   374  	log.Info("Swarm network started", "bzzaddr", fmt.Sprintf("%x", self.bzz.Hive.BaseAddr()))
   375  
   376  	if self.ps != nil {
   377  		self.ps.Start(srv)
   378  	}
   379  
   380  	// start swarm http proxy server
   381  	if self.config.Port != "" {
   382  		addr := net.JoinHostPort(self.config.ListenAddr, self.config.Port)
   383  		server := httpapi.NewServer(self.api, self.config.Cors)
   384  
   385  		if self.config.Cors != "" {
   386  			log.Debug("Swarm HTTP proxy CORS headers", "allowedOrigins", self.config.Cors)
   387  		}
   388  
   389  		log.Debug("Starting Swarm HTTP proxy", "port", self.config.Port)
   390  		go func() {
   391  			err := server.ListenAndServe(addr)
   392  			if err != nil {
   393  				log.Error("Could not start Swarm HTTP proxy", "err", err.Error())
   394  			}
   395  		}()
   396  	}
   397  
   398  	self.periodicallyUpdateGauges()
   399  
   400  	startCounter.Inc(1)
   401  	self.streamer.Start(srv)
   402  	return nil
   403  }
   404  
   405  func (self *Swarm) periodicallyUpdateGauges() {
   406  	ticker := time.NewTicker(updateGaugesPeriod)
   407  
   408  	go func() {
   409  		for range ticker.C {
   410  			self.updateGauges()
   411  		}
   412  	}()
   413  }
   414  
   415  func (self *Swarm) updateGauges() {
   416  	uptimeGauge.Update(time.Since(startTime).Nanoseconds())
   417  	requestsCacheGauge.Update(int64(self.netStore.RequestsCacheLen()))
   418  }
   419  
   420  // implements the node.Service interface
   421  // stops all component services.
   422  func (self *Swarm) Stop() error {
   423  	if self.tracerClose != nil {
   424  		err := self.tracerClose.Close()
   425  		if err != nil {
   426  			return err
   427  		}
   428  	}
   429  
   430  	if self.ps != nil {
   431  		self.ps.Stop()
   432  	}
   433  	if ch := self.config.Swap.Chequebook(); ch != nil {
   434  		ch.Stop()
   435  		ch.Save()
   436  	}
   437  
   438  	if self.netStore != nil {
   439  		self.netStore.Close()
   440  	}
   441  	self.sfs.Stop()
   442  	stopCounter.Inc(1)
   443  	self.streamer.Stop()
   444  	return self.bzz.Stop()
   445  }
   446  
   447  // implements the node.Service interface
   448  func (self *Swarm) Protocols() (protos []p2p.Protocol) {
   449  	protos = append(protos, self.bzz.Protocols()...)
   450  
   451  	if self.ps != nil {
   452  		protos = append(protos, self.ps.Protocols()...)
   453  	}
   454  	return
   455  }
   456  
   457  func (self *Swarm) RegisterPssProtocol(spec *protocols.Spec, targetprotocol *p2p.Protocol, options *pss.ProtocolParams) (*pss.Protocol, error) {
   458  	if !pss.IsActiveProtocol {
   459  		return nil, fmt.Errorf("Pss protocols not available (built with !nopssprotocol tag)")
   460  	}
   461  	topic := pss.ProtocolTopic(spec)
   462  	return pss.RegisterProtocol(self.ps, &topic, spec, targetprotocol, options)
   463  }
   464  
   465  // implements node.Service
   466  // APIs returns the RPC API descriptors the Swarm implementation offers
   467  func (self *Swarm) APIs() []rpc.API {
   468  
   469  	apis := []rpc.API{
   470  		// public APIs
   471  		{
   472  			Namespace: "bzz",
   473  			Version:   "3.0",
   474  			Service:   &Info{self.config, chequebook.ContractParams},
   475  			Public:    true,
   476  		},
   477  		// admin APIs
   478  		{
   479  			Namespace: "bzz",
   480  			Version:   "3.0",
   481  			Service:   api.NewControl(self.api, self.bzz.Hive),
   482  			Public:    false,
   483  		},
   484  		{
   485  			Namespace: "chequebook",
   486  			Version:   chequebook.Version,
   487  			Service:   chequebook.NewApi(self.config.Swap.Chequebook),
   488  			Public:    false,
   489  		},
   490  		{
   491  			Namespace: "swarmfs",
   492  			Version:   fuse.Swarmfs_Version,
   493  			Service:   self.sfs,
   494  			Public:    false,
   495  		},
   496  	}
   497  
   498  	apis = append(apis, self.bzz.APIs()...)
   499  
   500  	if self.ps != nil {
   501  		apis = append(apis, self.ps.APIs()...)
   502  	}
   503  
   504  	return apis
   505  }
   506  
   507  func (self *Swarm) Api() *api.API {
   508  	return self.api
   509  }
   510  
   511  // SetChequebook ensures that the local checquebook is set up on chain.
   512  func (self *Swarm) SetChequebook(ctx context.Context) error {
   513  	err := self.config.Swap.SetChequebook(ctx, self.backend, self.config.Path)
   514  	if err != nil {
   515  		return err
   516  	}
   517  	log.Info(fmt.Sprintf("new chequebook set (%v): saving config file, resetting all connections in the hive", self.config.Swap.Contract.Hex()))
   518  	return nil
   519  }
   520  
   521  // serialisable info about swarm
   522  type Info struct {
   523  	*api.Config
   524  	*chequebook.Params
   525  }
   526  
   527  func (self *Info) Info() *Info {
   528  	return self
   529  }