Remove blockHashes set from daoState

Was only used from one use case which can be done differently.
This commit is contained in:
chimp1984 2021-10-26 23:08:45 +02:00
parent 65c308f5ed
commit 779bd0cf1e
No known key found for this signature in database
GPG key ID: 9801B4EC591F90E3
4 changed files with 3 additions and 96 deletions

View file

@ -135,6 +135,8 @@ public class BlockParser {
}
private boolean isBlockAlreadyAdded(RawBlock rawBlock) {
return daoStateService.isBlockHashKnown(rawBlock.getHash());
return daoStateService.getBlockAtHeight(rawBlock.getHeight())
.map(block -> block.getHash().equals(rawBlock.getHash()))
.orElse(false);
}
}

View file

@ -286,17 +286,6 @@ public class DaoStateService implements DaoSetupService {
return daoState.getBlocks();
}
/**
* Whether specified block hash belongs to a block we already know about.
*
* @param blockHash The hash of a {@link Block}.
* @return True if the hash belongs to a {@link Block} we know about, otherwise
* {@code false}.
*/
public boolean isBlockHashKnown(String blockHash) {
return daoState.getBlockHashes().contains(blockHash);
}
public Optional<Block> getLastBlock() {
if (!getBlocks().isEmpty())
return Optional.of(daoState.getLastBlock());

View file

@ -115,8 +115,6 @@ public class DaoState implements PersistablePayload {
@JsonExclude
private transient final Map<Integer, Block> blocksByHeight; // Blocks indexed by height
@JsonExclude
private transient final Set<String> blockHashes; // Cache of known block hashes
@JsonExclude
private transient final Map<TxOutputType, Set<TxOutput>> txOutputsByTxOutputType = new HashMap<>();
@ -172,10 +170,6 @@ public class DaoState implements PersistablePayload {
.peek(this::addToTxOutputsByTxOutputTypeMap)
.collect(Collectors.toMap(Tx::getId, Function.identity(), (x, y) -> x, HashMap::new));
blockHashes = blocks.stream()
.map(Block::getHash)
.collect(Collectors.toSet());
blocksByHeight = blocks.stream()
.collect(Collectors.toMap(Block::getHeight, Function.identity(), (x, y) -> x, HashMap::new));
}
@ -293,10 +287,6 @@ public class DaoState implements PersistablePayload {
return Collections.unmodifiableMap(txCache);
}
public Set<String> getBlockHashes() {
return Collections.unmodifiableSet(blockHashes);
}
public Map<Integer, Block> getBlocksByHeight() {
return Collections.unmodifiableMap(blocksByHeight);
}
@ -322,7 +312,6 @@ public class DaoState implements PersistablePayload {
public void addBlock(Block block) {
blocks.add(block);
blockHashes.add(block.getHash());
blocksByHeight.put(block.getHeight(), block);
}
@ -337,7 +326,6 @@ public class DaoState implements PersistablePayload {
public void clearAndSetBlocks(List<Block> newBlocks) {
blocks.clear();
blocksByHeight.clear();
blockHashes.clear();
addBlocks(newBlocks);
}

View file

@ -1,72 +0,0 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/
package bisq.core.dao.state;
import bisq.core.dao.state.model.DaoState;
import bisq.core.dao.state.model.blockchain.Block;
import bisq.core.util.coin.BsqFormatter;
import org.bitcoinj.core.Coin;
import org.junit.Assert;
import org.junit.Test;
public class DaoStateServiceTest {
@Test
public void testIsBlockHashKnown() {
DaoStateService stateService = new DaoStateService(
new DaoState(),
new GenesisTxInfo("fakegenesistxid", 100, Coin.parseCoin("2.5").value),
new BsqFormatter());
Assert.assertEquals(
"Unknown block should not exist.",
false,
stateService.isBlockHashKnown("fakeblockhash0")
);
Block block = new Block(0, 1534800000, "fakeblockhash0", null);
stateService.onNewBlockHeight(0);
stateService.onNewBlockWithEmptyTxs(block);
Assert.assertEquals(
"Block has to be genesis block to get added.",
false,
stateService.isBlockHashKnown("fakeblockhash0")
);
Assert.assertEquals(
"Block that was never added should still not exist.",
false,
stateService.isBlockHashKnown("fakeblockhash1")
);
block = new Block(1, 1534800001, "fakeblockhash1", null);
stateService.onNewBlockHeight(1);
stateService.onNewBlockWithEmptyTxs(block);
block = new Block(2, 1534800002, "fakeblockhash2", null);
stateService.onNewBlockHeight(2);
stateService.onNewBlockWithEmptyTxs(block);
block = new Block(3, 1534800003, "fakeblockhash3", null);
stateService.onNewBlockHeight(3);
stateService.onNewBlockWithEmptyTxs(block);
Assert.assertEquals(
"Block that was never added should still not exist after adding more blocks.",
false,
stateService.isBlockHashKnown("fakeblockhash4")
);
}
}