Skip to content

Commit

Permalink
Rename constants in RepositoryBtcBlockStoreWithCacheChainWorkTest
Browse files Browse the repository at this point in the history
  • Loading branch information
marcos-iov committed Dec 12, 2024
1 parent b8aff0c commit ab2a578
Showing 1 changed file with 32 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,10 @@

import static co.rsk.bitcoinj.core.StoredBlock.COMPACT_SERIALIZED_SIZE_LEGACY;
import static co.rsk.bitcoinj.core.StoredBlock.COMPACT_SERIALIZED_SIZE_V2;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import co.rsk.bitcoinj.core.BtcBlock;
import co.rsk.bitcoinj.core.NetworkParameters;
import co.rsk.bitcoinj.core.Sha256Hash;
import co.rsk.bitcoinj.core.StoredBlock;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.*;

import co.rsk.bitcoinj.core.*;
import co.rsk.core.RskAddress;
import co.rsk.db.MutableTrieCache;
import co.rsk.db.MutableTrieImpl;
Expand All @@ -20,6 +14,7 @@
import co.rsk.trie.Trie;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;
import org.ethereum.config.blockchain.upgrades.ActivationConfig;
Expand All @@ -37,7 +32,7 @@

class RepositoryBtcBlockStoreWithCacheChainWorkTest {

private final String BLOCK_STORE_CHAIN_HEAD_KEY = "blockStoreChainHead";
private static final String BLOCK_STORE_CHAIN_HEAD_KEY = "blockStoreChainHead";
// Max chain work to fit in 12 bytes
private static final BigInteger MAX_WORK_V1 = new BigInteger(/* 12 bytes */ "ffffffffffffffffffffffff", 16);
// Chain work too large to fit in 12 bytes
Expand All @@ -54,16 +49,15 @@ class RepositoryBtcBlockStoreWithCacheChainWorkTest {

private static final BridgeConstants bridgeMainnetConstants = BridgeMainNetConstants.getInstance();
private static final NetworkParameters mainneNetworkParameters = bridgeMainnetConstants.getBtcParams();
private Repository repository;
private Map<Sha256Hash, StoredBlock> cacheBlocks;
private static final RskAddress BRIDGE_ADDR = PrecompiledContracts.BRIDGE_ADDR;
private BridgeStorageProvider bridgeStorageProvider;

private Repository repository;
private RepositoryBtcBlockStoreWithCache repositoryBtcBlockStoreWithCache;

// Just an arbitrary block
private static final String blockHeader = "00e00820925b77c9ff4d0036aa29f3238cde12e9af9d55c34ed30200000000000000000032a9fa3e12ef87a2327b55db6a16a1227bb381db8b269d90aa3a6e38cf39665f91b47766255d0317c1b1575f";
private static final int blockHeight = 849137;
private static final BtcBlock BLOCK = new BtcBlock(mainneNetworkParameters, Hex.decode(blockHeader));
private static final String BLOCK_HEADER = "00e00820925b77c9ff4d0036aa29f3238cde12e9af9d55c34ed30200000000000000000032a9fa3e12ef87a2327b55db6a16a1227bb381db8b269d90aa3a6e38cf39665f91b47766255d0317c1b1575f";
private static final int BLOCK_HEIGHT = 849137;
private static final BtcBlock BLOCK = new BtcBlock(mainneNetworkParameters, Hex.decode(BLOCK_HEADER));

Check notice

Code scanning / CodeQL

Deprecated method or constructor invocation Note test

Invoking
BtcBlock.BtcBlock
should be avoided because it has been deprecated.

@BeforeEach
void setUp() {
Expand All @@ -73,7 +67,9 @@ void setUp() {
}

void arrange(ActivationConfig.ForBlock activations) {
bridgeStorageProvider = new BridgeStorageProvider(repository, BRIDGE_ADDR, mainneNetworkParameters, activations);
Map<Sha256Hash, StoredBlock> cacheBlocks = new HashMap<>();
BridgeStorageProvider bridgeStorageProvider = new BridgeStorageProvider(repository,
BRIDGE_ADDR, mainneNetworkParameters, activations);

repositoryBtcBlockStoreWithCache = new RepositoryBtcBlockStoreWithCache(
mainneNetworkParameters,
Expand All @@ -90,13 +86,11 @@ void arrange(ActivationConfig.ForBlock activations) {
@MethodSource("invalidChainWorkForV1")
void put_preRskip_whenInvalidChainWorkForV1_shouldFail(BigInteger chainWork) {
arrange(arrowHeadActivations);
StoredBlock storedBlock = new StoredBlock(BLOCK, chainWork, blockHeight);
StoredBlock storedBlock = new StoredBlock(BLOCK, chainWork, BLOCK_HEIGHT);

// act
IllegalArgumentException actualException = Assertions.assertThrows(
IllegalArgumentException.class, () -> {
repositoryBtcBlockStoreWithCache.put(storedBlock);
});
IllegalArgumentException actualException = assertThrows(
IllegalArgumentException.class, () -> repositoryBtcBlockStoreWithCache.put(storedBlock));

String expectedMessage = "The given number does not fit in 12";
String actualMessage = actualException.getMessage();
Expand All @@ -115,7 +109,7 @@ private static Stream<Arguments> invalidChainWorkForV1() {
@MethodSource("validChainWorkForV1")
void put_preRskip_whenValidChainWorkForV1_shouldStoreBlock(BigInteger chainWork) {
arrange(arrowHeadActivations);
StoredBlock storedBlock = new StoredBlock(BLOCK, chainWork, blockHeight);
StoredBlock storedBlock = new StoredBlock(BLOCK, chainWork, BLOCK_HEIGHT);

// act
repositoryBtcBlockStoreWithCache.put(storedBlock);
Expand Down Expand Up @@ -156,7 +150,7 @@ void put_postRskip_whenChainWorkAnySizeUnder32Bytes_shouldStoreBlock(BigInteger
int expectedCompactSerializedSize = COMPACT_SERIALIZED_SIZE_V2;

arrange(lovellActivations);
StoredBlock storedBlock = new StoredBlock(BLOCK, chainWork, blockHeight);
StoredBlock storedBlock = new StoredBlock(BLOCK, chainWork, BLOCK_HEIGHT);

// act
repositoryBtcBlockStoreWithCache.put(storedBlock);
Expand Down Expand Up @@ -196,13 +190,12 @@ private static Stream<Arguments> validChainWorkForV2() {
@MethodSource("invalidChainWorkForV2")
void put_postRskip_whenInvalidChainWorkForV2_shouldFail(BigInteger chainWork) {
arrange(lovellActivations);
StoredBlock storedBlock = new StoredBlock(BLOCK, chainWork, blockHeight);
StoredBlock storedBlock = new StoredBlock(BLOCK, chainWork, BLOCK_HEIGHT);

// act
IllegalArgumentException actualException = Assertions.assertThrows(
IllegalArgumentException.class, () -> {
repositoryBtcBlockStoreWithCache.put(storedBlock);
});
IllegalArgumentException actualException = assertThrows(
IllegalArgumentException.class, () -> repositoryBtcBlockStoreWithCache.put(storedBlock)
);

String expectedMessage = "The given number does not fit in 32";
String actualMessage = actualException.getMessage();
Expand All @@ -219,7 +212,7 @@ private static Stream<Arguments> invalidChainWorkForV2() {
@MethodSource("validChainWorkForV1")
void get_preRskip_whenValidChainWorkForV1_shouldGetStoredBlock(BigInteger chainWork) {
arrange(arrowHeadActivations);
StoredBlock expectedStoreBlock = new StoredBlock(BLOCK, chainWork, blockHeight);
StoredBlock expectedStoreBlock = new StoredBlock(BLOCK, chainWork, BLOCK_HEIGHT);
Sha256Hash expectedHash = expectedStoreBlock.getHeader().getHash();
arrangeRepositoryWithExpectedStoredBlock(expectedStoreBlock);

Expand Down Expand Up @@ -249,7 +242,7 @@ private void arrangeRepositoryWithExpectedStoredBlock(StoredBlock expectedStoreB
@MethodSource("validChainWorkForV1")
void get_postRskip_whenChainWorkForV1_shouldGetStoredBlock(BigInteger chainWork) {
arrange(lovellActivations);
StoredBlock expectedStoreBlock = new StoredBlock(BLOCK, chainWork, blockHeight);
StoredBlock expectedStoreBlock = new StoredBlock(BLOCK, chainWork, BLOCK_HEIGHT);
Sha256Hash expectedHash = expectedStoreBlock.getHeader().getHash();
arrangeRepositoryWithExpectedStoredBlockV2(expectedStoreBlock);

Expand Down Expand Up @@ -279,7 +272,7 @@ private void arrangeRepositoryWithExpectedStoredBlockV2(StoredBlock expectedStor
@MethodSource("validChainWorkForV2")
void get_postRskip_whenStoredBLochHasChainWorkOver12Bytes_shouldGetStoredBlock(BigInteger chainWork) {
arrange(lovellActivations);
StoredBlock expectedStoreBlock = new StoredBlock(BLOCK, chainWork, blockHeight);
StoredBlock expectedStoreBlock = new StoredBlock(BLOCK, chainWork, BLOCK_HEIGHT);
Sha256Hash expectedHash = expectedStoreBlock.getHeader().getHash();
arrangeRepositoryWithExpectedStoredBlockV2(expectedStoreBlock);

Expand All @@ -300,7 +293,7 @@ void get_postRskip_whenStoredBLochHasChainWorkOver12Bytes_shouldGetStoredBlock(B
void getChainHead_preRskip_whenValidChainWorkForV1_shouldGetChainHead(BigInteger chainWork) {
arrange(arrowHeadActivations);
reset(repository);
StoredBlock expectedStoreBlock = new StoredBlock(BLOCK, chainWork, blockHeight);
StoredBlock expectedStoreBlock = new StoredBlock(BLOCK, chainWork, BLOCK_HEIGHT);

arrangeRepositoryWithExpectedChainHead(expectedStoreBlock);

Expand Down Expand Up @@ -330,7 +323,7 @@ private void arrangeRepositoryWithExpectedChainHead(StoredBlock expectedStoreBlo
void getChainHead_postRskip_whenChainWorkForV1_shouldGetChainHead(BigInteger chainWork) {
arrange(lovellActivations);
reset(repository);
StoredBlock expectedStoreBlock = new StoredBlock(BLOCK, chainWork, blockHeight);
StoredBlock expectedStoreBlock = new StoredBlock(BLOCK, chainWork, BLOCK_HEIGHT);

arrangeRepositoryWithChainHeadV2(expectedStoreBlock);

Expand Down Expand Up @@ -360,7 +353,7 @@ private void arrangeRepositoryWithChainHeadV2(StoredBlock expectedStoreBlock) {
void getChainHead_postRskip_whenStoredBLochHasChainWorkOver12Bytes_shouldGetChainHead(BigInteger chainWork) {
arrange(lovellActivations);
reset(repository);
StoredBlock expectedStoreBlock = new StoredBlock(BLOCK, chainWork, blockHeight);
StoredBlock expectedStoreBlock = new StoredBlock(BLOCK, chainWork, BLOCK_HEIGHT);

arrangeRepositoryWithChainHeadV2(expectedStoreBlock);

Expand All @@ -381,7 +374,7 @@ void getChainHead_postRskip_whenStoredBLochHasChainWorkOver12Bytes_shouldGetChai
void setChainHead_preRskip_whenValidChainWorkForV1_shouldStoreChainHead(BigInteger chainWork) {
arrange(arrowHeadActivations);
reset(repository);
StoredBlock expectedStoreBlock = new StoredBlock(BLOCK, chainWork, blockHeight);
StoredBlock expectedStoreBlock = new StoredBlock(BLOCK, chainWork, BLOCK_HEIGHT);

// act
repositoryBtcBlockStoreWithCache.setChainHead(expectedStoreBlock);
Expand All @@ -402,7 +395,7 @@ void setChainHead_preRskip_whenValidChainWorkForV1_shouldStoreChainHead(BigInteg
void setChainHead_postRskip_whenChainWorkForV1_shouldStoreChainHead(BigInteger chainWork) {
arrange(lovellActivations);
reset(repository);
StoredBlock expectedStoreBlock = new StoredBlock(BLOCK, chainWork, blockHeight);
StoredBlock expectedStoreBlock = new StoredBlock(BLOCK, chainWork, BLOCK_HEIGHT);

// act
repositoryBtcBlockStoreWithCache.setChainHead(expectedStoreBlock);
Expand All @@ -423,7 +416,7 @@ void setChainHead_postRskip_whenChainWorkForV1_shouldStoreChainHead(BigInteger c
void setChainHead_postRskip_whenStoredBLochHasChainWorkOver12Bytes1_shouldStoreChainHead(BigInteger chainWork) {
arrange(lovellActivations);
reset(repository);
StoredBlock expectedStoreBlock = new StoredBlock(BLOCK, chainWork, blockHeight);
StoredBlock expectedStoreBlock = new StoredBlock(BLOCK, chainWork, BLOCK_HEIGHT);

// act
repositoryBtcBlockStoreWithCache.setChainHead(expectedStoreBlock);
Expand Down

0 comments on commit ab2a578

Please sign in to comment.