Skip to content

Commit

Permalink
Add tests to verify the first 240 blocks of Doge & Lite
Browse files Browse the repository at this point in the history
  • Loading branch information
Ross Nicoll committed Jan 15, 2022
1 parent 0b009a4 commit 9d21979
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 3 deletions.
19 changes: 19 additions & 0 deletions core/src/test/java/org/libdohj/params/BlockLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import static org.junit.Assert.assertEquals;
Expand All @@ -33,4 +34,22 @@ protected Map<String, AltcoinBlock> loadBlocks(final String[][] blocks) throws I
}
return loadedBlocks;
}

/**
* Load all the block headers found in the given filename.
*/
protected LinkedHashMap<String, AltcoinBlock> loadAllHeaders(final String filename, final int headerSize) throws IOException {
final AltcoinSerializer serializer = (AltcoinSerializer)params.getDefaultSerializer();
final LinkedHashMap<String, AltcoinBlock> loadedBlocks = new LinkedHashMap<>();
final InputStream stream = getClass().getResourceAsStream(filename);
if (stream == null) {
throw new IOException("Failed to find resource " + filename);
}
final byte[] payload = new byte[headerSize];
while (stream.read(payload) == headerSize) {
final AltcoinBlock block = (AltcoinBlock) serializer.makeBlock(payload);
loadedBlocks.put(block.getHashAsString(), block);
}
return loadedBlocks;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,29 @@
*/
package org.libdohj.params;

import org.bitcoinj.core.Block;
import org.bitcoinj.core.Context;
import org.bitcoinj.core.Sha256Hash;
import org.bitcoinj.core.*;
import org.bitcoinj.script.Script;
import org.bitcoinj.store.BlockStore;
import org.bitcoinj.store.BlockStoreException;
import org.bitcoinj.store.MemoryBlockStore;
import org.bitcoinj.wallet.Wallet;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/**
*
* @author Ross Nicoll
*/
public class DogecoinMainNetParamsTest {
private static final DogecoinMainNetParams params = DogecoinMainNetParams.get();
private static final BlockLoader loader = new BlockLoader(params);
private static final String BLOCK_240_HASH = "3752567a4c6085970f5b726feee3b8fc0f37ca95bb2a8daf497683b5168ec8d1";

@Before
public void setUp() throws Exception {
Expand All @@ -42,4 +51,23 @@ public void shouldHaveCorrectGenesisBlock() {
final Sha256Hash actual = genesis.getHash();
assertEquals(expected, actual);
}

/**
* This connects the first n blocks - this is more of a functional test than the single block
* test, and should handle cases such as difficulty recalculation.
*/
@Test
public void shouldConnectBlocks() throws BlockStoreException, PrunedException, IOException {
final BlockStore store = new MemoryBlockStore(params);
final Wallet wallet = Wallet.createDeterministic(params, Script.ScriptType.P2PKH);
final StoredBlock storedGenesis = new StoredBlock(params.getGenesisBlock(), params.genesisBlock.getWork(), 0);
store.put(storedGenesis);
final Map<String, AltcoinBlock> loadedBlocks = loader.loadAllHeaders("dogecoin_block1-240.bin", 80);
final BlockChain blockChain = new BlockChain(params, wallet, store);
assertTrue(blockChain.add(params.getGenesisBlock()));
for (Block block: loadedBlocks.values()) {
assertTrue(blockChain.add(block));
}
assertEquals(BLOCK_240_HASH, blockChain.getChainHead().getHeader().getHashAsString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ public void shouldHaveCorrectGenesisBlock() {
assertEquals(expected, actual);
}

/**
* Test connecting just block 1, while verifying progress at a number of steps. This is designed to expose if any
* of the more fundamental functionality is broken before we try connecting several hundred headers.
*/
@Test
public void shouldConnectBlock1() throws BlockStoreException, PrunedException, IOException {
final BlockStore store = new MemoryBlockStore(params);
Expand All @@ -91,4 +95,22 @@ public void shouldConnectBlock1() throws BlockStoreException, PrunedException, I
assertTrue(blockChain.add(params.getGenesisBlock()));
assertTrue(blockChain.add(block1));
}

/**
* This connects the first n blocks - this is more of a functional test than the single block
* test, and should handle cases such as difficulty recalculation.
*/
@Test
public void shouldConnectBlocks() throws BlockStoreException, PrunedException, IOException {
final BlockStore store = new MemoryBlockStore(params);
final Wallet wallet = Wallet.createDeterministic(params, Script.ScriptType.P2PKH);
final StoredBlock storedGenesis = new StoredBlock(params.getGenesisBlock(), params.genesisBlock.getWork(), 0);
store.put(storedGenesis);
final Map<String, AltcoinBlock> loadedBlocks = loader.loadAllHeaders("litecoin_block1-240.bin", 80);
final BlockChain blockChain = new BlockChain(params, wallet, store);
assertTrue(blockChain.add(params.getGenesisBlock()));
for (Block block: loadedBlocks.values()) {
assertTrue(blockChain.add(block));
}
}
}
Binary file not shown.
Binary file not shown.

0 comments on commit 9d21979

Please sign in to comment.