from __future__ import unicode_literals

import json
from time import sleep, time # 中文编码
def encode_str(_str):
try:
return bytearray(_str, "utf-8").hex()
except Exception as e:
print(e) # 中文解码
def decode_str(_str):
try:
return bytearray.fromhex(_str).decode("utf-8")
except Exception as e:
print(e) MATH_BYTECODE = "6060604052341561000f57600080fd5b5b60c08061001e6000396000f300606060405263ffffffff7c0100000000000000000000000000000000000000000000000000000000600035041663696c75c68114603c575b600080fd5b3415604657600080fd5b60526004356024356054565b005b817f3ea5d667934df8dbb4978ddd30e65375fd1bf06f8ed39d033379c5cd3efb102c428360405191825260208201526040908101905180910390a25b50505600a165627a7a723058208d6649bbcad0893ba1e52ea3904adaf2ff4b9ed80f71f5ac5c6b982ce5bac3a20029"
# 6060604052341561000f57600080fd5b5b6105078061001f6000396000f300606060405263ffffffff7c01000000000000000000000000000000000000000000000000000000006000350416630c90d8448114610048578063d10fa94614610111575b600080fd5b341561005357600080fd5b61009960046024813581810190830135806020601f820181900481020160405190810160405281815292919060208401838380828437509496506101e395505050505050565b60405160208082528190810183818151815260200191508051906020019080838360005b838110156100d65780820151818401525b6020016100bd565b50505050905090810190601f1680156101035780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b341561011c57600080fd5b61016260046024813581810190830135806020601f820181900481020160405190810160405281815292919060208401838380828437509496506102fa95505050505050565b604051821515815260406020820181815290820183818151815260200191508051906020019080838360005b838110156101a75780820151818401525b60200161018e565b50505050905090810190601f1680156101d45780820380516001836020036101000a031916815260200191505b50935050505060405180910390f35b6101eb610429565b600080836040518082805190602001908083835b6020831061021f57805182525b601f1990920191602091820191016101ff565b6001836020036101000a03801982511681845116808217855250505050505090500191505090815260200160405180910390209050806001018054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156102ec5780601f106102c1576101008083540402835291602001916102ec565b820191906000526020600020905b8154815290600101906020018083116102cf57829003601f168201915b505050505091505b50919050565b6000610304610429565b60408051908101604052808481526020016040805190810160405280600981526020017fe4b88de5ad98e59ca800000000000000000000000000000000000000000000008152508152506000846040518082805190602001908083835b6020831061038157805182525b601f199092019160209182019101610361565b6001836020036101000a0380198251168184511680821785525050505050509050019150509081526020016040519081900390208151819080516103c992916020019061043b565b506020820151816001019080516103e492916020019061043b565b50905050600060408051908101604052600681527f6f6b6f6b31310000000000000000000000000000000000000000000000000000602082015290925090505b915091565b60206040519081016040526000815290565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061047c57805160ff19168380011785556104a9565b828001600101855582156104a9579182015b828111156104a957825182559160200191906001019061048e565b5b506104b69291506104ba565b5090565b6104d891905b808211156104b657600081556001016104c0565b5090565b905600a165627a7a72305820faf711785ac7369219291016aa91535aa22004c60da82a8fcd956777c3b7a7000029
MATH_ABI = json.loads("""
[
{
"constant": false,
"inputs": [
{
"name": "_type",
"type": "bytes32"
},
{
"name": "_metadata",
"type": "bytes32"
}
],
"name": "publish",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"name": "_type",
"type": "bytes32"
},
{
"indexed": false,
"name": "_created",
"type": "uint256"
},
{
"indexed": false,
"name": "_metadata",
"type": "bytes32"
}
],
"name": "ArticleOperationLog",
"type": "event"
}
]
""") import web3.personal as wp
from web3 import Web3, HTTPProvider web3 = Web3(HTTPProvider('http://localhost:8545'))
web3.eth.defaultAccount = b'0x3364aa6342c8c18' print("unlockAccount")
p = wp.Personal(web3)
print(p.unlockAccount("0x336394f4b93aa6342c8c18", "123456"))
print("unlockAccount ok") MathContract = web3.eth.contract(
abi=MATH_ABI,
# bytecode=MATH_BYTECODE,
) # deploy_txn = MathContract.deploy()
# transaction={
# 'from': b'0x336495440ce8dd55029bd394f4b93aa6342c8c18',
# 'value': 12345,
# },
# args=('DGD', 18) print("isConnected:", web3.isConnected()) # print(deploy_txn)
# deploy_receipt = web3.eth.getTransactionReceipt("0x71ce48069f2e17079c2cf016f40bd47d995f4c77aab74d12ae89a9cf13cfeb77")
# print(deploy_receipt, "\n\n") # print(deploy_txn)
# deploy_receipt = web3.eth.getTransactionReceipt(b"0xb2e4aacfbd147ddd6f807c0f4d47cc77c6a21ce7e2c9cbd17b7ba3d410473bd4")
# print(deploy_receipt)
# assert deploy_receipt is not None # deploy_receipt["contractAddress"]
# math_contract = MathContract(address=deploy_receipt["contractAddress"])
import re def my_callback(a):
# print(a)
args = a.get("args")
print(decode_str(re.split(r"\W+", args.get("_type"))[0]))
print(str(args.get("_created")))
print(decode_str(re.split(r"\W+", args.get("_metadata"))[0]))
print("\n\n") def my_callback1(a):
print("\n\n\n\npastEvents")
# print(a)
args = a.get("args")
print(decode_str(re.split(r"\W+", args.get("_type"))[0]))
print(str(args.get("_created")))
print(decode_str(re.split(r"\W+", args.get("_metadata"))[0])) math_contract = MathContract(address="0x8851accb27fc4b149bda886d6d579f2fb5c1a492")
# math_contract.on("ArticleOperationLog",{'filter': {'_type': 'hello'}}).watch(my_callback) # math_contract.on("ArticleOperationLog").watch(my_callback)
# math_contract.on("ArticleOperationLog", {"filter": {"_type": encode_str("hello")}}).watch(my_callback) # 历史日志
math_contract.pastEvents("ArticleOperationLog", {"filter": {"_type": encode_str("hello1")}}).watch(my_callback1) # 历史日志
# math_contract.pastEvents("ArticleOperationLog").watch(my_callback1) while 1:
# math_contract.transact().publish(encode_str("hello1"), encode_str("hello1上海"))
print(time())
sleep(2) # _a = "1234562" # print(math_contract.encodeABI("addDNA", args=(_a,_a,_a))) # 发送一个tx到区块链上
# print(math_contract.transact().addDNA(_a, _a, _a)) # 发送一个tx到本地执行,不会写入到区块链上
# print(math_contract.call().addDNA(_a, _a, _a))
# print(math_contract.call().getDNA(_a))

