Skip to content

Commit

Permalink
Merge pull request FISCO-BCOS#316 from FISCO-BCOS/feature-txdecode
Browse files Browse the repository at this point in the history
add input/output/event decode.
  • Loading branch information
bxq2011hust authored Jun 17, 2019
2 parents 1d1fddc + 0584ee0 commit be67e83
Show file tree
Hide file tree
Showing 39 changed files with 8,785 additions and 39 deletions.
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ Go to [issues page](https://github.com/FISCO-BCOS/web3sdk/issues)
The code formatting tool are described by the [google-java-format-gradle-plugin](https://github.com/sherter/google-java-format-gradle-plugin).

Execute the task `googleJavaFormat` to format all *.java files in the project
```
```bash
./gradlew goJF
```
Execute the task `verifyGoogleJavaFormat` to verify that all *.java files are formatted properly
```
```bash
./gradlew verGJF
```

Expand Down
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ dependencies {
'junit:junit:4.12',
'org.mockito:mockito-core:2.23.0'
compile 'de.vandermeer:asciitable:0.3.2'
compile 'org.projectlombok:lombok:1.18.2'
}

//archivesBaseName = 'web3sdk'
Expand Down
4 changes: 2 additions & 2 deletions doc/CONTRIBUTING_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@
代码格式化gradle插件[google-java-format-gradle-plugin](https://github.com/sherter/google-java-format-gradle-plugin).

执行任务 `googleJavaFormat`格式化java文件。
```
```bash
./gradlew goJF
```
执行任务 `verifyGoogleJavaFormat`验证java文件是否格式化完成
```
```bash
./gradlew verGJF
```

Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.5-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-5.3.1-all.zip
56 changes: 56 additions & 0 deletions solidity/Table.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
pragma solidity ^0.4.24;

contract TableFactory {
function openTable(string) public constant returns (Table); //open table
function createTable(string,string,string) public returns(int); //create table
}

//select condition
contract Condition {
function EQ(string, int) public;
function EQ(string, string) public;

function NE(string, int) public;
function NE(string, string) public;

function GT(string, int) public;
function GE(string, int) public;

function LT(string, int) public;
function LE(string, int) public;

function limit(int) public;
function limit(int, int) public;
}

//one record
contract Entry {
function getInt(string) public constant returns(int);
function getAddress(string) public constant returns(address);
function getBytes64(string) public constant returns(byte[64]);
function getBytes32(string) public constant returns(bytes32);

function set(string, int) public;
function set(string, string) public;
}

//record sets
contract Entries {
function get(int) public constant returns(Entry);
function size() public constant returns(int);
}

//Table main contract
contract Table {
//select api
function select(string, Condition) public constant returns(Entries);
//insert api
function insert(string, Entry) public returns(int);
//update api
function update(string, Entry, Condition) public returns(int);
//remove api
function remove(string, Condition) public returns(int);

function newEntry() public constant returns(Entry);
function newCondition() public constant returns(Condition);
}
94 changes: 94 additions & 0 deletions solidity/TableTest.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
pragma solidity ^0.4.24;

import "./Table.sol";

contract TableTest {
event CreateResult(int count);
event InsertResult(int count, string name, bytes8 item_name, bytes32[] item_id);
event UpdateResult(int count, string name);
event RemoveResult(int count);

//create table
function create() public returns(int){
TableFactory tf = TableFactory(0x1001); //The fixed address is 0x1001 for TableFactory

int count = tf.createTable("t_test", "name", "item_id,item_name");
emit CreateResult(count);
return count;
}

//select records
function select(string name) public constant returns(bytes32[], int[], bytes32[]){
TableFactory tf = TableFactory(0x1001);
Table table = tf.openTable("t_test");

Condition condition = table.newCondition();

Entries entries = table.select(name, condition);
bytes32[] memory user_name_bytes_list = new bytes32[](uint256(entries.size()));
int[] memory item_id_list = new int[](uint256(entries.size()));
bytes32[] memory item_name_bytes_list = new bytes32[](uint256(entries.size()));

for(int i=0; i<entries.size(); ++i) {
Entry entry = entries.get(i);

user_name_bytes_list[uint256(i)] = entry.getBytes32("name");
item_id_list[uint256(i)] = entry.getInt("item_id");
item_name_bytes_list[uint256(i)] = entry.getBytes32("item_name");
}

return (user_name_bytes_list, item_id_list, item_name_bytes_list);
}
//insert records
function insert(string name, int item_id, string item_name) public returns(int result, string result_name, bytes32, address addr, bytes32[]) {
TableFactory tf = TableFactory(0x1001);
Table table = tf.openTable("t_test");

Entry entry = table.newEntry();
entry.set("name", name);
entry.set("item_id", item_id);
entry.set("item_name", item_name);

int count = table.insert(name, entry);

bytes32[] memory bytes_list = new bytes32[](uint256(2));
bytes_list[0] = "fisco";
bytes_list[1] = "bcos";

emit InsertResult(count, "fruit", "apple", bytes_list);
emit UpdateResult(count, "fruit");

return (count, "hello", "world", 0x12, bytes_list);
}
//update records
function update(string name, int item_id, string item_name) public returns(int) {
TableFactory tf = TableFactory(0x1001);
Table table = tf.openTable("t_test");

Entry entry = table.newEntry();
entry.set("item_name", item_name);

Condition condition = table.newCondition();
condition.EQ("name", name);
condition.EQ("item_id", item_id);

int count = table.update(name, entry, condition);
emit UpdateResult(count, "fruit");

return count;
}
//remove records
function remove(string name, int item_id) public returns(int){
TableFactory tf = TableFactory(0x1001);
Table table = tf.openTable("t_test");

Condition condition = table.newCondition();
condition.EQ("name", name);
condition.EQ("item_id", item_id);

int count = table.remove(name, condition);
emit RemoveResult(count);

return count;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
public class CRUDServiceTest extends TestBase {


CRUDSerivce crudSerivce = new CRUDSerivce(web3j, credentials);
private CRUDSerivce crudSerivce = new CRUDSerivce(web3j, credentials);

@SuppressWarnings("unchecked")
@Test
Expand Down Expand Up @@ -66,7 +66,7 @@ public void curdTest() throws Exception {
// select records
Condition condition2 = table.getCondition();
condition2.EQ("item_id", "1");
condition2.Limit(1);;
condition2.Limit(1);
List<Map<String, String>> resultSelect2 = crudSerivce.select(table, condition2);
assertEquals(resultSelect2.get(0).get("name"), "fruit");
assertEquals(resultSelect2.get(0).get("item_id"), "1");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
public class PermissionServiceTest extends TestBase {


PermissionService permissionService = new PermissionService(web3j, credentials);
private PermissionService permissionService = new PermissionService(web3j, credentials);

@Test(expected= PrecompileMessageException.class)
public void userTableManager() throws Exception {

String grantUserTableManagerResult = permissionService.grantUserTableManager("tt", Common.TX_ORIGIN);
permissionService.grantUserTableManager("tt", Common.TX_ORIGIN);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
public class SystemConfigServiceTest extends TestBase {


SystemConfigService systemConfigSerivce = new SystemConfigService(web3j, credentials);
private SystemConfigService systemConfigSerivce = new SystemConfigService(web3j, credentials);

@Test
public void setValueByKey() throws Exception {
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/org/fisco/bcos/channel/client/P12Manager.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class P12Manager {
private final String NAME = "key";
private String password;
private KeyStore keyStore;
PKCS12KeyStoreSpi p12KeyStore;
private PKCS12KeyStoreSpi p12KeyStore;

public P12Manager() {
Security.setProperty("crypto.policy", "unlimited");
Expand Down Expand Up @@ -75,9 +75,7 @@ public PublicKey getPublicKey(String password)
bcW.getAffineXCoord().toBigInteger(), bcW.getAffineYCoord().toBigInteger());
ECPublicKeySpec keySpec = new ECPublicKeySpec(w, tryFindNamedCurveSpec(params));
return (PublicKey)
KeyFactory.getInstance(
"EC",
org.bouncycastle.jce.provider.BouncyCastleProvider.PROVIDER_NAME)
KeyFactory.getInstance("EC", BouncyCastleProvider.PROVIDER_NAME)
.generatePublic(keySpec);
}

Expand Down
8 changes: 2 additions & 6 deletions src/main/java/org/fisco/bcos/channel/client/PEMManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ public PrivateKey getPrivateKey()
// X509EncodedKeySpec(pem.getContent()));

PKCS8EncodedKeySpec encodedKeySpec = new PKCS8EncodedKeySpec(pem.getContent());
KeyFactory keyFacotry =
KeyFactory.getInstance(
"EC", org.bouncycastle.jce.provider.BouncyCastleProvider.PROVIDER_NAME);
KeyFactory keyFacotry = KeyFactory.getInstance("EC", BouncyCastleProvider.PROVIDER_NAME);
return keyFacotry.generatePrivate(encodedKeySpec);
}

Expand All @@ -86,9 +84,7 @@ public PublicKey getPublicKey()
bcW.getAffineXCoord().toBigInteger(), bcW.getAffineYCoord().toBigInteger());
ECPublicKeySpec keySpec = new ECPublicKeySpec(w, tryFindNamedCurveSpec(params));
return (PublicKey)
KeyFactory.getInstance(
"EC",
org.bouncycastle.jce.provider.BouncyCastleProvider.PROVIDER_NAME)
KeyFactory.getInstance("EC", BouncyCastleProvider.PROVIDER_NAME)
.generatePublic(keySpec);
}

Expand Down
27 changes: 16 additions & 11 deletions src/main/java/org/fisco/bcos/channel/client/Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,13 @@
import org.fisco.bcos.channel.dto.ChannelPush2;
import org.fisco.bcos.channel.dto.ChannelRequest;
import org.fisco.bcos.channel.dto.ChannelResponse;
import org.fisco.bcos.channel.handler.*;
import org.fisco.bcos.channel.handler.ChannelConnections;
import org.fisco.bcos.channel.handler.ConnectionCallback;
import org.fisco.bcos.channel.handler.ConnectionInfo;
import org.fisco.bcos.channel.handler.GroupChannelConnectionsConfig;
import org.fisco.bcos.channel.handler.Message;
import org.fisco.bcos.web3j.protocol.core.methods.response.TransactionReceipt;
import org.fisco.bcos.web3j.protocol.exceptions.TransactionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
Expand Down Expand Up @@ -234,6 +239,9 @@ public void run() throws Exception {

public BcosResponse sendEthereumMessage(BcosRequest request) {
class Callback extends BcosResponseCallback {
public BcosResponse bcosResponse;
public Semaphore semaphore = new Semaphore(1, true);

Callback() {
try {
semaphore.acquire(1);
Expand All @@ -246,19 +254,16 @@ class Callback extends BcosResponseCallback {

@Override
public void onResponse(BcosResponse response) {
fiscoResponse = response;
bcosResponse = response;

if (fiscoResponse != null && fiscoResponse.getContent() != null) {
if (bcosResponse != null && bcosResponse.getContent() != null) {
logger.debug("response: {}", response.getContent());
} else {
logger.error("fisco error");
logger.error("response is null");
}

semaphore.release();
}

public BcosResponse fiscoResponse;
public Semaphore semaphore = new Semaphore(1, true);
};

Callback callback = new Callback();
Expand All @@ -271,7 +276,7 @@ public void onResponse(BcosResponse response) {
Thread.currentThread().interrupt();
}

return callback.fiscoResponse;
return callback.bcosResponse;
}

public BcosResponse sendEthereumMessage(
Expand Down Expand Up @@ -394,10 +399,10 @@ public void asyncSendEthereumMessage(BcosRequest request, BcosResponseCallback c
if (channelConnections == null) {
if (orgID != null) {
logger.error("not found:{}", orgID);
throw new Exception("not found orgID");
throw new TransactionException("not found orgID");
} else {
logger.error("not found:{}", agencyName);
throw new Exception("not found agencyName");
throw new TransactionException("not found agencyName");
}
}
ChannelHandlerContext ctx = channelConnections.randomNetworkConnection();
Expand Down Expand Up @@ -442,7 +447,7 @@ public void run(Timeout timeout) throws Exception {
response.setErrorCode(-1);
response.setErrorMessage(
e.getMessage()
+ "Requset send failed! Can not connect to nodes success, please checkout the node status and the sdk config!");
+ " Requset send failed! Can not connect to nodes success, please checkout the node status and the sdk config!");
response.setContent("");
response.setMessageID(request.getMessageID());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,6 @@ public void channelInactive(ChannelHandlerContext ctx) throws Exception {

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
String host = ((SocketChannel) ctx.channel()).remoteAddress().getAddress().getHostAddress();
Integer port = ((SocketChannel) ctx.channel()).remoteAddress().getPort();

final ChannelHandlerContext ctxF = ctx;
final ByteBuf in = (ByteBuf) msg;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ static String buildMethodSignature(String methodName, List<Type> parameters) {
return result.toString();
}

static String buildMethodId(String methodSignature) {
public static String buildMethodId(String methodSignature) {
byte[] input = methodSignature.getBytes();
byte[] hash = Hash.sha3(input);
return Numeric.toHexString(hash).substring(0, 10);
Expand Down
Loading

0 comments on commit be67e83

Please sign in to comment.