Skip to content

Commit

Permalink
unify error code and fix failed unittests
Browse files Browse the repository at this point in the history
  • Loading branch information
HaoyangLiu committed Jun 15, 2020
1 parent 6fcefa1 commit 9b6e096
Show file tree
Hide file tree
Showing 9 changed files with 349 additions and 492 deletions.
19 changes: 9 additions & 10 deletions contracts/BSCValidatorSet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,10 @@ contract BSCValidatorSet is IBSCValidatorSet, System, IParamSubscriber, IApplica

bytes public constant INIT_VALIDATORSET_BYTES = hex"f84580f842f840949fb29aac15b9a4b7f17c3385939b007540f4d791949fb29aac15b9a4b7f17c3385939b007540f4d791949fb29aac15b9a4b7f17c3385939b007540f4d79164";

uint32 public constant CODE_OK = 0;
uint32 public constant ERROR_FAIL_DECODE = 101;
uint32 public constant ERROR_UNKNOWN_PACKAGE_TYPE = 102;
uint32 public constant ERROR_FAIL_CHECK_VALIDATORS = 103;
uint32 public constant ERROR_LEN_OF_VAL_MISMATCH = 104;
uint32 public constant ERROR_RELAYFEE_TOO_LARGE = 105;
uint32 public constant ERROR_UNKNOWN_PACKAGE_TYPE = 101;
uint32 public constant ERROR_FAIL_CHECK_VALIDATORS = 102;
uint32 public constant ERROR_LEN_OF_VAL_MISMATCH = 103;
uint32 public constant ERROR_RELAYFEE_TOO_LARGE = 104;


/*********************** state of the contract **************************/
Expand Down Expand Up @@ -88,6 +86,7 @@ contract BSCValidatorSet is IBSCValidatorSet, System, IParamSubscriber, IApplica
event validatorMisdemeanor(address indexed validator, uint256 amount);
event validatorFelony(address indexed validator, uint256 amount);
event failReasonWithStr(string message);
event unexpectedPackage(uint8 channelId, bytes msgBytes);
event paramChange(string key, bytes value);

/*********************** init **************************/
Expand Down Expand Up @@ -123,14 +122,14 @@ contract BSCValidatorSet is IBSCValidatorSet, System, IParamSubscriber, IApplica
}
}

function handleAckPackage(uint8, bytes calldata) external onlyCrossChainContract override {
function handleAckPackage(uint8 channelId, bytes calldata msgBytes) external onlyCrossChainContract override {
// should not happen
require(false, "receive unexpected ack package");
emit unexpectedPackage(channelId, msgBytes);
}

function handleFailAckPackage(uint8, bytes calldata) external onlyCrossChainContract override {
function handleFailAckPackage(uint8 channelId, bytes calldata msgBytes) external onlyCrossChainContract override {
// should not happen
require(false, "receive unexpected fail ack package");
emit unexpectedPackage(channelId, msgBytes);
}

/*********************** External Functions **************************/
Expand Down
19 changes: 9 additions & 10 deletions contracts/BSCValidatorSet.template
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,10 @@ contract BSCValidatorSet is IBSCValidatorSet, System, IParamSubscriber, IApplica

bytes public constant INIT_VALIDATORSET_BYTES = hex"{{initValidatorSetBytes}}";

uint32 public constant CODE_OK = 0;
uint32 public constant ERROR_FAIL_DECODE = 101;
uint32 public constant ERROR_UNKNOWN_PACKAGE_TYPE = 102;
uint32 public constant ERROR_FAIL_CHECK_VALIDATORS = 103;
uint32 public constant ERROR_LEN_OF_VAL_MISMATCH = 104;
uint32 public constant ERROR_RELAYFEE_TOO_LARGE = 105;
uint32 public constant ERROR_UNKNOWN_PACKAGE_TYPE = 101;
uint32 public constant ERROR_FAIL_CHECK_VALIDATORS = 102;
uint32 public constant ERROR_LEN_OF_VAL_MISMATCH = 103;
uint32 public constant ERROR_RELAYFEE_TOO_LARGE = 104;


/*********************** state of the contract **************************/
Expand Down Expand Up @@ -88,6 +86,7 @@ contract BSCValidatorSet is IBSCValidatorSet, System, IParamSubscriber, IApplica
event validatorMisdemeanor(address indexed validator, uint256 amount);
event validatorFelony(address indexed validator, uint256 amount);
event failReasonWithStr(string message);
event unexpectedPackage(uint8 channelId, bytes msgBytes);
event paramChange(string key, bytes value);

/*********************** init **************************/
Expand Down Expand Up @@ -123,14 +122,14 @@ contract BSCValidatorSet is IBSCValidatorSet, System, IParamSubscriber, IApplica
}
}

