Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public class PrecompiledRetCode {

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

// CNSPrecompiled -51299 ~ -51200
public static final RetCode CODE_VERSION_LENGTH_OVERFLOW =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,15 @@ public RetCode mkdir(String path) throws ContractException {
public List<FileInfo> list(String path) throws ContractException {
try {
String list = bfsPrecompiled.list(path);
if (list.isEmpty()) {
throw new ContractException(
"BfsService: list return empty string, check error msg in blockchain node.");
}
return ObjectMapperFactory.getObjectMapper()
.readValue(list, new TypeReference<List<FileInfo>>() {});
} catch (JsonProcessingException e) {
throw new ContractException(
"CnsService: failed to call selectByName interface, error message: "
+ e.getMessage());
"BfsService: failed to call list interface, error message: " + e.getMessage());
} catch (ContractException e) {
throw ReceiptParser.parseExceptionCall(e);
}
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
* Copyright 2014-2020 [fisco-dev]
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*
*/
package org.fisco.bcos.sdk.contract.precompiled.crud;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.fisco.bcos.sdk.client.Client;
import org.fisco.bcos.sdk.codec.datatypes.generated.tuples.generated.Tuple2;
import org.fisco.bcos.sdk.contract.precompiled.callback.PrecompiledCallback;
import org.fisco.bcos.sdk.contract.precompiled.crud.common.Entry;
import org.fisco.bcos.sdk.contract.precompiled.model.PrecompiledAddress;
import org.fisco.bcos.sdk.crypto.keypair.CryptoKeyPair;
import org.fisco.bcos.sdk.model.PrecompiledConstant;
import org.fisco.bcos.sdk.model.PrecompiledRetCode;
import org.fisco.bcos.sdk.model.RetCode;
import org.fisco.bcos.sdk.model.TransactionReceipt;
import org.fisco.bcos.sdk.model.callback.TransactionCallback;
import org.fisco.bcos.sdk.transaction.codec.decode.ReceiptParser;
import org.fisco.bcos.sdk.transaction.model.exception.ContractException;
import org.fisco.bcos.sdk.utils.StringUtils;

/** This class not support in FISCO BCOS 3.0.0 rc1 Do not use it. */
public class KVTableService {
private final KVTablePrecompiled kvTablePrecompiled;
private static final String VALUE_FIELDS_DELIMITER = ",";

public KVTableService(Client client, CryptoKeyPair credential) {
this.kvTablePrecompiled =
KVTablePrecompiled.load(
client.isWASM()
? PrecompiledAddress.KV_TABLE_PRECOMPILED_NAME
: PrecompiledAddress.KV_TABLE_PRECOMPILED_ADDRESS,
client,
credential);
}

public static String convertValueFieldsToString(List<String> valueFields) {
return StringUtils.join(valueFields, VALUE_FIELDS_DELIMITER);
}

public void checkKey(String key) throws ContractException {
if (key.length() > PrecompiledConstant.TABLE_KEY_MAX_LENGTH) {
throw new ContractException(PrecompiledRetCode.OVER_TABLE_KEY_LENGTH_LIMIT);
}
}

public RetCode createTable(String tableName, String keyFieldName, List<String> valueFields)
throws ContractException {
checkKey(keyFieldName);
String valueFieldsString = convertValueFieldsToString(valueFields);
return ReceiptParser.parseTransactionReceipt(
kvTablePrecompiled.createTable(tableName, keyFieldName, valueFieldsString));
}

public RetCode set(String tableName, String key, Entry fieldNameToValue)
throws ContractException {
return ReceiptParser.parseTransactionReceipt(
kvTablePrecompiled.set(tableName, key, fieldNameToValue.getKVPrecompiledEntry()));
}

public Map<String, String> get(String tableName, String key) throws ContractException {
try {
Tuple2<Boolean, KVTablePrecompiled.Entry> getResult =
kvTablePrecompiled.get(tableName, key);
if (!getResult.getValue1()) {
throw new ContractException("get from " + tableName + " failed, return false.");
}
return parseGetResult(getResult.getValue2());
} catch (ContractException e) {
throw ReceiptParser.parseExceptionCall(e);
}
}

public static Map<String, String> parseGetResult(KVTablePrecompiled.Entry getResult) {
Map<String, String> result = new HashMap<>();
for (KVTablePrecompiled.KVField kvField : getResult.fields.getValue()) {
result.put(kvField.key, kvField.value);
}
return result;
}

public Map<String, String> desc(String tableName) throws ContractException {
Tuple2<String, String> descOutput =
kvTablePrecompiled.getDescOutput(kvTablePrecompiled.desc(tableName));
Map<String, String> tableDesc = new HashMap<>();
tableDesc.put(PrecompiledConstant.KEY_FIELD_NAME, descOutput.getValue1());
tableDesc.put(PrecompiledConstant.VALUE_FIELD_NAME, descOutput.getValue2());
return tableDesc;
}

public void asyncSet(String tableName, String key, Entry entry, PrecompiledCallback callback) {
this.kvTablePrecompiled.set(
tableName,
key,
entry.getKVPrecompiledEntry(),
new TransactionCallback() {
@Override
public void onResponse(TransactionReceipt receipt) {
RetCode retCode = new RetCode();
try {
retCode = ReceiptParser.parseTransactionReceipt(receipt);
} catch (ContractException e) {
retCode.setCode(e.getErrorCode());
retCode.setMessage(e.getMessage());
retCode.setTransactionReceipt(receipt);
}
callback.onResponse(retCode);
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.fisco.bcos.sdk.contract.precompiled.crud.KVTablePrecompiled;
import org.fisco.bcos.sdk.contract.precompiled.crud.TablePrecompiled;

@Deprecated
public class Entry {
private Map<String, String> fieldNameToValue = new HashMap<>();

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

@Deprecated
public Entry(TablePrecompiled.Entry entry) {
for (TablePrecompiled.KVField field : entry.fields.getValue()) {
this.fieldNameToValue.put(field.key, field.value);
}
}

public Entry(KVTablePrecompiled.Entry entry) {
for (KVTablePrecompiled.KVField field : entry.fields.getValue()) {
this.fieldNameToValue.put(field.key, field.value);
}
}

public Map<String, String> getFieldNameToValue() {
return fieldNameToValue;
}

@Deprecated
public TablePrecompiled.Entry getTablePrecompiledEntry() {
List<TablePrecompiled.KVField> fields = new ArrayList<>();
fieldNameToValue.forEach(
Expand All @@ -50,6 +58,16 @@ public TablePrecompiled.Entry getTablePrecompiledEntry() {
return new TablePrecompiled.Entry(fields);
}

public KVTablePrecompiled.Entry getKVPrecompiledEntry() {
List<KVTablePrecompiled.KVField> fields = new ArrayList<>();
fieldNameToValue.forEach(
(String k, String v) -> {
KVTablePrecompiled.KVField kvField = new KVTablePrecompiled.KVField(k, v);
fields.add(kvField);
});
return new KVTablePrecompiled.Entry(fields);
}

public void setFieldNameToValue(Map<String, String> fieldNameToValue) {
this.fieldNameToValue = fieldNameToValue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
package org.fisco.bcos.sdk.contract.precompiled.model;

public class PrecompiledAddress {
public static final String SYSCONFIG_PRECOMPILED_ADDRESS =
public static final String SYS_CONFIG_PRECOMPILED_ADDRESS =
"0000000000000000000000000000000000001000";

@Deprecated
public static final String TABLEFACTORY_PRECOMPILED_ADDRESS =
public static final String TABLE_FACTORY_PRECOMPILED_ADDRESS =
"0000000000000000000000000000000000001001";

public static final String CONSENSUS_PRECOMPILED_ADDRESS =
Expand All @@ -29,12 +29,15 @@ public class PrecompiledAddress {
public static final String COMMITTEE_MANAGER_ADDRESS =
"0000000000000000000000000000000000010001";
public static final String CONTRACT_AUTH_ADDRESS = "0000000000000000000000000000000000001005";
public static final String KV_TABLE_PRECOMPILED_ADDRESS =
"0000000000000000000000000000000000001009";

public static final String SYSCONFIG_PRECOMPILED_NAME = "/sys/status";
public static final String SYS_CONFIG_PRECOMPILED_NAME = "/sys/status";
public static final String CONSENSUS_PRECOMPILED_NAME = "/sys/consensus";
public static final String CNS_PRECOMPILED_NAME = "/sys/cns";
public static final String BFS_PRECOMPILED_NAME = "/sys/bfs";
@Deprecated public static final String TABLEFACTORY_PRECOMPILED_NAME = "/sys/table_storage";
public static final String KV_TABLE_PRECOMPILED_NAME = "/sys/kv_storage";
@Deprecated public static final String TABLE_FACTORY_PRECOMPILED_NAME = "/sys/table_storage";

private PrecompiledAddress() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public SystemConfigService(Client client, CryptoKeyPair credential) {
this.systemConfigPrecompiled =
SystemConfigPrecompiled.load(
client.isWASM()
? PrecompiledAddress.SYSCONFIG_PRECOMPILED_NAME
: PrecompiledAddress.SYSCONFIG_PRECOMPILED_ADDRESS,
? PrecompiledAddress.SYS_CONFIG_PRECOMPILED_NAME
: PrecompiledAddress.SYS_CONFIG_PRECOMPILED_ADDRESS,
client,
credential);
}
Expand Down