-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeta_object.sol
94 lines (78 loc) · 2.93 KB
/
meta_object.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
pragma solidity 0.5.4;
import "./ownable.sol";
import "./lib_enctoken.sol";
contract CounterObject is Adminable {
event CounterIncremented(bytes32 ident, uint8 slot, uint32 val);
event BitSetAndGet(bytes32 ident, uint8 ord, bool prev);
event WordGroupDeleted(bytes32 ident);
struct wordGroup {
uint32[8] slots;
}
mapping(bytes32 => wordGroup) wordGroups;
function deleteGroup(bytes32 _ident) public onlyAdmin {
delete wordGroups[_ident];
emit WordGroupDeleted(_ident);
}
function incrementCounter(bytes32 _ident, uint8 _ord) public onlyAdmin {
require(_ord < 8);
uint32 x = wordGroups[_ident].slots[_ord];
wordGroups[_ident].slots[_ord]++;
emit CounterIncremented(_ident, _ord, x);
}
function getCounter(bytes32 _ident, uint8 _ord) public view returns (uint32) {
require(_ord < 8);
return wordGroups[_ident].slots[_ord];
}
function getBit(bytes32 _ident, uint8 _ord) public view returns (bool) {
uint256 slot = _ord / (4 * 8); // bytes per slot * bits per slot
uint256 bit = _ord % (4 * 8);
uint32 checkVal = uint32(1) << bit;
return wordGroups[_ident].slots[slot] & checkVal == 0 ? false : true;
}
function setAndGetBitInternal(bytes32 _ident, uint8 _ord) internal returns (bool) {
uint256 slot = _ord / (4 * 8); // bytes per slot * bits per slot
uint256 bit = _ord % (4 * 8);
uint32 checkVal = uint32(1) << bit;
bool currSet = wordGroups[_ident].slots[slot] & checkVal == 0 ? false : true;
if (!currSet)
wordGroups[_ident].slots[slot] |= checkVal;
emit BitSetAndGet(_ident, _ord, currSet);
return currSet;
}
function setAndGetBit(bytes32 _ident, uint8 _ord) public onlyAdmin returns (bool) {
return setAndGetBitInternal(_ident, _ord);
}
}
contract MetaObject is Adminable {
mapping(bytes32 => bytes) mapSmallKeys;
mapping(bytes => bytes) mapBigKeys;
event ObjectMetaChanged(bytes key);
function putMeta(bytes memory key, bytes memory value) public onlyAdmin {
if (key.length <= 32) {
bytes32 smallKey;
uint256 keyLen = key.length;
assembly {
smallKey := mload(add(key, keyLen))
}
delete mapSmallKeys[smallKey];
if (value.length > 0)
mapSmallKeys[smallKey] = value;
} else {
delete mapBigKeys[key];
if (value.length > 0)
mapBigKeys[key] = value;
}
emit ObjectMetaChanged(key);
}
function getMeta(bytes memory key) public view returns (bytes memory) {
if (key.length <= 32) {
bytes32 smallKey;
uint256 keyLen = key.length;
assembly {
smallKey := mload(add(key, keyLen))
}
return mapSmallKeys[smallKey];
}
return mapBigKeys[key];
}
}