Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_cancel_transfer(iterations=ITERATIONS):
amount = 1
expiration = 1
hashlock = sha3(ADDRESS)
lock = Lock(amount, expiration, hashlock)
identifier = 1
nonce = 1
asset = ADDRESS
transferred_amount = 1
recipient = ADDRESS
locksroot = sha3(ADDRESS)
msg = RefundTransfer(
identifier,
nonce,
asset,
transferred_amount,
recipient,
locksroot,
lock,
)
msg.sign(PRIVKEY, ADDRESS)
run_timeit('RefundTransfer', msg, iterations=iterations)
def test_mediated_transfer(iterations=ITERATIONS):
identifier = 1
amount = 1
expiration = 1
hashlock = sha3(ADDRESS)
lock = Lock(amount, expiration, hashlock)
nonce = 1
asset = ADDRESS
balance = 1
recipient = ADDRESS
locksroot = sha3(ADDRESS)
target = ADDRESS
initiator = ADDRESS
msg = MediatedTransfer(
identifier,
nonce,
asset,
balance,
recipient,
locksroot,
lock,
target,
initiator,
fee=0,
)
msg.sign(PRIVKEY, ADDRESS)
raise ValueError('Negative transfer')
amount = transfer.transferred_amount - from_state.transferred_amount
distributable = from_state.distributable(to_state)
if isinstance(transfer, DirectTransfer):
if amount > distributable:
raise InsufficientBalance(transfer)
elif isinstance(transfer, LockedTransfer):
if amount + transfer.lock.amount > distributable:
raise InsufficientBalance(transfer)
elif isinstance(transfer, Secret):
hashlock = sha3(transfer.secret)
lock = from_state.get_lock_by_hashlock(hashlock)
transferred_amount = from_state.transferred_amount + lock.amount
# transfer.transferred_amount could be larger than the previous
# transferred_amount + lock.amount, that scenario is a bug of the
# payer
if transfer.transferred_amount != transferred_amount:
raise ValueError(
'invalid transferred_amount, expected: {} got: {}'.format(
transferred_amount,
transfer.transferred_amount,
)
)
# all checks need to be done before the internal state of the channel
# is changed, otherwise if a check fails and the state was changed the
def test_mediated_transfer(iterations=ITERATIONS):
identifier = 1
amount = 1
expiration = 1
hashlock = sha3(ADDRESS)
lock = Lock(amount, expiration, hashlock)
nonce = 1
asset = ADDRESS
balance = 1
recipient = ADDRESS
locksroot = sha3(ADDRESS)
target = ADDRESS
initiator = ADDRESS
msg = MediatedTransfer(
identifier,
nonce,
asset,
balance,
recipient,
locksroot,
def profile_transfer(num_nodes=10, channels_per_node=2):
num_assets = 1
deposit = 10000
assets = [
sha3('asset:{}'.format(number))[:20]
for number in range(num_assets)
]
private_keys = [
sha3('speed:{}'.format(position))
for position in range(num_nodes)
]
BlockChainServiceMock.reset()
blockchain_services = list()
for privkey in private_keys:
blockchain = BlockChainServiceMock(
privkey,
MOCK_REGISTRY_ADDRESS,
)
blockchain_services.append(blockchain)
registry = blockchain_services[0].registry(MOCK_REGISTRY_ADDRESS)
for asset in assets:
registry.add_asset(asset)
def register_secret(self, secret):
""" Register a secret so that it can be used in a balance proof.
Note:
This methods needs to be called once a `Secret` message is received
or a `SecretRevealed` event happens.
Raises:
ValueError: If the hashlock is not known.
"""
hashlock = sha3(secret)
if not self.is_known(hashlock):
raise ValueError('secret does not correspond to any hashlock')
if self.is_locked(hashlock):
pendinglock = self.hashlocks_to_pendinglocks[hashlock]
del self.hashlocks_to_pendinglocks[hashlock]
self.hashlocks_to_unclaimedlocks[hashlock] = UnlockPartialProof(
pendinglock.lock,
pendinglock.lockhashed,
secret,
)
def transfer_speed(num_transfers=100, max_locked=100): # pylint: disable=too-many-locals
channels_per_node = 1
num_nodes = 2
num_assets = 1
private_keys = [
sha3('speed:{}'.format(position))
for position in range(num_nodes)
]
assets = [
sha3('asset:{}'.format(number))[:20]
for number in range(num_assets)
]
amounts = [
a % 100 + 1
for a in range(1, num_transfers + 1)
]
deposit = sum(amounts)
secrets = [
str(i)
for i in range(num_transfers)
]
BlockChainServiceMock._instance = True
def compute_merkleroot_without(self, without):
""" Compute the resulting merkle root if the lock `include` is added in
the tree.
"""
if not self.is_known(without.hashlock):
raise ValueError('unknown lock', lock=without)
leaves = list(self.merkletree.layers[LEAVES])
leaves.remove(sha3(without.as_bytes))
if leaves:
tree_without = MerkleTreeState(compute_layers(leaves))
locksroot = merkleroot(tree_without)
else:
locksroot = EMPTY_MERKLE_ROOT
return locksroot
DEFAULT_SETTLE_TIMEOUT,
DEFAULT_POLL_TIMEOUT,
UDPTransport,
BlockChainServiceMock
)
app0, app1 = apps # pylint: disable=unbalanced-tuple-unpacking
channel0 = app0.raiden.get_manager_by_asset_address(assets[0]).address_channel.values()[0]
channel1 = app1.raiden.get_manager_by_asset_address(assets[0]).address_channel.values()[0]
expiration = app0.raiden.chain.block_number() + DEFAULT_REVEAL_TIMEOUT + 3
start = time.time()
for i, amount in enumerate(amounts):
hashlock = sha3(secrets[i])
locked_transfer = channel0.create_lockedtransfer(
amount=amount,
identifier=1, # TODO: fill in identifier
expiration=expiration,
hashlock=hashlock,
)
app0.raiden.sign(locked_transfer)
channel0.register_transfer(locked_transfer)
channel1.register_transfer(locked_transfer)
if i > max_locked:
idx = i - max_locked
secret = secrets[idx]
channel0.register_secret(secret)
channel1.register_secret(secret)
def message_hash(self) -> bytes:
metadata_hash = (self.metadata and self.metadata.hash) or b""
return sha3(self._packed_data() + metadata_hash)