forked from Qortal-Forker/qortal
		
	Merge pull request #102 from catbref/faster-qort-buy-PoC
Proof of concept: speed up QORT buying
This commit is contained in:
		@@ -19,6 +19,7 @@ import org.qortal.data.transaction.MessageTransactionData;
 | 
			
		||||
import org.qortal.group.Group;
 | 
			
		||||
import org.qortal.repository.DataException;
 | 
			
		||||
import org.qortal.repository.Repository;
 | 
			
		||||
import org.qortal.repository.RepositoryManager;
 | 
			
		||||
import org.qortal.transaction.DeployAtTransaction;
 | 
			
		||||
import org.qortal.transaction.MessageTransaction;
 | 
			
		||||
import org.qortal.transaction.Transaction.ValidationResult;
 | 
			
		||||
@@ -317,20 +318,27 @@ public class LitecoinACCTv3TradeBot implements AcctTradeBot {
 | 
			
		||||
 | 
			
		||||
		boolean isMessageAlreadySent = repository.getMessageRepository().exists(tradeBotData.getTradeNativePublicKey(), messageRecipient, messageData);
 | 
			
		||||
		if (!isMessageAlreadySent) {
 | 
			
		||||
			PrivateKeyAccount sender = new PrivateKeyAccount(repository, tradeBotData.getTradePrivateKey());
 | 
			
		||||
			MessageTransaction messageTransaction = MessageTransaction.build(repository, sender, Group.NO_GROUP, messageRecipient, messageData, false, false);
 | 
			
		||||
			// Do this in a new thread so caller doesn't have to wait for computeNonce()
 | 
			
		||||
			// In the unlikely event that the transaction doesn't validate then the buy won't happen and eventually Alice's AT will be refunded
 | 
			
		||||
			new Thread(() -> {
 | 
			
		||||
				try (final Repository threadsRepository = RepositoryManager.getRepository()) {
 | 
			
		||||
					PrivateKeyAccount sender = new PrivateKeyAccount(threadsRepository, tradeBotData.getTradePrivateKey());
 | 
			
		||||
					MessageTransaction messageTransaction = MessageTransaction.build(threadsRepository, sender, Group.NO_GROUP, messageRecipient, messageData, false, false);
 | 
			
		||||
 | 
			
		||||
			messageTransaction.computeNonce();
 | 
			
		||||
			messageTransaction.sign(sender);
 | 
			
		||||
					messageTransaction.computeNonce();
 | 
			
		||||
					messageTransaction.sign(sender);
 | 
			
		||||
 | 
			
		||||
			// reset repository state to prevent deadlock
 | 
			
		||||
			repository.discardChanges();
 | 
			
		||||
			ValidationResult result = messageTransaction.importAsUnconfirmed();
 | 
			
		||||
					// reset repository state to prevent deadlock
 | 
			
		||||
					threadsRepository.discardChanges();
 | 
			
		||||
					ValidationResult result = messageTransaction.importAsUnconfirmed();
 | 
			
		||||
 | 
			
		||||
			if (result != ValidationResult.OK) {
 | 
			
		||||
				LOGGER.warn(() -> String.format("Unable to send MESSAGE to Bob's trade-bot %s: %s", messageRecipient, result.name()));
 | 
			
		||||
				return ResponseResult.NETWORK_ISSUE;
 | 
			
		||||
			}
 | 
			
		||||
					if (result != ValidationResult.OK) {
 | 
			
		||||
						LOGGER.warn(() -> String.format("Unable to send MESSAGE to Bob's trade-bot %s: %s", messageRecipient, result.name()));
 | 
			
		||||
					}
 | 
			
		||||
				} catch (DataException e) {
 | 
			
		||||
					LOGGER.warn(() -> String.format("Unable to send MESSAGE to Bob's trade-bot %s: %s", messageRecipient, e.getMessage()));
 | 
			
		||||
				}
 | 
			
		||||
			}, "TradeBot response").start();
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		TradeBot.updateTradeBotState(repository, tradeBotData, () -> String.format("Funding P2SH-A %s. Messaged Bob. Waiting for AT-lock", p2shAddress));
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user