Skip to content

Commit

Permalink
Merge pull request #132 from mysteriumnetwork/fix/add_r_to_event
Browse files Browse the repository at this point in the history
added revealed R to events
  • Loading branch information
creotiv authored Oct 6, 2021
2 parents cca6475 + 3f9bc9a commit 55af719
Show file tree
Hide file tree
Showing 11 changed files with 11,412 additions and 5,800 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ npm install
2. Run local ethereum node, e.g. `ganache`. Make sure to use version greater than 6.12.2.

```bash
npx ganache-cli --port 7545 --mnemonic "amused glory pen avocado toilet dragon entry kitchen cliff retreat canyon danger"
npx ganache-cli --port 8545 --mnemonic "amused glory pen avocado toilet dragon entry kitchen cliff retreat canyon danger"
```

3. Run tests
Expand Down
4 changes: 2 additions & 2 deletions contracts/ChannelImplementation.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ contract ChannelImplementation is FundsRecovery, Utils {
address public operator; // channel operator = sha3(IdentityPublicKey)[:20]
IUniswapV2Router internal dex; // any uniswap v2 compatible dex router address

event PromiseSettled(address beneficiary, uint256 amount, uint256 totalSettled);
event PromiseSettled(address beneficiary, uint256 amount, uint256 totalSettled, bytes32 lock);
event ExitRequested(uint256 timelock);
event Withdraw(address beneficiary, uint256 amount);

Expand Down Expand Up @@ -112,7 +112,7 @@ contract ChannelImplementation is FundsRecovery, Utils {
token.transfer(msg.sender, _transactorFee);
}

emit PromiseSettled(hermes.contractAddress, _unpaidAmount, hermes.settled);
emit PromiseSettled(hermes.contractAddress, _unpaidAmount, hermes.settled, _lock);
}

// Returns blocknumber until which exit request should be locked
Expand Down
4 changes: 2 additions & 2 deletions contracts/HermesImplementation.sol
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ contract HermesImplementation is FundsRecovery, Utils {
return status;
}

event PromiseSettled(bytes32 indexed channelId, address indexed beneficiary, uint256 amountSentToBeneficiary, uint256 fees);
event PromiseSettled(bytes32 indexed channelId, address indexed beneficiary, uint256 amountSentToBeneficiary, uint256 fees, bytes32 lock);
event NewStake(bytes32 indexed channelId, uint256 stakeAmount);
event MinStakeValueUpdated(uint256 newMinStake);
event MaxStakeValueUpdated(uint256 newMaxStake);
Expand Down Expand Up @@ -201,7 +201,7 @@ contract HermesImplementation is FundsRecovery, Utils {

uint256 _amountToTransfer = _unpaidAmount -_fees;

emit PromiseSettled(_channelId, _beneficiary, _amountToTransfer, _fees);
emit PromiseSettled(_channelId, _beneficiary, _amountToTransfer, _fees, _preimage);

return _amountToTransfer;
}
Expand Down
4 changes: 2 additions & 2 deletions contracts/flattened/ChannelImplementation.sol.flattened
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ contract ChannelImplementation is FundsRecovery, Utils {
address public operator; // channel operator = sha3(IdentityPublicKey)[:20]
IUniswapV2Router internal dex; // any uniswap v2 compatible dex router address

event PromiseSettled(address beneficiary, uint256 amount, uint256 totalSettled);
event PromiseSettled(address beneficiary, uint256 amount, uint256 totalSettled, bytes32 lock);
event ExitRequested(uint256 timelock);
event Withdraw(address beneficiary, uint256 amount);

Expand Down Expand Up @@ -531,7 +531,7 @@ contract ChannelImplementation is FundsRecovery, Utils {
token.transfer(msg.sender, _transactorFee);
}

emit PromiseSettled(hermes.contractAddress, _unpaidAmount, hermes.settled);
emit PromiseSettled(hermes.contractAddress, _unpaidAmount, hermes.settled, _lock);
}

// Returns blocknumber until which exit request should be locked
Expand Down
4 changes: 2 additions & 2 deletions contracts/flattened/HermesImplementation.sol.flattened
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ contract HermesImplementation is FundsRecovery, Utils {
return status;
}