python 智能合约日志操作的更多相关文章

  1. python中的日志操作和发送邮件

    1.python中的日志操作 安装log模块:pip install nnlog 参数:my_log = nnlog.Logger('server_log.log',level='debug',bac ...

  2. [Python之路] 日志操作

    使用logging模块来写日志 日志直接输出到准备输出 import logging logging.basicConfig(level=logging.WARNING, format="% ...

  3. EOS开发基础之六:使用cleos命令行客户端操作EOS——智能合约之eosio.msig和eosio.system

    上一节我们搞了老半天,也没整明白Exchange这个合约到底干啥用的.就它官方提供的说法,是用于货币的创造和交易.我没整明白,所以去看了下代码,发现代码头文件里面有下面这段话: This contra ...

  4. EOS开发基础之四:使用cleos命令行客户端操作EOS——智能合约之eosio.bios和eosio.token

    现实世界中的合约,简单地说,是一个参与活动的所有人都需要遵循的协议.合约可以是正式的法律合同(例如,金融交易),或者是简单的游戏规则.典型的活动可以是诸如资金转移(在金融合约的情况下)或游戏动作(在游 ...

  5. 从零开始的Python学习Episode 14——日志操作

    日志操作 一.logging模块 %(message)s 日志信息 %(levelno)s 日志级别 datefmt 设置时间格式 filename 设置日志保存的路径 level 设置日志记录的级别 ...

  6. Python之日志操作(logging)

    import logging   1.自定义日志级别,日志格式,输出位置 logging.basicConfig( level=logging.DEBUG, format='%(asctime)s | ...

  7. Hyperledger Fabric Chaincode for Operators——实操智能合约

    什么是Chaincode(智能合约)? chaincode是一个程序,它是使用Go语言编写的,最终在Java等其他编程语言中实现了指定的接口.chaincode运行在一个被背书peer进程独立出来的安 ...

  8. 【精解】EOS智能合约演练

    EOS,智能合约,abi,wasm,cleos,eosiocpp,开发调试,钱包,账户,签名权限 热身 本文旨在针对EOS智能合约进行一个完整的实操演练,过程中深入熟悉掌握整个EOS智能合约的流程,过 ...

  9. 以太坊智能合约虚拟机(EVM)原理与实现

    以太坊 EVM原理与实现 以太坊底层通过EVM模块支持合约的执行与调用,调用时根据合约地址获取到代码,生成环境后载入到EVM中运行.通常智能合约的开发流程是用solidlity编写逻辑代码,再通过编译 ...

随机推荐

  1. js取一个对象中的另一个对象

    最开始的截图 原本是想取到其中的foodName 先是用一个for循环循环了下 for (var i=0;i<res.data.length;i++) { this.goodsList.res. ...

  2. Zabbix——自动发现

    前提条件: Zabbix版本为4.0 固定网段寻找网络设备,并添加组.添加模板.添加proxy. 设置完毕,等待~~ 如果没有问题,将会直接出现在host中.

  3. Zabbix——设置报警阈值

    前提条件: 1. Zabbix-server 版本为4.0 2.邮件告警正常使用 3. 阈值改为1分钟进行邮件发送 点击: 找到agent,点击触发器: 设置网络ping包进行检测

  4. 复习宝典之Redis

    查看更多宝典,请点击<金三银四,你的专属面试宝典> 第八章:Redis Redis是一个key-value的nosql数据库.先存到内存中,会根据一定的策略持久化到磁盘,即使断电也不会丢失 ...

  5. 红帽RHEL6.8离线环境下升级到RHEL7.3

    Red Hat Enterprise Linux 7 (RHEL 7) 是第一个支持从前一个 RHEL 主发行版本(RHEL 6)进行原位(in-place)升级的 RHEL 主版本.原位升级(in- ...

  6. Webstorm新建vue类型文件设置

    今天安装了Node.js,配置了vue需要的框架,发现原有的wenstorm新建文件的时候没有vue文件选项,因此,学习了一下webstorm如何配置创建vue文件 具体过程如下: 第一步,打开web ...

  7. angularjs中控制器之间的通信----$on、$emit和$broadcast解析

    $on.$emit和$broadcast使得event.data在controller之间的传递变的简单. $emit只能向parent controller传递event与data $broadca ...

  8. laravel4.2 union联合,join关联分组查询最新记录时,查询条件不对,解决方案

    需求: 分组联合查询,或者最新记录. 问题:  mysql分组的时候默认会查询第一条记录,存在gourp by时 order by 无效. 一般解决办法就是 ,select * from ( sele ...

  9. java语言描述 用抽象类模拟咖啡机的工作

    import java.util.Scanner; class Test { public static void main(String[] args) { coffee per = new cof ...

  10. 最小生成树——Kruscal(克鲁斯卡尔算法)

    一.核心思想 ​ 将输入的数据由小到大进行排序,再使用并查集算法(传送门)将每个点连接起来,同时求和. ​ 个人认为这个算法比较偏向暴力,有些题可能会超时. 二.例题 洛谷-P3366 题目地址:ht ...