Skip to content

Commit

Permalink
Penalization now works with stake unlocked
Browse files Browse the repository at this point in the history
  • Loading branch information
raullaprida committed Jun 11, 2021
1 parent a8ca041 commit 4e6c5d9
Show file tree
Hide file tree
Showing 5 changed files with 230 additions and 93 deletions.
61 changes: 29 additions & 32 deletions contracts/RelayHub.sol
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,9 @@ contract RelayHub is IRelayHub {
_;
}

/// Slash the stake of the relay relayManager. In order to prevent stake kidnapping, burns half of stake on the way.
/// @param relayWorker - worker whose manager will be penalized
/// @param beneficiary - address that receives half of the penalty amount
function penalize(address relayWorker, address payable beneficiary)
external
override
Expand All @@ -261,42 +264,33 @@ contract RelayHub is IRelayHub {
address(uint160(uint256(workerToManager[relayWorker] >> 4)));
require(relayManager != address(0), "Unknown relay worker");

requireManagerStaked(relayManager);

penalizeRelayManager(relayManager, beneficiary);
}

function getStakeInfo(address relayManager)
external
view
override
returns (StakeInfo memory stakeInfo)
{
return stakes[relayManager];
}

/// Slash the stake of the relay relayManager. In order to prevent stake kidnapping, burns half of stake on the way.
/// @param relayManager - entry to penalize
/// @param beneficiary - address that receives half of the penalty amount
function penalizeRelayManager(
address relayManager,
address payable beneficiary
) internal {
StakeInfo storage stakeInfo = stakes[relayManager];

uint256 amount = stakeInfo.stake;

//In the case the stake owner have already withrawn their funds
require(amount > 0, "Unstaked relay manager");

// Half of the stake will be burned (sent to address 0)
stakeInfo.stake = 0;

uint256 toBurn = SafeMath.div(amount, 2);
uint256 reward = SafeMath.sub(amount, toBurn);

// Ether is burned and transferred
// RBTC is burned and transferred
address(0).transfer(toBurn);
beneficiary.transfer(reward);
emit StakePenalized(relayManager, beneficiary, reward);
}

function getStakeInfo(address relayManager)
external
view
override
returns (StakeInfo memory stakeInfo)
{
return stakes[relayManager];
}
// Put a stake for a relayManager and set its unstake delay.
// If the entry does not exist, it is created, and the caller of this function becomes its owner.
// If the entry already exists, only the owner can call this function.
Expand All @@ -307,13 +301,16 @@ contract RelayHub is IRelayHub {
payable
override
{

StakeInfo storage stakeInfo = stakes[relayManager];

require(
stakes[relayManager].owner == address(0) ||
stakes[relayManager].owner == msg.sender,
stakeInfo.owner == address(0) ||
stakeInfo.owner == msg.sender,
"not owner"
);
require(
unstakeDelay >= stakes[relayManager].unstakeDelay,
unstakeDelay >= stakeInfo.unstakeDelay,
"unstakeDelay cannot be decreased"
);
require(msg.sender != relayManager, "caller is the relayManager");
Expand All @@ -323,21 +320,21 @@ contract RelayHub is IRelayHub {
);

//If it is the initial stake, it must meet the entry value
if (stakes[relayManager].owner == address(0)) {
if (stakeInfo.owner == address(0)) {
require(
msg.value >= minimumEntryDepositValue,
"Insufficient intitial stake"
);
}

stakes[relayManager].owner = msg.sender;
stakes[relayManager].stake += msg.value;
stakes[relayManager].unstakeDelay = unstakeDelay;
stakeInfo.owner = msg.sender;
stakeInfo.stake += msg.value;
stakeInfo.unstakeDelay = unstakeDelay;
emit StakeAdded(
relayManager,
stakes[relayManager].owner,
stakes[relayManager].stake,
stakes[relayManager].unstakeDelay
stakeInfo.owner,
stakeInfo.stake,
stakeInfo.unstakeDelay
);
}

Expand Down
9 changes: 4 additions & 5 deletions src/common/Utils.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import abi from 'web3-eth-abi'
import { toBN } from 'web3-utils'
import sigUtil from 'eth-sig-util'
import sigUtil, { EIP712TypedData } from 'eth-sig-util'
import { EventData } from 'web3-eth-contract'
import { JsonRpcResponse } from 'web3-core-helpers'
import { PrefixedHexString } from 'ethereumjs-tx'
import { arrayify } from '@ethersproject/bytes'
import { Address } from '../relayclient/types/Aliases'
import { ServerConfigParams } from '../relayserver/ServerConfigParams'

import TypedRequestData from './EIP712/TypedRequestData'
import chalk from 'chalk'
import { constants } from './Constants'
import { DeployTransactionRequest, RelayTransactionRequest } from '../relayclient/types/RelayTransactionRequest'
Expand Down Expand Up @@ -67,7 +66,7 @@ export function decodeRevertReason (revertBytes: PrefixedHexString, throwOnError
}

export function getLocalEip712Signature (
typedRequestData: TypedRequestData,
typedRequestData: EIP712TypedData,
privateKey: Buffer
): PrefixedHexString {
// @ts-ignore
Expand All @@ -76,12 +75,12 @@ export function getLocalEip712Signature (

export async function getEip712Signature (
web3: Web3,
typedRequestData: TypedRequestData,
typedRequestData: EIP712TypedData,
methodSuffix = '',
jsonStringifyRequest = false
): Promise<PrefixedHexString> {
const senderAddress = typedRequestData.message.from
let dataToSign: TypedRequestData | string
let dataToSign: EIP712TypedData | string
if (jsonStringifyRequest) {
dataToSign = JSON.stringify(typedRequestData)
} else {
Expand Down
Loading

0 comments on commit 4e6c5d9

Please sign in to comment.