本文整理汇总了Python中sawtooth_validator.state.merkle.MerkleDatabase.set_merkle_root方法的典型用法代码示例。如果您正苦于以下问题:Python MerkleDatabase.set_merkle_root方法的具体用法?Python MerkleDatabase.set_merkle_root怎么用?Python MerkleDatabase.set_merkle_root使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sawtooth_validator.state.merkle.MerkleDatabase
的用法示例。
在下文中一共展示了MerkleDatabase.set_merkle_root方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: compute_state_hashes_wo_scheduler
# 需要导入模块: from sawtooth_validator.state.merkle import MerkleDatabase [as 别名]
# 或者: from sawtooth_validator.state.merkle.MerkleDatabase import set_merkle_root [as 别名]
def compute_state_hashes_wo_scheduler(self):
"""Creates a state hash from the state updates from each txn in a
valid batch.
Returns state_hashes (list of str): The merkle roots from state
changes in 1 or more blocks in the yaml file.
"""
tree = MerkleDatabase(database=DictDatabase())
state_hashes = []
updates = {}
for batch in self._batches:
b_id = batch.header_signature
result = self._batch_results[b_id]
if result.is_valid:
for txn in batch.transactions:
txn_id = txn.header_signature
_, address_values = self._txn_execution[txn_id]
batch_updates = {}
for pair in address_values:
batch_updates.update({a: pair[a] for a in pair.keys()})
# since this is entirely serial, any overwrite
# of an address is expected and desirable.
updates.update(batch_updates)
# This handles yaml files that have state roots in them
if result.state_hash is not None:
s_h = tree.update(set_items=updates, virtual=False)
tree.set_merkle_root(merkle_root=s_h)
state_hashes.append(s_h)
if len(state_hashes) == 0:
state_hashes.append(tree.update(set_items=updates))
return state_hashes
示例2: create_view
# 需要导入模块: from sawtooth_validator.state.merkle import MerkleDatabase [as 别名]
# 或者: from sawtooth_validator.state.merkle.MerkleDatabase import set_merkle_root [as 别名]
def create_view(self, state_root_hash=None):
"""Creates a StateView for the given state root hash.
Args:
state_root_hash (str): The state root hash of the state view
to return. If None, returns the state view for the
Returns:
StateView: state view locked to the given root hash.
"""
# Create a default Merkle database and if we have a state root hash,
# update the Merkle database's root to that
merkle_db = MerkleDatabase(self._database)
if state_root_hash is not None:
merkle_db.set_merkle_root(state_root_hash)
return StateView(merkle_db)
示例3: compute_state_hashes_wo_scheduler
# 需要导入模块: from sawtooth_validator.state.merkle import MerkleDatabase [as 别名]
# 或者: from sawtooth_validator.state.merkle.MerkleDatabase import set_merkle_root [as 别名]
def compute_state_hashes_wo_scheduler(self, base_dir):
"""Creates a state hash from the state updates from each txn in a
valid batch.
Returns state_hashes (list of str): The merkle roots from state
changes in 1 or more blocks in the yaml file.
"""
database = NativeLmdbDatabase(
os.path.join(base_dir, 'compute_state_hashes_wo_scheduler.lmdb'),
indexes=MerkleDatabase.create_index_configuration(),
_size=10 * 1024 * 1024)
tree = MerkleDatabase(database=database)
state_hashes = []
updates = {}
for batch in self._batches:
b_id = batch.header_signature
result = self._batch_results[b_id]
if result.is_valid:
for txn in batch.transactions:
txn_id = txn.header_signature
_, address_values, deletes = self._txn_execution[txn_id]
batch_updates = {}
for pair in address_values:
batch_updates.update({a: pair[a] for a in pair.keys()})
# since this is entirely serial, any overwrite
# of an address is expected and desirable.
updates.update(batch_updates)
for address in deletes:
if address in updates:
del updates[address]
# This handles yaml files that have state roots in them
if result.state_hash is not None:
s_h = tree.update(set_items=updates, virtual=False)
tree.set_merkle_root(merkle_root=s_h)
state_hashes.append(s_h)
if not state_hashes:
state_hashes.append(tree.update(set_items=updates))
return state_hashes
示例4: StateGetRequestHandler
# 需要导入模块: from sawtooth_validator.state.merkle import MerkleDatabase [as 别名]
# 或者: from sawtooth_validator.state.merkle.MerkleDatabase import set_merkle_root [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)
示例5: StateListRequestHandler
# 需要导入模块: from sawtooth_validator.state.merkle import MerkleDatabase [as 别名]
# 或者: from sawtooth_validator.state.merkle.MerkleDatabase import set_merkle_root [as 别名]
class StateListRequestHandler(Handler):
def __init__(self, database):
self._tree = MerkleDatabase(database)
def handle(self, identity, message_content):
request = client_pb2.ClientStateListRequest()
resp_proto = client_pb2.ClientStateListResponse
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:
prefix = request.prefix
leaves = self._tree.leaves(prefix)
if len(leaves) == 0:
status = resp_proto.NORESOURCE
response = resp_proto(status=status)
else:
entries = [Entry(address=a, data=v) for a, v in leaves.items()]
response = resp_proto(status=status, entries=entries)
return HandlerResult(
status=HandlerStatus.RETURN,
message_out=response,
message_type=validator_pb2.Message.CLIENT_STATE_LIST_RESPONSE)
示例6: test_complex_basecontext_squash
# 需要导入模块: from sawtooth_validator.state.merkle import MerkleDatabase [as 别名]
# 或者: from sawtooth_validator.state.merkle.MerkleDatabase import set_merkle_root [as 别名]
#.........这里部分代码省略.........
context_id=context_2b,
address_value_list=[{a: bytes(v)}
for a, v in zip(outputs_2b,
range(len(outputs_2b)))]
)
# 3)
inputs_3_2a_1 = [self._create_address('qq'),
self._create_address('dd')]
outputs_3_2a_1 = [self._create_address('dd'),
self._create_address('pp')]
context_3_2a_1 = self.context_manager.create_context(
state_hash=sh1,
base_contexts=[context_2a],
inputs=inputs_3_2a_1,
outputs=outputs_3_2a_1
)
inputs_3_2a_2 = [self._create_address('aa')]
outputs_3_2a_2 = [self._create_address('aa'),
self._create_address('ll')]
context_3_2a_2 = self.context_manager.create_context(
state_hash=sh1,
base_contexts=[context_2a],
inputs=inputs_3_2a_2,
outputs=outputs_3_2a_2)
inputs_3_2b_1 = [self._create_address('nn'),
self._create_address('ab')]
outputs_3_2b_1 = [self._create_address('mm'),
self._create_address('ba')]
context_3_2b_1 = self.context_manager.create_context(
state_hash=sh1,
base_contexts=[context_2b],
inputs=inputs_3_2b_1,
outputs=outputs_3_2b_1)
inputs_3_2b_2 = [self._create_address('nn'),
self._create_address('oo')]
outputs_3_2b_2 = [self._create_address('ab'),
self._create_address('oo')]
context_3_2b_2 = self.context_manager.create_context(
state_hash=sh1,
base_contexts=[context_2b],
inputs=inputs_3_2b_2,
outputs=outputs_3_2b_2)
self.context_manager.set(
context_id=context_3_2a_1,
address_value_list=[{a: bytes(v)}
for a, v in zip(outputs_3_2a_1,
range(len(outputs_3_2a_1)))])
self.context_manager.set(
context_id=context_3_2a_2,
address_value_list=[{a: bytes(v)}
for a, v in zip(outputs_3_2a_2,
range(len(outputs_3_2a_2)))])
self.context_manager.set(
context_id=context_3_2b_1,
address_value_list=[{a: bytes(v)}
for a, v in zip(outputs_3_2b_1,
range(len(outputs_3_2b_1)))])
self.context_manager.set(
context_id=context_3_2b_2,
address_value_list=[{a: bytes(v)}
for a, v in zip(outputs_3_2b_2,
range(len(outputs_3_2b_2)))])
# 4)
sh2 = squash(
state_root=sh1,
context_ids=[context_3_2a_1, context_3_2a_2,
context_3_2b_1, context_3_2b_2],
persist=False,
clean_up=True)
tree = MerkleDatabase(self.database_results)
state_hash_from_1 = tree.update(
set_items={a: v for a, v in zip(outputs_1,
[bytes(i)
for i in range(len(outputs_1))])},
virtual=False)
self.assertEquals(state_hash_from_1, sh1,
"The manually calculated state hash from the first "
"context and the one calculated by squashing that "
"state hash should be the same")
tree.set_merkle_root(state_hash_from_1)
test_sh2 = tree.update(set_items={self._create_address('aa'): bytes(0),
self._create_address('ab'): bytes(0),
self._create_address('ba'): bytes(1),
self._create_address('dd'): bytes(0),
self._create_address('ll'): bytes(1),
self._create_address('mm'): bytes(0),
self._create_address('oo'): bytes(1),
self._create_address('pp'): bytes(1),
self._create_address('nn'): bytes(0),
self._create_address('cc'): bytes(0)})
self.assertEquals(sh2, test_sh2, "Manually calculated and context "
"manager calculated merkle hashes "
"are the same")
示例7: TestIdentityView
# 需要导入模块: from sawtooth_validator.state.merkle import MerkleDatabase [as 别名]
# 或者: from sawtooth_validator.state.merkle.MerkleDatabase import set_merkle_root [as 别名]
class TestIdentityView(unittest.TestCase):
def __init__(self, test_name):
super().__init__(test_name)
self._temp_dir = None
def setUp(self):
self._temp_dir = tempfile.mkdtemp()
self._database = NativeLmdbDatabase(
os.path.join(self._temp_dir, 'test_identity_view.lmdb'),
indexes=MerkleDatabase.create_index_configuration(),
_size=10 * 1024 * 1024)
self._tree = MerkleDatabase(self._database)
def tearDown(self):
shutil.rmtree(self._temp_dir)
def test_identityview_roles(self):
"""Tests get_role and get_roles get the correct Roles and the
IdentityViewFactory produces the correct view of the database.
Notes:
1. Create an empty MerkleDatabase and update it with one
serialized RoleList.
2. Assert that get_role returns that named Role.
3. Assert that get_role returns None for a name that doesn't
correspond to a Role.
4. Assert that all the Roles are returned by get_roles.
5. Update the MerkleDatabase with another serialized RoleList with
a different name.
6. Repeat 2.
7. Repeat 3.
8. Repeat 4.
"""
state_view_factory = StateViewFactory(self._database)
identity_view_factory = identity_view.IdentityViewFactory(
state_view_factory=state_view_factory)
# 1.
role_list = identity_pb2.RoleList()
role1 = role_list.roles.add()
role1_name = "sawtooth.test.example1"
role1.name = role1_name
role1.policy_name = "this_is_an_example"
state_root1 = self._tree.update(
set_items={
_get_role_address(role1_name): role_list.SerializeToString()
},
virtual=False)
# 2.
identity_view1 = identity_view_factory.create_identity_view(
state_hash=state_root1)
self.assertEqual(
identity_view1.get_role(role1_name),
role1,
"IdentityView().get_role returns the correct Role by name.")
# 3.
self.assertIsNone(
identity_view1.get_role("Not-a-Role"),
"IdentityView().get_role returns None if there is "
"no Role with that name.")
# 4.
self.assertEqual(identity_view1.get_roles(),
[role1],
"IdentityView().get_roles returns all the roles in"
" State.")
# 5.
role_list2 = identity_pb2.RoleList()
role2 = role_list2.roles.add()
role2_name = "sawtooth.test.example2"
role2.name = role2_name
role2.policy_name = "this_is_another_example"
self._tree.set_merkle_root(merkle_root=state_root1)
state_root2 = self._tree.update(
{
_get_role_address(role2_name): role_list2.SerializeToString()
},
virtual=False)
# 6.
identity_view2 = identity_view_factory.create_identity_view(
state_hash=state_root2)
self.assertEqual(
identity_view2.get_role(role2_name),
role2,
"IdentityView().get_role returns the correct Role by name.")
#.........这里部分代码省略.........
示例8: TestSawtoothMerkleTrie
# 需要导入模块: from sawtooth_validator.state.merkle import MerkleDatabase [as 别名]
# 或者: from sawtooth_validator.state.merkle.MerkleDatabase import set_merkle_root [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)
#.........这里部分代码省略.........
示例9: TestSawtoothMerkleTrie
# 需要导入模块: from sawtooth_validator.state.merkle import MerkleDatabase [as 别名]
# 或者: from sawtooth_validator.state.merkle.MerkleDatabase import set_merkle_root [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])
示例10: TestSawtoothMerkleTrie
# 需要导入模块: from sawtooth_validator.state.merkle import MerkleDatabase [as 别名]
# 或者: from sawtooth_validator.state.merkle.MerkleDatabase import set_merkle_root [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)
#.........这里部分代码省略.........