Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dao improvements #1746

Merged
merged 20 commits into from Oct 4, 2018
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 12 additions & 12 deletions common/src/main/proto/pb.proto
Expand Up @@ -1351,8 +1351,8 @@ enum TxType {
COMPENSATION_REQUEST = 8;
BLIND_VOTE = 9;
VOTE_REVEAL = 10;
LOCK_UP = 11;
UN_LOCK = 12;
LOCKUP = 11;
UNLOCK = 12;
}

message TxInput {
Expand Down Expand Up @@ -1385,21 +1385,21 @@ message TxOutput {

enum TxOutputType {
PB_ERROR_TX_OUTPUT_TYPE = 0;
UNDEFINED = 1;
UNDEFINED_OUTPUT = 1;
GENESIS_OUTPUT = 2;
BSQ_OUTPUT = 3;
BTC_OUTPUT = 4;
PROPOSAL_OP_RETURN_OUTPUT = 5;
COMP_REQ_OP_RETURN_OUTPUT = 6;
ISSUANCE_CANDIDATE_OUTPUT = 7;
BLIND_VOTE_LOCK_STAKE_OUTPUT = 8;
BLIND_VOTE_OP_RETURN_OUTPUT = 9;
VOTE_REVEAL_UNLOCK_STAKE_OUTPUT = 10;
VOTE_REVEAL_OP_RETURN_OUTPUT = 11;
BOND_LOCK = 12;
BOND_LOCK_OP_RETURN_OUTPUT = 13;
BOND_UNLOCK = 14;
BOND_UNLOCK_OP_RETURN_OUTPUT = 15;
CONFISCATE_BOND_OP_RETURN_OUTPUT = 7;
ISSUANCE_CANDIDATE_OUTPUT = 8;
BLIND_VOTE_LOCK_STAKE_OUTPUT = 9;
BLIND_VOTE_OP_RETURN_OUTPUT = 10;
VOTE_REVEAL_UNLOCK_STAKE_OUTPUT = 11;
VOTE_REVEAL_OP_RETURN_OUTPUT = 12;
LOCKUP_OUTPUT = 13;
LOCKUP_OP_RETURN_OUTPUT = 14;
UNLOCK_OUTPUT = 15;
INVALID_OUTPUT = 16;
}

Expand Down
Expand Up @@ -90,7 +90,7 @@ public boolean isTxInPhaseAndCycle(BlindVote blindVote) {
String txId = blindVote.getTxId();
Optional<Tx> optionalTx = bsqStateService.getTx(txId);
if (!optionalTx.isPresent()) {
log.warn("Tx is not in bsqStateService. blindVoteTxId={}", txId);
log.debug("Tx is not in bsqStateService. blindVoteTxId={}", txId);
return false;
}

Expand All @@ -100,7 +100,7 @@ public boolean isTxInPhaseAndCycle(BlindVote blindVote) {
return false;
}
if (!periodService.isTxInPhase(txId, DaoPhase.Phase.BLIND_VOTE)) {
log.warn("Tx is not in BLIND_VOTE phase. blindVote={}", blindVote);
log.debug("Tx is not in BLIND_VOTE phase. blindVote={}", blindVote);
return false;
}
return true;
Expand Down
Expand Up @@ -228,7 +228,7 @@ private void onProtectedDataAdded(ProtectedStorageEntry entry) {
log.info("We received a TempProposalPayload and store it to our protectedStoreList. proposalTxId={}",
proposal.getTxId());
} else {
log.warn("We received an invalid proposal from the P2P network. Proposal.txId={}, blockHeight={}",
log.debug("We received an invalid proposal from the P2P network. Proposal.txId={}, blockHeight={}",
proposal.getTxId(), bsqStateService.getChainHeight());
}
}
Expand Down
Expand Up @@ -21,7 +21,7 @@

// Need to be in sync with TxOutputType
enum JsonTxOutputType {
UNDEFINED("Undefined"),
UNDEFINED_OUTPUT("Undefined"),
GENESIS_OUTPUT("Genesis"),
BSQ_OUTPUT("BSQ"),
BTC_OUTPUT("BTC"),
Expand All @@ -33,9 +33,9 @@ enum JsonTxOutputType {
BLIND_VOTE_OP_RETURN_OUTPUT("Blind vote opReturn"),
VOTE_REVEAL_UNLOCK_STAKE_OUTPUT("Vote reveal unlock stake"),
VOTE_REVEAL_OP_RETURN_OUTPUT("Vote reveal opReturn"),
LOCKUP("Lockup"),
LOCKUP_OUTPUT("Lockup"),
LOCKUP_OP_RETURN_OUTPUT("Lockup opReturn"),
UNLOCK("Unlock"),
UNLOCK_OUTPUT("Unlock"),
INVALID_OUTPUT("Invalid");

@Getter
Expand Down
Expand Up @@ -178,7 +178,8 @@ public void onMessage(NetworkEnvelope networkEnvelope, Connection connection) {
"RequestDataHandler.onMessage: connection.getPeersNodeAddressOptional() must be present " +
"at that moment");
cleanup();
log.info("We received from peer {} a {}", nodeAddress.getFullAddress(), getBlocksResponse);
log.info("We received from peer {} a BlocksResponse with {} blocks",
nodeAddress.getFullAddress(), getBlocksResponse.getBlocks().size());
listener.onComplete(getBlocksResponse);
} else {
log.warn("Nonce not matching. That can happen rarely if we get a response after a canceled " +
Expand Down
Expand Up @@ -58,17 +58,16 @@ static Optional<OpReturnType> getOptionalOpReturnTypeCandidate(TempTxOutput temp
* Parse the type of OP_RETURN data and validate it.
*
* @param tempTxOutput The temporary transaction output to parse.
* @param isLastOutput If true, the output being parsed has a non-zero value.
* @return The type of the transaction output, which will be either one of the
* {@code *_OP_RETURN_OUTPUT} values, or {@code UNDEFINED} in case of
* unexpected state.
*/
static TxOutputType getTxOutputType(TempTxOutput tempTxOutput, boolean isLastOutput) {
static TxOutputType getTxOutputType(TempTxOutput tempTxOutput) {
boolean nonZeroOutput = tempTxOutput.getValue() != 0;
byte[] opReturnData = tempTxOutput.getOpReturnData();
checkNotNull(opReturnData, "opReturnData must not be null");

if (nonZeroOutput || !isLastOutput || opReturnData.length < 22) {
if (nonZeroOutput || opReturnData.length < 22) {
log.warn("OP_RETURN data does not match our rules. opReturnData={}",
Utils.HEX.encode(opReturnData));
return TxOutputType.INVALID_OUTPUT;
Expand Down
Expand Up @@ -74,7 +74,7 @@ void process(TxOutputKey txOutputKey, int blockHeight, String txId, int inputInd
// for later verification at the outputs of a reveal tx.
TxOutputType connectedTxOutputType = connectedTxOutput.getTxOutputType();
switch (connectedTxOutputType) {
case UNDEFINED:
case UNDEFINED_OUTPUT:
case GENESIS_OUTPUT:
case BSQ_OUTPUT:
case BTC_OUTPUT:
Expand All @@ -98,7 +98,7 @@ void process(TxOutputKey txOutputKey, int blockHeight, String txId, int inputInd
case VOTE_REVEAL_UNLOCK_STAKE_OUTPUT:
case VOTE_REVEAL_OP_RETURN_OUTPUT:
break;
case LOCKUP:
case LOCKUP_OUTPUT:
// A LOCKUP BSQ txOutput is spent to a corresponding UNLOCK
// txOutput. The UNLOCK can only be spent after lockTime blocks has passed.
if (!optionalSpentLockupTxOutput.isPresent()) {
Expand All @@ -108,7 +108,7 @@ void process(TxOutputKey txOutputKey, int blockHeight, String txId, int inputInd
break;
case LOCKUP_OP_RETURN_OUTPUT:
break;
case UNLOCK:
case UNLOCK_OUTPUT:
// This txInput is Spending an UNLOCK txOutput
spentUnlockConnectedTxOutputs.add(connectedTxOutput);

Expand Down
54 changes: 25 additions & 29 deletions core/src/main/java/bisq/core/dao/node/parser/TxOutputParser.java
Expand Up @@ -51,7 +51,7 @@ public class TxOutputParser {
@Setter
private long availableInputValue = 0;
private int lockTime;
private List<TempTxOutput> bsqOutputs = new ArrayList<>();
private List<TempTxOutput> tempTxOutputs = new ArrayList<>();
@Setter
private int unlockBlockHeight;
@Setter
Expand All @@ -77,38 +77,34 @@ public class TxOutputParser {
this.bsqStateService = bsqStateService;
}

public void commitTxOutputs() {
bsqOutputs.forEach(bsqOutput -> {
bsqStateService.addUnspentTxOutput(TxOutput.fromTempOutput(bsqOutput));
});
}

/**
* This sets all outputs to BTC_OUTPUT and doesn't add any txOutputs to the bsqStateService
*/
public void commitTxOutputsForInvalidTx() {
bsqOutputs.forEach(bsqOutput -> {
bsqOutput.setTxOutputType(TxOutputType.BTC_OUTPUT);
});

}

public void processGenesisTxOutput(TempTx genesisTx) {
for (int i = 0; i < genesisTx.getTempTxOutputs().size(); ++i) {
TempTxOutput tempTxOutput = genesisTx.getTempTxOutputs().get(i);
bsqOutputs.add(tempTxOutput);
tempTxOutputs.add(tempTxOutput);
}
commitTxOutputs();
commitTxOutputsForValidTx();
}

public void commitTxOutputsForValidTx() {
tempTxOutputs.forEach(output -> bsqStateService.addUnspentTxOutput(TxOutput.fromTempOutput(output)));
}

/**
* This sets all outputs to BTC_OUTPUT and doesn't add any txOutputs to the unspentTxOutput map in bsqStateService
*/
public void commitTxOutputsForInvalidTx() {
tempTxOutputs.forEach(output -> output.setTxOutputType(TxOutputType.BTC_OUTPUT));
}

boolean isOpReturnOutput(TempTxOutput txOutput) {
return txOutput.getOpReturnData() != null;
}

void processOpReturnOutput(boolean isLastOutput, TempTxOutput tempTxOutput) {
void processOpReturnOutput(TempTxOutput tempTxOutput) {
byte[] opReturnData = tempTxOutput.getOpReturnData();
if (opReturnData != null) {
handleOpReturnOutput(tempTxOutput, isLastOutput);
handleOpReturnOutput(tempTxOutput);
} else {
log.error("This should be an opReturn output");
}
Expand All @@ -117,9 +113,9 @@ void processOpReturnOutput(boolean isLastOutput, TempTxOutput tempTxOutput) {
/**
* Process a transaction output.
*
* @param isLastOutput If it is the last output
* @param tempTxOutput The TempTxOutput we are parsing
* @param index The index in the outputs
* @param isLastOutput If it is the last output
* @param tempTxOutput The TempTxOutput we are parsing
* @param index The index in the outputs
*/
void processTxOutput(boolean isLastOutput, TempTxOutput tempTxOutput, int index) {
// We do not check for pubKeyScript.scriptType.NULL_DATA because that is only set if dumpBlockchainData is true
Expand Down Expand Up @@ -163,8 +159,8 @@ private void handleUnlockBondTx(TempTxOutput txOutput) {
checkArgument(optionalSpentLockupTxOutput.isPresent(), "optionalSpentLockupTxOutput must be present");
availableInputValue -= optionalSpentLockupTxOutput.get().getValue();

txOutput.setTxOutputType(TxOutputType.UNLOCK);
bsqOutputs.add(txOutput);
txOutput.setTxOutputType(TxOutputType.UNLOCK_OUTPUT);
tempTxOutputs.add(txOutput);

//TODO move up to TxParser
// We should add unlockBlockHeight to TempTxOutput and remove unlockBlockHeight from tempTx
Expand All @@ -191,14 +187,14 @@ private void handleBsqOutput(TempTxOutput txOutput, int index, long txOutputValu
bsqOutput = TxOutputType.VOTE_REVEAL_UNLOCK_STAKE_OUTPUT;
optionalVoteRevealUnlockStakeOutput = Optional.of(txOutput);
} else if (isFirstOutput && opReturnTypeCandidate == OpReturnType.LOCKUP) {
bsqOutput = TxOutputType.LOCKUP;
bsqOutput = TxOutputType.LOCKUP_OUTPUT;
txOutput.setLockTime(lockTime);
optionalLockupOutput = Optional.of(txOutput);
} else {
bsqOutput = TxOutputType.BSQ_OUTPUT;
}
txOutput.setTxOutputType(bsqOutput);
bsqOutputs.add(txOutput);
tempTxOutputs.add(txOutput);

bsqOutputFound = true;
}
Expand All @@ -219,8 +215,8 @@ private void handleBtcOutput(TempTxOutput txOutput, int index) {
}
}

private void handleOpReturnOutput(TempTxOutput tempTxOutput, boolean isLastOutput) {
TxOutputType txOutputType = OpReturnParser.getTxOutputType(tempTxOutput, isLastOutput);
private void handleOpReturnOutput(TempTxOutput tempTxOutput) {
TxOutputType txOutputType = OpReturnParser.getTxOutputType(tempTxOutput);
tempTxOutput.setTxOutputType(txOutputType);

optionalVerifiedOpReturnType = getMappedOpReturnType(txOutputType);
Expand Down
10 changes: 5 additions & 5 deletions core/src/main/java/bisq/core/dao/node/parser/TxParser.java
Expand Up @@ -127,7 +127,7 @@ public Optional<Tx> findTx(RawTx rawTx, String genesisTxId, int genesisBlockHeig
int lastNonOpReturnIndex = lastIndex;
if (txOutputParser.isOpReturnOutput(outputs.get(lastIndex))) {
// TODO(SQ): perhaps the check for isLastOutput could be skipped
ManfredKarrer marked this conversation as resolved.
Show resolved Hide resolved
txOutputParser.processOpReturnOutput(true, outputs.get(lastIndex));
txOutputParser.processOpReturnOutput(outputs.get(lastIndex));
lastNonOpReturnIndex -= 1;
}

Expand All @@ -152,7 +152,7 @@ public Optional<Tx> findTx(RawTx rawTx, String genesisTxId, int genesisBlockHeig
// Process the type of transaction if not already determined to be INVALID
if (tempTx.getTxType() != TxType.INVALID) {
boolean isAnyTxOutputTypeUndefined = tempTx.getTempTxOutputs().stream()
.anyMatch(txOutput -> TxOutputType.UNDEFINED == txOutput.getTxOutputType());
.anyMatch(txOutput -> TxOutputType.UNDEFINED_OUTPUT == txOutput.getTxOutputType());
if (!isAnyTxOutputTypeUndefined) {
// TODO(chirhonul): we don't modify the tempTx within the call below, so maybe we should
// use RawTx?
Expand All @@ -170,8 +170,8 @@ public Optional<Tx> findTx(RawTx rawTx, String genesisTxId, int genesisBlockHeig
}
}

if (tempTx.getTxType() != TxType.INVALID){
txOutputParser.commitTxOutputs();
if (tempTx.getTxType() != TxType.INVALID) {
txOutputParser.commitTxOutputsForValidTx();
} else {
txOutputParser.commitTxOutputsForInvalidTx();
}
Expand Down Expand Up @@ -351,7 +351,7 @@ static TxType getBisqTxType(TempTx tx, boolean hasOpReturnCandidate, long remain
if (bsqFeesBurnt) {
// Burned fee but no opReturn
txType = TxType.PAY_TRADE_FEE;
} else if (tx.getTempTxOutputs().get(0).getTxOutputType() == TxOutputType.UNLOCK) {
} else if (tx.getTempTxOutputs().get(0).getTxOutputType() == TxOutputType.UNLOCK_OUTPUT) {
txType = TxType.UNLOCK;
} else {
log.debug("No burned fee and no OP_RETURN, so this is a TRANSFER_BSQ tx.");
Expand Down