event PromiseSettled(bytes32 indexed channelId, address indexed beneficiary, uint256 amountSentToBeneficiary, uint256 fees);
event PromiseSettled(bytes32 indexed channelId, address indexed beneficiary, uint256 amountSentToBeneficiary, uint256 fees, bytes32 lock);
event NewStake(bytes32 indexed channelId, uint256 stakeAmount);
event MinStakeValueUpdated(uint256 newMinStake);
event MaxStakeValueUpdated(uint256 newMaxStake);
Expand Down Expand Up @@ -606,7 +606,7 @@ contract HermesImplementation is FundsRecovery, Utils {

uint256 _amountToTransfer = _unpaidAmount -_fees;

emit PromiseSettled(_channelId, _beneficiary, _amountToTransfer, _fees);
emit PromiseSettled(_channelId, _beneficiary, _amountToTransfer, _fees, _preimage);

return _amountToTransfer;
}
Expand Down
17,105 changes: 11,334 additions & 5,771 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"ganache-cli": "^6.12.2",
"lodash": "^4.17.21",
"secp256k1": "^4.0.2",
"truffle": "^5.3.9",
"truffle": "^5.4.13",
"truffle-flattener": "^1.5.0"
}
}
}
4 changes: 2 additions & 2 deletions scripts/deployRegistry.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ module.exports = async (web3, account = undefined) => {
}

// Deploy HermesImplementation into deterministic address
const hermesTxMetadata = generateDeployTx(hermesBytecode, 3327541)
const hermesTxMetadata = generateDeployTx(hermesBytecode, 3328541)
const deployedHermesCode = await web3.eth.getCode(hermesTxMetadata.contractAddress)
if (deployedHermesCode.length <= 3) {
await web3.eth.sendTransaction({
from: account, to: hermesTxMetadata.sender, value: '3327541000000000000'
from: account, to: hermesTxMetadata.sender, value: '3328541000000000000'
})
await web3.eth.sendSignedTransaction(hermesTxMetadata.rawTx)
}
Expand Down
51 changes: 42 additions & 9 deletions test/hermes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Smart-contract code can be found in `contracts/HermesImplementation.sol`.
*/

