forked from zkopru-network/zkopru
/
Layer2.sol
127 lines (103 loc) · 4.08 KB
/
Layer2.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
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
pragma solidity >= 0.6.0;
import "../libraries/Types.sol";
import { Pairing } from "../libraries/Pairing.sol";
import { SNARKsVerifier } from "../libraries/SNARKs.sol";
import { Configurated } from "./Configurated.sol";
import { SMT254 } from "../libraries/SMT.sol";
import { OPRU, SplitRollUp } from "../libraries/Tree.sol";
struct RollUpProofs {
SplitRollUp[] ofUTXORollUp;
SMT254.OPRU[] ofNullifierRollUp;
SplitRollUp[] ofWithdrawalRollUp;
mapping(uint8=>mapping(uint=>address)) permittedTo;
}
contract Layer2 is Configurated {
/** State of the layer2 blockchain is maintained by the optimistic roll up */
Blockchain chain;
/** SNARKs verifying keys assigned by the setup wizard for each tx type */
mapping(bytes32=>SNARKsVerifier.VerifyingKey) vks;
/** Addresses allowed to migrate from. Setup wizard manages the list */
mapping(address=>bool) public allowedMigrants;
/** Roll up proofs for challenge */
RollUpProofs proof;
function genesis() public view returns (bytes32) {
return chain.genesis;
}
function latest() public view returns (bytes32) {
return chain.latest;
}
function proposedBlocks() public view returns (uint256) {
return chain.proposedBlocks;
}
function parentOf(bytes32 header) public view returns (bytes32) {
return chain.parentOf[header];
}
function utxoRootOf(bytes32 header) public view returns (bytes32) {
return bytes32(chain.utxoRootOf[header]);
}
function finalizedUTXOs(bytes32 utxoRoot) public view returns (bool) {
return chain.finalizedUTXOs[uint(utxoRoot)];
}
function proposers(address addr) public view returns (uint stake, uint reward, uint exitAllowance) {
Proposer memory proposer = chain.proposers[addr];
stake = proposer.stake;
reward = proposer.reward;
exitAllowance = proposer.exitAllowance;
}
function proposals(bytes32 proposalId) public view returns (bytes32 header, uint challengeDue, bool slashed) {
Proposal memory proposal = chain.proposals[proposalId];
header = proposal.headerHash;
challengeDue = proposal.challengeDue;
slashed = proposal.slashed;
}
function stagedDeposits() public view returns (bytes32 merged, uint fee) {
MassDeposit memory massDeposit = chain.stagedDeposits;
merged = massDeposit.merged;
fee = massDeposit.fee;
}
function stagedSize() public view returns (uint) {
return chain.stagedSize;
}
function massDepositId() public view returns (uint) {
return chain.massDepositId;
}
function committedDeposits(bytes32 massDepositHash) public view returns (uint) {
return chain.committedDeposits[massDepositHash];
}
function withdrawables(uint idx) public view returns (bytes32 root, uint index) {
Withdrawable memory withdrawable = chain.withdrawables[idx];
root = withdrawable.root;
index = withdrawable.index;
}
function snapshotTimestamp() public view returns (uint) {
return chain.snapshotTimestamp;
}
function withdrawn(bytes32 leaf) public view returns (bool) {
return chain.withdrawn[leaf];
}
function migrations(bytes32 migrationHash) public view returns (bool) {
return chain.migrations[migrationHash];
}
function getVk(uint8 numOfInputs, uint8 numOfOutputs) public view returns (
uint[2] memory alfa1,
uint[2][2] memory beta2,
uint[2][2] memory gamma2,
uint[2][2] memory delta2,
uint[2][] memory ic
) {
bytes32 txSig = Types.getSNARKsSignature(numOfInputs, numOfOutputs);
SNARKsVerifier.VerifyingKey memory vk = vks[txSig];
alfa1[0] = vk.alfa1.X;
alfa1[1] = vk.alfa1.Y;
beta2[0] = vk.beta2.X;
beta2[1] = vk.beta2.Y;
gamma2[0] = vk.gamma2.X;
gamma2[1] = vk.gamma2.Y;
delta2[0] = vk.delta2.X;
delta2[1] = vk.delta2.Y;
ic = new uint[2][](vk.ic.length);
for(uint i = 0; i < vk.ic.length; i++) {
ic[i] = [vk.ic[i].X, vk.ic[i].Y];
}
}
}