Skip to content

Commit

Permalink
Merge pull request #2716 from rsksmart/recreate_valid_block_stored_chain
Browse files Browse the repository at this point in the history
Add methods to create a real valid chain with stored blocks to avoid using mocks
  • Loading branch information
marcos-iov authored Sep 12, 2024
2 parents bdbdfa8 + 9d03dfa commit e34bda6
Showing 1 changed file with 54 additions and 4 deletions.
58 changes: 54 additions & 4 deletions rskj-core/src/test/java/co/rsk/peg/BridgeSupportTestUtil.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package co.rsk.peg;

import co.rsk.bitcoinj.core.BtcBlock;
import co.rsk.bitcoinj.core.Sha256Hash;
import co.rsk.bitcoinj.core.StoredBlock;
import co.rsk.bitcoinj.core.*;
import co.rsk.bitcoinj.store.BlockStoreException;
import co.rsk.db.MutableTrieCache;
import co.rsk.db.MutableTrieImpl;
import co.rsk.peg.bitcoin.BitcoinTestUtils;
import co.rsk.trie.Trie;
import org.bouncycastle.util.encoders.Hex;
import org.ethereum.core.Repository;
import org.ethereum.db.MutableRepository;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
Expand All @@ -22,6 +24,55 @@ public static Repository createRepository() {
return new MutableRepository(new MutableTrieCache(new MutableTrieImpl(null, new Trie())));
}

public static PartialMerkleTree createValidPmtForTransactions(List<Sha256Hash> hashesToAdd, NetworkParameters networkParameters) {
byte[] relevantNodesBits = new byte[(int)Math.ceil(hashesToAdd.size() / 8.0)];
for (int i = 0; i < hashesToAdd.size(); i++) {
Utils.setBitLE(relevantNodesBits, i);
}

return PartialMerkleTree.buildFromLeaves(networkParameters, relevantNodesBits, hashesToAdd);
}

public static void recreateChainFromPmt(
BtcBlockStoreWithCache btcBlockStoreWithCache,
int chainHeight,
PartialMerkleTree partialMerkleTree,
int btcBlockWithPmtHeight,
NetworkParameters networkParameters
) throws BlockStoreException {

// first create a block that has the wanted partial merkle tree
BtcBlock btcBlockWithPmt = createBtcBlockWithPmt(partialMerkleTree, networkParameters);
// store it on the chain at wanted height
StoredBlock storedBtcBlockWithPmt = new StoredBlock(btcBlockWithPmt, BigInteger.ONE, btcBlockWithPmtHeight);
btcBlockStoreWithCache.put(storedBtcBlockWithPmt);
btcBlockStoreWithCache.setMainChainBlock(btcBlockWithPmtHeight, btcBlockWithPmt.getHash());

// create and store a new chainHead at wanted chain height
Sha256Hash otherTransactionHash = Sha256Hash.of(Hex.decode("aa"));
PartialMerkleTree pmt = createValidPmtForTransactions(Collections.singletonList(otherTransactionHash), networkParameters);
BtcBlock chainHeadBlock = createBtcBlockWithPmt(pmt, networkParameters);
StoredBlock storedChainHeadBlock = new StoredBlock(chainHeadBlock, BigInteger.TEN, chainHeight);
btcBlockStoreWithCache.put(storedChainHeadBlock);
btcBlockStoreWithCache.setChainHead(storedChainHeadBlock);
}

private static BtcBlock createBtcBlockWithPmt(PartialMerkleTree pmt, NetworkParameters networkParameters) {
Sha256Hash prevBlockHash = BitcoinTestUtils.createHash(1);
Sha256Hash merkleRoot = pmt.getTxnHashAndMerkleRoot(new ArrayList<>());

return new co.rsk.bitcoinj.core.BtcBlock(
networkParameters,
1,
prevBlockHash,
merkleRoot,
1,
1,
1,
new ArrayList<>()
);
}

public static void mockChainOfStoredBlocks(BtcBlockStoreWithCache btcBlockStore, BtcBlock targetHeader, int headHeight, int targetHeight) throws BlockStoreException {
// Simulate that the block is in there by mocking the getter by height,
// and then simulate that the txs have enough confirmations by setting a high head.
Expand All @@ -35,5 +86,4 @@ public static void mockChainOfStoredBlocks(BtcBlockStoreWithCache btcBlockStore,
when(btcBlockStore.getChainHead()).thenReturn(currentStored);
when(currentStored.getHeight()).thenReturn(headHeight);
}

}

0 comments on commit e34bda6

Please sign in to comment.