Skip to content

Commit 1207e47

Browse files
authored
Merge pull request #277 from tronprotocol/develop
1.0.3 merge develop to master
2 parents efe407d + 38fd95b commit 1207e47

File tree

562 files changed

+7124
-757
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

562 files changed

+7124
-757
lines changed

dapp-chain/contract/common/versionable/Versionable.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ contract Versionable {
22

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

5-
string public codeVersion = "1.0.1";
5+
string public codeVersion = "1.0.3";
66

77
event ChangeVersion(string oldVersion, string newVersion);
88

dapp-chain/contract/mainChain/MainChainGateway.sol

Lines changed: 98 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ contract MainChainGateway is OracleManagerContract {
1414

1515
event TRXReceived(address from, uint64 value, uint256 nonce);
1616
event TRC10Received(address from, uint64 tokenId, uint64 tokenValue, uint256 nonce);
17-
event TRC20Received(address from, address contractAddress, uint64 value, uint256 nonce);
17+
event TRC20Received(address from, address contractAddress, uint256 value, uint256 nonce);
1818
event TRC721Received(address from, address contractAddress, uint256 uid, uint256 nonce);
1919
event TRC20Mapping(address contractAddress, uint256 nonce);
2020
event TRC721Mapping(address contractAddress, uint256 nonce);
@@ -41,6 +41,9 @@ contract MainChainGateway is OracleManagerContract {
4141
MappingMsg[] userMappingList;
4242
uint256 uint64Max = 18446744073709551615;
4343

44+
address payable public refGatewayAddress;
45+
uint256 public nonceBaseValue = 100000000000000000000; // 1*(10**20)
46+
4447
struct DepositMsg {
4548
address user;
4649
uint64 value;
@@ -128,9 +131,30 @@ contract MainChainGateway is OracleManagerContract {
128131

129132
// Approve and Deposit function for 2-step deposits
130133
// Requires first to have called `approve` on the specified TRC20 contract
134+
// version2 depositTRC20 function with uint256 as input value
135+
function depositTRC20(address contractAddress, uint256 value) payable
136+
public goDelegateCall onlyNotStop onlyNotPause isHuman returns (uint256) {
137+
require(isMapAddrInGateways(contractAddress), "not an allowed token");
138+
require(value >= depositMinTrc20, "value must be >= depositMinTrc20");
139+
require(msg.value >= depositFee, "msg.value need >= depositFee");
140+
if (msg.value > depositFee) {
141+
msg.sender.transfer(msg.value - depositFee);
142+
}
143+
bonus += depositFee;
144+
145+
if (!TRC20(contractAddress).transferFrom(msg.sender, address(this), value)) {
146+
revert("TRC20 transferFrom error");
147+
}
148+
userDepositList.push(DepositMsg(msg.sender, 0, 2, contractAddress, 0, 0, value));
149+
emit TRC20Received(msg.sender, contractAddress, value, nonceBaseValue + userDepositList.length - 1);
150+
return nonceBaseValue + userDepositList.length - 1;
151+
152+
}
153+
154+
// version1 depositTRC20 function with uint64 as input value
131155
function depositTRC20(address contractAddress, uint64 value) payable
132156
public goDelegateCall onlyNotStop onlyNotPause isHuman returns (uint256) {
133-
require(mainToSideContractMap[contractAddress] == 1, "not an allowed token");
157+
require(isMapAddrInGateways(contractAddress), "not an allowed token");
134158
require(value >= depositMinTrc20, "value must be >= depositMinTrc20");
135159
require(msg.value >= depositFee, "msg.value need >= depositFee");
136160
if (msg.value > depositFee) {
@@ -141,27 +165,37 @@ contract MainChainGateway is OracleManagerContract {
141165
if (!TRC20(contractAddress).transferFrom(msg.sender, address(this), value)) {
142166
revert("TRC20 transferFrom error");
143167
}
144-
userDepositList.push(DepositMsg(msg.sender, value, 2, contractAddress, 0, 0, 0));
145-
emit TRC20Received(msg.sender, contractAddress, value, userDepositList.length - 1);
146-
return userDepositList.length - 1;
168+
userDepositList.push(DepositMsg(msg.sender, 0, 2, contractAddress, 0, 0, value));
169+
emit TRC20Received(msg.sender, contractAddress, value, nonceBaseValue + userDepositList.length - 1);
170+
return nonceBaseValue + userDepositList.length - 1;
147171

148172
}
149173

150174
function getDepositMsg(uint256 nonce) view public returns (address, uint256, uint256, address, uint256, uint256, uint256){
151-
DepositMsg memory _depositMsg = userDepositList[nonce];
152-
return (_depositMsg.user, uint256(_depositMsg.value), uint256(_depositMsg._type), _depositMsg.mainChainAddress,
153-
uint256(_depositMsg.tokenId), uint256(_depositMsg.status), uint256(_depositMsg.uId));
154-
175+
if (nonce >= nonceBaseValue) {
176+
DepositMsg memory _depositMsg = userDepositList[nonce - nonceBaseValue];
177+
return (_depositMsg.user, uint256(_depositMsg.value), uint256(_depositMsg._type), _depositMsg.mainChainAddress,
178+
uint256(_depositMsg.tokenId), uint256(_depositMsg.status), uint256(_depositMsg.uId));
179+
}
180+
else {
181+
return MainChainGateway(refGatewayAddress).getDepositMsg(nonce);
182+
}
155183
}
156184

157185
function getMappingMsg(uint256 nonce) view public returns (address, uint256, uint256){
158-
MappingMsg memory _mappingMsg = userMappingList[nonce];
159-
return (_mappingMsg.mainChainAddress, uint256(_mappingMsg._type), uint256(_mappingMsg.status));
186+
if (nonce >= nonceBaseValue) {
187+
MappingMsg memory _mappingMsg = userMappingList[nonce - nonceBaseValue];
188+
return (_mappingMsg.mainChainAddress, uint256(_mappingMsg._type), uint256(_mappingMsg.status));
189+
}
190+
else {
191+
return MainChainGateway(refGatewayAddress).getMappingMsg(nonce);
192+
}
193+
160194
}
161195

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

171205
TRC721(contractAddress).transferFrom(msg.sender, address(this), uid);
172206
userDepositList.push(DepositMsg(msg.sender, 0, 3, contractAddress, 0, 0, uid));
173-
emit TRC721Received(msg.sender, contractAddress, uid, userDepositList.length - 1);
174-
return userDepositList.length - 1;
207+
emit TRC721Received(msg.sender, contractAddress, uid, nonceBaseValue + userDepositList.length - 1);
208+
return nonceBaseValue + userDepositList.length - 1;
175209
}
176210

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

183217
userDepositList.push(DepositMsg(msg.sender, uint64(value), 0, address(0), 0, 0, 0));
184-
emit TRXReceived(msg.sender, uint64(value), userDepositList.length - 1);
185-
return userDepositList.length - 1;
218+
emit TRXReceived(msg.sender, uint64(value), nonceBaseValue + userDepositList.length - 1);
219+
return nonceBaseValue + userDepositList.length - 1;
186220
}
187221

188222
function depositTRC10(uint64 tokenId, uint64 tokenValue) payable public checkForTrc10(tokenId, tokenValue)
@@ -196,12 +230,14 @@ contract MainChainGateway is OracleManagerContract {
196230
require(uint256(tokenId) <= uint64Max, "tokenId must <= uint64Max");
197231
require(tokenValue <= uint64Max, "tokenValue must <= uint64Max");
198232
userDepositList.push(DepositMsg(msg.sender, tokenValue, 1, address(0), tokenId, 0, 0));
199-
emit TRC10Received(msg.sender, tokenId, tokenValue, userDepositList.length - 1);
200-
return userDepositList.length - 1;
233+
emit TRC10Received(msg.sender, tokenId, tokenValue, nonceBaseValue + userDepositList.length - 1);
234+
return nonceBaseValue + userDepositList.length - 1;
201235
}
202236

203237
function() payable external goDelegateCall onlyNotStop onlyNotPause {
204-
revert("not allow function fallback");
238+
if (msg.sender != refGatewayAddress) {
239+
revert("not allow function fallback");
240+
}
205241
}
206242

207243
function mappingTRC20(bytes memory txId) payable public goDelegateCall onlyNotStop onlyNotPause isHuman returns (uint256) {
@@ -212,14 +248,14 @@ contract MainChainGateway is OracleManagerContract {
212248
bonus += mappingFee;
213249
address trc20Address = calcContractAddress(txId, msg.sender);
214250
require(trc20Address != sunTokenAddress, "mainChainAddress == sunTokenAddress");
215-
require(mainToSideContractMap[trc20Address] != 1, "trc20Address mapped");
251+
require(!isMapAddrInGateways(trc20Address), "trc20Address mapped");
216252
uint256 size;
217253
assembly {size := extcodesize(trc20Address)}
218254
require(size > 0);
219255
userMappingList.push(MappingMsg(trc20Address, DataModel.TokenKind.TRC20, DataModel.Status.SUCCESS));
220256
mainToSideContractMap[trc20Address] = 1;
221-
emit TRC20Mapping(trc20Address, userMappingList.length - 1);
222-
return userMappingList.length - 1;
257+
emit TRC20Mapping(trc20Address, nonceBaseValue + userMappingList.length - 1);
258+
return nonceBaseValue + userMappingList.length - 1;
223259
}
224260

225261
// 2. deployDAppTRC721AndMapping
@@ -231,32 +267,33 @@ contract MainChainGateway is OracleManagerContract {
231267
bonus += mappingFee;
232268
address trc721Address = calcContractAddress(txId, msg.sender);
233269
require(trc721Address != sunTokenAddress, "mainChainAddress == sunTokenAddress");
234-
require(mainToSideContractMap[trc721Address] != 1, "trc721Address mapped");
270+
require(!isMapAddrInGateways(trc721Address), "trc721Address mapped");
235271
uint256 size;
236272
assembly {size := extcodesize(trc721Address)}
237273
require(size > 0);
238274
userMappingList.push(MappingMsg(trc721Address, DataModel.TokenKind.TRC721, DataModel.Status.SUCCESS));
239275
mainToSideContractMap[trc721Address] = 1;
240-
emit TRC721Mapping(trc721Address, userMappingList.length - 1);
241-
return userMappingList.length - 1;
276+
emit TRC721Mapping(trc721Address, nonceBaseValue + userMappingList.length - 1);
277+
return nonceBaseValue + userMappingList.length - 1;
242278
}
243279

244280
function retryDeposit(uint256 nonce) payable public goDelegateCall onlyNotStop onlyNotPause isHuman {
245281
require(msg.value >= retryFee, "msg.value need >= retryFee");
282+
require(nonce >= nonceBaseValue, "nonce should not < nonceBaseValue");
283+
require(nonce < nonceBaseValue + userDepositList.length, "nonce >= nonceBaseValue + userDepositList.length");
246284
if (msg.value > retryFee) {
247285
msg.sender.transfer(msg.value - retryFee);
248286
}
249287
bonus += retryFee;
250-
require(nonce < userDepositList.length, "nonce >= userDepositList.length");
251-
DepositMsg storage depositMsg = userDepositList[nonce];
288+
DepositMsg storage depositMsg = userDepositList[nonce - nonceBaseValue];
252289
// TRX, // 0
253290
// TRC10, // 1
254291
// TRC20, // 2
255292
// TRC721, // 3
256293
if (depositMsg._type == 0) {
257294
emit TRXReceived(depositMsg.user, depositMsg.value, nonce);
258295
} else if (depositMsg._type == 2) {
259-
emit TRC20Received(depositMsg.user, depositMsg.mainChainAddress, depositMsg.value, nonce);
296+
emit TRC20Received(depositMsg.user, depositMsg.mainChainAddress, depositMsg.uId, nonce);
260297
} else if (depositMsg._type == 3) {
261298
emit TRC721Received(depositMsg.user, depositMsg.mainChainAddress, depositMsg.uId, nonce);
262299
} else {
@@ -266,12 +303,13 @@ contract MainChainGateway is OracleManagerContract {
266303

267304
function retryMapping(uint256 nonce) payable public goDelegateCall onlyNotStop onlyNotPause isHuman {
268305
require(msg.value >= retryFee, "msg.value need >= retryFee");
306+
require(nonce >= nonceBaseValue, "nonce should not < nonceBaseValue");
307+
require(nonce < nonceBaseValue + userMappingList.length, "nonce >= nonceBaseValue + userMappingList.length");
269308
if (msg.value > retryFee) {
270309
msg.sender.transfer(msg.value - retryFee);
271310
}
272311
bonus += retryFee;
273-
require(nonce < userMappingList.length, "nonce >= userMappingList.length");
274-
MappingMsg storage mappingMsg = userMappingList[nonce];
312+
MappingMsg storage mappingMsg = userMappingList[nonce - nonceBaseValue];
275313
require(mappingMsg.status == DataModel.Status.SUCCESS, "mappingMsg.status != SUCCESS ");
276314

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

314352
// Returns all the TRC20
315353
function getTRC20(address contractAddress) external view returns (uint256) {
316-
return TRC20(contractAddress).balanceOf(contractAddress);
354+
return TRC20(contractAddress).balanceOf(address(this));
355+
}
356+
357+
function getBonus() external view returns(uint256) {
358+
// TODO:
359+
// Changed in next version to make it recursively as:
360+
// MainChainGateway(refGatewayAddress).getBonus() + bonus
361+
if(refGatewayAddress == address(0))
362+
return bonus;
363+
else
364+
return MainChainGateway(refGatewayAddress).bonus() + bonus;
317365
}
318366

319367
// Returns TRC721 token by uid
@@ -353,4 +401,23 @@ contract MainChainGateway is OracleManagerContract {
353401
depositMinTrc20 = minValue;
354402
}
355403

404+
function setRefGatewayAddress(address payable ref) public goDelegateCall onlyOwner {
405+
refGatewayAddress = ref;
406+
}
407+
408+
function isMapAddrInGateways(address trcAddress) public goDelegateCall returns (bool){
409+
if (mainToSideContractMap[trcAddress] == 1) {
410+
return true;
411+
}
412+
if (refGatewayAddress == address(0)) {
413+
return false;
414+
}
415+
// TODO: In next version we should use gatewayPeers[i].isMapAddrInGateways(trcAddress) here
416+
if (MainChainGateway(refGatewayAddress).mainToSideContractMap(trcAddress) == 1) {
417+
mainToSideContractMap[trcAddress] = 1;
418+
return true;
419+
}
420+
return false;
421+
}
422+
356423
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
contract DataModel {
2+
enum TokenKind {
3+
TRX, // 0
4+
TRC10, // 1
5+
TRC20, // 2
6+
TRC721 // 3
7+
}
8+
enum Status {
9+
SUCCESS, // 0
10+
LOCKING, // 1
11+
FAIL, // 2
12+
REFUNDED // 3
13+
}
14+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
library ECVerify {
2+
3+
function recover(bytes32 hash, bytes memory signature) internal pure returns (address) {
4+
uint8 v;
5+
bytes32 r;
6+
bytes32 s;
7+
assembly {
8+
r := mload(add(signature, 32))
9+
s := mload(add(signature, 64))
10+
v := and(mload(add(signature, 65)), 255)
11+
}
12+
if (v < 27) {
13+
v += 27;
14+
}
15+
return ecrecover(hash, v, r, s);
16+
}
17+
18+
function ecverify(bytes32 hash, bytes memory sig, address signer) internal pure returns (bool) {
19+
return signer == recover(hash, sig);
20+
}
21+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import "../versionable/Versionable.sol";
2+
3+
contract Delegatecallable is Versionable {
4+
address public logicAddress;
5+
6+
event DelegateResult(bool result, bytes msg);
7+
event LogicAddressChanged(address oldAddress, address newAddress);
8+
modifier goDelegateCall {
9+
if (logicAddress != address(0)) {
10+
(bool result,bytes memory mesg) = logicAddress.delegatecall(msg.data);
11+
if (!result) {
12+
revert(string(mesg));
13+
}
14+
emit DelegateResult(result, mesg);
15+
return;
16+
}
17+
_;
18+
}
19+
function changeLogicAddress(address newLogicAddress) internal {
20+
string memory newVersion;
21+
if (newLogicAddress == address(0)) {
22+
newVersion = initVersion;
23+
} else {
24+
newVersion = Versionable(newLogicAddress).getCodeVersion();
25+
}
26+
emit ChangeVersion(codeVersion, newVersion);
27+
emit LogicAddressChanged(logicAddress, newLogicAddress);
28+
codeVersion = newVersion;
29+
logicAddress = newLogicAddress;
30+
}
31+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* @title SafeMath
3+
* @dev Math operations with safety checks that throw on error
4+
*/
5+
library SafeMath {
6+
7+
/**
8+
* @dev Multiplies two numbers, throws on overflow.
9+
*/
10+
function mul(uint256 a, uint256 b) internal pure returns (uint256 c) {
11+
// Gas optimization: this is cheaper than asserting 'a' not being zero, but the
12+
// benefit is lost if 'b' is also tested.
13+
// See: https://github.com/OpenZeppelin/openzeppelin-solidity/pull/522
14+
if (a == 0) {
15+
return 0;
16+
}
17+
18+
c = a * b;
19+
require(c / a == b);
20+
return c;
21+
}
22+
23+
/**
24+
* @dev Integer division of two numbers, truncating the quotient.
25+
*/
26+
function div(uint256 a, uint256 b) internal pure returns (uint256) {
27+
// assert(b > 0); // Solidity automatically throws when dividing by 0
28+
// uint256 c = a / b;
29+
// require(a == b * c + a % b); // There is no case in which this doesn't hold
30+
return a / b;
31+
}
32+
33+
/**
34+
* @dev Subtracts two numbers, throws on overflow (i.e. if subtrahend is greater than minuend).
35+
*/
36+
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
37+
require(b <= a);
38+
return a - b;
39+
}
40+
41+
/**
42+
* @dev Adds two numbers, throws on overflow.
43+
*/
44+
function add(uint256 a, uint256 b) internal pure returns (uint256 c) {
45+
c = a + b;
46+
require(c >= a);
47+
return c;
48+
}
49+
}

0 commit comments

Comments
 (0)