3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-01-30 23:02:15 +00:00

Clear some FindBugs warnings.

This commit is contained in:
Mike Hearn 2013-02-19 17:05:23 +01:00
parent 5a3515bef0
commit 883bf03007
3 changed files with 53 additions and 32 deletions

View File

@ -240,10 +240,12 @@ public class Peer {
}
if (m == null) return;
if (currentFilteredBlock != null && !(m instanceof Transaction)) {
processFilteredBlock(currentFilteredBlock);
currentFilteredBlock = null;
synchronized (Peer.this) {
if (currentFilteredBlock != null && !(m instanceof Transaction)) {
processFilteredBlock(currentFilteredBlock);
currentFilteredBlock = null;
}
}
if (m instanceof NotFoundMessage) {
@ -259,7 +261,9 @@ public class Peer {
// messages stream in. We'll call processFilteredBlock when a non-tx message arrives (eg, another
// FilteredBlock) or when a tx that isn't needed by that block is found. A ping message is sent after
// a getblocks, to force the non-tx message path.
currentFilteredBlock = (FilteredBlock)m;
synchronized (Peer.this) {
currentFilteredBlock = (FilteredBlock)m;
}
} else if (m instanceof Transaction) {
processTransaction((Transaction) m);
} else if (m instanceof GetDataMessage) {

View File

@ -596,9 +596,10 @@ public class PeerGroup extends AbstractIdleService {
* Be careful regenerating the bloom filter too often, as it decreases anonymity because remote nodes can
* compare transactions against both the new and old filters to significantly decrease the false positive rate.
*
* See the docs for {@link BloomFilter#BloomFilter(int, double)} for a brief explanation of anonymity when using bloom filters.
* See the docs for {@link BloomFilter#BloomFilter(int, double, long)} for a brief explanation of anonymity when
* using bloom filters.
*/
public void setBloomFilterFalsePositiveRate(double bloomFilterFPRate) {
public synchronized void setBloomFilterFalsePositiveRate(double bloomFilterFPRate) {
this.bloomFilterFPRate = bloomFilterFPRate;
recalculateFastCatchupAndFilter();
}

View File

@ -16,20 +16,19 @@
package com.google.bitcoin.core;
import org.junit.Test;
import org.spongycastle.util.encoders.Hex;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import org.junit.Test;
import org.spongycastle.util.encoders.Hex;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.Assert.*;
public class ScriptTest {
// From tx 05e04c26c12fe408a3c1b71aa7996403f6acad1045252b1c62e055496f4d2cb1 on the testnet.
@ -67,8 +66,9 @@ public class ScriptTest {
assertTrue(s.isSentToRawPubKey());
}
static HashMap<String, Integer> mapOpNames;
private Script parseScriptString(NetworkParameters params, String string) throws Exception {
private static HashMap<String, Integer> mapOpNames;
private synchronized static void initMapOpNames() {
if (mapOpNames == null) {
mapOpNames = new HashMap<String, Integer>();
for (int op = Script.OP_NOP; op <= Script.OP_NOP10; op++) {
@ -79,7 +79,10 @@ public class ScriptTest {
mapOpNames.put(name, op);
}
}
}
private Script parseScriptString(NetworkParameters params, String string) throws Exception {
initMapOpNames();
String[] words = string.split("[ \\t\\n]");
UnsafeByteArrayOutputStream out = new UnsafeByteArrayOutputStream();
@ -100,7 +103,7 @@ public class ScriptTest {
} else if (w.length() >= 2 && w.startsWith("'") && w.endsWith("'")) {
// Single-quoted string, pushed as data. NOTE: this is poor-man's
// parsing, spaces/tabs/newlines in single-quoted strings won't work.
Script.writeBytes(out, w.substring(1, w.length() - 1).getBytes());
Script.writeBytes(out, w.substring(1, w.length() - 1).getBytes(Charset.forName("UTF-8")));
} else if (mapOpNames.containsKey(w)) {
// opcode, e.g. OP_ADD or OP_1:
out.write(mapOpNames.get(w));
@ -117,7 +120,8 @@ public class ScriptTest {
@Test
public void dataDrivenValidScripts() throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("script_valid.json")));
BufferedReader in = new BufferedReader(new InputStreamReader(
getClass().getResourceAsStream("script_valid.json"), Charset.forName("UTF-8")));
NetworkParameters params = NetworkParameters.testNet();
@ -125,6 +129,7 @@ public class ScriptTest {
String script = "";
while (in.ready()) {
String line = in.readLine();
if (line == null || line.equals("")) continue;
script += line;
if (line.equals("]") && script.equals("]") && !in.ready())
break; // ignore last ]
@ -143,7 +148,8 @@ public class ScriptTest {
@Test
public void dataDrivenInvalidScripts() throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("script_invalid.json")));
BufferedReader in = new BufferedReader(new InputStreamReader(
getClass().getResourceAsStream("script_invalid.json"), Charset.forName("UTF-8")));
NetworkParameters params = NetworkParameters.testNet();
@ -151,6 +157,7 @@ public class ScriptTest {
String script = "";
while (in.ready()) {
String line = in.readLine();
if (line == null || line.equals("")) continue;
script += line;
if (line.equals("]") && script.equals("]") && !in.ready())
break; // ignore last ]
@ -162,14 +169,16 @@ public class ScriptTest {
scriptSig.correctlySpends(new Transaction(params), 0, scriptPubKey, true);
fail();
} catch (VerificationException e) {}
} catch (VerificationException e) {
// Expected.
}
script = "";
}
}
in.close();
}
class JSONObject {
private static class JSONObject {
String string;
List<JSONObject> list;
boolean booleanValue;
@ -210,6 +219,8 @@ public class ScriptTest {
if (!inString)
inArray++;
break;
default:
break;
}
}
if (inArray != 0 || closePos == 0)
@ -259,7 +270,8 @@ public class ScriptTest {
@Test
public void dataDrivenValidTransactions() throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("tx_valid.json")));
BufferedReader in = new BufferedReader(new InputStreamReader(
getClass().getResourceAsStream("tx_valid.json"), Charset.forName("UTF-8")));
NetworkParameters params = NetworkParameters.testNet();
@ -269,8 +281,7 @@ public class ScriptTest {
StringBuffer buffer = new StringBuffer(1000);
while (in.ready()) {
String line = in.readLine();
if (line.equals(""))
continue;
if (line == null || line.equals("")) continue;
buffer.append(line);
if (line.equals("]") && buffer.toString().equals("]") && !in.ready())
break;
@ -283,10 +294,12 @@ public class ScriptTest {
String hash = input.list.get(0).string;
int index = input.list.get(1).integer;
String script = input.list.get(2).string;
scriptPubKeys.put(new TransactionOutPoint(params, index, new Sha256Hash(Hex.decode(hash.getBytes()))), parseScriptString(params, script));
Sha256Hash sha256Hash = new Sha256Hash(Hex.decode(hash.getBytes(Charset.forName("UTF-8"))));
scriptPubKeys.put(new TransactionOutPoint(params, index, sha256Hash), parseScriptString(params, script));
}
Transaction transaction = new Transaction(params, Hex.decode(tx.get(0).list.get(1).string.getBytes()));
byte[] bytes = tx.get(0).list.get(1).string.getBytes(Charset.forName("UTF-8"));
Transaction transaction = new Transaction(params, Hex.decode(bytes));
boolean enforceP2SH = tx.get(0).list.get(2).booleanValue;
assertTrue(tx.get(0).list.get(2).isBoolean());
@ -307,17 +320,18 @@ public class ScriptTest {
@Test
public void dataDrivenInvalidTransactions() throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("tx_invalid.json")));
BufferedReader in = new BufferedReader(new InputStreamReader(
getClass().getResourceAsStream("tx_invalid.json"), Charset.forName("UTF-8")));
NetworkParameters params = NetworkParameters.testNet();
// Poor man's (aka. really, really poor) JSON parser (because pulling in a lib for this is probably not overkill)
// Poor man's (aka. really, really poor) JSON parser (because pulling in a lib for this is probably overkill)
List<JSONObject> tx = new ArrayList<JSONObject>(1);
in.read(); // remove first [
StringBuffer buffer = new StringBuffer(1000);
while (in.ready()) {
String line = in.readLine();
if (line.equals(""))
if (line == null || line.equals(""))
continue;
buffer.append(line);
if (line.equals("]") && buffer.toString().equals("]") && !in.ready())
@ -331,10 +345,12 @@ public class ScriptTest {
String hash = input.list.get(0).string;
int index = input.list.get(1).integer;
String script = input.list.get(2).string;
scriptPubKeys.put(new TransactionOutPoint(params, index, new Sha256Hash(Hex.decode(hash.getBytes()))), parseScriptString(params, script));
Sha256Hash sha256Hash = new Sha256Hash(Hex.decode(hash.getBytes(Charset.forName("UTF-8"))));
scriptPubKeys.put(new TransactionOutPoint(params, index, sha256Hash), parseScriptString(params, script));
}
Transaction transaction = new Transaction(params, Hex.decode(tx.get(0).list.get(1).string.getBytes()));
byte[] bytes = tx.get(0).list.get(1).string.getBytes(Charset.forName("UTF-8"));
Transaction transaction = new Transaction(params, Hex.decode(bytes));
boolean enforceP2SH = tx.get(0).list.get(2).booleanValue;
assertTrue(tx.get(0).list.get(2).isBoolean());