Skip to content
Snippets Groups Projects
Select Git revision
  • 247b38d50cb773aae9eb5bb8d717e3c662ae3c9a
  • without_tipselection default
  • develop protected
  • fix/grafana-local-dashboard
  • wasp
  • fix/dashboard-explorer-freeze
  • master
  • feat/timerqueue
  • test/sync_debug_and_650
  • feat/sync_revamp_inv
  • wip/sync
  • tool/db-recovery
  • portcheck/fix
  • fix/synchronization
  • feat/new-dashboard-analysis
  • feat/refactored-analysis-dashboard
  • feat/new-analysis-dashboard
  • test/demo-prometheus-fpc
  • prometheus_metrics
  • wip/analysis-server
  • merge/fpc-test-value-transfer
  • v0.2.2
  • v0.2.1
  • v0.2.0
  • v0.1.3
  • v0.1.2
  • v0.1.1
  • v0.1.0
28 results

tipselection.go

  • tipselection.go 838 B
    package tipselection
    
    import (
    	"math/rand"
    	"sync"
    
    	"github.com/iotaledger/goshimmer/packages/model/meta_transaction"
    	"github.com/iotaledger/iota.go/trinary"
    )
    
    var (
    	tipSet map[trinary.Hash]struct{}
    	mutex  sync.RWMutex
    )
    
    func GetRandomTip(excluding ...trinary.Hash) trinary.Trytes {
    	mutex.RLock()
    	defer mutex.RUnlock()
    
    	numTips := len(tipSet)
    	if numTips == 0 {
    		return meta_transaction.BRANCH_NULL_HASH
    	}
    
    	var ignore trinary.Hash
    	if len(excluding) > 0 {
    		ignore = excluding[0]
    	}
    	if _, contains := tipSet[ignore]; contains {
    		if numTips == 1 {
    			return ignore
    		}
    		numTips -= 1
    	}
    
    	i := rand.Intn(numTips)
    	for k := range tipSet {
    		if k == ignore {
    			continue
    		}
    		if i == 0 {
    			return k
    		}
    		i--
    	}
    	panic("unreachable")
    }
    
    func GetTipsCount() int {
    	mutex.RLock()
    	defer mutex.RUnlock()
    
    	return len(tipSet)
    }