-
Notifications
You must be signed in to change notification settings - Fork 0
/
TrackableCounter.sol
79 lines (63 loc) · 1.88 KB
/
TrackableCounter.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
pragma solidity ^0.4.24;
import {SimpleDecode} from "../lib/SimpleDecode.sol";
import {RequestableI} from "../lib/RequestableI.sol";
import {BaseCounter} from "./BaseCounter.sol";
import {SafeMath} from "openzeppelin-solidity/contracts/math/SafeMath.sol";
contract TrackableCounter is BaseCounter, RequestableI {
// SimpleDecode library to decode trieValue.
using SimpleDecode for bytes;
using SafeMath for *;
// trie key for state variable `n`.
bytes32 constant public TRIE_KEY_N = 0x00;
// previous count before enter request in root chain and exit request in child chain.
uint public requestableN;
// address of RootChain contract.
address public rootchain;
mapping (uint => bool) appliedRequests;
constructor(address _rootchain) {
rootchain = _rootchain;
}
/// @dev override BaseCounter.count function.
function count() external {
requestableN++;
n++;
emit Counted(n);
}
function applyRequestInRootChain(
bool isExit,
uint256 requestId,
address requestor,
bytes32 trieKey,
bytes trieValue
) external returns (bool success) {
require(!appliedRequests[requestId]);
require(msg.sender == rootchain);
// only accept request for `n`.
require(trieKey == TRIE_KEY_N);
uint _n = trieValue.toUint()
if (isExit) {
n = n.add(_n);
} else {
requestableN = requestableN.sub(_n);
}
appliedRequests[requestId] = true;
}
function applyRequestInChildChain(
bool isExit,
uint256 requestId,
address requestor,
bytes32 trieKey,
bytes trieValue
) external returns (bool success) {
require(!appliedRequests[requestId]);
require(msg.sender == address(0));
// only accept request for `n`.
require(trieKey == TRIE_KEY_N);
if (isExit) {
requestableN = requestableN.sub(_n);
} else {
n = n.add(_n);
}
appliedRequests[requestId] = true;
}
}