github.com/braveheart12/insolar-09-08-19@v0.8.7/network/routing/table.go (about)

     1  /*
     2   * The Clear BSD License
     3   *
     4   * Copyright (c) 2019 Insolar Technologies
     5   *
     6   * All rights reserved.
     7   *
     8   * Redistribution and use in source and binary forms, with or without modification, are permitted (subject to the limitations in the disclaimer below) provided that the following conditions are met:
     9   *
    10   *  Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
    11   *  Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
    12   *  Neither the name of Insolar Technologies nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
    13   *
    14   * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    15   *
    16   */
    17  
    18  package routing
    19  
    20  import (
    21  	"strconv"
    22  
    23  	"github.com/insolar/insolar/core"
    24  	"github.com/insolar/insolar/log"
    25  	"github.com/insolar/insolar/network"
    26  	"github.com/insolar/insolar/network/transport/host"
    27  	"github.com/pkg/errors"
    28  )
    29  
    30  type Table struct {
    31  	NodeKeeper network.NodeKeeper
    32  }
    33  
    34  func (t *Table) ResolveConsensus(id core.ShortNodeID) (*host.Host, error) {
    35  	node := t.NodeKeeper.GetActiveNodeByShortID(id)
    36  	if node != nil {
    37  		return host.NewHostNS(node.ConsensusAddress(), node.ID(), node.ShortID())
    38  	}
    39  	h := t.NodeKeeper.ResolveConsensus(id)
    40  	if h == nil {
    41  		return nil, errors.New("no such local node with ShortID: " + strconv.FormatUint(uint64(id), 10))
    42  	}
    43  	return h, nil
    44  }
    45  
    46  func (t *Table) ResolveConsensusRef(ref core.RecordRef) (*host.Host, error) {
    47  	node := t.NodeKeeper.GetActiveNode(ref)
    48  	if node != nil {
    49  		return host.NewHostNS(node.ConsensusAddress(), node.ID(), node.ShortID())
    50  	}
    51  	h := t.NodeKeeper.ResolveConsensusRef(ref)
    52  	if h == nil {
    53  		return nil, errors.New("no such local node with node ID: " + ref.String())
    54  	}
    55  	return h, nil
    56  }
    57  
    58  func (t *Table) isLocalNode(core.RecordRef) bool {
    59  	return true
    60  }
    61  
    62  func (t *Table) resolveRemoteNode(ref core.RecordRef) (*host.Host, error) {
    63  	return nil, errors.New("not implemented")
    64  }
    65  
    66  func (t *Table) addRemoteHost(h *host.Host) {
    67  	log.Warn("not implemented")
    68  }
    69  
    70  // Resolve NodeID -> ShortID, Address. Can initiate network requests.
    71  func (t *Table) Resolve(ref core.RecordRef) (*host.Host, error) {
    72  	if t.isLocalNode(ref) {
    73  		node := t.NodeKeeper.GetActiveNode(ref)
    74  		if node == nil {
    75  			return nil, errors.New("no such local node with NodeID: " + ref.String())
    76  		}
    77  		return host.NewHostNS(node.Address(), node.ID(), node.ShortID())
    78  	}
    79  	return t.resolveRemoteNode(ref)
    80  }
    81  
    82  // AddToKnownHosts add host to routing table.
    83  func (t *Table) AddToKnownHosts(h *host.Host) {
    84  	if t.isLocalNode(h.NodeID) {
    85  		// we should already have this node in NodeNetwork active list, do nothing
    86  		return
    87  	}
    88  	t.addRemoteHost(h)
    89  }
    90  
    91  // GetRandomNodes get a specified number of random nodes. Returns less if there are not enough nodes in network.
    92  func (t *Table) GetRandomNodes(count int) []host.Host {
    93  	// TODO: this workaround returns all nodes
    94  	nodes := t.NodeKeeper.GetActiveNodes()
    95  	result := make([]host.Host, 0)
    96  	for _, n := range nodes {
    97  		address, err := host.NewAddress(n.Address())
    98  		if err != nil {
    99  			log.Error(err)
   100  			continue
   101  		}
   102  		result = append(result, host.Host{NodeID: n.ID(), Address: address})
   103  	}
   104  
   105  	// TODO: original implementation
   106  	/*
   107  		// not so random for now
   108  		nodes := t.NodeKeeper.GetActiveNodes()
   109  		//return nodes
   110  		resultCount := count
   111  		if count > len(nodes) {
   112  			resultCount = len(nodes)
   113  		}
   114  		result := make([]host.Host, 0)
   115  		for i := 0; i < resultCount; i++ {
   116  			address, err := host.NewAddress(nodes[i].Address())
   117  			if err != nil {
   118  				log.Error(err)
   119  				continue
   120  			}
   121  			h := host.Host{NodeID: nodes[i].ID(), Address: address}
   122  			result = append(result, h)
   123  		}
   124  	*/
   125  	return result
   126  }
   127  
   128  // Rebalance recreate shards of routing table with known hosts according to new partition policy.
   129  func (t *Table) Rebalance(network.PartitionPolicy) {
   130  	log.Warn("not implemented")
   131  }
   132  
   133  func (t *Table) Inject(nodeKeeper network.NodeKeeper) {
   134  	t.NodeKeeper = nodeKeeper
   135  }