Router: Store router ident and hash at startup
Some checks failed
Java CI / build (push) Has been cancelled
Java CI / javadoc-latest (push) Has been cancelled
Java CI / build-java7 (push) Has been cancelled
Java with IzPack Snapshot Setup / setup (push) Has been cancelled

so ctx.routerHash() can be lockless
This commit is contained in:
zzz
2025-05-06 12:53:47 -04:00
parent 720fe4edaf
commit 9238f7f335
2 changed files with 30 additions and 6 deletions

View File

@ -43,6 +43,7 @@ import net.i2p.data.SigningPrivateKey;
import net.i2p.data.SigningPublicKey;
import net.i2p.data.i2np.GarlicMessage;
import net.i2p.data.router.RouterAddress;
import net.i2p.data.router.RouterIdentity;
import net.i2p.data.router.RouterInfo;
import net.i2p.router.CommSystemFacade.Status;
import net.i2p.router.crypto.FamilyKeyCrypto;
@ -94,6 +95,8 @@ public class Router implements RouterClock.ClockShiftListener {
private String _configFilename;
private RouterInfo _routerInfo;
private final ReentrantReadWriteLock _routerInfoLock = new ReentrantReadWriteLock(false);
private RouterIdentity _routerIdent;
private Hash _routerHash;
/** not for external use */
public final Object routerInfoFileLock = new Object();
private final Object _configFileLock = new Object();
@ -583,6 +586,26 @@ public class Router implements RouterClock.ClockShiftListener {
}
}
/**
* Our current router identity.
* Warning, may be null if called very early.
* Lockless.
* @since 0.9.67
*/
public RouterIdentity getRouterIdentity() {
return _routerIdent;
}
/**
* Our current router hash.
* Warning, may be null if called very early.
* Lockless.
* @since 0.9.67
*/
public Hash getRouterHash() {
return _routerHash;
}
/**
* Caller must ensure info is valid - no validation done here.
* Not for external use.
@ -592,6 +615,12 @@ public class Router implements RouterClock.ClockShiftListener {
*/
public void setRouterInfo(RouterInfo info) {
_routerInfoLock.writeLock().lock();
if (!info.getIdentity().equals(_routerIdent)) {
if (_routerIdent != null) // shouldn't happen
_log.log(Log.CRIT, "Changing router ident while running");
_routerIdent = info.getIdentity();
_routerHash = _routerIdent.calculateHash();
}
try {
_routerInfo = info;
} finally {

View File

@ -316,17 +316,12 @@ public class RouterContext extends I2PAppContext {
* Convenience method for getting the router hash.
* Equivalent to context.router().getRouterInfo().getIdentity().getHash()
*
* Warning - risk of deadlock - do not call while holding locks
*
* @return may be null if called very early
*/
public Hash routerHash() {
if (_router == null)
return null;
RouterInfo ri = _router.getRouterInfo();
if (ri == null)
return null;
return ri.getIdentity().getHash();
return _router.getRouterHash();
}
/**