Skip to content

Commit 0a1b267

Browse files
authored
<fix>(contracts): add TableV320 contracts. (#783)
1 parent 9f8531d commit 0a1b267

File tree

6 files changed

+329
-157
lines changed

6 files changed

+329
-157
lines changed

.ci/ci_check.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ build_node()
4747
{
4848
local node_type="${1}"
4949
if [ "${node_type}" == "sm" ];then
50-
bash -x build_chain.sh -l 127.0.0.1:4 -s -A
50+
bash -x build_chain.sh -l 127.0.0.1:4 -s
5151
sed_cmd=$(get_sed_cmd)
5252
$sed_cmd 's/sm_crypto_channel=false/sm_crypto_channel=true/g' nodes/127.0.0.1/node*/config.ini
5353
else
54-
bash -x build_chain.sh -l 127.0.0.1:4 -A
54+
bash -x build_chain.sh -l 127.0.0.1:4
5555
fi
5656
./nodes/127.0.0.1/fisco-bcos -v
5757
./nodes/127.0.0.1/start_all.sh

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ dependencies {
4040
//compile 'org.fisco-bcos:solcJ:0.5.2.1'
4141
compile 'org.fisco-bcos:solcJ:0.8.11.1'
4242

43-
compile('org.fisco-bcos.java-sdk:fisco-bcos-java-sdk:3.2.0') {
43+
compile('org.fisco-bcos.java-sdk:fisco-bcos-java-sdk:3.2.3-SNAPSHOT') {
4444
exclude group: "org.slf4j"
4545
}
4646
compile('org.fisco-bcos:evm-static-analysis:1.0.0') {

src/main/resources/contract/solidity/Table.sol

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,43 @@
11
// SPDX-License-Identifier: Apache-2.0
2+
// 该接口文件定义了FISCO BCOS v3.1.0及以前版本的接口,使用时需要将该文件放在合约目录下
3+
// 若要使用FISCO BCOS v3.2.0及以后版本的接口,请使用TableV320.sol,旧合约仍然能在新节点中使用
24
pragma solidity >=0.6.10 <0.8.20;
35
pragma experimental ABIEncoderV2;
4-
import "./EntryWrapper.sol";
56

67
// KeyOrder指定Key的排序规则,字典序和数字序,如果指定为数字序,key只能为数字
7-
enum KeyOrder {Lexicographic, Numerical}
8-
struct TableInfo {
9-
KeyOrder keyOrder;
10-
string keyColumn;
11-
string[] valueColumns;
12-
}
8+
// enum KeyOrder {Lexicographic, Numerical}
9+
struct TableInfo {
10+
string keyColumn;
11+
string[] valueColumns;
12+
}
13+
14+
// 记录,用于select和insert
15+
struct Entry {
16+
string key;
17+
string[] fields; // 考虑2.0的Entry接口,临时Precompiled的问题,考虑加工具类接口
18+
}
1319

1420
// 更新字段,用于update
15-
struct UpdateField {
16-
string columnName;
17-
// 考虑工具类
18-
string value;
19-
}
21+
struct UpdateField {
22+
string columnName;
23+
// 考虑工具类
24+
string value;
25+
}
2026

2127
// 筛选条件,大于、大于等于、小于、小于等于
22-
enum ConditionOP {GT, GE, LT, LE, EQ, NE, STARTS_WITH, ENDS_WITH, CONTAINS}
23-
struct Condition {
24-
ConditionOP op;
25-
string field;
26-
string value;
27-
}
28+
enum ConditionOP {GT, GE, LT, LE}
29+
struct Condition {
30+
ConditionOP op;
31+
// string field;
32+
string value;
33+
}
2834

2935
// 数量限制
30-
struct Limit {
31-
uint32 offset;
32-
// count limit max is 500
33-
uint32 count;
34-
}
36+
struct Limit {
37+
uint32 offset;
38+
// count limit max is 500
39+
uint32 count;
40+
}
3541

3642
// 表管理合约,是静态Precompiled,有固定的合约地址
3743
abstract contract TableManager {
@@ -49,7 +55,7 @@ abstract contract TableManager {
4955
function appendColumns(string memory path, string[] memory newColumns) public virtual returns (int32);
5056

5157
// 获取表信息
52-
function descWithKeyOrder(string memory tableName) public view virtual returns (TableInfo memory);
58+
function desc(string memory tableName) public view virtual returns (TableInfo memory);
5359
}
5460

5561
// 表合约,是动态Precompiled,TableManager创建时指定地址
Lines changed: 78 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -1,131 +1,79 @@
1-
// SPDX-License-Identifier: Apache-2.0
2-
pragma solidity >=0.6.10 <0.8.20;
3-
pragma experimental ABIEncoderV2;
4-
5-
import "./Table.sol";
6-
import "./Cast.sol";
7-
8-
contract TableTest {
9-
event CreateResult(int256 count);
10-
event InsertResult(int256 count);
11-
event UpdateResult(int256 count);
12-
event RemoveResult(int256 count);
13-
14-
Cast constant cast = Cast(address(0x100f));
15-
TableManager constant tm = TableManager(address(0x1002));
16-
Table table;
17-
string constant TABLE_NAME = "t_testV320";
18-
constructor () public {
19-
// create table
20-
string[] memory columnNames = new string[](3);
21-
columnNames[0] = "name";
22-
columnNames[1] = "age";
23-
columnNames[2] = "status";
24-
TableInfo memory tf = TableInfo(KeyOrder.Numerical ,"id", columnNames);
25-
26-
tm.createTable(TABLE_NAME, tf);
27-
address t_address = tm.openTable(TABLE_NAME);
28-
require(t_address!=address(0x0),"");
29-
table = Table(t_address);
30-
}
31-
32-
function select(int64 id) public view returns (string memory, string memory)
33-
{
34-
Entry memory entry = table.select(cast.s64ToString(id));
35-
string memory name;
36-
string memory age;
37-
if(entry.fields.length == 3){
38-
name = entry.fields[0];
39-
age = entry.fields[1];
40-
}
41-
return (name, age);
42-
}
43-
44-
function insert(int64 id, string memory name, string memory age) public returns (int32){
45-
Entry memory entry = Entry(cast.s64ToString(id), new string[](3));
46-
entry.fields[0] = name;
47-
entry.fields[1] = age;
48-
entry.fields[2] = "init";
49-
int32 result = table.insert(entry);
50-
emit InsertResult(result);
51-
return result;
52-
}
53-
54-
function update(int64 id, string memory name, string memory age) public returns (int32){
55-
UpdateField[] memory updateFields = new UpdateField[](2);
56-
updateFields[0] = UpdateField("name", name);
57-
updateFields[1] = UpdateField("age", age);
58-
59-
int32 result = table.update(cast.s64ToString(id), updateFields);
60-
emit UpdateResult(result);
61-
return result;
62-
}
63-
64-
function remove(int64 id) public returns(int32){
65-
int32 result = table.remove(cast.s64ToString(id));
66-
emit RemoveResult(result);
67-
return result;
68-
}
69-
70-
function select(int64 idLow, int64 idHigh) public view returns (string[] memory)
71-
{
72-
Limit memory limit = Limit(0, 500);
73-
Condition[] memory cond = new Condition[](2);
74-
cond[0] = Condition(ConditionOP.GT, "id", cast.s64ToString(idLow));
75-
cond[1] = Condition(ConditionOP.LE, "id", cast.s64ToString(idHigh));
76-
Entry[] memory entries = table.select(cond, limit);
77-
string[] memory names = new string[](entries.length);
78-
for(uint i = 0; i < names.length; i++)
79-
{
80-
names[i] = entries[i].fields[0];
81-
}
82-
return names;
83-
}
84-
85-
function count(int64 idLow, int64 idHigh) public view returns (uint32)
86-
{
87-
Condition[] memory cond = new Condition[](2);
88-
cond[0] = Condition(ConditionOP.GT, "id", cast.s64ToString(idLow));
89-
cond[1] = Condition(ConditionOP.LE, "id", cast.s64ToString(idHigh));
90-
return table.count(cond);
91-
}
92-
93-
function update(int64 idLow, int64 idHigh) public returns (int32)
94-
{
95-
UpdateField[] memory updateFields = new UpdateField[](1);
96-
updateFields[0] = UpdateField("status", "updated");
97-
98-
Limit memory limit = Limit(0, 500);
99-
Condition[] memory cond = new Condition[](2);
100-
cond[0] = Condition(ConditionOP.GT, "id", cast.s64ToString(idLow));
101-
cond[1] = Condition(ConditionOP.LE, "id", cast.s64ToString(idHigh));
102-
return table.update(cond, limit, updateFields);
103-
}
104-
105-
function remove(int64 idLow, int64 idHigh) public returns (int32)
106-
{
107-
Limit memory limit = Limit(0, 500);
108-
Condition[] memory cond = new Condition[](2);
109-
cond[0] = Condition(ConditionOP.GT, "id", cast.s64ToString(idLow));
110-
cond[1] = Condition(ConditionOP.LE, "id", cast.s64ToString(idHigh));
111-
return table.remove(cond, limit);
112-
}
113-
114-
function createTable(string memory tableName, uint8 keyOrder, string memory key,string[] memory fields) public returns(int256){
115-
require(keyOrder == 0 || keyOrder == 1);
116-
KeyOrder _keyOrder = KeyOrder.Lexicographic;
117-
if (keyOrder == 1)
118-
{
119-
_keyOrder = KeyOrder.Numerical;
120-
}
121-
TableInfo memory tf = TableInfo(_keyOrder, key, fields);
122-
int32 result = tm.createTable(tableName,tf);
123-
emit CreateResult(result);
124-
return result;
125-
}
126-
127-
function desc() public view returns(string memory, string[] memory){
128-
TableInfo memory ti = tm.descWithKeyOrder(TABLE_NAME);
129-
return (ti.keyColumn,ti.valueColumns);
130-
}
1+
// SPDX-License-Identifier: Apache-2.0
2+
pragma solidity >=0.6.10 <0.8.20;
3+
pragma experimental ABIEncoderV2;
4+
5+
import "./Table.sol";
6+
7+
contract TableTest {
8+
event CreateResult(int256 count);
9+
event InsertResult(int256 count);
10+
event UpdateResult(int256 count);
11+
event RemoveResult(int256 count);
12+
13+
TableManager constant tm = TableManager(address(0x1002));
14+
Table table;
15+
string constant TABLE_NAME = "t_test";
16+
constructor () public{
17+
// create table
18+
string[] memory columnNames = new string[](2);
19+
columnNames[0] = "name";
20+
columnNames[1] = "age";
21+
TableInfo memory tf = TableInfo("id", columnNames);
22+
23+
tm.createTable(TABLE_NAME, tf);
24+
address t_address = tm.openTable(TABLE_NAME);
25+
require(t_address!=address(0x0),"");
26+
table = Table(t_address);
27+
}
28+
29+
function select(string memory id) public view returns (string memory,string memory)
30+
{
31+
Entry memory entry = table.select(id);
32+
33+
string memory name;
34+
string memory age;
35+
if(entry.fields.length==2){
36+
name = entry.fields[0];
37+
age = entry.fields[1];
38+
}
39+
return (name,age);
40+
}
41+
42+
function insert(string memory id,string memory name,string memory age) public returns (int32){
43+
string[] memory columns = new string[](2);
44+
columns[0] = name;
45+
columns[1] = age;
46+
Entry memory entry = Entry(id, columns);
47+
int32 result = table.insert(entry);
48+
emit InsertResult(result);
49+
return result;
50+
}
51+
52+
function update(string memory id, string memory name, string memory age) public returns (int32){
53+
UpdateField[] memory updateFields = new UpdateField[](2);
54+
updateFields[0] = UpdateField("name", name);
55+
updateFields[1] = UpdateField("age", age);
56+
57+
int32 result = table.update(id, updateFields);
58+
emit UpdateResult(result);
59+
return result;
60+
}
61+
62+
function remove(string memory id) public returns(int32){
63+
int32 result = table.remove(id);
64+
emit RemoveResult(result);
65+
return result;
66+
}
67+
68+
function createTable(string memory tableName,string memory key,string[] memory fields) public returns(int256){
69+
TableInfo memory tf = TableInfo(key, fields);
70+
int32 result = tm.createTable(tableName,tf);
71+
emit CreateResult(result);
72+
return result;
73+
}
74+
75+
function desc() public view returns(string memory, string[] memory){
76+
TableInfo memory ti = tm.desc(TABLE_NAME);
77+
return (ti.keyColumn,ti.valueColumns);
78+
}
13179
}

0 commit comments

Comments
 (0)