Skip to content
This repository has been archived by the owner on Mar 28, 2023. It is now read-only.

Commit

Permalink
Merge pull request #436 from keep-network/forward-value-to-keep
Browse files Browse the repository at this point in the history
Forward value to keep

we forward initial value to keepFactory. This is part of an effort to get
rid of funder bond and related logic.
  • Loading branch information
Shadowfiend authored Feb 5, 2020
2 parents 43bb30b + f8aa4dd commit 01eb296
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 39 deletions.
5 changes: 2 additions & 3 deletions implementation/contracts/deposit/DepositFunding.sol
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,13 @@ library DepositFunding {

require(_system.getAllowNewDeposits(), "Opening new deposits is currently disabled.");
require(_d.inStart(), "Deposit setup already requested");
/* solium-disable-next-line value-in-payable */
require(msg.value == TBTCConstants.getFunderBondAmount(), "incorrect funder bond amount");
require(_system.isAllowedLotSize(_lotSize), "provided lot size not supported");
// TODO: Whole value is stored as funder bond in the deposit, but part
// of it should be transferred to keep: https://github.com/keep-network/tbtc/issues/297
_d.lotSizeSatoshis = _lotSize;
uint256 _bondRequirement = _lotSize.mul(_system.getInitialCollateralizedPercent()).div(100);
_d.keepAddress = _system.requestNewKeep(_m, _n, _bondRequirement);
/* solium-disable-next-line value-in-payable */
_d.keepAddress = _system.requestNewKeep.value(msg.value)(_m, _n, _bondRequirement);
_d.signerFeeDivisor = _system.getSignerFeeDivisor();
_d.undercollateralizedThresholdPercent = _system.getUndercollateralizedThresholdPercent();
_d.severelyUndercollateralizedThresholdPercent = _system.getSeverelyUndercollateralizedThresholdPercent();
Expand Down
2 changes: 1 addition & 1 deletion implementation/contracts/deposit/DepositLiquidation.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ library DepositLiquidation {
bytes memory _preimage
) public returns (bool _isFraud) {
IBondedECDSAKeep _keep = IBondedECDSAKeep(_d.keepAddress);
return _keep.submitSignatureFraud(_d.keepAddress, _v, _r, _s, _signedDigest, _preimage);
return _keep.submitSignatureFraud(_v, _r, _s, _signedDigest, _preimage);
}

/// @notice Determines the collateralization percentage of the signing group
Expand Down
4 changes: 2 additions & 2 deletions implementation/contracts/deposit/DepositUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ library DepositUtils {
/// @return The amount of bonded ETH in wei
function fetchBondAmount(Deposit storage _d) public view returns (uint256) {
IBondedECDSAKeep _keep = IBondedECDSAKeep(_d.keepAddress);
return _keep.checkBondAmount(_d.keepAddress);
return _keep.checkBondAmount();
}

/// @notice Convert a LE bytes8 to a uint256
Expand Down Expand Up @@ -372,7 +372,7 @@ library DepositUtils {
function seizeSignerBonds(Deposit storage _d) internal returns (uint256) {
uint256 _preCallBalance = address(this).balance;
IBondedECDSAKeep _keep = IBondedECDSAKeep(_d.keepAddress);
_keep.seizeSignerBonds(_d.keepAddress);
_keep.seizeSignerBonds();
uint256 _postCallBalance = address(this).balance;
require(_postCallBalance > _preCallBalance, "No funds received, unexpected");
return _postCallBalance.sub(_preCallBalance);
Expand Down
13 changes: 12 additions & 1 deletion implementation/contracts/system/TBTCSystem.sol
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,17 @@ contract TBTCSystem is Ownable, ITBTCSystem, DepositLog {
return IRelay(relay).getPrevEpochDifficulty();
}

/// @notice Gets a fee estimate for creating a new Deposit.
/// @return Uint256 estimate.
function createNewDepositFeeEstimate()
external
returns (uint256)
{
IBondedECDSAKeepVendor _keepVendor = IBondedECDSAKeepVendor(keepVendor);
IBondedECDSAKeepFactory _keepFactory = IBondedECDSAKeepFactory(_keepVendor.selectFactory());
return _keepFactory.openKeepFeeEstimate();
}

/// @notice Request a new keep opening.
/// @param _m Minimum number of honest keep members required to sign.
/// @param _n Number of members in the keep.
Expand All @@ -228,6 +239,6 @@ contract TBTCSystem is Ownable, ITBTCSystem, DepositLog {
{
IBondedECDSAKeepVendor _keepVendor = IBondedECDSAKeepVendor(keepVendor);
IBondedECDSAKeepFactory _keepFactory = IBondedECDSAKeepFactory(_keepVendor.selectFactory());
return _keepFactory.openKeep(_n, _m, msg.sender, _bond);
return _keepFactory.openKeep.value(msg.value)(_n, _m, msg.sender, _bond);
}
}
26 changes: 20 additions & 6 deletions implementation/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion implementation/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"author": "James Prestwich",
"license": "UNLICENSED",
"dependencies": {
"@keep-network/keep-ecdsa": "^0.7.5",
"@keep-network/keep-ecdsa": "^0.7.7",
"@summa-tx/bitcoin-spv-sol": "^2.2.0",
"@summa-tx/relay-sol": "^1.1.0",
"@truffle/hdwallet-provider": "^1.0.26",
Expand Down
53 changes: 33 additions & 20 deletions implementation/test/DepositFactoryTest.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import deployTestDeposit from './helpers/deployTestDeposit'
import expectThrow from './helpers/expectThrow'

import BN from 'bn.js'
import utils from './utils'
Expand All @@ -10,12 +11,15 @@ const ECDSAKeepStub = artifacts.require('ECDSAKeepStub')
const Deposit = artifacts.require('Deposit')
const TestDeposit = artifacts.require('TestDeposit')

const TBTCSystem = artifacts.require('TBTCSystem')

contract('DepositFactory', () => {
const funderBondAmount = new BN('10').pow(new BN('5'))
const openKeepFee = new BN('123456') // set in ECDAKeepFactory
const fullBtc = 100000000

describe('createDeposit()', async () => {
let depositFactory
let ecdsaKeepFactoryStub

before(async () => {
// To properly test createDeposit, we deploy the real Deposit contract and
Expand All @@ -30,12 +34,12 @@ contract('DepositFactory', () => {

await depositFactory.createDeposit(
fullBtc,
{ value: funderBondAmount }
{ value: openKeepFee }
)

await depositFactory.createDeposit(
fullBtc,
{ value: funderBondAmount }
{ value: openKeepFee }
)

const eventList = await depositFactory.getPastEvents('DepositCloneCreated', { fromBlock: blockNumber, toBlock: 'latest' })
Expand All @@ -46,24 +50,33 @@ contract('DepositFactory', () => {
assert.notEqual(eventList[0].returnValues.depositCloneAddress, eventList[1].returnValues.depositCloneAddress, 'clone addresses should not be equal')
})

it('correctly forwards value to Deposit', async () => {
const blockNumber = await web3.eth.getBlockNumber()
it('correctly forwards value to keep factory', async () => {
// Use real TBTCSystem contract to validate value forwarding:
// DepositFactory -> Deposit -> TBTCSystem -> ECDSAKeepFactory
({
ecdsaKeepFactoryStub,
depositFactory,
} = await deployTestDeposit([], { 'TestDeposit': Deposit, 'TBTCSystemStub': TBTCSystem }))

await depositFactory.createDeposit(
fullBtc,
{ value: funderBondAmount }
{ value: openKeepFee }
)
const eventList = await depositFactory.getPastEvents(
'DepositCloneCreated',
{
fromBlock: blockNumber,
toBlock: 'latest',
})

const depositAddress = eventList[eventList.length - 1].returnValues.depositCloneAddress
expect(
await web3.eth.getBalance(ecdsaKeepFactoryStub.address),
'Factory did not correctly forward value on Deposit creation'
).to.eq.BN(openKeepFee)
})

const balance = await web3.eth.getBalance(depositAddress)
assert.equal(balance, funderBondAmount, 'Factory did not correctly forward value on Deposit creation')
it('reverts if insufficient fee is provided', async () => {
const badOpenKeepFee = openKeepFee.sub(new BN(1))
await expectThrow(
depositFactory.createDeposit(
fullBtc,
{ value: badOpenKeepFee }
),
'Insufficient value for new keep creation'
)
})
})

Expand Down Expand Up @@ -95,12 +108,12 @@ contract('DepositFactory', () => {

await depositFactory.createDeposit(
fullBtc,
{ value: funderBondAmount }
{ value: openKeepFee }
)

await depositFactory.createDeposit(
fullBtc,
{ value: funderBondAmount }
{ value: openKeepFee }
)

const eventList = await depositFactory.getPastEvents('DepositCloneCreated', { fromBlock: blockNumber, toBlock: 'latest' })
Expand Down Expand Up @@ -155,7 +168,7 @@ contract('DepositFactory', () => {
1,
1,
fullBtc,
{ value: funderBondAmount }
{ value: openKeepFee }
)

await testDeposit.setKeepAddress(keep.address)
Expand All @@ -171,7 +184,7 @@ contract('DepositFactory', () => {

await depositFactory.createDeposit(
fullBtc,
{ value: funderBondAmount }
{ value: openKeepFee }
)

const eventList = await depositFactory.getPastEvents('DepositCloneCreated', { fromBlock: blockNumber, toBlock: 'latest' })
Expand Down
18 changes: 16 additions & 2 deletions implementation/test/TBTCSystemTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,14 @@ contract('TBTCSystem', (accounts) => {
})

describe('requestNewKeep()', async () => {
let openKeepFee
before(async () => {
openKeepFee = await ecdsaKeepFactory.openKeepFeeEstimate.call()
})
it('sends caller as owner to open new keep', async () => {
const expectedKeepOwner = accounts[2]

await tbtcSystem.requestNewKeep(5, 10, 0, { from: expectedKeepOwner })
await tbtcSystem.requestNewKeep(5, 10, 0, { from: expectedKeepOwner, value: openKeepFee })
const keepOwner = await ecdsaKeepFactory.keepOwner.call()

assert.equal(expectedKeepOwner, keepOwner, 'incorrect keep owner address')
Expand All @@ -46,10 +50,20 @@ contract('TBTCSystem', (accounts) => {
it('returns keep address', async () => {
const expectedKeepAddress = await ecdsaKeepFactory.keepAddress.call()

const result = await tbtcSystem.requestNewKeep.call(5, 10, 0)
const result = await tbtcSystem.requestNewKeep.call(5, 10, 0, { value: openKeepFee })

assert.equal(expectedKeepAddress, result, 'incorrect keep address')
})

it('forwards value to keep factory', async () => {
const initialBalance = await web3.eth.getBalance(ecdsaKeepFactory.address)

await tbtcSystem.requestNewKeep(5, 10, 0, { value: openKeepFee })

const finalBalance = await web3.eth.getBalance(ecdsaKeepFactory.address)
const balanceCheck = new BN(finalBalance).sub(new BN(initialBalance))
expect(balanceCheck, 'TBTCSystem did not correctly forward value to keep factory').to.eq.BN(openKeepFee)
})
})

describe('setSignerFeeDivisor', async () => {
Expand Down
11 changes: 11 additions & 0 deletions implementation/test/contracts/keep/ECDSAKeepFactoryStub.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,25 @@ import {IBondedECDSAKeepFactory} from "@keep-network/keep-ecdsa/contracts/api/IB
contract ECDSAKeepFactoryStub is IBondedECDSAKeepFactory {
address public keepOwner;
address public keepAddress = address(888);
uint256 feeEstimate = 123456;

function openKeep(
uint256,
uint256,
address _owner,
uint256
) external payable returns (address) {
require(msg.value >= feeEstimate, "Insufficient value for new keep creation");
keepOwner = _owner;
return keepAddress;
}

function openKeepFeeEstimate() external returns (uint256){
return feeEstimate;
}

function setOpenKeepFeeEstimate(uint256 _fee) external {
feeEstimate = _fee;
}

}
5 changes: 2 additions & 3 deletions implementation/test/contracts/keep/ECDSAKeepStub.sol
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ contract ECDSAKeepStub is IBondedECDSAKeep {
// Functions implemented for IBondedECDSAKeep interface.

function submitSignatureFraud(
address,
uint8,
bytes32,
bytes32,
Expand All @@ -65,11 +64,11 @@ contract ECDSAKeepStub is IBondedECDSAKeep {
return success;
}

function checkBondAmount(address) external view returns (uint256){
function checkBondAmount() external view returns (uint256){
return bondAmount;
}

function seizeSignerBonds(address) external returns (bool){
function seizeSignerBonds() external returns (bool){
if (address(this).balance > 0) {
msg.sender.transfer(address(this).balance);
}
Expand Down

0 comments on commit 01eb296

Please sign in to comment.