You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

713 lines
24 KiB

4 years ago
Redo coordinator structure, connect API to node - API: - Modify the constructor so that hardcoded rollup constants don't need to be passed (introduce a `Config` and use `configAPI` internally) - Common: - Update rollup constants with proper *big.Int when required - Add BidCoordinator and Slot structs used by the HistoryDB and Synchronizer. - Add helper methods to AuctionConstants - AuctionVariables: Add column `DefaultSlotSetBidSlotNum` (in the SQL table: `default_slot_set_bid_slot_num`), which indicates at which slotNum does the `DefaultSlotSetBid` specified starts applying. - Config: - Move coordinator exclusive configuration from the node config to the coordinator config - Coordinator: - Reorganize the code towards having the goroutines started and stopped from the coordinator itself instead of the node. - Remove all stop and stopped channels, and use context.Context and sync.WaitGroup instead. - Remove BatchInfo setters and assing variables directly - In ServerProof and ServerProofPool use context instead stop channel. - Use message passing to notify the coordinator about sync updates and reorgs - Introduce the Pipeline, which can be started and stopped by the Coordinator - Introduce the TxManager, which manages ethereum transactions (the TxManager is also in charge of making the forge call to the rollup smart contract). The TxManager keeps ethereum transactions and: 1. Waits for the transaction to be accepted 2. Waits for the transaction to be confirmed for N blocks - In forge logic, first prepare a batch and then wait for an available server proof to have all work ready once the proof server is ready. - Remove the `isForgeSequence` method which was querying the smart contract, and instead use notifications sent by the Synchronizer to figure out if it's forging time. - Update test (which is a minimal test to manually see if the coordinator starts) - HistoryDB: - Add method to get the number of batches in a slot (used to detect when a slot has passed the bid winner forging deadline) - Add method to get the best bid and associated coordinator of a slot (used to detect the forgerAddress that can forge the slot) - General: - Rename some instances of `currentBlock` to `lastBlock` to be more clear. - Node: - Connect the API to the node and call the methods to update cached state when the sync advances blocks. - Call methods to update Coordinator state when the sync advances blocks and finds reorgs. - Synchronizer: - Add Auction field in the Stats, which contain the current slot with info about highest bidder and other related info required to know who can forge in the current block. - Better organization of cached state: - On Sync, update the internal cached state - On Init or Reorg, load the state from HistoryDB into the internal cached state.
4 years ago
Allow serving API only via new cli command - Add new command to the cli/node: `serveapi` that alows serving the API just by connecting to the PostgreSQL database. The mode flag should me passed in order to select whether we are connecting to a synchronizer database or a coordinator database. If `coord` is chosen as mode, the coordinator endpoints can be activated in order to allow inserting l2txs and authorizations into the L2DB. Summary of the implementation details - New SQL table with 3 columns (plus `item_id` pk). The table only contains a single row with `item_id` = 1. Columns: - state: historydb.StateAPI in JSON. This is the struct that is served via the `/state` API endpoint. The node will periodically update this struct and store it int he DB. The api server will query it from the DB to serve it. - config: historydb.NodeConfig in JSON. This struct contains node configuration parameters that the API needs to be aware of. It's updated once every time the node starts. - constants: historydb.Constants in JSON. This struct contains all the hermez network constants gathered via the ethereum client by the node. It's written once every time the node starts. - The HistoryDB contains methods to get and update each one of these columns individually. - The HistoryDB contains all methods that query the DB and prepare objects that will appear in the StateAPI endpoint. - The configuration used in for the `serveapi` cli/node command is defined in `config.APIServer`, and is a subset of `node.Config` in order to allow reusing the same configuration file of the node if desired. - A new object is introduced in the api: `StateAPIUpdater`, which contains all the necessary information to update the StateAPI in the DB periodically by the node. - Moved the types `SCConsts`, `SCVariables` and `SCVariablesPtr` from `syncrhonizer` to `common` for convenience.
3 years ago
Allow serving API only via new cli command - Add new command to the cli/node: `serveapi` that alows serving the API just by connecting to the PostgreSQL database. The mode flag should me passed in order to select whether we are connecting to a synchronizer database or a coordinator database. If `coord` is chosen as mode, the coordinator endpoints can be activated in order to allow inserting l2txs and authorizations into the L2DB. Summary of the implementation details - New SQL table with 3 columns (plus `item_id` pk). The table only contains a single row with `item_id` = 1. Columns: - state: historydb.StateAPI in JSON. This is the struct that is served via the `/state` API endpoint. The node will periodically update this struct and store it int he DB. The api server will query it from the DB to serve it. - config: historydb.NodeConfig in JSON. This struct contains node configuration parameters that the API needs to be aware of. It's updated once every time the node starts. - constants: historydb.Constants in JSON. This struct contains all the hermez network constants gathered via the ethereum client by the node. It's written once every time the node starts. - The HistoryDB contains methods to get and update each one of these columns individually. - The HistoryDB contains all methods that query the DB and prepare objects that will appear in the StateAPI endpoint. - The configuration used in for the `serveapi` cli/node command is defined in `config.APIServer`, and is a subset of `node.Config` in order to allow reusing the same configuration file of the node if desired. - A new object is introduced in the api: `StateAPIUpdater`, which contains all the necessary information to update the StateAPI in the DB periodically by the node. - Moved the types `SCConsts`, `SCVariables` and `SCVariablesPtr` from `syncrhonizer` to `common` for convenience.
3 years ago
  1. -- +migrate Up
  2. -- NOTE: We use "DECIMAL(78,0)" to encode go *big.Int types. All the *big.Int
  3. -- that we deal with represent a value in the SNARK field, which is an integer
  4. -- of 256 bits. `log(2**256, 10) = 77.06`: that is, a 256 bit number can have
  5. -- at most 78 digits, so we use this value to specify the precision in the
  6. -- PostgreSQL DECIMAL guaranteeing that we will never lose precision.
  7. -- History
  8. CREATE TABLE block (
  9. eth_block_num BIGINT PRIMARY KEY,
  10. timestamp TIMESTAMP WITHOUT TIME ZONE NOT NULL,
  11. hash BYTEA NOT NULL
  12. );
  13. CREATE TABLE coordinator (
  14. item_id SERIAL PRIMARY KEY,
  15. bidder_addr BYTEA NOT NULL,
  16. forger_addr BYTEA NOT NULL,
  17. eth_block_num BIGINT NOT NULL REFERENCES block (eth_block_num) ON DELETE CASCADE,
  18. url BYTEA NOT NULL
  19. );
  20. CREATE TABLE batch (
  21. item_id SERIAL PRIMARY KEY,
  22. batch_num BIGINT UNIQUE NOT NULL,
  23. eth_block_num BIGINT NOT NULL REFERENCES block (eth_block_num) ON DELETE CASCADE,
  24. forger_addr BYTEA NOT NULL, -- fake foreign key for coordinator
  25. fees_collected BYTEA NOT NULL,
  26. fee_idxs_coordinator BYTEA NOT NULL,
  27. state_root DECIMAL(78,0) NOT NULL,
  28. num_accounts BIGINT NOT NULL,
  29. last_idx BIGINT NOT NULL,
  30. exit_root DECIMAL(78,0) NOT NULL,
  31. forge_l1_txs_num BIGINT,
  32. slot_num BIGINT NOT NULL,
  33. total_fees_usd NUMERIC
  34. );
  35. CREATE TABLE bid (
  36. item_id SERIAL PRIMARY KEY,
  37. slot_num BIGINT NOT NULL,
  38. bid_value DECIMAL(78,0) NOT NULL,
  39. eth_block_num BIGINT NOT NULL REFERENCES block (eth_block_num) ON DELETE CASCADE,
  40. bidder_addr BYTEA NOT NULL -- fake foreign key for coordinator
  41. );
  42. CREATE TABLE token (
  43. item_id SERIAL PRIMARY KEY,
  44. token_id INT UNIQUE NOT NULL,
  45. eth_block_num BIGINT NOT NULL REFERENCES block (eth_block_num) ON DELETE CASCADE,
  46. eth_addr BYTEA UNIQUE NOT NULL,
  47. name VARCHAR(20) NOT NULL,
  48. symbol VARCHAR(10) NOT NULL,
  49. decimals INT NOT NULL,
  50. usd NUMERIC, -- value of a normalized token (1 token = 10^decimals units)
  51. usd_update TIMESTAMP WITHOUT TIME ZONE
  52. );
  53. -- Add ETH as TokenID 0
  54. INSERT INTO block (
  55. eth_block_num,
  56. timestamp,
  57. hash
  58. ) VALUES (
  59. 0,
  60. '2015-07-30 03:26:13',
  61. '\xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3'
  62. ); -- info from https://etherscan.io/block/0
  63. INSERT INTO token (
  64. token_id,
  65. eth_block_num,
  66. eth_addr,
  67. name,
  68. symbol,
  69. decimals
  70. ) VALUES (
  71. 0,
  72. 0,
  73. '\x0000000000000000000000000000000000000000',
  74. 'Ether',
  75. 'ETH',
  76. 18
  77. );
  78. -- +migrate StatementBegin
  79. CREATE FUNCTION hez_idx(BIGINT, VARCHAR)
  80. RETURNS VARCHAR
  81. AS
  82. $BODY$
  83. BEGIN
  84. RETURN 'hez:' || $2 || ':' || $1;
  85. END;
  86. $BODY$
  87. LANGUAGE plpgsql;
  88. -- +migrate StatementEnd
  89. CREATE TABLE account (
  90. item_id SERIAL,
  91. idx BIGINT PRIMARY KEY,
  92. token_id INT NOT NULL REFERENCES token (token_id) ON DELETE CASCADE,
  93. batch_num BIGINT NOT NULL REFERENCES batch (batch_num) ON DELETE CASCADE,
  94. bjj BYTEA NOT NULL,
  95. eth_addr BYTEA NOT NULL
  96. );
  97. CREATE TABLE account_update (
  98. item_id SERIAL,
  99. eth_block_num BIGINT NOT NULL REFERENCES block (eth_block_num) ON DELETE CASCADE,
  100. batch_num BIGINT NOT NULL REFERENCES batch (batch_num) ON DELETE CASCADE,
  101. idx BIGINT NOT NULL REFERENCES account (idx) ON DELETE CASCADE,
  102. nonce BIGINT NOT NULL,
  103. balance DECIMAL(78,0) NOT NULL
  104. );
  105. CREATE TABLE exit_tree (
  106. item_id SERIAL PRIMARY KEY,
  107. batch_num BIGINT REFERENCES batch (batch_num) ON DELETE CASCADE,
  108. account_idx BIGINT REFERENCES account (idx) ON DELETE CASCADE,
  109. merkle_proof BYTEA NOT NULL,
  110. balance DECIMAL(78,0) NOT NULL,
  111. instant_withdrawn BIGINT REFERENCES block (eth_block_num) ON DELETE SET NULL,
  112. delayed_withdraw_request BIGINT REFERENCES block (eth_block_num) ON DELETE SET NULL,
  113. owner BYTEA,
  114. token BYTEA,
  115. delayed_withdrawn BIGINT REFERENCES block (eth_block_num) ON DELETE SET NULL
  116. );
  117. -- +migrate StatementBegin
  118. CREATE FUNCTION set_token_usd_update()
  119. RETURNS TRIGGER
  120. AS
  121. $BODY$
  122. BEGIN
  123. IF NEW."usd" IS NOT NULL AND NEW."usd_update" IS NULL THEN
  124. NEW."usd_update" = timezone('utc', now());
  125. END IF;
  126. RETURN NEW;
  127. END;
  128. $BODY$
  129. LANGUAGE plpgsql;
  130. -- +migrate StatementEnd
  131. CREATE TRIGGER trigger_token_usd_update BEFORE UPDATE OR INSERT ON token
  132. FOR EACH ROW EXECUTE PROCEDURE set_token_usd_update();
  133. CREATE SEQUENCE tx_item_id;
  134. -- important note about "amount_success" and "deposit_amount_success" (only relevant to L1 user txs):
  135. -- The behaviour should be:
  136. -- When tx is not forged: amount_success = false, deposit_amount_success = false
  137. -- When tx is forged:
  138. -- amount_success = false if the "effective amount" is 0, else true
  139. -- deposit_amount_success = false if the "effective deposit amount" is 0, else true
  140. --
  141. -- However, in order to reduce the amount of updates, by default amount_success and deposit_amount_success will be set to true (when tx is unforged)
  142. -- whne they should be false. This can be worked around at a query level by checking if "batch_num IS NULL" (which indicates that the tx is unforged).
  143. CREATE TABLE tx (
  144. -- Generic TX
  145. item_id INTEGER PRIMARY KEY DEFAULT nextval('tx_item_id'),
  146. is_l1 BOOLEAN NOT NULL,
  147. id BYTEA,
  148. type VARCHAR(40) NOT NULL,
  149. position INT NOT NULL,
  150. from_idx BIGINT,
  151. effective_from_idx BIGINT REFERENCES account (idx) ON DELETE SET NULL,
  152. from_eth_addr BYTEA,
  153. from_bjj BYTEA,
  154. to_idx BIGINT NOT NULL,
  155. to_eth_addr BYTEA,
  156. to_bjj BYTEA,
  157. amount DECIMAL(78,0) NOT NULL,
  158. amount_success BOOLEAN NOT NULL DEFAULT true,
  159. amount_f NUMERIC NOT NULL,
  160. token_id INT NOT NULL REFERENCES token (token_id),
  161. amount_usd NUMERIC, -- Value of the amount in USD at the moment the tx was inserted in the DB
  162. batch_num BIGINT REFERENCES batch (batch_num) ON DELETE SET NULL, -- Can be NULL in the case of L1 txs that are on the queue but not forged yet.
  163. eth_block_num BIGINT NOT NULL REFERENCES block (eth_block_num) ON DELETE CASCADE,
  164. -- L1
  165. to_forge_l1_txs_num BIGINT,
  166. user_origin BOOLEAN,
  167. deposit_amount DECIMAL(78,0),
  168. deposit_amount_success BOOLEAN NOT NULL DEFAULT true,
  169. deposit_amount_f NUMERIC,
  170. deposit_amount_usd NUMERIC,
  171. -- L2
  172. fee INT,
  173. fee_usd NUMERIC,
  174. nonce BIGINT
  175. );
  176. -- +migrate StatementBegin
  177. CREATE FUNCTION fee_percentage(NUMERIC)
  178. RETURNS NUMERIC
  179. AS
  180. $BODY$
  181. DECLARE perc NUMERIC;
  182. BEGIN
  183. SELECT CASE
  184. WHEN $1 = 000 THEN 0.000000e+00
  185. WHEN $1 = 001 THEN 2.675309e-18
  186. WHEN $1 = 002 THEN 8.251782e-18
  187. WHEN $1 = 003 THEN 2.545198e-17
  188. WHEN $1 = 004 THEN 7.850462e-17
  189. WHEN $1 = 005 THEN 2.421414e-16
  190. WHEN $1 = 006 THEN 7.468660e-16
  191. WHEN $1 = 007 THEN 2.303650e-15
  192. WHEN $1 = 008 THEN 7.105427e-15
  193. WHEN $1 = 009 THEN 2.191613e-14
  194. WHEN $1 = 010 THEN 6.759860e-14
  195. WHEN $1 = 011 THEN 2.085026e-13
  196. WHEN $1 = 012 THEN 6.431099e-13
  197. WHEN $1 = 013 THEN 1.983622e-12
  198. WHEN $1 = 014 THEN 6.118327e-12
  199. WHEN $1 = 015 THEN 1.887150e-11
  200. WHEN $1 = 016 THEN 5.820766e-11
  201. WHEN $1 = 017 THEN 1.795370e-10
  202. WHEN $1 = 018 THEN 5.537677e-10
  203. WHEN $1 = 019 THEN 1.708053e-09
  204. WHEN $1 = 020 THEN 5.268356e-09
  205. WHEN $1 = 021 THEN 1.624983e-08
  206. WHEN $1 = 022 THEN 5.012133e-08
  207. WHEN $1 = 023 THEN 1.545953e-07
  208. WHEN $1 = 024 THEN 4.768372e-07
  209. WHEN $1 = 025 THEN 1.470767e-06
  210. WHEN $1 = 026 THEN 4.536465e-06
  211. WHEN $1 = 027 THEN 1.399237e-05
  212. WHEN $1 = 028 THEN 4.315837e-05
  213. WHEN $1 = 029 THEN 1.331186e-04
  214. WHEN $1 = 030 THEN 4.105940e-04
  215. WHEN $1 = 031 THEN 1.266445e-03
  216. WHEN $1 = 032 THEN 3.906250e-03
  217. WHEN $1 = 033 THEN 4.044004e-03
  218. WHEN $1 = 034 THEN 4.186615e-03
  219. WHEN $1 = 035 THEN 4.334256e-03
  220. WHEN $1 = 036 THEN 4.487103e-03
  221. WHEN $1 = 037 THEN 4.645340e-03
  222. WHEN $1 = 038 THEN 4.809158e-03
  223. WHEN $1 = 039 THEN 4.978752e-03
  224. WHEN $1 = 040 THEN 5.154328e-03
  225. WHEN $1 = 041 THEN 5.336095e-03
  226. WHEN $1 = 042 THEN 5.524272e-03
  227. WHEN $1 = 043 THEN 5.719085e-03
  228. WHEN $1 = 044 THEN 5.920768e-03
  229. WHEN $1 = 045 THEN 6.129563e-03
  230. WHEN $1 = 046 THEN 6.345722e-03
  231. WHEN $1 = 047 THEN 6.569503e-03
  232. WHEN $1 = 048 THEN 6.801176e-03
  233. WHEN $1 = 049 THEN 7.041019e-03
  234. WHEN $1 = 050 THEN 7.289320e-03
  235. WHEN $1 = 051 THEN 7.546378e-03
  236. WHEN $1 = 052 THEN 7.812500e-03
  237. WHEN $1 = 053 THEN 8.088007e-03
  238. WHEN $1 = 054 THEN 8.373230e-03
  239. WHEN $1 = 055 THEN 8.668512e-03
  240. WHEN $1 = 056 THEN 8.974206e-03
  241. WHEN $1 = 057 THEN 9.290681e-03
  242. WHEN $1 = 058 THEN 9.618316e-03
  243. WHEN $1 = 059 THEN 9.957505e-03
  244. WHEN $1 = 060 THEN 1.030866e-02
  245. WHEN $1 = 061 THEN 1.067219e-02
  246. WHEN $1 = 062 THEN 1.104854e-02
  247. WHEN $1 = 063 THEN 1.143817e-02
  248. WHEN $1 = 064 THEN 1.184154e-02
  249. WHEN $1 = 065 THEN 1.225913e-02
  250. WHEN $1 = 066 THEN 1.269144e-02
  251. WHEN $1 = 067 THEN 1.313901e-02
  252. WHEN $1 = 068 THEN 1.360235e-02
  253. WHEN $1 = 069 THEN 1.408204e-02
  254. WHEN $1 = 070 THEN 1.457864e-02
  255. WHEN $1 = 071 THEN 1.509276e-02
  256. WHEN $1 = 072 THEN 1.562500e-02
  257. WHEN $1 = 073 THEN 1.617601e-02
  258. WHEN $1 = 074 THEN 1.674646e-02
  259. WHEN $1 = 075 THEN 1.733702e-02
  260. WHEN $1 = 076 THEN 1.794841e-02
  261. WHEN $1 = 077 THEN 1.858136e-02
  262. WHEN $1 = 078 THEN 1.923663e-02
  263. WHEN $1 = 079 THEN 1.991501e-02
  264. WHEN $1 = 080 THEN 2.061731e-02
  265. WHEN $1 = 081 THEN 2.134438e-02
  266. WHEN $1 = 082 THEN 2.209709e-02
  267. WHEN $1 = 083 THEN 2.287634e-02
  268. WHEN $1 = 084 THEN 2.368307e-02
  269. WHEN $1 = 085 THEN 2.451825e-02
  270. WHEN $1 = 086 THEN 2.538289e-02
  271. WHEN $1 = 087 THEN 2.627801e-02
  272. WHEN $1 = 088 THEN 2.720471e-02
  273. WHEN $1 = 089 THEN 2.816408e-02
  274. WHEN $1 = 090 THEN 2.915728e-02
  275. WHEN $1 = 091 THEN 3.018551e-02
  276. WHEN $1 = 092 THEN 3.125000e-02
  277. WHEN $1 = 093 THEN 3.235203e-02
  278. WHEN $1 = 094 THEN 3.349292e-02
  279. WHEN $1 = 095 THEN 3.467405e-02
  280. WHEN $1 = 096 THEN 3.589682e-02
  281. WHEN $1 = 097 THEN 3.716272e-02
  282. WHEN $1 = 098 THEN 3.847326e-02
  283. WHEN $1 = 099 THEN 3.983002e-02
  284. WHEN $1 = 100 THEN 4.123462e-02
  285. WHEN $1 = 101 THEN 4.268876e-02
  286. WHEN $1 = 102 THEN 4.419417e-02
  287. WHEN $1 = 103 THEN 4.575268e-02
  288. WHEN $1 = 104 THEN 4.736614e-02
  289. WHEN $1 = 105 THEN 4.903651e-02
  290. WHEN $1 = 106 THEN 5.076577e-02
  291. WHEN $1 = 107 THEN 5.255603e-02
  292. WHEN $1 = 108 THEN 5.440941e-02
  293. WHEN $1 = 109 THEN 5.632815e-02
  294. WHEN $1 = 110 THEN 5.831456e-02
  295. WHEN $1 = 111 THEN 6.037102e-02
  296. WHEN $1 = 112 THEN 6.250000e-02
  297. WHEN $1 = 113 THEN 6.470406e-02
  298. WHEN $1 = 114 THEN 6.698584e-02
  299. WHEN $1 = 115 THEN 6.934809e-02
  300. WHEN $1 = 116 THEN 7.179365e-02
  301. WHEN $1 = 117 THEN 7.432544e-02
  302. WHEN $1 = 118 THEN 7.694653e-02
  303. WHEN $1 = 119 THEN 7.966004e-02
  304. WHEN $1 = 120 THEN 8.246924e-02
  305. WHEN $1 = 121 THEN 8.537752e-02
  306. WHEN $1 = 122 THEN 8.838835e-02
  307. WHEN $1 = 123 THEN 9.150536e-02
  308. WHEN $1 = 124 THEN 9.473229e-02
  309. WHEN $1 = 125 THEN 9.807301e-02
  310. WHEN $1 = 126 THEN 1.015315e-01
  311. WHEN $1 = 127 THEN 1.051121e-01
  312. WHEN $1 = 128 THEN 1.088188e-01
  313. WHEN $1 = 129 THEN 1.126563e-01
  314. WHEN $1 = 130 THEN 1.166291e-01
  315. WHEN $1 = 131 THEN 1.207420e-01
  316. WHEN $1 = 132 THEN 1.250000e-01
  317. WHEN $1 = 133 THEN 1.294081e-01
  318. WHEN $1 = 134 THEN 1.339717e-01
  319. WHEN $1 = 135 THEN 1.386962e-01
  320. WHEN $1 = 136 THEN 1.435873e-01
  321. WHEN $1 = 137 THEN 1.486509e-01
  322. WHEN $1 = 138 THEN 1.538931e-01
  323. WHEN $1 = 139 THEN 1.593201e-01
  324. WHEN $1 = 140 THEN 1.649385e-01
  325. WHEN $1 = 141 THEN 1.707550e-01
  326. WHEN $1 = 142 THEN 1.767767e-01
  327. WHEN $1 = 143 THEN 1.830107e-01
  328. WHEN $1 = 144 THEN 1.894646e-01
  329. WHEN $1 = 145 THEN 1.961460e-01
  330. WHEN $1 = 146 THEN 2.030631e-01
  331. WHEN $1 = 147 THEN 2.102241e-01
  332. WHEN $1 = 148 THEN 2.176376e-01
  333. WHEN $1 = 149 THEN 2.253126e-01
  334. WHEN $1 = 150 THEN 2.332582e-01
  335. WHEN $1 = 151 THEN 2.414841e-01
  336. WHEN $1 = 152 THEN 2.500000e-01
  337. WHEN $1 = 153 THEN 2.588162e-01
  338. WHEN $1 = 154 THEN 2.679434e-01
  339. WHEN $1 = 155 THEN 2.773924e-01
  340. WHEN $1 = 156 THEN 2.871746e-01
  341. WHEN $1 = 157 THEN 2.973018e-01
  342. WHEN $1 = 158 THEN 3.077861e-01
  343. WHEN $1 = 159 THEN 3.186402e-01
  344. WHEN $1 = 160 THEN 3.298770e-01
  345. WHEN $1 = 161 THEN 3.415101e-01
  346. WHEN $1 = 162 THEN 3.535534e-01
  347. WHEN $1 = 163 THEN 3.660214e-01
  348. WHEN $1 = 164 THEN 3.789291e-01
  349. WHEN $1 = 165 THEN 3.922920e-01
  350. WHEN $1 = 166 THEN 4.061262e-01
  351. WHEN $1 = 167 THEN 4.204482e-01
  352. WHEN $1 = 168 THEN 4.352753e-01
  353. WHEN $1 = 169 THEN 4.506252e-01
  354. WHEN $1 = 170 THEN 4.665165e-01
  355. WHEN $1 = 171 THEN 4.829682e-01
  356. WHEN $1 = 172 THEN 5.000000e-01
  357. WHEN $1 = 173 THEN 5.176325e-01
  358. WHEN $1 = 174 THEN 5.358867e-01
  359. WHEN $1 = 175 THEN 5.547847e-01
  360. WHEN $1 = 176 THEN 5.743492e-01
  361. WHEN $1 = 177 THEN 5.946036e-01
  362. WHEN $1 = 178 THEN 6.155722e-01
  363. WHEN $1 = 179 THEN 6.372803e-01
  364. WHEN $1 = 180 THEN 6.597540e-01
  365. WHEN $1 = 181 THEN 6.830201e-01
  366. WHEN $1 = 182 THEN 7.071068e-01
  367. WHEN $1 = 183 THEN 7.320428e-01
  368. WHEN $1 = 184 THEN 7.578583e-01
  369. WHEN $1 = 185 THEN 7.845841e-01
  370. WHEN $1 = 186 THEN 8.122524e-01
  371. WHEN $1 = 187 THEN 8.408964e-01
  372. WHEN $1 = 188 THEN 8.705506e-01
  373. WHEN $1 = 189 THEN 9.012505e-01
  374. WHEN $1 = 190 THEN 9.330330e-01
  375. WHEN $1 = 191 THEN 9.659363e-01
  376. WHEN $1 = 192 THEN 1.000000e+00
  377. WHEN $1 = 193 THEN 2.000000e+00
  378. WHEN $1 = 194 THEN 4.000000e+00
  379. WHEN $1 = 195 THEN 8.000000e+00
  380. WHEN $1 = 196 THEN 1.600000e+01
  381. WHEN $1 = 197 THEN 3.200000e+01
  382. WHEN $1 = 198 THEN 6.400000e+01
  383. WHEN $1 = 199 THEN 1.280000e+02
  384. WHEN $1 = 200 THEN 2.560000e+02
  385. WHEN $1 = 201 THEN 5.120000e+02
  386. WHEN $1 = 202 THEN 1.024000e+03
  387. WHEN $1 = 203 THEN 2.048000e+03
  388. WHEN $1 = 204 THEN 4.096000e+03
  389. WHEN $1 = 205 THEN 8.192000e+03
  390. WHEN $1 = 206 THEN 1.638400e+04
  391. WHEN $1 = 207 THEN 3.276800e+04
  392. WHEN $1 = 208 THEN 6.553600e+04
  393. WHEN $1 = 209 THEN 1.310720e+05
  394. WHEN $1 = 210 THEN 2.621440e+05
  395. WHEN $1 = 211 THEN 5.242880e+05
  396. WHEN $1 = 212 THEN 1.048576e+06
  397. WHEN $1 = 213 THEN 2.097152e+06
  398. WHEN $1 = 214 THEN 4.194304e+06
  399. WHEN $1 = 215 THEN 8.388608e+06
  400. WHEN $1 = 216 THEN 1.677722e+07
  401. WHEN $1 = 217 THEN 3.355443e+07
  402. WHEN $1 = 218 THEN 6.710886e+07
  403. WHEN $1 = 219 THEN 1.342177e+08
  404. WHEN $1 = 220 THEN 2.684355e+08
  405. WHEN $1 = 221 THEN 5.368709e+08
  406. WHEN $1 = 222 THEN 1.073742e+09
  407. WHEN $1 = 223 THEN 2.147484e+09
  408. WHEN $1 = 224 THEN 4.294967e+09
  409. WHEN $1 = 225 THEN 8.589935e+09
  410. WHEN $1 = 226 THEN 1.717987e+10
  411. WHEN $1 = 227 THEN 3.435974e+10
  412. WHEN $1 = 228 THEN 6.871948e+10
  413. WHEN $1 = 229 THEN 1.374390e+11
  414. WHEN $1 = 230 THEN 2.748779e+11
  415. WHEN $1 = 231 THEN 5.497558e+11
  416. WHEN $1 = 232 THEN 1.099512e+12
  417. WHEN $1 = 233 THEN 2.199023e+12
  418. WHEN $1 = 234 THEN 4.398047e+12
  419. WHEN $1 = 235 THEN 8.796093e+12
  420. WHEN $1 = 236 THEN 1.759219e+13
  421. WHEN $1 = 237 THEN 3.518437e+13
  422. WHEN $1 = 238 THEN 7.036874e+13
  423. WHEN $1 = 239 THEN 1.407375e+14
  424. WHEN $1 = 240 THEN 2.814750e+14
  425. WHEN $1 = 241 THEN 5.629500e+14
  426. WHEN $1 = 242 THEN 1.125900e+15
  427. WHEN $1 = 243 THEN 2.251800e+15
  428. WHEN $1 = 244 THEN 4.503600e+15
  429. WHEN $1 = 245 THEN 9.007199e+15
  430. WHEN $1 = 246 THEN 1.801440e+16
  431. WHEN $1 = 247 THEN 3.602880e+16
  432. WHEN $1 = 248 THEN 7.205759e+16
  433. WHEN $1 = 249 THEN 1.441152e+17
  434. WHEN $1 = 250 THEN 2.882304e+17
  435. WHEN $1 = 251 THEN 5.764608e+17
  436. WHEN $1 = 252 THEN 1.152922e+18
  437. WHEN $1 = 253 THEN 2.305843e+18
  438. WHEN $1 = 254 THEN 4.611686e+18
  439. WHEN $1 = 255 THEN 9.223372e+18
  440. END INTO perc;
  441. RETURN perc;
  442. END;
  443. $BODY$
  444. LANGUAGE plpgsql;
  445. -- +migrate StatementEnd
  446. -- +migrate StatementBegin
  447. CREATE FUNCTION set_tx()
  448. RETURNS TRIGGER
  449. AS
  450. $BODY$
  451. DECLARE
  452. _value NUMERIC;
  453. _usd_update TIMESTAMP;
  454. _tx_timestamp TIMESTAMP;
  455. BEGIN
  456. IF NEW.is_l1 THEN
  457. -- Validate L1 Tx
  458. IF NEW.user_origin IS NULL OR
  459. NEW.from_eth_addr IS NULL OR
  460. NEW.from_bjj IS NULL OR
  461. NEW.deposit_amount IS NULL OR
  462. NEW.deposit_amount_f IS NULL OR
  463. (NOT NEW.user_origin AND NEW.batch_num IS NULL) THEN -- If is Coordinator L1, must include batch_num
  464. RAISE EXCEPTION 'Invalid L1 tx: %', NEW;
  465. END IF;
  466. ELSE
  467. -- Validate L2 Tx
  468. IF NEW.batch_num IS NULL OR NEW.nonce IS NULL THEN
  469. RAISE EXCEPTION 'Invalid L2 tx: %', NEW;
  470. END IF;
  471. -- Set fee if it's null
  472. IF NEW.fee IS NULL THEN
  473. NEW.fee = (SELECT 0);
  474. END IF;
  475. -- Set token_id
  476. NEW."token_id" = (SELECT token_id FROM account WHERE idx = NEW."from_idx");
  477. -- Set from_{eth_addr,bjj}
  478. SELECT INTO NEW."from_eth_addr", NEW."from_bjj" eth_addr, bjj FROM account WHERE idx = NEW.from_idx;
  479. END IF;
  480. -- Set USD related
  481. SELECT INTO _value, _usd_update, _tx_timestamp
  482. usd / POWER(10, decimals), usd_update, timestamp FROM token INNER JOIN block on token.eth_block_num = block.eth_block_num WHERE token_id = NEW.token_id;
  483. IF _usd_update - interval '24 hours' < _usd_update AND _usd_update + interval '24 hours' > _usd_update THEN
  484. IF _value > 0.0 THEN
  485. IF NEW."amount_f" > 0.0 THEN
  486. NEW."amount_usd" = (SELECT _value * NEW."amount_f");
  487. IF NOT NEW."is_l1" AND NEW."fee" > 0 THEN
  488. NEW."fee_usd" = (SELECT NEW."amount_usd" * fee_percentage(NEW.fee::NUMERIC));
  489. END IF;
  490. END IF;
  491. IF NEW."is_l1" AND NEW."deposit_amount_f" > 0.0 THEN
  492. NEW."deposit_amount_usd" = (SELECT _value * NEW.deposit_amount_f);
  493. END IF;
  494. END IF;
  495. END IF;
  496. -- Set to_{eth_addr,bjj}
  497. IF NEW."to_idx" > 255 THEN
  498. SELECT INTO NEW."to_eth_addr", NEW."to_bjj" eth_addr, bjj FROM account WHERE idx = NEW."to_idx";
  499. END IF;
  500. RETURN NEW;
  501. END;
  502. $BODY$
  503. LANGUAGE plpgsql;
  504. -- +migrate StatementEnd
  505. CREATE TRIGGER trigger_set_tx BEFORE INSERT ON tx
  506. FOR EACH ROW EXECUTE PROCEDURE set_tx();
  507. -- +migrate StatementBegin
  508. CREATE FUNCTION forge_l1_user_txs()
  509. RETURNS TRIGGER
  510. AS
  511. $BODY$
  512. BEGIN
  513. IF NEW.forge_l1_txs_num IS NOT NULL THEN
  514. UPDATE tx
  515. SET item_id = upd.item_id, batch_num = NEW.batch_num
  516. FROM (
  517. SELECT id, nextval('tx_item_id') FROM tx
  518. WHERE user_origin AND NEW.forge_l1_txs_num = to_forge_l1_txs_num
  519. ORDER BY position
  520. FOR UPDATE
  521. ) as upd (id, item_id)
  522. WHERE tx.id = upd.id;
  523. END IF;
  524. RETURN NEW;
  525. END;
  526. $BODY$
  527. LANGUAGE plpgsql;
  528. -- +migrate StatementEnd
  529. CREATE TRIGGER trigger_forge_l1_txs AFTER INSERT ON batch
  530. FOR EACH ROW EXECUTE PROCEDURE forge_l1_user_txs();
  531. CREATE TABLE rollup_vars (
  532. eth_block_num BIGINT PRIMARY KEY REFERENCES block (eth_block_num) ON DELETE CASCADE,
  533. fee_add_token DECIMAL(78,0) NOT NULL,
  534. forge_l1_timeout BIGINT NOT NULL,
  535. withdrawal_delay BIGINT NOT NULL,
  536. buckets BYTEA NOT NULL,
  537. safe_mode BOOLEAN NOT NULL
  538. );
  539. CREATE TABLE bucket_update (
  540. item_id SERIAL PRIMARY KEY,
  541. eth_block_num BIGINT NOT NULL REFERENCES block (eth_block_num) ON DELETE CASCADE,
  542. num_bucket BIGINT NOT NULL,
  543. block_stamp BIGINT NOT NULL,
  544. withdrawals DECIMAL(78,0) NOT NULL
  545. );
  546. CREATE TABLE token_exchange (
  547. item_id SERIAL PRIMARY KEY,
  548. eth_block_num BIGINT NOT NULL REFERENCES block (eth_block_num) ON DELETE CASCADE,
  549. eth_addr BYTEA NOT NULL,
  550. value_usd BIGINT NOT NULL
  551. );
  552. CREATE TABLE escape_hatch_withdrawal (
  553. item_id SERIAL PRIMARY KEY,
  554. eth_block_num BIGINT NOT NULL REFERENCES block (eth_block_num) ON DELETE CASCADE,
  555. who_addr BYTEA NOT NULL,
  556. to_addr BYTEA NOT NULL,
  557. token_addr BYTEA NOT NULL,
  558. amount DECIMAL(78,0) NOT NULL
  559. );
  560. CREATE TABLE auction_vars (
  561. eth_block_num BIGINT PRIMARY KEY REFERENCES block (eth_block_num) ON DELETE CASCADE,
  562. donation_address BYTEA NOT NULL,
  563. boot_coordinator BYTEA NOT NULL,
  564. boot_coordinator_url BYTEA NOT NULL,
  565. default_slot_set_bid BYTEA NOT NULL,
  566. default_slot_set_bid_slot_num BIGINT NOT NULL, -- slot_num after which the new default_slot_set_bid applies
  567. closed_auction_slots INT NOT NULL,
  568. open_auction_slots INT NOT NULL,
  569. allocation_ratio VARCHAR(200),
  570. outbidding INT NOT NULL,
  571. slot_deadline INT NOT NULL
  572. );
  573. CREATE TABLE wdelayer_vars (
  574. eth_block_num BIGINT PRIMARY KEY REFERENCES block (eth_block_num) ON DELETE CASCADE,
  575. gov_address BYTEA NOT NULL,
  576. emg_address BYTEA NOT NULL,
  577. withdrawal_delay BIGINT NOT NULL,
  578. emergency_start_block BIGINT NOT NULL,
  579. emergency_mode BOOLEAN NOT NULL
  580. );
  581. -- L2
  582. CREATE TABLE tx_pool (
  583. tx_id BYTEA PRIMARY KEY,
  584. from_idx BIGINT NOT NULL,
  585. effective_from_eth_addr BYTEA,
  586. effective_from_bjj BYTEA,
  587. to_idx BIGINT,
  588. to_eth_addr BYTEA,
  589. to_bjj BYTEA,
  590. effective_to_eth_addr BYTEA,
  591. effective_to_bjj BYTEA,
  592. token_id INT NOT NULL REFERENCES token (token_id) ON DELETE CASCADE,
  593. amount DECIMAL(78,0) NOT NULL,
  594. amount_f NUMERIC NOT NULL,
  595. fee SMALLINT NOT NULL,
  596. nonce BIGINT NOT NULL,
  597. state CHAR(4) NOT NULL,
  598. info VARCHAR,
  599. signature BYTEA NOT NULL,
  600. timestamp TIMESTAMP WITHOUT TIME ZONE DEFAULT timezone('utc', now()),
  601. batch_num BIGINT,
  602. rq_from_idx BIGINT,
  603. rq_to_idx BIGINT,
  604. rq_to_eth_addr BYTEA,
  605. rq_to_bjj BYTEA,
  606. rq_token_id INT,
  607. rq_amount DECIMAL(78,0),
  608. rq_fee SMALLINT,
  609. rq_nonce BIGINT,
  610. tx_type VARCHAR(40) NOT NULL,
  611. client_ip VARCHAR,
  612. external_delete BOOLEAN NOT NULL DEFAULT false
  613. );
  614. -- +migrate StatementBegin
  615. CREATE FUNCTION set_pool_tx()
  616. RETURNS TRIGGER
  617. AS
  618. $BODY$
  619. BEGIN
  620. SELECT INTO NEW."effective_from_eth_addr", NEW."effective_from_bjj" eth_addr, bjj FROM account WHERE idx = NEW."from_idx";
  621. -- Set to_{eth_addr,bjj}
  622. IF NEW.to_idx > 255 THEN
  623. SELECT INTO NEW."effective_to_eth_addr", NEW."effective_to_bjj" eth_addr, bjj FROM account WHERE idx = NEW."to_idx";
  624. ELSE
  625. NEW."effective_to_eth_addr" = NEW."to_eth_addr";
  626. NEW."effective_to_bjj" = NEW."to_bjj";
  627. END IF;
  628. RETURN NEW;
  629. END;
  630. $BODY$
  631. LANGUAGE plpgsql;
  632. -- +migrate StatementEnd
  633. CREATE TRIGGER trigger_set_pool_tx BEFORE INSERT ON tx_pool
  634. FOR EACH ROW EXECUTE PROCEDURE set_pool_tx();
  635. CREATE TABLE account_creation_auth (
  636. eth_addr BYTEA PRIMARY KEY,
  637. bjj BYTEA NOT NULL,
  638. signature BYTEA NOT NULL,
  639. timestamp TIMESTAMP WITHOUT TIME ZONE NOT NULL DEFAULT timezone('utc', now())
  640. );
  641. CREATE TABLE node_info (
  642. item_id SERIAL PRIMARY KEY,
  643. state BYTEA, -- object returned by GET /state
  644. config BYTEA, -- Node config
  645. -- max_pool_txs BIGINT, -- L2DB config
  646. -- min_fee NUMERIC, -- L2DB config
  647. constants BYTEA -- info of the network that is constant
  648. );
  649. INSERT INTO node_info(item_id) VALUES (1); -- Always have a single row that we will update
  650. -- +migrate Down
  651. -- triggers
  652. DROP TRIGGER IF EXISTS trigger_token_usd_update ON token;
  653. DROP TRIGGER IF EXISTS trigger_set_tx ON tx;
  654. DROP TRIGGER IF EXISTS trigger_forge_l1_txs ON batch;
  655. DROP TRIGGER IF EXISTS trigger_set_pool_tx ON tx_pool;
  656. -- functions
  657. DROP FUNCTION IF EXISTS hez_idx;
  658. DROP FUNCTION IF EXISTS set_token_usd_update;
  659. DROP FUNCTION IF EXISTS fee_percentage;
  660. DROP FUNCTION IF EXISTS set_tx;
  661. DROP FUNCTION IF EXISTS forge_l1_user_txs;
  662. DROP FUNCTION IF EXISTS set_pool_tx;
  663. -- drop tables IF EXISTS
  664. DROP TABLE IF EXISTS node_info;
  665. DROP TABLE IF EXISTS account_creation_auth;
  666. DROP TABLE IF EXISTS tx_pool;
  667. DROP TABLE IF EXISTS auction_vars;
  668. DROP TABLE IF EXISTS rollup_vars;
  669. DROP TABLE IF EXISTS escape_hatch_withdrawal;
  670. DROP TABLE IF EXISTS bucket_update;
  671. DROP TABLE IF EXISTS token_exchange;
  672. DROP TABLE IF EXISTS wdelayer_vars;
  673. DROP TABLE IF EXISTS tx;
  674. DROP TABLE IF EXISTS exit_tree;
  675. DROP TABLE IF EXISTS account_update;
  676. DROP TABLE IF EXISTS account;
  677. DROP TABLE IF EXISTS token;
  678. DROP TABLE IF EXISTS bid;
  679. DROP TABLE IF EXISTS batch;
  680. DROP TABLE IF EXISTS coordinator;
  681. DROP TABLE IF EXISTS block;
  682. -- sequences
  683. DROP SEQUENCE IF EXISTS tx_item_id;