Compare commits
13 Commits
Author | SHA1 | Date |
---|---|---|
Christian Basler | c435e2f79e | |
Christian Basler | d5d3640991 | |
Christian Basler | 64ee41aee8 | |
Christian Basler | d3205336ed | |
Christian Basler | 7b14081c63 | |
Christian Basler | e1173d0619 | |
Christian Basler | f0a5a40edd | |
Christian Basler | 1bc82cdd7d | |
Christian Basler | a880a8c10b | |
Christian Basler | 6a5fe01860 | |
Christian Basler | 5cf6d308f2 | |
Christian Basler | ad97cd0633 | |
Christian Basler | 5043e9ed03 |
|
@ -0,0 +1,9 @@
|
|||
kind: pipeline
|
||||
name: default
|
||||
|
||||
steps:
|
||||
- name: test
|
||||
image: adoptopenjdk/openjdk8:alpine
|
||||
commands:
|
||||
- ./gradlew assemble
|
||||
- ./gradlew check
|
|
@ -69,9 +69,9 @@ public class Plaintext implements Streamable {
|
|||
ackData = builder.ackData;
|
||||
if (builder.ackMessage != null && builder.ackMessage.length > 0) {
|
||||
ackMessage = Factory.getObjectMessage(
|
||||
3,
|
||||
new ByteArrayInputStream(builder.ackMessage),
|
||||
builder.ackMessage.length);
|
||||
3,
|
||||
new ByteArrayInputStream(builder.ackMessage),
|
||||
builder.ackMessage.length);
|
||||
}
|
||||
signature = builder.signature;
|
||||
status = builder.status;
|
||||
|
@ -85,25 +85,25 @@ public class Plaintext implements Streamable {
|
|||
|
||||
public static Plaintext read(Type type, InputStream in) throws IOException {
|
||||
return readWithoutSignature(type, in)
|
||||
.signature(Decode.varBytes(in))
|
||||
.received(UnixTime.now())
|
||||
.build();
|
||||
.signature(Decode.varBytes(in))
|
||||
.received(UnixTime.now())
|
||||
.build();
|
||||
}
|
||||
|
||||
public static Plaintext.Builder readWithoutSignature(Type type, InputStream in) throws IOException {
|
||||
long version = Decode.varInt(in);
|
||||
return new Builder(type)
|
||||
.addressVersion(version)
|
||||
.stream(Decode.varInt(in))
|
||||
.behaviorBitfield(Decode.int32(in))
|
||||
.publicSigningKey(Decode.bytes(in, 64))
|
||||
.publicEncryptionKey(Decode.bytes(in, 64))
|
||||
.nonceTrialsPerByte(version >= 3 ? Decode.varInt(in) : 0)
|
||||
.extraBytes(version >= 3 ? Decode.varInt(in) : 0)
|
||||
.destinationRipe(type == Type.MSG ? Decode.bytes(in, 20) : null)
|
||||
.encoding(Decode.varInt(in))
|
||||
.message(Decode.varBytes(in))
|
||||
.ackMessage(type == Type.MSG ? Decode.varBytes(in) : null);
|
||||
.addressVersion(version)
|
||||
.stream(Decode.varInt(in))
|
||||
.behaviorBitfield(Decode.int32(in))
|
||||
.publicSigningKey(Decode.bytes(in, 64))
|
||||
.publicEncryptionKey(Decode.bytes(in, 64))
|
||||
.nonceTrialsPerByte(version >= 3 ? Decode.varInt(in) : 0)
|
||||
.extraBytes(version >= 3 ? Decode.varInt(in) : 0)
|
||||
.destinationRipe(type == Type.MSG ? Decode.bytes(in, 20) : null)
|
||||
.encoding(Decode.varInt(in))
|
||||
.message(Decode.varBytes(in))
|
||||
.ackMessage(type == Type.MSG ? Decode.varBytes(in) : null);
|
||||
}
|
||||
|
||||
public InventoryVector getInventoryVector() {
|
||||
|
@ -198,6 +198,7 @@ public class Plaintext implements Streamable {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void write(ByteBuffer buffer, boolean includeSignature) {
|
||||
Encode.varInt(from.getVersion(), buffer);
|
||||
Encode.varInt(from.getStream(), buffer);
|
||||
|
@ -263,6 +264,9 @@ public class Plaintext implements Streamable {
|
|||
}
|
||||
|
||||
public void setStatus(Status status) {
|
||||
if (status != Status.RECEIVED && sent == null && status != Status.DRAFT) {
|
||||
sent = UnixTime.now();
|
||||
}
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
|
@ -279,14 +283,16 @@ public class Plaintext implements Streamable {
|
|||
}
|
||||
|
||||
public void updateNextTry() {
|
||||
if (nextTry == null) {
|
||||
if (sent != null && to.has(Feature.DOES_ACK)) {
|
||||
nextTry = UnixTime.now(+ttl);
|
||||
if (to != null) {
|
||||
if (nextTry == null) {
|
||||
if (sent != null && to.has(Feature.DOES_ACK)) {
|
||||
nextTry = UnixTime.now(+ttl);
|
||||
retries++;
|
||||
}
|
||||
} else {
|
||||
nextTry = nextTry + (1 << retries) * ttl;
|
||||
retries++;
|
||||
}
|
||||
} else {
|
||||
nextTry = nextTry + (1 << retries) * ttl;
|
||||
retries++;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -320,15 +326,15 @@ public class Plaintext implements Streamable {
|
|||
if (o == null || getClass() != o.getClass()) return false;
|
||||
Plaintext plaintext = (Plaintext) o;
|
||||
return Objects.equals(encoding, plaintext.encoding) &&
|
||||
Objects.equals(from, plaintext.from) &&
|
||||
Arrays.equals(message, plaintext.message) &&
|
||||
Objects.equals(getAckMessage(), plaintext.getAckMessage()) &&
|
||||
Arrays.equals(to.getRipe(), plaintext.to.getRipe()) &&
|
||||
Arrays.equals(signature, plaintext.signature) &&
|
||||
Objects.equals(status, plaintext.status) &&
|
||||
Objects.equals(sent, plaintext.sent) &&
|
||||
Objects.equals(received, plaintext.received) &&
|
||||
Objects.equals(labels, plaintext.labels);
|
||||
Objects.equals(from, plaintext.from) &&
|
||||
Arrays.equals(message, plaintext.message) &&
|
||||
Objects.equals(getAckMessage(), plaintext.getAckMessage()) &&
|
||||
Arrays.equals(to == null ? null : to.getRipe(), plaintext.to == null ? null : plaintext.to.getRipe()) &&
|
||||
Arrays.equals(signature, plaintext.signature) &&
|
||||
Objects.equals(status, plaintext.status) &&
|
||||
Objects.equals(sent, plaintext.sent) &&
|
||||
Objects.equals(received, plaintext.received) &&
|
||||
Objects.equals(labels, plaintext.labels);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -582,13 +588,13 @@ public class Plaintext implements Streamable {
|
|||
public Plaintext build() {
|
||||
if (from == null) {
|
||||
from = new BitmessageAddress(Factory.createPubkey(
|
||||
addressVersion,
|
||||
stream,
|
||||
publicSigningKey,
|
||||
publicEncryptionKey,
|
||||
nonceTrialsPerByte,
|
||||
extraBytes,
|
||||
behaviorBitfield
|
||||
addressVersion,
|
||||
stream,
|
||||
publicSigningKey,
|
||||
publicEncryptionKey,
|
||||
nonceTrialsPerByte,
|
||||
extraBytes,
|
||||
behaviorBitfield
|
||||
));
|
||||
}
|
||||
if (to == null && type != Type.BROADCAST && destinationRipe != null) {
|
||||
|
|
|
@ -33,6 +33,7 @@ import java.io.IOException;
|
|||
import java.math.BigInteger;
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.Provider;
|
||||
import java.security.SecureRandom;
|
||||
|
||||
import static ch.dissem.bitmessage.InternalContext.NETWORK_EXTRA_BYTES;
|
||||
|
@ -49,10 +50,10 @@ public abstract class AbstractCryptography implements Cryptography, InternalCont
|
|||
private static final BigInteger TWO_POW_64 = TWO.pow(64);
|
||||
private static final BigInteger TWO_POW_16 = TWO.pow(16);
|
||||
|
||||
private final String provider;
|
||||
protected final Provider provider;
|
||||
private InternalContext context;
|
||||
|
||||
protected AbstractCryptography(String provider) {
|
||||
protected AbstractCryptography(Provider provider) {
|
||||
this.provider = provider;
|
||||
}
|
||||
|
||||
|
@ -137,7 +138,6 @@ public abstract class AbstractCryptography implements Cryptography, InternalCont
|
|||
if (extraBytes == 0) extraBytes = NETWORK_EXTRA_BYTES;
|
||||
|
||||
BigInteger TTL = BigInteger.valueOf(object.getExpiresTime() - UnixTime.now());
|
||||
BigInteger numerator = TWO_POW_64;
|
||||
BigInteger powLength = BigInteger.valueOf(object.getPayloadBytesWithoutNonce().length + extraBytes);
|
||||
BigInteger denominator = BigInteger.valueOf(nonceTrialsPerByte)
|
||||
.multiply(
|
||||
|
@ -145,7 +145,7 @@ public abstract class AbstractCryptography implements Cryptography, InternalCont
|
|||
powLength.multiply(TTL).divide(TWO_POW_16)
|
||||
)
|
||||
);
|
||||
return Bytes.expand(numerator.divide(denominator).toByteArray(), 8);
|
||||
return Bytes.expand(TWO_POW_64.divide(denominator).toByteArray(), 8);
|
||||
}
|
||||
|
||||
private byte[] hash(String algorithm, byte[]... data) {
|
||||
|
|
|
@ -52,21 +52,16 @@ import java.util.Arrays;
|
|||
public class BouncyCryptography extends AbstractCryptography {
|
||||
private static final X9ECParameters EC_CURVE_PARAMETERS = CustomNamedCurves.getByName("secp256k1");
|
||||
private static final String ALGORITHM_ECDSA = "ECDSA";
|
||||
private static final String PROVIDER = "BC";
|
||||
|
||||
static {
|
||||
java.security.Security.addProvider(new BouncyCastleProvider());
|
||||
}
|
||||
|
||||
public BouncyCryptography() {
|
||||
super(PROVIDER);
|
||||
super(new BouncyCastleProvider());
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] crypt(boolean encrypt, byte[] data, byte[] key_e, byte[] initializationVector) {
|
||||
BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(
|
||||
new CBCBlockCipher(new AESEngine()),
|
||||
new PKCS7Padding()
|
||||
new CBCBlockCipher(new AESEngine()),
|
||||
new PKCS7Padding()
|
||||
);
|
||||
CipherParameters params = new ParametersWithIV(new KeyParameter(key_e), initializationVector);
|
||||
|
||||
|
@ -100,18 +95,18 @@ public class BouncyCryptography extends AbstractCryptography {
|
|||
public boolean isSignatureValid(byte[] data, byte[] signature, Pubkey pubkey) {
|
||||
try {
|
||||
ECParameterSpec spec = new ECParameterSpec(
|
||||
EC_CURVE_PARAMETERS.getCurve(),
|
||||
EC_CURVE_PARAMETERS.getG(),
|
||||
EC_CURVE_PARAMETERS.getN(),
|
||||
EC_CURVE_PARAMETERS.getH(),
|
||||
EC_CURVE_PARAMETERS.getSeed()
|
||||
EC_CURVE_PARAMETERS.getCurve(),
|
||||
EC_CURVE_PARAMETERS.getG(),
|
||||
EC_CURVE_PARAMETERS.getN(),
|
||||
EC_CURVE_PARAMETERS.getH(),
|
||||
EC_CURVE_PARAMETERS.getSeed()
|
||||
);
|
||||
|
||||
ECPoint Q = keyToPoint(pubkey.getSigningKey());
|
||||
KeySpec keySpec = new ECPublicKeySpec(Q, spec);
|
||||
PublicKey publicKey = KeyFactory.getInstance(ALGORITHM_ECDSA, PROVIDER).generatePublic(keySpec);
|
||||
PublicKey publicKey = KeyFactory.getInstance(ALGORITHM_ECDSA, provider).generatePublic(keySpec);
|
||||
|
||||
Signature sig = Signature.getInstance(ALGORITHM_ECDSA, PROVIDER);
|
||||
Signature sig = Signature.getInstance(ALGORITHM_ECDSA, provider);
|
||||
sig.initVerify(publicKey);
|
||||
sig.update(data);
|
||||
return sig.verify(signature);
|
||||
|
@ -124,19 +119,19 @@ public class BouncyCryptography extends AbstractCryptography {
|
|||
public byte[] getSignature(byte[] data, PrivateKey privateKey) {
|
||||
try {
|
||||
ECParameterSpec spec = new ECParameterSpec(
|
||||
EC_CURVE_PARAMETERS.getCurve(),
|
||||
EC_CURVE_PARAMETERS.getG(),
|
||||
EC_CURVE_PARAMETERS.getN(),
|
||||
EC_CURVE_PARAMETERS.getH(),
|
||||
EC_CURVE_PARAMETERS.getSeed()
|
||||
EC_CURVE_PARAMETERS.getCurve(),
|
||||
EC_CURVE_PARAMETERS.getG(),
|
||||
EC_CURVE_PARAMETERS.getN(),
|
||||
EC_CURVE_PARAMETERS.getH(),
|
||||
EC_CURVE_PARAMETERS.getSeed()
|
||||
);
|
||||
|
||||
BigInteger d = keyToBigInt(privateKey.getPrivateSigningKey());
|
||||
KeySpec keySpec = new ECPrivateKeySpec(d, spec);
|
||||
java.security.PrivateKey privKey = KeyFactory.getInstance(ALGORITHM_ECDSA, PROVIDER)
|
||||
.generatePrivate(keySpec);
|
||||
java.security.PrivateKey privKey = KeyFactory.getInstance(ALGORITHM_ECDSA, provider)
|
||||
.generatePrivate(keySpec);
|
||||
|
||||
Signature sig = Signature.getInstance(ALGORITHM_ECDSA, PROVIDER);
|
||||
Signature sig = Signature.getInstance(ALGORITHM_ECDSA, provider);
|
||||
sig.initSign(privKey);
|
||||
sig.update(data);
|
||||
return sig.sign();
|
||||
|
@ -153,8 +148,8 @@ public class BouncyCryptography extends AbstractCryptography {
|
|||
@Override
|
||||
public byte[] createPoint(byte[] x, byte[] y) {
|
||||
return EC_CURVE_PARAMETERS.getCurve().createPoint(
|
||||
new BigInteger(1, x),
|
||||
new BigInteger(1, y)
|
||||
new BigInteger(1, x),
|
||||
new BigInteger(1, y)
|
||||
).getEncoded(false);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,4 +14,5 @@ dependencies {
|
|||
compile project(':core')
|
||||
compile 'com.madgag.spongycastle:prov:1.52.0.0'
|
||||
testCompile 'junit:junit:4.12'
|
||||
testCompile 'org.mockito:mockito-core:1.10.19'
|
||||
}
|
||||
|
|
|
@ -52,21 +52,16 @@ import java.util.Arrays;
|
|||
public class SpongyCryptography extends AbstractCryptography {
|
||||
private static final X9ECParameters EC_CURVE_PARAMETERS = CustomNamedCurves.getByName("secp256k1");
|
||||
private static final String ALGORITHM_ECDSA = "ECDSA";
|
||||
private static final String PROVIDER = "SC";
|
||||
|
||||
static {
|
||||
java.security.Security.addProvider(new BouncyCastleProvider());
|
||||
}
|
||||
|
||||
public SpongyCryptography() {
|
||||
super(PROVIDER);
|
||||
super(new BouncyCastleProvider());
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] crypt(boolean encrypt, byte[] data, byte[] key_e, byte[] initializationVector) {
|
||||
BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(
|
||||
new CBCBlockCipher(new AESEngine()),
|
||||
new PKCS7Padding()
|
||||
new CBCBlockCipher(new AESEngine()),
|
||||
new PKCS7Padding()
|
||||
);
|
||||
CipherParameters params = new ParametersWithIV(new KeyParameter(key_e), initializationVector);
|
||||
|
||||
|
@ -100,18 +95,18 @@ public class SpongyCryptography extends AbstractCryptography {
|
|||
public boolean isSignatureValid(byte[] data, byte[] signature, Pubkey pubkey) {
|
||||
try {
|
||||
ECParameterSpec spec = new ECParameterSpec(
|
||||
EC_CURVE_PARAMETERS.getCurve(),
|
||||
EC_CURVE_PARAMETERS.getG(),
|
||||
EC_CURVE_PARAMETERS.getN(),
|
||||
EC_CURVE_PARAMETERS.getH(),
|
||||
EC_CURVE_PARAMETERS.getSeed()
|
||||
EC_CURVE_PARAMETERS.getCurve(),
|
||||
EC_CURVE_PARAMETERS.getG(),
|
||||
EC_CURVE_PARAMETERS.getN(),
|
||||
EC_CURVE_PARAMETERS.getH(),
|
||||
EC_CURVE_PARAMETERS.getSeed()
|
||||
);
|
||||
|
||||
ECPoint Q = keyToPoint(pubkey.getSigningKey());
|
||||
KeySpec keySpec = new ECPublicKeySpec(Q, spec);
|
||||
PublicKey publicKey = KeyFactory.getInstance(ALGORITHM_ECDSA, PROVIDER).generatePublic(keySpec);
|
||||
PublicKey publicKey = KeyFactory.getInstance(ALGORITHM_ECDSA, provider).generatePublic(keySpec);
|
||||
|
||||
Signature sig = Signature.getInstance(ALGORITHM_ECDSA, PROVIDER);
|
||||
Signature sig = Signature.getInstance(ALGORITHM_ECDSA, provider);
|
||||
sig.initVerify(publicKey);
|
||||
sig.update(data);
|
||||
return sig.verify(signature);
|
||||
|
@ -124,19 +119,19 @@ public class SpongyCryptography extends AbstractCryptography {
|
|||
public byte[] getSignature(byte[] data, PrivateKey privateKey) {
|
||||
try {
|
||||
ECParameterSpec spec = new ECParameterSpec(
|
||||
EC_CURVE_PARAMETERS.getCurve(),
|
||||
EC_CURVE_PARAMETERS.getG(),
|
||||
EC_CURVE_PARAMETERS.getN(),
|
||||
EC_CURVE_PARAMETERS.getH(),
|
||||
EC_CURVE_PARAMETERS.getSeed()
|
||||
EC_CURVE_PARAMETERS.getCurve(),
|
||||
EC_CURVE_PARAMETERS.getG(),
|
||||
EC_CURVE_PARAMETERS.getN(),
|
||||
EC_CURVE_PARAMETERS.getH(),
|
||||
EC_CURVE_PARAMETERS.getSeed()
|
||||
);
|
||||
|
||||
BigInteger d = keyToBigInt(privateKey.getPrivateSigningKey());
|
||||
KeySpec keySpec = new ECPrivateKeySpec(d, spec);
|
||||
java.security.PrivateKey privKey = KeyFactory.getInstance(ALGORITHM_ECDSA, PROVIDER)
|
||||
.generatePrivate(keySpec);
|
||||
java.security.PrivateKey privKey = KeyFactory.getInstance(ALGORITHM_ECDSA, provider)
|
||||
.generatePrivate(keySpec);
|
||||
|
||||
Signature sig = Signature.getInstance(ALGORITHM_ECDSA, PROVIDER);
|
||||
Signature sig = Signature.getInstance(ALGORITHM_ECDSA, provider);
|
||||
sig.initSign(privKey);
|
||||
sig.update(data);
|
||||
return sig.sign();
|
||||
|
@ -153,8 +148,8 @@ public class SpongyCryptography extends AbstractCryptography {
|
|||
@Override
|
||||
public byte[] createPoint(byte[] x, byte[] y) {
|
||||
return EC_CURVE_PARAMETERS.getCurve().createPoint(
|
||||
new BigInteger(1, x),
|
||||
new BigInteger(1, y)
|
||||
new BigInteger(1, x),
|
||||
new BigInteger(1, y)
|
||||
).getEncoded(false);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,157 @@
|
|||
package ch.dissem.bitmessage.security;
|
||||
|
||||
import ch.dissem.bitmessage.InternalContext;
|
||||
import ch.dissem.bitmessage.cryptography.sc.SpongyCryptography;
|
||||
import ch.dissem.bitmessage.entity.ObjectMessage;
|
||||
import ch.dissem.bitmessage.entity.payload.GenericPayload;
|
||||
import ch.dissem.bitmessage.entity.valueobject.PrivateKey;
|
||||
import ch.dissem.bitmessage.exception.InsufficientProofOfWorkException;
|
||||
import ch.dissem.bitmessage.ports.MultiThreadedPOWEngine;
|
||||
import ch.dissem.bitmessage.ports.ProofOfWorkEngine;
|
||||
import ch.dissem.bitmessage.utils.CallbackWaiter;
|
||||
import ch.dissem.bitmessage.utils.Singleton;
|
||||
import ch.dissem.bitmessage.utils.UnixTime;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.xml.bind.DatatypeConverter;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
import static ch.dissem.bitmessage.utils.UnixTime.DAY;
|
||||
import static ch.dissem.bitmessage.utils.UnixTime.MINUTE;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* @author Christian Basler
|
||||
*/
|
||||
public class CryptographyTest {
|
||||
public static final byte[] TEST_VALUE = "teststring".getBytes();
|
||||
public static final byte[] TEST_SHA1 = DatatypeConverter.parseHexBinary(""
|
||||
+ "b8473b86d4c2072ca9b08bd28e373e8253e865c4");
|
||||
public static final byte[] TEST_SHA512 = DatatypeConverter.parseHexBinary(""
|
||||
+ "6253b39071e5df8b5098f59202d414c37a17d6a38a875ef5f8c7d89b0212b028"
|
||||
+ "692d3d2090ce03ae1de66c862fa8a561e57ed9eb7935ce627344f742c0931d72");
|
||||
public static final byte[] TEST_RIPEMD160 = DatatypeConverter.parseHexBinary(""
|
||||
+ "cd566972b5e50104011a92b59fa8e0b1234851ae");
|
||||
|
||||
private static SpongyCryptography crypto;
|
||||
|
||||
@BeforeClass
|
||||
public static void setUp() {
|
||||
crypto = new SpongyCryptography();
|
||||
Singleton.initialize(crypto);
|
||||
InternalContext ctx = mock(InternalContext.class);
|
||||
when(ctx.getProofOfWorkEngine()).thenReturn(new MultiThreadedPOWEngine());
|
||||
crypto.setContext(ctx);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRipemd160() {
|
||||
assertArrayEquals(TEST_RIPEMD160, crypto.ripemd160(TEST_VALUE));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSha1() {
|
||||
assertArrayEquals(TEST_SHA1, crypto.sha1(TEST_VALUE));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSha512() {
|
||||
assertArrayEquals(TEST_SHA512, crypto.sha512(TEST_VALUE));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testChaining() {
|
||||
assertArrayEquals(TEST_SHA512, crypto.sha512("test".getBytes(), "string".getBytes()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ensureDoubleHashYieldsSameResultAsHashOfHash() {
|
||||
assertArrayEquals(crypto.sha512(TEST_SHA512), crypto.doubleSha512(TEST_VALUE));
|
||||
}
|
||||
|
||||
@Test(expected = IOException.class)
|
||||
public void ensureExceptionForInsufficientProofOfWork() throws IOException {
|
||||
ObjectMessage objectMessage = new ObjectMessage.Builder()
|
||||
.nonce(new byte[8])
|
||||
.expiresTime(UnixTime.now(+28 * DAY))
|
||||
.objectType(0)
|
||||
.payload(GenericPayload.read(0, 1, new ByteArrayInputStream(new byte[0]), 0))
|
||||
.build();
|
||||
crypto.checkProofOfWork(objectMessage, 1000, 1000);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoProofOfWork() throws Exception {
|
||||
ObjectMessage objectMessage = new ObjectMessage.Builder()
|
||||
.nonce(new byte[8])
|
||||
.expiresTime(UnixTime.now(+2 * MINUTE))
|
||||
.objectType(0)
|
||||
.payload(GenericPayload.read(0, 1, new ByteArrayInputStream(new byte[0]), 0))
|
||||
.build();
|
||||
final CallbackWaiter<byte[]> waiter = new CallbackWaiter<>();
|
||||
crypto.doProofOfWork(objectMessage, 1000, 1000,
|
||||
new ProofOfWorkEngine.Callback() {
|
||||
@Override
|
||||
public void onNonceCalculated(byte[] initialHash, byte[] nonce) {
|
||||
waiter.setValue(nonce);
|
||||
}
|
||||
});
|
||||
objectMessage.setNonce(waiter.waitForValue());
|
||||
try {
|
||||
crypto.checkProofOfWork(objectMessage, 1000, 1000);
|
||||
} catch (InsufficientProofOfWorkException e) {
|
||||
fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ensureEncryptionAndDecryptionWorks() {
|
||||
byte[] data = crypto.randomBytes(100);
|
||||
byte[] key_e = crypto.randomBytes(32);
|
||||
byte[] iv = crypto.randomBytes(16);
|
||||
byte[] encrypted = crypto.crypt(true, data, key_e, iv);
|
||||
byte[] decrypted = crypto.crypt(false, encrypted, key_e, iv);
|
||||
assertArrayEquals(data, decrypted);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void ensureDecryptionFailsWithInvalidCypherText() {
|
||||
byte[] data = crypto.randomBytes(128);
|
||||
byte[] key_e = crypto.randomBytes(32);
|
||||
byte[] iv = crypto.randomBytes(16);
|
||||
crypto.crypt(false, data, key_e, iv);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMultiplication() {
|
||||
byte[] a = crypto.randomBytes(PrivateKey.PRIVATE_KEY_SIZE);
|
||||
byte[] A = crypto.createPublicKey(a);
|
||||
|
||||
byte[] b = crypto.randomBytes(PrivateKey.PRIVATE_KEY_SIZE);
|
||||
byte[] B = crypto.createPublicKey(b);
|
||||
|
||||
assertArrayEquals(crypto.multiply(A, b), crypto.multiply(B, a));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ensureSignatureIsValid() {
|
||||
byte[] data = crypto.randomBytes(100);
|
||||
PrivateKey privateKey = new PrivateKey(false, 1, 1000, 1000);
|
||||
byte[] signature = crypto.getSignature(data, privateKey);
|
||||
assertThat(crypto.isSignatureValid(data, signature, privateKey.getPubkey()), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ensureSignatureIsInvalidForTemperedData() {
|
||||
byte[] data = crypto.randomBytes(100);
|
||||
PrivateKey privateKey = new PrivateKey(false, 1, 1000, 1000);
|
||||
byte[] signature = crypto.getSignature(data, privateKey);
|
||||
data[0]++;
|
||||
assertThat(crypto.isSignatureValid(data, signature, privateKey.getPubkey()), is(false));
|
||||
}
|
||||
}
|
|
@ -1,4 +1,20 @@
|
|||
#!/usr/bin/env bash
|
||||
#!/usr/bin/env sh
|
||||
|
||||
#
|
||||
# Copyright 2015 the original author or authors.
|
||||
#
|
||||
# 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
|
||||
#
|
||||
# https://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.
|
||||
#
|
||||
|
||||
##############################################################################
|
||||
##
|
||||
|
@ -6,42 +22,6 @@
|
|||
##
|
||||
##############################################################################
|
||||
|
||||
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||
DEFAULT_JVM_OPTS=""
|
||||
|
||||
APP_NAME="Gradle"
|
||||
APP_BASE_NAME=`basename "$0"`
|
||||
|
||||
# Use the maximum available, or set MAX_FD != -1 to use that value.
|
||||
MAX_FD="maximum"
|
||||
|
||||
warn ( ) {
|
||||
echo "$*"
|
||||
}
|
||||
|
||||
die ( ) {
|
||||
echo
|
||||
echo "$*"
|
||||
echo
|
||||
exit 1
|
||||
}
|
||||
|
||||
# OS specific support (must be 'true' or 'false').
|
||||
cygwin=false
|
||||
msys=false
|
||||
darwin=false
|
||||
case "`uname`" in
|
||||
CYGWIN* )
|
||||
cygwin=true
|
||||
;;
|
||||
Darwin* )
|
||||
darwin=true
|
||||
;;
|
||||
MINGW* )
|
||||
msys=true
|
||||
;;
|
||||
esac
|
||||
|
||||
# Attempt to set APP_HOME
|
||||
# Resolve links: $0 may be a link
|
||||
PRG="$0"
|
||||
|
@ -60,8 +40,49 @@ cd "`dirname \"$PRG\"`/" >/dev/null
|
|||
APP_HOME="`pwd -P`"
|
||||
cd "$SAVED" >/dev/null
|
||||
|
||||
APP_NAME="Gradle"
|
||||
APP_BASE_NAME=`basename "$0"`
|
||||
|
||||
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
|
||||
|
||||
# Use the maximum available, or set MAX_FD != -1 to use that value.
|
||||
MAX_FD="maximum"
|
||||
|
||||
warn () {
|
||||
echo "$*"
|
||||
}
|
||||
|
||||
die () {
|
||||
echo
|
||||
echo "$*"
|
||||
echo
|
||||
exit 1
|
||||
}
|
||||
|
||||
# OS specific support (must be 'true' or 'false').
|
||||
cygwin=false
|
||||
msys=false
|
||||
darwin=false
|
||||
nonstop=false
|
||||
case "`uname`" in
|
||||
CYGWIN* )
|
||||
cygwin=true
|
||||
;;
|
||||
Darwin* )
|
||||
darwin=true
|
||||
;;
|
||||
MSYS* | MINGW* )
|
||||
msys=true
|
||||
;;
|
||||
NONSTOP* )
|
||||
nonstop=true
|
||||
;;
|
||||
esac
|
||||
|
||||
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
|
||||
|
||||
|
||||
# Determine the Java command to use to start the JVM.
|
||||
if [ -n "$JAVA_HOME" ] ; then
|
||||
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
|
||||
|
@ -85,7 +106,7 @@ location of your Java installation."
|
|||
fi
|
||||
|
||||
# Increase the maximum file descriptors if we can.
|
||||
if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
|
||||
if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
|
||||
MAX_FD_LIMIT=`ulimit -H -n`
|
||||
if [ $? -eq 0 ] ; then
|
||||
if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
|
||||
|
@ -105,10 +126,11 @@ if $darwin; then
|
|||
GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
|
||||
fi
|
||||
|
||||
# For Cygwin, switch paths to Windows format before running java
|
||||
if $cygwin ; then
|
||||
# For Cygwin or MSYS, switch paths to Windows format before running java
|
||||
if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
|
||||
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
|
||||
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
|
||||
|
||||
JAVACMD=`cygpath --unix "$JAVACMD"`
|
||||
|
||||
# We build the pattern for arguments to be converted via cygpath
|
||||
|
@ -134,27 +156,30 @@ if $cygwin ; then
|
|||
else
|
||||
eval `echo args$i`="\"$arg\""
|
||||
fi
|
||||
i=$((i+1))
|
||||
i=`expr $i + 1`
|
||||
done
|
||||
case $i in
|
||||
(0) set -- ;;
|
||||
(1) set -- "$args0" ;;
|
||||
(2) set -- "$args0" "$args1" ;;
|
||||
(3) set -- "$args0" "$args1" "$args2" ;;
|
||||
(4) set -- "$args0" "$args1" "$args2" "$args3" ;;
|
||||
(5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
|
||||
(6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
|
||||
(7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
|
||||
(8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
|
||||
(9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
|
||||
0) set -- ;;
|
||||
1) set -- "$args0" ;;
|
||||
2) set -- "$args0" "$args1" ;;
|
||||
3) set -- "$args0" "$args1" "$args2" ;;
|
||||
4) set -- "$args0" "$args1" "$args2" "$args3" ;;
|
||||
5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
|
||||
6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
|
||||
7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
|
||||
8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
|
||||
9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
|
||||
esac
|
||||
fi
|
||||
|
||||
# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
|
||||
function splitJvmOpts() {
|
||||
JVM_OPTS=("$@")
|
||||
# Escape application args
|
||||
save () {
|
||||
for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
|
||||
echo " "
|
||||
}
|
||||
eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
|
||||
JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
|
||||
APP_ARGS=`save "$@"`
|
||||
|
||||
exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
|
||||
# Collect all arguments for the java command, following the shell quoting and substitution rules
|
||||
eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
|
||||
|
||||
exec "$JAVACMD" "$@"
|
||||
|
|
|
@ -54,7 +54,7 @@ public abstract class AbstractConnection {
|
|||
protected final NetworkHandler.MessageListener listener;
|
||||
protected final Map<InventoryVector, Long> ivCache;
|
||||
protected final Deque<MessagePayload> sendingQueue;
|
||||
protected final Set<InventoryVector> commonRequestedObjects;
|
||||
protected final Map<InventoryVector, Long> commonRequestedObjects;
|
||||
protected final Set<InventoryVector> requestedObjects;
|
||||
|
||||
protected volatile State state;
|
||||
|
@ -71,7 +71,7 @@ public abstract class AbstractConnection {
|
|||
|
||||
public AbstractConnection(InternalContext context, Mode mode,
|
||||
NetworkAddress node,
|
||||
Set<InventoryVector> commonRequestedObjects,
|
||||
Map<InventoryVector, Long> commonRequestedObjects,
|
||||
long syncTimeout) {
|
||||
this.ctx = context;
|
||||
this.mode = mode;
|
||||
|
@ -143,7 +143,7 @@ public abstract class AbstractConnection {
|
|||
int originalSize = inv.getInventory().size();
|
||||
updateIvCache(inv.getInventory());
|
||||
List<InventoryVector> missing = ctx.getInventory().getMissing(inv.getInventory(), streams);
|
||||
missing.removeAll(commonRequestedObjects);
|
||||
missing.removeAll(commonRequestedObjects.keySet());
|
||||
LOG.trace("Received inventory with " + originalSize + " elements, of which are "
|
||||
+ missing.size() + " missing.");
|
||||
send(new GetData.Builder().inventory(missing).build());
|
||||
|
@ -175,7 +175,7 @@ public abstract class AbstractConnection {
|
|||
} catch (IOException e) {
|
||||
LOG.error("Stream " + objectMessage.getStream() + ", object type " + objectMessage.getType() + ": " + e.getMessage(), e);
|
||||
} finally {
|
||||
if (!commonRequestedObjects.remove(objectMessage.getInventoryVector())) {
|
||||
if (commonRequestedObjects.remove(objectMessage.getInventoryVector()) == null) {
|
||||
LOG.debug("Received object that wasn't requested.");
|
||||
}
|
||||
}
|
||||
|
@ -205,6 +205,10 @@ public abstract class AbstractConnection {
|
|||
return ivCache.containsKey(iv);
|
||||
}
|
||||
|
||||
public boolean requested(InventoryVector iv) {
|
||||
return requestedObjects.contains(iv);
|
||||
}
|
||||
|
||||
private void cleanupIvCache() {
|
||||
Long fiveMinutesAgo = UnixTime.now(-5 * MINUTE);
|
||||
for (Map.Entry<InventoryVector, Long> entry : ivCache.entrySet()) {
|
||||
|
|
|
@ -36,9 +36,9 @@ import java.net.InetAddress;
|
|||
import java.net.InetSocketAddress;
|
||||
import java.net.Socket;
|
||||
import java.net.SocketTimeoutException;
|
||||
import java.util.HashSet;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
import static ch.dissem.bitmessage.networking.AbstractConnection.Mode.CLIENT;
|
||||
import static ch.dissem.bitmessage.networking.AbstractConnection.Mode.SYNC;
|
||||
|
@ -64,20 +64,20 @@ class Connection extends AbstractConnection {
|
|||
private boolean socketInitialized;
|
||||
|
||||
public Connection(InternalContext context, Mode mode, Socket socket,
|
||||
Set<InventoryVector> requestedObjectsMap) throws IOException {
|
||||
Map<InventoryVector, Long> requestedObjectsMap) throws IOException {
|
||||
this(context, mode, socket, requestedObjectsMap,
|
||||
new NetworkAddress.Builder().ip(socket.getInetAddress()).port(socket.getPort()).stream(1).build(),
|
||||
0);
|
||||
new NetworkAddress.Builder().ip(socket.getInetAddress()).port(socket.getPort()).stream(1).build(),
|
||||
0);
|
||||
}
|
||||
|
||||
public Connection(InternalContext context, Mode mode, NetworkAddress node,
|
||||
Set<InventoryVector> requestedObjectsMap) {
|
||||
Map<InventoryVector, Long> requestedObjectsMap) {
|
||||
this(context, mode, new Socket(), requestedObjectsMap,
|
||||
node, 0);
|
||||
node, 0);
|
||||
}
|
||||
|
||||
private Connection(InternalContext context, Mode mode, Socket socket,
|
||||
Set<InventoryVector> commonRequestedObjects, NetworkAddress node, long syncTimeout) {
|
||||
Map<InventoryVector, Long> commonRequestedObjects, NetworkAddress node, long syncTimeout) {
|
||||
super(context, mode, node, commonRequestedObjects, syncTimeout);
|
||||
this.startTime = UnixTime.now();
|
||||
this.socket = socket;
|
||||
|
@ -86,9 +86,9 @@ class Connection extends AbstractConnection {
|
|||
public static Connection sync(InternalContext ctx, InetAddress address, int port, MessageListener listener,
|
||||
long timeoutInSeconds) throws IOException {
|
||||
return new Connection(ctx, SYNC, new Socket(address, port),
|
||||
new HashSet<InventoryVector>(),
|
||||
new NetworkAddress.Builder().ip(address).port(port).stream(1).build(),
|
||||
timeoutInSeconds);
|
||||
new HashMap<InventoryVector, Long>(),
|
||||
new NetworkAddress.Builder().ip(address).port(port).stream(1).build(),
|
||||
timeoutInSeconds);
|
||||
}
|
||||
|
||||
public long getStartTime() {
|
||||
|
|
|
@ -18,7 +18,6 @@ package ch.dissem.bitmessage.networking;
|
|||
|
||||
import ch.dissem.bitmessage.InternalContext;
|
||||
import ch.dissem.bitmessage.entity.valueobject.NetworkAddress;
|
||||
import ch.dissem.bitmessage.ports.NetworkHandler;
|
||||
import ch.dissem.bitmessage.utils.UnixTime;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
@ -32,12 +31,13 @@ import static ch.dissem.bitmessage.networking.DefaultNetworkHandler.NETWORK_MAGI
|
|||
/**
|
||||
* @author Christian Basler
|
||||
*/
|
||||
@Deprecated
|
||||
@SuppressWarnings("deprecation")
|
||||
public class ConnectionOrganizer implements Runnable {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(ConnectionOrganizer.class);
|
||||
|
||||
private final InternalContext ctx;
|
||||
private final DefaultNetworkHandler networkHandler;
|
||||
private final NetworkHandler.MessageListener listener;
|
||||
|
||||
private Connection initialConnection;
|
||||
|
||||
|
@ -45,7 +45,6 @@ public class ConnectionOrganizer implements Runnable {
|
|||
DefaultNetworkHandler networkHandler) {
|
||||
this.ctx = ctx;
|
||||
this.networkHandler = networkHandler;
|
||||
this.listener = ctx.getNetworkListener();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -87,7 +86,7 @@ public class ConnectionOrganizer implements Runnable {
|
|||
|
||||
if (active < NETWORK_MAGIC_NUMBER) {
|
||||
List<NetworkAddress> addresses = ctx.getNodeRegistry().getKnownAddresses(
|
||||
NETWORK_MAGIC_NUMBER - active, ctx.getStreams());
|
||||
NETWORK_MAGIC_NUMBER - active, ctx.getStreams());
|
||||
boolean first = active == 0 && initialConnection == null;
|
||||
for (NetworkAddress address : addresses) {
|
||||
Connection c = new Connection(ctx, CLIENT, address, networkHandler.requestedObjects);
|
||||
|
|
|
@ -39,7 +39,6 @@ import static ch.dissem.bitmessage.networking.AbstractConnection.Mode.SERVER;
|
|||
import static ch.dissem.bitmessage.networking.AbstractConnection.State.ACTIVE;
|
||||
import static ch.dissem.bitmessage.utils.DebugUtils.inc;
|
||||
import static ch.dissem.bitmessage.utils.ThreadFactoryBuilder.pool;
|
||||
import static java.util.Collections.newSetFromMap;
|
||||
|
||||
/**
|
||||
* Handles all the networky stuff.
|
||||
|
@ -59,7 +58,7 @@ public class DefaultNetworkHandler implements NetworkHandler, ContextHolder {
|
|||
private ServerRunnable server;
|
||||
private volatile boolean running;
|
||||
|
||||
final Set<InventoryVector> requestedObjects = newSetFromMap(new ConcurrentHashMap<InventoryVector, Boolean>(50_000));
|
||||
final Map<InventoryVector, Long> requestedObjects = new ConcurrentHashMap<>(50_000);
|
||||
|
||||
@Override
|
||||
public void setContext(InternalContext context) {
|
||||
|
|
|
@ -31,6 +31,7 @@ import static ch.dissem.bitmessage.networking.AbstractConnection.Mode.SERVER;
|
|||
/**
|
||||
* @author Christian Basler
|
||||
*/
|
||||
@Deprecated
|
||||
public class ServerRunnable implements Runnable, Closeable {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(ServerRunnable.class);
|
||||
private final InternalContext ctx;
|
||||
|
|
|
@ -26,11 +26,10 @@ import ch.dissem.bitmessage.entity.valueobject.NetworkAddress;
|
|||
import ch.dissem.bitmessage.exception.NodeException;
|
||||
import ch.dissem.bitmessage.factory.V3MessageReader;
|
||||
import ch.dissem.bitmessage.networking.AbstractConnection;
|
||||
import ch.dissem.bitmessage.utils.UnixTime;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.Iterator;
|
||||
import java.util.Queue;
|
||||
import java.util.Set;
|
||||
import java.util.*;
|
||||
|
||||
import static ch.dissem.bitmessage.networking.AbstractConnection.Mode.CLIENT;
|
||||
import static ch.dissem.bitmessage.networking.AbstractConnection.Mode.SYNC;
|
||||
|
@ -46,7 +45,7 @@ public class ConnectionInfo extends AbstractConnection {
|
|||
private long lastUpdate = System.currentTimeMillis();
|
||||
|
||||
public ConnectionInfo(InternalContext context, Mode mode, NetworkAddress node,
|
||||
Set<InventoryVector> commonRequestedObjects, long syncTimeout) {
|
||||
Map<InventoryVector, Long> commonRequestedObjects, long syncTimeout) {
|
||||
super(context, mode, node, commonRequestedObjects, syncTimeout);
|
||||
headerOut.flip();
|
||||
if (mode == CLIENT || mode == SYNC) {
|
||||
|
@ -147,8 +146,12 @@ public class ConnectionInfo extends AbstractConnection {
|
|||
protected void send(MessagePayload payload) {
|
||||
sendingQueue.add(payload);
|
||||
if (payload instanceof GetData) {
|
||||
requestedObjects.addAll(((GetData) payload).getInventory());
|
||||
commonRequestedObjects.addAll(((GetData) payload).getInventory());
|
||||
Long now = UnixTime.now();
|
||||
List<InventoryVector> inventory = ((GetData) payload).getInventory();
|
||||
requestedObjects.addAll(inventory);
|
||||
for (InventoryVector iv : inventory) {
|
||||
commonRequestedObjects.put(iv, now);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -46,14 +46,14 @@ import static ch.dissem.bitmessage.utils.Collections.selectRandom;
|
|||
import static ch.dissem.bitmessage.utils.DebugUtils.inc;
|
||||
import static ch.dissem.bitmessage.utils.ThreadFactoryBuilder.pool;
|
||||
import static java.nio.channels.SelectionKey.*;
|
||||
import static java.util.Collections.newSetFromMap;
|
||||
|
||||
/**
|
||||
* Network handler using java.nio, resulting in less threads.
|
||||
*/
|
||||
public class NioNetworkHandler implements NetworkHandler, InternalContext.ContextHolder {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(NioNetworkHandler.class);
|
||||
private static final long REQUESTED_OBJECTS_MAX_TIME = 30 * 60_000; // 30 minutes
|
||||
private static final long REQUESTED_OBJECTS_MAX_TIME = 2 * 60_000; // 2 minutes
|
||||
private static final Long DELAYED = Long.MIN_VALUE;
|
||||
|
||||
private final ExecutorService threadPool = Executors.newCachedThreadPool(
|
||||
pool("network")
|
||||
|
@ -66,8 +66,7 @@ public class NioNetworkHandler implements NetworkHandler, InternalContext.Contex
|
|||
private ServerSocketChannel serverChannel;
|
||||
private Queue<NetworkAddress> connectionQueue = new ConcurrentLinkedQueue<>();
|
||||
private Map<ConnectionInfo, SelectionKey> connections = new ConcurrentHashMap<>();
|
||||
private final Set<InventoryVector> requestedObjects = newSetFromMap(new ConcurrentHashMap<InventoryVector, Boolean>(10_000));
|
||||
private long requestedObjectsTimeout = 0;
|
||||
private final Map<InventoryVector, Long> requestedObjects = new ConcurrentHashMap<>(10_000);
|
||||
|
||||
private Thread starter;
|
||||
|
||||
|
@ -80,7 +79,7 @@ public class NioNetworkHandler implements NetworkHandler, InternalContext.Contex
|
|||
channel.configureBlocking(false);
|
||||
ConnectionInfo connection = new ConnectionInfo(ctx, SYNC,
|
||||
new NetworkAddress.Builder().ip(server).port(port).stream(1).build(),
|
||||
new HashSet<InventoryVector>(), timeoutInSeconds);
|
||||
new HashMap<InventoryVector, Long>(), timeoutInSeconds);
|
||||
while (channel.isConnected() && !connection.isSyncFinished()) {
|
||||
write(channel, connection);
|
||||
read(channel, connection);
|
||||
|
@ -147,7 +146,6 @@ public class NioNetworkHandler implements NetworkHandler, InternalContext.Contex
|
|||
} catch (IOException e) {
|
||||
throw new ApplicationException(e);
|
||||
}
|
||||
requestedObjectsTimeout = System.currentTimeMillis() + REQUESTED_OBJECTS_MAX_TIME;
|
||||
requestedObjects.clear();
|
||||
|
||||
starter = thread("connection manager", new Runnable() {
|
||||
|
@ -189,15 +187,22 @@ public class NioNetworkHandler implements NetworkHandler, InternalContext.Contex
|
|||
// The list 'requested objects' helps to prevent downloading an object
|
||||
// twice. From time to time there is an error though, and an object is
|
||||
// never downloaded. To prevent a large list of failed objects and give
|
||||
// them a chance to get downloaded again, let's clear the list from time
|
||||
// to time. The timeout should be such that most of the initial object
|
||||
// sync should be done by then, but small enough to prevent objects with
|
||||
// a normal time out from not being downloaded at all.
|
||||
long now = System.currentTimeMillis();
|
||||
if (now > requestedObjectsTimeout) {
|
||||
requestedObjectsTimeout = now + REQUESTED_OBJECTS_MAX_TIME;
|
||||
requestedObjects.clear();
|
||||
// them a chance to get downloaded again, we will attempt to download an
|
||||
// object from another node after some time out.
|
||||
long timedOut = System.currentTimeMillis() - REQUESTED_OBJECTS_MAX_TIME;
|
||||
List<InventoryVector> delayed = new LinkedList<>();
|
||||
Iterator<Map.Entry<InventoryVector, Long>> iterator = requestedObjects.entrySet().iterator();
|
||||
while (iterator.hasNext()) {
|
||||
Map.Entry<InventoryVector, Long> e = iterator.next();
|
||||
//noinspection NumberEquality
|
||||
if (e.getValue() == DELAYED) {
|
||||
iterator.remove();
|
||||
} else if (e.getValue() < timedOut) {
|
||||
delayed.add(e.getKey());
|
||||
e.setValue(DELAYED);
|
||||
}
|
||||
}
|
||||
request(delayed);
|
||||
|
||||
try {
|
||||
Thread.sleep(30_000);
|
||||
|
@ -422,7 +427,7 @@ public class NioNetworkHandler implements NetworkHandler, InternalContext.Contex
|
|||
break;
|
||||
}
|
||||
}
|
||||
if (connection.knowsOf(next)) {
|
||||
if (connection.knowsOf(next) && !connection.requested(next)) {
|
||||
List<InventoryVector> ivs = distribution.get(connection);
|
||||
if (ivs.size() == GetData.MAX_INVENTORY_SIZE) {
|
||||
connection.send(new GetData.Builder().inventory(ivs).build());
|
||||
|
@ -442,7 +447,9 @@ public class NioNetworkHandler implements NetworkHandler, InternalContext.Contex
|
|||
} while (iterator.hasNext());
|
||||
|
||||
// remove objects nobody knows of
|
||||
requestedObjects.removeAll(inventoryVectors);
|
||||
for (InventoryVector iv : inventoryVectors) {
|
||||
requestedObjects.remove(iv);
|
||||
}
|
||||
|
||||
for (ConnectionInfo connection : distribution.keySet()) {
|
||||
List<InventoryVector> ivs = distribution.get(connection);
|
||||
|
|
|
@ -76,6 +76,7 @@ public class NetworkHandlerTest {
|
|||
}
|
||||
|
||||
@Parameterized.Parameters
|
||||
@SuppressWarnings("deprecation")
|
||||
public static List<Object[]> parameters() {
|
||||
return Arrays.asList(new Object[][]{
|
||||
{new DefaultNetworkHandler(), new DefaultNetworkHandler()},
|
||||
|
|
Loading…
Reference in New Issue