Skip to content

Commit 331a33e

Browse files
#368 move sql schema to distinct file (#586)
* Introduce proxy/db_scheme.py * Debug * Introduce run-db-creation.sh * Use entrypoint: proxy/run-db-creation.sh * Add dbcreation.log * Debug * Debug * Add dbcreation.log * Use from ..proxy * Debug * Debug * Introduce dbcreation_mode to run proxy * Change paths * Will run in dbcreation mode with SOLANA_URL and EVM_LOADER * Debug * Debug * Debug * SOLANA_URL: http://solana:8899 * Add logged_groups * Debug * Log schema * Use psql * Add postgresql-client-common in the docker image * Add postgresql-client in the docker image * Set network_mode: container:postgres * Remove networks * Use network_mode: service:postgres * Remove hostname * Remove hostname * ports: - "5432:5432" * networks: - net * Add ports: - "5432" * Add networks: - net * Try to use links: - postgres * Use export PGPASSWORD=${POSTGRES_PASSWORD} to set the password for psql Use psql -h ${POSTGRES_HOST} ... * Use run_dbcreation() * Correction after the prev merge * Correction after the prev merge * Lowercase in function names * Refactor to introduce scheme.sql * Debug * Debug * Debug * Add table airdrop_scheduled Add table constants * Revert some changes * Fixed: psql:proxy/db/scheme.sql:73: ERROR: syntax error at or near "{" LINE 1: ...IF NOT EXISTS neon_transaction_logs_block_hash ON {table_nam... * Use run_indexer(solana_url) * Use run_airdropper with 7 args * Remove creation.py * Remove creation.py * Remove dbcreation_mode * check it * check it * Catch "Operator has NO resources!" exception * Catch "Operator has NO resources!" exception * Get rid of create_table in sql_dict.py * Get rid of stuff from sql_dict * Get rid of crating table stuff from TrxReceiptsStorage * Roll back NEON_ERC20_MAX_AMOUNT NEON_ETH_MAX_AMOUNT * Spit and polish Co-authored-by: rozhkovdmitrii <[email protected]>
1 parent 22b10f9 commit 331a33e

18 files changed

+267
-233
lines changed

.buildkite/pipeline.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ steps:
1616
- "solana.log"
1717
- "measurements.log"
1818
- "evm_loader.log"
19+
- "dbcreation.log"
1920
- "faucet.log"
2021
- "airdropper.log"
2122
- "indexer.log"

.buildkite/steps/deploy-test.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ function cleanup_docker {
4444

4545
if docker logs solana >solana.log 2>&1; then echo "solana logs saved"; fi
4646
if docker logs evm_loader >evm_loader.log 2>&1; then echo "evm_loader logs saved"; fi
47+
if docker logs dbcreation >dbcreation.log 2>&1; then echo "dbcreation logs saved"; fi
4748
if docker logs faucet >faucet.log 2>&1; then echo "faucet logs saved"; fi
4849
if docker logs airdropper >airdropper.log 2>&1; then echo "airdropper logs saved"; fi
4950
if docker logs indexer >indexer.log 2>&1; then echo "indexer logs saved"; fi

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ WORKDIR /opt
1313

1414
RUN apt update && \
1515
DEBIAN_FRONTEND=noninteractive apt install -y git software-properties-common openssl curl parallel \
16-
ca-certificates python3-pip python3-venv && \
16+
ca-certificates python3-pip python3-venv postgresql-client && \
1717
python3 -m venv venv && \
1818
pip3 install --upgrade pip && \
1919
/bin/bash -c "source venv/bin/activate" && \

proxy/__main__.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@
1616
from .indexer.indexer import run_indexer
1717

1818
if __name__ == '__main__':
19+
solana_url = os.environ['SOLANA_URL']
20+
evm_loader_id = os.environ['EVM_LOADER']
21+
print(f"Will run with SOLANA_URL={solana_url}; EVM_LOADER={evm_loader_id}")
22+
1923
airdropper_mode = os.environ.get('AIRDROPPER_MODE', 'False').lower() in [1, 'true', 'True']
2024
indexer_mode = os.environ.get('INDEXER_MODE', 'False').lower() in [1, 'true', 'True']
25+
2126
if airdropper_mode:
2227
print("Will run in airdropper mode")
23-
solana_url = os.environ['SOLANA_URL']
2428
pyth_mapping_account = PublicKey(os.environ['PYTH_MAPPING_ACCOUNT'])
2529
faucet_url = os.environ['FAUCET_URL']
2630
wrapper_whitelist = os.environ['INDEXER_ERC20_WRAPPER_WHITELIST']
@@ -40,9 +44,6 @@
4044
max_conf)
4145
elif indexer_mode:
4246
print("Will run in indexer mode")
43-
44-
solana_url = os.environ['SOLANA_URL']
45-
4647
run_indexer(solana_url)
4748
else:
4849
entry_point()

