Compare commits

...

3 Commits

2 changed files with 65 additions and 2 deletions

View File

@ -35,6 +35,7 @@ import net.i2p.util.SecureFileOutputStream;
class BlindCache { class BlindCache {
private final RouterContext _context; private final RouterContext _context;
private final Log _log;
// unblinded key // unblinded key
private final ConcurrentHashMap<SigningPublicKey, BlindData> _cache; private final ConcurrentHashMap<SigningPublicKey, BlindData> _cache;
// blinded key // blinded key
@ -43,22 +44,50 @@ class BlindCache {
private final ConcurrentHashMap<Hash, BlindData> _hashCache; private final ConcurrentHashMap<Hash, BlindData> _hashCache;
private boolean _changed; private boolean _changed;
private static final String PERSIST_FILE = "router.blindcache.dat"; private final String PERSIST_FILE;
private final String _dbid;
/** /**
* Caller MUST call startup() to load persistent cache from disk * Caller MUST call startup() to load persistent cache from disk
*/ */
public BlindCache(RouterContext ctx) { public BlindCache(RouterContext ctx) {
_context = ctx; _context = ctx;
_log = _context.logManager().getLog(getClass());
_cache = new ConcurrentHashMap<SigningPublicKey, BlindData>(32); _cache = new ConcurrentHashMap<SigningPublicKey, BlindData>(32);
_reverseCache = new ConcurrentHashMap<SigningPublicKey, BlindData>(32); _reverseCache = new ConcurrentHashMap<SigningPublicKey, BlindData>(32);
_hashCache = new ConcurrentHashMap<Hash, 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 (_log.shouldLog(Log.DEBUG))
_log.debug("Loading blind cache for " + subDb);
PERSIST_FILE = "router." + subDb.toString() + ".blindcache.dat";
_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. * May be restarted by calling startup() again.
*/ */
public synchronized void shutdown() { public synchronized void shutdown() {
if (_log.shouldLog(Log.DEBUG))
_log.debug("stopping the blind cache for " + _dbid);
if (_changed) if (_changed)
store(); store();
_cache.clear(); _cache.clear();
@ -67,6 +96,8 @@ class BlindCache {
} }
public synchronized void startup() { public synchronized void startup() {
if (_log.shouldLog(Log.DEBUG))
_log.debug("starting the blind cache for " + _dbid);
load(); load();
} }
@ -79,6 +110,8 @@ class BlindCache {
* @return the unblinded or blinded hash * @return the unblinded or blinded hash
*/ */
public Hash getHash(Destination dest) { 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); Hash rv = getBlindedHash(dest);
if (rv != null) if (rv != null)
return rv; return rv;
@ -94,6 +127,8 @@ class BlindCache {
* @return the blinded hash or h * @return the blinded hash or h
*/ */
public Hash getHash(Hash 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); BlindData bd = _hashCache.get(h);
if (bd != null) if (bd != null)
return bd.getBlindedHash(); return bd.getBlindedHash();
@ -109,6 +144,8 @@ class BlindCache {
* @return the blinded hash or null if not blinded * @return the blinded hash or null if not blinded
*/ */
public Hash getBlindedHash(Destination dest) { 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()); BlindData bd = _cache.get(dest.getSigningPublicKey());
if (bd != null) if (bd != null)
return bd.getBlindedHash(); return bd.getBlindedHash();
@ -125,6 +162,8 @@ class BlindCache {
* @throws IllegalArgumentException on various errors * @throws IllegalArgumentException on various errors
*/ */
public Hash getBlindedHash(SigningPublicKey spk) { 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); BlindData bd = _cache.get(spk);
if (bd == null) if (bd == null)
bd = new BlindData(_context, spk, Blinding.getDefaultBlindedType(spk.getType()), null); bd = new BlindData(_context, spk, Blinding.getDefaultBlindedType(spk.getType()), null);
@ -141,6 +180,8 @@ class BlindCache {
* @throws IllegalArgumentException on various errors * @throws IllegalArgumentException on various errors
*/ */
public void setBlinded(Destination dest, SigType blindedType, String secret) { public void setBlinded(Destination dest, SigType blindedType, String secret) {
if (_log.shouldLog(Log.DEBUG))
_log.debug("setting blinded data for " + dest + " sigtype " + blindedType + " in blind cache for " + _dbid);
SigningPublicKey spk = dest.getSigningPublicKey(); SigningPublicKey spk = dest.getSigningPublicKey();
BlindData bd = _cache.get(spk); BlindData bd = _cache.get(spk);
if (bd != null) { if (bd != null) {
@ -162,6 +203,8 @@ class BlindCache {
* @throws IllegalArgumentException on various errors * @throws IllegalArgumentException on various errors
*/ */
public void setBlinded(Destination dest) { 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(); SigningPublicKey spk = dest.getSigningPublicKey();
BlindData bd = _cache.get(spk); BlindData bd = _cache.get(spk);
if (bd != null) { if (bd != null) {
@ -174,6 +217,8 @@ class BlindCache {
* Persists immediately if secret or privkey is non-null * Persists immediately if secret or privkey is non-null
*/ */
public void addToCache(BlindData bd) { public void addToCache(BlindData bd) {
if (_log.shouldLog(Log.DEBUG))
_log.debug("adding BlindData to cache " + bd + " for " + _dbid);
storeInCache(bd); storeInCache(bd);
if (bd.getSecret() != null || bd.getAuthPrivKey() != null) { if (bd.getSecret() != null || bd.getAuthPrivKey() != null) {
store(); store();
@ -186,6 +231,8 @@ class BlindCache {
* @since 0.9.41 from addToCache() * @since 0.9.41 from addToCache()
*/ */
private void storeInCache(BlindData bd) { private void storeInCache(BlindData bd) {
if (_log.shouldLog(Log.DEBUG))
_log.debug("storing BlindData in cache" + bd + " for " + _dbid);
_cache.put(bd.getUnblindedPubKey(), bd); _cache.put(bd.getUnblindedPubKey(), bd);
_reverseCache.put(bd.getBlindedPubKey(), bd); _reverseCache.put(bd.getBlindedPubKey(), bd);
Destination dest = bd.getDestination(); Destination dest = bd.getDestination();
@ -197,6 +244,8 @@ class BlindCache {
* The cached data or null * The cached data or null
*/ */
public BlindData getData(Destination dest) { 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()); BlindData rv = getData(dest.getSigningPublicKey());
if (rv != null) { if (rv != null) {
Destination d = rv.getDestination(); Destination d = rv.getDestination();
@ -214,6 +263,8 @@ class BlindCache {
* @param spk the unblinded public key * @param spk the unblinded public key
*/ */
public BlindData getData(SigningPublicKey spk) { 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(); SigType type = spk.getType();
if (type != SigType.EdDSA_SHA512_Ed25519 && if (type != SigType.EdDSA_SHA512_Ed25519 &&
type != SigType.RedDSA_SHA512_Ed25519) type != SigType.RedDSA_SHA512_Ed25519)
@ -227,6 +278,8 @@ class BlindCache {
* @param spk the blinded public key * @param spk the blinded public key
*/ */
public BlindData getReverseData(SigningPublicKey spk) { 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(); SigType type = spk.getType();
if (type != SigType.RedDSA_SHA512_Ed25519) if (type != SigType.RedDSA_SHA512_Ed25519)
return null; return null;
@ -238,6 +291,8 @@ class BlindCache {
* *
*/ */
public synchronized void rollover() { public synchronized void rollover() {
if (_log.shouldLog(Log.DEBUG))
_log.debug("rollover in blind cache " + _dbid);
_reverseCache.clear(); _reverseCache.clear();
for (BlindData bd : _cache.values()) { for (BlindData bd : _cache.values()) {
_reverseCache.put(bd.getBlindedPubKey(), bd); _reverseCache.put(bd.getBlindedPubKey(), bd);
@ -250,6 +305,8 @@ class BlindCache {
* @since 0.9.41 * @since 0.9.41
*/ */
public synchronized List<BlindData> getData() { 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()); List<BlindData> rv = new ArrayList<BlindData>(_cache.size());
rv.addAll(_cache.values()); rv.addAll(_cache.values());
return rv; return rv;
@ -264,6 +321,8 @@ class BlindCache {
* @since 0.9.41 * @since 0.9.41
*/ */
public boolean removeBlindData(SigningPublicKey spk) { 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; boolean rv = false;
BlindData bd = _cache.remove(spk); BlindData bd = _cache.remove(spk);
if (bd != null) { if (bd != null) {
@ -286,6 +345,8 @@ class BlindCache {
* if negative, it's a negative expiration date. * if negative, it's a negative expiration date.
*/ */
private synchronized void load() { private synchronized void load() {
if (_log.shouldLog(Log.DEBUG))
_log.debug("loading blind data for " + _dbid);
File file = new File(_context.getConfigDir(), PERSIST_FILE); File file = new File(_context.getConfigDir(), PERSIST_FILE);
if (!file.exists()) if (!file.exists())
return; return;
@ -330,6 +391,8 @@ class BlindCache {
} }
private synchronized void store() { private synchronized void store() {
if (_log.shouldLog(Log.DEBUG))
_log.debug("storing blind data for " + _dbid);
if (_cache.isEmpty()) if (_cache.isEmpty())
return; return;
Log log = _context.logManager().getLog(BlindCache.class); Log log = _context.logManager().getLog(BlindCache.class);

View File

@ -184,7 +184,7 @@ public abstract class KademliaNetworkDatabaseFacade extends NetworkDatabaseFacad
_reseedChecker = null; _reseedChecker = null;
else else
_reseedChecker = new ReseedChecker(context); _reseedChecker = new ReseedChecker(context);
_blindCache = new BlindCache(context); _blindCache = new BlindCache(context, dbid);
_localKey = null; _localKey = null;
if (_log.shouldLog(Log.DEBUG)) if (_log.shouldLog(Log.DEBUG))