-
Notifications
You must be signed in to change notification settings - Fork 1
/
Starknet.sol
208 lines (179 loc) · 6.49 KB
/
Starknet.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
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
/*
Copyright 2019-2021 StarkWare Industries Ltd.
Licensed under the Apache License, Version 2.0 (the "License").
You may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.starkware.co/open-source-license/
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions
and limitations under the License.
*/
// SPDX-License-Identifier: Apache-2.0.
pragma solidity ^0.6.12;
pragma experimental ABIEncoderV2;
import "IFactRegistry.sol";
import "IIdentity.sol";
import "Output.sol";
import "StarknetGovernance.sol";
import "StarknetMessaging.sol";
import "StarknetOperator.sol";
import "StarknetState.sol";
import "NamedStorage.sol";
import "ContractInitializer.sol";
import "ProxySupport.sol";
import "OnchainDataFactTreeEncoder.sol";
import "GovernedFinalizable.sol";
contract Starknet is
IIdentity,
StarknetMessaging,
StarknetGovernance,
GovernedFinalizable,
StarknetOperator,
ContractInitializer,
ProxySupport
{
using StarknetState for StarknetState.State;
// Logs the new state following a state update.
event LogStateUpdate(uint256 globalRoot, int256 sequenceNumber);
// Logs a stateTransitionFact that was used to update the state.
event LogStateTransitionFact(bytes32 stateTransitionFact);
// Random storage slot tags.
string internal constant PROGRAM_HASH_TAG =
"STARKNET_1.0_INIT_PROGRAM_HASH_UINT";
string internal constant VERIFIER_ADDRESS_TAG =
"STARKNET_1.0_INIT_VERIFIER_ADDRESS";
string internal constant STATE_STRUCT_TAG =
"STARKNET_1.0_INIT_STARKNET_STATE_STRUCT";
function setProgramHash(
uint256 newProgramHash
) external notFinalized onlyGovernance {
programHash(newProgramHash);
}
// State variable "programHash" read-access functions.
function programHash() public view returns (uint256) {
return NamedStorage.getUintValue(PROGRAM_HASH_TAG);
}
// State variable "programHash" write-access functions.
function programHash(uint256 value) internal {
NamedStorage.setUintValue(PROGRAM_HASH_TAG, value);
}
// State variable "verifier" access functions.
function verifier() internal view returns (address) {
return NamedStorage.getAddressValue(VERIFIER_ADDRESS_TAG);
}
function setVerifierAddress(address value) internal {
NamedStorage.setAddressValueOnce(VERIFIER_ADDRESS_TAG, value);
}
// State variable "state" access functions.
function state()
internal
pure
returns (StarknetState.State storage stateStruct)
{
bytes32 location = keccak256(abi.encodePacked(STATE_STRUCT_TAG));
assembly {
stateStruct_slot := location
}
}
function isInitialized() internal view override returns (bool) {
return programHash() != 0;
}
function numOfSubContracts() internal pure override returns (uint256) {
return 0;
}
function validateInitData(bytes calldata data) internal pure override {
require(data.length == 4 * 32, "ILLEGAL_INIT_DATA_SIZE");
uint256 programHash_ = abi.decode(data[:32], (uint256));
require(programHash_ != 0, "BAD_INITIALIZATION");
}
function processSubContractAddresses(
bytes calldata subContractAddresses
) internal override {}
function initializeContractState(bytes calldata data) internal override {
(
uint256 programHash_,
address verifier_,
StarknetState.State memory initialState
) = abi.decode(data, (uint256, address, StarknetState.State));
programHash(programHash_);
setVerifierAddress(verifier_);
state().copy(initialState);
}
/**
Returns a string that identifies the contract.
*/
function identify() external pure override returns (string memory) {
return "StarkWare_Starknet_2021_1";
}
/**
Returns the current state root.
*/
function stateRoot() external view returns (uint256) {
return state().globalRoot;
}
/**
Returns the current sequence number.
*/
function stateSequenceNumber() external view returns (int256) {
return state().sequenceNumber;
}
/**
Updates the state of the StarkNet, based on a proof of the
StarkNet OS that the state transition is valid.
Arguments:
sequenceNumber - The expected sequence number of the new block.
programOutput - The main part of the StarkNet OS program output.
data_availability_fact - An encoding of the on-chain data associated
with the 'programOutput'.
*/
function updateState(
int256 sequenceNumber,
uint256[] calldata programOutput,
uint256 onchainDataHash,
uint256 onchainDataSize
) external onlyOperator {
// Validate program output.
StarknetOutput.validate(programOutput);
bytes32 stateTransitionFact = OnchainDataFactTreeEncoder
.encodeFactWithOnchainData(
programOutput,
OnchainDataFactTreeEncoder.DataAvailabilityFact(
onchainDataHash,
onchainDataSize
)
);
bytes32 sharpFact = keccak256(
abi.encode(programHash(), stateTransitionFact)
);
require(
IFactRegistry(verifier()).isValid(sharpFact),
"NO_STATE_TRANSITION_PROOF"
);
emit LogStateTransitionFact(stateTransitionFact);
// Process L2 -> L1 messages.
uint256 outputOffset = StarknetOutput.HEADER_SIZE;
outputOffset += StarknetOutput.processMessages(
// isL2ToL1=
true,
programOutput[outputOffset:],
l2ToL1Messages()
);
// Process L1 -> L2 messages.
outputOffset += StarknetOutput.processMessages(
// isL2ToL1=
false,
programOutput[outputOffset:],
l1ToL2Messages()
);
require(
outputOffset == programOutput.length,
"STARKNET_OUTPUT_TOO_LONG"
);
// Perform state update.
state().update(sequenceNumber, programOutput);
StarknetState.State memory state_ = state();
emit LogStateUpdate(state_.globalRoot, state_.sequenceNumber);
}
}