Payment channels: make ProtobufParser use finer grained, cycle detecting locking.

This commit is contained in:
Mike Hearn
2013-11-01 13:00:26 +01:00
parent c36989cfce
commit 06ac0105f3
3 changed files with 86 additions and 71 deletions

View File

@@ -134,7 +134,7 @@ public class PaymentChannelClientConnection {
* @throws IllegalStateException If the channel has been closed or is not yet open
* (see {@link PaymentChannelClientConnection#getChannelOpenFuture()} for the second)
*/
public synchronized void incrementPayment(BigInteger size) throws ValueOutOfRangeException, IllegalStateException {
public void incrementPayment(BigInteger size) throws ValueOutOfRangeException, IllegalStateException {
channelClient.incrementPayment(size);
}
@@ -145,7 +145,7 @@ public class PaymentChannelClientConnection {
* <p>Note that if you call any methods which update state directly the server will not be notified and channel
* initialization logic in the connection may fail unexpectedly.</p>
*/
public synchronized PaymentChannelClientState state() {
public PaymentChannelClientState state() {
return channelClient.state();
}
@@ -153,7 +153,7 @@ public class PaymentChannelClientConnection {
* Closes the connection, notifying the server it should close the channel by broadcasting the most recent payment
* transaction.
*/
public synchronized void close() {
public void close() {
// Shutdown is a little complicated.
//
// This call will cause the CLOSE message to be written to the wire, and then the destroyConnection() method that

View File

@@ -17,14 +17,18 @@
package com.google.bitcoin.protocols.niowrapper;
import com.google.bitcoin.core.Utils;
import com.google.bitcoin.utils.Threading;
import com.google.protobuf.ByteString;
import com.google.protobuf.MessageLite;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.concurrent.GuardedBy;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.ReentrantLock;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
@@ -66,10 +70,11 @@ public class ProtobufParser<MessageType extends MessageLite> extends AbstractTim
// a smaller network buffer per client and only allocate more memory when we need it to deserialize large messages.
// Though this is not in of itself a DoS protection, it allows for handling more legitimate clients per server and
// attacking clients can be made to timeout/get blocked if they are sending crap to fill buffers.
private int messageBytesOffset = 0;
private byte[] messageBytes;
@GuardedBy("lock") private int messageBytesOffset = 0;
@GuardedBy("lock") private byte[] messageBytes;
private final ReentrantLock lock = Threading.lock("ProtobufParser");
private MessageWriteTarget writeTarget;
private final AtomicReference<MessageWriteTarget> writeTarget = new AtomicReference<MessageWriteTarget>();
/**
* Creates a new protobuf handler.
@@ -91,9 +96,9 @@ public class ProtobufParser<MessageType extends MessageLite> extends AbstractTim
}
@Override
public synchronized void setWriteTarget(MessageWriteTarget writeTarget) {
checkState(this.writeTarget == null);
this.writeTarget = checkNotNull(writeTarget);
public void setWriteTarget(MessageWriteTarget writeTarget) {
// Only allow it to be set once.
checkState(this.writeTarget.getAndSet(checkNotNull(writeTarget)) == null);
}
@Override
@@ -104,8 +109,8 @@ public class ProtobufParser<MessageType extends MessageLite> extends AbstractTim
/**
* Closes this connection, eventually triggering a {@link ProtobufParser.Listener#connectionClosed()} event.
*/
public synchronized void closeConnection() {
this.writeTarget.closeConnection();
public void closeConnection() {
this.writeTarget.get().closeConnection();
}
@Override
@@ -123,7 +128,9 @@ public class ProtobufParser<MessageType extends MessageLite> extends AbstractTim
}
@Override
public synchronized int receiveBytes(ByteBuffer buff) throws Exception {
public int receiveBytes(ByteBuffer buff) throws Exception {
lock.lock();
try {
if (messageBytes != null) {
// Just keep filling up the currently being worked on message
int bytesToGet = Math.min(messageBytes.length - messageBytesOffset, buff.remaining());
@@ -182,6 +189,9 @@ public class ProtobufParser<MessageType extends MessageLite> extends AbstractTim
return len + 4 + receiveBytes(buff);
else
return len + 4;
} finally {
lock.unlock();
}
}
@Override
@@ -202,14 +212,15 @@ public class ProtobufParser<MessageType extends MessageLite> extends AbstractTim
*
* @throws IllegalStateException If the encoded message is larger than the maximum message size.
*/
public synchronized void write(MessageType msg) throws IllegalStateException {
public void write(MessageType msg) throws IllegalStateException {
byte[] messageBytes = msg.toByteArray();
checkState(messageBytes.length <= maxMessageSize);
byte[] messageLength = new byte[4];
Utils.uint32ToByteArrayBE(messageBytes.length, messageLength, 0);
try {
writeTarget.writeBytes(messageLength);
writeTarget.writeBytes(messageBytes);
MessageWriteTarget target = writeTarget.get();
target.writeBytes(messageLength);
target.writeBytes(messageBytes);
} catch (IOException e) {
closeConnection();
}

View File

@@ -1829,6 +1829,7 @@ public class WalletTest extends TestWithWallet {
wallet.addEventListener(new AbstractWalletEventListener() {
@Override
public void onCoinsReceived(Wallet wallet, Transaction tx, BigInteger prevBalance, BigInteger newBalance) {
log.info("onCoinsReceived 1");
throw new RuntimeException("barf");
}
});
@@ -1836,12 +1837,15 @@ public class WalletTest extends TestWithWallet {
wallet.addEventListener(new AbstractWalletEventListener() {
@Override
public void onCoinsReceived(Wallet wallet, Transaction tx, BigInteger prevBalance, BigInteger newBalance) {
log.info("onCoinsReceived 2");
flag.incrementAndGet();
}
});
sendMoneyToWallet(Utils.toNanoCoins(1, 0), AbstractBlockChain.NewBlockType.BEST_CHAIN);
log.info("Wait for user thread");
Threading.waitForUserCode();
log.info("... and test flag.");
assertEquals(1, flag.get());
}