Skip to content
Snippets Groups Projects
  • Wolfgang Welz's avatar
    8ae11a7e
    Fix: Solidifier (#190) · 8ae11a7e
    Wolfgang Welz authored
    * fix solidifier
    
    Request branch and trunk if absent. Remove solidified transactions from unsolidTxs.
    
    * Recursively request not yet requesting transactions
    
    * remove existing transactions from request queue
    
    * :recycle: refactor solidifier
    8ae11a7e
    History
    Fix: Solidifier (#190)
    Wolfgang Welz authored
    * fix solidifier
    
    Request branch and trunk if absent. Remove solidified transactions from unsolidTxs.
    
    * Recursively request not yet requesting transactions
    
    * remove existing transactions from request queue
    
    * :recycle: refactor solidifier
unsolidTxs.go 894 B
package tangle

import (
	"sync"
	"time"
)

type UnsolidTxs struct {
	internal map[string]Info
	sync.RWMutex
}

type Info struct {
	lastRequest time.Time
	counter     int
}

func NewUnsolidTxs() *UnsolidTxs {
	return &UnsolidTxs{
		internal: make(map[string]Info),
	}
}

func (u *UnsolidTxs) Add(hash string) bool {
	u.Lock()
	defer u.Unlock()
	_, contains := u.internal[hash]
	if contains {
		return false
	}
	info := Info{
		lastRequest: time.Now(),
		counter:     1,
	}
	u.internal[hash] = info
	return true
}

func (u *UnsolidTxs) Remove(hash string) {
	u.Lock()
	delete(u.internal, hash)
	u.Unlock()
}

func (u *UnsolidTxs) Update(targetTime time.Time) (result []string) {
	u.Lock()
	for k, v := range u.internal {
		if v.lastRequest.Before(targetTime) {
			result = append(result, k)

			v.lastRequest = time.Now()
			v.counter++

			u.internal[k] = v
		}
	}
	u.Unlock()
	return result
}