本文整理汇总了Python中sawtooth_validator.state.merkle.MerkleDatabase.get方法的典型用法代码示例。如果您正苦于以下问题:Python MerkleDatabase.get方法的具体用法?Python MerkleDatabase.get怎么用?Python MerkleDatabase.get使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sawtooth_validator.state.merkle.MerkleDatabase
的用法示例。
在下文中一共展示了MerkleDatabase.get方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get
# 需要导入模块: from sawtooth_validator.state.merkle import MerkleDatabase [as 别名]
# 或者: from sawtooth_validator.state.merkle.MerkleDatabase import get [as 别名]
def get(self, context_id, address_list):
"""Get the values associated with list of addresses, for a specific
context referenced by context_id.
Args:
context_id (str): the return value of create_context, referencing
a particular context.
address_list (list): a list of address strs
Returns:
values_list (list): a list of (address, value) tuples
Raises:
AuthorizationException: Raised when an address in address_list is
not authorized either by not being in the inputs for the
txn associated with this context, or it is under a namespace
but the characters that are under the namespace are not valid
address characters.
"""
if context_id not in self._contexts:
return []
for add in address_list:
if not self.address_is_valid(address=add):
raise AuthorizationException(address=add)
context = self._contexts[context_id]
addresses_in_ctx = [add for add in address_list if add in context]
addresses_not_in_ctx = list(set(address_list) - set(addresses_in_ctx))
values = context.get(addresses_in_ctx)
values_list = list(zip(addresses_in_ctx, values))
if addresses_not_in_ctx:
# Validate the addresses that won't be validated by a direct get on
# the context.
for address in addresses_not_in_ctx:
context.validate_read(address)
address_values, reads = self._find_address_values_in_chain(
base_contexts=[context_id],
addresses_to_find=addresses_not_in_ctx)
values_list.extend(address_values)
if reads:
tree = MerkleDatabase(self._database, context.merkle_root)
add_values = []
for add in reads:
value = None
try:
value = tree.get(add)
except KeyError:
# The address is not in the radix tree/merkle tree
pass
add_values.append((add, value))
values_list.extend(add_values)
values_list.sort(key=lambda x: address_list.index(x[0]))
return values_list
示例2: run
# 需要导入模块: from sawtooth_validator.state.merkle import MerkleDatabase [as 别名]
# 或者: from sawtooth_validator.state.merkle.MerkleDatabase import get [as 别名]
def run(self):
while True:
context_state_addresslist_tuple = self._addresses.get(block=True)
c_id, state_hash, address_list = context_state_addresslist_tuple
tree = MerkleDatabase(self._database, state_hash)
return_values = []
for address in address_list:
value = None
try:
value = tree.get(address)
except KeyError:
pass
return_values.append((address, value))
self._inflated_addresses.put((c_id, return_values))
示例3: StateGetRequestHandler
# 需要导入模块: from sawtooth_validator.state.merkle import MerkleDatabase [as 别名]
# 或者: from sawtooth_validator.state.merkle.MerkleDatabase import get [as 别名]
class StateGetRequestHandler(Handler):
def __init__(self, database):
self._tree = MerkleDatabase(database)
def handle(self, identity, message_content):
request = client_pb2.ClientStateGetRequest()
resp_proto = client_pb2.ClientStateGetResponse
status = resp_proto.OK
try:
request.ParseFromString(message_content)
self._tree.set_merkle_root(request.merkle_root)
except KeyError as e:
status = resp_proto.NORESOURCE
LOGGER.debug(e)
except DecodeError:
status = resp_proto.ERROR
LOGGER.info("Expected protobuf of class %s failed to "
"deserialize", request)
if status != resp_proto.OK:
response = resp_proto(status=status)
else:
address = request.address
try:
value = self._tree.get(address)
except KeyError:
status = resp_proto.NORESOURCE
LOGGER.debug("No entry at state address %s", address)
except ValueError:
status = resp_proto.NONLEAF
LOGGER.debug("Node at state address %s is a nonleaf", address)
response = resp_proto(status=status)
if status == resp_proto.OK:
response.value = value
return HandlerResult(
status=HandlerStatus.RETURN,
message_out=response,
message_type=validator_pb2.Message.CLIENT_STATE_GET_RESPONSE)
示例4: TestSawtoothMerkleTrie
# 需要导入模块: from sawtooth_validator.state.merkle import MerkleDatabase [as 别名]
# 或者: from sawtooth_validator.state.merkle.MerkleDatabase import get [as 别名]
class TestSawtoothMerkleTrie(unittest.TestCase):
def setUp(self):
self.dir = tempfile.mkdtemp()
self.file = os.path.join(self.dir, 'merkle.lmdb')
self.lmdb = lmdb_nolock_database.LMDBNoLockDatabase(
self.file,
'n')
self.trie = MerkleDatabase(self.lmdb)
def tearDown(self):
self.trie.close()
def test_merkle_trie_root_advance(self):
value = {'name': 'foo', 'value': 1}
orig_root = self.get_merkle_root()
new_root = self.set('foo', value)
self.assert_root(orig_root)
self.assert_no_key('foo')
self.set_merkle_root(new_root)
self.assert_root(new_root)
self.assert_value_at_address('foo', value)
def test_merkle_trie_delete(self):
value = {'name': 'bar', 'value': 1}
new_root = self.set('bar', value)
self.set_merkle_root(new_root)
self.assert_root(new_root)
self.assert_value_at_address('bar', value)
# deleting an invalid key should raise an error
with self.assertRaises(KeyError):
self.delete('barf')
del_root = self.delete('bar')
# del_root hasn't been set yet, so address should still have value
self.assert_root(new_root)
self.assert_value_at_address('bar', value)
self.set_merkle_root(del_root)
self.assert_root(del_root)
self.assert_no_key('bar')
def test_merkle_trie_update(self):
init_root = self.get_merkle_root()
values = {}
key_hashes = {
key: _hash(key)
for key in (_random_string(10) for _ in range(1000))
}
for key, hashed in key_hashes.items():
value = {key: _random_string(512)}
new_root = self.set(hashed, value, ishash=True)
values[hashed] = value
self.set_merkle_root(new_root)
self.assert_not_root(init_root)
for address, value in values.items():
self.assert_value_at_address(
address, value, ishash=True)
set_items = {
hashed: {
key: 5.0
}
for key, hashed in random.sample(key_hashes.items(), 50)
}
values.update(set_items)
delete_items = {
hashed
for hashed in random.sample(list(key_hashes.values()), 50)
}
# make sure there are no sets and deletes of the same key
delete_items = delete_items - set_items.keys()
for addr in delete_items:
del values[addr]
virtual_root = self.update(set_items, delete_items, virtual=True)
# virtual root shouldn't match actual contents of tree
with self.assertRaises(KeyError):
self.set_merkle_root(virtual_root)
actual_root = self.update(set_items, delete_items, virtual=False)
# the virtual root should be the same as the actual root
self.assertEqual(virtual_root, actual_root)
#.........这里部分代码省略.........
示例5: TestSawtoothMerkleTrie
# 需要导入模块: from sawtooth_validator.state.merkle import MerkleDatabase [as 别名]
# 或者: from sawtooth_validator.state.merkle.MerkleDatabase import get [as 别名]
class TestSawtoothMerkleTrie(unittest.TestCase):
def setUp(self):
self.lmdb = lmdb_nolock_database.LMDBNoLockDatabase(
"/home/vagrant/merkle.lmdb",
'n')
self.trie = MerkleDatabase(self.lmdb)
def tearDown(self):
self.trie.close()
def test_merkle_trie_root_advance(self):
value = {"name": "foo", "value": 1}
orig_root = self.trie.get_merkle_root()
new_root = self.trie.set(MerkleDatabase.hash("foo"),
value)
with self.assertRaises(KeyError):
self.trie.get(MerkleDatabase.hash("foo"))
self.trie.set_merkle_root(new_root)
self.assertEqual(self.trie.get(MerkleDatabase.hash("foo")),
value)
def test_merkle_trie_delete(self):
value = {"name": "bar", "value": 1}
new_root = self.trie.set(MerkleDatabase.hash("bar"), value)
self.trie.set_merkle_root(new_root)
self.assertEqual(self.trie.get(MerkleDatabase.hash("bar")),
value)
del_root = self.trie.delete(MerkleDatabase.hash("bar"))
self.trie.set_merkle_root(del_root)
with self.assertRaises(KeyError):
self.trie.get(MerkleDatabase.hash("bar"))
def test_merkle_trie_update(self):
value = ''.join(random.choice(string.ascii_lowercase)
for _ in range(512))
keys = []
for i in range(1000):
key = ''.join(random.choice(string.ascii_lowercase)
for _ in range(10))
keys.append(key)
hash = MerkleDatabase.hash(key)
new_root = self.trie.set(hash, {key: value})
self.trie.set_merkle_root(new_root)
set_items = {}
for key in random.sample(keys, 50):
hash = MerkleDatabase.hash(key)
thing = {key: 5.0}
set_items[hash] = thing
update_root = self.trie.update(set_items)
self.trie.set_merkle_root(update_root)
for address in set_items:
self.assertEqual(self.trie.get(address),
set_items[address])
示例6: TestSawtoothMerkleTrie
# 需要导入模块: from sawtooth_validator.state.merkle import MerkleDatabase [as 别名]
# 或者: from sawtooth_validator.state.merkle.MerkleDatabase import get [as 别名]
class TestSawtoothMerkleTrie(unittest.TestCase):
def setUp(self):
self.dir = tempfile.mkdtemp()
self.file = os.path.join(self.dir, 'merkle.lmdb')
self.lmdb = NativeLmdbDatabase(
self.file,
indexes=MerkleDatabase.create_index_configuration(),
_size=120 * 1024 * 1024)
self.trie = MerkleDatabase(self.lmdb)
def tearDown(self):
self.trie.close()
shutil.rmtree(self.dir)
def test_merkle_trie_root_advance(self):
value = {'name': 'foo', 'value': 1}
orig_root = self.get_merkle_root()
new_root = self.set('foo', value)
self.assert_root(orig_root)
self.assert_no_key('foo')
self.set_merkle_root(new_root)
self.assert_root(new_root)
self.assert_value_at_address('foo', value)
def test_merkle_trie_delete(self):
value = {'name': 'bar', 'value': 1}
new_root = self.set('bar', value)
self.set_merkle_root(new_root)
self.assert_root(new_root)
self.assert_value_at_address('bar', value)
# deleting an invalid key should raise an error
with self.assertRaises(KeyError):
self.delete('barf')
del_root = self.delete('bar')
# del_root hasn't been set yet, so address should still have value
self.assert_root(new_root)
self.assert_value_at_address('bar', value)
self.set_merkle_root(del_root)
self.assert_root(del_root)
self.assert_no_key('bar')
def test_merkle_trie_update(self):
init_root = self.get_merkle_root()
values = {}
key_hashes = {
key: _hash(key)
for key in (_random_string(10) for _ in range(1000))
}
for key, hashed in key_hashes.items():
value = {key: _random_string(512)}
new_root = self.set(hashed, value, ishash=True)
values[hashed] = value
self.set_merkle_root(new_root)
self.assert_not_root(init_root)
for address, value in values.items():
self.assert_value_at_address(
address, value, ishash=True)
set_items = {
hashed: {
key: 5.0
}
for key, hashed in random.sample(key_hashes.items(), 50)
}
values.update(set_items)
delete_items = {
hashed
for hashed in random.sample(list(key_hashes.values()), 50)
}
# make sure there are no sets and deletes of the same key
delete_items = delete_items - set_items.keys()
for addr in delete_items:
del values[addr]
virtual_root = self.update(set_items, delete_items, virtual=True)
# virtual root shouldn't match actual contents of tree
with self.assertRaises(KeyError):
self.set_merkle_root(virtual_root)
actual_root = self.update(set_items, delete_items, virtual=False)
#.........这里部分代码省略.........