Commit 32e0ff08 authored by 孙谷宇's avatar 孙谷宇
Browse files

update contract

parent 88f9eea6
[{"constant":false,"inputs":[{"name":"idList","type":"string[]"},{"name":"departmentIdList","type":"string[]"},{"name":"tagList","type":"int256[]"},{"name":"timestampList","type":"int256[]"}],"name":"batchAdd","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"functionName","type":"string"},{"name":"criticalSize","type":"uint256"}],"name":"registerParallelFunction","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"functionName","type":"string"}],"name":"unregisterParallelFunction","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[],"name":"enableParallel","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[],"name":"disableParallel","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"id","type":"string"},{"name":"start","type":"int256"},{"name":"end","type":"int256"}],"name":"select","outputs":[{"name":"","type":"int256[]"},{"name":"","type":"string[]"},{"name":"","type":"int256[]"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"id","type":"string"},{"name":"departmentId","type":"string"},{"name":"tag","type":"int256"},{"name":"timestamp","type":"int256"}],"name":"add","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"name":"id","type":"string"},{"indexed":false,"name":"departmentId","type":"string"},{"indexed":false,"name":"tag","type":"int256"}],"name":"newRecord","type":"event"}]
\ No newline at end of file
[{"constant":false,"inputs":[{"name":"idList","type":"string[]"},{"name":"departmentIdList","type":"string[]"},{"name":"tagList","type":"int256[]"},{"name":"timestampList","type":"int256[]"}],"name":"batchAdd","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"functionName","type":"string"},{"name":"criticalSize","type":"uint256"}],"name":"registerParallelFunction","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"departmentId","type":"string"},{"name":"start","type":"int256"},{"name":"end","type":"int256"}],"name":"selectByDepartment","outputs":[{"name":"","type":"int256[]"},{"name":"","type":"string[]"},{"name":"","type":"int256[]"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"functionName","type":"string"}],"name":"unregisterParallelFunction","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[],"name":"enableParallel","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[],"name":"disableParallel","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"id","type":"string"},{"name":"start","type":"int256"},{"name":"end","type":"int256"}],"name":"select","outputs":[{"name":"","type":"int256[]"},{"name":"","type":"string[]"},{"name":"","type":"int256[]"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"id","type":"string"},{"name":"departmentId","type":"string"},{"name":"tag","type":"int256"},{"name":"timestamp","type":"int256"}],"name":"add","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"name":"id","type":"string"},{"indexed":false,"name":"departmentId","type":"string"},{"indexed":false,"name":"tag","type":"int256"}],"name":"newRecord","type":"event"}]
\ No newline at end of file
......@@ -5,34 +5,49 @@ import "./Table.sol";
contract Tag is ParallelContract {
TableFactory _tf;
string constant TABLE_NAME = "tag";
string constant TABLE_NAME = "tag1";
string constant TABLE_ORG = "tag_org1";
event newRecord(string id, string departmentId, int tag);
constructor() public {
_tf = TableFactory(0x1001);
_tf.createTable(TABLE_NAME, "id", "departmentId,tag,timestamp");
_tf.createTable(TABLE_ORG, "departmentId", "id,tag,timestamp");
}
function _add(Table table, Entry entry, string id, string departmentId, int tag, int timestamp) internal {
function _add(Table table, Entry entry, string id, string departmentId, int tag, int timestamp, string tableName) internal {
entry.set("id", id);
entry.set("departmentId", departmentId);
entry.set("tag", tag);
entry.set("timestamp", timestamp);
table.insert(id, entry);
if (keccak256(tableName) == keccak256(TABLE_NAME)) {
table.insert(id, entry);
} else if (keccak256(tableName) == keccak256(TABLE_ORG)) {
table.insert(departmentId, entry);
}
}
function add(string id, string departmentId, int tag, int timestamp) public {
Table table = _tf.openTable(TABLE_NAME);
Entry item = table.newEntry();
_add(table, item, id, departmentId, tag, timestamp);
_add(table, item, id, departmentId, tag, timestamp, TABLE_NAME);
table = _tf.openTable(TABLE_ORG);
item = table.newEntry();
_add(table, item, id, departmentId, tag, timestamp, TABLE_ORG);
emit newRecord(id, departmentId, tag);
}
function batchAdd(string[] idList, string[] departmentIdList, int[] tagList, int[] timestampList) public {
Table table = _tf.openTable(TABLE_NAME);
Table table2 = _tf.openTable(TABLE_ORG);
for (uint i = 0; i < idList.length; i++) {
Entry item = table.newEntry();
_add(table, item, idList[i], departmentIdList[i], tagList[i], timestampList[i]);
_add(table, item, idList[i], departmentIdList[i], tagList[i], timestampList[i], TABLE_NAME);
item = table.newEntry();
_add(table2, item, idList[i], departmentIdList[i], tagList[i], timestampList[i], TABLE_ORG);
emit newRecord(idList[i], departmentIdList[i], tagList[i]);
}
}
......@@ -44,19 +59,23 @@ contract Tag is ParallelContract {
unregisterParallelFunction("add(string,string,int,int)");
}
function toArray(Entries items) internal view returns(int[], string[] memory, int[]) {
function toArray(Entries items, string tableName) internal view returns(int[], string[] memory, int[]) {
uint size = uint(items.size());
int[] memory tags = new int[](size);
string[] memory departmentIds = new string[](size);
string[] memory info = new string[](size);
int[] memory times = new int[](size);
for (uint i = 0; i < size; i++) {
Entry item = items.get(int(i));
tags[i] = item.getInt("tag");
departmentIds[i] = item.getString("departmentId");
times[i] = item.getInt("timestamp");
if (keccak256(tableName) == keccak256(TABLE_NAME)) {
info[i] = item.getString("departmentId");
} else if (keccak256(tableName) == keccak256(TABLE_ORG)) {
info[i] = item.getString("id");
}
}
return (tags, departmentIds, times);
return (tags, info, times);
}
function select(string id, int start, int end) public view returns(int[], string[] memory, int[]) {
......@@ -70,6 +89,20 @@ contract Tag is ParallelContract {
condition.LE("timestamp", end);
}
Entries items = table.select(id, condition);
return toArray(items);
return toArray(items, TABLE_NAME);
}
function selectByDepartment(string departmentId, int start, int end) public view returns(int[], string[] memory, int[]) {
Table table = _tf.openTable(TABLE_ORG);
Condition condition = table.newCondition();
if (start > 0) {
condition.GE("timestamp", start);
}
if (end > 0) {
condition.LE("timestamp", end);
}
Entries items = table.select(departmentId, condition);
return toArray(items, TABLE_ORG);
}
}
......@@ -23,10 +23,8 @@ from utils.rpc import HTTPProvider
from identity.identity import IdNumber
if not os.path.exists("contracts/Tag.abi"):
Compiler.compile_file("contracts/Tag.sol")
contract = to_checksum_address("0x2d1c577e41809453c50e7e5c3f57d06f3cdd90ce")
Compiler.compile_file("contracts/Tag.sol")
contract = to_checksum_address("0x8eac88a70907646154f39b0dd75252c873429a65")
abi_file = "contracts/Tag.abi"
data_parser = DatatypeParser()
......@@ -180,7 +178,7 @@ async def main(client: AsyncBcosClient):
times = []
for _ in range(PER_TX):
ids.append(IdNumber.generate_id())
department_ids.append(str(random.randint(1, 1000)))
department_ids.append(str(random.randint(1, 50)))
tags.append(random.randint(1, 200))
times.append(int(time.time()))
args = [ids, department_ids, tags, times]
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment