Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python SDK不能插入 #196

Closed
lcy1317 opened this issue Jul 9, 2023 · 6 comments
Closed

Python SDK不能插入 #196

lcy1317 opened this issue Jul 9, 2023 · 6 comments

Comments

@lcy1317
Copy link

lcy1317 commented Jul 9, 2023

我根据TableTest.sol写了一个合约,只是把TableTest里的int256类型也换成了string类型,然后使用python SDK去进行合约调用,表现出以下行为:

  • 调用select没有任何问题,能select到内容
  • 调用insert能获得返回值,能返回count值1,也能返回输入参数
  • 再次调用select,检索不到插入的内容,非常奇怪,因为上一步有返回值但就是无法select到
  • 使用console的方式去同样调用insert
  • 再次调用select,能检索到插入的内容
  • 还是使用python sdk的方式去insert,不行

python 调用部分

script_path = "../console/demo.exp"
ContractAddr = "0xd5d0b10598272471824256b5a336d76dae29d278"
abi_file = "../console/contracts/.compiled/1/TableDemo/"+ ContractAddr+"/TableDemo.abi"
data_parser = DatatypeParser()
data_parser.load_abi_file(abi_file)
contract_abi = data_parser.contract_abi
try:
    client = BcosClient()
    print(client.getinfo())
    # 尝试进行TableDemo合约的调用
    # 表名、主key、值域
    print("\n>>Call:------------------------------------------------------------------------")
    res = client.call(ContractAddr, contract_abi, "insert",["TEST","TEST2"])
    client.finish()
except:
    pass

合约:

pragma solidity>=0.4.24 <0.6.11;
pragma experimental ABIEncoderV2;

import "./Table.sol";

contract TableDemo {
    event CreateResult(int256 count);
    event InsertResult(int256 count);
    event UpdateResult(int256 count);
    event RemoveResult(int256 count);

    TableFactory tableFactory;
    string constant TABLE_NAME = "test04";
    constructor() public {
        tableFactory = TableFactory(0x1001); //The fixed address is 0x1001 for TableFactory
        // the parameters of createTable are tableName,keyField,"vlaueFiled1,vlaueFiled2,vlaueFiled3,..."
        tableFactory.createTable(TABLE_NAME, "mmsg", "mtype,mcontent");
    }

    //select records
    function selectAll()
    public
    view
    returns (string[] memory, string[] memory)
    {
        Table table = tableFactory.openTable(TABLE_NAME);

        Condition condition = table.newCondition();

        Entries entries = table.select("records", condition);
        // string[] memory mmsg_list = new string[](
        //     uint256(entries.size())
        // );
        string[] memory mtype_list = new string[](uint256(entries.size()));
        string[] memory mcontent_list = new string[](
            uint256(entries.size())
        );

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

            // mmsg_list[uint256(i)] = entry.getString("mmsg");
            mtype_list[uint256(i)] = entry.getString("mtype");
            mcontent_list[uint256(i)] = entry.getString("mcontent");
        }

        return (mtype_list, mcontent_list);
    }

    function selectByType(string memory mtype)
    public
    view
    returns (string[] memory, string[] memory)
    {
        Table table = tableFactory.openTable(TABLE_NAME);

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

        Entries entries = table.select("records", condition);
        // string[] memory mmsg_list = new string[](
        //     uint256(entries.size())
        // );
        string[] memory mtype_list = new string[](uint256(entries.size()));
        string[] memory mcontent_list = new string[](
            uint256(entries.size())
        );

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

            // mmsg_list[uint256(i)] = entry.getString("mmsg");
            mtype_list[uint256(i)] = entry.getString("mtype");
            mcontent_list[uint256(i)] = entry.getString("mcontent");
        }

        return (mtype_list, mcontent_list);
    }

    //insert records
    function insert(string memory mtype, string memory mcontent)
    public
    returns (int256, string memory, string memory)
    {
        Table table = tableFactory.openTable(TABLE_NAME);

        Entry entry = table.newEntry();
        entry.set("mmsg", "records");
        entry.set("mtype", mtype);
        entry.set("mcontent", mcontent);

        int256 count = table.insert("records", entry);
        emit InsertResult(count);

        return (count,mtype,mcontent);
    }
    //update records
    function update(string memory mmsg, string memory mtype, string memory mcontent)
    public
    returns (int256)
    {
        Table table = tableFactory.openTable(TABLE_NAME);

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

        Condition condition = table.newCondition();
        condition.EQ("mmsg", mmsg);
        condition.EQ("mtype", mtype);

        int256 count = table.update(mmsg, entry, condition);
        emit UpdateResult(count);

        return count;
    }
    //remove records
    function remove(string memory mmsg, string memory mtype) public returns (int256) {
        Table table = tableFactory.openTable(TABLE_NAME);

        Condition condition = table.newCondition();
        condition.EQ("mmsg", mmsg);
        condition.EQ("mtype", mtype);

        int256 count = table.remove(mmsg, condition);
        emit RemoveResult(count);

        return count;
    }
}
@lcy1317
Copy link
Author

