2018-03-19 00:46:48 +00:00
|
|
|
#!/usr/bin/env python3
|
2018-12-03 01:06:40 +00:00
|
|
|
|
|
|
|
|
2018-11-04 19:23:11 +00:00
|
|
|
"""
|
|
|
|
This file is part of nucypher.
|
|
|
|
|
|
|
|
nucypher is free software: you can redistribute it and/or modify
|
2019-03-05 02:50:11 +00:00
|
|
|
it under the terms of the GNU Affero General Public License as published by
|
2018-11-04 19:23:11 +00:00
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
nucypher is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2019-03-05 02:50:11 +00:00
|
|
|
GNU Affero General Public License for more details.
|
2018-11-04 19:23:11 +00:00
|
|
|
|
2019-03-05 02:50:11 +00:00
|
|
|
You should have received a copy of the GNU Affero General Public License
|
2018-11-04 19:23:11 +00:00
|
|
|
along with nucypher. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
"""
|
2018-03-19 00:46:48 +00:00
|
|
|
|
2019-02-05 12:46:11 +00:00
|
|
|
import coincurve
|
2018-12-13 22:56:16 +00:00
|
|
|
import json
|
2018-03-23 05:05:03 +00:00
|
|
|
import os
|
2019-02-05 12:46:11 +00:00
|
|
|
|
|
|
|
from cryptography.hazmat.primitives.asymmetric import ec
|
|
|
|
from eth_utils import to_canonical_address
|
|
|
|
|
2019-03-20 04:22:23 +00:00
|
|
|
from nucypher.blockchain.economics import TokenEconomics
|
|
|
|
from nucypher.policy.models import IndisputableEvidence, Policy
|
2019-02-05 12:46:11 +00:00
|
|
|
from umbral import pre
|
|
|
|
from umbral.curvebn import CurveBN
|
|
|
|
from umbral.keys import UmbralPrivateKey
|
|
|
|
from umbral.signing import Signer, Signature
|
2018-12-13 22:56:16 +00:00
|
|
|
|
|
|
|
import time
|
2018-12-08 20:23:26 +00:00
|
|
|
from os.path import abspath, dirname
|
|
|
|
|
|
|
|
import io
|
|
|
|
import re
|
2019-02-05 12:46:11 +00:00
|
|
|
|
|
|
|
from cryptography.hazmat.backends.openssl import backend
|
|
|
|
from cryptography.hazmat.primitives import hashes
|
2018-12-08 20:23:26 +00:00
|
|
|
from twisted.logger import globalLogPublisher, Logger, jsonFileLogObserver, ILogObserver
|
|
|
|
from zope.interface import provider
|
2018-06-15 05:51:28 +00:00
|
|
|
|
2019-03-22 03:36:38 +00:00
|
|
|
from nucypher.blockchain.eth.agents import NucypherTokenAgent, MinerAgent, PolicyAgent, MiningAdjudicatorAgent
|
2018-12-03 01:06:40 +00:00
|
|
|
from nucypher.utilities.sandbox.blockchain import TesterBlockchain
|
2018-03-07 19:22:59 +00:00
|
|
|
|
2018-12-08 20:23:26 +00:00
|
|
|
|
2019-02-05 12:46:11 +00:00
|
|
|
ALGORITHM_SHA256 = 1
|
2019-03-20 04:22:23 +00:00
|
|
|
TOKEN_ECONOMICS = TokenEconomics()
|
2019-03-22 03:36:38 +00:00
|
|
|
MIN_ALLOWED_LOCKED = TOKEN_ECONOMICS.minimum_allowed_locked
|
2019-03-20 04:22:23 +00:00
|
|
|
MIN_LOCKED_PERIODS = TOKEN_ECONOMICS.minimum_locked_periods
|
|
|
|
MAX_ALLOWED_LOCKED = TOKEN_ECONOMICS.maximum_allowed_locked
|
|
|
|
MAX_MINTING_PERIODS = TOKEN_ECONOMICS.maximum_locked_periods
|
2019-02-05 12:46:11 +00:00
|
|
|
|
|
|
|
|
2018-12-13 22:56:16 +00:00
|
|
|
class AnalyzeGas:
|
2018-12-14 18:10:08 +00:00
|
|
|
"""
|
|
|
|
Callable twisted log observer with built-in record-keeping for gas estimation runs.
|
|
|
|
"""
|
2018-12-08 20:23:26 +00:00
|
|
|
|
2018-12-14 18:10:08 +00:00
|
|
|
# Logging
|
2018-12-13 22:56:16 +00:00
|
|
|
LOG_NAME = 'estimate-gas'
|
|
|
|
LOG_FILENAME = '{}.log.json'.format(LOG_NAME)
|
|
|
|
OUTPUT_DIR = os.path.join(abspath(dirname(__file__)), 'results')
|
2018-12-14 18:10:08 +00:00
|
|
|
JSON_OUTPUT_FILENAME = '{}.json'.format(LOG_NAME)
|
|
|
|
|
|
|
|
_PATTERN = re.compile(r'''
|
|
|
|
^ # Anchor at the start of a string
|
|
|
|
(.+) # Any character sequence longer than 1; Captured
|
|
|
|
\s=\s # Space-Equal-Space
|
|
|
|
(\d+) # A sequence of digits; Captured
|
|
|
|
$ # Anchor at the end of the string
|
|
|
|
''', re.VERBOSE)
|
2018-12-08 20:23:26 +00:00
|
|
|
|
2018-12-13 22:56:16 +00:00
|
|
|
def __init__(self) -> None:
|
2018-12-14 18:10:08 +00:00
|
|
|
|
|
|
|
self.log = Logger(self.__class__.__name__)
|
2018-12-13 22:56:16 +00:00
|
|
|
self.gas_estimations = dict()
|
2018-12-08 20:23:26 +00:00
|
|
|
|
2018-12-13 22:56:16 +00:00
|
|
|
if not os.path.isdir(self.OUTPUT_DIR):
|
|
|
|
os.mkdir(self.OUTPUT_DIR)
|
2018-12-08 20:23:26 +00:00
|
|
|
|
|
|
|
@provider(ILogObserver)
|
2018-12-13 22:56:16 +00:00
|
|
|
def __call__(self, event, *args, **kwargs) -> None:
|
2018-12-14 18:10:08 +00:00
|
|
|
|
2018-12-13 22:56:16 +00:00
|
|
|
if event.get('log_namespace') == self.LOG_NAME:
|
2018-12-08 20:23:26 +00:00
|
|
|
message = event.get("log_format")
|
2018-12-14 18:10:08 +00:00
|
|
|
|
|
|
|
matches = self._PATTERN.match(message)
|
|
|
|
if not matches:
|
|
|
|
self.log.debug("No match for {} with pattern {}".format(message, self._PATTERN))
|
2018-12-08 20:23:26 +00:00
|
|
|
return
|
2018-12-14 18:10:08 +00:00
|
|
|
|
|
|
|
label, gas = matches.groups()
|
2018-12-13 22:56:16 +00:00
|
|
|
self.paint_line(label, gas)
|
2018-12-08 20:23:26 +00:00
|
|
|
self.gas_estimations[label] = int(gas)
|
|
|
|
|
2018-12-13 22:56:16 +00:00
|
|
|
@staticmethod
|
|
|
|
def paint_line(label: str, gas: str) -> None:
|
2018-12-21 13:59:38 +00:00
|
|
|
print('{label} {gas:,}'.format(label=label.ljust(70, '.'), gas=int(gas)))
|
2018-01-27 13:05:19 +00:00
|
|
|
|
2018-12-13 22:56:16 +00:00
|
|
|
def to_json_file(self) -> None:
|
|
|
|
print('Saving JSON Output...')
|
2018-06-15 05:51:28 +00:00
|
|
|
|
2018-12-13 22:56:16 +00:00
|
|
|
epoch_time = str(int(time.time()))
|
|
|
|
timestamped_filename = '{}-{}'.format(epoch_time, self.JSON_OUTPUT_FILENAME)
|
|
|
|
filepath = os.path.join(self.OUTPUT_DIR, timestamped_filename)
|
|
|
|
with open(filepath, 'w') as file:
|
|
|
|
file.write(json.dumps(self.gas_estimations, indent=4))
|
2018-06-15 05:51:28 +00:00
|
|
|
|
2018-12-13 22:56:16 +00:00
|
|
|
def start_collection(self) -> None:
|
|
|
|
print("Starting Data Collection...")
|
2018-12-08 20:23:26 +00:00
|
|
|
|
2018-12-13 22:56:16 +00:00
|
|
|
json_filepath = os.path.join(self.OUTPUT_DIR, AnalyzeGas.LOG_FILENAME)
|
|
|
|
json_io = io.open(json_filepath, "w")
|
|
|
|
json_observer = jsonFileLogObserver(json_io)
|
|
|
|
globalLogPublisher.addObserver(json_observer)
|
|
|
|
globalLogPublisher.addObserver(self)
|
2018-06-15 05:51:28 +00:00
|
|
|
|
2018-12-13 22:56:16 +00:00
|
|
|
|
2019-02-05 12:46:11 +00:00
|
|
|
# TODO organize support functions
|
|
|
|
def generate_args_for_slashing(testerchain, miner, corrupt: bool = True):
|
|
|
|
def sign_data(data, umbral_privkey):
|
|
|
|
umbral_pubkey_bytes = umbral_privkey.get_pubkey().to_bytes(is_compressed=False)
|
|
|
|
|
|
|
|
# Prepare hash of the data
|
|
|
|
hash_ctx = hashes.Hash(hashes.SHA256(), backend=backend)
|
|
|
|
hash_ctx.update(data)
|
|
|
|
data_hash = hash_ctx.finalize()
|
|
|
|
|
|
|
|
# Sign data and calculate recoverable signature
|
|
|
|
cryptography_priv_key = umbral_privkey.to_cryptography_privkey()
|
|
|
|
signature_der_bytes = cryptography_priv_key.sign(data, ec.ECDSA(hashes.SHA256()))
|
|
|
|
signature = Signature.from_bytes(signature_der_bytes, der_encoded=True)
|
|
|
|
recoverable_signature = bytes(signature) + bytes([0])
|
|
|
|
pubkey_bytes = coincurve.PublicKey.from_signature_and_message(recoverable_signature, data_hash, hasher=None) \
|
|
|
|
.format(compressed=False)
|
|
|
|
if pubkey_bytes != umbral_pubkey_bytes:
|
|
|
|
recoverable_signature = bytes(signature) + bytes([1])
|
|
|
|
return recoverable_signature
|
|
|
|
|
|
|
|
delegating_privkey = UmbralPrivateKey.gen_key()
|
|
|
|
_symmetric_key, capsule = pre._encapsulate(delegating_privkey.get_pubkey())
|
|
|
|
signing_privkey = UmbralPrivateKey.gen_key()
|
|
|
|
signer = Signer(signing_privkey)
|
|
|
|
priv_key_bob = UmbralPrivateKey.gen_key()
|
|
|
|
pub_key_bob = priv_key_bob.get_pubkey()
|
|
|
|
kfrags = pre.generate_kfrags(delegating_privkey=delegating_privkey,
|
|
|
|
signer=signer,
|
|
|
|
receiving_pubkey=pub_key_bob,
|
|
|
|
threshold=2,
|
|
|
|
N=4,
|
|
|
|
sign_delegating_key=False,
|
|
|
|
sign_receiving_key=False)
|
|
|
|
capsule.set_correctness_keys(delegating_privkey.get_pubkey(), pub_key_bob, signing_privkey.get_pubkey())
|
|
|
|
cfrag = pre.reencrypt(kfrags[0], capsule, metadata=os.urandom(34))
|
|
|
|
capsule_bytes = capsule.to_bytes()
|
|
|
|
if corrupt:
|
|
|
|
cfrag.proof.bn_sig = CurveBN.gen_rand(capsule.params.curve)
|
|
|
|
cfrag_bytes = cfrag.to_bytes()
|
|
|
|
hash_ctx = hashes.Hash(hashes.SHA256(), backend=backend)
|
|
|
|
hash_ctx.update(capsule_bytes + cfrag_bytes)
|
|
|
|
requester_umbral_private_key = UmbralPrivateKey.gen_key()
|
|
|
|
requester_umbral_public_key_bytes = requester_umbral_private_key.get_pubkey().to_bytes(is_compressed=False)
|
|
|
|
capsule_signature_by_requester = sign_data(capsule_bytes, requester_umbral_private_key)
|
|
|
|
miner_umbral_private_key = UmbralPrivateKey.gen_key()
|
|
|
|
miner_umbral_public_key_bytes = miner_umbral_private_key.get_pubkey().to_bytes(is_compressed=False)
|
|
|
|
# Sign Umbral public key using eth-key
|
|
|
|
hash_ctx = hashes.Hash(hashes.SHA256(), backend=backend)
|
|
|
|
hash_ctx.update(miner_umbral_public_key_bytes)
|
|
|
|
miner_umbral_public_key_hash = hash_ctx.finalize()
|
|
|
|
address = to_canonical_address(miner)
|
|
|
|
sig_key = testerchain.interface.provider.ethereum_tester.backend._key_lookup[address]
|
|
|
|
signed_miner_umbral_public_key = bytes(sig_key.sign_msg_hash(miner_umbral_public_key_hash))
|
|
|
|
|
|
|
|
capsule_signature_by_requester_and_miner = sign_data(capsule_signature_by_requester, miner_umbral_private_key)
|
|
|
|
cfrag_signature_by_miner = sign_data(cfrag_bytes, miner_umbral_private_key)
|
|
|
|
evidence = IndisputableEvidence(capsule, cfrag, ursula=None)
|
|
|
|
evidence_data = evidence.precompute_values()
|
|
|
|
return (capsule_bytes,
|
|
|
|
capsule_signature_by_requester,
|
|
|
|
capsule_signature_by_requester_and_miner,
|
|
|
|
cfrag_bytes,
|
|
|
|
cfrag_signature_by_miner,
|
|
|
|
requester_umbral_public_key_bytes,
|
|
|
|
miner_umbral_public_key_bytes,
|
|
|
|
signed_miner_umbral_public_key,
|
|
|
|
evidence_data)
|
|
|
|
|
|
|
|
|
2018-12-14 18:10:08 +00:00
|
|
|
def estimate_gas(analyzer: AnalyzeGas = None) -> None:
|
|
|
|
"""
|
|
|
|
Execute a linear sequence of NyCypher transactions mimicking
|
|
|
|
post-deployment usage on a local PyEVM blockchain;
|
|
|
|
Record the resulting estimated transaction gas expenditure.
|
|
|
|
|
|
|
|
Note: The function calls below are *order dependant*
|
|
|
|
"""
|
2018-12-13 22:56:16 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# Setup
|
|
|
|
#
|
2018-12-15 01:36:48 +00:00
|
|
|
|
2018-12-18 18:31:41 +00:00
|
|
|
if analyzer is None:
|
2018-12-14 18:10:08 +00:00
|
|
|
analyzer = AnalyzeGas()
|
|
|
|
|
2018-12-13 22:56:16 +00:00
|
|
|
log = Logger(AnalyzeGas.LOG_NAME)
|
|
|
|
|
2018-12-14 18:10:08 +00:00
|
|
|
# Blockchain
|
2018-12-18 18:31:41 +00:00
|
|
|
testerchain, agents = TesterBlockchain.bootstrap_network()
|
2018-12-03 01:06:40 +00:00
|
|
|
web3 = testerchain.interface.w3
|
2018-12-13 22:56:16 +00:00
|
|
|
|
2018-12-15 01:36:48 +00:00
|
|
|
# Accounts
|
|
|
|
origin, ursula1, ursula2, ursula3, alice1, *everyone_else = testerchain.interface.w3.eth.accounts
|
|
|
|
|
2018-12-14 18:10:08 +00:00
|
|
|
# Contracts
|
2018-12-15 01:36:48 +00:00
|
|
|
token_agent = NucypherTokenAgent(blockchain=testerchain)
|
|
|
|
miner_agent = MinerAgent(blockchain=testerchain)
|
|
|
|
policy_agent = PolicyAgent(blockchain=testerchain)
|
2019-03-22 03:36:38 +00:00
|
|
|
adjudicator_agent = MiningAdjudicatorAgent()
|
2018-12-15 01:36:48 +00:00
|
|
|
|
|
|
|
# Contract Callers
|
2018-12-14 17:11:09 +00:00
|
|
|
token_functions = token_agent.contract.functions
|
|
|
|
miner_functions = miner_agent.contract.functions
|
|
|
|
policy_functions = policy_agent.contract.functions
|
2019-03-22 03:36:38 +00:00
|
|
|
adjudicator_functions = adjudicator_agent.contract.functions
|
2018-12-08 20:23:26 +00:00
|
|
|
|
2018-12-14 18:10:08 +00:00
|
|
|
analyzer.start_collection()
|
2018-12-13 22:56:16 +00:00
|
|
|
print("********* Estimating Gas *********")
|
2018-06-15 05:51:28 +00:00
|
|
|
|
2018-12-14 17:11:09 +00:00
|
|
|
#
|
2018-03-02 17:10:49 +00:00
|
|
|
# Pre deposit tokens
|
2018-12-14 17:11:09 +00:00
|
|
|
#
|
|
|
|
tx = token_functions.approve(miner_agent.contract_address, MIN_ALLOWED_LOCKED * 5).transact({'from': origin})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
2018-12-14 17:11:09 +00:00
|
|
|
log.info("Pre-deposit tokens for 5 owners = " + str(miner_functions.preDeposit(everyone_else[0:5],
|
|
|
|
[MIN_ALLOWED_LOCKED] * 5,
|
|
|
|
[MIN_LOCKED_PERIODS] * 5)
|
2019-02-05 12:46:11 +00:00
|
|
|
.estimateGas({'from': origin})))
|
2018-02-08 09:29:32 +00:00
|
|
|
|
2018-12-14 17:11:09 +00:00
|
|
|
#
|
2018-02-08 09:29:32 +00:00
|
|
|
# Give Ursula and Alice some coins
|
2018-12-14 17:11:09 +00:00
|
|
|
#
|
2019-02-05 12:46:11 +00:00
|
|
|
log.info("Transfer tokens = " + str(
|
|
|
|
token_functions.transfer(ursula1, MIN_ALLOWED_LOCKED * 10).estimateGas({'from': origin})))
|
2018-12-14 17:11:09 +00:00
|
|
|
tx = token_functions.transfer(ursula1, MIN_ALLOWED_LOCKED * 10).transact({'from': origin})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
2018-12-14 17:11:09 +00:00
|
|
|
tx = token_functions.transfer(ursula2, MIN_ALLOWED_LOCKED * 10).transact({'from': origin})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
2018-12-14 17:11:09 +00:00
|
|
|
tx = token_functions.transfer(ursula3, MIN_ALLOWED_LOCKED * 10).transact({'from': origin})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
2018-02-08 09:29:32 +00:00
|
|
|
|
2018-12-14 17:11:09 +00:00
|
|
|
#
|
2018-02-08 09:29:32 +00:00
|
|
|
# Ursula and Alice give Escrow rights to transfer
|
2018-12-14 17:11:09 +00:00
|
|
|
#
|
|
|
|
log.info("Approving transfer = "
|
2019-02-05 12:46:11 +00:00
|
|
|
+ str(
|
|
|
|
token_functions.approve(miner_agent.contract_address, MIN_ALLOWED_LOCKED * 6).estimateGas({'from': ursula1})))
|
2018-12-14 17:11:09 +00:00
|
|
|
tx = token_functions.approve(miner_agent.contract_address, MIN_ALLOWED_LOCKED * 6).transact({'from': ursula1})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
2018-12-14 17:11:09 +00:00
|
|
|
tx = token_functions.approve(miner_agent.contract_address, MIN_ALLOWED_LOCKED * 6).transact({'from': ursula2})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
2018-12-14 17:11:09 +00:00
|
|
|
tx = token_functions.approve(miner_agent.contract_address, MIN_ALLOWED_LOCKED * 6).transact({'from': ursula3})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
2018-02-08 09:29:32 +00:00
|
|
|
|
2018-12-14 17:11:09 +00:00
|
|
|
#
|
2018-02-08 09:29:32 +00:00
|
|
|
# Ursula and Alice transfer some tokens to the escrow and lock them
|
2018-12-14 17:11:09 +00:00
|
|
|
#
|
2019-03-22 03:36:38 +00:00
|
|
|
log.info("First initial deposit tokens = " + str(miner_functions.deposit(MIN_ALLOWED_LOCKED * 3, MIN_LOCKED_PERIODS).estimateGas({'from': ursula1})))
|
2018-12-14 17:11:09 +00:00
|
|
|
tx = miner_functions.deposit(MIN_ALLOWED_LOCKED * 3, MIN_LOCKED_PERIODS).transact({'from': ursula1})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
2018-12-08 20:23:26 +00:00
|
|
|
log.info("Second initial deposit tokens = " +
|
2019-02-05 12:46:11 +00:00
|
|
|
str(miner_functions.deposit(MIN_ALLOWED_LOCKED * 3, MIN_LOCKED_PERIODS).estimateGas({'from': ursula2})))
|
2018-12-14 17:11:09 +00:00
|
|
|
tx = miner_functions.deposit(MIN_ALLOWED_LOCKED * 3, MIN_LOCKED_PERIODS).transact({'from': ursula2})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
2018-12-08 20:23:26 +00:00
|
|
|
log.info("Third initial deposit tokens = " +
|
2019-02-05 12:46:11 +00:00
|
|
|
str(miner_functions.deposit(MIN_ALLOWED_LOCKED * 3, MIN_LOCKED_PERIODS).estimateGas({'from': ursula3})))
|
2018-12-14 17:11:09 +00:00
|
|
|
tx = miner_functions.deposit(MIN_ALLOWED_LOCKED * 3, MIN_LOCKED_PERIODS).transact({'from': ursula3})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
2018-02-08 09:29:32 +00:00
|
|
|
|
2018-12-14 17:11:09 +00:00
|
|
|
#
|
2018-02-08 09:29:32 +00:00
|
|
|
# Wait 1 period and confirm activity
|
2018-12-14 17:11:09 +00:00
|
|
|
#
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.time_travel(periods=1)
|
2018-12-08 20:23:26 +00:00
|
|
|
log.info("First confirm activity = " +
|
2019-02-05 12:46:11 +00:00
|
|
|
str(miner_functions.confirmActivity().estimateGas({'from': ursula1})))
|
2018-12-14 17:11:09 +00:00
|
|
|
tx = miner_functions.confirmActivity().transact({'from': ursula1})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
2018-12-08 20:23:26 +00:00
|
|
|
log.info("Second confirm activity = " +
|
2019-02-05 12:46:11 +00:00
|
|
|
str(miner_functions.confirmActivity().estimateGas({'from': ursula2})))
|
2018-12-14 17:11:09 +00:00
|
|
|
tx = miner_functions.confirmActivity().transact({'from': ursula2})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
2018-12-08 20:23:26 +00:00
|
|
|
log.info("Third confirm activity = " +
|
2019-02-05 12:46:11 +00:00
|
|
|
str(miner_functions.confirmActivity().estimateGas({'from': ursula3})))
|
2018-12-14 17:11:09 +00:00
|
|
|
tx = miner_functions.confirmActivity().transact({'from': ursula3})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
2018-02-08 09:29:32 +00:00
|
|
|
|
2018-12-14 17:11:09 +00:00
|
|
|
#
|
2018-02-08 09:29:32 +00:00
|
|
|
# Wait 1 period and mint tokens
|
2018-12-14 17:11:09 +00:00
|
|
|
#
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.time_travel(periods=1)
|
2018-12-14 17:11:09 +00:00
|
|
|
log.info("First mining (1 stake) = " + str(miner_functions.mint().estimateGas({'from': ursula1})))
|
|
|
|
tx = miner_functions.mint().transact({'from': ursula1})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
2018-12-14 17:11:09 +00:00
|
|
|
log.info("Second mining (1 stake) = " + str(miner_functions.mint().estimateGas({'from': ursula2})))
|
|
|
|
tx = miner_functions.mint().transact({'from': ursula2})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
2018-12-14 17:11:09 +00:00
|
|
|
log.info("Third/last mining (1 stake) = " + str(miner_functions.mint().estimateGas({'from': ursula3})))
|
|
|
|
tx = miner_functions.mint().transact({'from': ursula3})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
|
|
|
|
2018-12-08 20:23:26 +00:00
|
|
|
log.info("First confirm activity again = " +
|
2019-02-05 12:46:11 +00:00
|
|
|
str(miner_functions.confirmActivity().estimateGas({'from': ursula1})))
|
2018-12-14 17:11:09 +00:00
|
|
|
tx = miner_functions.confirmActivity().transact({'from': ursula1})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
2018-12-08 20:23:26 +00:00
|
|
|
log.info("Second confirm activity again = " +
|
2019-02-05 12:46:11 +00:00
|
|
|
str(miner_functions.confirmActivity().estimateGas({'from': ursula2})))
|
2018-12-14 17:11:09 +00:00
|
|
|
tx = miner_functions.confirmActivity().transact({'from': ursula2})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
2018-12-08 20:23:26 +00:00
|
|
|
log.info("Third confirm activity again = " +
|
2019-02-05 12:46:11 +00:00
|
|
|
str(miner_functions.confirmActivity().estimateGas({'from': ursula3})))
|
2018-12-14 17:11:09 +00:00
|
|
|
tx = miner_functions.confirmActivity().transact({'from': ursula3})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
2018-02-08 09:29:32 +00:00
|
|
|
|
2018-12-14 17:11:09 +00:00
|
|
|
#
|
2018-02-08 09:29:32 +00:00
|
|
|
# Confirm again
|
2018-12-14 17:11:09 +00:00
|
|
|
#
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.time_travel(periods=1)
|
2018-12-08 20:23:26 +00:00
|
|
|
log.info("First confirm activity + mint = " +
|
2019-02-05 12:46:11 +00:00
|
|
|
str(miner_functions.confirmActivity().estimateGas({'from': ursula1})))
|
2018-12-14 17:11:09 +00:00
|
|
|
tx = miner_functions.confirmActivity().transact({'from': ursula1})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
2018-12-08 20:23:26 +00:00
|
|
|
log.info("Second confirm activity + mint = " +
|
2019-02-05 12:46:11 +00:00
|
|
|
str(miner_functions.confirmActivity().estimateGas({'from': ursula2})))
|
2018-12-14 17:11:09 +00:00
|
|
|
tx = miner_functions.confirmActivity().transact({'from': ursula2})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
2018-12-08 20:23:26 +00:00
|
|
|
log.info("Third confirm activity + mint = " +
|
2019-02-05 12:46:11 +00:00
|
|
|
str(miner_functions.confirmActivity().estimateGas({'from': ursula3})))
|
2018-12-14 17:11:09 +00:00
|
|
|
tx = miner_functions.confirmActivity().transact({'from': ursula3})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
2018-02-08 09:29:32 +00:00
|
|
|
|
2018-12-14 17:11:09 +00:00
|
|
|
#
|
2018-02-08 09:29:32 +00:00
|
|
|
# Get locked tokens
|
2018-12-14 17:11:09 +00:00
|
|
|
#
|
|
|
|
log.info("Getting locked tokens = " + str(miner_functions.getLockedTokens(ursula1).estimateGas()))
|
2018-02-08 09:29:32 +00:00
|
|
|
|
2018-12-14 17:11:09 +00:00
|
|
|
#
|
2018-02-08 09:29:32 +00:00
|
|
|
# Wait 1 period and withdraw tokens
|
2018-12-14 17:11:09 +00:00
|
|
|
#
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.time_travel(periods=1)
|
2018-12-14 17:11:09 +00:00
|
|
|
log.info("First withdraw = " + str(miner_functions.withdraw(1).estimateGas({'from': ursula1})))
|
|
|
|
tx = miner_functions.withdraw(1).transact({'from': ursula1})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
2018-12-14 17:11:09 +00:00
|
|
|
log.info("Second withdraw = " + str(miner_functions.withdraw(1).estimateGas({'from': ursula2})))
|
|
|
|
tx = miner_functions.withdraw(1).transact({'from': ursula2})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
2018-12-14 17:11:09 +00:00
|
|
|
log.info("Third withdraw = " + str(miner_functions.withdraw(1).estimateGas({'from': ursula3})))
|
|
|
|
tx = miner_functions.withdraw(1).transact({'from': ursula3})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
2018-02-08 09:29:32 +00:00
|
|
|
|
2018-12-14 17:11:09 +00:00
|
|
|
#
|
2019-03-10 14:33:33 +00:00
|
|
|
# Confirm activity with re-stake
|
2018-12-14 17:11:09 +00:00
|
|
|
#
|
2019-03-10 14:33:33 +00:00
|
|
|
tx = miner_functions.setReStake(True).transact({'from': ursula1})
|
|
|
|
testerchain.wait_for_receipt(tx)
|
|
|
|
tx = miner_functions.setReStake(True).transact({'from': ursula2})
|
|
|
|
testerchain.wait_for_receipt(tx)
|
|
|
|
tx = miner_functions.setReStake(True).transact({'from': ursula3})
|
|
|
|
testerchain.wait_for_receipt(tx)
|
|
|
|
|
|
|
|
log.info("First confirm activity + mint with re-stake = " +
|
|
|
|
str(miner_functions.confirmActivity().estimateGas({'from': ursula1})))
|
|
|
|
tx = miner_functions.confirmActivity().transact({'from': ursula1})
|
|
|
|
testerchain.wait_for_receipt(tx)
|
|
|
|
log.info("Second confirm activity + mint with re-stake = " +
|
|
|
|
str(miner_functions.confirmActivity().estimateGas({'from': ursula2})))
|
|
|
|
tx = miner_functions.confirmActivity().transact({'from': ursula2})
|
|
|
|
testerchain.wait_for_receipt(tx)
|
|
|
|
log.info("Third confirm activity + mint with re-stake = " +
|
|
|
|
str(miner_functions.confirmActivity().estimateGas({'from': ursula3})))
|
|
|
|
tx = miner_functions.confirmActivity().transact({'from': ursula3})
|
|
|
|
testerchain.wait_for_receipt(tx)
|
|
|
|
|
|
|
|
tx = miner_functions.setReStake(False).transact({'from': ursula1})
|
|
|
|
testerchain.wait_for_receipt(tx)
|
|
|
|
tx = miner_functions.setReStake(False).transact({'from': ursula2})
|
|
|
|
testerchain.wait_for_receipt(tx)
|
|
|
|
tx = miner_functions.setReStake(False).transact({'from': ursula3})
|
|
|
|
testerchain.wait_for_receipt(tx)
|
|
|
|
|
|
|
|
#
|
|
|
|
# Wait 2 periods and confirm activity after downtime
|
|
|
|
#
|
|
|
|
testerchain.time_travel(periods=2)
|
2018-12-08 20:23:26 +00:00
|
|
|
log.info("First confirm activity after downtime = " +
|
2019-02-05 12:46:11 +00:00
|
|
|
str(miner_functions.confirmActivity().estimateGas({'from': ursula1})))
|
2018-12-14 17:11:09 +00:00
|
|
|
tx = miner_functions.confirmActivity().transact({'from': ursula1})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
2018-12-08 20:23:26 +00:00
|
|
|
log.info("Second confirm activity after downtime = " +
|
2019-02-05 12:46:11 +00:00
|
|
|
str(miner_functions.confirmActivity().estimateGas({'from': ursula2})))
|
2018-12-14 17:11:09 +00:00
|
|
|
tx = miner_functions.confirmActivity().transact({'from': ursula2})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
2018-12-08 20:23:26 +00:00
|
|
|
log.info("Third confirm activity after downtime = " +
|
2019-02-05 12:46:11 +00:00
|
|
|
str(miner_functions.confirmActivity().estimateGas({'from': ursula3})))
|
2018-12-14 17:11:09 +00:00
|
|
|
tx = miner_functions.confirmActivity().transact({'from': ursula3})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
2018-02-08 09:29:32 +00:00
|
|
|
|
2018-12-14 17:11:09 +00:00
|
|
|
#
|
2018-02-08 09:29:32 +00:00
|
|
|
# Ursula and Alice deposit some tokens to the escrow again
|
2018-12-14 17:11:09 +00:00
|
|
|
#
|
2018-12-08 20:23:26 +00:00
|
|
|
log.info("First deposit tokens again = " +
|
2019-02-05 12:46:11 +00:00
|
|
|
str(miner_functions.deposit(MIN_ALLOWED_LOCKED * 2, MIN_LOCKED_PERIODS).estimateGas({'from': ursula1})))
|
2018-12-14 17:11:09 +00:00
|
|
|
tx = miner_functions.deposit(MIN_ALLOWED_LOCKED * 2, MIN_LOCKED_PERIODS).transact({'from': ursula1})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
2018-12-08 20:23:26 +00:00
|
|
|
log.info("Second deposit tokens again = " +
|
2019-02-05 12:46:11 +00:00
|
|
|
str(miner_functions.deposit(MIN_ALLOWED_LOCKED * 2, MIN_LOCKED_PERIODS).estimateGas({'from': ursula2})))
|
2018-12-14 17:11:09 +00:00
|
|
|
tx = miner_functions.deposit(MIN_ALLOWED_LOCKED * 2, MIN_LOCKED_PERIODS).transact({'from': ursula2})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
2018-12-08 20:23:26 +00:00
|
|
|
log.info("Third deposit tokens again = " +
|
2019-02-05 12:46:11 +00:00
|
|
|
str(miner_functions.deposit(MIN_ALLOWED_LOCKED * 2, MIN_LOCKED_PERIODS).estimateGas({'from': ursula3})))
|
2018-12-14 17:11:09 +00:00
|
|
|
tx = miner_functions.deposit(MIN_ALLOWED_LOCKED * 2, MIN_LOCKED_PERIODS).transact({'from': ursula3})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
2018-02-08 09:29:32 +00:00
|
|
|
|
2018-12-14 17:11:09 +00:00
|
|
|
#
|
2018-02-08 09:29:32 +00:00
|
|
|
# Wait 1 period and mint tokens
|
2018-12-14 17:11:09 +00:00
|
|
|
#
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.time_travel(periods=1)
|
2018-12-14 17:11:09 +00:00
|
|
|
log.info("First mining again = " + str(miner_functions.mint().estimateGas({'from': ursula1})))
|
|
|
|
tx = miner_functions.mint().transact({'from': ursula1})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
2018-12-14 17:11:09 +00:00
|
|
|
log.info("Second mining again = " + str(miner_functions.mint().estimateGas({'from': ursula2})))
|
|
|
|
tx = miner_functions.mint().transact({'from': ursula2})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
2018-12-14 17:11:09 +00:00
|
|
|
log.info("Third/last mining again = " + str(miner_functions.mint().estimateGas({'from': ursula3})))
|
|
|
|
tx = miner_functions.mint().transact({'from': ursula3})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
2018-02-08 09:29:32 +00:00
|
|
|
|
2018-12-14 17:11:09 +00:00
|
|
|
#
|
2018-03-10 13:04:11 +00:00
|
|
|
# Create policy
|
2018-12-14 17:11:09 +00:00
|
|
|
#
|
2019-03-20 04:22:23 +00:00
|
|
|
policy_id_1 = os.urandom(int(Policy.POLICY_ID_LENGTH))
|
|
|
|
policy_id_2 = os.urandom(int(Policy.POLICY_ID_LENGTH))
|
2018-03-10 13:04:11 +00:00
|
|
|
number_of_periods = 10
|
2018-12-08 20:23:26 +00:00
|
|
|
log.info("First creating policy (1 node, 10 periods) = " +
|
2019-02-05 12:46:11 +00:00
|
|
|
str(policy_functions.createPolicy(policy_id_1, number_of_periods, 0, [ursula1]).estimateGas(
|
|
|
|
{'from': alice1, 'value': 10000})))
|
|
|
|
tx = policy_functions.createPolicy(policy_id_1, number_of_periods, 0, [ursula1]).transact(
|
|
|
|
{'from': alice1, 'value': 10000})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
2018-12-08 20:23:26 +00:00
|
|
|
log.info("Second creating policy (1 node, 10 periods) = " +
|
2019-02-05 12:46:11 +00:00
|
|
|
str(policy_functions.createPolicy(policy_id_2, number_of_periods, 0, [ursula1]).estimateGas(
|
|
|
|
{'from': alice1, 'value': 10000})))
|
|
|
|
tx = policy_functions.createPolicy(policy_id_2, number_of_periods, 0, [ursula1]).transact(
|
|
|
|
{'from': alice1, 'value': 10000})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
2018-03-10 13:04:11 +00:00
|
|
|
|
2018-12-14 17:11:09 +00:00
|
|
|
#
|
2018-03-10 13:04:11 +00:00
|
|
|
# Revoke policy
|
2018-12-14 17:11:09 +00:00
|
|
|
#
|
|
|
|
log.info("Revoking policy = " + str(policy_functions.revokePolicy(policy_id_1).estimateGas({'from': alice1})))
|
|
|
|
tx = policy_functions.revokePolicy(policy_id_1).transact({'from': alice1})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
2018-12-14 17:11:09 +00:00
|
|
|
tx = policy_functions.revokePolicy(policy_id_2).transact({'from': alice1})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
2018-03-10 13:04:11 +00:00
|
|
|
|
2018-12-14 17:11:09 +00:00
|
|
|
#
|
2018-03-10 13:04:11 +00:00
|
|
|
# Create policy with more periods
|
2018-12-14 17:11:09 +00:00
|
|
|
#
|
2019-03-20 04:22:23 +00:00
|
|
|
policy_id_1 = os.urandom(int(Policy.POLICY_ID_LENGTH))
|
|
|
|
policy_id_2 = os.urandom(int(Policy.POLICY_ID_LENGTH))
|
|
|
|
policy_id_3 = os.urandom(int(Policy.POLICY_ID_LENGTH))
|
2018-03-10 13:04:11 +00:00
|
|
|
number_of_periods = 100
|
2018-12-08 20:23:26 +00:00
|
|
|
log.info("First creating policy (1 node, " + str(number_of_periods) + " periods, first reward) = " +
|
2019-02-05 12:46:11 +00:00
|
|
|
str(policy_functions.createPolicy(policy_id_1, number_of_periods, 50, [ursula2]).estimateGas(
|
|
|
|
{'from': alice1, 'value': 10050})))
|
|
|
|
tx = policy_functions.createPolicy(policy_id_1, number_of_periods, 50, [ursula2]).transact(
|
|
|
|
{'from': alice1, 'value': 10050})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
|
|
|
testerchain.time_travel(periods=1)
|
2018-12-08 20:23:26 +00:00
|
|
|
log.info("Second creating policy (1 node, " + str(number_of_periods) + " periods, first reward) = " +
|
2019-02-05 12:46:11 +00:00
|
|
|
str(policy_functions.createPolicy(policy_id_2, number_of_periods, 50, [ursula2]).estimateGas(
|
|
|
|
{'from': alice1, 'value': 10050})))
|
|
|
|
tx = policy_functions.createPolicy(policy_id_2, number_of_periods, 50, [ursula2]).transact(
|
|
|
|
{'from': alice1, 'value': 10050})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
2018-12-08 20:23:26 +00:00
|
|
|
log.info("Third creating policy (1 node, " + str(number_of_periods) + " periods, first reward) = " +
|
2019-02-05 12:46:11 +00:00
|
|
|
str(policy_functions.createPolicy(policy_id_3, number_of_periods, 50, [ursula1]).estimateGas(
|
|
|
|
{'from': alice1, 'value': 10050})))
|
|
|
|
tx = policy_functions.createPolicy(policy_id_3, number_of_periods, 50, [ursula1]).transact(
|
|
|
|
{'from': alice1, 'value': 10050})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
2018-03-10 13:04:11 +00:00
|
|
|
|
2018-12-14 17:11:09 +00:00
|
|
|
#
|
2018-03-10 13:04:11 +00:00
|
|
|
# Mine and revoke policy
|
2018-12-14 17:11:09 +00:00
|
|
|
#
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.time_travel(periods=10)
|
2018-12-14 17:11:09 +00:00
|
|
|
tx = miner_functions.confirmActivity().transact({'from': ursula2})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
2018-12-14 17:11:09 +00:00
|
|
|
tx = miner_functions.confirmActivity().transact({'from': ursula1})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
|
|
|
|
|
|
|
testerchain.time_travel(periods=1)
|
2018-12-14 17:11:09 +00:00
|
|
|
log.info("First mining after downtime = " + str(miner_functions.mint().estimateGas({'from': ursula1})))
|
|
|
|
tx = miner_functions.mint().transact({'from': ursula1})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
2018-12-14 17:11:09 +00:00
|
|
|
log.info("Second mining after downtime = " + str(miner_functions.mint().estimateGas({'from': ursula2})))
|
|
|
|
tx = miner_functions.mint().transact({'from': ursula2})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
|
|
|
|
|
|
|
testerchain.time_travel(periods=10)
|
2018-12-08 20:23:26 +00:00
|
|
|
log.info("First revoking policy after downtime = " +
|
2019-02-05 12:46:11 +00:00
|
|
|
str(policy_functions.revokePolicy(policy_id_1).estimateGas({'from': alice1})))
|
2018-12-14 17:11:09 +00:00
|
|
|
tx = policy_functions.revokePolicy(policy_id_1).transact({'from': alice1})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
2018-12-08 20:23:26 +00:00
|
|
|
log.info("Second revoking policy after downtime = " +
|
2019-02-05 12:46:11 +00:00
|
|
|
str(policy_functions.revokePolicy(policy_id_2).estimateGas({'from': alice1})))
|
2018-12-14 17:11:09 +00:00
|
|
|
tx = policy_functions.revokePolicy(policy_id_2).transact({'from': alice1})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
2018-12-08 20:23:26 +00:00
|
|
|
log.info("Second revoking policy after downtime = " +
|
2019-02-05 12:46:11 +00:00
|
|
|
str(policy_functions.revokePolicy(policy_id_3).estimateGas({'from': alice1})))
|
2018-12-14 17:11:09 +00:00
|
|
|
tx = policy_functions.revokePolicy(policy_id_3).transact({'from': alice1})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
2018-03-16 18:44:20 +00:00
|
|
|
|
2018-12-14 17:11:09 +00:00
|
|
|
#
|
2018-03-16 18:44:20 +00:00
|
|
|
# Create policy with multiple nodes
|
2018-12-14 17:11:09 +00:00
|
|
|
#
|
2019-03-20 04:22:23 +00:00
|
|
|
policy_id_1 = os.urandom(int(Policy.POLICY_ID_LENGTH))
|
|
|
|
policy_id_2 = os.urandom(int(Policy.POLICY_ID_LENGTH))
|
|
|
|
policy_id_3 = os.urandom(int(Policy.POLICY_ID_LENGTH))
|
2018-03-16 18:44:20 +00:00
|
|
|
number_of_periods = 100
|
2018-12-08 20:23:26 +00:00
|
|
|
log.info("First creating policy (3 nodes, 100 periods, first reward) = " +
|
2018-12-14 17:11:09 +00:00
|
|
|
str(policy_functions
|
|
|
|
.createPolicy(policy_id_1, number_of_periods, 50, [ursula1, ursula2, ursula3])
|
|
|
|
.estimateGas({'from': alice1, 'value': 30150})))
|
2019-02-05 12:46:11 +00:00
|
|
|
tx = policy_functions.createPolicy(policy_id_1, number_of_periods, 50, [ursula1, ursula2, ursula3]).transact(
|
|
|
|
{'from': alice1, 'value': 30150})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
2018-12-08 20:23:26 +00:00
|
|
|
log.info("Second creating policy (3 nodes, 100 periods, first reward) = " +
|
2018-12-14 17:11:09 +00:00
|
|
|
str(policy_functions
|
|
|
|
.createPolicy(policy_id_2, number_of_periods, 50, [ursula1, ursula2, ursula3])
|
|
|
|
.estimateGas({'from': alice1, 'value': 30150})))
|
2019-02-05 12:46:11 +00:00
|
|
|
tx = policy_functions.createPolicy(policy_id_2, number_of_periods, 50, [ursula1, ursula2, ursula3]).transact(
|
|
|
|
{'from': alice1, 'value': 30150})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
2018-12-08 20:23:26 +00:00
|
|
|
log.info("Third creating policy (2 nodes, 100 periods, first reward) = " +
|
2019-02-05 12:46:11 +00:00
|
|
|
str(policy_functions.createPolicy(policy_id_3, number_of_periods, 50, [ursula1, ursula2]).estimateGas(
|
|
|
|
{'from': alice1, 'value': 20100})))
|
|
|
|
tx = policy_functions.createPolicy(policy_id_3, number_of_periods, 50, [ursula1, ursula2]).transact(
|
|
|
|
{'from': alice1, 'value': 20100})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
|
|
|
|
|
|
|
for index in range(5):
|
2018-12-14 17:11:09 +00:00
|
|
|
tx = miner_functions.confirmActivity().transact({'from': ursula1})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
2018-12-14 17:11:09 +00:00
|
|
|
tx = miner_functions.confirmActivity().transact({'from': ursula2})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
2018-12-14 17:11:09 +00:00
|
|
|
tx = miner_functions.confirmActivity().transact({'from': ursula3})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
|
|
|
testerchain.time_travel(periods=1)
|
|
|
|
|
2018-12-14 17:11:09 +00:00
|
|
|
tx = miner_functions.mint().transact({'from': ursula1})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
2018-12-14 17:11:09 +00:00
|
|
|
tx = miner_functions.mint().transact({'from': ursula2})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
2018-12-14 17:11:09 +00:00
|
|
|
tx = miner_functions.mint().transact({'from': ursula3})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
|
|
|
|
2018-12-14 17:11:09 +00:00
|
|
|
#
|
2018-06-15 05:51:28 +00:00
|
|
|
# Check regular deposit
|
2018-12-14 17:11:09 +00:00
|
|
|
#
|
2019-02-05 12:46:11 +00:00
|
|
|
log.info("First deposit tokens = " + str(
|
|
|
|
miner_functions.deposit(MIN_ALLOWED_LOCKED, MIN_LOCKED_PERIODS).estimateGas({'from': ursula1})))
|
2018-12-14 17:11:09 +00:00
|
|
|
tx = miner_functions.deposit(MIN_ALLOWED_LOCKED, MIN_LOCKED_PERIODS).transact({'from': ursula1})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
2019-02-05 12:46:11 +00:00
|
|
|
log.info("Second deposit tokens = " + str(
|
|
|
|
miner_functions.deposit(MIN_ALLOWED_LOCKED, MIN_LOCKED_PERIODS).estimateGas({'from': ursula2})))
|
2018-12-14 17:11:09 +00:00
|
|
|
tx = miner_functions.deposit(MIN_ALLOWED_LOCKED, MIN_LOCKED_PERIODS).transact({'from': ursula2})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
2019-02-05 12:46:11 +00:00
|
|
|
log.info("Third deposit tokens = " + str(
|
|
|
|
miner_functions.deposit(MIN_ALLOWED_LOCKED, MIN_LOCKED_PERIODS).estimateGas({'from': ursula3})))
|
2018-12-14 17:11:09 +00:00
|
|
|
tx = miner_functions.deposit(MIN_ALLOWED_LOCKED, MIN_LOCKED_PERIODS).transact({'from': ursula3})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
|
|
|
|
2018-12-14 17:11:09 +00:00
|
|
|
#
|
2018-06-15 05:51:28 +00:00
|
|
|
# ApproveAndCall
|
2018-12-14 17:11:09 +00:00
|
|
|
#
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.time_travel(periods=1)
|
|
|
|
|
2018-12-14 17:11:09 +00:00
|
|
|
tx = miner_functions.mint().transact({'from': ursula1})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
2018-12-14 17:11:09 +00:00
|
|
|
tx = miner_functions.mint().transact({'from': ursula2})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
2018-12-14 17:11:09 +00:00
|
|
|
tx = miner_functions.mint().transact({'from': ursula3})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
|
|
|
|
2018-12-08 20:23:26 +00:00
|
|
|
log.info("First approveAndCall = " +
|
2018-12-14 17:11:09 +00:00
|
|
|
str(token_functions.approveAndCall(miner_agent.contract_address,
|
|
|
|
MIN_ALLOWED_LOCKED * 2,
|
|
|
|
web3.toBytes(MIN_LOCKED_PERIODS)).estimateGas({'from': ursula1})))
|
|
|
|
tx = token_functions.approveAndCall(miner_agent.contract_address,
|
|
|
|
MIN_ALLOWED_LOCKED * 2,
|
|
|
|
web3.toBytes(MIN_LOCKED_PERIODS)).transact({'from': ursula1})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
2018-12-08 20:23:26 +00:00
|
|
|
log.info("Second approveAndCall = " +
|
2018-12-14 17:11:09 +00:00
|
|
|
str(token_functions.approveAndCall(miner_agent.contract_address, MIN_ALLOWED_LOCKED * 2,
|
|
|
|
web3.toBytes(MIN_LOCKED_PERIODS)).estimateGas({'from': ursula2})))
|
|
|
|
tx = token_functions.approveAndCall(miner_agent.contract_address,
|
|
|
|
MIN_ALLOWED_LOCKED * 2,
|
|
|
|
web3.toBytes(MIN_LOCKED_PERIODS)).transact({'from': ursula2})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
2018-12-08 20:23:26 +00:00
|
|
|
log.info("Third approveAndCall = " +
|
2018-12-14 17:11:09 +00:00
|
|
|
str(token_functions.approveAndCall(miner_agent.contract_address,
|
|
|
|
MIN_ALLOWED_LOCKED * 2,
|
|
|
|
web3.toBytes(MIN_LOCKED_PERIODS)).estimateGas({'from': ursula3})))
|
|
|
|
tx = token_functions.approveAndCall(miner_agent.contract_address,
|
|
|
|
MIN_ALLOWED_LOCKED * 2,
|
|
|
|
web3.toBytes(MIN_LOCKED_PERIODS)).transact({'from': ursula3})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
|
|
|
|
2018-12-14 17:11:09 +00:00
|
|
|
#
|
2018-06-15 05:51:28 +00:00
|
|
|
# Locking tokens
|
2018-12-14 17:11:09 +00:00
|
|
|
#
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.time_travel(periods=1)
|
|
|
|
|
2018-12-14 17:11:09 +00:00
|
|
|
tx = miner_functions.confirmActivity().transact({'from': ursula1})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
2018-12-14 17:11:09 +00:00
|
|
|
tx = miner_functions.confirmActivity().transact({'from': ursula2})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
2018-12-14 17:11:09 +00:00
|
|
|
tx = miner_functions.confirmActivity().transact({'from': ursula3})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
|
|
|
|
2018-12-08 20:23:26 +00:00
|
|
|
log.info("First locking tokens = " +
|
2019-02-05 12:46:11 +00:00
|
|
|
str(miner_functions.lock(MIN_ALLOWED_LOCKED, MIN_LOCKED_PERIODS).estimateGas({'from': ursula1})))
|
2018-12-14 17:11:09 +00:00
|
|
|
tx = miner_functions.lock(MIN_ALLOWED_LOCKED, MIN_LOCKED_PERIODS).transact({'from': ursula1})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
2018-12-08 20:23:26 +00:00
|
|
|
log.info("Second locking tokens = " +
|
2019-02-05 12:46:11 +00:00
|
|
|
str(miner_functions.lock(MIN_ALLOWED_LOCKED, MIN_LOCKED_PERIODS).estimateGas({'from': ursula2})))
|
|
|
|
tx = miner_functions.lock(MIN_ALLOWED_LOCKED, MIN_LOCKED_PERIODS).transact({'from': ursula2})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
2018-12-08 20:23:26 +00:00
|
|
|
log.info("Third locking tokens = " +
|
2019-02-05 12:46:11 +00:00
|
|
|
str(miner_functions.lock(MIN_ALLOWED_LOCKED, MIN_LOCKED_PERIODS).estimateGas({'from': ursula3})))
|
2018-12-14 17:11:09 +00:00
|
|
|
tx = miner_functions.lock(MIN_ALLOWED_LOCKED, MIN_LOCKED_PERIODS).transact({'from': ursula3})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
|
|
|
|
2018-12-14 17:11:09 +00:00
|
|
|
#
|
2018-06-15 05:51:28 +00:00
|
|
|
# Divide stake
|
2018-12-14 17:11:09 +00:00
|
|
|
#
|
2019-02-05 12:46:11 +00:00
|
|
|
log.info("First divide stake = " + str(
|
|
|
|
miner_functions.divideStake(1, MIN_ALLOWED_LOCKED, 2).estimateGas({'from': ursula1})))
|
2018-12-14 17:11:09 +00:00
|
|
|
tx = miner_functions.divideStake(1, MIN_ALLOWED_LOCKED, 2).transact({'from': ursula1})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
2019-02-05 12:46:11 +00:00
|
|
|
log.info("Second divide stake = " + str(
|
|
|
|
miner_functions.divideStake(3, MIN_ALLOWED_LOCKED, 2).estimateGas({'from': ursula1})))
|
2018-12-14 17:11:09 +00:00
|
|
|
tx = miner_functions.divideStake(3, MIN_ALLOWED_LOCKED, 2).transact({'from': ursula1})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
|
|
|
|
2018-12-14 17:11:09 +00:00
|
|
|
#
|
2018-06-15 05:51:28 +00:00
|
|
|
# Divide almost finished stake
|
2018-12-14 17:11:09 +00:00
|
|
|
#
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.time_travel(periods=1)
|
2018-12-14 17:11:09 +00:00
|
|
|
tx = miner_functions.confirmActivity().transact({'from': ursula1})
|
2018-06-15 05:51:28 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
|
|
|
testerchain.time_travel(periods=1)
|
2019-02-05 12:46:11 +00:00
|
|
|
log.info("Divide stake (next period is not confirmed) = " + str(
|
|
|
|
miner_functions.divideStake(0, MIN_ALLOWED_LOCKED, 2).estimateGas({'from': ursula1})))
|
2018-12-14 17:11:09 +00:00
|
|
|
tx = miner_functions.confirmActivity().transact({'from': ursula1})
|
|
|
|
testerchain.wait_for_receipt(tx)
|
2019-02-05 12:46:11 +00:00
|
|
|
log.info("Divide stake (next period is confirmed) = " + str(
|
|
|
|
miner_functions.divideStake(0, MIN_ALLOWED_LOCKED, 2).estimateGas({'from': ursula1})))
|
2018-12-14 18:10:08 +00:00
|
|
|
|
2019-03-10 14:33:33 +00:00
|
|
|
#
|
2018-12-21 13:59:38 +00:00
|
|
|
# Slashing tests
|
2019-03-10 14:33:33 +00:00
|
|
|
#
|
2018-12-21 13:59:38 +00:00
|
|
|
tx = miner_functions.confirmActivity().transact({'from': ursula1})
|
|
|
|
testerchain.wait_for_receipt(tx)
|
|
|
|
testerchain.time_travel(periods=1)
|
2019-03-10 14:33:33 +00:00
|
|
|
# Deploy adjudicator to estimate slashing method in MinersEscrow contract
|
2018-12-21 13:59:38 +00:00
|
|
|
adjudicator, _ = testerchain.interface.deploy_contract(
|
2019-02-05 12:46:11 +00:00
|
|
|
'MiningAdjudicator', miner_agent.contract.address, ALGORITHM_SHA256, MIN_ALLOWED_LOCKED - 1, 0, 2, 2
|
2018-12-21 13:59:38 +00:00
|
|
|
)
|
|
|
|
tx = miner_functions.setMiningAdjudicator(adjudicator.address).transact()
|
|
|
|
testerchain.wait_for_receipt(tx)
|
|
|
|
adjudicator_functions = adjudicator.functions
|
|
|
|
|
2019-03-10 14:33:33 +00:00
|
|
|
#
|
2018-12-21 13:59:38 +00:00
|
|
|
# Slashing
|
2019-03-10 14:33:33 +00:00
|
|
|
#
|
2019-02-05 12:46:11 +00:00
|
|
|
slashing_args = generate_args_for_slashing(testerchain, ursula1)
|
|
|
|
log.info("Slash just value = " + str(
|
|
|
|
adjudicator_functions.evaluateCFrag(*slashing_args).estimateGas({'from': alice1})))
|
|
|
|
tx = adjudicator_functions.evaluateCFrag(*slashing_args).transact({'from': alice1})
|
2018-12-21 13:59:38 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
|
|
|
|
|
|
|
deposit = miner_functions.minerInfo(ursula1).call()[0]
|
|
|
|
unlocked = deposit - miner_functions.getLockedTokens(ursula1).call()
|
|
|
|
tx = miner_functions.withdraw(unlocked).transact({'from': ursula1})
|
|
|
|
testerchain.wait_for_receipt(tx)
|
|
|
|
|
2019-02-24 09:48:30 +00:00
|
|
|
sub_stakes_length = str(miner_functions.getSubStakesLength(ursula1).call())
|
2019-02-05 12:46:11 +00:00
|
|
|
slashing_args = generate_args_for_slashing(testerchain, ursula1)
|
2019-02-24 09:48:30 +00:00
|
|
|
log.info("First slashing one sub stake and saving old one (" + sub_stakes_length + " sub stakes) = " +
|
2019-02-05 12:46:11 +00:00
|
|
|
str(adjudicator_functions.evaluateCFrag(*slashing_args).estimateGas({'from': alice1})))
|
|
|
|
tx = adjudicator_functions.evaluateCFrag(*slashing_args).transact({'from': alice1})
|
2018-12-21 13:59:38 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
2019-02-05 12:46:11 +00:00
|
|
|
|
2019-02-24 09:48:30 +00:00
|
|
|
sub_stakes_length = str(miner_functions.getSubStakesLength(ursula1).call())
|
2019-02-05 12:46:11 +00:00
|
|
|
slashing_args = generate_args_for_slashing(testerchain, ursula1)
|
2019-02-24 09:48:30 +00:00
|
|
|
log.info("Second slashing one sub stake and saving old one (" + sub_stakes_length + " sub stakes) = " +
|
2019-02-05 12:46:11 +00:00
|
|
|
str(adjudicator_functions.evaluateCFrag(*slashing_args).estimateGas({'from': alice1})))
|
|
|
|
tx = adjudicator_functions.evaluateCFrag(*slashing_args).transact({'from': alice1})
|
2018-12-21 13:59:38 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
2019-02-05 12:46:11 +00:00
|
|
|
|
2019-02-24 09:48:30 +00:00
|
|
|
sub_stakes_length = str(miner_functions.getSubStakesLength(ursula1).call())
|
2019-02-05 12:46:11 +00:00
|
|
|
slashing_args = generate_args_for_slashing(testerchain, ursula1)
|
2019-02-24 09:48:30 +00:00
|
|
|
log.info("Third slashing one sub stake and saving old one (" + sub_stakes_length + " sub stakes) = " +
|
2019-02-05 12:46:11 +00:00
|
|
|
str(adjudicator_functions.evaluateCFrag(*slashing_args).estimateGas({'from': alice1})))
|
|
|
|
tx = adjudicator_functions.evaluateCFrag(*slashing_args).transact({'from': alice1})
|
2018-12-21 13:59:38 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
|
|
|
|
2019-02-24 09:48:30 +00:00
|
|
|
sub_stakes_length = str(miner_functions.getSubStakesLength(ursula1).call())
|
2019-02-05 12:46:11 +00:00
|
|
|
slashing_args = generate_args_for_slashing(testerchain, ursula1)
|
2019-02-24 09:48:30 +00:00
|
|
|
log.info("Slashing two sub stakes and saving old one (" + sub_stakes_length + " sub stakes) = " +
|
2019-02-05 12:46:11 +00:00
|
|
|
str(adjudicator_functions.evaluateCFrag(*slashing_args).estimateGas({'from': alice1})))
|
|
|
|
tx = adjudicator_functions.evaluateCFrag(*slashing_args).transact({'from': alice1})
|
2018-12-21 13:59:38 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
|
|
|
|
|
|
|
for index in range(18):
|
|
|
|
tx = miner_functions.confirmActivity().transact({'from': ursula1})
|
|
|
|
testerchain.wait_for_receipt(tx)
|
|
|
|
testerchain.time_travel(periods=1)
|
|
|
|
|
|
|
|
tx = miner_functions.lock(MIN_ALLOWED_LOCKED, MIN_LOCKED_PERIODS).transact({'from': ursula1})
|
|
|
|
testerchain.wait_for_receipt(tx)
|
|
|
|
deposit = miner_functions.minerInfo(ursula1).call()[0]
|
|
|
|
unlocked = deposit - miner_functions.getLockedTokens(ursula1, 1).call()
|
|
|
|
tx = miner_functions.withdraw(unlocked).transact({'from': ursula1})
|
|
|
|
testerchain.wait_for_receipt(tx)
|
|
|
|
|
2019-02-24 09:48:30 +00:00
|
|
|
sub_stakes_length = str(miner_functions.getSubStakesLength(ursula1).call())
|
2019-02-05 12:46:11 +00:00
|
|
|
slashing_args = generate_args_for_slashing(testerchain, ursula1)
|
2019-02-24 09:48:30 +00:00
|
|
|
log.info("Slashing two sub stakes, shortest and new one (" + sub_stakes_length + " sub stakes) = " +
|
2019-02-05 12:46:11 +00:00
|
|
|
str(adjudicator_functions.evaluateCFrag(*slashing_args).estimateGas({'from': alice1})))
|
|
|
|
tx = adjudicator_functions.evaluateCFrag(*slashing_args).transact({'from': alice1})
|
2018-12-21 13:59:38 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
|
|
|
|
2019-02-24 09:48:30 +00:00
|
|
|
sub_stakes_length = str(miner_functions.getSubStakesLength(ursula1).call())
|
2019-02-05 12:46:11 +00:00
|
|
|
slashing_args = generate_args_for_slashing(testerchain, ursula1)
|
2019-02-24 09:48:30 +00:00
|
|
|
log.info("Slashing three sub stakes, two shortest and new one (" + sub_stakes_length + " sub stakes) = " +
|
2019-02-05 12:46:11 +00:00
|
|
|
str(adjudicator_functions.evaluateCFrag(*slashing_args).estimateGas({'from': alice1})))
|
|
|
|
tx = adjudicator_functions.evaluateCFrag(*slashing_args).transact({'from': alice1})
|
2018-12-21 13:59:38 +00:00
|
|
|
testerchain.wait_for_receipt(tx)
|
|
|
|
|
2019-02-05 12:46:11 +00:00
|
|
|
slashing_args = generate_args_for_slashing(testerchain, ursula1, corrupt=False)
|
|
|
|
log.info("Evaluating correct CFrag = " +
|
|
|
|
str(adjudicator_functions.evaluateCFrag(*slashing_args).estimateGas({'from': alice1})))
|
|
|
|
tx = adjudicator_functions.evaluateCFrag(*slashing_args).transact({'from': alice1})
|
|
|
|
testerchain.wait_for_receipt(tx)
|
2018-12-21 13:59:38 +00:00
|
|
|
|
2018-12-08 20:23:26 +00:00
|
|
|
print("********* All Done! *********")
|
2018-01-27 13:05:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2018-12-08 20:23:26 +00:00
|
|
|
print("Starting Up...")
|
2018-12-13 22:56:16 +00:00
|
|
|
analyzer = AnalyzeGas()
|
2018-12-14 18:10:08 +00:00
|
|
|
estimate_gas(analyzer=analyzer)
|
2018-12-13 22:56:16 +00:00
|
|
|
analyzer.to_json_file()
|