proxy/common_neon/costs.py

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,7 @@
66

77
class SQLCost(BaseDB):
88
def __init__(self):
9-
BaseDB.__init__(self)
10-
11-
def _create_table_sql(self) -> str:
12-
self._table_name = 'OPERATOR_COST'
13-
return f"""
14-
CREATE TABLE IF NOT EXISTS {self._table_name} (
15-
id SERIAL PRIMARY KEY,
16-
hash char(64),
17-
cost bigint,
18-
used_gas bigint,
19-
sender char(40),
20-
to_address char(40) ,
21-
sig char(100),
22-
status varchar(100),
23-
reason varchar(100)
24-
);
25-
"""
9+
BaseDB.__init__(self, 'OPERATOR_COST')
2610

2711
def insert(self, hash, cost, used_gas, sender, to_address, sig, status, reason):
2812
with self._conn.cursor() as cur:

proxy/db/scheme.sql

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
CREATE TABLE IF NOT EXISTS constants (
2+
key TEXT UNIQUE,
3+
value BYTEA
4+
);
5+
6+
CREATE TABLE IF NOT EXISTS airdrop_scheduled (
7+
key TEXT UNIQUE,
8+
value BYTEA
9+
);
10+
11+
CREATE TABLE IF NOT EXISTS OPERATOR_COST (
12+
id SERIAL PRIMARY KEY,
13+
hash char(64),
14+
cost bigint,
15+
used_gas bigint,
16+
sender char(40),
17+
to_address char(40) ,
18+
sig char(100),
19+
status varchar(100),
20+
reason varchar(100)
21+
);
22+
23+
CREATE TABLE IF NOT EXISTS neon_accounts (
24+
neon_account CHAR(42),
25+
pda_account VARCHAR(50),
26+
code_account VARCHAR(50),
27+
slot BIGINT,
28+
code TEXT,
29+
30+
UNIQUE(pda_account, code_account)
31+
);
32+
33+
CREATE TABLE IF NOT EXISTS failed_airdrop_attempts (
34+
attempt_time BIGINT,
35+
eth_address TEXT,
36+
reason TEXT
37+
);
38+
CREATE INDEX IF NOT EXISTS failed_attempt_time_idx ON failed_airdrop_attempts (attempt_time);
39+
40+
CREATE TABLE IF NOT EXISTS airdrop_ready (
41+
eth_address TEXT UNIQUE,
42+
scheduled_ts BIGINT,
43+
finished_ts BIGINT,
44+
duration INTEGER,
45+
amount_galans INTEGER
46+
);
47+
48+
CREATE TABLE IF NOT EXISTS solana_block (
49+
slot BIGINT,
50+
hash CHAR(66),
51+
52+
parent_hash CHAR(66),
53+
blocktime BIGINT,
54+
signatures BYTEA,
55+
56+
UNIQUE(slot),
57+
UNIQUE(hash)
58+
);
59+
60+
CREATE TABLE IF NOT EXISTS neon_transaction_logs (
61+
address CHAR(42),
62+
blockHash CHAR(66),
63+
blockNumber BIGINT,
64+
65+
transactionHash CHAR(66),
66+
transactionLogIndex INT,
67+
topic TEXT,
68+
69+
json TEXT,
70+
71+
UNIQUE(blockNumber, transactionHash, transactionLogIndex)
72+
);
73+
CREATE INDEX IF NOT EXISTS neon_transaction_logs_block_hash ON neon_transaction_logs(blockHash);
74+
CREATE INDEX IF NOT EXISTS neon_transaction_logs_address ON neon_transaction_logs(address);
75+
CREATE INDEX IF NOT EXISTS neon_transaction_logs_topic ON neon_transaction_logs(topic);
76+
77+
CREATE TABLE IF NOT EXISTS solana_neon_transactions (
78+
sol_sign CHAR(88),
79+
neon_sign CHAR(66),
80+
slot BIGINT,
81+
idx INT,
82+
83+
UNIQUE(sol_sign, neon_sign, idx),
84+
UNIQUE(neon_sign, sol_sign, idx)
85+
);
86+
87+
CREATE TABLE IF NOT EXISTS neon_transactions (
88+
neon_sign CHAR(66),
89+
from_addr CHAR(42),
90+
sol_sign CHAR(88),
91+
slot BIGINT,
92+
block_hash CHAR(66),
93+
idx INT,
94+
95+
nonce VARCHAR,
96+
gas_price VARCHAR,
97+
gas_limit VARCHAR,
98+
value VARCHAR,
99+
gas_used VARCHAR,
100+
101+
to_addr CHAR(42),
102+
contract CHAR(42),
103+
104+
status CHAR(3),
105+
106+
return_value TEXT,
107+
108+
v TEXT,
109+
r TEXT,
110+
s TEXT,
111+
112+
calldata TEXT,
113+
logs BYTEA,
114+
115+
UNIQUE(neon_sign),
116+
UNIQUE(sol_sign, idx)
117+
);
118+
119+
CREATE TABLE IF NOT EXISTS transaction_receipts (
120+
slot BIGINT,
121+
signature VARCHAR(88),
122+
trx BYTEA,
123+
PRIMARY KEY (slot, signature)
124+
);
125+
126+
CREATE TABLE IF NOT EXISTS constants (
127+
key TEXT UNIQUE,
128+
value BYTEA
129+
)
130+
131+
CREATE TABLE IF NOT EXISTS airdrop_scheduled (
132+
key TEXT UNIQUE,
133+
value BYTEA
134+
)
135+
136+
CREATE TABLE IF NOT EXISTS transaction_receipts (
137+
slot BIGINT,
138+
signature VARCHAR(88),
139+
trx BYTEA,
140+
PRIMARY KEY (slot, signature)
141+
);
142+
143+
CREATE TABLE IF NOT EXISTS test_storage (
144+
slot BIGINT,
145+
signature VARCHAR(88),
146+
trx BYTEA,
147+
PRIMARY KEY (slot, signature)
148+
);

