-
-
Notifications
You must be signed in to change notification settings - Fork 79
/
pg_dump.sql
346 lines (246 loc) · 7.73 KB
/
pg_dump.sql
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
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
--
-- PostgreSQL database dump
--
-- Dumped from database version 15.1
-- Dumped by pg_dump version 15.0
SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;
--
-- Name: pool; Type: SCHEMA; Schema: -; Owner: -
--
CREATE SCHEMA pool;
--
-- Name: pay_solution(integer); Type: PROCEDURE; Schema: pool; Owner: -
--
CREATE PROCEDURE pool.pay_solution(IN solution_id_arg integer)
LANGUAGE plpython3u
AS $_$solution = plpy.execute(f"SELECT * FROM solution WHERE id = {solution_id_arg}", 1)
if solution.nrows() == 0:
plpy.error("Solution id does not exist")
if solution[0]["paid"]:
plpy.error("Solution already paid")
solution_id = solution[0]["id"]
shares = plpy.execute(f"SELECT * FROM share WHERE solution_id = {solution_id}")
if shares.nrows() == 0:
plpy.fatal("No share data for solution")
data = {}
for share in shares:
data[share["address"]] = share["share"]
raw_reward = solution[0]["reward"]
reward = int(raw_reward * 0.995)
total_shares = sum(data.values())
reward_per_share = reward // total_shares
def get_plan(name, stmt, types):
if name in SD:
return SD[name]
plan = plpy.prepare(stmt, types)
SD[name] = plan
return plan
payout_plan = get_plan("payout_plan", "INSERT INTO payout (solution_id, address, amount) VALUES ($1, $2, $3)", ["integer", "text", "bigint"])
balance_plan = get_plan("balance_plan", "INSERT INTO balance (address, unpaid) VALUES ($1, $2) ON CONFLICT (address) DO UPDATE SET unpaid = balance.unpaid + $2", ["text", "bigint"])
stats_plan = get_plan("stats_plan", "INSERT INTO stats (key, value) VALUES ($1, $2) ON CONFLICT (key) DO UPDATE SET value = stats.value + $2", ["text", "bigint"])
solution_plan = get_plan("block_plan", "UPDATE solution SET paid = true WHERE id = $1", ["integer"])
try:
with plpy.subtransaction():
paid = 0
for miner, share in data.items():
amount = reward_per_share * share
payout_plan.execute([solution_id, miner, amount])
balance_plan.execute([miner, amount])
solution_plan.execute([solution_id])
paid += amount
stats_plan.execute(["total_paid", paid])
stats_plan.execute(["total_fee", raw_reward - reward])
stats_plan.execute(["total_rounding", reward - paid])
except plpy.SPIError as e:
plpy.fatal(f"Error while updating database: {e.args}")
$_$;
SET default_tablespace = '';
SET default_table_access_method = heap;
--
-- Name: balance; Type: TABLE; Schema: pool; Owner: -
--
CREATE TABLE pool.balance (
id integer NOT NULL,
address text NOT NULL,
unpaid bigint DEFAULT 0 NOT NULL,
paid bigint DEFAULT 0 NOT NULL,
pending bigint DEFAULT 0 NOT NULL
);
--
-- Name: balance_id_seq; Type: SEQUENCE; Schema: pool; Owner: -
--
CREATE SEQUENCE pool.balance_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
--
-- Name: balance_id_seq; Type: SEQUENCE OWNED BY; Schema: pool; Owner: -
--
ALTER SEQUENCE pool.balance_id_seq OWNED BY pool.balance.id;
--
-- Name: solution; Type: TABLE; Schema: pool; Owner: -
--
CREATE TABLE pool.solution (
id integer NOT NULL,
height bigint,
reward bigint,
"timestamp" bigint DEFAULT EXTRACT(epoch FROM now()) NOT NULL,
paid boolean DEFAULT false NOT NULL,
valid boolean DEFAULT false NOT NULL,
solution_id text NOT NULL,
checked integer DEFAULT 0 NOT NULL
);
--
-- Name: block_id_seq; Type: SEQUENCE; Schema: pool; Owner: -
--
CREATE SEQUENCE pool.block_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
--
-- Name: block_id_seq; Type: SEQUENCE OWNED BY; Schema: pool; Owner: -
--
ALTER SEQUENCE pool.block_id_seq OWNED BY pool.solution.id;
--
-- Name: payout; Type: TABLE; Schema: pool; Owner: -
--
CREATE TABLE pool.payout (
id integer NOT NULL,
solution_id integer NOT NULL,
address text NOT NULL,
amount bigint NOT NULL,
"timestamp" integer DEFAULT EXTRACT(epoch FROM now())
);
--
-- Name: payout_id_seq; Type: SEQUENCE; Schema: pool; Owner: -
--
CREATE SEQUENCE pool.payout_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
--
-- Name: payout_id_seq; Type: SEQUENCE OWNED BY; Schema: pool; Owner: -
--
ALTER SEQUENCE pool.payout_id_seq OWNED BY pool.payout.id;
--
-- Name: share; Type: TABLE; Schema: pool; Owner: -
--
CREATE TABLE pool.share (
id integer NOT NULL,
solution_id integer NOT NULL,
address text NOT NULL,
share bigint NOT NULL
);
--
-- Name: share_id_seq; Type: SEQUENCE; Schema: pool; Owner: -
--
CREATE SEQUENCE pool.share_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
--
-- Name: share_id_seq; Type: SEQUENCE OWNED BY; Schema: pool; Owner: -
--
ALTER SEQUENCE pool.share_id_seq OWNED BY pool.share.id;
--
-- Name: stats; Type: TABLE; Schema: pool; Owner: -
--
CREATE TABLE pool.stats (
key text NOT NULL,
value bigint
);
--
-- Name: balance id; Type: DEFAULT; Schema: pool; Owner: -
--
ALTER TABLE ONLY pool.balance ALTER COLUMN id SET DEFAULT nextval('pool.balance_id_seq'::regclass);
--
-- Name: payout id; Type: DEFAULT; Schema: pool; Owner: -
--
ALTER TABLE ONLY pool.payout ALTER COLUMN id SET DEFAULT nextval('pool.payout_id_seq'::regclass);
--
-- Name: share id; Type: DEFAULT; Schema: pool; Owner: -
--
ALTER TABLE ONLY pool.share ALTER COLUMN id SET DEFAULT nextval('pool.share_id_seq'::regclass);
--
-- Name: solution id; Type: DEFAULT; Schema: pool; Owner: -
--
ALTER TABLE ONLY pool.solution ALTER COLUMN id SET DEFAULT nextval('pool.block_id_seq'::regclass);
--
-- Name: balance balance_pk; Type: CONSTRAINT; Schema: pool; Owner: -
--
ALTER TABLE ONLY pool.balance
ADD CONSTRAINT balance_pk PRIMARY KEY (id);
--
-- Name: payout payout_pk; Type: CONSTRAINT; Schema: pool; Owner: -
--
ALTER TABLE ONLY pool.payout
ADD CONSTRAINT payout_pk PRIMARY KEY (id);
--
-- Name: share share_pk; Type: CONSTRAINT; Schema: pool; Owner: -
--
ALTER TABLE ONLY pool.share
ADD CONSTRAINT share_pk PRIMARY KEY (id);
--
-- Name: solution solution_pk; Type: CONSTRAINT; Schema: pool; Owner: -
--
ALTER TABLE ONLY pool.solution
ADD CONSTRAINT solution_pk PRIMARY KEY (id);
--
-- Name: stats stats_pk; Type: CONSTRAINT; Schema: pool; Owner: -
--
ALTER TABLE ONLY pool.stats
ADD CONSTRAINT stats_pk PRIMARY KEY (key);
--
-- Name: balance_address_uindex; Type: INDEX; Schema: pool; Owner: -
--
CREATE UNIQUE INDEX balance_address_uindex ON pool.balance USING btree (address);
--
-- Name: payout_address_index; Type: INDEX; Schema: pool; Owner: -
--
CREATE INDEX payout_address_index ON pool.payout USING btree (address);
--
-- Name: solution_height_index; Type: INDEX; Schema: pool; Owner: -
--
CREATE INDEX solution_height_index ON pool.solution USING btree (height);
--
-- Name: solution_paid_index; Type: INDEX; Schema: pool; Owner: -
--
CREATE INDEX solution_paid_index ON pool.solution USING btree (paid);
--
-- Name: solution_valid_index; Type: INDEX; Schema: pool; Owner: -
--
CREATE INDEX solution_valid_index ON pool.solution USING btree (valid);
--
-- Name: payout payout_solution_id_fk; Type: FK CONSTRAINT; Schema: pool; Owner: -
--
ALTER TABLE ONLY pool.payout
ADD CONSTRAINT payout_solution_id_fk FOREIGN KEY (solution_id) REFERENCES pool.solution(id);
--
-- Name: share share_solution_id_fk; Type: FK CONSTRAINT; Schema: pool; Owner: -
--
ALTER TABLE ONLY pool.share
ADD CONSTRAINT share_solution_id_fk FOREIGN KEY (solution_id) REFERENCES pool.solution(id);
--
-- PostgreSQL database dump complete
--