├── commitmentScheme.sol
├── delivery_drone.sol
├── queue.sol
├── console.sol
├── iterable_mapping.sol
└── heap.sol
/commitmentScheme.sol:
--------------------------------------------------------------------------------
1 | enum Commitment {
2 | Hidden(bytes32 hash),
3 | Revealed(uint value)
4 | }
5 |
6 | function reveal(Commitment storage _c, uint _value, uint _nonce) {
7 | if (_c == Commitment.Hidden && this.Hidden.hash == sha3(_value, _nonce))
8 | _c = Commitment.Revealed(_value);
9 | }
10 |
11 |
--------------------------------------------------------------------------------
/delivery_drone.sol:
--------------------------------------------------------------------------------
1 | ////////////////////////////////////////////////////////////
2 | // This is an example contract hacked together at a meetup.
3 | // It is by far not complete and only used to show some
4 | // features of Solidity.
5 | ////////////////////////////////////////////////////////////
6 | contract DeliveryDroneControl {
7 | /// @dev account of the drone itself
8 | address drone;
9 |
10 | struct Delivery { string from; string to; }
11 | Delivery[] public requestQueue;
12 |
13 | enum Status { Idle, Delivering, ToNextDelivery }
14 | Status public status;
15 |
16 | event DeliveryRequested(string from, string to);
17 |
18 | /// @dev constructor, stores the address of the drone.
19 | function DeliveryDroneControl(address _drone) {
20 | drone = _drone;
21 | }
22 |
23 | /// @notice request drone delivery from `from` to `to`.
24 | function requestDelivery(string from, string to) {
25 | /// construct the struct "Delivery" and assign it to storage.
26 | var queue = requestQueue; // stores reference to storage
27 | queue[queue.length++] = Delivery(from, to);
28 | DeliveryRequested(from, to);
29 | }
30 |
31 | modifier onlyCalledByDrone() { if (msg.sender == drone) _ }
32 |
33 | /// @dev called by the drone to get the next location to fly to
34 | function getNextLocation() onlyCalledByDrone returns (string) {
35 | if (requestQueue.length == 0) return "";
36 | // @todo this is not actually a queue
37 | if (status == Status.Delivering)
38 | return requestQueue[0].to;
39 | else
40 | return requestQueue[0].from;
41 | }
42 |
43 | function delivered() calledByDrone {
44 | }
45 |
46 | }
47 |
--------------------------------------------------------------------------------
/queue.sol:
--------------------------------------------------------------------------------
1 | ////////////////////////////////////////////////////////////
2 | // This is an example contract hacked together at a meetup.
3 | // It is by far not complete and only used to show some
4 | // features of Solidity.
5 | ////////////////////////////////////////////////////////////
6 | contract queue
7 | {
8 | struct Queue {
9 | uint[] data;
10 | uint front;
11 | uint back;
12 | }
13 | /// @dev the number of elements stored in the queue.
14 | function length(Queue storage q) constant internal returns (uint) {
15 | return q.back - q.front;
16 | }
17 | /// @dev the number of elements this queue can hold
18 | function capacity(Queue storage q) constant internal returns (uint) {
19 | return q.data.length - 1;
20 | }
21 | /// @dev push a new element to the back of the queue
22 | function push(Queue storage q, uint data) internal
23 | {
24 | if ((q.back + 1) % q.data.length == q.front)
25 | return; // throw;
26 | q.data[q.back] = data;
27 | q.back = (q.back + 1) % q.data.length;
28 | }
29 | /// @dev remove and return the element at the front of the queue
30 | function pop(Queue storage q) internal returns (uint r)
31 | {
32 | if (q.back == q.front)
33 | return; // throw;
34 | r = q.data[q.front];
35 | delete q.data[q.front];
36 | q.front = (q.front + 1) % q.data.length;
37 | }
38 | }
39 |
40 | contract QueueUserMayBeDeliveryDroneControl is queue {
41 | Queue requests;
42 | function QueueUserMayBeDeliveryDroneControl() {
43 | requests.data.length = 200;
44 | }
45 | function addRequest(uint d) {
46 | push(requests, d);
47 | }
48 | function popRequest() returns (uint) {
49 | return pop(requests);
50 | }
51 | function queueLength() returns (uint) {
52 | return length(requests);
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/console.sol:
--------------------------------------------------------------------------------
1 | // Adapted from nomiclabs' console.log
2 |
3 | // SPDX-License-Identifier: MIT
4 | pragma solidity >=0.8.0;
5 |
6 | import "./string.sol" as String;
7 |
8 | export { log };
9 |
10 |
11 | function _sendLogPayload(bytes memory payload) private view {
12 | uint256 payloadLength = payload.length;
13 | address consoleAddress = 0x000000000000000000636F6e736F6c652e6c6f67;
14 | (bool success,) = consoleAddress.staticcall{gas: gasleft()}(payload);
15 |
16 | }
17 |
18 | function _createSignature() pure returns (string memory)
19 | {
20 | return abi.encodePacked(
21 | "log(",
22 | type(A).name,
23 | ")"
24 | );
25 | }
26 |
27 | function _createSignature() pure returns (string memory)
28 | {
29 | return abi.encodePacked(
30 | "log(",
31 | type(A).name,
32 | type(B).name,
33 | ")"
34 | );
35 | }
36 |
37 | function _createSignature() pure returns (string memory)
38 | {
39 | return abi.encodePacked(
40 | "log(",
41 | type(A).name,
42 | type(B).name,
43 | type(C).name,
44 | ")"
45 | );
46 | }
47 |
48 | function _createSignature() pure returns (string memory)
49 | {
50 | return abi.encodePacked(
51 | "log(",
52 | type(A).name,
53 | type(B).name,
54 | type(C).name,
55 | type(D).name,
56 | ")"
57 | );
58 | }
59 |
60 | function log(A a) view {
61 | // need 'type(A).name'
62 | _sendLogPayload(abi.encodeWithSignature(_createSignature::(), a));
63 | }
64 |
65 | function log(A a, B b) view {
66 | _sendLogPayload(abi.encodeWithSignature(_createSignature::(), a, b));
67 | }
68 |
69 | function log(A a, B b, C c) view {
70 | _sendLogPayload(abi.encodeWithSignature(_createSignature::(), a, b, c));
71 | }
72 |
73 | function log(A a, B b, C c, D d) view {
74 | _sendLogPayload(abi.encodeWithSignature(_createSignature::(), a, b, c, d));
75 | }
--------------------------------------------------------------------------------
/iterable_mapping.sol:
--------------------------------------------------------------------------------
1 | /// @dev Models a uint -> uint mapping where it is possible to iterate over all keys.
2 | struct itmap
3 | {
4 | struct IndexValue { uint keyIndex; uint value; }
5 | struct KeyFlag { uint key; bool deleted; }
6 | struct KeyValue { uint key; uint value; }
7 |
8 | mapping(uint => IndexValue) data;
9 | KeyFlag[] keys;
10 | uint size;
11 | }
12 | library itmap_impl
13 | {
14 | function insert(itmap storage self, uint key, uint value) returns (bool replaced)
15 | {
16 | uint keyIndex = self.data[key].keyIndex;
17 | self.data[key].value = value;
18 | if (keyIndex > 0)
19 | return true;
20 | else
21 | {
22 | keyIndex = keys.length++;
23 | self.data[key].keyIndex = keyIndex + 1;
24 | self.keys[keyIndex].key = key;
25 | self.size++;
26 | return false;
27 | }
28 | }
29 | function remove(itmap storage self, uint key) returns (bool success)
30 | {
31 | uint keyIndex = self.data[key].keyIndex;
32 | if (keyIndex == 0)
33 | return false;
34 | delete self.data[key];
35 | self.keys[keyIndex - 1].deleted = true;
36 | self.size --;
37 | }
38 | function contains(itmap storage self, uint key)
39 | {
40 | return self.data[key].keyIndex > 0;
41 | }
42 | function iterate_start(itmap storage self) returns (uint keyIndex)
43 | {
44 | return iterate_next(self, -1);
45 | }
46 | function iterate_valid(itmap storage self, uint keyIndex) returns (bool)
47 | {
48 | return keyIndex < self.keys.length;
49 | }
50 | function iterate_next(itmap storage self, uint keyIndex) returns (uint r_keyIndex)
51 | {
52 | keyIndex++;
53 | while (keyIndex < self.keys.length && self.keys[keyIndex].deleted)
54 | keyIndex++;
55 | return keyIndex;
56 | }
57 | function iterate_get(itmap storage self, uint keyIndex) returns (KeyValue r)
58 | {
59 | r.key = self.keys[keyIndex].key;
60 | r.value = self.data[key];
61 | }
62 | }
63 |
64 | /// How to use it:
65 | contract User
66 | {
67 | /// Just a struct holding our data.
68 | itmap data;
69 | /// Tell the compiler to bind all functions from itmap_impl to all instances of itmap.
70 | using itamp_impl for itmap;
71 | /// Insert something
72 | function insert(uint k, uint v) returns (uint size)
73 | {
74 | /// Actually calls itmap_impl.insert, auto-supplying the first parameter for us.
75 | data.insert(k, v);
76 | /// We can still access members of the struct - but we should take care not to mess with them.
77 | return data.size;
78 | }
79 | /// Computes the sum of all stored data.
80 | function sum() returns (uint)
81 | {
82 | uint s;
83 | for (var i = data.iterate_start(); data.iterate_valid(i); i = data.iterate_next(i))
84 | s += data.iterate_get(i).value;
85 | }
86 | }
87 |
--------------------------------------------------------------------------------
/heap.sol:
--------------------------------------------------------------------------------
1 | /*
2 | * Modified from chriseth's code. Copyright chriseth and Jinhua Wang.
3 | * Permission is hereby granted, free of charge, to any person obtaining a copy
4 | * of this software and associated documentation files (the "Software"), to deal
5 | * in the Software without restriction, including without limitation the rights
6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 | * copies of the Software, and to permit persons to whom the Software is
8 | * furnished to do so, subject to the following conditions:
9 |
10 | * The above copyright notice and this permission notice shall be included in all
11 | * copies or substantial portions of the Software.
12 |
13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 | * SOFTWARE.
20 | */
21 |
22 | struct Heap[T] {
23 | T[] data;
24 | }
25 |
26 | library MinHeap_impl[T] {
27 | /*
28 | * Implementation of Minimum Heap
29 | */
30 | function insert(Heap[T] storage _heap, T _value)
31 | {
32 | _heap.data.length++;
33 | for (
34 | uint _index = _heap.data.length - 1;
35 | _index > 0 && _value < _heap.data[_index / 2];
36 | _index /= 2)
37 | {
38 | _heap.data[_index] = _heap.data[_index / 2];
39 | }
40 | _heap.data[_index] = _value;
41 | }
42 | function top(Heap[T] storage _heap) returns (T)
43 | {
44 | return _heap.data[0];
45 | }
46 | function pop(Heap[T] storage _heap)
47 | {
48 | T storage last = _heap.data[_heap.data.length - 1];
49 | for (
50 | uint index = 0;
51 | 2 * index < _heap.data.length
52 | ;)
53 | {
54 | uint nextIndex = 2 * index;
55 | if (2 * index + 1 < _heap.data.length && _heap.data[2 * index + 1] < _heap.data[2 * index])
56 | nextIndex = 2 * index + 1;
57 | if (_heap.data[nextIndex] < last)
58 | _heap.data[index] = _heap.data[nextIndex];
59 | else
60 | break;
61 | index = nextIndex;
62 | }
63 | _heap.data[index] = last;
64 | _heap.data.length--;
65 | }
66 | }
67 |
68 | library MaxHeap_impl[T] {
69 | /*
70 | * Implementation of Maximum Heap
71 | */
72 | function insert(Heap[T] storage _heap, T _value)
73 | {
74 | _heap.data.length++;
75 | for (
76 | uint _index = _heap.data.length - 1;
77 | _index > 0 && _value > _heap.data[_index / 2];
78 | _index /= 2)
79 | {
80 | _heap.data[_index] = _heap.data[_index / 2];
81 | }
82 | _heap.data[_index] = _value;
83 | }
84 | function top(Heap[T] storage _heap) returns (T)
85 | {
86 | return _heap.data[0];
87 | }
88 | function pop(Heap[T] storage _heap)
89 | {
90 | T storage last = _heap.data[_heap.data.length - 1];
91 | for (
92 | uint index = 0;
93 | 2 * index < _heap.data.length
94 | ;)
95 | {
96 | uint nextIndex = 2 * index;
97 | if (2 * index + 1 < _heap.data.length && _heap.data[2 * index + 1] > _heap.data[2 * index])
98 | nextIndex = 2 * index + 1;
99 | if (_heap.data[nextIndex] > last)
100 | _heap.data[index] = _heap.data[nextIndex];
101 | else
102 | break;
103 | index = nextIndex;
104 | }
105 | _heap.data[index] = last;
106 | _heap.data.length--;
107 | }
108 | }
--------------------------------------------------------------------------------