Slashing economics fixture

pull/947/head
David Núñez 2019-04-25 15:13:46 +02:00
parent 77f2a5ab1b
commit 361c068aad
3 changed files with 26 additions and 28 deletions

View File

@ -15,18 +15,14 @@ You should have received a copy of the GNU Affero General Public License
along with nucypher. If not, see <https://www.gnu.org/licenses/>.
"""
import os
import pytest
from web3.contract import Contract
from nucypher.blockchain.eth.deployers import DispatcherDeployer
ALGORITHM_SHA256 = 1
BASE_PENALTY = 100
PENALTY_HISTORY_COEFFICIENT = 10
PERCENTAGE_PENALTY_COEFFICIENT = 8
REWARD_COEFFICIENT = 2
secret = (123456).to_bytes(32, byteorder='big')
secret = os.urandom(DispatcherDeployer.DISPATCHER_SECRET_LENGTH)
@pytest.fixture()
@ -36,15 +32,15 @@ def escrow(testerchain):
@pytest.fixture(params=[False, True])
def adjudicator_contract(testerchain, escrow, request):
def adjudicator_contract(testerchain, escrow, request, slashing_economics):
contract, _ = testerchain.interface.deploy_contract(
'MiningAdjudicator',
escrow.address,
ALGORITHM_SHA256,
BASE_PENALTY,
PENALTY_HISTORY_COEFFICIENT,
PERCENTAGE_PENALTY_COEFFICIENT,
REWARD_COEFFICIENT)
slashing_economics.algorithm_sha256,
slashing_economics.base_penalty,
slashing_economics.penalty_history_coefficient,
slashing_economics.percentage_penalty_coefficient,
slashing_economics.reward_coefficient)
if request.param:
secret_hash = testerchain.interface.w3.keccak(secret)

View File

@ -41,10 +41,6 @@ from nucypher.policy.models import IndisputableEvidence
ALGORITHM_KECCAK256 = 0
ALGORITHM_SHA256 = 1
BASE_PENALTY = 100
PENALTY_HISTORY_COEFFICIENT = 10
PERCENTAGE_PENALTY_COEFFICIENT = 8
REWARD_COEFFICIENT = 2
secret = (123456).to_bytes(32, byteorder='big')
secret2 = (654321).to_bytes(32, byteorder='big')
@ -94,15 +90,8 @@ def fragments(metadata):
return capsule, cfrag
def compute_penalty_and_reward(stake: int, penalty_history: int) -> Tuple[int, int]:
penalty = BASE_PENALTY + PENALTY_HISTORY_COEFFICIENT * penalty_history
penalty = min(penalty, stake // PERCENTAGE_PENALTY_COEFFICIENT)
reward = penalty // REWARD_COEFFICIENT
return penalty, reward
@pytest.mark.slow
def test_evaluate_cfrag(testerchain, escrow, adjudicator_contract):
def test_evaluate_cfrag(testerchain, escrow, adjudicator_contract, slashing_economics):
creator, miner, wrong_miner, investigator, *everyone_else = testerchain.interface.w3.eth.accounts
evaluation_log = adjudicator_contract.events.CFragEvaluated.createFilter(fromBlock='latest')
@ -111,6 +100,13 @@ def test_evaluate_cfrag(testerchain, escrow, adjudicator_contract):
investigator_balance = 0
number_of_evaluations = 0
def compute_penalty_and_reward(stake: int, penalty_history: int) -> Tuple[int, int]:
penalty_ = slashing_economics.base_penalty
penalty_ += slashing_economics.penalty_history_coefficient * penalty_history
penalty_ = min(penalty_, stake // slashing_economics.percentage_penalty_coefficient)
reward_ = penalty_ // slashing_economics.reward_coefficient
return penalty_, reward_
# Prepare one miner
tx = escrow.functions.setMinerInfo(miner, worker_stake).transact()
testerchain.wait_for_receipt(tx)
@ -344,7 +340,7 @@ def test_evaluate_cfrag(testerchain, escrow, adjudicator_contract):
previous_penalty = penalty
penalty, reward = compute_penalty_and_reward(worker_stake, worker_penalty_history)
# Penalty was increased because it's the second violation
assert penalty == previous_penalty + PENALTY_HISTORY_COEFFICIENT
assert penalty == previous_penalty + slashing_economics.penalty_history_coefficient
worker_stake -= penalty
investigator_balance += reward
worker_penalty_history += 1
@ -399,7 +395,7 @@ def test_evaluate_cfrag(testerchain, escrow, adjudicator_contract):
penalty, reward = compute_penalty_and_reward(worker_stake, worker_penalty_history)
# Penalty has reached maximum available percentage of value
assert penalty == worker_stake // PERCENTAGE_PENALTY_COEFFICIENT
assert penalty == worker_stake // slashing_economics.percentage_penalty_coefficient
worker_stake -= penalty
investigator_balance += reward
worker_penalty_history += 1

View File

@ -24,7 +24,7 @@ import pytest
from constant_sorrow.constants import NON_PAYMENT
from sqlalchemy.engine import create_engine
from nucypher.blockchain.economics import TokenEconomics
from nucypher.blockchain.economics import TokenEconomics, SlashingEconomics
from nucypher.blockchain.eth.deployers import NucypherTokenDeployer, MinerEscrowDeployer, PolicyManagerDeployer, \
DispatcherDeployer
from nucypher.blockchain.eth.interfaces import BlockchainDeployerInterface
@ -321,6 +321,12 @@ def token_economics():
economics = TokenEconomics()
return economics
@pytest.fixture(scope='session')
def slashing_economics():
economics = SlashingEconomics()
return economics
@pytest.fixture(scope='session')
def solidity_compiler():
"""Doing this more than once per session will result in slower test run times."""