/**
 * 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.store;

import com.google.bitcoin.core.*;

import java.util.HashMap;
import java.util.Map;

/**
 * Keeps {@link com.google.bitcoin.core.StoredBlock}s in memory. Used primarily for unit testing.
 */
public class MemoryBlockStore implements BlockStore {
    private Map<Sha256Hash, StoredBlock> blockMap;
    private StoredBlock chainHead;

    public MemoryBlockStore(NetworkParameters params) {
        blockMap = new HashMap<Sha256Hash, StoredBlock>();
        // Insert the genesis block.
        try {
            Block genesisHeader = params.getGenesisBlock().cloneAsHeader();
            StoredBlock storedGenesis = new StoredBlock(genesisHeader, genesisHeader.getWork(), 0);
            put(storedGenesis);
            setChainHead(storedGenesis);
        } catch (BlockStoreException e) {
            throw new RuntimeException(e);  // Cannot happen.
        } catch (VerificationException e) {
            throw new RuntimeException(e);  // Cannot happen.
        }
    }

    public synchronized void put(StoredBlock block) throws BlockStoreException {
        if (blockMap == null) throw new BlockStoreException("MemoryBlockStore is closed");
        Sha256Hash hash = block.getHeader().getHash();
        blockMap.put(hash, block);
    }

    public synchronized StoredBlock get(Sha256Hash hash) throws BlockStoreException {
        if (blockMap == null) throw new BlockStoreException("MemoryBlockStore is closed");
        return blockMap.get(hash);
    }

    public StoredBlock getChainHead() throws BlockStoreException {
        if (blockMap == null) throw new BlockStoreException("MemoryBlockStore is closed");
        return chainHead;
    }

    public void setChainHead(StoredBlock chainHead) throws BlockStoreException {
        if (blockMap == null) throw new BlockStoreException("MemoryBlockStore is closed");
        this.chainHead = chainHead;
    }
    
    public void close() {
        blockMap = null;
    }
}

@@ -676,16 +676,22 @@
 va.util.
+Linked
 HashMap;
@@ -886,16 +886,26 @@
 private 
+LinkedHash
 Map%3CSha2
@@ -933,16 +933,231 @@
 blockMap
+ = new LinkedHashMap%3CSha256Hash, StoredBlock%3E() %7B%0A        @Override%0A        protected boolean removeEldestEntry(Map.Entry%3CSha256Hash, StoredBlock%3E eldest) %7B%0A            return blockMap.size() %3E 5000;%0A        %7D%0A    %7D
 ;%0A    pr
@@ -1246,67 +1246,8 @@
 ) %7B%0A
-        blockMap = new HashMap%3CSha256Hash, StoredBlock%3E();%0A
     

Make MemoryBlockStore store only a rolling window of the last 5000 blocks. Fixes BuildCheckpoints which was trying to store every block header and running out of heap space.