Added minimum reward rate for nodes in the PolicyManager

pull/271/head
szotov 2018-05-08 15:27:10 +03:00
parent b685d93c31
commit 33ff3d0955
2 changed files with 38 additions and 6 deletions

View File

@ -73,6 +73,7 @@ contract PolicyManager is Upgradeable {
uint256 rewardRate;
uint256 lastMinedPeriod;
mapping (uint256 => int256) rewardDelta;
uint256 minRewardRate;
}
bytes20 constant RESERVED_POLICY_ID = bytes20(0);
@ -131,6 +132,7 @@ contract PolicyManager is Upgradeable {
address node = _nodes[i];
require(escrow.getLockedTokens(node) != 0 && node != RESERVED_NODE);
NodeInfo storage nodeInfo = nodes[node];
require(policy.rewardRate >= nodeInfo.minRewardRate);
nodeInfo.rewardDelta[currentPeriod] = nodeInfo.rewardDelta[currentPeriod].add(_firstReward);
nodeInfo.rewardDelta[policy.startPeriod] = nodeInfo.rewardDelta[policy.startPeriod]
.add(startReward);
@ -352,6 +354,14 @@ contract PolicyManager is Upgradeable {
arrangement.lastRefundedPeriod = maxPeriod.add(uint(1));
}
/**
* @notice Set the minimum reward that the node will take
**/
function setMinRewardRate(uint256 _minRewardRate) public {
NodeInfo storage node = nodes[msg.sender];
node.minRewardRate = _minRewardRate;
}
/**
* @notice Get number of nodes in policy
* @param _policyId Policy id
@ -463,7 +473,8 @@ contract PolicyManager is Upgradeable {
NodeInfo memory nodeInfoToCheck = delegateGetNodeInfo(_testTarget, RESERVED_NODE);
require(nodeInfoToCheck.reward == nodeInfo.reward &&
nodeInfoToCheck.rewardRate == nodeInfo.rewardRate &&
nodeInfoToCheck.lastMinedPeriod == nodeInfo.lastMinedPeriod);
nodeInfoToCheck.lastMinedPeriod == nodeInfo.lastMinedPeriod &&
nodeInfoToCheck.minRewardRate == nodeInfo.minRewardRate);
require(int256(delegateGet(_testTarget, "getNodeRewardDelta(address,uint256)",
bytes32(RESERVED_NODE), 11)) == nodeInfo.rewardDelta[11]);
@ -489,5 +500,6 @@ contract PolicyManager is Upgradeable {
nodeInfo.rewardRate = 33;
nodeInfo.lastMinedPeriod = 44;
nodeInfo.rewardDelta[11] = 55;
nodeInfo.minRewardRate = 777;
}
}

View File

@ -14,6 +14,7 @@ DISABLED_FIELD = 5
REWARD_FIELD = 0
REWARD_RATE_FIELD = 1
LAST_MINED_PERIOD_FIELD = 2
MIN_REWARD_RATE_FIELD = 3
@pytest.fixture()
@ -235,13 +236,32 @@ def test_create_revoke(web3, chain, escrow, policy_manager):
tx = policy_manager.functions.revokeArrangement(policy_id_2, node1).transact({'from': client})
chain.wait_for_receipt(tx)
# Try create policy with wrong value
# Try to create policy with wrong value
with pytest.raises((TransactionFailed, ValueError)):
tx = policy_manager.functions.createPolicy(policy_id, 10, 0, [node1]).transact({'from': client, 'value': 11})
chain.wait.for_receipt(tx)
tx = policy_manager.functions.createPolicy(policy_id_3, 10, 0, [node1]).transact({'from': client, 'value': 11})
chain.wait_for_receipt(tx)
with pytest.raises((TransactionFailed, ValueError)):
tx = policy_manager.functions.createPolicy(policy_id, 10, 1, [node1]).transact({'from': client, 'value': 12})
chain.wait.for_receipt(tx)
tx = policy_manager.functions.createPolicy(policy_id_3, 10, 1, [node1]).transact({'from': client, 'value': 22})
chain.wait_for_receipt(tx)
with pytest.raises((TransactionFailed, ValueError)):
tx = policy_manager.functions.createPolicy(policy_id_3, 10, 1, [node1]).transact({'from': client, 'value': 11})
chain.wait_for_receipt(tx)
# Set minimum reward rate for nodes
tx = policy_manager.functions.setMinRewardRate(10).transact({'from': node1})
chain.wait_for_receipt(tx)
tx = policy_manager.functions.setMinRewardRate(20).transact({'from': node2})
chain.wait_for_receipt(tx)
assert 10 == policy_manager.functions.nodes(node1).call()[MIN_REWARD_RATE_FIELD]
assert 20 == policy_manager.functions.nodes(node2).call()[MIN_REWARD_RATE_FIELD]
# Try to create policy with low rate
with pytest.raises((TransactionFailed, ValueError)):
tx = policy_manager.functions.createPolicy(policy_id_3, 1, 0, [node1]).transact({'from': client, 'value': 5})
chain.wait_for_receipt(tx)
with pytest.raises((TransactionFailed, ValueError)):
tx = policy_manager.functions.createPolicy(policy_id_3, 1, 0, [node1, node2]).transact({'from': client, 'value': 30})
chain.wait_for_receipt(tx)
# Create another policy with pay for first period
# Reward rate is calculated as (firstReward + rewardRate * numberOfPeriods) * numberOfNodes