- Remove AutoConfirmResult enum from protobuf and add a AutoConfirmResult

instead which stores the stateName. [1]

- Adjust protobuf methods

- Add UNDEFINED to AutoConfirmResult.State to support cases where we
get no data to set the enum.

- Add NO_MATCH_FOUND (used in follow up commits)

- Refactoring: Improve constructors

[1]
Enums in protobuf are not well supported. They are global so an enum
with name (e.g. State) inside Trade conflicts with another enum inside
Message with the same name. So they do not reflect encapsulation in the
class like in java.
We moved over time to the strategy to use strings (from enum.name())
instead of the enum, avoiding also cumbersome fromProto and toProto
code and being more flexible with updates.
The autoConfirmResultState enum inside Trade was a bit confusing to me
as it was a different structure as in the java code. We try to mirror
the structure as far as possible.
This commit is contained in:
chimp1984 2020-08-30 14:39:44 -05:00
parent 5501822128
commit 3e728c69f7
No known key found for this signature in database
GPG Key ID: 9801B4EC591F90E3
3 changed files with 45 additions and 50 deletions

View File

@ -21,15 +21,16 @@ import bisq.core.locale.Res;
import bisq.common.proto.ProtoUtil;
import javax.annotation.Nullable;
import lombok.Value;
import lombok.extern.slf4j.Slf4j;
import javax.annotation.Nullable;
@Slf4j
@Value
public class AutoConfirmResult {
public enum State {
UNDEFINED,
FEATURE_DISABLED,
TX_NOT_FOUND,
TX_NOT_CONFIRMED,
@ -41,27 +42,25 @@ public class AutoConfirmResult {
TX_HASH_INVALID,
TX_KEY_INVALID,
ADDRESS_INVALID,
NO_MATCH_FOUND,
AMOUNT_NOT_MATCHING,
TRADE_LIMIT_EXCEEDED,
TRADE_DATE_NOT_MATCHING;
public static AutoConfirmResult.State fromProto(protobuf.Trade.AutoConfirmResult result) {
return ProtoUtil.enumFromProto(AutoConfirmResult.State.class, result.name());
}
public static protobuf.Trade.AutoConfirmResult toProtoMessage(AutoConfirmResult.State result) {
return protobuf.Trade.AutoConfirmResult.valueOf(result.name());
}
TRADE_DATE_NOT_MATCHING
}
// Only state gets persisted
private final State state;
private final transient int confirmCount;
private final transient int confirmsRequired;
///////////////////////////////////////////////////////////////////////////////////////////
// Constructor
///////////////////////////////////////////////////////////////////////////////////////////
public AutoConfirmResult(State state) {
this.state = state;
this.confirmCount = 0;
this.confirmsRequired = 0;
this(state, 0, 0);
}
// alternate constructor for showing confirmation progress information
@ -73,13 +72,33 @@ public class AutoConfirmResult {
// alternate constructor for error scenarios
public AutoConfirmResult(State state, @Nullable String errorMsg) {
this.state = state;
this.confirmCount = 0;
this.confirmsRequired = 0;
if (isErrorState())
this(state, 0, 0);
if (isErrorState()) {
log.error(errorMsg != null ? errorMsg : state.toString());
}
}
///////////////////////////////////////////////////////////////////////////////////////////
// PROTOBUF
///////////////////////////////////////////////////////////////////////////////////////////
public protobuf.AutoConfirmResult toProtoMessage() {
return protobuf.AutoConfirmResult.newBuilder().setStateName(state.name()).build();
}
public static AutoConfirmResult fromProto(protobuf.AutoConfirmResult proto) {
AutoConfirmResult.State state = ProtoUtil.enumFromProto(AutoConfirmResult.State.class, proto.getStateName());
return state != null ? new AutoConfirmResult(state) : new AutoConfirmResult(State.UNDEFINED);
}
///////////////////////////////////////////////////////////////////////////////////////////
// API
///////////////////////////////////////////////////////////////////////////////////////////
public String getTextStatus() {
switch (state) {
case TX_NOT_CONFIRMED:
@ -109,17 +128,4 @@ public class AutoConfirmResult {
public boolean isErrorState() {
return (!isPendingState() && !isSuccessState());
}
///////////////////////////////////////////////////////////////////////////////////////////
// PROTOBUF
///////////////////////////////////////////////////////////////////////////////////////////
public protobuf.Trade.AutoConfirmResult toProtoMessage() {
return State.toProtoMessage(state);
}
public static AutoConfirmResult fromProto(protobuf.Trade.AutoConfirmResult proto) {
return new AutoConfirmResult(AutoConfirmResult.State.fromProto(proto));
}
}

View File

@ -439,14 +439,16 @@ public abstract class Trade implements Tradable, Model {
@Nullable
private AutoConfirmResult autoConfirmResult;
public void setAutoConfirmResult(AutoConfirmResult autoConfirmResult) {
void setAutoConfirmResult(AutoConfirmResult autoConfirmResult) {
this.autoConfirmResult = autoConfirmResult;
autoConfirmResultProperty.setValue(autoConfirmResult);
}
@Getter
// This observable property can be used for UI to show a notification to user of the XMR proof status
transient final private ObjectProperty<AutoConfirmResult> autoConfirmResultProperty = new SimpleObjectProperty<>();
///////////////////////////////////////////////////////////////////////////////////////////
// Constructor, initialization
///////////////////////////////////////////////////////////////////////////////////////////
@ -593,7 +595,7 @@ public abstract class Trade implements Tradable, Model {
trade.setLockTime(proto.getLockTime());
trade.setLastRefreshRequestDate(proto.getLastRefreshRequestDate());
trade.setCounterCurrencyExtraData(ProtoUtil.stringOrNullFromProto(proto.getCounterCurrencyExtraData()));
trade.setAutoConfirmResult(AutoConfirmResult.fromProto(protobuf.Trade.AutoConfirmResult.valueOf(proto.getAutoConfirmResultValue())));
trade.setAutoConfirmResult(AutoConfirmResult.fromProto(proto.getAutoConfirmResult()));
trade.chatMessages.addAll(proto.getChatMessageList().stream()
.map(ChatMessage::fromPayloadProto)

View File

@ -1356,23 +1356,6 @@ message Trade {
TRADE_PERIOD_OVER = 3;
}
enum AutoConfirmResult {
FEATURE_DISABLED = 0;
TX_NOT_FOUND = 1;
TX_NOT_CONFIRMED = 2;
PROOF_OK = 3;
CONNECTION_FAIL = 4;
API_FAILURE = 5;
API_INVALID = 6;
TX_KEY_REUSED = 7;
TX_HASH_INVALID = 8;
TX_KEY_INVALID = 9;
ADDRESS_INVALID = 10;
AMOUNT_NOT_MATCHING = 11;
TRADE_LIMIT_EXCEEDED = 12;
TRADE_DATE_NOT_MATCHING = 13;
}
Offer offer = 1;
ProcessModel process_model = 2;
string taker_fee_tx_id = 3;
@ -1413,6 +1396,10 @@ message Trade {
AutoConfirmResult auto_confirm_result = 38;
}
message AutoConfirmResult {
string stateName = 1; // name of AutoConfirmResult.State enum
}
message BuyerAsMakerTrade {
Trade trade = 1;
}