lcy1317 commented Jul 9, 2023

我同样测试了给出的TableTest合约,使用如下的python脚本:期望插入一条记录并查询到他:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from client.bcosclient import BcosClient
from client.stattool import StatTool
from client.datatype_parser import DatatypeParser
from client.common.compiler import Compiler
from client_config import client_config
from client.bcoserror import BcosException, BcosError
from client.precompile.crud.crud_service import Table, Entry, CRUDService
from kafka import KafkaConsumer
import traceback
import os
import subprocess
import json
import time
import asyncio
ContractAddr = "0x71790d4d379cb1271f170f3fab891cf49cd02672"
# abi_file = "../console/contracts/TableDemo/"+ ContractAddr+"/TableDemo.abi"
abi_file = "../console/contracts/.compiled/1/TableTest/"+ ContractAddr+"/TableTest.abi"
data_parser = DatatypeParser()
data_parser.load_abi_file(abi_file)
contract_abi = data_parser.contract_abi
try:
    client = BcosClient()
    print(client.getinfo())
    # 尝试进行TableDemo合约的调用
    # 表名、主key、值域
    print("\n>>Call:------------------------------------------------------------------------")
    res = client.call(ContractAddr, contract_abi, "select", ["TEST"])
    print("call select:", res)

    res = client.call(ContractAddr, contract_abi, "insert",["TEST",2,"juele"])
    print("插入记录返回:", res)

    res = client.call(ContractAddr, contract_abi, "select", ["TEST"])
    print("call select:", res)

    client.finish()

except BcosException as e:
    print("demo_get failed, BcosException information: {}".format(e))
    traceback.print_exc()
except BcosError as e:
    print("demo_get failed, BcosError information: {}".format(e))
    traceback.print_exc()
except Exception as e:
    traceback.print_exc()
client.finish()

以下是返回结果:

channel 127.0.0.1:20200,groupid :1,crypto type:ECDSA,ssl type:ECDSA

Call:------------------------------------------------------------------------
call select: (('TEST',), (1,), ('woliekai',))
插入记录返回: (1,)
call select: (('TEST',), (1,), ('woliekai',))

可以看到插入记录的返回是成功return 1了,但是并无法查询到新增的记录

@coderkentzhang
Copy link
Contributor

python sdk client的call是对应合约里的只读/查询接口,即view修饰的接口
插入数据应该用 sendRawTransaction 接口

@lcy1317
Copy link
Author

lcy1317 commented Jul 13, 2023

以TableTest.sol 为例,请问是否可以给一个样例?res = client. sendRawTransaction(ContractAddr, contract_abi, "insert",["TEST",2,"juele"])是这样么?
以及想请问一下为什么使用client.call 他会提供返回值呢?

@lcy1317
Copy link
Author

lcy1317 commented Jul 13, 2023

此外想请问一下是否提供了异步插入的函数?

@coderkentzhang
Copy link
Contributor

call也是会调用合约并返回值的,只是交易不参加共识不会落盘
sendRawTransaction本身是异步的,sendRawTransaction之后getTransactionReceipt获取最终结果,
sendRawTransactionAndGetReceipt才是同步的。
参数列表和call类似,有一些用不到的参数留空就行了

@lcy1317
Copy link
Author

lcy1317 commented Jul 13, 2023

好的 谢谢!

@lcy1317 lcy1317 closed this as completed Jul 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants