Skip to content

Commit

Permalink
Merge pull request #102 from vulcanize/log-table
Browse files Browse the repository at this point in the history
Create a seperate table for storing logs.
  • Loading branch information
arijitAD authored Aug 27, 2021
2 parents ae82f95 + ca7f7d7 commit 639af8b
Show file tree
Hide file tree
Showing 23 changed files with 762 additions and 207 deletions.
1 change: 0 additions & 1 deletion .github/workflows/on-master.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,3 @@ jobs:
run: echo ${{ secrets.GITHUB_TOKEN }} | docker login https://docker.pkg.github.com -u vulcanize --password-stdin
- name: Docker Push
run: docker push docker.pkg.github.com/vulcanize/go-ethereum/go-ethereum:${{steps.vars.outputs.sha}}

7 changes: 4 additions & 3 deletions core/types/receipt.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ type Receipt struct {
BlockHash common.Hash `json:"blockHash,omitempty"`
BlockNumber *big.Int `json:"blockNumber,omitempty"`
TransactionIndex uint `json:"transactionIndex"`
LogRoot common.Hash `json:"logRoot"`
}

type receiptMarshaling struct {
Expand Down Expand Up @@ -211,7 +212,7 @@ func (r *Receipt) DecodeRLP(s *rlp.Stream) error {
}
}