function handleAckPackage(uint8, bytes calldata) external onlyCrossChainContract override {
function handleAckPackage(uint8 channelId, bytes calldata msgBytes) external onlyCrossChainContract override {
// should not happen
require(false, "receive unexpected ack package");
emit unexpectedPackage(channelId, msgBytes);
}

function handleFailAckPackage(uint8, bytes calldata) external onlyCrossChainContract override {
function handleFailAckPackage(uint8 channelId, bytes calldata msgBytes) external onlyCrossChainContract override {
// should not happen
require(false, "receive unexpected fail ack package");
emit unexpectedPackage(channelId, msgBytes);
}

/*********************** External Functions **************************/
Expand Down
2 changes: 0 additions & 2 deletions contracts/GovHub.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@ contract GovHub is System, IApplication{

uint8 public constant PARAM_UPDATE_MESSAGE_TYPE = 0;

uint32 public constant CODE_OK = 0;
uint32 public constant ERROR_TARGET_NOT_CONTRACT = 101;
uint32 public constant ERROR_TARGET_CONTRACT_FAIL = 102;
uint32 public constant ERROR_FAIL_DECODE = 103;

event failReasonWithStr(string message);
event failReasonWithBytes(bytes message);
Expand Down
3 changes: 3 additions & 0 deletions contracts/System.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ contract System {

bool public alreadyInit;

uint32 public constant CODE_OK = 0;
uint32 public constant ERROR_FAIL_DECODE = 100;

uint8 constant public BIND_CHANNELID = 0x01;
uint8 constant public TRANSFER_IN_CHANNELID = 0x02;
uint8 constant public TRANSFER_OUT_CHANNELID = 0x03;
Expand Down
3 changes: 3 additions & 0 deletions contracts/System.template
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ contract System {

bool public alreadyInit;

uint32 public constant CODE_OK = 0;
uint32 public constant ERROR_FAIL_DECODE = 100;

uint8 constant public BIND_CHANNELID = 0x01;
uint8 constant public TRANSFER_IN_CHANNELID = 0x02;
uint8 constant public TRANSFER_OUT_CHANNELID = 0x03;
Expand Down
93 changes: 51 additions & 42 deletions contracts/TokenHub.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import "./interface/ISystemReward.sol";
import "./lib/SafeMath.sol";
import "./rlp/RLPEncode.sol";
import "./rlp/RLPDecode.sol";
import "./rlp/CmnPkg.sol";
import "./System.sol";

contract TokenHub is ITokenHub, System, IParamSubscriber, IApplication, ISystemReward {
Expand Down Expand Up @@ -79,20 +80,24 @@ contract TokenHub is ITokenHub, System, IParamSubscriber, IApplication, ISystemR
uint32 status;
}

uint8 constant public BIND_PACKAGE = 0x00;
uint8 constant public UNBIND_PACKAGE = 0x01;
uint8 constant public BIND_PACKAGE = 0;
uint8 constant public UNBIND_PACKAGE = 1;

uint8 constant public BIND_STATUS_SUCCESS = 0x00;
uint8 constant public BIND_STATUS_TIMEOUT = 0x01;
uint8 constant public BIND_STATUS_INCORRECT_PARAMETERS = 0x02;
uint8 constant public BIND_STATUS_REJECTED = 0x03;
// bind channel error code
uint32 constant public ERROR_BIND_TIMEOUT = 1;
uint32 constant public ERROR_BIND_INCORRECT_PARAMETERS = 2;
uint32 constant public ERROR_BIND_REJECTED = 3;
uint32 constant public ERROR_BIND_UNKNOWN_PACKAGE_TYPE = 4;

uint8 constant public TRANSFER_IN_SUCCESS = 0x00;
uint8 constant public TRANSFER_IN_FAILURE_TIMEOUT = 0x01;
uint8 constant public TRANSFER_IN_FAILURE_UNBOUND_TOKEN = 0x02;
uint8 constant public TRANSFER_IN_FAILURE_INSUFFICIENT_BALANCE = 0x03;
uint8 constant public TRANSFER_IN_FAILURE_NON_PAYABLE_RECIPIENT = 0x04;
uint8 constant public TRANSFER_IN_FAILURE_UNKNOWN = 0x05;
// transfer in channel error code
uint32 constant public ERROR_TRANSFER_IN_TIMEOUT = 1;
uint32 constant public ERROR_TRANSFER_IN_UNBOUND_TOKEN = 2;
uint32 constant public ERROR_TRANSFER_IN_INSUFFICIENT_BALANCE = 3;
uint32 constant public ERROR_TRANSFER_IN_NON_PAYABLE_RECIPIENT = 4;
uint32 constant public ERROR_TRANSFER_IN_UNKNOWN = 5;

// channel error code
uint32 constant public ERROR_UNKNOWN_CHANNEL_ID = 101;

uint8 constant public BIND_CHANNEL_ID = 0x01;
uint8 constant public TRANSFER_IN_CHANNEL_ID = 0x02;
Expand All @@ -118,6 +123,7 @@ contract TokenHub is ITokenHub, System, IParamSubscriber, IApplication, ISystemR
event refundSuccess(address bep2eAddr, address refundAddr, uint256 amount);
event transferOutSuccess();
event rewardTo(address to, uint256 amount);
event unexpectedPackage(uint8 channelId, bytes msgBytes);

event LogUnexpectedRevertInBEP2E(address indexed contractAddr, string reason);
event LogUnexpectedFailureAssertionInBEP2E(address indexed contractAddr, bytes lowLevelData);
Expand Down Expand Up @@ -150,20 +156,24 @@ contract TokenHub is ITokenHub, System, IParamSubscriber, IApplication, ISystemR
return handleBindSyncPackage(msgBytes);
} else if (channelId == TRANSFER_IN_CHANNELID) {
return handleTransferInSyncPackage(msgBytes);
} else {
emit unexpectedPackage(channelId, msgBytes);
return CmnPkg.encodeCommonAckPackage(ERROR_UNKNOWN_CHANNEL_ID);
}
return new bytes(0);
}

function handleAckPackage(uint8 channelId, bytes calldata msgBytes) onlyInit onlyCrossChainContract external override {
if (channelId == BIND_CHANNELID) {
handleApproveBindAckPackage(msgBytes);
} else if (channelId == TRANSFER_OUT_CHANNELID) {
handleTransferOutAckPackage(msgBytes);
} else {
emit unexpectedPackage(channelId, msgBytes);
}
}

function handleFailAckPackage(uint8 /* channelId */, bytes calldata /* msgBytes */) onlyInit onlyCrossChainContract external override {
return;
function handleFailAckPackage(uint8 channelId, bytes calldata msgBytes) onlyInit onlyCrossChainContract external override {
emit unexpectedPackage(channelId, msgBytes);
}

function decodeBindSyncPackage(bytes memory msgBytes) internal pure returns(BindSyncPackage memory, bool) {
Expand Down Expand Up @@ -197,7 +207,7 @@ contract TokenHub is ITokenHub, System, IParamSubscriber, IApplication, ISystemR
function handleBindSyncPackage(bytes memory msgBytes) onlyInit internal returns(bytes memory) {
(BindSyncPackage memory bindSyncPkg, bool success) = decodeBindSyncPackage(msgBytes);
if (!success) {
return msgBytes;
return CmnPkg.encodeCommonAckPackage(ERROR_FAIL_DECODE);
}
if (bindSyncPkg.packageType == BIND_PACKAGE) {
bindPackageRecord[bindSyncPkg.bep2TokenSymbol]=bindSyncPkg;
Expand All @@ -207,11 +217,10 @@ contract TokenHub is ITokenHub, System, IParamSubscriber, IApplication, ISystemR
delete contractAddrToBEP2Symbol[contractAddr];
delete bep2SymbolToContractAddr[bindSyncPkg.bep2TokenSymbol];
}
} else {
return CmnPkg.encodeCommonAckPackage(ERROR_BIND_UNKNOWN_PACKAGE_TYPE);
}
BindAckPackage memory bindAckPackage = BindAckPackage({
bep2TokenSymbol: bindSyncPkg.bep2TokenSymbol
});
return encodeBindAckPackage(bindAckPackage);
return new bytes(0);
}

function handleApproveBindAckPackage(bytes memory msgBytes) onlyInit internal {
Expand All @@ -238,7 +247,7 @@ contract TokenHub is ITokenHub, System, IParamSubscriber, IApplication, ISystemR
if (bindSyncPkg.expireTime<block.timestamp) {
delete bindPackageRecord[bep2TokenSymbol];
ApproveBindSyncPackage memory approveBindSyncPackage = ApproveBindSyncPackage({
status: BIND_STATUS_TIMEOUT,
status: ERROR_BIND_TIMEOUT,
bep2TokenSymbol: bep2TokenSymbol
});
ICrossChain(CROSS_CHAIN_CONTRACT_ADDR).sendPackage(BIND_CHANNELID, encodeApproveBindSyncPackage(approveBindSyncPackage), relayFee.div(1e10));
Expand All @@ -254,7 +263,7 @@ contract TokenHub is ITokenHub, System, IParamSubscriber, IApplication, ISystemR
decimals!=bindSyncPkg.bep2eDecimals) {
delete bindPackageRecord[bep2TokenSymbol];
ApproveBindSyncPackage memory approveBindSyncPackage = ApproveBindSyncPackage({
status: BIND_STATUS_INCORRECT_PARAMETERS,
status: ERROR_BIND_INCORRECT_PARAMETERS,
bep2TokenSymbol: bep2TokenSymbol
});
ICrossChain(CROSS_CHAIN_CONTRACT_ADDR).sendPackage(BIND_CHANNELID, encodeApproveBindSyncPackage(approveBindSyncPackage), relayFee.div(1e10));
Expand All @@ -267,7 +276,7 @@ contract TokenHub is ITokenHub, System, IParamSubscriber, IApplication, ISystemR

delete bindPackageRecord[bep2TokenSymbol];
ApproveBindSyncPackage memory approveBindSyncPackage = ApproveBindSyncPackage({
status: BIND_STATUS_SUCCESS,
status: CODE_OK,
bep2TokenSymbol: bep2TokenSymbol
});
ICrossChain(CROSS_CHAIN_CONTRACT_ADDR).sendPackage(BIND_CHANNELID, encodeApproveBindSyncPackage(approveBindSyncPackage), relayFee.div(1e10));
Expand All @@ -283,7 +292,7 @@ contract TokenHub is ITokenHub, System, IParamSubscriber, IApplication, ISystemR
require(msg.value == relayFee, "msg.value doesn't equal to syncRelayFee");
delete bindPackageRecord[bep2TokenSymbol];
ApproveBindSyncPackage memory approveBindSyncPackage = ApproveBindSyncPackage({
status: BIND_STATUS_REJECTED,
status: ERROR_BIND_REJECTED,
bep2TokenSymbol: bep2TokenSymbol
});
ICrossChain(CROSS_CHAIN_CONTRACT_ADDR).sendPackage(BIND_CHANNELID, encodeApproveBindSyncPackage(approveBindSyncPackage), relayFee.div(1e10));
Expand All @@ -298,7 +307,7 @@ contract TokenHub is ITokenHub, System, IParamSubscriber, IApplication, ISystemR
require(msg.value == relayFee, "msg.value doesn't equal to syncRelayFee");
delete bindPackageRecord[bep2TokenSymbol];
ApproveBindSyncPackage memory approveBindSyncPackage = ApproveBindSyncPackage({
status: BIND_STATUS_TIMEOUT,
status: ERROR_BIND_TIMEOUT,
bep2TokenSymbol: bep2TokenSymbol
});
ICrossChain(CROSS_CHAIN_CONTRACT_ADDR).sendPackage(BIND_CHANNELID, encodeApproveBindSyncPackage(approveBindSyncPackage), relayFee.div(1e10));
Expand Down Expand Up @@ -339,16 +348,16 @@ contract TokenHub is ITokenHub, System, IParamSubscriber, IApplication, ISystemR
function handleTransferInSyncPackage(bytes memory msgBytes) internal returns(bytes memory) {
(TransferInSyncPackage memory transInSyncPkg, bool success) = decodeTransferInSyncPackage(msgBytes);
if (!success) {
return msgBytes;
return CmnPkg.encodeCommonAckPackage(ERROR_FAIL_DECODE);
}
uint32 status = doTransferIn(transInSyncPkg);
if (status != TRANSFER_IN_SUCCESS) {
uint32 resCode = doTransferIn(transInSyncPkg);
if (resCode != CODE_OK) {
uint256 bep2Amount = convertToBep2Amount(transInSyncPkg.amount, bep2eContractDecimals[transInSyncPkg.contractAddr]);
TransferInRefundPackage memory transInAckPkg = TransferInRefundPackage({
bep2TokenSymbol: transInSyncPkg.bep2TokenSymbol,
refundAmount: bep2Amount,
refundAddr: transInSyncPkg.refundAddr,
status: status
status: resCode
});
return encodeTransferInRefundPackage(transInAckPkg);
} else {
Expand All @@ -359,45 +368,45 @@ contract TokenHub is ITokenHub, System, IParamSubscriber, IApplication, ISystemR
function doTransferIn(TransferInSyncPackage memory transInSyncPkg) internal returns (uint32) {
if (transInSyncPkg.contractAddr==address(0x0)) {
if (block.timestamp > transInSyncPkg.expireTime) {
return TRANSFER_IN_FAILURE_TIMEOUT;
return ERROR_TRANSFER_IN_TIMEOUT;
}
if (address(this).balance < transInSyncPkg.amount) {
return TRANSFER_IN_FAILURE_INSUFFICIENT_BALANCE;
return ERROR_TRANSFER_IN_INSUFFICIENT_BALANCE;
}
if (!transInSyncPkg.recipient.send(transInSyncPkg.amount)) {
return TRANSFER_IN_FAILURE_NON_PAYABLE_RECIPIENT;
return ERROR_TRANSFER_IN_NON_PAYABLE_RECIPIENT;
}
return TRANSFER_IN_SUCCESS;
return CODE_OK;
} else {
if (block.timestamp > transInSyncPkg.expireTime) {
return TRANSFER_IN_FAILURE_TIMEOUT;
return ERROR_TRANSFER_IN_TIMEOUT;
}
if (contractAddrToBEP2Symbol[transInSyncPkg.contractAddr]!= transInSyncPkg.bep2TokenSymbol) {
return TRANSFER_IN_FAILURE_UNBOUND_TOKEN;
return ERROR_TRANSFER_IN_UNBOUND_TOKEN;
}
try IBEP2E(transInSyncPkg.contractAddr).balanceOf{gas: MAX_GAS_FOR_CALLING_BEP2E}(address(this)) returns (uint256 actualBalance) {
if (actualBalance < transInSyncPkg.amount) {
return TRANSFER_IN_FAILURE_INSUFFICIENT_BALANCE;
return ERROR_TRANSFER_IN_INSUFFICIENT_BALANCE;
}
} catch Error(string memory reason) {
emit LogUnexpectedRevertInBEP2E(transInSyncPkg.contractAddr, reason);
return TRANSFER_IN_FAILURE_UNKNOWN;
return ERROR_TRANSFER_IN_UNKNOWN;
} catch (bytes memory lowLevelData) {
emit LogUnexpectedFailureAssertionInBEP2E(transInSyncPkg.contractAddr, lowLevelData);
return TRANSFER_IN_FAILURE_UNKNOWN;
return ERROR_TRANSFER_IN_UNKNOWN;
}
try IBEP2E(transInSyncPkg.contractAddr).transfer{gas: MAX_GAS_FOR_CALLING_BEP2E}(transInSyncPkg.recipient, transInSyncPkg.amount) returns (bool success) {
if (success){
return TRANSFER_IN_SUCCESS;
return CODE_OK;
} else {
return TRANSFER_IN_FAILURE_UNKNOWN;
return ERROR_TRANSFER_IN_UNKNOWN;
}
} catch Error(string memory reason) {
emit LogUnexpectedRevertInBEP2E(transInSyncPkg.contractAddr, reason);
return TRANSFER_IN_FAILURE_UNKNOWN;
return ERROR_TRANSFER_IN_UNKNOWN;
} catch (bytes memory lowLevelData) {
emit LogUnexpectedFailureAssertionInBEP2E(transInSyncPkg.contractAddr, lowLevelData);
return TRANSFER_IN_FAILURE_UNKNOWN;
return ERROR_TRANSFER_IN_UNKNOWN;
}
}
}
Expand Down
Loading

0 comments on commit 9b6e096

Please sign in to comment.