-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Store mapping of eth transaction hashes to message cids #9965
Changes from all commits
a843607
f8dee09
f8121c8
6b0f111
3b28368
72f4250
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
package ethhashlookup | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can probably just name this file |
||
|
||
import ( | ||
"database/sql" | ||
"errors" | ||
"strconv" | ||
|
||
"github.com/ipfs/go-cid" | ||
_ "github.com/mattn/go-sqlite3" | ||
"golang.org/x/xerrors" | ||
|
||
"github.com/filecoin-project/lotus/chain/types/ethtypes" | ||
) | ||
|
||
var ErrNotFound = errors.New("not found") | ||
|
||
var pragmas = []string{ | ||
"PRAGMA synchronous = normal", | ||
"PRAGMA temp_store = memory", | ||
"PRAGMA mmap_size = 30000000000", | ||
"PRAGMA page_size = 32768", | ||
"PRAGMA auto_vacuum = NONE", | ||
"PRAGMA automatic_index = OFF", | ||
"PRAGMA journal_mode = WAL", | ||
"PRAGMA read_uncommitted = ON", | ||
} | ||
|
||
var ddls = []string{ | ||
`CREATE TABLE IF NOT EXISTS eth_tx_hashes ( | ||
hash TEXT PRIMARY KEY NOT NULL, | ||
cid TEXT NOT NULL UNIQUE, | ||
insertion_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL | ||
)`, | ||
|
||
`CREATE INDEX IF NOT EXISTS insertion_time_index ON eth_tx_hashes (insertion_time)`, | ||
|
||
// metadata containing version of schema | ||
`CREATE TABLE IF NOT EXISTS _meta ( | ||
version UINT64 NOT NULL UNIQUE | ||
)`, | ||
|
||
// version 1. | ||
`INSERT OR IGNORE INTO _meta (version) VALUES (1)`, | ||
} | ||
|
||
const schemaVersion = 1 | ||
|
||
const ( | ||
insertTxHash = `INSERT INTO eth_tx_hashes | ||
(hash, cid) | ||
VALUES(?, ?) | ||
ON CONFLICT (hash) DO UPDATE SET insertion_time = CURRENT_TIMESTAMP` | ||
) | ||
|
||
type EthTxHashLookup struct { | ||
db *sql.DB | ||
} | ||
|
||
func (ei *EthTxHashLookup) UpsertHash(txHash ethtypes.EthHash, c cid.Cid) error { | ||
hashEntry, err := ei.db.Prepare(insertTxHash) | ||
if err != nil { | ||
return xerrors.Errorf("prepare insert event: %w", err) | ||
} | ||
|
||
_, err = hashEntry.Exec(txHash.String(), c.String()) | ||
return err | ||
} | ||
|
||
func (ei *EthTxHashLookup) GetCidFromHash(txHash ethtypes.EthHash) (cid.Cid, error) { | ||
q, err := ei.db.Query("SELECT cid FROM eth_tx_hashes WHERE hash = :hash;", sql.Named("hash", txHash.String())) | ||
if err != nil { | ||
return cid.Undef, err | ||
} | ||
|
||
var c string | ||
if !q.Next() { | ||
return cid.Undef, ErrNotFound | ||
} | ||
err = q.Scan(&c) | ||
if err != nil { | ||
return cid.Undef, err | ||
} | ||
return cid.Decode(c) | ||
} | ||
|
||
func (ei *EthTxHashLookup) GetHashFromCid(c cid.Cid) (ethtypes.EthHash, error) { | ||
q, err := ei.db.Query("SELECT hash FROM eth_tx_hashes WHERE cid = :cid;", sql.Named("cid", c.String())) | ||
if err != nil { | ||
return ethtypes.EmptyEthHash, err | ||
} | ||
|
||
var hashString string | ||
if !q.Next() { | ||
return ethtypes.EmptyEthHash, ErrNotFound | ||
} | ||
err = q.Scan(&hashString) | ||
if err != nil { | ||
return ethtypes.EmptyEthHash, err | ||
} | ||
return ethtypes.ParseEthHash(hashString) | ||
} | ||
|
||
func (ei *EthTxHashLookup) DeleteEntriesOlderThan(days int) (int64, error) { | ||
res, err := ei.db.Exec("DELETE FROM eth_tx_hashes WHERE insertion_time < datetime('now', ?);", "-"+strconv.Itoa(days)+" day") | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
return res.RowsAffected() | ||
} | ||
|
||
func NewTransactionHashLookup(path string) (*EthTxHashLookup, error) { | ||
db, err := sql.Open("sqlite3", path+"?mode=rwc") | ||
if err != nil { | ||
return nil, xerrors.Errorf("open sqlite3 database: %w", err) | ||
} | ||
|
||
for _, pragma := range pragmas { | ||
if _, err := db.Exec(pragma); err != nil { | ||
_ = db.Close() | ||
return nil, xerrors.Errorf("exec pragma %q: %w", pragma, err) | ||
} | ||
} | ||
|
||
q, err := db.Query("SELECT name FROM sqlite_master WHERE type='table' AND name='_meta';") | ||
if err == sql.ErrNoRows || !q.Next() { | ||
// empty database, create the schema | ||
for _, ddl := range ddls { | ||
if _, err := db.Exec(ddl); err != nil { | ||
_ = db.Close() | ||
return nil, xerrors.Errorf("exec ddl %q: %w", ddl, err) | ||
} | ||
} | ||
Comment on lines
+125
to
+133
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would prefer if you assert against the number of tables you expect and their respective names. That way, if something gets corrupted (e.g. and just one table remains, e.g. if someone deleted the hash lookup table but not meta), you would be resilient enough to reconstruct the schema. You already do |
||
} else if err != nil { | ||
_ = db.Close() | ||
return nil, xerrors.Errorf("looking for _meta table: %w", err) | ||
} else { | ||
// Ensure we don't open a database from a different schema version | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: unnecessary blank line |
||
row := db.QueryRow("SELECT max(version) FROM _meta") | ||
var version int | ||
err := row.Scan(&version) | ||
if err != nil { | ||
_ = db.Close() | ||
return nil, xerrors.Errorf("invalid database version: no version found") | ||
} | ||
if version != schemaVersion { | ||
_ = db.Close() | ||
return nil, xerrors.Errorf("invalid database version: got %d, expected %d", version, schemaVersion) | ||
} | ||
} | ||
|
||
return &EthTxHashLookup{ | ||
db: db, | ||
}, nil | ||
} | ||
|
||
func (ei *EthTxHashLookup) Close() error { | ||
if ei.db == nil { | ||
return nil | ||
} | ||
return ei.db.Close() | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -392,10 +392,21 @@ func ParseEthHash(s string) (EthHash, error) { | |
return h, nil | ||
} | ||
|
||
func EthHashFromTxBytes(b []byte) EthHash { | ||
hasher := sha3.NewLegacyKeccak256() | ||
hasher.Write(b) | ||
hash := hasher.Sum(nil) | ||
|
||
var ethHash EthHash | ||
copy(ethHash[:], hash) | ||
return ethHash | ||
} | ||
|
||
func (h EthHash) String() string { | ||
return "0x" + hex.EncodeToString(h[:]) | ||
} | ||
|
||
// Should ONLY be used for blocks and Filecoin messages. Eth transactions expect a different hashing scheme. | ||
func (h EthHash) ToCid() cid.Cid { | ||
// err is always nil | ||
mh, _ := multihash.EncodeName(h[:], "blake2b-256") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we somehow type-alias EthHash to at least have some reasonable indication of what hash function we're expecting it to carry? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure I follow this comment @magik6k. You want better type safety to prevent accidental ToCid calls? |
||
|
@@ -556,7 +567,7 @@ type EthLog struct { | |
// The index corresponds to the sequence of messages produced by ChainGetParentMessages | ||
TransactionIndex EthUint64 `json:"transactionIndex"` | ||
|
||
// TransactionHash is the cid of the message that produced the event log. | ||
// TransactionHash is the hash of the RLP message that produced the event log. | ||
TransactionHash EthHash `json:"transactionHash"` | ||
Comment on lines
+570
to
571
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Eventually we might want to return events of built-in actors transactions too, but that gets a bit trickier anyway, so this is fine. |
||
|
||
// BlockHash is the hash of the tipset containing the message that produced the log. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting, this is a Filecoin specific
eth_
method but I don't think it matters. Can we separate such Filecoin specific Eth methods to another interface that we embed here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you explain exactly what you mean? Do you want another API in
EthAPI
on the same level asEthModuleAPI
andEthEventAPI
?