proxy/docker-compose-test.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ services:
88
SOLANA_URL: http://solana:8899
99
RUST_LOG: solana_runtime::system_instruction_processor=trace,solana_runtime::message_processor=debug,solana_bpf_loader=debug,solana_rbpf=debug
1010
hostname: solana
11+
ports:
12+
- 127.0.0.1:8899:8899
1113
expose:
1214
- "8899"
1315
- "9900"
@@ -53,9 +55,29 @@ services:
5355
start_period: 5s
5456
expose:
5557
- "5432"
58+
ports:
59+
- "5432"
5660
networks:
5761
- net
5862

63+
dbcreation:
64+
container_name: dbcreation
65+
image: neonlabsorg/proxy:${REVISION}
66+
environment:
67+
SOLANA_URL: http://solana:8899
68+
POSTGRES_DB: neon-db
69+
POSTGRES_USER: neon-proxy
70+
POSTGRES_PASSWORD: neon-proxy-pass
71+
POSTGRES_HOST: postgres
72+
entrypoint: proxy/run-dbcreation.sh
73+
networks:
74+
- net
75+
depends_on:
76+
postgres:
77+
condition: service_healthy
78+
evm_loader:
79+
condition: service_completed_successfully
80+
5981
proxy:
6082
container_name: proxy
6183
image: neonlabsorg/proxy:${REVISION}
@@ -77,6 +99,8 @@ services:
7799
OPERATOR_GAS_ACCOUNTS: 0x8966Ef2ae7A109Fd0977F5151b4607dc42929fBD;0x619d670152103a972B67a45b9Be764FF11979E4E
78100
hostname: proxy
79101
depends_on:
102+
dbcreation:
103+
condition: service_completed_successfully
80104
postgres:
81105
condition: service_healthy
82106
evm_loader:
@@ -143,6 +167,8 @@ services:
143167
depends_on:
144168
postgres:
145169
condition: service_healthy
170+
dbcreation:
171+
condition: service_completed_successfully
146172
faucet:
147173
condition: service_started
148174