const { BN } = require('@openzeppelin/test-helpers')
const { BN, expectEvent } = require('@openzeppelin/test-helpers')
const {
generateChannelId,
topUpTokens,
Expand Down Expand Up @@ -103,7 +103,11 @@ contract('Hermes Contract Implementation tests', ([txMaker, operatorAddress, ben
const balanceBefore = await token.balanceOf(beneficiaryA)

promise = generatePromise(amountToPay, Zero, channelState, operator, identityA.address)
await hermes.settlePromise(promise.identity, promise.amount, promise.fee, promise.lock, promise.signature)
var res = await hermes.settlePromise(promise.identity, promise.amount, promise.fee, promise.lock, promise.signature)

await expectEvent.inTransaction(res.receipt.transactionHash, hermes, 'PromiseSettled', {
"4":"0x"+promise.lock.toString('hex')
})

const balanceAfter = await token.balanceOf(beneficiaryA)
balanceAfter.should.be.bignumber.equal(balanceBefore.add(amountToPay))
Expand Down Expand Up @@ -154,7 +158,11 @@ contract('Hermes Contract Implementation tests', ([txMaker, operatorAddress, ben
const balanceBefore = await token.balanceOf(beneficiaryB)

promise = generatePromise(amountToPay, new BN(0), channelState, operator)
await hermes.settlePromise(identityB.address, promise.amount, promise.fee, promise.lock, promise.signature)
var res = await hermes.settlePromise(identityB.address, promise.amount, promise.fee, promise.lock, promise.signature)

await expectEvent.inTransaction(res.receipt.transactionHash, hermes, 'PromiseSettled', {
"4":"0x"+promise.lock.toString('hex')
})

const balanceAfter = await token.balanceOf(beneficiaryB)
balanceAfter.should.be.bignumber.equal(balanceBefore.add(amountToPay))
Expand Down Expand Up @@ -205,7 +213,11 @@ contract('Hermes Contract Implementation tests', ([txMaker, operatorAddress, ben
const txMakerBalanceBefore = await token.balanceOf(txMaker)

const promise = generatePromise(amountToPay, fee, channelState, operator)
await hermes.settlePromise(identityC.address, promise.amount, promise.fee, promise.lock, promise.signature)
var res =await hermes.settlePromise(identityC.address, promise.amount, promise.fee, promise.lock, promise.signature)

await expectEvent.inTransaction(res.receipt.transactionHash, hermes, 'PromiseSettled', {
"4":"0x"+promise.lock.toString('hex')
})

const beneficiaryBalanceAfter = await token.balanceOf(beneficiaryC)
beneficiaryBalanceAfter.should.be.bignumber.equal(beneficiaryBalanceBefore.add(amountToPay))
Expand All @@ -225,7 +237,11 @@ contract('Hermes Contract Implementation tests', ([txMaker, operatorAddress, ben
const fee = Zero

promise = generatePromise(amountToPay, fee, channelState, operator, identityC.address)
await hermes.settlePromise(promise.identity, promise.amount, promise.fee, promise.lock, promise.signature)
var res =await hermes.settlePromise(promise.identity, promise.amount, promise.fee, promise.lock, promise.signature)

await expectEvent.inTransaction(res.receipt.transactionHash, hermes, 'PromiseSettled', {
"4":"0x"+promise.lock.toString('hex')
})

const beneficiaryBalance = await token.balanceOf(beneficiaryC)
beneficiaryBalance.should.be.bignumber.equal(initialBeneficiaryBalance.add(maxStake)) // there is not possible to settle more than maxStake in one tx
Expand All @@ -235,7 +251,12 @@ contract('Hermes Contract Implementation tests', ([txMaker, operatorAddress, ben
const initialBeneficiaryBalance = await token.balanceOf(beneficiaryC)

// Settle previous promise to get rest of promised coins
await hermes.settlePromise(promise.identity, promise.amount, promise.fee, promise.lock, promise.signature)
var res =await hermes.settlePromise(promise.identity, promise.amount, promise.fee, promise.lock, promise.signature)

await expectEvent.inTransaction(res.receipt.transactionHash, hermes, 'PromiseSettled', {
"4":"0x"+promise.lock.toString('hex')
})

const beneficiaryBalance = await token.balanceOf(beneficiaryC)
beneficiaryBalance.should.be.bignumber.equal(initialBeneficiaryBalance.add(new BN('1000'))) // 1000 should be left after previous promise
})
Expand All @@ -252,7 +273,11 @@ contract('Hermes Contract Implementation tests', ([txMaker, operatorAddress, ben
const balanceBefore = new BN(await web3.eth.getBalance(beneficiaryC))

promise = generatePromise(amountToPay, new BN(0), channelState, operator, identityC.address)
await hermes.settleWithDEX(promise.identity, promise.amount, promise.fee, promise.lock, promise.signature)
var res =await hermes.settleWithDEX(promise.identity, promise.amount, promise.fee, promise.lock, promise.signature)

await expectEvent.inTransaction(res.receipt.transactionHash, hermes, 'PromiseSettled', {
"4":"0x"+promise.lock.toString('hex')
})

const balanceAfter = new BN(await web3.eth.getBalance(beneficiaryC))
balanceAfter.should.be.bignumber.equal(balanceBefore.add(expectedETHAmount))
Expand Down Expand Up @@ -333,7 +358,11 @@ contract('Hermes Contract Implementation tests', ([txMaker, operatorAddress, ben
// Settle all you can
const channelState = Object.assign({}, { channelId }, await hermes.channels(channelId))
const promise = generatePromise(amountToLend, Zero, channelState, operator, identityD.address)
await hermes.settlePromise(promise.identity, promise.amount, promise.fee, promise.lock, promise.signature)
var res = await hermes.settlePromise(promise.identity, promise.amount, promise.fee, promise.lock, promise.signature)

await expectEvent.inTransaction(res.receipt.transactionHash, hermes, 'PromiseSettled', {
"4":"0x"+promise.lock.toString('hex')
})

// Ensure that amountToLend is bigger than stake + locked in channels funds
let minimalExpectedBalance = await hermes.minimalExpectedBalance()
Expand Down Expand Up @@ -381,7 +410,11 @@ contract('Hermes Contract Implementation tests', ([txMaker, operatorAddress, ben
const channelId = generateChannelId(identityC.address, hermes.address)
const channelState = Object.assign({}, { channelId }, await hermes.channels(channelId))
const promise = generatePromise(new BN(700), Zero, channelState, operator)
await hermes.settlePromise(identityC.address, promise.amount, promise.fee, promise.lock, promise.signature)
var res = await hermes.settlePromise(identityC.address, promise.amount, promise.fee, promise.lock, promise.signature)

await expectEvent.inTransaction(res.receipt.transactionHash, hermes, 'PromiseSettled', {
"4":"0x"+promise.lock.toString('hex')
})

// Withdraw request should be rejected and no funds moved
const initialBalance = await token.balanceOf(hermes.address)
Expand Down
26 changes: 21 additions & 5 deletions test/hermesChannelSettlement.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Tested functions can be found in smart-contract code at `contracts/HermesImplementation.sol`.
*/

const { BN } = require('@openzeppelin/test-helpers')
const { BN, expectEvent } = require('@openzeppelin/test-helpers')
const { randomBytes } = require('crypto')
const {
generateChannelId,
Expand Down Expand Up @@ -88,7 +88,11 @@ contract("Channel openinig via settlement tests", ([txMaker, beneficiaryA, benef
// To open channel during settlement we must call `settleWithBeneficiary` instead of `settlePromise`
const beneficiaryChangeSignature = signChannelBeneficiaryChange(ChainID, registry.address, beneficiaryA, nonce, providerA)
const promise = generatePromise(amountToPay, Zero, channelState, operator, providerA.address)
await hermes.settleWithBeneficiary(promise.identity, promise.amount, promise.fee, promise.lock, promise.signature, beneficiaryA, beneficiaryChangeSignature)
var res = await hermes.settleWithBeneficiary(promise.identity, promise.amount, promise.fee, promise.lock, promise.signature, beneficiaryA, beneficiaryChangeSignature)

await expectEvent.inTransaction(res.receipt.transactionHash, hermes, 'PromiseSettled', {
"4":"0x"+promise.lock.toString('hex')
})

const balanceAfter = await token.balanceOf(beneficiaryA)
balanceAfter.should.be.bignumber.equal(balanceBefore.add(amountToPay))
Expand All @@ -110,7 +114,11 @@ contract("Channel openinig via settlement tests", ([txMaker, beneficiaryA, benef

// Generate and settle promise
const promise = generatePromise(amountToPay, Zero, channelState, operator, providerA.address)
await hermes.settlePromise(promise.identity, promise.amount, promise.fee, promise.lock, promise.signature)
var res =await hermes.settlePromise(promise.identity, promise.amount, promise.fee, promise.lock, promise.signature)

await expectEvent.inTransaction(res.receipt.transactionHash, hermes, 'PromiseSettled', {
"4":"0x"+promise.lock.toString('hex')
})

// Promise can settle even more than its stake (up to maxStake)
const balanceAfter = await token.balanceOf(beneficiaryA)
Expand All @@ -132,7 +140,11 @@ contract("Channel openinig via settlement tests", ([txMaker, beneficiaryA, benef
for (let times = 0; times < 5; times++) {
const balanceBefore = await token.balanceOf(beneficiaryA)

await hermes.settlePromise(promise.identity, promise.amount, promise.fee, promise.lock, promise.signature)
let res = await hermes.settlePromise(promise.identity, promise.amount, promise.fee, promise.lock, promise.signature)

await expectEvent.inTransaction(res.receipt.transactionHash, hermes, 'PromiseSettled', {
"4":"0x"+promise.lock.toString('hex')
})

const balanceAfter = await token.balanceOf(beneficiaryA)
balanceAfter.should.be.bignumber.equal(balanceBefore.add(maxStake))
Expand All @@ -152,7 +164,11 @@ contract("Channel openinig via settlement tests", ([txMaker, beneficiaryA, benef

// Generate promise and settle into stake
const promise = generatePromise(amountToPay, transactorFee, channelState, operator, providerA.address)
await hermes.settleIntoStake(promise.identity, promise.amount, promise.fee, promise.lock, promise.signature)
var res = await hermes.settleIntoStake(promise.identity, promise.amount, promise.fee, promise.lock, promise.signature)

await expectEvent.inTransaction(res.receipt.transactionHash, hermes, 'PromiseSettled', {
"4":"0x"+promise.lock.toString('hex')
})

// It should have increased stake
const channelStakeAfter = (await hermes.channels(channelId)).stake
Expand Down
4 changes: 2 additions & 2 deletions truffle-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module.exports = {
networks: {
development: {
host: "127.0.0.1",
port: 7545,
port: 8545,
network_id: "*",
disableConfirmationListener: true
},
Expand Down Expand Up @@ -53,7 +53,7 @@ module.exports = {
},
test: {
host: "127.0.0.1",
port: 7545,
port: 8545,
network_id: "*"
},
e2e: {
Expand Down

0 comments on commit 55af719

Please sign in to comment.