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

Commit

Permalink
Forward nested revert messages in RedemptionScript
Browse files Browse the repository at this point in the history
  • Loading branch information
liamzebedee committed Apr 22, 2020
1 parent 2c740e7 commit d0e7a33
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
19 changes: 14 additions & 5 deletions solidity/contracts/scripts/FundingScript.sol
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,20 @@ contract FundingScript {
// Call the VendingMachine.
// We could explictly encode the call to vending machine, but this would
// involve manually parsing _extraData and allocating variables.
(bool success, bytes memory returnData) = address(vendingMachine).call(
// solium-disable-previous-line security/no-low-level-calls
_extraData
);
require(success, string(returnData));
// We capture the `returnData` in order to forward any nested revert message
// from the contract call.
(bool success, bytes memory returnData) = address(vendingMachine).call(_extraData);

string memory revertMessage;
assembly {
// A revert message is ABI-encoded as a call to Error(string).
// Slicing the Error() signature (4 bytes) and Data offset (4 bytes)
// leaves us with a pre-encoded string.
// We also slice off the ABI-coded length of returnData (32).
revertMessage := add(returnData, 0x44)
}

require(success, revertMessage);

// Transfer the TBTC and feeRebateToken to the user.
tbtcToken.transfer(_from, tbtcToken.balanceOf(address(this)));
Expand Down
1 change: 1 addition & 0 deletions solidity/contracts/scripts/RedemptionScript.sol
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ contract RedemptionScript {

// We capture the `returnData` in order to forward any nested revert message
// from the contract call.
// solium-disable-next-line security/no-low-level-calls
(bool success, bytes memory returnData) = address(vendingMachine).call(_extraData);

string memory revertMessage;
Expand Down
33 changes: 33 additions & 0 deletions solidity/test/VendingMachineTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,39 @@ describe("VendingMachine", async function() {
expect(success).to.be.true
})

it("forwards nested revert error messages", async () => {
const unqualifiedDepositToTbtcABI = vendingMachine.abi.filter(
x => x.name == "unqualifiedDepositToTbtc",
)[0]
const calldata = web3.eth.abi.encodeFunctionCall(
unqualifiedDepositToTbtcABI,
[
testDeposit.address,
_version,
_txInputVector,
_txOutputVector,
_txLocktime,
_fundingOutputIndex,
_merkleProof,
_txIndexInBlock,
_bitcoinHeaders,
],
)

// To make the funding call fail.
await testDeposit.setState(states.ACTIVE)

await expectRevert(
tbtcDepositToken.approveAndCall.call(
fundingScript.address,
tdtId,
calldata,
{from: owner},
),
"Not awaiting funding",
)
})

it("reverts for unknown function calls encoded in _extraData", async () => {
const unknownFunctionSignature = "0xCAFEBABE"

Expand Down

0 comments on commit d0e7a33

Please sign in to comment.