Skip to content

Commit 2c92ac6

Browse files
authored
Merge pull request #427 from kyonRay/release-3.1.0
<fix>(precompiled): add kv table services.
2 parents 3893f17 + 4fcf244 commit 2c92ac6

File tree

7 files changed

+466
-10
lines changed

7 files changed

+466
-10
lines changed

sdk-core/src/main/java/org/fisco/bcos/sdk/model/PrecompiledRetCode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public class PrecompiledRetCode {
109109

110110
// SystemConfigPrecompiled -51399 ~ -51300
111111
public static final RetCode CODE_INVALID_CONFIGURATION_VALUES =
112-
new RetCode(-51300, "Invalid configuration entry");
112+
new RetCode(-51300, "Invalid configuration value");
113113

114114
// CNSPrecompiled -51299 ~ -51200
115115
public static final RetCode CODE_VERSION_LENGTH_OVERFLOW =

sdk-transaction/src/main/java/org/fisco/bcos/sdk/contract/precompiled/bfs/BFSService.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,15 @@ public RetCode mkdir(String path) throws ContractException {
3131
public List<FileInfo> list(String path) throws ContractException {
3232
try {
3333
String list = bfsPrecompiled.list(path);
34+
if (list.isEmpty()) {
35+
throw new ContractException(
36+
"BfsService: list return empty string, check error msg in blockchain node.");
37+
}
3438
return ObjectMapperFactory.getObjectMapper()
3539
.readValue(list, new TypeReference<List<FileInfo>>() {});
3640
} catch (JsonProcessingException e) {
3741
throw new ContractException(
38-
"CnsService: failed to call selectByName interface, error message: "
39-
+ e.getMessage());
42+
"BfsService: failed to call list interface, error message: " + e.getMessage());
4043
} catch (ContractException e) {
4144
throw ReceiptParser.parseExceptionCall(e);
4245
}

sdk-transaction/src/main/java/org/fisco/bcos/sdk/contract/precompiled/crud/KVTablePrecompiled.java

Lines changed: 308 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
* Copyright 2014-2020 [fisco-dev]
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*
14+
*/
15+
package org.fisco.bcos.sdk.contract.precompiled.crud;
16+
17+
import java.util.HashMap;
18+
import java.util.List;
19+
import java.util.Map;
20+
import org.fisco.bcos.sdk.client.Client;
21+
import org.fisco.bcos.sdk.codec.datatypes.generated.tuples.generated.Tuple2;
22+
import org.fisco.bcos.sdk.contract.precompiled.callback.PrecompiledCallback;
23+
import org.fisco.bcos.sdk.contract.precompiled.crud.common.Entry;
24+
import org.fisco.bcos.sdk.contract.precompiled.model.PrecompiledAddress;
25+
import org.fisco.bcos.sdk.crypto.keypair.CryptoKeyPair;
26+
import org.fisco.bcos.sdk.model.PrecompiledConstant;
27+
import org.fisco.bcos.sdk.model.PrecompiledRetCode;
28+
import org.fisco.bcos.sdk.model.RetCode;
29+
import org.fisco.bcos.sdk.model.TransactionReceipt;
30+
import org.fisco.bcos.sdk.model.callback.TransactionCallback;
31+
import org.fisco.bcos.sdk.transaction.codec.decode.ReceiptParser;
32+
import org.fisco.bcos.sdk.transaction.model.exception.ContractException;
33+
import org.fisco.bcos.sdk.utils.StringUtils;
34+
35+
/** This class not support in FISCO BCOS 3.0.0 rc1 Do not use it. */
36+
public class KVTableService {
37+
private final KVTablePrecompiled kvTablePrecompiled;
38+
private static final String VALUE_FIELDS_DELIMITER = ",";
39+
40+
public KVTableService(Client client, CryptoKeyPair credential) {
41+
this.kvTablePrecompiled =
42+
KVTablePrecompiled.load(
43+
client.isWASM()
44+
? PrecompiledAddress.KV_TABLE_PRECOMPILED_NAME
45+
: PrecompiledAddress.KV_TABLE_PRECOMPILED_ADDRESS,
46+
client,
47+
credential);
48+
}
49+
50+
public static String convertValueFieldsToString(List<String> valueFields) {
51+
return StringUtils.join(valueFields, VALUE_FIELDS_DELIMITER);
52+
}
53+
54+
public void checkKey(String key) throws ContractException {
55+
if (key.length() > PrecompiledConstant.TABLE_KEY_MAX_LENGTH) {
56+
throw new ContractException(PrecompiledRetCode.OVER_TABLE_KEY_LENGTH_LIMIT);
57+
}
58+
}
59+
60+
public RetCode createTable(String tableName, String keyFieldName, List<String> valueFields)
61+
throws ContractException {
62+
checkKey(keyFieldName);
63+
String valueFieldsString = convertValueFieldsToString(valueFields);
64+
return ReceiptParser.parseTransactionReceipt(
65+
kvTablePrecompiled.createTable(tableName, keyFieldName, valueFieldsString));
66+
}
67+
68+
public RetCode set(String tableName, String key, Entry fieldNameToValue)
69+
throws ContractException {
70+
return ReceiptParser.parseTransactionReceipt(
71+
kvTablePrecompiled.set(tableName, key, fieldNameToValue.getKVPrecompiledEntry()));
72+
}
73+
74+
public Map<String, String> get(String tableName, String key) throws ContractException {
75+
try {
76+
Tuple2<Boolean, KVTablePrecompiled.Entry> getResult =
77+
kvTablePrecompiled.get(tableName, key);
78+
if (!getResult.getValue1()) {
79+
throw new ContractException("get from " + tableName + " failed, return false.");
80+
}
81+
return parseGetResult(getResult.getValue2());
82+
} catch (ContractException e) {
83+
throw ReceiptParser.parseExceptionCall(e);
84+
}
85+
}
86+
87+
public static Map<String, String> parseGetResult(KVTablePrecompiled.Entry getResult) {
88+
Map<String, String> result = new HashMap<>();
89+
for (KVTablePrecompiled.KVField kvField : getResult.fields.getValue()) {
90+
result.put(kvField.key, kvField.value);
91+
}
92+
return result;
93+
}
94+
95+
public Map<String, String> desc(String tableName) throws ContractException {
96+
Tuple2<String, String> descOutput =
97+
kvTablePrecompiled.getDescOutput(kvTablePrecompiled.desc(tableName));
98+
Map<String, String> tableDesc = new HashMap<>();
99+
tableDesc.put(PrecompiledConstant.KEY_FIELD_NAME, descOutput.getValue1());
100+
tableDesc.put(PrecompiledConstant.VALUE_FIELD_NAME, descOutput.getValue2());
101+
return tableDesc;
102+
}
103+
104+
public void asyncSet(String tableName, String key, Entry entry, PrecompiledCallback callback) {
105+
this.kvTablePrecompiled.set(
106+
tableName,
107+
key,
108+
entry.getKVPrecompiledEntry(),
109+
new TransactionCallback() {
110+
@Override
111+
public void onResponse(TransactionReceipt receipt) {
112+
RetCode retCode = new RetCode();
113+
try {
114+
retCode = ReceiptParser.parseTransactionReceipt(receipt);
115+
} catch (ContractException e) {
116+
retCode.setCode(e.getErrorCode());
117+
retCode.setMessage(e.getMessage());
118+
retCode.setTransactionReceipt(receipt);
119+
}
120+
callback.onResponse(retCode);
121+
}
122+
});
123+
}
124+
}

sdk-transaction/src/main/java/org/fisco/bcos/sdk/contract/precompiled/crud/common/Entry.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
import java.util.HashMap;
1919
import java.util.List;
2020
import java.util.Map;
21+
import org.fisco.bcos.sdk.contract.precompiled.crud.KVTablePrecompiled;
2122
import org.fisco.bcos.sdk.contract.precompiled.crud.TablePrecompiled;
2223

23-
@Deprecated
2424
public class Entry {
2525
private Map<String, String> fieldNameToValue = new HashMap<>();
2626

@@ -30,16 +30,24 @@ public Entry(Map<String, String> fieldNameToValue) {
3030
this.fieldNameToValue = fieldNameToValue;
3131
}
3232

33+
@Deprecated
3334
public Entry(TablePrecompiled.Entry entry) {
3435
for (TablePrecompiled.KVField field : entry.fields.getValue()) {
3536
this.fieldNameToValue.put(field.key, field.value);
3637
}
3738
}
3839

40+
public Entry(KVTablePrecompiled.Entry entry) {
41+
for (KVTablePrecompiled.KVField field : entry.fields.getValue()) {
42+
this.fieldNameToValue.put(field.key, field.value);
43+
}
44+
}
45+
3946
public Map<String, String> getFieldNameToValue() {
4047
return fieldNameToValue;
4148
}
4249

50+
@Deprecated
4351
public TablePrecompiled.Entry getTablePrecompiledEntry() {
4452
List<TablePrecompiled.KVField> fields = new ArrayList<>();
4553
fieldNameToValue.forEach(
@@ -50,6 +58,16 @@ public TablePrecompiled.Entry getTablePrecompiledEntry() {
5058
return new TablePrecompiled.Entry(fields);
5159
}
5260

61+
public KVTablePrecompiled.Entry getKVPrecompiledEntry() {
62+
List<KVTablePrecompiled.KVField> fields = new ArrayList<>();
63+
fieldNameToValue.forEach(
64+
(String k, String v) -> {
65+
KVTablePrecompiled.KVField kvField = new KVTablePrecompiled.KVField(k, v);
66+
fields.add(kvField);
67+
});
68+
return new KVTablePrecompiled.Entry(fields);
69+
}
70+
5371
public void setFieldNameToValue(Map<String, String> fieldNameToValue) {
5472
this.fieldNameToValue = fieldNameToValue;
5573
}

sdk-transaction/src/main/java/org/fisco/bcos/sdk/contract/precompiled/model/PrecompiledAddress.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
package org.fisco.bcos.sdk.contract.precompiled.model;
1616

1717
public class PrecompiledAddress {
18-
public static final String SYSCONFIG_PRECOMPILED_ADDRESS =
18+
public static final String SYS_CONFIG_PRECOMPILED_ADDRESS =
1919
"0000000000000000000000000000000000001000";
2020

2121
@Deprecated
22-
public static final String TABLEFACTORY_PRECOMPILED_ADDRESS =
22+
public static final String TABLE_FACTORY_PRECOMPILED_ADDRESS =
2323
"0000000000000000000000000000000000001001";
2424

2525
public static final String CONSENSUS_PRECOMPILED_ADDRESS =
@@ -29,12 +29,15 @@ public class PrecompiledAddress {
2929
public static final String COMMITTEE_MANAGER_ADDRESS =
3030
"0000000000000000000000000000000000010001";
3131
public static final String CONTRACT_AUTH_ADDRESS = "0000000000000000000000000000000000001005";
32+
public static final String KV_TABLE_PRECOMPILED_ADDRESS =
33+
"0000000000000000000000000000000000001009";
3234

33-
public static final String SYSCONFIG_PRECOMPILED_NAME = "/sys/status";
35+
public static final String SYS_CONFIG_PRECOMPILED_NAME = "/sys/status";
3436
public static final String CONSENSUS_PRECOMPILED_NAME = "/sys/consensus";
3537
public static final String CNS_PRECOMPILED_NAME = "/sys/cns";
3638
public static final String BFS_PRECOMPILED_NAME = "/sys/bfs";
37-
@Deprecated public static final String TABLEFACTORY_PRECOMPILED_NAME = "/sys/table_storage";
39+
public static final String KV_TABLE_PRECOMPILED_NAME = "/sys/kv_storage";
40+
@Deprecated public static final String TABLE_FACTORY_PRECOMPILED_NAME = "/sys/table_storage";
3841

3942
private PrecompiledAddress() {}
4043
}

sdk-transaction/src/main/java/org/fisco/bcos/sdk/contract/precompiled/sysconfig/SystemConfigService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ public SystemConfigService(Client client, CryptoKeyPair credential) {
2828
this.systemConfigPrecompiled =
2929
SystemConfigPrecompiled.load(
3030
client.isWASM()
31-
? PrecompiledAddress.SYSCONFIG_PRECOMPILED_NAME
32-
: PrecompiledAddress.SYSCONFIG_PRECOMPILED_ADDRESS,
31+
? PrecompiledAddress.SYS_CONFIG_PRECOMPILED_NAME
32+
: PrecompiledAddress.SYS_CONFIG_PRECOMPILED_ADDRESS,
3333
client,
3434
credential);
3535
}

0 commit comments

Comments
 (0)