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

fix: map null value #10

Merged
merged 2 commits into from
Dec 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/main/java/com/baidu/xuper/api/JsonUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,11 @@ public static XchainOuterClass.PreExecWithSelectUTXOResponse json2PreExecWithSel
}
if (r.args != null) {
for (Map.Entry<String, byte[]> entry : r.args.entrySet()) {
ireqBuilder.putArgs(entry.getKey(), ByteString.copyFrom(entry.getValue()));
if (entry.getValue() == null) {
ireqBuilder.putArgs(entry.getKey(), ByteString.EMPTY);
} else {
ireqBuilder.putArgs(entry.getKey(), ByteString.copyFrom(entry.getValue()));
}
}
}
if (r.resource_limits != null) {
Expand Down Expand Up @@ -325,7 +329,11 @@ public static String TxStatus2Json(XchainOuterClass.TxStatus txStatus) {
if (item.getArgsMap().size() != 0) {
HashMap<String, byte[]> m = new HashMap<>();
for (Map.Entry<String, ByteString> entry : item.getArgsMap().entrySet()) {
m.put(entry.getKey(), entry.getValue().toByteArray());
if (entry.getValue().isEmpty()) {
m.put(entry.getKey(), null);
} else {
m.put(entry.getKey(), entry.getValue().toByteArray());
}
}
newIR.args = m;
}
Expand Down Expand Up @@ -409,7 +417,8 @@ public static String TxStatus2Json(XchainOuterClass.TxStatus txStatus) {

return JSON.toJSONString(result,
SerializerFeature.NotWriteRootClassName,
SerializerFeature.NotWriteDefaultValue
SerializerFeature.NotWriteDefaultValue,
SerializerFeature.WriteMapNullValue
);
}

Expand Down
13 changes: 12 additions & 1 deletion src/main/java/com/baidu/xuper/api/Transaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ private XchainOuterClass.TxInput[] genPureTxInputs(XchainOuterClass.UtxoOutput u
.setRefTxid(utxo.getRefTxid())
.setRefOffset(utxo.getRefOffset())
.setFromAddr(utxo.getToAddr())
.setAmount(ByteString.copyFrom(amount.toByteArray()))
.setAmount(ByteString.copyFrom(toByteArray(amount)))
.build();
}

Expand Down Expand Up @@ -505,4 +505,15 @@ public long getGasUsed() {
public XchainOuterClass.Transaction getRawTx() {
return pbtx;
}

// unsigned
private byte[] toByteArray(BigInteger bi) {
byte[] array = bi.toByteArray();
if (array[0] == 0) {
byte[] tmp = new byte[array.length - 1];
System.arraycopy(array, 1, tmp, 0, tmp.length);
array = tmp;
}
return array;
}
}
10 changes: 9 additions & 1 deletion src/main/java/com/baidu/xuper/api/TxEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@

import java.lang.reflect.Type;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;

class TxEncoder {
//private static int anInt;
private static final Gson gson = new GsonBuilder()
.serializeNulls()
.registerTypeHierarchyAdapter(ByteString.class, new PbByteStringAdapter())
.disableHtmlEscaping()
.create();
Expand Down Expand Up @@ -166,7 +168,13 @@ static Object create(InvokeRequest pb) {
}
if (pb.getArgsCount() != 0) {
TreeMap<String, ByteString> margs = new TreeMap<>();
margs.putAll(pb.getArgsMap());
for (Map.Entry<String, ByteString> entry : pb.getArgsMap().entrySet()) {
if (entry.getValue().isEmpty()) {
margs.put(entry.getKey(), null);
} else {
margs.put(entry.getKey(), entry.getValue());
}
}
m.put("args", margs);
}
if (pb.getResourceLimitsCount() != 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ private WordList getWordList(int language) {
case 2:
return English.INSTANCE;
default:
return null;
throw new IllegalStateException("Unexpected value: " + language);
}
}

Expand Down