StakingEscrow: workerToStaker -> stakerFromWorker

pull/1959/head
vzotova 2020-05-07 15:08:43 +03:00
parent f62765ec97
commit 28e41c4a21
5 changed files with 30 additions and 41 deletions

View File

@ -461,7 +461,7 @@ class StakingEscrowAgent(EthereumContractAgent):
@validate_checksum_address
def get_staker_from_worker(self, worker_address: str) -> str:
staker = self.contract.functions.getStakerFromWorker(worker_address).call()
staker = self.contract.functions.stakerFromWorker(worker_address).call()
return to_checksum_address(staker)
@validate_checksum_address

View File

@ -10,7 +10,7 @@ import "zeppelin/math/Math.sol";
/**
* @notice Supervises stakers' behavior and punishes when something's wrong.
* @dev |v2.1.1|
* @dev |v2.1.2|
*/
contract Adjudicator is Upgradeable {
@ -157,7 +157,7 @@ contract Adjudicator is Upgradeable {
address worker = SignatureVerifier.recover(
SignatureVerifier.hashEIP191(stamp, byte(0x45)), // Currently, we use version E (0x45) of EIP191 signatures
_workerIdentityEvidence);
address staker = escrow.getStakerFromWorker(worker);
address staker = escrow.stakerFromWorker(worker);
require(staker != address(0), "Worker must be related to a staker");
// 5. Check that staker can be slashed

View File

@ -37,7 +37,7 @@ interface WorkLockInterface {
/**
* @notice Contract holds and locks stakers tokens.
* Each staker that locks their tokens will receive some compensation
* @dev |v4.2.2|
* @dev |v5.1.1|
*/
contract StakingEscrow is Issuer, IERC900History {
@ -130,7 +130,7 @@ contract StakingEscrow is Issuer, IERC900History {
mapping (address => StakerInfo) public stakerInfo;
address[] public stakers;
mapping (address => address) public workerToStaker;
mapping (address => address) public stakerFromWorker;
mapping (uint16 => uint256) public lockedPerPeriod;
uint128[] public balanceHistory;
@ -446,13 +446,6 @@ contract StakingEscrow is Issuer, IERC900History {
return info.worker;
}
/**
* @notice Get staker using worker's address
*/
function getStakerFromWorker(address _worker) public view returns (address) {
return workerToStaker[_worker];
}
/**
* @notice Get work that completed by the staker
*/
@ -505,15 +498,15 @@ contract StakingEscrow is Issuer, IERC900History {
require(currentPeriod >= info.workerStartPeriod.add16(minWorkerPeriods),
"Not enough time has passed since the previous setting worker");
// Remove the old relation "worker->staker"
workerToStaker[info.worker] = address(0);
stakerFromWorker[info.worker] = address(0);
}
if (_worker != address(0)) {
require(workerToStaker[_worker] == address(0), "Specified worker is already in use");
require(stakerFromWorker[_worker] == address(0), "Specified worker is already in use");
require(stakerInfo[_worker].subStakes.length == 0 || _worker == msg.sender,
"Specified worker is a staker");
// Set new worker->staker relation
workerToStaker[_worker] = msg.sender;
stakerFromWorker[_worker] = msg.sender;
}
// Set new worker (or unset if _worker == address(0))
@ -660,7 +653,7 @@ contract StakingEscrow is Issuer, IERC900History {
require(numberOfSubStakes > 0 && subStakesLength >= endIndex);
StakerInfo storage info = stakerInfo[staker];
require(info.subStakes.length == 0);
require(workerToStaker[staker] == address(0), "A staker can't be a worker for another staker");
require(stakerFromWorker[staker] == address(0), "A staker can't be a worker for another staker");
stakers.push(staker);
policyManager.register(staker, previousPeriod);
@ -752,7 +745,7 @@ contract StakingEscrow is Issuer, IERC900History {
function deposit(address _staker, address _payer, uint256 _value, uint16 _periods) internal {
require(_value != 0);
StakerInfo storage info = stakerInfo[_staker];
require(workerToStaker[_staker] == address(0) || workerToStaker[_staker] == info.worker,
require(stakerFromWorker[_staker] == address(0) || stakerFromWorker[_staker] == info.worker,
"A staker can't be a worker for another staker");
// initial stake of the staker
if (info.subStakes.length == 0) {
@ -919,7 +912,7 @@ contract StakingEscrow is Issuer, IERC900History {
* @notice Confirm activity for the next period and mint for the previous period
*/
function confirmActivity() external isInitialized {
address staker = getStakerFromWorker(msg.sender);
address staker = stakerFromWorker[msg.sender];
StakerInfo storage info = stakerInfo[staker];
require(info.value > 0, "Staker must have a stake to confirm activity");
require(msg.sender == tx.origin, "Only worker with real address can confirm activity");
@ -1438,8 +1431,8 @@ contract StakingEscrow is Issuer, IERC900History {
require(address(delegateGet(_testTarget, this.workLock.selector)) == address(workLock));
require(delegateGet(_testTarget, this.lockedPerPeriod.selector,
bytes32(bytes2(RESERVED_PERIOD))) == lockedPerPeriod[RESERVED_PERIOD]);
require(address(delegateGet(_testTarget, this.workerToStaker.selector, bytes32(0))) ==
workerToStaker[address(0)]);
require(address(delegateGet(_testTarget, this.stakerFromWorker.selector, bytes32(0))) ==
stakerFromWorker[address(0)]);
require(delegateGet(_testTarget, this.getStakersLength.selector) == stakers.length);
if (stakers.length == 0) {
@ -1487,8 +1480,8 @@ contract StakingEscrow is Issuer, IERC900History {
totalStakedAt(block.number));
if (info.worker != address(0)) {
require(address(delegateGet(_testTarget, this.workerToStaker.selector, bytes32(uint256(info.worker)))) ==
workerToStaker[info.worker]);
require(address(delegateGet(_testTarget, this.stakerFromWorker.selector, bytes32(uint256(info.worker)))) ==
stakerFromWorker[info.worker]);
}
}
@ -1499,6 +1492,6 @@ contract StakingEscrow is Issuer, IERC900History {
lockedPerPeriod[RESERVED_PERIOD] = 111;
// Create fake worker
workerToStaker[address(0)] = address(this);
stakerFromWorker[address(0)] = address(this);
}
}

View File

@ -15,24 +15,20 @@ contract StakingEscrowForAdjudicatorMock {
uint32 public immutable secondsPerPeriod = 1;
mapping (address => uint256) public stakerInfo;
mapping (address => uint256) public rewardInfo;
mapping (address => address) public workerToStaker;
mapping (address => address) public stakerFromWorker;
function setStakerInfo(address _staker, uint256 _amount, address _worker) public {
stakerInfo[_staker] = _amount;
if (_worker == address(0)) {
_worker = _staker;
}
workerToStaker[_worker] = _staker;
stakerFromWorker[_worker] = _staker;
}
function getAllTokens(address _staker) public view returns (uint256) {
return stakerInfo[_staker];
}
function getStakerFromWorker(address _worker) public view returns (address) {
return workerToStaker[_worker];
}
function slashStaker(
address _staker,
uint256 _penalty,

View File

@ -584,7 +584,7 @@ def test_worker(testerchain, token, escrow_contract, deploy_contract):
testerchain.wait_for_receipt(tx)
assert sub_stake == escrow.functions.getAllTokens(intermediary1.address).call()
assert NULL_ADDRESS == escrow.functions.getWorkerFromStaker(intermediary1.address).call()
assert NULL_ADDRESS == escrow.functions.getStakerFromWorker(intermediary1.address).call()
assert NULL_ADDRESS == escrow.functions.stakerFromWorker(intermediary1.address).call()
tx = token.functions.transfer(intermediary2.address, sub_stake).transact()
testerchain.wait_for_receipt(tx)
@ -592,7 +592,7 @@ def test_worker(testerchain, token, escrow_contract, deploy_contract):
testerchain.wait_for_receipt(tx)
assert sub_stake == escrow.functions.getAllTokens(intermediary2.address).call()
assert NULL_ADDRESS == escrow.functions.getWorkerFromStaker(intermediary2.address).call()
assert NULL_ADDRESS == escrow.functions.getStakerFromWorker(intermediary2.address).call()
assert NULL_ADDRESS == escrow.functions.stakerFromWorker(intermediary2.address).call()
tx = token.functions.transfer(ursula3, sub_stake).transact()
testerchain.wait_for_receipt(tx)
@ -601,7 +601,7 @@ def test_worker(testerchain, token, escrow_contract, deploy_contract):
testerchain.wait_for_receipt(tx)
assert sub_stake == escrow.functions.getAllTokens(ursula3).call()
assert NULL_ADDRESS == escrow.functions.getWorkerFromStaker(ursula3).call()
assert NULL_ADDRESS == escrow.functions.getStakerFromWorker(ursula3).call()
assert NULL_ADDRESS == escrow.functions.stakerFromWorker(ursula3).call()
# Ursula can't confirm activity because there is no worker by default
with pytest.raises((TransactionFailed, ValueError)):
@ -617,7 +617,7 @@ def test_worker(testerchain, token, escrow_contract, deploy_contract):
tx = intermediary1.functions.setWorker(worker1).transact({'from': ursula1})
testerchain.wait_for_receipt(tx)
assert worker1 == escrow.functions.getWorkerFromStaker(intermediary1.address).call()
assert intermediary1.address == escrow.functions.getStakerFromWorker(worker1).call()
assert intermediary1.address == escrow.functions.stakerFromWorker(worker1).call()
tx = escrow.functions.confirmActivity().transact({'from': worker1})
testerchain.wait_for_receipt(tx)
@ -676,8 +676,8 @@ def test_worker(testerchain, token, escrow_contract, deploy_contract):
tx = intermediary1.functions.setWorker(worker2).transact({'from': ursula1})
testerchain.wait_for_receipt(tx)
assert worker2 == escrow.functions.getWorkerFromStaker(intermediary1.address).call()
assert intermediary1.address == escrow.functions.getStakerFromWorker(worker2).call()
assert NULL_ADDRESS == escrow.functions.getStakerFromWorker(worker1).call()
assert intermediary1.address == escrow.functions.stakerFromWorker(worker2).call()
assert NULL_ADDRESS == escrow.functions.stakerFromWorker(worker1).call()
number_of_events += 1
events = worker_log.get_all_entries()
@ -699,7 +699,7 @@ def test_worker(testerchain, token, escrow_contract, deploy_contract):
tx = intermediary2.functions.setWorker(worker1).transact({'from': ursula2})
testerchain.wait_for_receipt(tx)
assert worker1 == escrow.functions.getWorkerFromStaker(intermediary2.address).call()
assert intermediary2.address == escrow.functions.getStakerFromWorker(worker1).call()
assert intermediary2.address == escrow.functions.stakerFromWorker(worker1).call()
number_of_events += 1
events = worker_log.get_all_entries()
@ -720,8 +720,8 @@ def test_worker(testerchain, token, escrow_contract, deploy_contract):
tx = intermediary2.functions.setWorker(ursula2).transact({'from': ursula2})
testerchain.wait_for_receipt(tx)
assert ursula2 == escrow.functions.getWorkerFromStaker(intermediary2.address).call()
assert intermediary2.address == escrow.functions.getStakerFromWorker(ursula2).call()
assert NULL_ADDRESS == escrow.functions.getStakerFromWorker(worker1).call()
assert intermediary2.address == escrow.functions.stakerFromWorker(ursula2).call()
assert NULL_ADDRESS == escrow.functions.stakerFromWorker(worker1).call()
number_of_events += 1
events = worker_log.get_all_entries()
@ -736,7 +736,7 @@ def test_worker(testerchain, token, escrow_contract, deploy_contract):
.transact({'from': worker1})
testerchain.wait_for_receipt(tx)
assert sub_stake == escrow.functions.getAllTokens(worker1).call()
assert NULL_ADDRESS == escrow.functions.getStakerFromWorker(worker1).call()
assert NULL_ADDRESS == escrow.functions.stakerFromWorker(worker1).call()
assert NULL_ADDRESS == escrow.functions.getWorkerFromStaker(worker1).call()
# Ursula can't use the first worker again because worker is a staker now
@ -749,7 +749,7 @@ def test_worker(testerchain, token, escrow_contract, deploy_contract):
# (Probably not her best idea, but whatever)
tx = escrow.functions.setWorker(ursula3).transact({'from': ursula3})
testerchain.wait_for_receipt(tx)
assert ursula3 == escrow.functions.getStakerFromWorker(ursula3).call()
assert ursula3 == escrow.functions.stakerFromWorker(ursula3).call()
assert ursula3 == escrow.functions.getWorkerFromStaker(ursula3).call()
number_of_events += 1
@ -768,7 +768,7 @@ def test_worker(testerchain, token, escrow_contract, deploy_contract):
testerchain.time_travel(hours=1)
tx = escrow.functions.setWorker(worker3).transact({'from': ursula3})
testerchain.wait_for_receipt(tx)
assert ursula3 == escrow.functions.getStakerFromWorker(worker3).call()
assert ursula3 == escrow.functions.stakerFromWorker(worker3).call()
assert worker3 == escrow.functions.getWorkerFromStaker(ursula3).call()
number_of_events += 1