mirror of
https://github.com/bisq-network/bisq.git
synced 2024-11-19 18:03:12 +01:00
Add blockHeight cache in DaoState
Improve block lookup by height by introducing an index, instead of iterating through all blocks and comparing the height.
This commit is contained in:
parent
792d135693
commit
fea717eeab
@ -120,6 +120,7 @@ public class DaoStateService implements DaoSetupService {
|
|||||||
daoState.getBlocks().clear();
|
daoState.getBlocks().clear();
|
||||||
daoState.getBlocks().addAll(snapshot.getBlocks());
|
daoState.getBlocks().addAll(snapshot.getBlocks());
|
||||||
daoState.setBlockHashCache(snapshot.getBlockHashCache());
|
daoState.setBlockHashCache(snapshot.getBlockHashCache());
|
||||||
|
daoState.setBlockHeightCache(snapshot.getBlockHeightCache());
|
||||||
|
|
||||||
daoState.getCycles().clear();
|
daoState.getCycles().clear();
|
||||||
daoState.getCycles().addAll(snapshot.getCycles());
|
daoState.getCycles().addAll(snapshot.getCycles());
|
||||||
@ -311,9 +312,7 @@ public class DaoStateService implements DaoSetupService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Optional<Block> getBlockAtHeight(int height) {
|
public Optional<Block> getBlockAtHeight(int height) {
|
||||||
return getBlocks().stream()
|
return Optional.ofNullable(daoState.getBlockHeightCache().get(height));
|
||||||
.filter(block -> block.getHeight() == height)
|
|
||||||
.findAny();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean containsBlock(Block block) {
|
public boolean containsBlock(Block block) {
|
||||||
|
@ -109,6 +109,8 @@ public class DaoState implements PersistablePayload {
|
|||||||
@JsonExclude
|
@JsonExclude
|
||||||
private transient final Map<String, Tx> txCache; // key is txId
|
private transient final Map<String, Tx> txCache; // key is txId
|
||||||
@JsonExclude
|
@JsonExclude
|
||||||
|
private transient final Map<Integer, Block> blockHeightCache; // key is block height
|
||||||
|
@JsonExclude
|
||||||
private transient final Set<String> blockHashCache;
|
private transient final Set<String> blockHashCache;
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////////////////
|
||||||
@ -165,6 +167,9 @@ public class DaoState implements PersistablePayload {
|
|||||||
blockHashCache = blocks.stream()
|
blockHashCache = blocks.stream()
|
||||||
.map(Block::getHash)
|
.map(Block::getHash)
|
||||||
.collect(Collectors.toSet());
|
.collect(Collectors.toSet());
|
||||||
|
|
||||||
|
blockHeightCache = blocks.stream()
|
||||||
|
.collect(Collectors.toMap(Block::getHeight, Function.identity(), (x, y) -> x, HashMap::new));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -260,6 +265,11 @@ public class DaoState implements PersistablePayload {
|
|||||||
this.blockHashCache.addAll(blockHashCache);
|
this.blockHashCache.addAll(blockHashCache);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setBlockHeightCache(Map<Integer, Block> blockHeightCache) {
|
||||||
|
this.blockHeightCache.clear();
|
||||||
|
this.blockHeightCache.putAll(blockHeightCache);
|
||||||
|
}
|
||||||
|
|
||||||
public Map<String, Tx> getTxCache() {
|
public Map<String, Tx> getTxCache() {
|
||||||
return Collections.unmodifiableMap(txCache);
|
return Collections.unmodifiableMap(txCache);
|
||||||
}
|
}
|
||||||
@ -268,6 +278,8 @@ public class DaoState implements PersistablePayload {
|
|||||||
return Collections.unmodifiableSet(blockHashCache);
|
return Collections.unmodifiableSet(blockHashCache);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Map<Integer, Block> getBlockHeightCache() { return Collections.unmodifiableMap(blockHeightCache); }
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "DaoState{" +
|
return "DaoState{" +
|
||||||
|
Loading…
Reference in New Issue
Block a user