Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

R4R: check sequence first to save gas for relayer #3

Merged
merged 1 commit into from
May 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions contracts/TokenHub.sol
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ contract TokenHub is ITokenHub, System{



modifier sequenceInOrder(uint64 sequence, uint64 expectedSequence) {
require(sequence == expectedSequence, "sequence not in order");
_;
}

function bep2TokenSymbolConvert(string memory symbol) public pure returns(bytes32) {
bytes32 result;
assembly {
Expand Down Expand Up @@ -168,8 +173,7 @@ contract TokenHub is ITokenHub, System{
return bindPackage;
}

function handleBindPackage(bytes calldata msgBytes, bytes calldata proof, uint64 height, uint64 packageSequence) blockSynced(height) onlyRelayer override external returns (bool) {
require(packageSequence==_bindChannelSequence, "wrong bind sequence");
function handleBindPackage(bytes calldata msgBytes, bytes calldata proof, uint64 height, uint64 packageSequence) sequenceInOrder(packageSequence, _bindChannelSequence) blockSynced(height) onlyRelayer override external returns (bool) {
require(msgBytes.length==157, "wrong bind package size");
bytes32 appHash = ILightClient(LIGHT_CLIENT_ADDR).getAppHash(height);
require(MerkleProof.validateMerkleProof(appHash, STORE_NAME, generateKey(_bindChannelSequence, BIND_CHANNEL_ID), msgBytes, proof), "invalid merkle proof");
Expand Down Expand Up @@ -338,8 +342,7 @@ contract TokenHub is ITokenHub, System{
return transferInPackage;
}

function handleTransferInPackage(bytes calldata msgBytes, bytes calldata proof, uint64 height, uint64 packageSequence) blockSynced(height) onlyRelayer override external returns (bool) {
require(packageSequence==_transferInChannelSequence, "wrong transfer sequence");
function handleTransferInPackage(bytes calldata msgBytes, bytes calldata proof, uint64 height, uint64 packageSequence) sequenceInOrder(packageSequence, _transferInChannelSequence) blockSynced(height) onlyRelayer override external returns (bool) {
require(msgBytes.length==164, "wrong transfer package size");
bytes32 appHash = ILightClient(LIGHT_CLIENT_ADDR).getAppHash(height);
require(MerkleProof.validateMerkleProof(appHash, STORE_NAME, generateKey(_transferInChannelSequence, TRANSFER_IN_CHANNEL_ID), msgBytes, proof), "invalid merkle proof");
Expand Down Expand Up @@ -446,8 +449,7 @@ contract TokenHub is ITokenHub, System{
return refundPackage;
}

function handleRefundPackage(bytes calldata msgBytes, bytes calldata proof, uint64 height, uint64 packageSequence) blockSynced(height) onlyRelayer override external returns (bool) {
require(packageSequence==_refundChannelSequence, "wrong refund sequence");
function handleRefundPackage(bytes calldata msgBytes, bytes calldata proof, uint64 height, uint64 packageSequence) sequenceInOrder(packageSequence, _refundChannelSequence) blockSynced(height) onlyRelayer override external returns (bool) {
require(msgBytes.length==74, "wrong refund package size");
bytes32 appHash = ILightClient(LIGHT_CLIENT_ADDR).getAppHash(height);
require(MerkleProof.validateMerkleProof(appHash, STORE_NAME, generateKey(_refundChannelSequence,REFUND_CHANNEL_ID), msgBytes, proof), "invalid merkle proof");
Expand Down
14 changes: 8 additions & 6 deletions contracts/TokenHub.template
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ contract TokenHub is ITokenHub, System{
{% else %}
{% endif %}

modifier sequenceInOrder(uint64 sequence, uint64 expectedSequence) {
require(sequence == expectedSequence, "sequence not in order");
_;
}

function bep2TokenSymbolConvert(string memory symbol) public pure returns(bytes32) {
bytes32 result;
assembly {
Expand Down Expand Up @@ -171,8 +176,7 @@ contract TokenHub is ITokenHub, System{
return bindPackage;
}

function handleBindPackage(bytes calldata msgBytes, bytes calldata proof, uint64 height, uint64 packageSequence) blockSynced(height) onlyRelayer override external returns (bool) {
require(packageSequence==_bindChannelSequence, "wrong bind sequence");
function handleBindPackage(bytes calldata msgBytes, bytes calldata proof, uint64 height, uint64 packageSequence) sequenceInOrder(packageSequence, _bindChannelSequence) blockSynced(height) onlyRelayer override external returns (bool) {
require(msgBytes.length==157, "wrong bind package size");
bytes32 appHash = ILightClient(LIGHT_CLIENT_ADDR).getAppHash(height);
require(MerkleProof.validateMerkleProof(appHash, STORE_NAME, generateKey(_bindChannelSequence, BIND_CHANNEL_ID), msgBytes, proof), "invalid merkle proof");
Expand Down Expand Up @@ -341,8 +345,7 @@ contract TokenHub is ITokenHub, System{
return transferInPackage;
}

function handleTransferInPackage(bytes calldata msgBytes, bytes calldata proof, uint64 height, uint64 packageSequence) blockSynced(height) onlyRelayer override external returns (bool) {
require(packageSequence==_transferInChannelSequence, "wrong transfer sequence");
function handleTransferInPackage(bytes calldata msgBytes, bytes calldata proof, uint64 height, uint64 packageSequence) sequenceInOrder(packageSequence, _transferInChannelSequence) blockSynced(height) onlyRelayer override external returns (bool) {
require(msgBytes.length==164, "wrong transfer package size");
bytes32 appHash = ILightClient(LIGHT_CLIENT_ADDR).getAppHash(height);
require(MerkleProof.validateMerkleProof(appHash, STORE_NAME, generateKey(_transferInChannelSequence, TRANSFER_IN_CHANNEL_ID), msgBytes, proof), "invalid merkle proof");
Expand Down Expand Up @@ -449,8 +452,7 @@ contract TokenHub is ITokenHub, System{
return refundPackage;
}

function handleRefundPackage(bytes calldata msgBytes, bytes calldata proof, uint64 height, uint64 packageSequence) blockSynced(height) onlyRelayer override external returns (bool) {
require(packageSequence==_refundChannelSequence, "wrong refund sequence");
function handleRefundPackage(bytes calldata msgBytes, bytes calldata proof, uint64 height, uint64 packageSequence) sequenceInOrder(packageSequence, _refundChannelSequence) blockSynced(height) onlyRelayer override external returns (bool) {
require(msgBytes.length==74, "wrong refund package size");
bytes32 appHash = ILightClient(LIGHT_CLIENT_ADDR).getAppHash(height);
require(MerkleProof.validateMerkleProof(appHash, STORE_NAME, generateKey(_refundChannelSequence,REFUND_CHANNEL_ID), msgBytes, proof), "invalid merkle proof");
Expand Down
2 changes: 1 addition & 1 deletion genesis.json

Large diffs are not rendered by default.