saifi009 / bitcoinj

Automatically exported from code.google.com/p/bitcoinj
0 stars 0 forks source link

Patch for MemoryBlockStore to really clone Blocks #5

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Index: src/com/google/bitcoin/core/BlockStoreException.java
===================================================================
--- src/com/google/bitcoin/core/BlockStoreException.java    (revision 38)
+++ src/com/google/bitcoin/core/BlockStoreException.java    (working copy)
@@ -17,7 +17,16 @@
 package com.google.bitcoin.core;

 /**
- * Thrown when something goes wrong with storing a block. Examples: out of 
disk space.
+ * Thrown when something goes wrong with storing a block. Examples: out of disk
+ * space.
  */
 public class BlockStoreException extends Exception {
+
+    public BlockStoreException() {
+   super();
+    }
+
+    public BlockStoreException(Throwable cause) {
+   super(cause);
+    }
 }
Index: src/com/google/bitcoin/core/MemoryBlockStore.java
===================================================================
--- src/com/google/bitcoin/core/MemoryBlockStore.java   (revision 38)
+++ src/com/google/bitcoin/core/MemoryBlockStore.java   (working copy)
@@ -16,6 +16,12 @@

 package com.google.bitcoin.core;

+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.math.BigInteger;
 import java.nio.ByteBuffer;
 import java.util.HashMap;
 import java.util.Map;
@@ -30,15 +36,44 @@
     private Map<ByteBuffer, StoredBlock> blockMap;

     MemoryBlockStore() {
-        blockMap = new HashMap<ByteBuffer, StoredBlock>();
+   blockMap = new HashMap<ByteBuffer, StoredBlock>();
     }

     public synchronized void put(StoredBlock block) throws BlockStoreException {
-        byte[] hash = block.header.getHash();
-        blockMap.put(ByteBuffer.wrap(hash), block);
+
+   final StoredBlock clonedBlock = clone(block);
+
+   byte[] hash = clonedBlock.header.getHash();
+   blockMap.put(ByteBuffer.wrap(hash), clonedBlock);
     }

     public synchronized StoredBlock get(byte[] hash) throws BlockStoreException {
-        return blockMap.get(ByteBuffer.wrap(hash));
+   return blockMap.get(ByteBuffer.wrap(hash));
+    }
+
+    private StoredBlock clone(final StoredBlock block)
+       throws BlockStoreException {
+
+   try {
+       final ByteArrayOutputStream baos = new ByteArrayOutputStream(2000);
+       final ObjectOutputStream os = new ObjectOutputStream(baos);
+       os.writeObject(block.getHeader());
+       os.writeObject(block.getChainWork());
+       os.writeInt(block.getHeight());
+       os.close();
+
+       final ObjectInputStream is = new ObjectInputStream(
+           new ByteArrayInputStream(baos.toByteArray()));
+       final Block header = (Block) is.readObject();
+       final BigInteger chainWork = (BigInteger) is.readObject();
+       final int height = is.readInt();
+       is.close();
+
+       return new StoredBlock(header, chainWork, height);
+   } catch (final IOException x) {
+       throw new BlockStoreException(x);
+   } catch (final ClassNotFoundException x) {
+       throw new BlockStoreException(x);
+   }
     }
 }

Original issue reported on code.google.com by andreas....@gmail.com on 26 Mar 2011 at 12:01

GoogleCodeExporter commented 9 years ago
Thanks. I checked in my own fix for this. See if it works for you.

Please remember I can't accept your patches unless you sign the CLA.

Original comment by hearn@google.com on 28 Mar 2011 at 6:02

GoogleCodeExporter commented 9 years ago
Sometimes it's easier to send a patch than to describe how you want a program 
to be changed.

Original comment by andreas....@gmail.com on 28 Mar 2011 at 11:53

GoogleCodeExporter commented 9 years ago
Yes, but I'd still prefer English explanations actually. If your patch is the 
only possible way to make a change, it can be argued I used your code without 
you signing the CLA. In these cases it wasn't an issue as there were other 
things I wanted to tweak at the same time. Sorry to be a pain about this.

Original comment by hearn@google.com on 29 Mar 2011 at 9:46