Skip to content

Commit

Permalink
Add tests for fixFailedMessage with alternative receiver
Browse files Browse the repository at this point in the history
  • Loading branch information
k1rill-fedoseev committed Apr 17, 2020
1 parent 5e49ff5 commit 5d3899f
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ contract BasicAMBErc677ToErc677 is
addressStorage[keccak256(abi.encodePacked("messageHashRecipient", _hash))] = _recipient;
}

function messageHashRecipient(bytes32 _hash) public view returns (address) {
function messageHashRecipient(bytes32 _hash) internal view returns (address) {
return addressStorage[keccak256(abi.encodePacked("messageHashRecipient", _hash))];
}

Expand Down
84 changes: 83 additions & 1 deletion test/amb_erc677_to_erc677/AMBErc677ToErc677Behavior.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ function shouldBehaveLikeBasicAMBErc677ToErc677(otherSideMediatorContract, accou
let erc677Token
const owner = accounts[0]
const user = accounts[1]
const user2 = accounts[2]
describe('initialize', () => {
beforeEach(async () => {
bridgeContract = await AMBMock.new()
Expand Down Expand Up @@ -498,7 +499,6 @@ function shouldBehaveLikeBasicAMBErc677ToErc677(otherSideMediatorContract, accou
describe('relayTokens', () => {
let contract
let erc20Token
const user2 = accounts[2]
beforeEach(async function() {
bridgeContract = await AMBMock.new()
await bridgeContract.setMaxGasPerTx(maxGasPerTx)
Expand Down Expand Up @@ -992,6 +992,88 @@ function shouldBehaveLikeBasicAMBErc677ToErc677(otherSideMediatorContract, accou
expect(event[0].returnValues.value).to.be.equal(oneEther.toString())
})
})
describe('fixFailedMessage for alternative receiver', () => {
let dataHash
let contract
beforeEach(async function() {
bridgeContract = await AMBMock.new()
await bridgeContract.setMaxGasPerTx(maxGasPerTx)
mediatorContract = await otherSideMediatorContract.new()
erc677Token = await ERC677BridgeToken.new('test', 'TST', 18)
await erc677Token.mint(user, twoEthers, { from: owner }).should.be.fulfilled

contract = this.bridge

await contract.initialize(
bridgeContract.address,
mediatorContract.address,
erc677Token.address,
[dailyLimit, maxPerTx, minPerTx],
[executionDailyLimit, executionMaxPerTx],
maxGasPerTx,
decimalShiftZero,
owner
).should.be.fulfilled
await erc677Token.transferOwnership(contract.address)

expect(await erc677Token.balanceOf(user)).to.be.bignumber.equal(twoEthers)
expect(await erc677Token.totalSupply()).to.be.bignumber.equal(twoEthers)

// User transfer tokens
const transferTx = await erc677Token.transferAndCall(contract.address, oneEther, user2, { from: user }).should.be
.fulfilled

expect(await erc677Token.balanceOf(user)).to.be.bignumber.equal(oneEther)

const events = await getEvents(bridgeContract, { event: 'MockedEvent' })
expect(events.length).to.be.equal(1)
const data = `0x${events[0].returnValues.encodedData.substr(
148,
events[0].returnValues.encodedData.length - 148
)}`

// Bridge calls mediator from other side
await bridgeContract.executeMessageCall(
contract.address,
contract.address,
data,
transferTx.tx,
100
).should.be.fulfilled

expect(await bridgeContract.messageCallStatus(transferTx.tx)).to.be.equal(false)

// mediator from other side should use this dataHash to request fix the failed message
dataHash = await bridgeContract.failedMessageDataHash(transferTx.tx)
})
it('should fix burnt/locked tokens', async () => {
// Given
expect(await contract.messageHashFixed(dataHash)).to.be.equal(false)

// When
const fixData = await contract.contract.methods.fixFailedMessage(dataHash).encodeABI()

await bridgeContract.executeMessageCall(
contract.address,
mediatorContract.address,
fixData,
exampleTxHash,
1000000
).should.be.fulfilled

// Then
expect(await bridgeContract.messageCallStatus(exampleTxHash)).to.be.equal(true)
expect(await erc677Token.balanceOf(user)).to.be.bignumber.equal(twoEthers)
expect(await erc677Token.totalSupply()).to.be.bignumber.equal(twoEthers)
expect(await contract.messageHashFixed(dataHash)).to.be.equal(true)

const event = await getEvents(contract, { event: 'FailedMessageFixed' })
expect(event.length).to.be.equal(1)
expect(event[0].returnValues.dataHash).to.be.equal(dataHash)
expect(event[0].returnValues.recipient).to.be.equal(user)
expect(event[0].returnValues.value).to.be.equal(oneEther.toString())
})
})
describe('#claimTokens', () => {
it('should be able to claim tokens', async function() {
const contract = this.proxyContract
Expand Down
3 changes: 0 additions & 3 deletions test/amb_erc677_to_erc677/foreign_bridge.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,6 @@ contract('ForeignAMBErc677ToErc677', async accounts => {
// Then
const events = await getEvents(ambBridgeContract, { event: 'UserRequestForAffirmation' })
expect(events.length).to.be.equal(1)
const data = `0x${events[0].returnValues.encodedData.slice(2 + 40 + 40 + 66)}`
const hash = web3.utils.soliditySha3(data)
expect(await foreignBridge.messageHashRecipient(hash)).to.be.equal(user) // should keep message sender instead of alternative receiver messageHashRecipient
expect(events[0].returnValues.encodedData.includes(strip0x(user2).toLowerCase())).to.be.equal(true)
expect(await foreignBridge.totalSpentPerDay(currentDay)).to.be.bignumber.equal(halfEther)
})
Expand Down

0 comments on commit 5d3899f

Please sign in to comment.