github.com/unicornultrafoundation/go-u2u@v1.0.0-rc1.0.20240205080301-e74a83d3fadc/utils/txtime/txtime.go (about)

     1  package txtime
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/unicornultrafoundation/go-u2u/common"
     7  
     8  	"github.com/unicornultrafoundation/go-helios/utils/wlru"
     9  )
    10  
    11  var (
    12  	globalFinalized, _    = wlru.New(30000, 30000)
    13  	globalNonFinalized, _ = wlru.New(5000, 5000)
    14  	Enabled               = false
    15  )
    16  
    17  func Saw(txid common.Hash, t time.Time) {
    18  	if !Enabled {
    19  		return
    20  	}
    21  	globalNonFinalized.ContainsOrAdd(txid, t, 1)
    22  }
    23  
    24  func Validated(txid common.Hash, t time.Time) {
    25  	if !Enabled {
    26  		return
    27  	}
    28  	v, has := globalNonFinalized.Peek(txid)
    29  	if has {
    30  		t = v.(time.Time)
    31  	}
    32  	globalFinalized.ContainsOrAdd(txid, t, 1)
    33  }
    34  
    35  func Of(txid common.Hash) time.Time {
    36  	if !Enabled {
    37  		return time.Time{}
    38  	}
    39  	v, has := globalFinalized.Get(txid)
    40  	if has {
    41  		return v.(time.Time)
    42  	}
    43  	v, has = globalNonFinalized.Get(txid)
    44  	if has {
    45  		return v.(time.Time)
    46  	}
    47  	now := time.Now()
    48  	Saw(txid, now)
    49  	return now
    50  }