Skip to content

Commit

Permalink
refactor(plugin-htlc-eth-besu): stop using the deprecated api.encodeP…
Browse files Browse the repository at this point in the history
…acked

1. Add tests to vanilla HashTimeLock
2. Replace abi.encodePacked by abi.encode
3. Compile and replace generated ABIs (for HashTimeLock and PrivateHashTimeLock)

Co-authored-by: Peter Somogyvari <peter.somogyvari@accenture.com>

Signed-off-by: Rafael Belchior <rafael.belchior@tecnico.ulisboa.pt>
Signed-off-by: Peter Somogyvari <peter.somogyvari@accenture.com>
  • Loading branch information
RafaelAPB authored and petermetz committed Jul 18, 2023
1 parent aee28fe commit 67a66bb
Show file tree
Hide file tree
Showing 5 changed files with 7,555 additions and 355 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ contract HashTimeLock {

require(inputAmount > 0, "INVALID_AMOUNT");

bytes32 id = sha256(abi.encodePacked(sender, receiver, inputAmount, hashLock, expiration));
bytes32 id = keccak256(abi.encode(sender, receiver, inputAmount, hashLock, expiration));

require(contracts[id].status == INIT, "SWAP_EXISTS");

Expand All @@ -78,7 +78,7 @@ contract HashTimeLock {

require(c.expiration > block.timestamp, "INVALID_TIME");

require(c.hashLock == keccak256(abi.encodePacked(secret)), "INVALID_SECRET");
require(c.hashLock == keccak256(abi.encode(secret)), "INVALID_SECRET");

c.status = WITHDRAWN;

Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ contract PrivateHashTimeLock {

require(inputAmount > 0, "INVALID_AMOUNT");

bytes32 id = keccak256(abi.encodePacked(sender, receiver, inputAmount, hashLock, expiration));
bytes32 id = keccak256(abi.encode(sender, receiver, inputAmount, hashLock, expiration));

require(contracts[id].status == INIT, "SWAP_EXISTS");
require(priv.generator > 0);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
pragma solidity 0.8.19;

import {HashTimeLock} from "../../../main/solidity/contracts/HashTimeLock.sol";
import "forge-std/Test.sol";
import "forge-std/console2.sol";

contract HashTimeLockTest is Test {

bytes32 AliceSecret;
bytes32 HashedAliceSecret;
bytes32 Z;

function setUp() public {
AliceSecret = bytes32(0x0000000000000000000000000000000000000000000000000000000000000003);
// keccak256(abi.encodePacked(AliceSecret));
HashedAliceSecret = bytes32(0xc2575a0e9e593c00f959f8c92f12db2869c3395a3b0502d05e2516446f71f85b);
}

function test_Deployment() public {
new HashTimeLock();
}

function test_initializeHTLC() public {
// 5 eth
uint256 inputAmountEth = 5;
uint256 outputAmount = 5000000000000000000;
// 1/1/2030
uint256 expiration = 1893515539;
// account # 1 of anvil -a 10
address payable receiver = payable(0x70997970C51812dc3A010C7d01b50e0d17dc79C8);
string memory outputNetwork = "anvil";
string memory outputAddress = vm.toString(msg.sender);


HashTimeLock HtlcManager = new HashTimeLock();
console.log("Deployed HTLC: ", address(HtlcManager));

vm.expectCall(
address(HtlcManager),
5,
abi.encodeWithSelector(
HtlcManager.newContract.selector,
outputAmount,
expiration,
HashedAliceSecret,
receiver,
outputNetwork,
outputAddress
),
1
);

//vm.expectEmit(true, true, false, true, address(HtlcManager));
vm.recordLogs();

HtlcManager.newContract{value: inputAmountEth}(
outputAmount, expiration, HashedAliceSecret, receiver, outputNetwork, outputAddress
);

Vm.Log[] memory entries = vm.getRecordedLogs();

// get contract id from event
assertEq(entries.length, 1);
bytes32 id = entries[0].topics[1];

bool exists = HtlcManager.contractExists(id);
assert(exists);

// state is active
assert(HtlcManager.getSingleStatus(id) == 1);
}

function test_process_secret() public {
// 5 eth
uint256 inputAmountEth = 5;
uint256 outputAmount = 5000000000000000000;
// 1/1/2030
uint256 expiration = 1893515539;
// account # 1 of anvil -a 10
address payable receiver = payable(0x70997970C51812dc3A010C7d01b50e0d17dc79C8);
string memory outputNetwork = "anvil";
string memory outputAddress = vm.toString(msg.sender);


HashTimeLock HtlcManager = new HashTimeLock();
console.log("Deployed HTLC: ", address(HtlcManager));

vm.expectCall(
address(HtlcManager),
5,
abi.encodeWithSelector(
HtlcManager.newContract.selector,
outputAmount,
expiration,
HashedAliceSecret,
receiver,
outputNetwork,
outputAddress
),
1
);

//vm.expectEmit(true, true, false, true, address(HtlcManager));
vm.recordLogs();

HtlcManager.newContract{value: inputAmountEth}(
outputAmount, expiration, HashedAliceSecret, receiver, outputNetwork, outputAddress
);

Vm.Log[] memory entries = vm.getRecordedLogs();

// get contract id from event
assertEq(entries.length, 1);
bytes32 id = entries[0].topics[1];
assertEq(HashedAliceSecret, keccak256(abi.encode(AliceSecret)));
HtlcManager.withdraw(id, AliceSecret);
}

}

0 comments on commit 67a66bb

Please sign in to comment.