Skip to content

Commit

Permalink
Merge pull request #277 from tronprotocol/develop
Browse files Browse the repository at this point in the history
1.0.3 merge develop to master
  • Loading branch information
taihaofu authored Apr 28, 2020
2 parents efe407d + 38fd95b commit 1207e47
Show file tree
Hide file tree
Showing 562 changed files with 7,124 additions and 757 deletions.
2 changes: 1 addition & 1 deletion dapp-chain/contract/common/versionable/Versionable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ contract Versionable {

string public initVersion = "1.0.1";//do not modify

string public codeVersion = "1.0.1";
string public codeVersion = "1.0.3";

event ChangeVersion(string oldVersion, string newVersion);

Expand Down
129 changes: 98 additions & 31 deletions dapp-chain/contract/mainChain/MainChainGateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ contract MainChainGateway is OracleManagerContract {

event TRXReceived(address from, uint64 value, uint256 nonce);
event TRC10Received(address from, uint64 tokenId, uint64 tokenValue, uint256 nonce);
event TRC20Received(address from, address contractAddress, uint64 value, uint256 nonce);
event TRC20Received(address from, address contractAddress, uint256 value, uint256 nonce);
event TRC721Received(address from, address contractAddress, uint256 uid, uint256 nonce);
event TRC20Mapping(address contractAddress, uint256 nonce);
event TRC721Mapping(address contractAddress, uint256 nonce);
Expand All @@ -41,6 +41,9 @@ contract MainChainGateway is OracleManagerContract {
MappingMsg[] userMappingList;
uint256 uint64Max = 18446744073709551615;

address payable public refGatewayAddress;
uint256 public nonceBaseValue = 100000000000000000000; // 1*(10**20)

struct DepositMsg {
address user;
uint64 value;
Expand Down Expand Up @@ -128,9 +131,30 @@ contract MainChainGateway is OracleManagerContract {

// Approve and Deposit function for 2-step deposits
// Requires first to have called `approve` on the specified TRC20 contract
// version2 depositTRC20 function with uint256 as input value
function depositTRC20(address contractAddress, uint256 value) payable
public goDelegateCall onlyNotStop onlyNotPause isHuman returns (uint256) {
require(isMapAddrInGateways(contractAddress), "not an allowed token");
require(value >= depositMinTrc20, "value must be >= depositMinTrc20");
require(msg.value >= depositFee, "msg.value need >= depositFee");
if (msg.value > depositFee) {
msg.sender.transfer(msg.value - depositFee);
}
bonus += depositFee;

if (!TRC20(contractAddress).transferFrom(msg.sender, address(this), value)) {
revert("TRC20 transferFrom error");
}
userDepositList.push(DepositMsg(msg.sender, 0, 2, contractAddress, 0, 0, value));
emit TRC20Received(msg.sender, contractAddress, value, nonceBaseValue + userDepositList.length - 1);
return nonceBaseValue + userDepositList.length - 1;

}

// version1 depositTRC20 function with uint64 as input value
function depositTRC20(address contractAddress, uint64 value) payable
public goDelegateCall onlyNotStop onlyNotPause isHuman returns (uint256) {
require(mainToSideContractMap[contractAddress] == 1, "not an allowed token");
require(isMapAddrInGateways(contractAddress), "not an allowed token");
require(value >= depositMinTrc20, "value must be >= depositMinTrc20");
require(msg.value >= depositFee, "msg.value need >= depositFee");
if (msg.value > depositFee) {
Expand All @@ -141,27 +165,37 @@ contract MainChainGateway is OracleManagerContract {
if (!TRC20(contractAddress).transferFrom(msg.sender, address(this), value)) {
revert("TRC20 transferFrom error");
}
userDepositList.push(DepositMsg(msg.sender, value, 2, contractAddress, 0, 0, 0));
emit TRC20Received(msg.sender, contractAddress, value, userDepositList.length - 1);
return userDepositList.length - 1;
userDepositList.push(DepositMsg(msg.sender, 0, 2, contractAddress, 0, 0, value));
emit TRC20Received(msg.sender, contractAddress, value, nonceBaseValue + userDepositList.length - 1);
return nonceBaseValue + userDepositList.length - 1;

}

function getDepositMsg(uint256 nonce) view public returns (address, uint256, uint256, address, uint256, uint256, uint256){
DepositMsg memory _depositMsg = userDepositList[nonce];
return (_depositMsg.user, uint256(_depositMsg.value), uint256(_depositMsg._type), _depositMsg.mainChainAddress,
uint256(_depositMsg.tokenId), uint256(_depositMsg.status), uint256(_depositMsg.uId));

if (nonce >= nonceBaseValue) {
DepositMsg memory _depositMsg = userDepositList[nonce - nonceBaseValue];
return (_depositMsg.user, uint256(_depositMsg.value), uint256(_depositMsg._type), _depositMsg.mainChainAddress,
uint256(_depositMsg.tokenId), uint256(_depositMsg.status), uint256(_depositMsg.uId));
}
else {
return MainChainGateway(refGatewayAddress).getDepositMsg(nonce);
}
}

function getMappingMsg(uint256 nonce) view public returns (address, uint256, uint256){
MappingMsg memory _mappingMsg = userMappingList[nonce];
return (_mappingMsg.mainChainAddress, uint256(_mappingMsg._type), uint256(_mappingMsg.status));
if (nonce >= nonceBaseValue) {
MappingMsg memory _mappingMsg = userMappingList[nonce - nonceBaseValue];
return (_mappingMsg.mainChainAddress, uint256(_mappingMsg._type), uint256(_mappingMsg.status));
}
else {
return MainChainGateway(refGatewayAddress).getMappingMsg(nonce);
}

}

function depositTRC721(address contractAddress, uint256 uid) payable
public goDelegateCall onlyNotStop onlyNotPause isHuman returns (uint256) {
require(mainToSideContractMap[contractAddress] == 1, "not an allowed token");
require(isMapAddrInGateways(contractAddress), "not an allowed token");
require(msg.value >= depositFee, "msg.value need >= depositFee");
if (msg.value > depositFee) {
msg.sender.transfer(msg.value - depositFee);
Expand All @@ -170,8 +204,8 @@ contract MainChainGateway is OracleManagerContract {

TRC721(contractAddress).transferFrom(msg.sender, address(this), uid);
userDepositList.push(DepositMsg(msg.sender, 0, 3, contractAddress, 0, 0, uid));
emit TRC721Received(msg.sender, contractAddress, uid, userDepositList.length - 1);
return userDepositList.length - 1;
emit TRC721Received(msg.sender, contractAddress, uid, nonceBaseValue + userDepositList.length - 1);
return nonceBaseValue + userDepositList.length - 1;
}

function depositTRX() payable public goDelegateCall onlyNotStop onlyNotPause isHuman returns (uint256) {
Expand All @@ -181,8 +215,8 @@ contract MainChainGateway is OracleManagerContract {
require(value >= depositMinTrx && value <= uint64Max, "must between depositMinTrx and uint64Max");

userDepositList.push(DepositMsg(msg.sender, uint64(value), 0, address(0), 0, 0, 0));
emit TRXReceived(msg.sender, uint64(value), userDepositList.length - 1);
return userDepositList.length - 1;
emit TRXReceived(msg.sender, uint64(value), nonceBaseValue + userDepositList.length - 1);
return nonceBaseValue + userDepositList.length - 1;
}

function depositTRC10(uint64 tokenId, uint64 tokenValue) payable public checkForTrc10(tokenId, tokenValue)
Expand All @@ -196,12 +230,14 @@ contract MainChainGateway is OracleManagerContract {
require(uint256(tokenId) <= uint64Max, "tokenId must <= uint64Max");
require(tokenValue <= uint64Max, "tokenValue must <= uint64Max");
userDepositList.push(DepositMsg(msg.sender, tokenValue, 1, address(0), tokenId, 0, 0));
emit TRC10Received(msg.sender, tokenId, tokenValue, userDepositList.length - 1);
return userDepositList.length - 1;
emit TRC10Received(msg.sender, tokenId, tokenValue, nonceBaseValue + userDepositList.length - 1);
return nonceBaseValue + userDepositList.length - 1;
}

function() payable external goDelegateCall onlyNotStop onlyNotPause {
revert("not allow function fallback");
if (msg.sender != refGatewayAddress) {
revert("not allow function fallback");
}
}

function mappingTRC20(bytes memory txId) payable public goDelegateCall onlyNotStop onlyNotPause isHuman returns (uint256) {
Expand All @@ -212,14 +248,14 @@ contract MainChainGateway is OracleManagerContract {
bonus += mappingFee;
address trc20Address = calcContractAddress(txId, msg.sender);
require(trc20Address != sunTokenAddress, "mainChainAddress == sunTokenAddress");
require(mainToSideContractMap[trc20Address] != 1, "trc20Address mapped");
require(!isMapAddrInGateways(trc20Address), "trc20Address mapped");
uint256 size;
assembly {size := extcodesize(trc20Address)}
require(size > 0);
userMappingList.push(MappingMsg(trc20Address, DataModel.TokenKind.TRC20, DataModel.Status.SUCCESS));
mainToSideContractMap[trc20Address] = 1;
emit TRC20Mapping(trc20Address, userMappingList.length - 1);
return userMappingList.length - 1;
emit TRC20Mapping(trc20Address, nonceBaseValue + userMappingList.length - 1);
return nonceBaseValue + userMappingList.length - 1;
}

// 2. deployDAppTRC721AndMapping
Expand All @@ -231,32 +267,33 @@ contract MainChainGateway is OracleManagerContract {
bonus += mappingFee;
address trc721Address = calcContractAddress(txId, msg.sender);
require(trc721Address != sunTokenAddress, "mainChainAddress == sunTokenAddress");
require(mainToSideContractMap[trc721Address] != 1, "trc721Address mapped");
require(!isMapAddrInGateways(trc721Address), "trc721Address mapped");
uint256 size;
assembly {size := extcodesize(trc721Address)}
require(size > 0);
userMappingList.push(MappingMsg(trc721Address, DataModel.TokenKind.TRC721, DataModel.Status.SUCCESS));
mainToSideContractMap[trc721Address] = 1;
emit TRC721Mapping(trc721Address, userMappingList.length - 1);
return userMappingList.length - 1;
emit TRC721Mapping(trc721Address, nonceBaseValue + userMappingList.length - 1);
return nonceBaseValue + userMappingList.length - 1;
}

function retryDeposit(uint256 nonce) payable public goDelegateCall onlyNotStop onlyNotPause isHuman {
require(msg.value >= retryFee, "msg.value need >= retryFee");
require(nonce >= nonceBaseValue, "nonce should not < nonceBaseValue");
require(nonce < nonceBaseValue + userDepositList.length, "nonce >= nonceBaseValue + userDepositList.length");
if (msg.value > retryFee) {
msg.sender.transfer(msg.value - retryFee);
}
bonus += retryFee;
require(nonce < userDepositList.length, "nonce >= userDepositList.length");
DepositMsg storage depositMsg = userDepositList[nonce];
DepositMsg storage depositMsg = userDepositList[nonce - nonceBaseValue];
// TRX, // 0
// TRC10, // 1
// TRC20, // 2
// TRC721, // 3
if (depositMsg._type == 0) {
emit TRXReceived(depositMsg.user, depositMsg.value, nonce);
} else if (depositMsg._type == 2) {
emit TRC20Received(depositMsg.user, depositMsg.mainChainAddress, depositMsg.value, nonce);
emit TRC20Received(depositMsg.user, depositMsg.mainChainAddress, depositMsg.uId, nonce);
} else if (depositMsg._type == 3) {
emit TRC721Received(depositMsg.user, depositMsg.mainChainAddress, depositMsg.uId, nonce);
} else {
Expand All @@ -266,12 +303,13 @@ contract MainChainGateway is OracleManagerContract {

function retryMapping(uint256 nonce) payable public goDelegateCall onlyNotStop onlyNotPause isHuman {
require(msg.value >= retryFee, "msg.value need >= retryFee");
require(nonce >= nonceBaseValue, "nonce should not < nonceBaseValue");
require(nonce < nonceBaseValue + userMappingList.length, "nonce >= nonceBaseValue + userMappingList.length");
if (msg.value > retryFee) {
msg.sender.transfer(msg.value - retryFee);
}
bonus += retryFee;
require(nonce < userMappingList.length, "nonce >= userMappingList.length");
MappingMsg storage mappingMsg = userMappingList[nonce];
MappingMsg storage mappingMsg = userMappingList[nonce - nonceBaseValue];
require(mappingMsg.status == DataModel.Status.SUCCESS, "mappingMsg.status != SUCCESS ");

if (mappingMsg._type == DataModel.TokenKind.TRC20) {
Expand Down Expand Up @@ -313,7 +351,17 @@ contract MainChainGateway is OracleManagerContract {

// Returns all the TRC20
function getTRC20(address contractAddress) external view returns (uint256) {
return TRC20(contractAddress).balanceOf(contractAddress);
return TRC20(contractAddress).balanceOf(address(this));
}

function getBonus() external view returns(uint256) {
// TODO:
// Changed in next version to make it recursively as:
// MainChainGateway(refGatewayAddress).getBonus() + bonus
if(refGatewayAddress == address(0))
return bonus;
else
return MainChainGateway(refGatewayAddress).bonus() + bonus;
}

// Returns TRC721 token by uid
Expand Down Expand Up @@ -353,4 +401,23 @@ contract MainChainGateway is OracleManagerContract {
depositMinTrc20 = minValue;
}

function setRefGatewayAddress(address payable ref) public goDelegateCall onlyOwner {
refGatewayAddress = ref;
}

function isMapAddrInGateways(address trcAddress) public goDelegateCall returns (bool){
if (mainToSideContractMap[trcAddress] == 1) {
return true;
}
if (refGatewayAddress == address(0)) {
return false;
}
// TODO: In next version we should use gatewayPeers[i].isMapAddrInGateways(trcAddress) here
if (MainChainGateway(refGatewayAddress).mainToSideContractMap(trcAddress) == 1) {
mainToSideContractMap[trcAddress] = 1;
return true;
}
return false;
}

}
14 changes: 14 additions & 0 deletions dapp-chain/contract/v1.0.2/common/DataModel.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
contract DataModel {
enum TokenKind {
TRX, // 0
TRC10, // 1
TRC20, // 2
TRC721 // 3
}
enum Status {
SUCCESS, // 0
LOCKING, // 1
FAIL, // 2
REFUNDED // 3
}
}
21 changes: 21 additions & 0 deletions dapp-chain/contract/v1.0.2/common/ECVerify.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
library ECVerify {

function recover(bytes32 hash, bytes memory signature) internal pure returns (address) {
uint8 v;
bytes32 r;
bytes32 s;
assembly {
r := mload(add(signature, 32))
s := mload(add(signature, 64))
v := and(mload(add(signature, 65)), 255)
}
if (v < 27) {
v += 27;
}
return ecrecover(hash, v, r, s);
}

function ecverify(bytes32 hash, bytes memory sig, address signer) internal pure returns (bool) {
return signer == recover(hash, sig);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import "../versionable/Versionable.sol";

contract Delegatecallable is Versionable {
address public logicAddress;

event DelegateResult(bool result, bytes msg);
event LogicAddressChanged(address oldAddress, address newAddress);
modifier goDelegateCall {
if (logicAddress != address(0)) {
(bool result,bytes memory mesg) = logicAddress.delegatecall(msg.data);
if (!result) {
revert(string(mesg));
}
emit DelegateResult(result, mesg);
return;
}
_;
}
function changeLogicAddress(address newLogicAddress) internal {
string memory newVersion;
if (newLogicAddress == address(0)) {
newVersion = initVersion;
} else {
newVersion = Versionable(newLogicAddress).getCodeVersion();
}
emit ChangeVersion(codeVersion, newVersion);
emit LogicAddressChanged(logicAddress, newLogicAddress);
codeVersion = newVersion;
logicAddress = newLogicAddress;
}
}
49 changes: 49 additions & 0 deletions dapp-chain/contract/v1.0.2/common/math/SafeMath.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* @title SafeMath
* @dev Math operations with safety checks that throw on error
*/
library SafeMath {

/**
* @dev Multiplies two numbers, throws on overflow.
*/
function mul(uint256 a, uint256 b) internal pure returns (uint256 c) {
// Gas optimization: this is cheaper than asserting 'a' not being zero, but the
// benefit is lost if 'b' is also tested.
// See: https://github.com/OpenZeppelin/openzeppelin-solidity/pull/522
if (a == 0) {
return 0;
}

c = a * b;
require(c / a == b);
return c;
}

/**
* @dev Integer division of two numbers, truncating the quotient.
*/
function div(uint256 a, uint256 b) internal pure returns (uint256) {
// assert(b > 0); // Solidity automatically throws when dividing by 0
// uint256 c = a / b;
// require(a == b * c + a % b); // There is no case in which this doesn't hold
return a / b;
}

/**
* @dev Subtracts two numbers, throws on overflow (i.e. if subtrahend is greater than minuend).
*/
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
require(b <= a);
return a - b;
}

/**
* @dev Adds two numbers, throws on overflow.
*/
function add(uint256 a, uint256 b) internal pure returns (uint256 c) {
c = a + b;
require(c >= a);
return c;
}
}
Loading

0 comments on commit 1207e47

Please sign in to comment.