Skip to content

Commit

Permalink
Router: add logging to the BlindCache
Browse files Browse the repository at this point in the history
  • Loading branch information
eyedeekay committed Oct 9, 2023
1 parent ad338ef commit 95588cf
Showing 1 changed file with 51 additions and 2 deletions.
53 changes: 51 additions & 2 deletions router/java/src/net/i2p/router/networkdb/kademlia/BlindCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
class BlindCache {

private final RouterContext _context;
private final Log _log;
// unblinded key
private final ConcurrentHashMap<SigningPublicKey, BlindData> _cache;
// blinded key
Expand All @@ -44,36 +45,50 @@ class BlindCache {
private boolean _changed;

private final String PERSIST_FILE;
private final String _dbid;

/**
* Caller MUST call startup() to load persistent cache from disk
*/
public BlindCache(RouterContext ctx) {
_context = ctx;
_log = _context.logManager().getLog(getClass());
_cache = new ConcurrentHashMap<SigningPublicKey, BlindData>(32);
_reverseCache = new ConcurrentHashMap<SigningPublicKey, BlindData>(32);
_hashCache = new ConcurrentHashMap<Hash, BlindData>(32);
PERSIST_FILE = "router.blindcache.dat";
_dbid = "";
}

/**
* Caller MUST call startup() to load persistent cache from disk
*/
public BlindCache(RouterContext ctx, Hash subDb) {
_context = ctx;
_log = _context.logManager().getLog(getClass());
_cache = new ConcurrentHashMap<SigningPublicKey, BlindData>(32);
_reverseCache = new ConcurrentHashMap<SigningPublicKey, BlindData>(32);
_hashCache = new ConcurrentHashMap<Hash, BlindData>(32);
if (subDb == null)
if (subDb != null){
if (_log.shouldLog(Log.DEBUG))
_log.debug("Loading blind cache for " + subDb);
PERSIST_FILE = "router." + subDb.toString() + ".blindcache.dat";
else
_dbid = subDb.toString();
} else {
if (_log.shouldLog(Log.DEBUG))
_log.debug("Loading blind cache for main netdb");
PERSIST_FILE = "router.blindcache.dat";
_dbid = "main";
}

}

/**
* May be restarted by calling startup() again.
*/
public synchronized void shutdown() {
if (_log.shouldLog(Log.DEBUG))
_log.debug("stopping the blind cache for " + _dbid);
if (_changed)
store();
_cache.clear();
Expand All @@ -82,6 +97,8 @@ public synchronized void shutdown() {
}

public synchronized void startup() {
if (_log.shouldLog(Log.DEBUG))
_log.debug("starting the blind cache for " + _dbid);
load();
}

Expand All @@ -94,6 +111,8 @@ public synchronized void startup() {
* @return the unblinded or blinded hash
*/
public Hash getHash(Destination dest) {
if (_log.shouldLog(Log.DEBUG))
_log.debug("getting hash for dest" + dest + " out of blind cache for " + _dbid);
Hash rv = getBlindedHash(dest);
if (rv != null)
return rv;
Expand All @@ -109,6 +128,8 @@ public Hash getHash(Destination dest) {
* @return the blinded hash or h
*/
public Hash getHash(Hash h) {
if (_log.shouldLog(Log.DEBUG))
_log.debug("getting hash for hash" + h + " out of blind cache for " + _dbid);
BlindData bd = _hashCache.get(h);
if (bd != null)
return bd.getBlindedHash();
Expand All @@ -124,6 +145,8 @@ public Hash getHash(Hash h) {
* @return the blinded hash or null if not blinded
*/
public Hash getBlindedHash(Destination dest) {
if (_log.shouldLog(Log.DEBUG))
_log.debug("getting blinded hash for dest" + dest + " out of blind cache for " + _dbid);
BlindData bd = _cache.get(dest.getSigningPublicKey());
if (bd != null)
return bd.getBlindedHash();
Expand All @@ -140,6 +163,8 @@ public Hash getBlindedHash(Destination dest) {
* @throws IllegalArgumentException on various errors
*/
public Hash getBlindedHash(SigningPublicKey spk) {
if (_log.shouldLog(Log.DEBUG))
_log.debug("getting hash for SPK" + spk + " out of blind cache for " + _dbid);
BlindData bd = _cache.get(spk);
if (bd == null)
bd = new BlindData(_context, spk, Blinding.getDefaultBlindedType(spk.getType()), null);
Expand All @@ -156,6 +181,8 @@ public Hash getBlindedHash(SigningPublicKey spk) {
* @throws IllegalArgumentException on various errors
*/
public void setBlinded(Destination dest, SigType blindedType, String secret) {
if (_log.shouldLog(Log.DEBUG))
_log.debug("setting blinded data for " + dest + " sigtype " + blindedType.toString() + " in blind cache for " + _dbid);
SigningPublicKey spk = dest.getSigningPublicKey();
BlindData bd = _cache.get(spk);
if (bd != null) {
Expand All @@ -177,6 +204,8 @@ public void setBlinded(Destination dest, SigType blindedType, String secret) {
* @throws IllegalArgumentException on various errors
*/
public void setBlinded(Destination dest) {
if (_log.shouldLog(Log.DEBUG))
_log.debug("setting blinded data for " + dest + " in blind cache for " + _dbid);
SigningPublicKey spk = dest.getSigningPublicKey();
BlindData bd = _cache.get(spk);
if (bd != null) {
Expand All @@ -189,6 +218,8 @@ public void setBlinded(Destination dest) {
* Persists immediately if secret or privkey is non-null
*/
public void addToCache(BlindData bd) {
if (_log.shouldLog(Log.DEBUG))
_log.debug("adding BlindData to cache " + bd + " for " + _dbid);
storeInCache(bd);
if (bd.getSecret() != null || bd.getAuthPrivKey() != null) {
store();
Expand All @@ -201,6 +232,8 @@ public void addToCache(BlindData bd) {
* @since 0.9.41 from addToCache()
*/
private void storeInCache(BlindData bd) {
if (_log.shouldLog(Log.DEBUG))
_log.debug("storing BlindData in cache" + bd + " for " + _dbid);
_cache.put(bd.getUnblindedPubKey(), bd);
_reverseCache.put(bd.getBlindedPubKey(), bd);
Destination dest = bd.getDestination();
Expand All @@ -212,6 +245,8 @@ private void storeInCache(BlindData bd) {
* The cached data or null
*/
public BlindData getData(Destination dest) {
if (_log.shouldLog(Log.DEBUG))
_log.debug("getting blind data for dest " + dest + " out of blind cache for " + _dbid);
BlindData rv = getData(dest.getSigningPublicKey());
if (rv != null) {
Destination d = rv.getDestination();
Expand All @@ -229,6 +264,8 @@ else if (!dest.equals(d))
* @param spk the unblinded public key
*/
public BlindData getData(SigningPublicKey spk) {
if (_log.shouldLog(Log.DEBUG))
_log.debug("getting blind data for SPK " + spk + " out of blind cache for " + _dbid);
SigType type = spk.getType();
if (type != SigType.EdDSA_SHA512_Ed25519 &&
type != SigType.RedDSA_SHA512_Ed25519)
Expand All @@ -242,6 +279,8 @@ public BlindData getData(SigningPublicKey spk) {
* @param spk the blinded public key
*/
public BlindData getReverseData(SigningPublicKey spk) {
if (_log.shouldLog(Log.DEBUG))
_log.debug("getting blind data for SPK " + spk + " out of blind cache for " + _dbid);
SigType type = spk.getType();
if (type != SigType.RedDSA_SHA512_Ed25519)
return null;
Expand All @@ -253,6 +292,8 @@ public BlindData getReverseData(SigningPublicKey spk) {
*
*/
public synchronized void rollover() {
if (_log.shouldLog(Log.DEBUG))
_log.debug("rollover in blind cache " + _dbid);
_reverseCache.clear();
for (BlindData bd : _cache.values()) {
_reverseCache.put(bd.getBlindedPubKey(), bd);
Expand All @@ -265,6 +306,8 @@ public synchronized void rollover() {
* @since 0.9.41
*/
public synchronized List<BlindData> getData() {
if (_log.shouldLog(Log.DEBUG))
_log.debug("getting blind data out of blind cache for " + _dbid);
List<BlindData> rv = new ArrayList<BlindData>(_cache.size());
rv.addAll(_cache.values());
return rv;
Expand All @@ -279,6 +322,8 @@ public synchronized List<BlindData> getData() {
* @since 0.9.41
*/
public boolean removeBlindData(SigningPublicKey spk) {
if (_log.shouldLog(Log.DEBUG))
_log.debug("removing blind data for SPK" + spk + " out of blind cache for " + _dbid);
boolean rv = false;
BlindData bd = _cache.remove(spk);
if (bd != null) {
Expand All @@ -301,6 +346,8 @@ public boolean removeBlindData(SigningPublicKey spk) {
* if negative, it's a negative expiration date.
*/
private synchronized void load() {
if (_log.shouldLog(Log.DEBUG))
_log.debug("loading blind data for " + _dbid);
File file = new File(_context.getConfigDir(), PERSIST_FILE);
if (!file.exists())
return;
Expand Down Expand Up @@ -345,6 +392,8 @@ private synchronized void load() {
}

private synchronized void store() {
if (_log.shouldLog(Log.DEBUG))
_log.debug("storing blind data for " + _dbid);
if (_cache.isEmpty())
return;
Log log = _context.logManager().getLog(BlindCache.class);
Expand Down

0 comments on commit 95588cf

Please sign in to comment.