Skip to content

Commit

Permalink
fixes after dirty rebase; canonical hash/header finder function is in…
Browse files Browse the repository at this point in the history
… this commit now
  • Loading branch information
i-norden committed Oct 20, 2020
1 parent 6369835 commit 20af343
Show file tree
Hide file tree
Showing 30 changed files with 1,582 additions and 1,031 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,15 @@ The corresponding CLI flags can be found with the `./ipld-eth-server serve --hel
ipcPath = "~/.vulcanize/vulcanize.ipc" # $SERVER_IPC_PATH
wsPath = "127.0.0.1:8081" # $SERVER_WS_PATH
httpPath = "127.0.0.1:8082" # $SERVER_HTTP_PATH

[ethereum]
chainID = "1" # $ETH_CHAIN_ID
defaultSender = "" # $ETH_DEFAULT_SENDER_ADDR
```

The `database` fields are for connecting to a Postgres database that has been/is being populated by [ipld-eth-indexer](https://github.com/vulcanize/ipld-eth-indexer).
The `server` fields set the paths for exposing the ipld-eth-server endpoints
The `ethereum` fields set the chainID and default sender address to use for EVM simulation


### Endpoints
Expand Down
18 changes: 6 additions & 12 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,22 +100,16 @@ func init() {
serveCmd.PersistentFlags().String("server-http-path", "", "vdb server http path")
serveCmd.PersistentFlags().String("server-ipc-path", "", "vdb server ipc path")

serveCmd.PersistentFlags().String("eth-ws-path", "", "ws url for ethereum node")
serveCmd.PersistentFlags().String("eth-http-path", "", "http url for ethereum node")
serveCmd.PersistentFlags().String("eth-node-id", "", "eth node id")
serveCmd.PersistentFlags().String("eth-client-name", "", "eth client name")
serveCmd.PersistentFlags().String("eth-genesis-block", "", "eth genesis block hash")
serveCmd.PersistentFlags().String("eth-network-id", "", "eth network id")
serveCmd.PersistentFlags().String("eth-chain-id", "1", "eth chain id")
serveCmd.PersistentFlags().String("eth-default-sender", "", "default sender address")
serveCmd.PersistentFlags().String("eth-rpc-gas-cap", "", "rpc gas cap (for eth_Call execution)")

// and their bindings
viper.BindPFlag("server.wsPath", serveCmd.PersistentFlags().Lookup("server-ws-path"))
viper.BindPFlag("server.httpPath", serveCmd.PersistentFlags().Lookup("server-http-path"))
viper.BindPFlag("server.ipcPath", serveCmd.PersistentFlags().Lookup("server-ipc-path"))

viper.BindPFlag("ethereum.wsPath", serveCmd.PersistentFlags().Lookup("eth-ws-path"))
viper.BindPFlag("ethereum.httpPath", serveCmd.PersistentFlags().Lookup("eth-http-path"))
viper.BindPFlag("ethereum.nodeID", serveCmd.PersistentFlags().Lookup("eth-node-id"))
viper.BindPFlag("ethereum.clientName", serveCmd.PersistentFlags().Lookup("eth-client-name"))
viper.BindPFlag("ethereum.genesisBlock", serveCmd.PersistentFlags().Lookup("eth-genesis-block"))
viper.BindPFlag("ethereum.networkID", serveCmd.PersistentFlags().Lookup("eth-network-id"))
viper.BindPFlag("ethereum.chainID", rootCmd.PersistentFlags().Lookup("eth-chain-id"))
viper.BindPFlag("ethereum.defaultSender", rootCmd.PersistentFlags().Lookup("eth-default-sender"))
viper.BindPFlag("ethereum.rpcGasCap", rootCmd.PersistentFlags().Lookup("eth-rpc-gas-cap"))
}
124 changes: 124 additions & 0 deletions db/migrations/00014_create_cid_indexes.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
-- +goose Up
-- header indexes
CREATE INDEX block_number_index ON eth.header_cids USING brin (block_number);

CREATE INDEX block_hash_index ON eth.header_cids USING btree (block_hash);

CREATE INDEX header_cid_index ON eth.header_cids USING btree (cid);

CREATE INDEX header_mh_index ON eth.header_cids USING btree (mh_key);

CREATE INDEX state_root_index ON eth.header_cids USING btree (state_root);

CREATE INDEX timestamp_index ON eth.header_cids USING brin (timestamp);

-- transaction indexes
CREATE INDEX tx_header_id_index ON eth.transaction_cids USING btree (header_id);

CREATE INDEX tx_hash_index ON eth.transaction_cids USING btree (tx_hash);

CREATE INDEX tx_cid_index ON eth.transaction_cids USING btree (cid);

CREATE INDEX tx_mh_index ON eth.transaction_cids USING btree (mh_key);

CREATE INDEX tx_dst_index ON eth.transaction_cids USING btree (dst);

CREATE INDEX tx_src_index ON eth.transaction_cids USING btree (src);

CREATE INDEX tx_data_index ON eth.transaction_cids USING btree (tx_data);

-- receipt indexes
CREATE INDEX rct_tx_id_index ON eth.receipt_cids USING btree (tx_id);

CREATE INDEX rct_cid_index ON eth.receipt_cids USING btree (cid);

CREATE INDEX rct_mh_index ON eth.receipt_cids USING btree (mh_key);

CREATE INDEX rct_contract_index ON eth.receipt_cids USING btree (contract);

CREATE INDEX rct_contract_hash_index ON eth.receipt_cids USING btree (contract_hash);

CREATE INDEX rct_topic0_index ON eth.receipt_cids USING gin (topic0s);

CREATE INDEX rct_topic1_index ON eth.receipt_cids USING gin (topic1s);

CREATE INDEX rct_topic2_index ON eth.receipt_cids USING gin (topic2s);

CREATE INDEX rct_topic3_index ON eth.receipt_cids USING gin (topic3s);

CREATE INDEX rct_log_contract_index ON eth.receipt_cids USING gin (log_contracts);

-- state node indexes
CREATE INDEX state_header_id_index ON eth.state_cids USING btree (header_id);

CREATE INDEX state_leaf_key_index ON eth.state_cids USING btree (state_leaf_key);

CREATE INDEX state_cid_index ON eth.state_cids USING btree (cid);

CREATE INDEX state_mh_index ON eth.state_cids USING btree (mh_key);

CREATE INDEX state_path_index ON eth.state_cids USING btree (state_path);

-- storage node indexes
CREATE INDEX storage_state_id_index ON eth.storage_cids USING btree (state_id);

CREATE INDEX storage_leaf_key_index ON eth.storage_cids USING btree (storage_leaf_key);

CREATE INDEX storage_cid_index ON eth.storage_cids USING btree (cid);

CREATE INDEX storage_mh_index ON eth.storage_cids USING btree (mh_key);

CREATE INDEX storage_path_index ON eth.storage_cids USING btree (storage_path);

-- state accounts indexes
CREATE INDEX account_state_id_index ON eth.state_accounts USING btree (state_id);

CREATE INDEX storage_root_index ON eth.state_accounts USING btree (storage_root);

-- +goose Down
-- state account indexes
DROP INDEX eth.storage_root_index;
DROP INDEX eth.account_state_id_index;

-- storage node indexes
DROP INDEX eth.storage_path_index;
DROP INDEX eth.storage_mh_index;
DROP INDEX eth.storage_cid_index;
DROP INDEX eth.storage_leaf_key_index;
DROP INDEX eth.storage_state_id_index;

-- state node indexes
DROP INDEX eth.state_path_index;
DROP INDEX eth.state_mh_index;
DROP INDEX eth.state_cid_index;
DROP INDEX eth.state_leaf_key_index;
DROP INDEX eth.state_header_id_index;

-- receipt indexes
DROP INDEX eth.rct_log_contract_index;
DROP INDEX eth.rct_topic3_index;
DROP INDEX eth.rct_topic2_index;
DROP INDEX eth.rct_topic1_index;
DROP INDEX eth.rct_topic0_index;
DROP INDEX eth.rct_contract_hash_index;
DROP INDEX eth.rct_contract_index;
DROP INDEX eth.rct_mh_index;
DROP INDEX eth.rct_cid_index;
DROP INDEX eth.rct_tx_id_index;

-- transaction indexes
DROP INDEX eth.tx_data_index;
DROP INDEX eth.tx_src_index;
DROP INDEX eth.tx_dst_index;
DROP INDEX eth.tx_mh_index;
DROP INDEX eth.tx_cid_index;
DROP INDEX eth.tx_hash_index;
DROP INDEX eth.tx_header_id_index;

-- header indexes
DROP INDEX eth.timestamp_index;
DROP INDEX eth.state_root_index;
DROP INDEX eth.header_mh_index;
DROP INDEX eth.header_cid_index;
DROP INDEX eth.block_hash_index;
DROP INDEX eth.block_number_index;
48 changes: 48 additions & 0 deletions db/migrations/00015_create_canonical_hash_finder_functions.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
-- +goose Up
-- +goose StatementBegin
-- returns the number of child headers that reference backwards to the header with the provided hash
CREATE OR REPLACE FUNCTION header_weight(hash VARCHAR(66)) RETURNS BIGINT
AS $$
WITH RECURSIVE validator AS (
SELECT block_hash, parent_hash, block_number
FROM eth.header_cids
WHERE block_hash = hash
UNION
SELECT eth.header_cids.block_hash, eth.header_cids.parent_hash, eth.header_cids.block_number
FROM eth.header_cids
INNER JOIN validator
ON eth.header_cids.parent_hash = validator.block_hash
AND eth.header_cids.block_number = validator.block_number + 1
)
SELECT COUNT(*) FROM validator;
$$ LANGUAGE SQL;
-- +goose StatementEnd

-- +goose StatementBegin
-- returns the id for the header at the provided height which is heaviest
CREATE OR REPLACE FUNCTION canonical_header(height BIGINT) RETURNS INT AS
$BODY$
DECLARE
current_weight INT;
heaviest_weight INT DEFAULT 0;
heaviest_id INT;
r eth.header_cids%ROWTYPE;
BEGIN
FOR r IN SELECT * FROM eth.header_cids
WHERE block_number = height
LOOP
SELECT INTO current_weight * FROM header_weight(r.block_hash);
IF current_weight > heaviest_weight THEN
heaviest_weight := current_weight;
heaviest_id := r.id;
END IF;
END LOOP;
RETURN heaviest_id;
END
$BODY$
LANGUAGE 'plpgsql';
-- +goose StatementEnd

-- +goose Down
DROP FUNCTION header_weight;
DROP FUNCTION canonical_header;
Loading

0 comments on commit 20af343

Please sign in to comment.