trade protocol fixed states

This commit is contained in:
Manfred Karrer 2014-07-05 17:13:22 +02:00
parent 16e1267c08
commit 01ca7d3e17
3 changed files with 34 additions and 38 deletions

View file

@ -3,14 +3,10 @@
Bitsquare is a P2P Fiat-BTC Exchange, extensible to a generic P2P trading platform (include commodities and Bitsquare is a P2P Fiat-BTC Exchange, extensible to a generic P2P trading platform (include commodities and
cryptocurrencies) cryptocurrencies)
This is just a proof of concept prototype for demonstrating the basic workflow of the trader process.
It is not at all production code style (no tests, verifications missing, very limited use cases,...).
The project use Java 8 and Maven. The project use Java 8 and Maven.
We use the bitcoinj library and TomP2P for DHT and messaging. We use the bitcoinj library and TomP2P for DHT and messaging.
Test net is currently not working with bitcoinj as the DNS seed servers are not setup correctly (See: http://sourceforge.net/p/bitcoin/mailman/message/32349208/). If you want to use the RegTest mode you need to set regtest=1 in the bitcoin.config file inside the bitcoin data directory (https://en.bitcoin.it/wiki/Running_Bitcoin).
To use the RegTest mode you need to set regtest=1 in the bitcoin.config file inside the bitcoin data directory (https://en.bitcoin.it/wiki/Running_Bitcoin).
Then you can generate coins on demand with the Bitcoin qt client with that command in the console: setgenerate true 101 (101 only for the first start because the coin maturity of 100 blocks). Then you can generate coins on demand with the Bitcoin qt client with that command in the console: setgenerate true 101 (101 only for the first start because the coin maturity of 100 blocks).
See: https://bitcoinj.github.io/testing See: https://bitcoinj.github.io/testing
You can change the network mode in the guice module: BitSquareModule.java You can change the network mode in the guice module: BitSquareModule.java

View file

@ -98,7 +98,7 @@ public class ProtocolForOffererAsBuyer
// state // state
private State state; private State state;
private int position = 0; private int step = 0;
/////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////
@ -140,14 +140,14 @@ public class ProtocolForOffererAsBuyer
public void start() public void start()
{ {
log.debug("start called "); log.debug("start called " + step++);
state = State.HandleTakeOfferRequest; state = State.HandleTakeOfferRequest;
HandleTakeOfferRequest.run(this::onResultHandleTakeOfferRequest, this::onFault, peerAddress, messageFacade, trade.getState(), tradeId); HandleTakeOfferRequest.run(this::onResultHandleTakeOfferRequest, this::onFault, peerAddress, messageFacade, trade.getState(), tradeId);
} }
public void onResultHandleTakeOfferRequest(boolean takeOfferRequestAccepted) public void onResultHandleTakeOfferRequest(boolean takeOfferRequestAccepted)
{ {
log.debug("onResultHandleTakeOfferRequest called "); log.debug("onResultHandleTakeOfferRequest called " + step++);
if (takeOfferRequestAccepted) if (takeOfferRequestAccepted)
{ {
trade.setState(Trade.State.ACCEPTED); trade.setState(Trade.State.ACCEPTED);
@ -168,7 +168,7 @@ public class ProtocolForOffererAsBuyer
public void onTakeOfferFeePayedMessage(@NotNull TakeOfferFeePayedMessage message) public void onTakeOfferFeePayedMessage(@NotNull TakeOfferFeePayedMessage message)
{ {
log.debug("onTakeOfferFeePayedMessage called "); log.debug("onTakeOfferFeePayedMessage called " + step++);
log.debug("state " + state); log.debug("state " + state);
// validation // validation
@ -192,7 +192,7 @@ public class ProtocolForOffererAsBuyer
public void onResultVerifyTakeOfferFeePayment() public void onResultVerifyTakeOfferFeePayment()
{ {
log.debug("onResultVerifyTakeOfferFeePayment called "); log.debug("onResultVerifyTakeOfferFeePayment called " + step++);
BigInteger collateral = trade.getCollateralAmount(); BigInteger collateral = trade.getCollateralAmount();
state = State.CreateDepositTx; state = State.CreateDepositTx;
@ -202,7 +202,7 @@ public class ProtocolForOffererAsBuyer
public void onResultCreateDepositTx(String offererPubKey, String preparedOffererDepositTxAsHex, long offererTxOutIndex) public void onResultCreateDepositTx(String offererPubKey, String preparedOffererDepositTxAsHex, long offererTxOutIndex)
{ {
log.debug("onResultCreateDepositTx called "); log.debug("onResultCreateDepositTx called " + step++);
this.preparedOffererDepositTxAsHex = preparedOffererDepositTxAsHex; this.preparedOffererDepositTxAsHex = preparedOffererDepositTxAsHex;
this.offererTxOutIndex = offererTxOutIndex; this.offererTxOutIndex = offererTxOutIndex;
@ -221,7 +221,7 @@ public class ProtocolForOffererAsBuyer
public void onResultRequestTakerDepositPayment() public void onResultRequestTakerDepositPayment()
{ {
log.debug("onResultRequestTakerDepositPayment called "); log.debug("onResultRequestTakerDepositPayment called " + step++);
listener.onWaitingForPeerResponse(state); listener.onWaitingForPeerResponse(state);
} }
@ -232,7 +232,7 @@ public class ProtocolForOffererAsBuyer
public void onRequestOffererPublishDepositTxMessage(RequestOffererPublishDepositTxMessage message) public void onRequestOffererPublishDepositTxMessage(RequestOffererPublishDepositTxMessage message)
{ {
log.debug("onRequestOffererPublishDepositTxMessage called "); log.debug("onRequestOffererPublishDepositTxMessage called " + step++);
log.debug("state " + state); log.debug("state " + state);
// validation // validation
@ -267,7 +267,7 @@ public class ProtocolForOffererAsBuyer
public void onResultVerifyTakerAccount() public void onResultVerifyTakerAccount()
{ {
log.debug("onResultVerifyTakerAccount called "); log.debug("onResultVerifyTakerAccount called " + step++);
BigInteger tradeAmount = trade.getTradeAmount(); BigInteger tradeAmount = trade.getTradeAmount();
state = State.VerifyAndSignContract; state = State.VerifyAndSignContract;
@ -289,7 +289,7 @@ public class ProtocolForOffererAsBuyer
public void onResultVerifyAndSignContract(Contract contract, String contractAsJson, String signature) public void onResultVerifyAndSignContract(Contract contract, String contractAsJson, String signature)
{ {
log.debug("onResultVerifyAndSignContract called "); log.debug("onResultVerifyAndSignContract called " + step++);
trade.setContract(contract); trade.setContract(contract);
trade.setContractAsJson(contractAsJson); trade.setContractAsJson(contractAsJson);
@ -308,7 +308,7 @@ public class ProtocolForOffererAsBuyer
public void onResultSignAndPublishDepositTx(Transaction depositTransaction) public void onResultSignAndPublishDepositTx(Transaction depositTransaction)
{ {
log.debug("onResultSignAndPublishDepositTx called "); log.debug("onResultSignAndPublishDepositTx called " + step++);
trade.setDepositTransaction(depositTransaction); trade.setDepositTransaction(depositTransaction);
listener.onDepositTxPublished(depositTransaction.getHashAsString()); listener.onDepositTxPublished(depositTransaction.getHashAsString());
@ -319,7 +319,7 @@ public class ProtocolForOffererAsBuyer
public void onResultSendDepositTxIdToTaker() public void onResultSendDepositTxIdToTaker()
{ {
log.debug("onResultSendDepositTxIdToTaker called "); log.debug("onResultSendDepositTxIdToTaker called " + step++);
state = State.SetupListenerForBlockChainConfirmation; state = State.SetupListenerForBlockChainConfirmation;
SetupListenerForBlockChainConfirmation.run(this::onResultSetupListenerForBlockChainConfirmation, this::onFault, trade.getDepositTransaction(), listener); SetupListenerForBlockChainConfirmation.run(this::onResultSetupListenerForBlockChainConfirmation, this::onFault, trade.getDepositTransaction(), listener);
@ -327,7 +327,7 @@ public class ProtocolForOffererAsBuyer
public void onResultSetupListenerForBlockChainConfirmation() public void onResultSetupListenerForBlockChainConfirmation()
{ {
log.debug("onResultSetupListenerForBlockChainConfirmation called "); log.debug("onResultSetupListenerForBlockChainConfirmation called " + step++);
state = State.onResultSetupListenerForBlockChainConfirmation; state = State.onResultSetupListenerForBlockChainConfirmation;
listener.onWaitingForUserInteraction(state); listener.onWaitingForUserInteraction(state);
@ -341,7 +341,7 @@ public class ProtocolForOffererAsBuyer
// Triggered from UI event: Button click "Bank transfer inited" // Triggered from UI event: Button click "Bank transfer inited"
public void onUIEventBankTransferInited() public void onUIEventBankTransferInited()
{ {
log.debug("onUIEventBankTransferInited called "); log.debug("onUIEventBankTransferInited called " + step++);
log.debug("state " + state); log.debug("state " + state);
// validation // validation
@ -369,7 +369,7 @@ public class ProtocolForOffererAsBuyer
public void onResultSendSignedPayoutTx() public void onResultSendSignedPayoutTx()
{ {
log.debug("onResultSendSignedPayoutTx called "); log.debug("onResultSendSignedPayoutTx called " + step++);
listener.onWaitingForPeerResponse(state); listener.onWaitingForPeerResponse(state);
} }
@ -381,7 +381,7 @@ public class ProtocolForOffererAsBuyer
public void onPayoutTxPublishedMessage(PayoutTxPublishedMessage message) public void onPayoutTxPublishedMessage(PayoutTxPublishedMessage message)
{ {
log.debug("onPayoutTxPublishedMessage called "); log.debug("onPayoutTxPublishedMessage called " + step++);
log.debug("state " + state); log.debug("state " + state);
// validation // validation

View file

@ -94,7 +94,7 @@ public class ProtocolForTakerAsSeller
// state // state
private State state; private State state;
private int position = 0; private int step = 0;
/////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////
@ -136,14 +136,14 @@ public class ProtocolForTakerAsSeller
public void start() public void start()
{ {
log.debug("start called " + position++); log.debug("start called " + step++);
state = State.GetPeerAddress; state = State.GetPeerAddress;
GetPeerAddress.run(this::onResultGetPeerAddress, this::onFault, messageFacade, peersMessagePubKey); GetPeerAddress.run(this::onResultGetPeerAddress, this::onFault, messageFacade, peersMessagePubKey);
} }
public void onResultGetPeerAddress(PeerAddress peerAddress) public void onResultGetPeerAddress(PeerAddress peerAddress)
{ {
log.debug("onResultGetPeerAddress called " + position++); log.debug("onResultGetPeerAddress called " + step++);
this.peerAddress = peerAddress; this.peerAddress = peerAddress;
state = State.RequestTakeOffer; state = State.RequestTakeOffer;
@ -152,7 +152,7 @@ public class ProtocolForTakerAsSeller
public void onResultRequestTakeOffer() public void onResultRequestTakeOffer()
{ {
log.debug("onResultRequestTakeOffer called " + position++); log.debug("onResultRequestTakeOffer called " + step++);
listener.onWaitingForPeerResponse(state); listener.onWaitingForPeerResponse(state);
} }
@ -163,7 +163,7 @@ public class ProtocolForTakerAsSeller
public void onRespondToTakeOfferRequestMessage(RespondToTakeOfferRequestMessage message) public void onRespondToTakeOfferRequestMessage(RespondToTakeOfferRequestMessage message)
{ {
log.debug("onRespondToTakeOfferRequestMessage called " + position++); log.debug("onRespondToTakeOfferRequestMessage called " + step++);
log.debug("state " + state); log.debug("state " + state);
checkState(state == State.RequestTakeOffer); checkState(state == State.RequestTakeOffer);
checkArgument(tradeId.equals(message.getTradeId())); checkArgument(tradeId.equals(message.getTradeId()));
@ -181,7 +181,7 @@ public class ProtocolForTakerAsSeller
public void onResultPayTakeOfferFee(String takeOfferFeeTxId) public void onResultPayTakeOfferFee(String takeOfferFeeTxId)
{ {
log.debug("onResultPayTakeOfferFee called " + position++); log.debug("onResultPayTakeOfferFee called " + step++);
trade.setTakeOfferFeeTxID(takeOfferFeeTxId); trade.setTakeOfferFeeTxID(takeOfferFeeTxId);
state = State.SendTakeOfferFeePayedTxId; state = State.SendTakeOfferFeePayedTxId;
@ -190,7 +190,7 @@ public class ProtocolForTakerAsSeller
public void onResultSendTakeOfferFeePayedTxId() public void onResultSendTakeOfferFeePayedTxId()
{ {
log.debug("onResultSendTakeOfferFeePayedTxId called " + position++); log.debug("onResultSendTakeOfferFeePayedTxId called " + step++);
listener.onWaitingForPeerResponse(state); listener.onWaitingForPeerResponse(state);
} }
@ -201,7 +201,7 @@ public class ProtocolForTakerAsSeller
public void onRequestTakerDepositPaymentMessage(RequestTakerDepositPaymentMessage message) public void onRequestTakerDepositPaymentMessage(RequestTakerDepositPaymentMessage message)
{ {
log.debug("onRequestTakerDepositPaymentMessage called " + position++); log.debug("onRequestTakerDepositPaymentMessage called " + step++);
log.debug("state " + state); log.debug("state " + state);
// validation // validation
@ -228,7 +228,7 @@ public class ProtocolForTakerAsSeller
public void onResultVerifyOffererAccount() public void onResultVerifyOffererAccount()
{ {
log.debug("onResultVerifyOffererAccount called " + position++); log.debug("onResultVerifyOffererAccount called " + step++);
String takeOfferFeeTxId = trade.getTakeOfferFeeTxId(); String takeOfferFeeTxId = trade.getTakeOfferFeeTxId();
state = State.CreateAndSignContract; state = State.CreateAndSignContract;
CreateAndSignContract.run(this::onResultCreateAndSignContract, CreateAndSignContract.run(this::onResultCreateAndSignContract,
@ -248,7 +248,7 @@ public class ProtocolForTakerAsSeller
public void onResultCreateAndSignContract(Contract contract, String contractAsJson, String signature) public void onResultCreateAndSignContract(Contract contract, String contractAsJson, String signature)
{ {
log.debug("onResultCreateAndSignContract called " + position++); log.debug("onResultCreateAndSignContract called " + step++);
trade.setContract(contract); trade.setContract(contract);
trade.setContractAsJson(contractAsJson); trade.setContractAsJson(contractAsJson);
@ -260,7 +260,7 @@ public class ProtocolForTakerAsSeller
public void onResultPayDeposit(Transaction signedTakerDepositTx) public void onResultPayDeposit(Transaction signedTakerDepositTx)
{ {
log.debug("onResultPayDeposit called " + position++); log.debug("onResultPayDeposit called " + step++);
String contractAsJson = trade.getContractAsJson(); String contractAsJson = trade.getContractAsJson();
String takerSignature = trade.getTakerSignature(); String takerSignature = trade.getTakerSignature();
@ -282,7 +282,7 @@ public class ProtocolForTakerAsSeller
public void onResultSendSignedTakerDepositTxAsHex() public void onResultSendSignedTakerDepositTxAsHex()
{ {
log.debug("onResultSendSignedTakerDepositTxAsHex called " + position++); log.debug("onResultSendSignedTakerDepositTxAsHex called " + step++);
listener.onWaitingForPeerResponse(state); listener.onWaitingForPeerResponse(state);
} }
@ -294,7 +294,7 @@ public class ProtocolForTakerAsSeller
// informational, does only trigger UI feedback/update // informational, does only trigger UI feedback/update
public void onDepositTxPublishedMessage(DepositTxPublishedMessage message) public void onDepositTxPublishedMessage(DepositTxPublishedMessage message)
{ {
log.debug("onDepositTxPublishedMessage called " + position++); log.debug("onDepositTxPublishedMessage called " + step++);
log.debug("state " + state); log.debug("state " + state);
checkState(state.ordinal() >= State.SendSignedTakerDepositTxAsHex.ordinal()); checkState(state.ordinal() >= State.SendSignedTakerDepositTxAsHex.ordinal());
checkArgument(tradeId.equals(message.getTradeId())); checkArgument(tradeId.equals(message.getTradeId()));
@ -309,7 +309,7 @@ public class ProtocolForTakerAsSeller
// informational, store data for later, does only trigger UI feedback/update // informational, store data for later, does only trigger UI feedback/update
public void onBankTransferInitedMessage(BankTransferInitedMessage message) public void onBankTransferInitedMessage(BankTransferInitedMessage message)
{ {
log.debug("onBankTransferInitedMessage called " + position++); log.debug("onBankTransferInitedMessage called " + step++);
log.debug("state " + state); log.debug("state " + state);
// validate // validate
checkState(state.ordinal() >= State.SendSignedTakerDepositTxAsHex.ordinal() && state.ordinal() < State.SignAndPublishPayoutTx.ordinal()); checkState(state.ordinal() >= State.SendSignedTakerDepositTxAsHex.ordinal() && state.ordinal() < State.SignAndPublishPayoutTx.ordinal());
@ -341,7 +341,7 @@ public class ProtocolForTakerAsSeller
// User clicked the "bank transfer received" button, so we release the funds for pay out // User clicked the "bank transfer received" button, so we release the funds for pay out
public void onUIEventFiatReceived() public void onUIEventFiatReceived()
{ {
log.debug("onUIEventFiatReceived called " + position++); log.debug("onUIEventFiatReceived called " + step++);
log.debug("state " + state); log.debug("state " + state);
checkState(state == State.onBankTransferInitedMessage); checkState(state == State.onBankTransferInitedMessage);
@ -360,7 +360,7 @@ public class ProtocolForTakerAsSeller
public void onResultSignAndPublishPayoutTx(String transactionId, String payoutTxAsHex) public void onResultSignAndPublishPayoutTx(String transactionId, String payoutTxAsHex)
{ {
log.debug("onResultSignAndPublishPayoutTx called " + position++); log.debug("onResultSignAndPublishPayoutTx called " + step++);
listener.onPayoutTxPublished(trade, transactionId); listener.onPayoutTxPublished(trade, transactionId);
state = State.SendPayoutTxToOfferer; state = State.SendPayoutTxToOfferer;
@ -369,7 +369,7 @@ public class ProtocolForTakerAsSeller
public void onResultSendPayoutTxToOfferer() public void onResultSendPayoutTxToOfferer()
{ {
log.debug("onResultSendPayoutTxToOfferer called " + position++); log.debug("onResultSendPayoutTxToOfferer called " + step++);
listener.onCompleted(state); listener.onCompleted(state);
} }