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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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
pragma solidity ^0.4.23;

interface RegistryInterface {
  function getLatestVersion(address stor_addr, bytes32 exec_id, address provider, bytes32 app_name)
      external view returns (bytes32 latest_name);
  function getVersionImplementation(address stor_addr, bytes32 exec_id, address provider, bytes32 app_name, bytes32 version_name)
      external view returns (address index, bytes4[] selectors, address[] implementations);
}

contract AbstractStorage {

   
   
  bytes32 private exec_id;
  address private sender;

   
  uint private nonce;

   

  event ApplicationInitialized(bytes32 indexed execution_id, address indexed index, address script_exec);
  event ApplicationExecution(bytes32 indexed execution_id, address indexed script_target);
  event DeliveredPayment(bytes32 indexed execution_id, address indexed destination, uint amount);

   

   

  bytes32 internal constant EXEC_PERMISSIONS = keccak256('script_exec_permissions');
  bytes32 internal constant APP_IDX_ADDR = keccak256('index');

   

  bytes4 internal constant EMITS = bytes4(keccak256('Emit((bytes32[],bytes)[])'));
  bytes4 internal constant STORES = bytes4(keccak256('Store(bytes32[])'));
  bytes4 internal constant PAYS = bytes4(keccak256('Pay(bytes32[])'));
  bytes4 internal constant THROWS = bytes4(keccak256('Error(string)'));

   

  bytes4 internal constant REG_APP
      = bytes4(keccak256('registerApp(bytes32,address,bytes4[],address[])'));
  bytes4 internal constant REG_APP_VER
      = bytes4(keccak256('registerAppVersion(bytes32,bytes32,address,bytes4[],address[])'));
  bytes4 internal constant UPDATE_EXEC_SEL
      = bytes4(keccak256('updateExec(address)'));
  bytes4 internal constant UPDATE_INST_SEL
      = bytes4(keccak256('updateInstance(bytes32,bytes32,bytes32)'));

   
  function createRegistry(address _registry_idx, address _implementation) external returns (bytes32) {
    bytes32 new_exec_id = keccak256(++nonce);
    put(new_exec_id, keccak256(msg.sender, EXEC_PERMISSIONS), bytes32(1));
    put(new_exec_id, APP_IDX_ADDR, bytes32(_registry_idx));
    put(new_exec_id, keccak256(REG_APP, 'implementation'), bytes32(_implementation));
    put(new_exec_id, keccak256(REG_APP_VER, 'implementation'), bytes32(_implementation));
    put(new_exec_id, keccak256(UPDATE_INST_SEL, 'implementation'), bytes32(_implementation));
    put(new_exec_id, keccak256(UPDATE_EXEC_SEL, 'implementation'), bytes32(_implementation));
    emit ApplicationInitialized(new_exec_id, _registry_idx, msg.sender);
    return new_exec_id;
  }

   

   
  function createInstance(address _sender, bytes32 _app_name, address _provider, bytes32 _registry_id, bytes _calldata) external payable returns (bytes32 new_exec_id, bytes32 version) {
     
    require(_sender != 0 && _app_name != 0 && _provider != 0 && _registry_id != 0 && _calldata.length >= 4, 'invalid input');

     
    new_exec_id = keccak256(++nonce);

     
    assert(getIndex(new_exec_id) == address(0));

     
    address index;
    (index, version) = setImplementation(new_exec_id, _app_name, _provider, _registry_id);

     
    setContext(new_exec_id, _sender);

     
    require(address(index).delegatecall(_calldata) == false, 'Unsafe execution');
     
    executeAppReturn(new_exec_id);

     
    emit ApplicationInitialized(new_exec_id, index, msg.sender);

     
    assert(new_exec_id != bytes32(0));

     
    if (address(this).balance > 0)
      address(msg.sender).transfer(address(this).balance);
  }

   
  function exec(address _sender, bytes32 _exec_id, bytes _calldata) external payable returns (uint n_emitted, uint n_paid, uint n_stored) {
     
    require(_calldata.length >= 4 && _sender != address(0) && _exec_id != bytes32(0));

     
    address target = getTarget(_exec_id, getSelector(_calldata));
    require(target != address(0), 'Uninitialized application');

     
    setContext(_exec_id, _sender);

     
    require(address(target).delegatecall(_calldata) == false, 'Unsafe execution');
    (n_emitted, n_paid, n_stored) = executeAppReturn(_exec_id);

     
    if (n_emitted == 0 && n_paid == 0 && n_stored == 0)
      revert('No state change occured');

     
    emit ApplicationExecution(_exec_id, target);

     
    if (address(this).balance > 0)
      address(msg.sender).transfer(address(this).balance);
  }

   

   
  function executeAppReturn(bytes32 _exec_id) internal returns (uint n_emitted, uint n_paid, uint n_stored) {
    uint _ptr;       
    uint ptr_bound;  
    (ptr_bound, _ptr) = getReturnedData();
     
    if (getAction(_ptr) == THROWS) {
       
      doThrow(_ptr);
       
      assert(false);
    }

     
    require(ptr_bound >= _ptr + 64, 'Malformed returndata - invalid size');
    _ptr += 64;

     
    bytes4 action;
    while (_ptr <= ptr_bound && (action = getAction(_ptr)) != 0x0) {
      if (action == EMITS) {
         
        require(n_emitted == 0, 'Duplicate action: EMITS');
         
         
        (_ptr, n_emitted) = doEmit(_ptr, ptr_bound);
         
        require(n_emitted != 0, 'Unfulfilled action: EMITS');
      } else if (action == STORES) {
         
        require(n_stored == 0, 'Duplicate action: STORES');
         
         
        (_ptr, n_stored) = doStore(_ptr, ptr_bound, _exec_id);
         
        require(n_stored != 0, 'Unfulfilled action: STORES');
      } else if (action == PAYS) {
         
        require(n_paid == 0, 'Duplicate action: PAYS');
         
         
        (_ptr, n_paid) = doPay(_exec_id, _ptr, ptr_bound);
         
        require(n_paid != 0, 'Unfulfilled action: PAYS');
      } else {
         
        revert('Malformed returndata - unknown action');
      }
    }
    assert(n_emitted != 0 || n_paid != 0 || n_stored != 0);
  }

   

   
  function setImplementation(bytes32 _new_exec_id, bytes32 _app_name, address _provider, bytes32 _registry_id) internal returns (address index, bytes32 version) {
     
    index = getIndex(_registry_id);
    require(index != address(0) && index != address(this), 'Registry application not found');
     
    version = RegistryInterface(index).getLatestVersion(
      address(this), _registry_id, _provider, _app_name
    );
     
    require(version != bytes32(0), 'Invalid version name');

     
    bytes4[] memory selectors;
    address[] memory implementations;
    (index, selectors, implementations) = RegistryInterface(index).getVersionImplementation(
      address(this), _registry_id, _provider, _app_name, version
    );
     
    require(index != address(0), 'Invalid index address');
     
    require(selectors.length == implementations.length && selectors.length != 0, 'Invalid implementation length');

     
    bytes32 seed = APP_IDX_ADDR;
    put(_new_exec_id, seed, bytes32(index));
     
    for (uint i = 0; i < selectors.length; i++) {
      require(selectors[i] != 0 && implementations[i] != 0, 'invalid input - expected nonzero implementation');
      seed = keccak256(selectors[i], 'implementation');
      put(_new_exec_id, seed, bytes32(implementations[i]));
    }

    return (index, version);
  }

   
   
  function getIndex(bytes32 _exec_id) public view returns (address) {
    bytes32 seed = APP_IDX_ADDR;
    function (bytes32, bytes32) view returns (address) getter;
    assembly { getter := readMap }
    return getter(_exec_id, seed);
  }

   
  function getTarget(bytes32 _exec_id, bytes4 _selector) public view returns (address) {
    bytes32 seed = keccak256(_selector, 'implementation');
    function (bytes32, bytes32) view returns (address) getter;
    assembly { getter := readMap }
    return getter(_exec_id, seed);
  }

  struct Map { mapping(bytes32 => bytes32) inner; }

   
  function readMap(Map storage _map, bytes32 _seed) internal view returns (bytes32) {
    return _map.inner[_seed];
  }

   
  function put(bytes32 _exec_id, bytes32 _seed, bytes32 _val) internal {
    function (bytes32, bytes32, bytes32) puts;
    assembly { puts := putMap }
    puts(_exec_id, _seed, _val);
  }

   
  function putMap(Map storage _map, bytes32 _seed, bytes32 _val) internal {
    _map.inner[_seed] = _val;
  }

   

  function getSelector(bytes memory _calldata) internal pure returns (bytes4 sel) {
    assembly {
      sel := and(
        mload(add(0x20, _calldata)),
        0xffffffff00000000000000000000000000000000000000000000000000000000
      )
    }
  }

   
  function getReturnedData() internal pure returns (uint ptr_bounds, uint _returndata_ptr) {
    assembly {
       
      if lt(returndatasize, 0x60) {
        mstore(0, 0x20)
        mstore(0x20, 24)
        mstore(0x40, 'Insufficient return size')
        revert(0, 0x60)
      }
       
      _returndata_ptr := msize
       
      returndatacopy(_returndata_ptr, 0, returndatasize)
       
      ptr_bounds := add(_returndata_ptr, returndatasize)
       
       
      mstore(0x40, add(0x20, ptr_bounds))
    }
  }

   
  function getLength(uint _ptr) internal pure returns (uint length) {
    assembly { length := mload(_ptr) }
  }

   
  function doThrow(uint _ptr) internal pure {
    assert(getAction(_ptr) == THROWS);
    assembly { revert(_ptr, returndatasize) }
  }

   
  function doPay(bytes32 _exec_id, uint _ptr, uint _ptr_bound) internal returns (uint ptr, uint n_paid) {
     
    require(msg.value > 0);
    assert(getAction(_ptr) == PAYS);
    _ptr += 4;
     
    uint num_destinations = getLength(_ptr);
    _ptr += 32;
    address pay_to;
    uint amt;
     
    while (_ptr <= _ptr_bound && n_paid < num_destinations) {
       
      assembly {
        amt := mload(_ptr)
        pay_to := mload(add(0x20, _ptr))
      }
       
      if (pay_to == address(0) || pay_to == address(this))
        revert('PAYS: invalid destination');

       
      address(pay_to).transfer(amt);
      n_paid++;
       
      _ptr += 64;
       
      emit DeliveredPayment(_exec_id, pay_to, amt);
    }
    ptr = _ptr;
    assert(n_paid == num_destinations);
  }

   
  function doStore(uint _ptr, uint _ptr_bound, bytes32 _exec_id) internal returns (uint ptr, uint n_stored) {
    assert(getAction(_ptr) == STORES && _exec_id != bytes32(0));
    _ptr += 4;
     
    uint num_locations = getLength(_ptr);
    _ptr += 32;
    bytes32 location;
    bytes32 value;
     
    while (_ptr <= _ptr_bound && n_stored < num_locations) {
       
      assembly {
        location := mload(_ptr)
        value := mload(add(0x20, _ptr))
      }
       
      store(_exec_id, location, value);
       
      n_stored++;
      _ptr += 64;
    }
    ptr = _ptr;
    require(n_stored == num_locations);
  }

   
  function doEmit(uint _ptr, uint _ptr_bound) internal returns (uint ptr, uint n_emitted) {
    assert(getAction(_ptr) == EMITS);
    _ptr += 4;
     
    uint num_events = getLength(_ptr);
    _ptr += 32;
    bytes32[] memory topics;
    bytes memory data;
     
    while (_ptr <= _ptr_bound && n_emitted < num_events) {
       
      assembly {
        topics := _ptr
        data := add(add(_ptr, 0x20), mul(0x20, mload(topics)))
      }
       
      uint log_size = 32 + (32 * (1 + topics.length)) + data.length;
      assembly {
        switch mload(topics)                 
          case 0 {
             
            log0(
              add(0x20, data),               
              mload(data)                    
            )
          }
          case 1 {
             
            log1(
              add(0x20, data),               
              mload(data),                   
              mload(add(0x20, topics))       
            )
          }
          case 2 {
             
            log2(
              add(0x20, data),               
              mload(data),                   
              mload(add(0x20, topics)),      
              mload(add(0x40, topics))       
            )
          }
          case 3 {
             
            log3(
              add(0x20, data),               
              mload(data),                   
              mload(add(0x20, topics)),      
              mload(add(0x40, topics)),      
              mload(add(0x60, topics))       
            )
          }
          case 4 {
             
            log4(
              add(0x20, data),               
              mload(data),                   
              mload(add(0x20, topics)),      
              mload(add(0x40, topics)),      
              mload(add(0x60, topics)),      
              mload(add(0x80, topics))       
            )
          }
          default {
             
            mstore(0, 'EMITS: invalid topic count')
            revert(0, 0x20)
          }
      }
       
      n_emitted++;
      _ptr += log_size;
    }
    ptr = _ptr;
    require(n_emitted == num_events);
  }

   
  function getAction(uint _ptr) internal pure returns (bytes4 action) {
    assembly {
       
      action := and(mload(_ptr), 0xffffffff00000000000000000000000000000000000000000000000000000000)
    }
  }

   
   
  function setContext(bytes32 _exec_id, address _sender) internal {
     
    assert(_exec_id != bytes32(0) && _sender != address(0));
    exec_id = _exec_id;
    sender = _sender;
  }

   
  function store(bytes32 _exec_id, bytes32 _location, bytes32 _data) internal {
     
    _location = keccak256(_location, _exec_id);
     
    assembly { sstore(_location, _data) }
  }

   

   
  function read(bytes32 _exec_id, bytes32 _location) public view returns (bytes32 data_read) {
    _location = keccak256(_location, _exec_id);
    assembly { data_read := sload(_location) }
  }

   
  function readMulti(bytes32 _exec_id, bytes32[] _locations) public view returns (bytes32[] data_read) {
    data_read = new bytes32[](_locations.length);
    for (uint i = 0; i < _locations.length; i++) {
      data_read[i] = read(_exec_id, _locations[i]);
    }
  }
}