// UnmarshalBinary decodes the canonical encoding of receipts.
// UnmarshalBinary decodes the consensus encoding of receipts.
// It supports legacy RLP receipts and EIP-2718 typed receipts.
func (r *Receipt) UnmarshalBinary(b []byte) error {
if len(b) > 0 && b[0] > 0x7f {
Expand All @@ -234,13 +235,13 @@ func (r *Receipt) decodeTyped(b []byte) error {
return errEmptyTypedReceipt
}
switch b[0] {
case AccessListTxType:
case DynamicFeeTxType, AccessListTxType:
var data receiptRLP
err := rlp.DecodeBytes(b[1:], &data)
if err != nil {
return err
}
r.Type = AccessListTxType
r.Type = b[0]
return r.setFromRLP(data)
default:
return ErrTxTypeNotSupported
Expand Down
37 changes: 8 additions & 29 deletions statediff/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/rlp"
sdtrie "github.com/ethereum/go-ethereum/statediff/trie"
. "github.com/ethereum/go-ethereum/statediff/types"
"github.com/ethereum/go-ethereum/trie"
)
Expand All @@ -51,28 +52,6 @@ type builder struct {
stateCache state.Database
}

func resolveNode(it trie.NodeIterator, trieDB *trie.Database) (StateNode, []interface{}, error) {
nodePath := make([]byte, len(it.Path()))
copy(nodePath, it.Path())
node, err := trieDB.Node(it.Hash())
if err != nil {
return StateNode{}, nil, err
}
var nodeElements []interface{}
if err := rlp.DecodeBytes(node, &nodeElements); err != nil {
return StateNode{}, nil, err
}
ty, err := CheckKeyType(nodeElements)
if err != nil {
return StateNode{}, nil, err
}
return StateNode{
NodeType: ty,
Path: nodePath,
NodeValue: node,
}, nodeElements, nil
}

// convenience
func stateNodeAppender(nodes *[]StateNode) StateNodeSink {
return func(node StateNode) error {
Expand Down Expand Up @@ -127,7 +106,7 @@ func (sdb *builder) buildStateTrie(it trie.NodeIterator) ([]StateNode, []CodeAnd
if it.Leaf() || bytes.Equal(nullHashBytes, it.Hash().Bytes()) {
continue
}
node, nodeElements, err := resolveNode(it, sdb.stateCache.TrieDB())
node, nodeElements, err := sdtrie.ResolveNode(it, sdb.stateCache.TrieDB())
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -319,7 +298,7 @@ func (sdb *builder) createdAndUpdatedState(a, b trie.NodeIterator, watchedAddres
if it.Leaf() || bytes.Equal(nullHashBytes, it.Hash().Bytes()) {
continue
}
node, nodeElements, err := resolveNode(it, sdb.stateCache.TrieDB())
node, nodeElements, err := sdtrie.ResolveNode(it, sdb.stateCache.TrieDB())
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -363,7 +342,7 @@ func (sdb *builder) createdAndUpdatedStateWithIntermediateNodes(a, b trie.NodeIt
if it.Leaf() || bytes.Equal(nullHashBytes, it.Hash().Bytes()) {
continue
}
node, nodeElements, err := resolveNode(it, sdb.stateCache.TrieDB())
node, nodeElements, err := sdtrie.ResolveNode(it, sdb.stateCache.TrieDB())
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -415,7 +394,7 @@ func (sdb *builder) deletedOrUpdatedState(a, b trie.NodeIterator, diffPathsAtB m
if it.Leaf() || bytes.Equal(nullHashBytes, it.Hash().Bytes()) {
continue
}
node, nodeElements, err := resolveNode(it, sdb.stateCache.TrieDB())
node, nodeElements, err := sdtrie.ResolveNode(it, sdb.stateCache.TrieDB())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -576,7 +555,7 @@ func (sdb *builder) buildStorageNodesFromTrie(it trie.NodeIterator, watchedStora
if it.Leaf() || bytes.Equal(nullHashBytes, it.Hash().Bytes()) {
continue
}
node, nodeElements, err := resolveNode(it, sdb.stateCache.TrieDB())
node, nodeElements, err := sdtrie.ResolveNode(it, sdb.stateCache.TrieDB())
if err != nil {
return err
}
Expand Down Expand Up @@ -650,7 +629,7 @@ func (sdb *builder) createdAndUpdatedStorage(a, b trie.NodeIterator, watchedKeys
if it.Leaf() || bytes.Equal(nullHashBytes, it.Hash().Bytes()) {
continue
}
node, nodeElements, err := resolveNode(it, sdb.stateCache.TrieDB())
node, nodeElements, err := sdtrie.ResolveNode(it, sdb.stateCache.TrieDB())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -695,7 +674,7 @@ func (sdb *builder) deletedOrUpdatedStorage(a, b trie.NodeIterator, diffPathsAtB
if it.Leaf() || bytes.Equal(nullHashBytes, it.Hash().Bytes()) {
continue
}
node, nodeElements, err := resolveNode(it, sdb.stateCache.TrieDB())
node, nodeElements, err := sdtrie.ResolveNode(it, sdb.stateCache.TrieDB())
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,11 @@ CREATE TABLE eth.receipt_cids (
mh_key TEXT NOT NULL REFERENCES public.blocks (key) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
contract VARCHAR(66),
contract_hash VARCHAR(66),
topic0s VARCHAR(66)[],
topic1s VARCHAR(66)[],
topic2s VARCHAR(66)[],
topic3s VARCHAR(66)[],
log_contracts VARCHAR(66)[],
post_state VARCHAR(66),
post_status INTEGER,
log_root VARCHAR(66),
UNIQUE (tx_id)
);

-- +goose Down
DROP TABLE eth.receipt_cids;
DROP TABLE eth.receipt_cids;
17 changes: 1 addition & 16 deletions statediff/db/migrations/00013_create_cid_indexes.sql
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,6 @@ 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);

Expand Down Expand Up @@ -93,11 +83,6 @@ 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;
Expand All @@ -118,4 +103,4 @@ 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;
DROP INDEX eth.block_number_index;
60 changes: 60 additions & 0 deletions statediff/db/migrations/00016_create_eth_log_cids_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
-- +goose Up
CREATE TABLE eth.log_cids (
id SERIAL PRIMARY KEY,
leaf_cid TEXT NOT NULL,
leaf_mh_key TEXT NOT NULL REFERENCES public.blocks (key) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
receipt_id INTEGER NOT NULL REFERENCES eth.receipt_cids (id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
address VARCHAR(66),
log_data BYTEA,
index INTEGER NOT NULL,
topic0 VARCHAR(66),
topic1 VARCHAR(66),
topic2 VARCHAR(66),
topic3 VARCHAR(66),
UNIQUE (receipt_id, index)
);

CREATE INDEX log_mh_index ON eth.log_cids USING btree (leaf_mh_key);

CREATE INDEX log_cid_index ON eth.log_cids USING btree (leaf_cid);

CREATE INDEX log_rct_id_index ON eth.log_cids USING btree (receipt_id);
--
-- Name: log_topic0_index; Type: INDEX; Schema: eth; Owner: -
--

CREATE INDEX log_topic0_index ON eth.log_cids USING btree (topic0);


--
-- Name: log_topic1_index; Type: INDEX; Schema: eth; Owner: -
--

CREATE INDEX log_topic1_index ON eth.log_cids USING btree (topic1);


--
-- Name: log_topic2_index; Type: INDEX; Schema: eth; Owner: -
--

CREATE INDEX log_topic2_index ON eth.log_cids USING btree (topic2);


--
-- Name: log_topic3_index; Type: INDEX; Schema: eth; Owner: -
--

CREATE INDEX log_topic3_index ON eth.log_cids USING btree (topic3);


-- +goose Down
-- log indexes
DROP INDEX eth.log_mh_index;
DROP INDEX eth.log_cid_index;
DROP INDEX eth.log_rct_id_index;
DROP INDEX eth.log_topic0_index;
DROP INDEX eth.log_topic1_index;
DROP INDEX eth.log_topic2_index;
DROP INDEX eth.log_topic3_index;

DROP TABLE eth.log_cids;
29 changes: 0 additions & 29 deletions statediff/db/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -998,34 +998,6 @@ CREATE INDEX rct_log_contract_index ON eth.receipt_cids USING gin (log_contracts
CREATE INDEX rct_mh_index ON eth.receipt_cids USING btree (mh_key);


--
-- Name: rct_topic0_index; Type: INDEX; Schema: eth; Owner: -
--

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


--
-- Name: rct_topic1_index; Type: INDEX; Schema: eth; Owner: -
--

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


--
-- Name: rct_topic2_index; Type: INDEX; Schema: eth; Owner: -
--

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


--
-- Name: rct_topic3_index; Type: INDEX; Schema: eth; Owner: -
--

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


--
-- Name: rct_tx_id_index; Type: INDEX; Schema: eth; Owner: -
--
Expand Down Expand Up @@ -1330,4 +1302,3 @@ ALTER TABLE ONLY eth.uncle_cids
--
-- PostgreSQL database dump complete
--

25 changes: 0 additions & 25 deletions statediff/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,8 @@
package statediff

import (
"fmt"
"sort"
"strings"

sdtypes "github.com/ethereum/go-ethereum/statediff/types"
)

func sortKeys(data AccountMap) []string {
Expand Down Expand Up @@ -74,25 +71,3 @@ func findIntersection(a, b []string) []string {
}

}

// CheckKeyType checks what type of key we have
func CheckKeyType(elements []interface{}) (sdtypes.NodeType, error) {
if len(elements) > 2 {
return sdtypes.Branch, nil
}
if len(elements) < 2 {
return sdtypes.Unknown, fmt.Errorf("node cannot be less than two elements in length")
}
switch elements[0].([]byte)[0] / 16 {
case '\x00':
return sdtypes.Extension, nil
case '\x01':
return sdtypes.Extension, nil
case '\x02':
return sdtypes.Leaf, nil
case '\x03':
return sdtypes.Leaf, nil
default:
return sdtypes.Unknown, fmt.Errorf("unknown hex prefix")
}
}
Loading

0 comments on commit 639af8b

Please sign in to comment.