@@ -161,6 +187,8 @@ services:
161187
condition: service_healthy
162188
evm_loader:
163189
condition: service_completed_successfully
190+
dbcreation:
191+
condition: service_completed_successfully
164192
networks:
165193
- net
166194
entrypoint: proxy/run-indexer.sh

proxy/indexer/accounts_db.py

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,7 @@ def __str__(self):
1616

1717
class NeonAccountDB(BaseDB):
1818
def __init__(self):
19-
BaseDB.__init__(self)
20-
21-
def _create_table_sql(self) -> str:
22-
self._table_name = 'neon_accounts'
23-
return f"""
24-
CREATE TABLE IF NOT EXISTS {self._table_name} (
25-
neon_account CHAR(42),
26-
pda_account VARCHAR(50),
27-
code_account VARCHAR(50),
28-
slot BIGINT,
29-
code TEXT,
30-
31-
UNIQUE(pda_account, code_account)
32-
);"""
19+
BaseDB.__init__(self, 'neon_accounts')
3320

3421
def set_acc_by_request(self, neon_account: str, pda_account: str, code_account: str, code: str):
3522
with self._conn.cursor() as cursor:

proxy/indexer/airdropper.py

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from datetime import datetime
1111
from decimal import Decimal
1212
from logged_groups import logged_group
13+
1314
from ..environment import NEON_PRICE_USD, EVM_LOADER_ID
1415
from ..common_neon.solana_interactor import SolanaInteractor
1516

@@ -20,18 +21,7 @@
2021

2122
class FailedAttempts(BaseDB):
2223
def __init__(self) -> None:
23-
BaseDB.__init__(self)
24-
25-
def _create_table_sql(self) -> str:
26-
self._table_name = 'failed_airdrop_attempts'
27-
return f'''
28-
CREATE TABLE IF NOT EXISTS {self._table_name} (
29-
attempt_time BIGINT,
30-
eth_address TEXT,
31-
reason TEXT
32-
);
33-
CREATE INDEX IF NOT EXISTS failed_attempt_time_idx ON {self._table_name} (attempt_time);
34-
'''
24+
BaseDB.__init__(self, 'failed_airdrop_attempts')
3525

3626
def airdrop_failed(self, eth_address, reason):
3727
with self._conn.cursor() as cur:
@@ -43,19 +33,7 @@ def airdrop_failed(self, eth_address, reason):
4333

4434
class AirdropReadySet(BaseDB):
4535
def __init__(self):
46-
BaseDB.__init__(self)
47-
48-
def _create_table_sql(self) -> str:
49-
self._table_name = 'airdrop_ready'
50-
return f'''
51-
CREATE TABLE IF NOT EXISTS {self._table_name} (
52-
eth_address TEXT UNIQUE,
53-
scheduled_ts BIGINT,
54-
finished_ts BIGINT,
55-
duration INTEGER,
56-
amount_galans INTEGER
57-
)
58-
'''
36+
BaseDB.__init__(self, 'airdrop_ready')
5937

6038
def register_airdrop(self, eth_address: str, airdrop_info: dict):
6139
finished = int(datetime.now().timestamp())

proxy/indexer/base_db.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ class DBQueryExpression(NamedTuple):
2323

2424
@logged_group("neon.Indexer")
2525
class BaseDB:
26-
_create_table_lock = multiprocessing.Lock()
2726

28-
def __init__(self):
27+
def __init__(self, table_name):
28+
self._table_name = table_name
2929
self._conn = psycopg2.connect(
3030
dbname=POSTGRES_DB,
3131
user=POSTGRES_USER,
@@ -34,13 +34,6 @@ def __init__(self):
3434
)
3535
self._conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
3636

37-
with self._create_table_lock:
38-
cursor = self._conn.cursor()
39-
cursor.execute(self._create_table_sql())
40-
41-
def _create_table_sql(self) -> str:
42-
assert False, 'No script for the table'
43-
4437
def _build_expression(self, q: DBQuery) -> DBQueryExpression:
4538

4639
return DBQueryExpression(

0 commit comments

Comments
 (0)