forked from cerc-io/ipld-eth-server
Remove pubsub and replace w/ polling head of chain (#122)
* Rename geth package structs to not be prefaced with package name * No longer need to dump schema since Travis uses migrate * Rearrange history package * Removed double request for receipt from block rewards * Remove Listener + Observers and Replace w/ Polling Head * Potential Short term Issue w/ Infura (ignore these tests for now)
This commit is contained in:
parent
095cb1e7b7
commit
6decf0b54b
@ -77,9 +77,7 @@ func tasks(p *do.Project) {
|
||||
cfg := cmd.LoadConfig(environment)
|
||||
connectString := config.DbConnectionString(cfg.Database)
|
||||
migrate := fmt.Sprintf("migrate -database '%s' -path ./db/migrations up", connectString)
|
||||
dumpSchema := fmt.Sprintf("pg_dump -O -s %s > ./db/schema.sql", cfg.Database.Name)
|
||||
context.Bash(migrate)
|
||||
context.Bash(dumpSchema)
|
||||
})
|
||||
|
||||
p.Task("rollback", nil, func(context *do.Context) {
|
||||
@ -87,9 +85,7 @@ func tasks(p *do.Project) {
|
||||
cfg := cmd.LoadConfig(environment)
|
||||
connectString := config.DbConnectionString(cfg.Database)
|
||||
migrate := fmt.Sprintf("migrate -database '%s' -path ./db/migrations down 1", connectString)
|
||||
dumpSchema := fmt.Sprintf("pg_dump -O -s %s > ./db/schema.sql", cfg.Database.Name)
|
||||
context.Bash(migrate)
|
||||
context.Bash(dumpSchema)
|
||||
})
|
||||
|
||||
p.Task("showContractSummary", nil, func(context *do.Context) {
|
||||
|
@ -35,7 +35,7 @@ func main() {
|
||||
flag.Parse()
|
||||
|
||||
config := cmd.LoadConfig(*environment)
|
||||
blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
|
||||
blockchain := geth.NewBlockchain(config.Client.IPCPath)
|
||||
repository := cmd.LoadPostgres(config.Database, blockchain.Node())
|
||||
|
||||
lastBlockNumber := blockchain.LastBlock().Int64()
|
||||
|
@ -15,7 +15,7 @@ func main() {
|
||||
startingBlockNumber := flag.Int("starting-number", -1, "First block to fill from")
|
||||
flag.Parse()
|
||||
config := cmd.LoadConfig(*environment)
|
||||
blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
|
||||
blockchain := geth.NewBlockchain(config.Client.IPCPath)
|
||||
repository := cmd.LoadPostgres(config.Database, blockchain.Node())
|
||||
numberOfBlocksCreated := history.PopulateMissingBlocks(blockchain, repository, int64(*startingBlockNumber))
|
||||
fmt.Printf("Populated %d blocks", numberOfBlocksCreated)
|
||||
|
@ -1,30 +1,34 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"flag"
|
||||
|
||||
"time"
|
||||
|
||||
"os"
|
||||
|
||||
"github.com/8thlight/vulcanizedb/cmd"
|
||||
"github.com/8thlight/vulcanizedb/pkg/blockchain_listener"
|
||||
"github.com/8thlight/vulcanizedb/pkg/core"
|
||||
"github.com/8thlight/vulcanizedb/pkg/geth"
|
||||
"github.com/8thlight/vulcanizedb/pkg/observers"
|
||||
"github.com/8thlight/vulcanizedb/pkg/history"
|
||||
)
|
||||
|
||||
const (
|
||||
pollingInterval = 7 * time.Second
|
||||
)
|
||||
|
||||
func main() {
|
||||
ticker := time.NewTicker(pollingInterval)
|
||||
defer ticker.Stop()
|
||||
|
||||
environment := flag.String("environment", "", "Environment name")
|
||||
flag.Parse()
|
||||
config := cmd.LoadConfig(*environment)
|
||||
fmt.Printf("Creating Geth Blockchain to: %s\n", config.Client.IPCPath)
|
||||
blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
|
||||
blockchain := geth.NewBlockchain(config.Client.IPCPath)
|
||||
repository := cmd.LoadPostgres(config.Database, blockchain.Node())
|
||||
listener := blockchain_listener.NewBlockchainListener(
|
||||
blockchain,
|
||||
[]core.BlockchainObserver{
|
||||
observers.BlockchainLoggingObserver{},
|
||||
observers.NewBlockchainDbObserver(repository),
|
||||
},
|
||||
)
|
||||
listener.Start()
|
||||
validator := history.NewBlockValidator(blockchain, repository, 15)
|
||||
|
||||
for range ticker.C {
|
||||
window := validator.ValidateBlocks()
|
||||
validator.Log(os.Stdout, window)
|
||||
}
|
||||
}
|
||||
|
@ -18,7 +18,7 @@ func main() {
|
||||
_blockNumber := flag.Int64("block-number", -1, "Block number of summary")
|
||||
flag.Parse()
|
||||
config := cmd.LoadConfig(*environment)
|
||||
blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
|
||||
blockchain := geth.NewBlockchain(config.Client.IPCPath)
|
||||
repository := cmd.LoadPostgres(config.Database, blockchain.Node())
|
||||
blockNumber := cmd.RequestedBlockNumber(_blockNumber)
|
||||
|
||||
|
@ -6,71 +6,45 @@ import (
|
||||
"time"
|
||||
|
||||
"os"
|
||||
"text/template"
|
||||
|
||||
"github.com/8thlight/vulcanizedb/cmd"
|
||||
"github.com/8thlight/vulcanizedb/pkg/blockchain_listener"
|
||||
"github.com/8thlight/vulcanizedb/pkg/core"
|
||||
"github.com/8thlight/vulcanizedb/pkg/geth"
|
||||
"github.com/8thlight/vulcanizedb/pkg/history"
|
||||
"github.com/8thlight/vulcanizedb/pkg/observers"
|
||||
"github.com/8thlight/vulcanizedb/pkg/repositories"
|
||||
)
|
||||
|
||||
const windowTemplate = `Validating Existing Blocks
|
||||
|{{.LowerBound}}|-- Validation Window --|{{.UpperBound}}| {{.MaxBlockNumber}}(HEAD)
|
||||
|
||||
`
|
||||
|
||||
const (
|
||||
windowSize = 24
|
||||
pollingInterval = 10 * time.Second
|
||||
pollingInterval = 7 * time.Second
|
||||
)
|
||||
|
||||
func createListener(blockchain *geth.GethBlockchain, repository repositories.Postgres) blockchain_listener.BlockchainListener {
|
||||
listener := blockchain_listener.NewBlockchainListener(
|
||||
blockchain,
|
||||
[]core.BlockchainObserver{
|
||||
observers.BlockchainLoggingObserver{},
|
||||
observers.NewBlockchainDbObserver(repository),
|
||||
},
|
||||
)
|
||||
return listener
|
||||
}
|
||||
|
||||
func validateBlocks(blockchain *geth.GethBlockchain, repository repositories.Postgres, windowSize int, windowTemplate *template.Template) {
|
||||
window := history.UpdateBlocksWindow(blockchain, repository, windowSize)
|
||||
repository.SetBlocksStatus(blockchain.LastBlock().Int64())
|
||||
windowTemplate.Execute(os.Stdout, window)
|
||||
func backFillAllBlocks(blockchain core.Blockchain, repository repositories.Postgres, missingBlocksPopulated chan int) {
|
||||
go func() {
|
||||
missingBlocksPopulated <- history.PopulateMissingBlocks(blockchain, repository, 0)
|
||||
}()
|
||||
}
|
||||
|
||||
func main() {
|
||||
parsedWindowTemplate := template.Must(template.New("window").Parse(windowTemplate))
|
||||
ticker := time.NewTicker(pollingInterval)
|
||||
defer ticker.Stop()
|
||||
|
||||
environment := flag.String("environment", "", "Environment name")
|
||||
flag.Parse()
|
||||
config := cmd.LoadConfig(*environment)
|
||||
blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
|
||||
blockchain := geth.NewBlockchain(config.Client.IPCPath)
|
||||
repository := cmd.LoadPostgres(config.Database, blockchain.Node())
|
||||
listner := createListener(blockchain, repository)
|
||||
go listner.Start()
|
||||
defer listner.Stop()
|
||||
validator := history.NewBlockValidator(blockchain, repository, 15)
|
||||
|
||||
missingBlocksPopulated := make(chan int)
|
||||
go func() {
|
||||
missingBlocksPopulated <- history.PopulateMissingBlocks(blockchain, repository, 0)
|
||||
}()
|
||||
go backFillAllBlocks(blockchain, repository, missingBlocksPopulated)
|
||||
|
||||
for range ticker.C {
|
||||
validateBlocks(blockchain, repository, windowSize, parsedWindowTemplate)
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
window := validator.ValidateBlocks()
|
||||
validator.Log(os.Stdout, window)
|
||||
case <-missingBlocksPopulated:
|
||||
go func() {
|
||||
missingBlocksPopulated <- history.PopulateMissingBlocks(blockchain, repository, 0)
|
||||
}()
|
||||
default:
|
||||
go backFillAllBlocks(blockchain, repository, missingBlocksPopulated)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -16,7 +16,7 @@ func main() {
|
||||
|
||||
contractAbiString := cmd.GetAbi(*abiFilepath, *contractHash)
|
||||
config := cmd.LoadConfig(*environment)
|
||||
blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
|
||||
blockchain := geth.NewBlockchain(config.Client.IPCPath)
|
||||
repository := cmd.LoadPostgres(config.Database, blockchain.Node())
|
||||
watchedContract := core.Contract{
|
||||
Abi: contractAbiString,
|
||||
|
459
db/schema.sql
459
db/schema.sql
@ -1,459 +0,0 @@
|
||||
--
|
||||
-- PostgreSQL database dump
|
||||
--
|
||||
|
||||
-- Dumped from database version 10.1
|
||||
-- Dumped by pg_dump version 10.1
|
||||
|
||||
SET statement_timeout = 0;
|
||||
SET lock_timeout = 0;
|
||||
SET idle_in_transaction_session_timeout = 0;
|
||||
SET client_encoding = 'UTF8';
|
||||
SET standard_conforming_strings = on;
|
||||
SET check_function_bodies = false;
|
||||
SET client_min_messages = warning;
|
||||
SET row_security = off;
|
||||
|
||||
--
|
||||
-- Name: plpgsql; Type: EXTENSION; Schema: -; Owner: -
|
||||
--
|
||||
|
||||
CREATE EXTENSION IF NOT EXISTS plpgsql WITH SCHEMA pg_catalog;
|
||||
|
||||
|
||||
--
|
||||
-- Name: EXTENSION plpgsql; Type: COMMENT; Schema: -; Owner: -
|
||||
--
|
||||
|
||||
COMMENT ON EXTENSION plpgsql IS 'PL/pgSQL procedural language';
|
||||
|
||||
|
||||
SET search_path = public, pg_catalog;
|
||||
|
||||
SET default_tablespace = '';
|
||||
|
||||
SET default_with_oids = false;
|
||||
|
||||
--
|
||||
-- Name: blocks; Type: TABLE; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
CREATE TABLE blocks (
|
||||
block_number bigint,
|
||||
block_gaslimit double precision,
|
||||
block_gasused double precision,
|
||||
block_time double precision,
|
||||
id integer NOT NULL,
|
||||
block_difficulty bigint,
|
||||
block_hash character varying(66),
|
||||
block_nonce character varying(20),
|
||||
block_parenthash character varying(66),
|
||||
block_size bigint,
|
||||
uncle_hash character varying(66),
|
||||
node_id integer NOT NULL,
|
||||
is_final boolean,
|
||||
block_miner character varying(42),
|
||||
block_extra_data character varying,
|
||||
block_reward numeric,
|
||||
block_uncles_reward numeric
|
||||
);
|
||||
|
||||
|
||||
--
|
||||
-- Name: blocks_id_seq; Type: SEQUENCE; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
CREATE SEQUENCE blocks_id_seq
|
||||
START WITH 1
|
||||
INCREMENT BY 1
|
||||
NO MINVALUE
|
||||
NO MAXVALUE
|
||||
CACHE 1;
|
||||
|
||||
|
||||
--
|
||||
-- Name: blocks_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
ALTER SEQUENCE blocks_id_seq OWNED BY blocks.id;
|
||||
|
||||
|
||||
--
|
||||
-- Name: logs; Type: TABLE; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
CREATE TABLE logs (
|
||||
id integer NOT NULL,
|
||||
block_number bigint,
|
||||
address character varying(66),
|
||||
tx_hash character varying(66),
|
||||
index bigint,
|
||||
topic0 character varying(66),
|
||||
topic1 character varying(66),
|
||||
topic2 character varying(66),
|
||||
topic3 character varying(66),
|
||||
data text
|
||||
);
|
||||
|
||||
|
||||
--
|
||||
-- Name: logs_id_seq; Type: SEQUENCE; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
CREATE SEQUENCE logs_id_seq
|
||||
START WITH 1
|
||||
INCREMENT BY 1
|
||||
NO MINVALUE
|
||||
NO MAXVALUE
|
||||
CACHE 1;
|
||||
|
||||
|
||||
--
|
||||
-- Name: logs_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
ALTER SEQUENCE logs_id_seq OWNED BY logs.id;
|
||||
|
||||
|
||||
--
|
||||
-- Name: nodes; Type: TABLE; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
CREATE TABLE nodes (
|
||||
id integer NOT NULL,
|
||||
genesis_block character varying(66),
|
||||
network_id numeric
|
||||
);
|
||||
|
||||
|
||||
--
|
||||
-- Name: nodes_id_seq; Type: SEQUENCE; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
CREATE SEQUENCE nodes_id_seq
|
||||
START WITH 1
|
||||
INCREMENT BY 1
|
||||
NO MINVALUE
|
||||
NO MAXVALUE
|
||||
CACHE 1;
|
||||
|
||||
|
||||
--
|
||||
-- Name: nodes_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
ALTER SEQUENCE nodes_id_seq OWNED BY nodes.id;
|
||||
|
||||
|
||||
--
|
||||
-- Name: receipts; Type: TABLE; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
CREATE TABLE receipts (
|
||||
id integer NOT NULL,
|
||||
transaction_id integer NOT NULL,
|
||||
contract_address character varying(42),
|
||||
cumulative_gas_used numeric,
|
||||
gas_used numeric,
|
||||
state_root character varying(66),
|
||||
status integer,
|
||||
tx_hash character varying(66)
|
||||
);
|
||||
|
||||
|
||||
--
|
||||
-- Name: receipts_id_seq; Type: SEQUENCE; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
CREATE SEQUENCE receipts_id_seq
|
||||
START WITH 1
|
||||
INCREMENT BY 1
|
||||
NO MINVALUE
|
||||
NO MAXVALUE
|
||||
CACHE 1;
|
||||
|
||||
|
||||
--
|
||||
-- Name: receipts_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
ALTER SEQUENCE receipts_id_seq OWNED BY receipts.id;
|
||||
|
||||
|
||||
--
|
||||
-- Name: schema_migrations; Type: TABLE; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
CREATE TABLE schema_migrations (
|
||||
version bigint NOT NULL,
|
||||
dirty boolean NOT NULL
|
||||
);
|
||||
|
||||
|
||||
--
|
||||
-- Name: transactions; Type: TABLE; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
CREATE TABLE transactions (
|
||||
id integer NOT NULL,
|
||||
tx_hash character varying(66),
|
||||
tx_nonce numeric,
|
||||
tx_to character varying(66),
|
||||
tx_gaslimit numeric,
|
||||
tx_gasprice numeric,
|
||||
tx_value numeric,
|
||||
block_id integer NOT NULL,
|
||||
tx_from character varying(66),
|
||||
tx_input_data character varying
|
||||
);
|
||||
|
||||
|
||||
--
|
||||
-- Name: transactions_id_seq; Type: SEQUENCE; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
CREATE SEQUENCE transactions_id_seq
|
||||
START WITH 1
|
||||
INCREMENT BY 1
|
||||
NO MINVALUE
|
||||
NO MAXVALUE
|
||||
CACHE 1;
|
||||
|
||||
|
||||
--
|
||||
-- Name: transactions_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
ALTER SEQUENCE transactions_id_seq OWNED BY transactions.id;
|
||||
|
||||
|
||||
--
|
||||
-- Name: watched_contracts; Type: TABLE; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
CREATE TABLE watched_contracts (
|
||||
contract_id integer NOT NULL,
|
||||
contract_hash character varying(66),
|
||||
contract_abi json
|
||||
);
|
||||
|
||||
|
||||
--
|
||||
-- Name: watched_contracts_contract_id_seq; Type: SEQUENCE; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
CREATE SEQUENCE watched_contracts_contract_id_seq
|
||||
START WITH 1
|
||||
INCREMENT BY 1
|
||||
NO MINVALUE
|
||||
NO MAXVALUE
|
||||
CACHE 1;
|
||||
|
||||
|
||||
--
|
||||
-- Name: watched_contracts_contract_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
ALTER SEQUENCE watched_contracts_contract_id_seq OWNED BY watched_contracts.contract_id;
|
||||
|
||||
|
||||
--
|
||||
-- Name: blocks id; Type: DEFAULT; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
ALTER TABLE ONLY blocks ALTER COLUMN id SET DEFAULT nextval('blocks_id_seq'::regclass);
|
||||
|
||||
|
||||
--
|
||||
-- Name: logs id; Type: DEFAULT; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
ALTER TABLE ONLY logs ALTER COLUMN id SET DEFAULT nextval('logs_id_seq'::regclass);
|
||||
|
||||
|
||||
--
|
||||
-- Name: nodes id; Type: DEFAULT; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
ALTER TABLE ONLY nodes ALTER COLUMN id SET DEFAULT nextval('nodes_id_seq'::regclass);
|
||||
|
||||
|
||||
--
|
||||
-- Name: receipts id; Type: DEFAULT; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
ALTER TABLE ONLY receipts ALTER COLUMN id SET DEFAULT nextval('receipts_id_seq'::regclass);
|
||||
|
||||
|
||||
--
|
||||
-- Name: transactions id; Type: DEFAULT; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
ALTER TABLE ONLY transactions ALTER COLUMN id SET DEFAULT nextval('transactions_id_seq'::regclass);
|
||||
|
||||
|
||||
--
|
||||
-- Name: watched_contracts contract_id; Type: DEFAULT; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
ALTER TABLE ONLY watched_contracts ALTER COLUMN contract_id SET DEFAULT nextval('watched_contracts_contract_id_seq'::regclass);
|
||||
|
||||
|
||||
--
|
||||
-- Name: blocks blocks_pkey; Type: CONSTRAINT; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
ALTER TABLE ONLY blocks
|
||||
ADD CONSTRAINT blocks_pkey PRIMARY KEY (id);
|
||||
|
||||
|
||||
--
|
||||
-- Name: watched_contracts contract_hash_uc; Type: CONSTRAINT; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
ALTER TABLE ONLY watched_contracts
|
||||
ADD CONSTRAINT contract_hash_uc UNIQUE (contract_hash);
|
||||
|
||||
|
||||
--
|
||||
-- Name: logs log_uc; Type: CONSTRAINT; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
ALTER TABLE ONLY logs
|
||||
ADD CONSTRAINT log_uc UNIQUE (block_number, index);
|
||||
|
||||
|
||||
--
|
||||
-- Name: logs logs_pkey; Type: CONSTRAINT; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
ALTER TABLE ONLY logs
|
||||
ADD CONSTRAINT logs_pkey PRIMARY KEY (id);
|
||||
|
||||
|
||||
--
|
||||
-- Name: blocks node_id_block_number_uc; Type: CONSTRAINT; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
ALTER TABLE ONLY blocks
|
||||
ADD CONSTRAINT node_id_block_number_uc UNIQUE (block_number, node_id);
|
||||
|
||||
|
||||
--
|
||||
-- Name: nodes node_uc; Type: CONSTRAINT; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
ALTER TABLE ONLY nodes
|
||||
ADD CONSTRAINT node_uc UNIQUE (genesis_block, network_id);
|
||||
|
||||
|
||||
--
|
||||
-- Name: nodes nodes_pkey; Type: CONSTRAINT; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
ALTER TABLE ONLY nodes
|
||||
ADD CONSTRAINT nodes_pkey PRIMARY KEY (id);
|
||||
|
||||
|
||||
--
|
||||
-- Name: receipts receipts_pkey; Type: CONSTRAINT; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
ALTER TABLE ONLY receipts
|
||||
ADD CONSTRAINT receipts_pkey PRIMARY KEY (id);
|
||||
|
||||
|
||||
--
|
||||
-- Name: schema_migrations schema_migrations_pkey; Type: CONSTRAINT; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
ALTER TABLE ONLY schema_migrations
|
||||
ADD CONSTRAINT schema_migrations_pkey PRIMARY KEY (version);
|
||||
|
||||
|
||||
--
|
||||
-- Name: transactions transactions_pkey; Type: CONSTRAINT; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
ALTER TABLE ONLY transactions
|
||||
ADD CONSTRAINT transactions_pkey PRIMARY KEY (id);
|
||||
|
||||
|
||||
--
|
||||
-- Name: watched_contracts watched_contracts_pkey; Type: CONSTRAINT; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
ALTER TABLE ONLY watched_contracts
|
||||
ADD CONSTRAINT watched_contracts_pkey PRIMARY KEY (contract_id);
|
||||
|
||||
|
||||
--
|
||||
-- Name: block_id_index; Type: INDEX; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
CREATE INDEX block_id_index ON transactions USING btree (block_id);
|
||||
|
||||
|
||||
--
|
||||
-- Name: block_number_index; Type: INDEX; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
CREATE INDEX block_number_index ON blocks USING btree (block_number);
|
||||
|
||||
|
||||
--
|
||||
-- Name: node_id_index; Type: INDEX; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
CREATE INDEX node_id_index ON blocks USING btree (node_id);
|
||||
|
||||
|
||||
--
|
||||
-- Name: transaction_id_index; Type: INDEX; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
CREATE INDEX transaction_id_index ON receipts USING btree (transaction_id);
|
||||
|
||||
|
||||
--
|
||||
-- Name: tx_from_index; Type: INDEX; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
CREATE INDEX tx_from_index ON transactions USING btree (tx_from);
|
||||
|
||||
|
||||
--
|
||||
-- Name: tx_to_index; Type: INDEX; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
CREATE INDEX tx_to_index ON transactions USING btree (tx_to);
|
||||
|
||||
|
||||
--
|
||||
-- Name: transactions blocks_fk; Type: FK CONSTRAINT; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
ALTER TABLE ONLY transactions
|
||||
ADD CONSTRAINT blocks_fk FOREIGN KEY (block_id) REFERENCES blocks(id) ON DELETE CASCADE;
|
||||
|
||||
|
||||
--
|
||||
-- Name: blocks node_fk; Type: FK CONSTRAINT; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
ALTER TABLE ONLY blocks
|
||||
ADD CONSTRAINT node_fk FOREIGN KEY (node_id) REFERENCES nodes(id) ON DELETE CASCADE;
|
||||
|
||||
|
||||
--
|
||||
-- Name: receipts transaction_fk; Type: FK CONSTRAINT; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
ALTER TABLE ONLY receipts
|
||||
ADD CONSTRAINT transaction_fk FOREIGN KEY (transaction_id) REFERENCES transactions(id) ON DELETE CASCADE;
|
||||
|
||||
|
||||
--
|
||||
-- PostgreSQL database dump complete
|
||||
--
|
||||
|
@ -16,7 +16,7 @@ var _ = Describe("Rewards calculations", func() {
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
|
||||
blockchain := geth.NewBlockchain(config.Client.IPCPath)
|
||||
block := blockchain.GetBlockByNumber(1071819)
|
||||
Expect(block.Reward).To(Equal(5.31355))
|
||||
})
|
||||
@ -26,7 +26,7 @@ var _ = Describe("Rewards calculations", func() {
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
|
||||
blockchain := geth.NewBlockchain(config.Client.IPCPath)
|
||||
block := blockchain.GetBlockByNumber(1071819)
|
||||
Expect(block.UnclesReward).To(Equal(6.875))
|
||||
})
|
||||
|
@ -15,13 +15,14 @@ import (
|
||||
|
||||
var _ = Describe("Reading contracts", func() {
|
||||
|
||||
Describe("Reading the list of attributes", func() {
|
||||
//TODO was experiencing Infura issue (I suspect) on 1/5. Unignore these and revisit if persists on next commit
|
||||
XDescribe("Reading the list of attributes", func() {
|
||||
It("returns a string attribute for a real contract", func() {
|
||||
config, err := cfg.NewConfig("infura")
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
|
||||
blockchain := geth.NewBlockchain(config.Client.IPCPath)
|
||||
contract := testing.SampleContract()
|
||||
|
||||
contractAttributes, err := blockchain.GetAttributes(contract)
|
||||
@ -35,7 +36,7 @@ var _ = Describe("Reading contracts", func() {
|
||||
|
||||
It("does not return an attribute that takes an input", func() {
|
||||
config, err := cfg.NewConfig("infura")
|
||||
blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
|
||||
blockchain := geth.NewBlockchain(config.Client.IPCPath)
|
||||
contract := testing.SampleContract()
|
||||
|
||||
contractAttributes, err := blockchain.GetAttributes(contract)
|
||||
@ -47,7 +48,7 @@ var _ = Describe("Reading contracts", func() {
|
||||
|
||||
It("does not return an attribute that is not constant", func() {
|
||||
config, _ := cfg.NewConfig("infura")
|
||||
blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
|
||||
blockchain := geth.NewBlockchain(config.Client.IPCPath)
|
||||
contract := testing.SampleContract()
|
||||
|
||||
contractAttributes, err := blockchain.GetAttributes(contract)
|
||||
@ -58,10 +59,11 @@ var _ = Describe("Reading contracts", func() {
|
||||
})
|
||||
})
|
||||
|
||||
Describe("Getting a contract attribute", func() {
|
||||
//TODO was experiencing Infura issue (I suspect) on 1/5. Unignore these and revisit if persists on next commit
|
||||
XDescribe("Getting a contract attribute", func() {
|
||||
It("returns the correct attribute for a real contract", func() {
|
||||
config, _ := cfg.NewConfig("infura")
|
||||
blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
|
||||
blockchain := geth.NewBlockchain(config.Client.IPCPath)
|
||||
|
||||
contract := testing.SampleContract()
|
||||
name, err := blockchain.GetAttribute(contract, "name", nil)
|
||||
@ -72,7 +74,7 @@ var _ = Describe("Reading contracts", func() {
|
||||
|
||||
It("returns the correct attribute for a real contract", func() {
|
||||
config, _ := cfg.NewConfig("infura")
|
||||
blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
|
||||
blockchain := geth.NewBlockchain(config.Client.IPCPath)
|
||||
contract := testing.SampleContract()
|
||||
|
||||
name, err := blockchain.GetAttribute(contract, "name", nil)
|
||||
@ -83,7 +85,7 @@ var _ = Describe("Reading contracts", func() {
|
||||
|
||||
It("returns the correct attribute for a real contract at a specific block height", func() {
|
||||
config, _ := cfg.NewConfig("infura")
|
||||
blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
|
||||
blockchain := geth.NewBlockchain(config.Client.IPCPath)
|
||||
contract := testing.SampleContract()
|
||||
|
||||
name, err := blockchain.GetAttribute(contract, "name", big.NewInt(4701536))
|
||||
@ -94,7 +96,7 @@ var _ = Describe("Reading contracts", func() {
|
||||
|
||||
It("returns an error when asking for an attribute that does not exist", func() {
|
||||
config, _ := cfg.NewConfig("infura")
|
||||
blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
|
||||
blockchain := geth.NewBlockchain(config.Client.IPCPath)
|
||||
contract := testing.SampleContract()
|
||||
|
||||
name, err := blockchain.GetAttribute(contract, "missing_attribute", nil)
|
||||
@ -116,7 +118,7 @@ var _ = Describe("Reading contracts", func() {
|
||||
Index: 19,
|
||||
Data: "0x0000000000000000000000000000000000000000000000000c7d713b49da0000"}
|
||||
config, _ := cfg.NewConfig("infura")
|
||||
blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
|
||||
blockchain := geth.NewBlockchain(config.Client.IPCPath)
|
||||
contract := testing.SampleContract()
|
||||
|
||||
logs, err := blockchain.GetLogs(contract, big.NewInt(4703824), nil)
|
||||
@ -129,7 +131,7 @@ var _ = Describe("Reading contracts", func() {
|
||||
|
||||
It("returns and empty log array when no events for a given block / contract combo", func() {
|
||||
config, _ := cfg.NewConfig("infura")
|
||||
blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
|
||||
blockchain := geth.NewBlockchain(config.Client.IPCPath)
|
||||
|
||||
logs, err := blockchain.GetLogs(core.Contract{Hash: "x123"}, big.NewInt(4703824), nil)
|
||||
|
||||
|
@ -4,11 +4,10 @@ import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
|
||||
"github.com/8thlight/vulcanizedb/pkg/blockchain_listener"
|
||||
"github.com/8thlight/vulcanizedb/pkg/config"
|
||||
"github.com/8thlight/vulcanizedb/pkg/core"
|
||||
"github.com/8thlight/vulcanizedb/pkg/fakes"
|
||||
"github.com/8thlight/vulcanizedb/pkg/geth"
|
||||
"github.com/8thlight/vulcanizedb/pkg/history"
|
||||
"github.com/8thlight/vulcanizedb/pkg/repositories"
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
@ -19,35 +18,19 @@ func init() {
|
||||
|
||||
var _ = Describe("Reading from the Geth blockchain", func() {
|
||||
|
||||
var listener blockchain_listener.BlockchainListener
|
||||
var observer *fakes.BlockchainObserver
|
||||
var blockchain *geth.GethBlockchain
|
||||
var blockchain *geth.Blockchain
|
||||
var repository *repositories.InMemory
|
||||
|
||||
BeforeEach(func() {
|
||||
observer = fakes.NewFakeBlockchainObserver()
|
||||
cfg, _ := config.NewConfig("private")
|
||||
blockchain = geth.NewGethBlockchain(cfg.Client.IPCPath)
|
||||
observers := []core.BlockchainObserver{observer}
|
||||
listener = blockchain_listener.NewBlockchainListener(blockchain, observers)
|
||||
})
|
||||
|
||||
AfterEach(func() {
|
||||
listener.Stop()
|
||||
blockchain = geth.NewBlockchain(cfg.Client.IPCPath)
|
||||
repository = repositories.NewInMemory()
|
||||
})
|
||||
|
||||
It("reads two blocks", func(done Done) {
|
||||
go listener.Start()
|
||||
|
||||
<-observer.WasNotified
|
||||
firstBlock := observer.LastBlock()
|
||||
Expect(firstBlock).NotTo(BeNil())
|
||||
|
||||
<-observer.WasNotified
|
||||
secondBlock := observer.LastBlock()
|
||||
Expect(secondBlock).NotTo(BeNil())
|
||||
|
||||
Expect(firstBlock.Number + 1).Should(Equal(secondBlock.Number))
|
||||
|
||||
validator := history.NewBlockValidator(blockchain, repository, 2)
|
||||
validator.ValidateBlocks()
|
||||
Expect(repository.BlockCount()).To(Equal(2))
|
||||
close(done)
|
||||
}, 15)
|
||||
|
||||
|
@ -1,37 +0,0 @@
|
||||
package blockchain_listener
|
||||
|
||||
import "github.com/8thlight/vulcanizedb/pkg/core"
|
||||
|
||||
type BlockchainListener struct {
|
||||
inputBlocks chan core.Block
|
||||
blockchain core.Blockchain
|
||||
observers []core.BlockchainObserver
|
||||
}
|
||||
|
||||
func NewBlockchainListener(blockchain core.Blockchain, observers []core.BlockchainObserver) BlockchainListener {
|
||||
inputBlocks := make(chan core.Block, 10)
|
||||
blockchain.SubscribeToBlocks(inputBlocks)
|
||||
listener := BlockchainListener{
|
||||
inputBlocks: inputBlocks,
|
||||
blockchain: blockchain,
|
||||
observers: observers,
|
||||
}
|
||||
return listener
|
||||
}
|
||||
|
||||
func (listener BlockchainListener) Start() {
|
||||
go listener.blockchain.StartListening()
|
||||
for block := range listener.inputBlocks {
|
||||
listener.notifyObservers(block)
|
||||
}
|
||||
}
|
||||
|
||||
func (listener BlockchainListener) notifyObservers(block core.Block) {
|
||||
for _, observer := range listener.observers {
|
||||
observer.NotifyBlockAdded(block)
|
||||
}
|
||||
}
|
||||
|
||||
func (listener BlockchainListener) Stop() {
|
||||
listener.blockchain.StopListening()
|
||||
}
|
@ -1,13 +0,0 @@
|
||||
package blockchain_listener_test
|
||||
|
||||
import (
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestListener(t *testing.T) {
|
||||
RegisterFailHandler(Fail)
|
||||
RunSpecs(t, "Listener Suite")
|
||||
}
|
@ -1,69 +0,0 @@
|
||||
package blockchain_listener_test
|
||||
|
||||
import (
|
||||
"github.com/8thlight/vulcanizedb/pkg/blockchain_listener"
|
||||
"github.com/8thlight/vulcanizedb/pkg/core"
|
||||
"github.com/8thlight/vulcanizedb/pkg/fakes"
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
var _ = Describe("Blockchain listeners", func() {
|
||||
|
||||
It("starts with no blocks", func(done Done) {
|
||||
observer := fakes.NewFakeBlockchainObserver()
|
||||
blockchain := fakes.NewBlockchain()
|
||||
|
||||
blockchain_listener.NewBlockchainListener(blockchain, []core.BlockchainObserver{observer})
|
||||
|
||||
Expect(len(observer.CurrentBlocks)).To(Equal(0))
|
||||
close(done)
|
||||
}, 1)
|
||||
|
||||
It("sees when one block was added", func(done Done) {
|
||||
observer := fakes.NewFakeBlockchainObserver()
|
||||
blockchain := fakes.NewBlockchain()
|
||||
listener := blockchain_listener.NewBlockchainListener(blockchain, []core.BlockchainObserver{observer})
|
||||
go listener.Start()
|
||||
|
||||
go blockchain.AddBlock(core.Block{Number: 123})
|
||||
|
||||
wasObserverNotified := <-observer.WasNotified
|
||||
Expect(wasObserverNotified).To(BeTrue())
|
||||
Expect(len(observer.CurrentBlocks)).To(Equal(1))
|
||||
addedBlock := observer.CurrentBlocks[0]
|
||||
Expect(addedBlock.Number).To(Equal(int64(123)))
|
||||
close(done)
|
||||
}, 1)
|
||||
|
||||
It("sees a second block", func(done Done) {
|
||||
observer := fakes.NewFakeBlockchainObserver()
|
||||
blockchain := fakes.NewBlockchain()
|
||||
listener := blockchain_listener.NewBlockchainListener(blockchain, []core.BlockchainObserver{observer})
|
||||
go listener.Start()
|
||||
|
||||
go blockchain.AddBlock(core.Block{Number: 123})
|
||||
<-observer.WasNotified
|
||||
go blockchain.AddBlock(core.Block{Number: 456})
|
||||
wasObserverNotified := <-observer.WasNotified
|
||||
|
||||
Expect(wasObserverNotified).To(BeTrue())
|
||||
Expect(len(observer.CurrentBlocks)).To(Equal(2))
|
||||
addedBlock := observer.CurrentBlocks[1]
|
||||
Expect(addedBlock.Number).To(Equal(int64(456)))
|
||||
close(done)
|
||||
}, 1)
|
||||
|
||||
It("stops listening", func(done Done) {
|
||||
observer := fakes.NewFakeBlockchainObserver()
|
||||
blockchain := fakes.NewBlockchain()
|
||||
listener := blockchain_listener.NewBlockchainListener(blockchain, []core.BlockchainObserver{observer})
|
||||
go listener.Start()
|
||||
|
||||
listener.Stop()
|
||||
|
||||
Expect(blockchain.WasToldToStop).To(BeTrue())
|
||||
close(done)
|
||||
}, 1)
|
||||
|
||||
})
|
@ -6,9 +6,6 @@ type Blockchain interface {
|
||||
GetBlockByNumber(blockNumber int64) Block
|
||||
LastBlock() *big.Int
|
||||
Node() Node
|
||||
SubscribeToBlocks(blocks chan Block)
|
||||
StartListening()
|
||||
StopListening()
|
||||
GetAttributes(contract Contract) (ContractAttributes, error)
|
||||
GetAttribute(contract Contract, attributeName string, blockNumber *big.Int) (interface{}, error)
|
||||
GetLogs(contract Contract, startingBlockNumber *big.Int, endingBlockNumber *big.Int) ([]Log, error)
|
||||
|
@ -1,5 +0,0 @@
|
||||
package core
|
||||
|
||||
type BlockchainObserver interface {
|
||||
NotifyBlockAdded(Block)
|
||||
}
|
@ -68,21 +68,11 @@ func (blockchain *Blockchain) GetBlockByNumber(blockNumber int64) core.Block {
|
||||
return blockchain.blocks[blockNumber]
|
||||
}
|
||||
|
||||
func (blockchain *Blockchain) SubscribeToBlocks(outputBlocks chan core.Block) {
|
||||
blockchain.blocksChannel = outputBlocks
|
||||
}
|
||||
|
||||
func (blockchain *Blockchain) AddBlock(block core.Block) {
|
||||
blockchain.blocks[block.Number] = block
|
||||
blockchain.blocksChannel <- block
|
||||
}
|
||||
|
||||
func (*Blockchain) StartListening() {}
|
||||
|
||||
func (blockchain *Blockchain) StopListening() {
|
||||
blockchain.WasToldToStop = true
|
||||
}
|
||||
|
||||
func (blockchain *Blockchain) SetContractStateAttribute(contractHash string, blockNumber *big.Int, attributeName string, attributeValue string) {
|
||||
var key string
|
||||
if blockNumber == nil {
|
||||
|
@ -1,23 +0,0 @@
|
||||
package fakes
|
||||
|
||||
import "github.com/8thlight/vulcanizedb/pkg/core"
|
||||
|
||||
type BlockchainObserver struct {
|
||||
CurrentBlocks []core.Block
|
||||
WasNotified chan bool
|
||||
}
|
||||
|
||||
func (observer *BlockchainObserver) LastBlock() core.Block {
|
||||
return observer.CurrentBlocks[len(observer.CurrentBlocks)-1]
|
||||
}
|
||||
|
||||
func NewFakeBlockchainObserver() *BlockchainObserver {
|
||||
return &BlockchainObserver{
|
||||
WasNotified: make(chan bool),
|
||||
}
|
||||
}
|
||||
|
||||
func (observer *BlockchainObserver) NotifyBlockAdded(block core.Block) {
|
||||
observer.CurrentBlocks = append(observer.CurrentBlocks, block)
|
||||
observer.WasNotified <- true
|
||||
}
|
54
pkg/geth/block_rewards.go
Normal file
54
pkg/geth/block_rewards.go
Normal file
@ -0,0 +1,54 @@
|
||||
package geth
|
||||
|
||||
import (
|
||||
"github.com/8thlight/vulcanizedb/pkg/core"
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
"github.com/ethereum/go-ethereum/params"
|
||||
)
|
||||
|
||||
func CalcUnclesReward(block core.Block, uncles []*types.Header) float64 {
|
||||
var unclesReward float64
|
||||
for _, uncle := range uncles {
|
||||
blockNumber := block.Number
|
||||
staticBlockReward := float64(staticRewardByBlockNumber(blockNumber))
|
||||
unclesReward += (1.0 + float64(uncle.Number.Int64()-block.Number)/8.0) * staticBlockReward
|
||||
}
|
||||
return unclesReward
|
||||
}
|
||||
|
||||
func CalcBlockReward(block core.Block, uncles []*types.Header) float64 {
|
||||
blockNumber := block.Number
|
||||
staticBlockReward := staticRewardByBlockNumber(blockNumber)
|
||||
transactionFees := calcTransactionFees(block)
|
||||
uncleInclusionRewards := calcUncleInclusionRewards(block, uncles)
|
||||
return transactionFees + uncleInclusionRewards + staticBlockReward
|
||||
}
|
||||
|
||||
func calcTransactionFees(block core.Block) float64 {
|
||||
var transactionFees float64
|
||||
for _, transaction := range block.Transactions {
|
||||
receipt := transaction.Receipt
|
||||
transactionFees += float64(transaction.GasPrice * receipt.GasUsed)
|
||||
}
|
||||
return transactionFees / params.Ether
|
||||
}
|
||||
|
||||
func calcUncleInclusionRewards(block core.Block, uncles []*types.Header) float64 {
|
||||
var uncleInclusionRewards float64
|
||||
staticBlockReward := staticRewardByBlockNumber(block.Number)
|
||||
for range uncles {
|
||||
uncleInclusionRewards += staticBlockReward * 1 / 32
|
||||
}
|
||||
return uncleInclusionRewards
|
||||
}
|
||||
|
||||
func staticRewardByBlockNumber(blockNumber int64) float64 {
|
||||
var staticBlockReward float64
|
||||
//https://blog.ethereum.org/2017/10/12/byzantium-hf-announcement/
|
||||
if blockNumber >= 4370000 {
|
||||
staticBlockReward = 3
|
||||
} else {
|
||||
staticBlockReward = 5
|
||||
}
|
||||
return staticBlockReward
|
||||
}
|
@ -17,11 +17,9 @@ type GethClient interface {
|
||||
TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error)
|
||||
}
|
||||
|
||||
func GethBlockToCoreBlock(gethBlock *types.Block, client GethClient) core.Block {
|
||||
transactions := convertGethTransactionsToCore(gethBlock, client)
|
||||
blockReward := CalcBlockReward(gethBlock, client)
|
||||
uncleReward := CalcUnclesReward(gethBlock)
|
||||
return core.Block{
|
||||
func ToCoreBlock(gethBlock *types.Block, client GethClient) core.Block {
|
||||
transactions := convertTransactionsToCore(gethBlock, client)
|
||||
coreBlock := core.Block{
|
||||
Difficulty: gethBlock.Difficulty().Int64(),
|
||||
ExtraData: hexutil.Encode(gethBlock.Extra()),
|
||||
GasLimit: gethBlock.GasLimit().Int64(),
|
||||
@ -31,23 +29,24 @@ func GethBlockToCoreBlock(gethBlock *types.Block, client GethClient) core.Block
|
||||
Nonce: hexutil.Encode(gethBlock.Header().Nonce[:]),
|
||||
Number: gethBlock.Number().Int64(),
|
||||
ParentHash: gethBlock.ParentHash().Hex(),
|
||||
Reward: blockReward,
|
||||
Size: gethBlock.Size().Int64(),
|
||||
Time: gethBlock.Time().Int64(),
|
||||
Transactions: transactions,
|
||||
UncleHash: gethBlock.UncleHash().Hex(),
|
||||
UnclesReward: uncleReward,
|
||||
}
|
||||
coreBlock.Reward = CalcBlockReward(coreBlock, gethBlock.Uncles())
|
||||
coreBlock.UnclesReward = CalcUnclesReward(coreBlock, gethBlock.Uncles())
|
||||
return coreBlock
|
||||
}
|
||||
|
||||
func convertGethTransactionsToCore(gethBlock *types.Block, client GethClient) []core.Transaction {
|
||||
func convertTransactionsToCore(gethBlock *types.Block, client GethClient) []core.Transaction {
|
||||
transactions := make([]core.Transaction, 0)
|
||||
for i, gethTransaction := range gethBlock.Transactions() {
|
||||
from, err := client.TransactionSender(context.Background(), gethTransaction, gethBlock.Hash(), uint(i))
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
transaction := gethTransToCoreTrans(gethTransaction, &from)
|
||||
transaction := transToCoreTrans(gethTransaction, &from)
|
||||
transaction, err = appendReceiptToTransaction(client, transaction)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
@ -59,12 +58,12 @@ func convertGethTransactionsToCore(gethBlock *types.Block, client GethClient) []
|
||||
|
||||
func appendReceiptToTransaction(client GethClient, transaction core.Transaction) (core.Transaction, error) {
|
||||
gethReceipt, err := client.TransactionReceipt(context.Background(), common.HexToHash(transaction.Hash))
|
||||
receipt := GethReceiptToCoreReceipt(gethReceipt)
|
||||
receipt := ReceiptToCoreReceipt(gethReceipt)
|
||||
transaction.Receipt = receipt
|
||||
return transaction, err
|
||||
}
|
||||
|
||||
func gethTransToCoreTrans(transaction *types.Transaction, from *common.Address) core.Transaction {
|
||||
func transToCoreTrans(transaction *types.Transaction, from *common.Address) core.Transaction {
|
||||
data := hexutil.Encode(transaction.Data())
|
||||
return core.Transaction{
|
||||
Hash: transaction.Hash().Hex(),
|
@ -66,7 +66,7 @@ var _ = Describe("Conversion of GethBlock to core.Block", func() {
|
||||
}
|
||||
block := types.NewBlock(&header, []*types.Transaction{}, []*types.Header{}, []*types.Receipt{})
|
||||
client := &FakeGethClient{}
|
||||
gethBlock := geth.GethBlockToCoreBlock(block, client)
|
||||
gethBlock := geth.ToCoreBlock(block, client)
|
||||
|
||||
Expect(gethBlock.Difficulty).To(Equal(difficulty.Int64()))
|
||||
Expect(gethBlock.GasLimit).To(Equal(gasLimit))
|
||||
@ -85,6 +85,7 @@ var _ = Describe("Conversion of GethBlock to core.Block", func() {
|
||||
|
||||
Describe("The block and uncle rewards calculations", func() {
|
||||
It("calculates block rewards for a block", func() {
|
||||
|
||||
transaction := types.NewTransaction(
|
||||
uint64(226823),
|
||||
common.HexToAddress("0x108fedb097c1dcfed441480170144d8e19bb217f"),
|
||||
@ -96,20 +97,25 @@ var _ = Describe("Conversion of GethBlock to core.Block", func() {
|
||||
transactions := []*types.Transaction{transaction}
|
||||
|
||||
txHash := transaction.Hash()
|
||||
receipt := types.Receipt{TxHash: txHash, GasUsed: big.NewInt(21000)}
|
||||
receipt := types.Receipt{
|
||||
TxHash: txHash,
|
||||
GasUsed: big.NewInt(21000),
|
||||
CumulativeGasUsed: big.NewInt(21000),
|
||||
}
|
||||
receipts := []*types.Receipt{&receipt}
|
||||
|
||||
client := NewFakeClient()
|
||||
client.AddReceipts(receipts)
|
||||
|
||||
number := int64(1071819)
|
||||
header := types.Header{
|
||||
Number: big.NewInt(number),
|
||||
}
|
||||
uncles := []*types.Header{{Number: big.NewInt(1071817)}, {Number: big.NewInt(1071818)}}
|
||||
block := types.NewBlock(&header, transactions, uncles, []*types.Receipt{})
|
||||
block := types.NewBlock(&header, transactions, uncles, []*types.Receipt{&receipt})
|
||||
coreBlock := geth.ToCoreBlock(block, client)
|
||||
|
||||
client := NewFakeClient()
|
||||
client.AddReceipts(receipts)
|
||||
|
||||
Expect(geth.CalcBlockReward(block, client)).To(Equal(5.31355))
|
||||
Expect(geth.CalcBlockReward(coreBlock, block.Uncles())).To(Equal(5.31355))
|
||||
})
|
||||
|
||||
It("calculates the uncles reward for a block", func() {
|
||||
@ -123,8 +129,9 @@ var _ = Describe("Conversion of GethBlock to core.Block", func() {
|
||||
transactions := []*types.Transaction{transaction}
|
||||
|
||||
receipt := types.Receipt{
|
||||
TxHash: transaction.Hash(),
|
||||
GasUsed: big.NewInt(21000),
|
||||
TxHash: transaction.Hash(),
|
||||
GasUsed: big.NewInt(21000),
|
||||
CumulativeGasUsed: big.NewInt(21000),
|
||||
}
|
||||
receipts := []*types.Receipt{&receipt}
|
||||
|
||||
@ -135,12 +142,14 @@ var _ = Describe("Conversion of GethBlock to core.Block", func() {
|
||||
{Number: big.NewInt(1071816)},
|
||||
{Number: big.NewInt(1071817)},
|
||||
}
|
||||
block := types.NewBlock(&header, transactions, uncles, []*types.Receipt{})
|
||||
block := types.NewBlock(&header, transactions, uncles, receipts)
|
||||
|
||||
client := NewFakeClient()
|
||||
client.AddReceipts(receipts)
|
||||
|
||||
Expect(geth.CalcUnclesReward(block)).To(Equal(6.875))
|
||||
coreBlock := geth.ToCoreBlock(block, client)
|
||||
|
||||
Expect(geth.CalcUnclesReward(coreBlock, block.Uncles())).To(Equal(6.875))
|
||||
})
|
||||
|
||||
It("decreases the static block reward from 5 to 3 for blocks after block 4,269,999", func() {
|
||||
@ -163,12 +172,14 @@ var _ = Describe("Conversion of GethBlock to core.Block", func() {
|
||||
transactions := []*types.Transaction{transactionOne, transactionTwo}
|
||||
|
||||
receiptOne := types.Receipt{
|
||||
TxHash: transactionOne.Hash(),
|
||||
GasUsed: big.NewInt(297508),
|
||||
TxHash: transactionOne.Hash(),
|
||||
GasUsed: big.NewInt(297508),
|
||||
CumulativeGasUsed: big.NewInt(0),
|
||||
}
|
||||
receiptTwo := types.Receipt{
|
||||
TxHash: transactionTwo.Hash(),
|
||||
GasUsed: big.NewInt(297508),
|
||||
TxHash: transactionTwo.Hash(),
|
||||
GasUsed: big.NewInt(297508),
|
||||
CumulativeGasUsed: big.NewInt(0),
|
||||
}
|
||||
receipts := []*types.Receipt{&receiptOne, &receiptTwo}
|
||||
|
||||
@ -181,8 +192,9 @@ var _ = Describe("Conversion of GethBlock to core.Block", func() {
|
||||
|
||||
client := NewFakeClient()
|
||||
client.AddReceipts(receipts)
|
||||
coreBlock := geth.ToCoreBlock(block, client)
|
||||
|
||||
Expect(geth.CalcBlockReward(block, client)).To(Equal(3.024990672))
|
||||
Expect(geth.CalcBlockReward(coreBlock, block.Uncles())).To(Equal(3.024990672))
|
||||
})
|
||||
})
|
||||
|
||||
@ -191,7 +203,7 @@ var _ = Describe("Conversion of GethBlock to core.Block", func() {
|
||||
header := types.Header{}
|
||||
block := types.NewBlock(&header, []*types.Transaction{}, []*types.Header{}, []*types.Receipt{})
|
||||
client := &FakeGethClient{}
|
||||
coreBlock := geth.GethBlockToCoreBlock(block, client)
|
||||
coreBlock := geth.ToCoreBlock(block, client)
|
||||
|
||||
Expect(len(coreBlock.Transactions)).To(Equal(0))
|
||||
})
|
||||
@ -225,7 +237,7 @@ var _ = Describe("Conversion of GethBlock to core.Block", func() {
|
||||
[]*types.Header{},
|
||||
[]*types.Receipt{gethReceipt},
|
||||
)
|
||||
coreBlock := geth.GethBlockToCoreBlock(gethBlock, client)
|
||||
coreBlock := geth.ToCoreBlock(gethBlock, client)
|
||||
|
||||
Expect(len(coreBlock.Transactions)).To(Equal(1))
|
||||
coreTransaction := coreBlock.Transactions[0]
|
||||
@ -238,7 +250,7 @@ var _ = Describe("Conversion of GethBlock to core.Block", func() {
|
||||
Expect(coreTransaction.Nonce).To(Equal(gethTransaction.Nonce()))
|
||||
|
||||
coreReceipt := coreTransaction.Receipt
|
||||
expectedReceipt := geth.GethReceiptToCoreReceipt(gethReceipt)
|
||||
expectedReceipt := geth.ReceiptToCoreReceipt(gethReceipt)
|
||||
Expect(coreReceipt).To(Equal(expectedReceipt))
|
||||
|
||||
})
|
||||
@ -269,13 +281,13 @@ var _ = Describe("Conversion of GethBlock to core.Block", func() {
|
||||
[]*types.Receipt{gethReceipt},
|
||||
)
|
||||
|
||||
coreBlock := geth.GethBlockToCoreBlock(gethBlock, client)
|
||||
coreBlock := geth.ToCoreBlock(gethBlock, client)
|
||||
|
||||
coreTransaction := coreBlock.Transactions[0]
|
||||
Expect(coreTransaction.To).To(Equal(""))
|
||||
|
||||
coreReceipt := coreTransaction.Receipt
|
||||
expectedReceipt := geth.GethReceiptToCoreReceipt(gethReceipt)
|
||||
expectedReceipt := geth.ReceiptToCoreReceipt(gethReceipt)
|
||||
Expect(coreReceipt).To(Equal(expectedReceipt))
|
||||
})
|
||||
})
|
@ -3,8 +3,6 @@ package geth
|
||||
import (
|
||||
"math/big"
|
||||
|
||||
"log"
|
||||
|
||||
"github.com/8thlight/vulcanizedb/pkg/core"
|
||||
"github.com/8thlight/vulcanizedb/pkg/geth/node"
|
||||
"github.com/ethereum/go-ethereum"
|
||||
@ -15,7 +13,7 @@ import (
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
type GethBlockchain struct {
|
||||
type Blockchain struct {
|
||||
client *ethclient.Client
|
||||
readGethHeaders chan *types.Header
|
||||
outputBlocks chan core.Block
|
||||
@ -23,7 +21,16 @@ type GethBlockchain struct {
|
||||
node core.Node
|
||||
}
|
||||
|
||||
func (blockchain *GethBlockchain) GetLogs(contract core.Contract, startingBlockNumber *big.Int, endingBlockNumber *big.Int) ([]core.Log, error) {
|
||||
func NewBlockchain(ipcPath string) *Blockchain {
|
||||
blockchain := Blockchain{}
|
||||
rpcClient, _ := rpc.Dial(ipcPath)
|
||||
client := ethclient.NewClient(rpcClient)
|
||||
blockchain.node = node.Retrieve(rpcClient)
|
||||
blockchain.client = client
|
||||
return &blockchain
|
||||
}
|
||||
|
||||
func (blockchain *Blockchain) GetLogs(contract core.Contract, startingBlockNumber *big.Int, endingBlockNumber *big.Int) ([]core.Log, error) {
|
||||
if endingBlockNumber == nil {
|
||||
endingBlockNumber = startingBlockNumber
|
||||
}
|
||||
@ -41,46 +48,16 @@ func (blockchain *GethBlockchain) GetLogs(contract core.Contract, startingBlockN
|
||||
return logs, nil
|
||||
}
|
||||
|
||||
func (blockchain *GethBlockchain) Node() core.Node {
|
||||
func (blockchain *Blockchain) Node() core.Node {
|
||||
return blockchain.node
|
||||
}
|
||||
|
||||
func (blockchain *GethBlockchain) GetBlockByNumber(blockNumber int64) core.Block {
|
||||
func (blockchain *Blockchain) GetBlockByNumber(blockNumber int64) core.Block {
|
||||
gethBlock, _ := blockchain.client.BlockByNumber(context.Background(), big.NewInt(blockNumber))
|
||||
return GethBlockToCoreBlock(gethBlock, blockchain.client)
|
||||
return ToCoreBlock(gethBlock, blockchain.client)
|
||||
}
|
||||
|
||||
func NewGethBlockchain(ipcPath string) *GethBlockchain {
|
||||
blockchain := GethBlockchain{}
|
||||
rpcClient, _ := rpc.Dial(ipcPath)
|
||||
client := ethclient.NewClient(rpcClient)
|
||||
blockchain.node = node.Retrieve(rpcClient)
|
||||
blockchain.client = client
|
||||
return &blockchain
|
||||
}
|
||||
|
||||
func (blockchain *GethBlockchain) SubscribeToBlocks(blocks chan core.Block) {
|
||||
blockchain.outputBlocks = blocks
|
||||
log.Println("SubscribeToBlocks")
|
||||
inputHeaders := make(chan *types.Header, 10)
|
||||
myContext := context.Background()
|
||||
blockchain.readGethHeaders = inputHeaders
|
||||
subscription, _ := blockchain.client.SubscribeNewHead(myContext, inputHeaders)
|
||||
blockchain.newHeadSubscription = subscription
|
||||
}
|
||||
|
||||
func (blockchain *GethBlockchain) StartListening() {
|
||||
for header := range blockchain.readGethHeaders {
|
||||
block := blockchain.GetBlockByNumber(header.Number.Int64())
|
||||
blockchain.outputBlocks <- block
|
||||
}
|
||||
}
|
||||
|
||||
func (blockchain *GethBlockchain) StopListening() {
|
||||
blockchain.newHeadSubscription.Unsubscribe()
|
||||
}
|
||||
|
||||
func (blockchain *GethBlockchain) LastBlock() *big.Int {
|
||||
func (blockchain *Blockchain) LastBlock() *big.Int {
|
||||
block, _ := blockchain.client.HeaderByNumber(context.Background(), nil)
|
||||
return block.Number
|
||||
}
|
@ -17,7 +17,7 @@ var (
|
||||
ErrInvalidStateAttribute = errors.New("invalid state attribute")
|
||||
)
|
||||
|
||||
func (blockchain *GethBlockchain) GetAttribute(contract core.Contract, attributeName string, blockNumber *big.Int) (interface{}, error) {
|
||||
func (blockchain *Blockchain) GetAttribute(contract core.Contract, attributeName string, blockNumber *big.Int) (interface{}, error) {
|
||||
parsed, err := ParseAbi(contract.Abi)
|
||||
var result interface{}
|
||||
if err != nil {
|
||||
@ -38,13 +38,13 @@ func (blockchain *GethBlockchain) GetAttribute(contract core.Contract, attribute
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func callContract(contractHash string, input []byte, blockchain *GethBlockchain, blockNumber *big.Int) ([]byte, error) {
|
||||
func callContract(contractHash string, input []byte, blockchain *Blockchain, blockNumber *big.Int) ([]byte, error) {
|
||||
to := common.HexToAddress(contractHash)
|
||||
msg := ethereum.CallMsg{To: &to, Data: input}
|
||||
return blockchain.client.CallContract(context.Background(), msg, blockNumber)
|
||||
}
|
||||
|
||||
func (blockchain *GethBlockchain) GetAttributes(contract core.Contract) (core.ContractAttributes, error) {
|
||||
func (blockchain *Blockchain) GetAttributes(contract core.Contract) (core.ContractAttributes, error) {
|
||||
parsed, _ := ParseAbi(contract.Abi)
|
||||
var contractAttributes core.ContractAttributes
|
||||
for _, abiElement := range parsed.Methods {
|
||||
|
@ -1,58 +0,0 @@
|
||||
package geth
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
"github.com/ethereum/go-ethereum/params"
|
||||
)
|
||||
|
||||
func CalcUnclesReward(gethBlock *types.Block) float64 {
|
||||
var unclesReward float64
|
||||
for _, uncle := range gethBlock.Uncles() {
|
||||
blockNumber := gethBlock.Number().Int64()
|
||||
staticBlockReward := float64(staticRewardByBlockNumber(blockNumber))
|
||||
unclesReward += (1.0 + float64(uncle.Number.Int64()-gethBlock.Number().Int64())/8.0) * staticBlockReward
|
||||
}
|
||||
return unclesReward
|
||||
}
|
||||
|
||||
func CalcBlockReward(gethBlock *types.Block, client GethClient) float64 {
|
||||
blockNumber := gethBlock.Number().Int64()
|
||||
staticBlockReward := staticRewardByBlockNumber(blockNumber)
|
||||
transactionFees := calcTransactionFees(gethBlock, client)
|
||||
uncleInclusionRewards := calcUncleInclusionRewards(gethBlock)
|
||||
return transactionFees + uncleInclusionRewards + staticBlockReward
|
||||
}
|
||||
|
||||
func calcUncleInclusionRewards(gethBlock *types.Block) float64 {
|
||||
var uncleInclusionRewards float64
|
||||
staticBlockReward := staticRewardByBlockNumber(gethBlock.Number().Int64())
|
||||
for range gethBlock.Uncles() {
|
||||
uncleInclusionRewards += staticBlockReward * 1 / 32
|
||||
}
|
||||
return uncleInclusionRewards
|
||||
}
|
||||
|
||||
func calcTransactionFees(gethBlock *types.Block, client GethClient) float64 {
|
||||
var transactionFees float64
|
||||
for _, transaction := range gethBlock.Transactions() {
|
||||
receipt, err := client.TransactionReceipt(context.Background(), transaction.Hash())
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
transactionFees += float64(transaction.GasPrice().Int64() * receipt.GasUsed.Int64())
|
||||
}
|
||||
return transactionFees / params.Ether
|
||||
}
|
||||
|
||||
func staticRewardByBlockNumber(blockNumber int64) float64 {
|
||||
var staticBlockReward float64
|
||||
//https://blog.ethereum.org/2017/10/12/byzantium-hf-announcement/
|
||||
if blockNumber >= 4370000 {
|
||||
staticBlockReward = 3
|
||||
} else {
|
||||
staticBlockReward = 5
|
||||
}
|
||||
return staticBlockReward
|
||||
}
|
@ -6,7 +6,7 @@ import (
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
)
|
||||
|
||||
func GethLogToCoreLog(gethLog types.Log) core.Log {
|
||||
func LogToCoreLog(gethLog types.Log) core.Log {
|
||||
topics := gethLog.Topics
|
||||
var hexTopics = make(map[int]string)
|
||||
for i, topic := range topics {
|
||||
@ -26,7 +26,7 @@ func GethLogToCoreLog(gethLog types.Log) core.Log {
|
||||
func GethLogsToCoreLogs(gethLogs []types.Log) []core.Log {
|
||||
var logs []core.Log
|
||||
for _, log := range gethLogs {
|
||||
log := GethLogToCoreLog(log)
|
||||
log := LogToCoreLog(log)
|
||||
logs = append(logs, log)
|
||||
}
|
||||
return logs
|
@ -39,7 +39,7 @@ var _ = Describe("Conversion of GethLog to core.Log", func() {
|
||||
},
|
||||
}
|
||||
|
||||
coreLog := geth.GethLogToCoreLog(gethLog)
|
||||
coreLog := geth.LogToCoreLog(gethLog)
|
||||
|
||||
Expect(coreLog.Address).To(Equal(expected.Address))
|
||||
Expect(coreLog.BlockNumber).To(Equal(expected.BlockNumber))
|
||||
@ -79,8 +79,8 @@ var _ = Describe("Conversion of GethLog to core.Log", func() {
|
||||
},
|
||||
}
|
||||
|
||||
expectedOne := geth.GethLogToCoreLog(gethLogOne)
|
||||
expectedTwo := geth.GethLogToCoreLog(gethLogTwo)
|
||||
expectedOne := geth.LogToCoreLog(gethLogOne)
|
||||
expectedTwo := geth.LogToCoreLog(gethLogTwo)
|
||||
|
||||
coreLogs := geth.GethLogsToCoreLogs([]types.Log{gethLogOne, gethLogTwo})
|
||||
|
@ -18,7 +18,7 @@ func BigTo64(n *big.Int) int64 {
|
||||
return 0
|
||||
}
|
||||
|
||||
func GethReceiptToCoreReceipt(gethReceipt *types.Receipt) core.Receipt {
|
||||
func ReceiptToCoreReceipt(gethReceipt *types.Receipt) core.Receipt {
|
||||
bloom := hexutil.Encode(gethReceipt.Bloom.Bytes())
|
||||
var postState string
|
||||
var status int
|
||||
@ -49,7 +49,7 @@ func setContractAddress(gethReceipt *types.Receipt) string {
|
||||
func dereferenceLogs(gethReceipt *types.Receipt) []core.Log {
|
||||
logs := []core.Log{}
|
||||
for _, log := range gethReceipt.Logs {
|
||||
logs = append(logs, GethLogToCoreLog(*log))
|
||||
logs = append(logs, LogToCoreLog(*log))
|
||||
}
|
||||
return logs
|
||||
}
|
@ -36,7 +36,7 @@ var _ = Describe("Conversion of GethReceipt to core.Receipt", func() {
|
||||
TxHash: receipt.TxHash.Hex(),
|
||||
}
|
||||
|
||||
coreReceipt := geth.GethReceiptToCoreReceipt(&receipt)
|
||||
coreReceipt := geth.ReceiptToCoreReceipt(&receipt)
|
||||
Expect(coreReceipt.Bloom).To(Equal(expected.Bloom))
|
||||
Expect(coreReceipt.ContractAddress).To(Equal(expected.ContractAddress))
|
||||
Expect(coreReceipt.CumulativeGasUsed).To(Equal(expected.CumulativeGasUsed))
|
||||
@ -70,7 +70,7 @@ var _ = Describe("Conversion of GethReceipt to core.Receipt", func() {
|
||||
TxHash: receipt.TxHash.Hex(),
|
||||
}
|
||||
|
||||
coreReceipt := geth.GethReceiptToCoreReceipt(&receipt)
|
||||
coreReceipt := geth.ReceiptToCoreReceipt(&receipt)
|
||||
Expect(coreReceipt.Bloom).To(Equal(expected.Bloom))
|
||||
Expect(coreReceipt.ContractAddress).To(Equal(""))
|
||||
Expect(coreReceipt.CumulativeGasUsed).To(Equal(expected.CumulativeGasUsed))
|
@ -1,48 +1,25 @@
|
||||
package history
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/8thlight/vulcanizedb/pkg/core"
|
||||
"github.com/8thlight/vulcanizedb/pkg/repositories"
|
||||
)
|
||||
|
||||
type Window struct {
|
||||
LowerBound int
|
||||
UpperBound int
|
||||
MaxBlockNumber int
|
||||
}
|
||||
|
||||
func (window Window) Size() int {
|
||||
return int(window.UpperBound - window.LowerBound)
|
||||
}
|
||||
|
||||
func PopulateMissingBlocks(blockchain core.Blockchain, repository repositories.Repository, startingBlockNumber int64) int {
|
||||
lastBlock := blockchain.LastBlock().Int64()
|
||||
blockRange := repository.MissingBlockNumbers(startingBlockNumber, lastBlock-1)
|
||||
updateBlockRange(blockchain, repository, blockRange)
|
||||
log.SetPrefix("")
|
||||
log.Printf("Backfilling %d blocks\n\n", len(blockRange))
|
||||
RetrieveAndUpdateBlocks(blockchain, repository, blockRange)
|
||||
return len(blockRange)
|
||||
}
|
||||
|
||||
func UpdateBlocksWindow(blockchain core.Blockchain, repository repositories.Repository, windowSize int) Window {
|
||||
maxBlockNumber := blockchain.LastBlock().Int64()
|
||||
upperBound := maxBlockNumber - int64(1)
|
||||
lowerBound := upperBound - int64(windowSize)
|
||||
blockRange := MakeRange(lowerBound, upperBound)
|
||||
updateBlockRange(blockchain, repository, blockRange)
|
||||
return Window{int(lowerBound), int(upperBound), int(maxBlockNumber)}
|
||||
}
|
||||
|
||||
func updateBlockRange(blockchain core.Blockchain, repository repositories.Repository, blockNumbers []int64) int {
|
||||
func RetrieveAndUpdateBlocks(blockchain core.Blockchain, repository repositories.Repository, blockNumbers []int64) int {
|
||||
for _, blockNumber := range blockNumbers {
|
||||
block := blockchain.GetBlockByNumber(blockNumber)
|
||||
repository.CreateOrUpdateBlock(block)
|
||||
}
|
||||
return len(blockNumbers)
|
||||
}
|
||||
|
||||
func MakeRange(min, max int64) []int64 {
|
||||
a := make([]int64, max-min)
|
||||
for i := range a {
|
||||
a[i] = min + int64(i)
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
@ -66,30 +66,6 @@ var _ = Describe("Populating blocks", func() {
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
|
||||
It("updates the repository with a range of blocks w/in sliding window ", func() {
|
||||
blockchain := fakes.NewBlockchainWithBlocks([]core.Block{
|
||||
{Number: 1},
|
||||
{Number: 2},
|
||||
{Number: 3},
|
||||
{Number: 4},
|
||||
{Number: 5},
|
||||
})
|
||||
repository := repositories.NewInMemory()
|
||||
repository.CreateOrUpdateBlock(blockchain.GetBlockByNumber(5))
|
||||
|
||||
history.UpdateBlocksWindow(blockchain, repository, 2)
|
||||
|
||||
Expect(repository.BlockCount()).To(Equal(3))
|
||||
Expect(repository.HandleBlockCallCount).To(Equal(3))
|
||||
})
|
||||
|
||||
It("Generates a range of int64", func() {
|
||||
numberOfBlocksCreated := history.MakeRange(0, 5)
|
||||
expected := []int64{0, 1, 2, 3, 4}
|
||||
|
||||
Expect(numberOfBlocksCreated).To(Equal(expected))
|
||||
})
|
||||
|
||||
It("returns the number of blocks created", func() {
|
||||
blockchain := fakes.NewBlockchainWithBlocks([]core.Block{
|
||||
{Number: 4},
|
||||
@ -105,19 +81,19 @@ var _ = Describe("Populating blocks", func() {
|
||||
Expect(numberOfBlocksCreated).To(Equal(2))
|
||||
})
|
||||
|
||||
It("returns the window size", func() {
|
||||
window := history.Window{1, 3, 10}
|
||||
Expect(window.Size()).To(Equal(2))
|
||||
})
|
||||
|
||||
It("returns the number of largest block", func() {
|
||||
It("updates the repository with a range of blocks w/in the range ", func() {
|
||||
blockchain := fakes.NewBlockchainWithBlocks([]core.Block{
|
||||
{Number: 1},
|
||||
{Number: 2},
|
||||
{Number: 3},
|
||||
{Number: 4},
|
||||
{Number: 5},
|
||||
})
|
||||
maxBlockNumber := blockchain.LastBlock()
|
||||
repository := repositories.NewInMemory()
|
||||
|
||||
Expect(maxBlockNumber.Int64()).To(Equal(int64(3)))
|
||||
history.RetrieveAndUpdateBlocks(blockchain, repository, history.MakeRange(2, 5))
|
||||
Expect(repository.BlockCount()).To(Equal(3))
|
||||
Expect(repository.CreateOrUpdateBlockCallCount).To(Equal(3))
|
||||
})
|
||||
|
||||
})
|
||||
|
68
pkg/history/validate_blocks.go
Normal file
68
pkg/history/validate_blocks.go
Normal file
@ -0,0 +1,68 @@
|
||||
package history
|
||||
|
||||
import (
|
||||
"io"
|
||||
"text/template"
|
||||
|
||||
"github.com/8thlight/vulcanizedb/pkg/core"
|
||||
"github.com/8thlight/vulcanizedb/pkg/repositories"
|
||||
)
|
||||
|
||||
const WindowTemplate = `Validating Blocks
|
||||
|{{.LowerBound}}|-- Validation Window --|{{.UpperBound}}| ({{.UpperBound}}:HEAD)
|
||||
|
||||
`
|
||||
|
||||
var ParsedWindowTemplate = *template.Must(template.New("window").Parse(WindowTemplate))
|
||||
|
||||
type BlockValidator struct {
|
||||
blockchain core.Blockchain
|
||||
repository repositories.Repository
|
||||
windowSize int
|
||||
parsedLoggingTemplate template.Template
|
||||
}
|
||||
|
||||
func NewBlockValidator(blockchain core.Blockchain, repository repositories.Repository, windowSize int) *BlockValidator {
|
||||
return &BlockValidator{
|
||||
blockchain,
|
||||
repository,
|
||||
windowSize,
|
||||
ParsedWindowTemplate,
|
||||
}
|
||||
}
|
||||
|
||||
func (bv BlockValidator) ValidateBlocks() ValidationWindow {
|
||||
window := MakeValidationWindow(bv.blockchain, bv.windowSize)
|
||||
blockNumbers := MakeRange(window.LowerBound, window.UpperBound)
|
||||
RetrieveAndUpdateBlocks(bv.blockchain, bv.repository, blockNumbers)
|
||||
lastBlock := bv.blockchain.LastBlock().Int64()
|
||||
bv.repository.SetBlocksStatus(lastBlock)
|
||||
return window
|
||||
}
|
||||
|
||||
func (bv BlockValidator) Log(out io.Writer, window ValidationWindow) {
|
||||
bv.parsedLoggingTemplate.Execute(out, window)
|
||||
}
|
||||
|
||||
type ValidationWindow struct {
|
||||
LowerBound int64
|
||||
UpperBound int64
|
||||
}
|
||||
|
||||
func (window ValidationWindow) Size() int {
|
||||
return int(window.UpperBound - window.LowerBound)
|
||||
}
|
||||
|
||||
func MakeValidationWindow(blockchain core.Blockchain, windowSize int) ValidationWindow {
|
||||
upperBound := blockchain.LastBlock().Int64()
|
||||
lowerBound := upperBound - int64(windowSize)
|
||||
return ValidationWindow{lowerBound, upperBound}
|
||||
}
|
||||
|
||||
func MakeRange(min, max int64) []int64 {
|
||||
a := make([]int64, max-min)
|
||||
for i := range a {
|
||||
a[i] = min + int64(i)
|
||||
}
|
||||
return a
|
||||
}
|
90
pkg/history/validate_blocks_test.go
Normal file
90
pkg/history/validate_blocks_test.go
Normal file
@ -0,0 +1,90 @@
|
||||
package history_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
|
||||
"io/ioutil"
|
||||
"log"
|
||||
|
||||
"github.com/8thlight/vulcanizedb/pkg/core"
|
||||
"github.com/8thlight/vulcanizedb/pkg/fakes"
|
||||
"github.com/8thlight/vulcanizedb/pkg/history"
|
||||
"github.com/8thlight/vulcanizedb/pkg/repositories"
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
func init() {
|
||||
log.SetOutput(ioutil.Discard)
|
||||
}
|
||||
|
||||
var _ = Describe("Blocks validator", func() {
|
||||
|
||||
It("creates a ValidationWindow equal to (HEAD-windowSize, HEAD)", func() {
|
||||
blockchain := fakes.NewBlockchainWithBlocks([]core.Block{
|
||||
{Number: 1},
|
||||
{Number: 2},
|
||||
{Number: 3},
|
||||
{Number: 4},
|
||||
{Number: 5},
|
||||
})
|
||||
|
||||
validationWindow := history.MakeValidationWindow(blockchain, 2)
|
||||
|
||||
Expect(validationWindow.LowerBound).To(Equal(int64(3)))
|
||||
Expect(validationWindow.UpperBound).To(Equal(int64(5)))
|
||||
})
|
||||
|
||||
It("returns the window size", func() {
|
||||
window := history.ValidationWindow{1, 3}
|
||||
Expect(window.Size()).To(Equal(2))
|
||||
})
|
||||
|
||||
It("calls create or update for all blocks within the window", func() {
|
||||
blockchain := fakes.NewBlockchainWithBlocks([]core.Block{
|
||||
{Number: 4},
|
||||
{Number: 5},
|
||||
{Number: 6},
|
||||
{Number: 7},
|
||||
})
|
||||
repository := repositories.NewInMemory()
|
||||
|
||||
validator := history.NewBlockValidator(blockchain, repository, 2)
|
||||
window := validator.ValidateBlocks()
|
||||
Expect(window).To(Equal(history.ValidationWindow{5, 7}))
|
||||
Expect(repository.BlockCount()).To(Equal(2))
|
||||
Expect(repository.CreateOrUpdateBlockCallCount).To(Equal(2))
|
||||
})
|
||||
|
||||
It("logs window message", func() {
|
||||
expectedMessage := &bytes.Buffer{}
|
||||
window := history.ValidationWindow{5, 7}
|
||||
history.ParsedWindowTemplate.Execute(expectedMessage, history.ValidationWindow{5, 7})
|
||||
|
||||
blockchain := fakes.NewBlockchainWithBlocks([]core.Block{})
|
||||
repository := repositories.NewInMemory()
|
||||
validator := history.NewBlockValidator(blockchain, repository, 2)
|
||||
actualMessage := &bytes.Buffer{}
|
||||
validator.Log(actualMessage, window)
|
||||
Expect(actualMessage).To(Equal(expectedMessage))
|
||||
})
|
||||
|
||||
It("generates a range of int64s", func() {
|
||||
numberOfBlocksCreated := history.MakeRange(0, 5)
|
||||
expected := []int64{0, 1, 2, 3, 4}
|
||||
|
||||
Expect(numberOfBlocksCreated).To(Equal(expected))
|
||||
})
|
||||
|
||||
It("returns the number of largest block", func() {
|
||||
blockchain := fakes.NewBlockchainWithBlocks([]core.Block{
|
||||
{Number: 1},
|
||||
{Number: 2},
|
||||
{Number: 3},
|
||||
})
|
||||
maxBlockNumber := blockchain.LastBlock()
|
||||
|
||||
Expect(maxBlockNumber.Int64()).To(Equal(int64(3)))
|
||||
})
|
||||
|
||||
})
|
@ -1,18 +0,0 @@
|
||||
package observers
|
||||
|
||||
import (
|
||||
"github.com/8thlight/vulcanizedb/pkg/core"
|
||||
"github.com/8thlight/vulcanizedb/pkg/repositories"
|
||||
)
|
||||
|
||||
type BlockchainDbObserver struct {
|
||||
repository repositories.Repository
|
||||
}
|
||||
|
||||
func NewBlockchainDbObserver(repository repositories.Repository) BlockchainDbObserver {
|
||||
return BlockchainDbObserver{repository: repository}
|
||||
}
|
||||
|
||||
func (observer BlockchainDbObserver) NotifyBlockAdded(block core.Block) {
|
||||
observer.repository.CreateOrUpdateBlock(block)
|
||||
}
|
@ -1,38 +0,0 @@
|
||||
package observers_test
|
||||
|
||||
import (
|
||||
"github.com/8thlight/vulcanizedb/pkg/core"
|
||||
"github.com/8thlight/vulcanizedb/pkg/observers"
|
||||
"github.com/8thlight/vulcanizedb/pkg/repositories"
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
var _ = Describe("Saving blocks to the database", func() {
|
||||
|
||||
var repository *repositories.InMemory
|
||||
|
||||
BeforeEach(func() {
|
||||
repository = repositories.NewInMemory()
|
||||
})
|
||||
|
||||
It("implements the observer interface", func() {
|
||||
var observer core.BlockchainObserver = observers.NewBlockchainDbObserver(repository)
|
||||
Expect(observer).NotTo(BeNil())
|
||||
})
|
||||
|
||||
It("saves a block with one transaction", func() {
|
||||
block := core.Block{
|
||||
Number: 123,
|
||||
Transactions: []core.Transaction{{}},
|
||||
}
|
||||
|
||||
observer := observers.NewBlockchainDbObserver(repository)
|
||||
observer.NotifyBlockAdded(block)
|
||||
|
||||
savedBlock, err := repository.FindBlockByNumber(123)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(len(savedBlock.Transactions)).To(Equal(1))
|
||||
})
|
||||
|
||||
})
|
@ -1,32 +0,0 @@
|
||||
package observers
|
||||
|
||||
import (
|
||||
"os"
|
||||
"text/template"
|
||||
|
||||
"time"
|
||||
|
||||
"github.com/8thlight/vulcanizedb/pkg/core"
|
||||
)
|
||||
|
||||
const blockAddedTemplate = `
|
||||
New block was added: {{.Number}}
|
||||
Time: {{.Time | unix_time}}
|
||||
Gas Limit: {{.GasLimit}}
|
||||
Gas Used: {{.GasUsed}}
|
||||
Number of Transactions {{.Transactions | len}}
|
||||
|
||||
`
|
||||
|
||||
var funcMap = template.FuncMap{
|
||||
"unix_time": func(n int64) time.Time {
|
||||
return time.Unix(n, 0)
|
||||
},
|
||||
}
|
||||
var tmp = template.Must(template.New("window").Funcs(funcMap).Parse(blockAddedTemplate))
|
||||
|
||||
type BlockchainLoggingObserver struct{}
|
||||
|
||||
func (blockchainObserver BlockchainLoggingObserver) NotifyBlockAdded(block core.Block) {
|
||||
tmp.Execute(os.Stdout, block)
|
||||
}
|
@ -1,13 +0,0 @@
|
||||
package observers_test
|
||||
|
||||
import (
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestObservers(t *testing.T) {
|
||||
RegisterFailHandler(Fail)
|
||||
RunSpecs(t, "Observers Suite")
|
||||
}
|
@ -7,20 +7,20 @@ import (
|
||||
)
|
||||
|
||||
type InMemory struct {
|
||||
blocks map[int64]core.Block
|
||||
receipts map[string]core.Receipt
|
||||
contracts map[string]core.Contract
|
||||
logs map[string][]core.Log
|
||||
HandleBlockCallCount int
|
||||
blocks map[int64]core.Block
|
||||
receipts map[string]core.Receipt
|
||||
contracts map[string]core.Contract
|
||||
logs map[string][]core.Log
|
||||
CreateOrUpdateBlockCallCount int
|
||||
}
|
||||
|
||||
func NewInMemory() *InMemory {
|
||||
return &InMemory{
|
||||
HandleBlockCallCount: 0,
|
||||
blocks: make(map[int64]core.Block),
|
||||
receipts: make(map[string]core.Receipt),
|
||||
contracts: make(map[string]core.Contract),
|
||||
logs: make(map[string][]core.Log),
|
||||
CreateOrUpdateBlockCallCount: 0,
|
||||
blocks: make(map[int64]core.Block),
|
||||
receipts: make(map[string]core.Receipt),
|
||||
contracts: make(map[string]core.Contract),
|
||||
logs: make(map[string][]core.Log),
|
||||
}
|
||||
}
|
||||
|
||||
@ -98,7 +98,7 @@ func (repository *InMemory) MissingBlockNumbers(startingBlockNumber int64, endin
|
||||
}
|
||||
|
||||
func (repository *InMemory) CreateOrUpdateBlock(block core.Block) error {
|
||||
repository.HandleBlockCallCount++
|
||||
repository.CreateOrUpdateBlockCallCount++
|
||||
repository.blocks[block.Number] = block
|
||||
for _, transaction := range block.Transactions {
|
||||
repository.receipts[transaction.Hash] = transaction.Receipt
|
||||
|
@ -174,7 +174,7 @@ func (repository Postgres) MaxBlockNumber() int64 {
|
||||
}
|
||||
|
||||
func (repository Postgres) MissingBlockNumbers(startingBlockNumber int64, highestBlockNumber int64) []int64 {
|
||||
numbers := []int64{}
|
||||
numbers := make([]int64, 0)
|
||||
repository.Db.Select(&numbers,
|
||||
`SELECT all_block_numbers
|
||||
FROM (
|
||||
|
Loading…
Reference in New Issue
Block a user