我的工作是关于Smart Contract开发,使用(py)ethereum和{a3}
读“A Programmer’s Guide to Ethereum and Serpent”时,我在第5.9点看到:
[...] Persistent data structures can be declared using the data declaration. This allows for the declaration of arrays and tuples. [...]
以及:
[...] For simple storage, self.storage[] is useful, but for larger contracts, we recommend the use of data (unless you need a key- value storage, of course) [...]
代码示例:
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import serpent
from ethereum import tester, utils, abi
serpent_code = '''
data mystorage[]
def test_data_storage(key,value):
if not self.mystorage[key]:
self.mystorage[key]=value
return(1)
else:
return(0)
def get_value_mystorage(key):
if not self.mystorage[key]:
return(0)
else:
return(self.mystorage[key])
def test_self_storage(key,value):
if not self.storage[key]:
self.storage[key]=value
return(1)
else:
return(0)
def get_value_self_storage(key):
if not self.storage[key]:
return(0)
else:
return(self.storage[key])
'''
s = tester.state()
c = s.abi_contract(serpent_code)
#example with self storage
c.test_self_storage("keyA",1)
print c.get_value_self_storage("keyA") #store and access data works in self.storage!
#example with mystorage
c.test_data_storage("keyB",2)
print c.get_value_mystorage("keyB") #store and access data works in data as persistant data storage!
#fail example with complex data
my_complex_data={"keyA":1,"keyB":2, "keyC":[1,2,3], "keyD":{"a":1,"b":2}}
c.test_data_storage("keyComplex",my_complex_data)
#don't store anything because error:
# ethereum.abi.ValueOutOfBounds: {'keyC': [1, 2, 3], 'keyB': 2, 'keyA': 1, 'keyD': {'a': 1, 'b': 2}}
我的问题是:什么是最好的方法,以及如何像字典一样存储复杂数据(代码中的查看My_complex_data变量),将其他dict(或数组作为键值)存储为持久数据结构?在
有人知道这是否可能,以及如何将任何类结构存储为持久数据结构吗?在
重要:请注意,根据this Vitalik Tweet,Serpent现在是一种“过时的技术”。在
毒蛇
README
已更新为:如果您想从Python编写以太坊契约来发布产品,那么就开始考虑迁移到Solidity或{a4}(这仍然是一种“新的实验性编程语言”)
关于我的问题,我最终找到了一个(棘手的/肮脏的)解决方案,即在将复杂数据推送到持久数据存储之前对其进行编码,然后在从存储中检索数据后进行解码。在
请参阅以下更新代码:
哪个返回:
^{pr2}$相关问题 更多 >
编程相关推荐