Add a DiskBlockStore and associated unit tests. This removes the need to download the block chain from scratch each time a program is started up.

This commit is contained in:
Mike Hearn
2011-03-28 17:59:10 +00:00
parent 65b80720bd
commit 668b176283
11 changed files with 322 additions and 22 deletions

View File

@@ -40,13 +40,13 @@ public class BlockChainTest {
@Before
public void setUp() {
testNetChain = new BlockChain(testNet, new Wallet(testNet));
testNetChain = new BlockChain(testNet, new Wallet(testNet), new MemoryBlockStore(testNet));
unitTestParams = NetworkParameters.unitTests();
wallet = new Wallet(unitTestParams);
wallet.addKey(new ECKey());
coinbaseTo = wallet.keychain.get(0).toAddress(unitTestParams);
chain = new BlockChain(unitTestParams, wallet);
chain = new BlockChain(unitTestParams, wallet, new MemoryBlockStore(unitTestParams));
}
@Test

View File

@@ -101,6 +101,14 @@ public class BlockTest {
}
}
@Test
public void testHeaderParse() throws Exception {
Block block = new Block(params, blockBytes);
Block header = block.cloneAsHeader();
Block reparsed = new Block(params, header.bitcoinSerialize());
assertEquals(reparsed, header);
}
@Test
public void testBitCoinSerialization() throws Exception {
// We have to be able to reserialize everything exactly as we found it for hashing to work. This test also

View File

@@ -0,0 +1,47 @@
/**
* Copyright 2011 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.bitcoin.core;
import org.junit.Test;
import java.io.File;
import static org.junit.Assert.assertEquals;
public class DiskBlockStoreTest {
@Test
public void testStorage() throws Exception {
File temp = File.createTempFile("bitcoinj-test", null, null);
System.out.println(temp.getAbsolutePath());
//temp.deleteOnExit();
NetworkParameters params = NetworkParameters.unitTests();
Address to = new ECKey().toAddress(params);
DiskBlockStore store = new DiskBlockStore(params, temp);
// Check the first block in a new store is the genesis block.
StoredBlock genesis = store.getChainHead();
assertEquals(params.genesisBlock, genesis.getHeader());
// Build a new block.
StoredBlock b1 = genesis.build(genesis.getHeader().createNextBlock(to).cloneAsHeader());
store.put(b1);
store.setChainHead(b1);
// Check we can get it back out again if we rebuild the store object.
store = new DiskBlockStore(params, temp);
StoredBlock b2 = store.get(b1.getHeader().getHash());
assertEquals(b1, b2);
// Check the chain head was stored correctly also.
assertEquals(b1, store.getChainHead());
}
}