Skip to content

Commit

Permalink
add tests and fix getter setter
Browse files Browse the repository at this point in the history
  • Loading branch information
tommady committed Aug 18, 2024
1 parent 5213b55 commit 8e4b342
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 31 deletions.
24 changes: 15 additions & 9 deletions evm-exporter/src/getter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,22 +77,22 @@ impl Getter for PgGetter {
.conn
.get()?
.query_one("SELECT latest_height FROM common", &[])?
.get("latest_height"))
.get::<&str, i64>("latest_height") as u32)
}
fn lowest_height(&self) -> Result<u32> {
Ok(self
.conn
.get()?
.query_one("SELECT lowest_height FROM common", &[])?
.get("lowest_height"))
.get::<&str, i64>("lowest_height") as u32)
}
fn get_balance(&self, height: u32, address: H160) -> Result<U256> {
Ok(U256::from_str(
self.conn
.get()?
.query_one(
"SELECT balance FROM balance WHERE address = $1 AND height = $2",
&[&address.to_string(), &height],
&[&address.to_string(), &(height as i64)],
)?
.get("balance"),
)?)
Expand All @@ -103,7 +103,7 @@ impl Getter for PgGetter {
.get()?
.query_one(
"SELECT nonce FROM nonce WHERE address = $1 AND height = $2",
&[&address.to_string(), &height],
&[&address.to_string(), &(height as i64)],
)?
.get("nonce"),
)?)
Expand All @@ -114,7 +114,7 @@ impl Getter for PgGetter {
.get()?
.query_one(
"SELECT code FROM byte_code WHERE address = $1 AND height = $2",
&[&address.to_string(), &height],
&[&address.to_string(), &(height as i64)],
)?
.get("code"),
)?)
Expand All @@ -132,7 +132,7 @@ impl Getter for PgGetter {
.get()?
.query_one(
"SELECT 1 FROM state WHERE address = $1 AND height = $2",
&[&address.to_string(), &height],
&[&address.to_string(), &(height as i64)],
)?
.is_empty())
}
Expand All @@ -142,7 +142,7 @@ impl Getter for PgGetter {
.get()?
.query_one(
"SELECT value FROM state WHERE idx = $1 AND address = $2 AND height = $3",
&[&index.to_string(), &address.to_string(), &height],
&[&index.to_string(), &address.to_string(), &(height as i64)],
)?
.get("value"),
)?)
Expand Down Expand Up @@ -267,15 +267,21 @@ impl Getter for PgGetter {
Ok(U256::from_str(
self.conn
.get()?
.query_one("SELECT value FROM issuance WHERE height = $1", &[&height])?
.query_one(
"SELECT value FROM issuance WHERE height = $1",
&[&(height as i64)],
)?
.get("value"),
)?)
}
fn get_allowances(&self, height: u32, owner: H160, spender: H160) -> Result<U256> {
Ok(U256::from_str(
self.conn
.get()?
.query_one("SELECT value FROM allowances WHERE owner = $1 AND spender = $2 AND height = $3", &[&owner.to_string(),&spender.to_string(),&height])?
.query_one(
"SELECT value FROM allowances WHERE owner = $1 AND spender = $2 AND height = $3",
&[&owner.to_string(),&spender.to_string(),&( height as i64 )],
)?
.get("value"),
)?)
}
Expand Down
29 changes: 14 additions & 15 deletions evm-exporter/src/setter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,53 +105,53 @@ impl Setter for PgSetter {
}
fn set_height(&self, height: u32) -> Result<()> {
self.conn.get()?
.execute("UPDATE common set latest_height = $1", &[&height])?;
.execute("UPDATE common set latest_height = $1", &[&( height as i64 )])?;
Ok(())
}
fn set_lowest_height(&self, height: u32) -> Result<()> {
self.conn.get()?
.execute("UPDATE common set lowest_height = $1", &[&height])?;
.execute("UPDATE common set lowest_height = $1", &[&( height as i64 )])?;
Ok(())
}
fn set_balance(&self, height: u32, address: H160, balance: U256) -> Result<()> {
self.conn.get()?.execute(
"INSERT INTO balance(balance, address, height) VALUES($1, $2, $3)",
&[&balance.to_string(), &address.to_string(), &height],
&[&balance.to_string(), &address.to_string(), &( height as i64 )],
)?;
Ok(())
}
fn remove_balance(&self, height: u32, address: H160) -> Result<()> {
self.conn.get()?.execute(
"DELETE FROM balance WHERE height = $1 AND address = $2",
&[&height, &address.to_string()],
&[&( height as i64 ), &address.to_string()],
)?;
Ok(())
}
fn set_nonce(&self, height: u32, address: H160, nonce: U256) -> Result<()> {
self.conn.get()?.execute(
"INSERT INTO nonce(nonce, address, height) VALUES($1, $2, $3)",
&[&nonce.to_string(), &address.to_string(), &height],
&[&nonce.to_string(), &address.to_string(), &( height as i64 )],
)?;
Ok(())
}
fn remove_nonce(&self, height: u32, address: H160) -> Result<()> {
self.conn.get()?.execute(
"DELETE FROM nonce WHERE height = $1 AND address = $2",
&[&height, &address.to_string()],
&[&( height as i64 ), &address.to_string()],
)?;
Ok(())
}
fn set_byte_code(&self, height: u32, address: H160, code: Vec<u8>) -> Result<()> {
self.conn.get()?.execute(
"INSERT INTO byte_code(code, address, height) VALUES($1, $2, $3)",
&[&hex::encode(code), &address.to_string(), &height],
&[&hex::encode(code), &address.to_string(), &( height as i64 )],
)?;
Ok(())
}
fn remove_byte_code(&self, height: u32, address: H160) -> Result<()> {
self.conn.get()?.execute(
"DELETE FROM byte_code WHERE height = $1 AND address = $2",
&[&height, &address.to_string()],
&[&( height as i64 ), &address.to_string()],
)?;
Ok(())
}
Expand All @@ -162,15 +162,15 @@ impl Setter for PgSetter {
&value.to_string(),
&index.to_string(),
&address.to_string(),
&height,
&( height as i64 ),
],
)?;
Ok(())
}
fn remove_state(&self, height: u32, address: H160, index: H256) -> Result<()> {
self.conn.get()?.execute(
"DELETE FROM state WHERE height = $1 AND address = $2 AND idx = $3",
&[&height, &address.to_string(), &index.to_string()],
&[&( height as i64 ), &address.to_string(), &index.to_string()],
)?;
Ok(())
}
Expand All @@ -181,8 +181,7 @@ impl Setter for PgSetter {
statuses: Vec<TransactionStatus>,
) -> Result<()> {
self.conn.get()?.execute(
r"INSERT INTO block_info(block_hash, block_height, block, receipt, statuses)
VALUES($1, $2, $3, $4, $5)",
"INSERT INTO block_info(block_hash, block_height, block, receipt, statuses) VALUES($1, $2, $3, $4, $5)",
&[
&block.header.hash().to_string(),
&block.header.number.to_string(),
Expand All @@ -194,7 +193,7 @@ impl Setter for PgSetter {

for (i, tx) in statuses.iter().enumerate() {
self.conn.get()?.execute(
"INSERT INTO transactions(transaction_hash, transaction_index) VALUES($1, $2))",
"INSERT INTO transactions(transaction_hash, transaction_index) VALUES($1, $2)",
&[
&tx.transaction_hash.to_string(),
&serde_json::to_string(&(block.header.hash().to_string(), i as u32))?,
Expand Down Expand Up @@ -230,7 +229,7 @@ impl Setter for PgSetter {
}
fn set_pending_tx(&self, transaction: LegacyTransaction) -> Result<()> {
let sign_address = recover_signer(&transaction)?;
let latest_height: u32 = self
let latest_height: i64 = self
.conn
.get()?
.query_one("SELECT latest_height FROM common", &[])?
Expand Down Expand Up @@ -282,7 +281,7 @@ impl Setter for PgSetter {
Ok(())
}
fn set_total_issuance(&self, height: u32, value: U256) -> Result<()> {
self.conn.get()?.execute("INSERT INTO issuance(value, height) VALUES($1, $2)", &[&value.to_string(), &height])?;
self.conn.get()?.execute("INSERT INTO issuance(value, height) VALUES($1, $2)", &[&value.to_string(), &( height as i64 )])?;
Ok(())
}
fn set_allowances(
Expand Down
14 changes: 7 additions & 7 deletions rocksdb-exporter/run_rocksdb_exporter.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/bin/bash

URL=${LATEST_URL}
REDIS_HOST="${REDIS_HOST:=127.0.0.1}"
REDIS_PORT="${REDIS_PORT:=6379}"
ROOT_DIR=$(dirname `readlink -f $0`)
# REDIS_HOST="${REDIS_HOST:=127.0.0.1}"
# REDIS_PORT="${REDIS_PORT:=6379}"
ROOT_DIR=$(dirname $(readlink -f $0))
rm -f ${ROOT_DIR}/snapshot.tar.gz

if ! wget -O "${ROOT_DIR}/latest" "${URL}"; then
Expand All @@ -24,9 +24,9 @@ rm -rvf ${ROOT_DIR}/data/ledger

tar -xvf ${ROOT_DIR}/snapshot/snapshot.tar.gz -C ${ROOT_DIR}

cat ${ROOT_DIR}/versioned-kv.lua | redis-cli -h ${REDIS_HOST} -p ${REDIS_PORT} -x FUNCTION LOAD REPLACE
sed -i "s#127.0.0.1#${REDIS_HOST}#g" ${ROOT_DIR}/rocksdb-exporter-config.toml
sed -i "s#6379#${REDIS_PORT}#g" ${ROOT_DIR}/rocksdb-exporter-config.toml
# cat ${ROOT_DIR}/versioned-kv.lua | redis-cli -h ${REDIS_HOST} -p ${REDIS_PORT} -x FUNCTION LOAD REPLACE
# sed -i "s#127.0.0.1#${REDIS_HOST}#g" ${ROOT_DIR}/rocksdb-exporter-config.toml
# sed -i "s#6379#${REDIS_PORT}#g" ${ROOT_DIR}/rocksdb-exporter-config.toml

export EXPORT_CONFIG_FILE_PATH=${ROOT_DIR}/rocksdb-exporter-config.toml
# export EXPORT_CONFIG_FILE_PATH=${ROOT_DIR}/rocksdb-exporter-config.toml
${ROOT_DIR}/rocksdb-exporter
75 changes: 75 additions & 0 deletions tests/compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
version: '3'

services:
postgres:
image: docker.io/postgres:latest
ports:
- "5432:5432"
volumes:
- "db_data:/var/lib/postgresql/data"
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=mysecretpassword
- POSTGRES_DB=mydatabase

migrate:
image: docker.io/postgres:latest
depends_on: postgres
volumes:
- ./migration.sh:/data/migration.sh
command: ["/bin/bash", "-c", "/data/migration.sh"]

rocksdb_exporter:
image: "localhost/rocksdb_exporter:latest"
depends_on: migrate
environment:
- LATEST_URL=https://prod-testnet-us-west-2-chain-data-backup.s3.us-west-2.amazonaws.com/latest
- EXPORT_CONFIG_FILE_PATH=/config/config.toml
volumes:
- ./config/rocksdb-exporter-config.toml:/config/config.toml
- rocksdb_exporter_snapshot:/rocksdb-exporter/snapshot

# web3_service:
# image: "localhost/enterprise-web3-platform:latest"
# depends_on: rocksdb_exporter
# environment:
# - WEB3_CONFIG_FILE_PATH=/config/config.toml
# ports:
# - "8545:8545"
# - "8546:8546"
# volumes:
# - ./config/web3-service-config.toml:/config/config.toml

# findorad:
# container_name: findorad
# image: localhost/findorad-binary-image:test
# command:
# - 'node'
# - '--enable-snapshot'
# - '--snapshot-mode=external'
# - '--enable-enterprise-web3'
# environment:
# - LEDGER_DIR=/var/ledger
# - TENDERMINT_HOST=0.0.0.0
# - ABCI_HOST=0.0.0.0
# - SERVER_HOST=0.0.0.0
# - LEDGER_HOST=0.0.0.0
# - RUST_LOG=INFO
# - ABCI_LOG_LEVEL="info,abciapp=info,baseapp=info,account=info,ethereum=info,evm=info,eth_rpc=info"
# - ENABLE_LEDGER_SERVICE=true
# - ENABLE_QUERY_SERVICE=true
# - ENABLE_ETH_API_SERVICE=1
# - EVM_CHAIN_ID=2152
# - RUC_LOG_LEVEL=ERROR
# - POSTGRES_URI="postgresql://postgres:[email protected]:5432/mydatabase?sslmode=disable"
# volumes:
# - findorad_ledger:/var/ledger
# - findorad_tendermint:/root/.tendermint



volumes:
db_data:
findorad_ledger:
findorad_tendermint:
rocksdb_exporter_snapshot:
5 changes: 5 additions & 0 deletions tests/config/rocksdb-exporter-config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
state_db_path = "/rocksdb-exporter/data/ledger/state.db"
history_db_path = "/rocksdb-exporter/data/ledger/history.db"
postgres_uri = "postgresql://postgres:mysecretpassword@postgres:5432/mydatabase?sslmode=disable"
redis_url = [""]
clear = true
7 changes: 7 additions & 0 deletions tests/config/web3-service-config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
http_port = 8545
ws_port = 8546
tendermint_url = "https://prod-testnet.prod.findora.org:26657"
chain_id = 2152
gas_price = 10000000000
postgres_uri = "postgresql://postgres:[email protected]:5432/mydatabase?sslmode=disable"
redis_url = [""]
16 changes: 16 additions & 0 deletions tests/migration.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash

apt update && apt install wget -y

wget https://raw.githubusercontent.com/FindoraNetwork/enterprise-web3/main/evm-exporter/migrations/allowances.sql && psql "postgresql://postgres:mysecretpassword@postgres:5432/mydatabase" -f allowances.sql
wget https://raw.githubusercontent.com/FindoraNetwork/enterprise-web3/main/evm-exporter/migrations/balance.sql && psql "postgresql://postgres:mysecretpassword@postgres:5432/mydatabase" -f balance.sql
wget https://raw.githubusercontent.com/FindoraNetwork/enterprise-web3/main/evm-exporter/migrations/block_info.sql && psql "postgresql://postgres:mysecretpassword@postgres:5432/mydatabase" -f block_info.sql
wget https://raw.githubusercontent.com/FindoraNetwork/enterprise-web3/main/evm-exporter/migrations/byte_code.sql && psql "postgresql://postgres:mysecretpassword@postgres:5432/mydatabase" -f byte_code.sql
wget https://raw.githubusercontent.com/FindoraNetwork/enterprise-web3/main/evm-exporter/migrations/common.sql && psql "postgresql://postgres:mysecretpassword@postgres:5432/mydatabase" -f common.sql
wget https://raw.githubusercontent.com/FindoraNetwork/enterprise-web3/main/evm-exporter/migrations/issuance.sql && psql "postgresql://postgres:mysecretpassword@postgres:5432/mydatabase" -f issuance.sql
wget https://raw.githubusercontent.com/FindoraNetwork/enterprise-web3/main/evm-exporter/migrations/nonce.sql && psql "postgresql://postgres:mysecretpassword@postgres:5432/mydatabase" -f nonce.sql
wget https://raw.githubusercontent.com/FindoraNetwork/enterprise-web3/main/evm-exporter/migrations/pending_byte_code.sql && psql "postgresql://postgres:mysecretpassword@postgres:5432/mydatabase" -f pending_byte_code.sql
wget https://raw.githubusercontent.com/FindoraNetwork/enterprise-web3/main/evm-exporter/migrations/pending_state.sql && psql "postgresql://postgres:mysecretpassword@postgres:5432/mydatabase" -f pending_state.sql
wget https://raw.githubusercontent.com/FindoraNetwork/enterprise-web3/main/evm-exporter/migrations/pending_transactions.sql && psql "postgresql://postgres:mysecretpassword@postgres:5432/mydatabase" -f pending_transactions.sql
wget https://raw.githubusercontent.com/FindoraNetwork/enterprise-web3/main/evm-exporter/migrations/state.sql && psql "postgresql://postgres:mysecretpassword@postgres:5432/mydatabase" -f state.sql
wget https://raw.githubusercontent.com/FindoraNetwork/enterprise-web3/main/evm-exporter/migrations/transactions.sql && psql "postgresql://postgres:mysecretpassword@postgres:5432/mydatabase" -f transactions.sql

0 comments on commit 8e4b342

Please sign in to comment.