Skip to content
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

[VOC-2] Ethereum-Optimism using ipfs-ethdb #22

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions cmd/geth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,15 @@ var (
utils.EWASMInterpreterFlag,
utils.EVMInterpreterFlag,
configFileFlag,
utils.PostgresDatastoreFlag,
utils.PostgresDatabaseNameFlag,
utils.PostgresHostnameFlag,
utils.PostgresPortFlag,
utils.PostgresUserFlag,
utils.PostgresPasswordFlag,
utils.PostgresMaxOpenConnectionsFlag,
utils.PostgresMaxIdleConnectionsFlag,
utils.PostgresMaxConnLifetimeFlag,
}

rpcFlags = []cli.Flag{
Expand Down
128 changes: 128 additions & 0 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import (
"text/template"
"time"

"github.com/ethereum/go-ethereum/ethdb/postgres"

"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -749,6 +751,52 @@ var (
Usage: "External EVM configuration (default = built-in interpreter)",
Value: "",
}

// Postgres flags
PostgresDatastoreFlag = cli.BoolFlag{
Name: "postgres",
Usage: "Turn on Postgres as the backing datastore",
}
PostgresHostnameFlag = cli.StringFlag{
Name: "postgres.hostname",
Usage: "Hostname for the Postgres database",
Value: "localhost",
}
PostgresPortFlag = cli.IntFlag{
Name: "postgres.port",
Usage: "Port for the Postgres database",
Value: 5432,
}
PostgresUserFlag = cli.StringFlag{
Name: "postgres.user",
Usage: "User for the Postgres database",
Value: "postgres",
}
PostgresPasswordFlag = cli.StringFlag{
Name: "postgres.password",
Usage: "Password for the Postgres database",
Value: "",
}
PostgresDatabaseNameFlag = cli.StringFlag{
Name: "postgres.database",
Usage: "Name for the Postgres database",
Value: "geth",
}
PostgresMaxOpenConnectionsFlag = cli.IntFlag{
Name: "postgres.maxopen",
Usage: "Max number of open Postgres connections",
Value: 1024,
}
PostgresMaxIdleConnectionsFlag = cli.IntFlag{
Name: "postgres.maxidle",
Usage: "Max number of idle Postgres connections",
Value: 1,
}
PostgresMaxConnLifetimeFlag = cli.DurationFlag{
Name: "postgres.maxlifetime",
Usage: "Max lifetime for Postgres connections",
Value: 0,
}
)

// MakeDataDir retrieves the currently requested data directory, terminating
Expand Down Expand Up @@ -1174,6 +1222,9 @@ func SetNodeConfig(ctx *cli.Context, cfg *node.Config) {
setNodeUserIdent(ctx, cfg)
setDataDir(ctx, cfg)
setSmartCard(ctx, cfg)
if ctx.GlobalBool(PostgresDatastoreFlag.Name) {
setNodePostgres(ctx, cfg)
}

if ctx.GlobalIsSet(ExternalSignerFlag.Name) {
cfg.ExternalSigner = ctx.GlobalString(ExternalSignerFlag.Name)
Expand All @@ -1193,6 +1244,43 @@ func SetNodeConfig(ctx *cli.Context, cfg *node.Config) {
}
}

func setNodePostgres(ctx *cli.Context, cfg *node.Config) {
if !ctx.GlobalIsSet(PostgresDatabaseNameFlag.Name) {
log.Info("Node: Postgres database name not provided, defaulting to 'geth'")
}
if !ctx.GlobalIsSet(PostgresHostnameFlag.Name) {
log.Info("Node: Postgres hostname not provided, defaulting to localhost")
}
if !ctx.GlobalIsSet(PostgresPortFlag.Name) {
log.Info("Node: Postgres port not provided, defaulting to 5432")
}
if !ctx.GlobalIsSet(PostgresUserFlag.Name) {
log.Info("Node: Postgres user not provided, defaulting to 'postgres'")
}
if !ctx.GlobalIsSet(PostgresPasswordFlag.Name) {
log.Info("Node: Postgres password not provided")
}
if !ctx.GlobalIsSet(PostgresMaxOpenConnectionsFlag.Name) {
log.Info("Node: Postgres MaxOpenConnections not set, defaulting to 1024")
}
if !ctx.GlobalIsSet(PostgresMaxIdleConnectionsFlag.Name) {
log.Info("Node: Postgres MaxIdleConnections not set, defaulting to 16")
}
if !ctx.GlobalIsSet(PostgresMaxConnLifetimeFlag.Name) {
log.Info("Node: Postgres MaxConnLifetime not set, defaulting to no limit")
}
cfg.PostgresConfig = &postgres.Config{
Database: ctx.GlobalString(PostgresDatabaseNameFlag.Name),
Hostname: ctx.GlobalString(PostgresHostnameFlag.Name),
Port: ctx.GlobalInt(PostgresPortFlag.Name),
User: ctx.GlobalString(PostgresUserFlag.Name),
Password: ctx.GlobalString(PostgresPasswordFlag.Name),
MaxOpen: ctx.GlobalInt(PostgresMaxOpenConnectionsFlag.Name),
MaxIdle: ctx.GlobalInt(PostgresMaxIdleConnectionsFlag.Name),
MaxLifetime: ctx.GlobalDuration(PostgresMaxConnLifetimeFlag.Name),
}
}

func setSmartCard(ctx *cli.Context, cfg *node.Config) {
// Skip enabling smartcards if no path is set
path := ctx.GlobalString(SmartCardDaemonPathFlag.Name)
Expand Down Expand Up @@ -1429,6 +1517,9 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *eth.Config) {
setMiner(ctx, &cfg.Miner)
setWhitelist(ctx, cfg)
setLes(ctx, cfg)
if ctx.GlobalBool(PostgresDatastoreFlag.Name) {
setEthPostgres(ctx, cfg)
}

if ctx.GlobalIsSet(SyncModeFlag.Name) {
cfg.SyncMode = *GlobalTextMarshaler(ctx, SyncModeFlag.Name).(*downloader.SyncMode)
Expand Down Expand Up @@ -1524,6 +1615,43 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *eth.Config) {
}
}

func setEthPostgres(ctx *cli.Context, cfg *eth.Config) {
if !ctx.GlobalIsSet(PostgresDatabaseNameFlag.Name) {
log.Info("Eth: Postgres database name not provided, defaulting to 'geth'")
}
if !ctx.GlobalIsSet(PostgresHostnameFlag.Name) {
log.Info("Eth: Postgres hostname not provided, defaulting to localhost")
}
if !ctx.GlobalIsSet(PostgresPortFlag.Name) {
log.Info("Eth: Postgres port not provided, defaulting to 5432")
}
if !ctx.GlobalIsSet(PostgresUserFlag.Name) {
log.Info("Eth: Postgres user not provided, defaulting to 'postgres'")
}
if !ctx.GlobalIsSet(PostgresPasswordFlag.Name) {
log.Info("Eth: Postgres password not provided")
}
if !ctx.GlobalIsSet(PostgresMaxOpenConnectionsFlag.Name) {
log.Info("Eth: Postgres MaxOpenConnections not set, defaulting to 1024")
}
if !ctx.GlobalIsSet(PostgresMaxIdleConnectionsFlag.Name) {
log.Info("Eth: Postgres MaxIdleConnections not set, defaulting to 16")
}
if !ctx.GlobalIsSet(PostgresMaxConnLifetimeFlag.Name) {
log.Info("Eth: Postgres MaxConnLifetime not set, defaulting to no limit")
}
cfg.PostgresConfig = &postgres.Config{
Database: ctx.GlobalString(PostgresDatabaseNameFlag.Name),
Hostname: ctx.GlobalString(PostgresHostnameFlag.Name),
Port: ctx.GlobalInt(PostgresPortFlag.Name),
User: ctx.GlobalString(PostgresUserFlag.Name),
Password: ctx.GlobalString(PostgresPasswordFlag.Name),
MaxOpen: ctx.GlobalInt(PostgresMaxOpenConnectionsFlag.Name),
MaxIdle: ctx.GlobalInt(PostgresMaxIdleConnectionsFlag.Name),
MaxLifetime: ctx.GlobalDuration(PostgresMaxConnLifetimeFlag.Name),
}
}

// RegisterEthService adds an Ethereum client to the stack.
func RegisterEthService(stack *node.Node, cfg *eth.Config) {
var err error
Expand Down
8 changes: 4 additions & 4 deletions core/rawdb/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,11 +266,11 @@ func InspectDatabase(db ethdb.Database) error {
)
total += size
switch {
case bytes.HasPrefix(key, headerPrefix) && bytes.HasSuffix(key, headerTDSuffix):
case bytes.HasPrefix(key, HeaderPrefix) && bytes.HasSuffix(key, headerTDSuffix):
tdSize += size
case bytes.HasPrefix(key, headerPrefix) && bytes.HasSuffix(key, headerHashSuffix):
case bytes.HasPrefix(key, HeaderPrefix) && bytes.HasSuffix(key, headerHashSuffix):
numHashPairing += size
case bytes.HasPrefix(key, headerPrefix) && len(key) == (len(headerPrefix)+8+common.HashLength):
case bytes.HasPrefix(key, HeaderPrefix) && len(key) == (len(HeaderPrefix)+8+common.HashLength):
headerSize += size
case bytes.HasPrefix(key, headerNumberPrefix) && len(key) == (len(headerNumberPrefix)+common.HashLength):
hashNumPairing += size
Expand All @@ -280,7 +280,7 @@ func InspectDatabase(db ethdb.Database) error {
receiptSize += size
case bytes.HasPrefix(key, txLookupPrefix) && len(key) == (len(txLookupPrefix)+common.HashLength):
txlookupSize += size
case bytes.HasPrefix(key, preimagePrefix) && len(key) == (len(preimagePrefix)+common.HashLength):
case bytes.HasPrefix(key, PreimagePrefix) && len(key) == (len(PreimagePrefix)+common.HashLength):
preimageSize += size
case bytes.HasPrefix(key, bloomBitsPrefix) && len(key) == (len(bloomBitsPrefix)+10+common.HashLength):
bloomBitsSize += size
Expand Down
60 changes: 33 additions & 27 deletions core/rawdb/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ import (

// The fields below define the low level database schema prefixing.
var (
// KeyDelineation is used to delineate the key prefixes and suffixes
KeyDelineation = []byte("-fix-")

// NumberDelineation is used to delineate the block number encoded in a key
NumberDelineation = []byte("-nmb-")

// databaseVerisionKey tracks the current database version.
databaseVerisionKey = []byte("DatabaseVersion")

Expand All @@ -42,7 +48,7 @@ var (
fastTrieProgressKey = []byte("TrieSync")

// Data item prefixes (use single byte to avoid mixing data types, avoid `i`, used for indexes).
headerPrefix = []byte("h") // headerPrefix + num (uint64 big endian) + hash -> header
HeaderPrefix = []byte("h") // headerPrefix + num (uint64 big endian) + hash -> header
headerTDSuffix = []byte("t") // headerPrefix + num (uint64 big endian) + hash + headerTDSuffix -> td
headerHashSuffix = []byte("n") // headerPrefix + num (uint64 big endian) + headerHashSuffix -> hash
headerNumberPrefix = []byte("H") // headerNumberPrefix + hash -> num (uint64 big endian)
Expand All @@ -56,7 +62,7 @@ var (
// Optmism specific
txMetaPrefix = []byte("x") // txMetaPrefix + hash -> transaction metadata

preimagePrefix = []byte("secure-key-") // preimagePrefix + hash -> preimage
PreimagePrefix = []byte("secure-key-") // preimagePrefix + hash -> preimage
configPrefix = []byte("ethereum-config-") // config prefix for the db

// Chain index prefixes (use `i` + single byte to avoid mixing data types).
Expand Down Expand Up @@ -108,67 +114,67 @@ func encodeBlockNumber(number uint64) []byte {
return enc
}

// headerKeyPrefix = headerPrefix + num (uint64 big endian)
// headerKeyPrefix = headerPrefix + KeyDelineation + num (uint64 big endian) + NumberDelineation
func headerKeyPrefix(number uint64) []byte {
return append(headerPrefix, encodeBlockNumber(number)...)
return append(append(append(HeaderPrefix, KeyDelineation...), encodeBlockNumber(number)...), NumberDelineation...)
}

// headerKey = headerPrefix + num (uint64 big endian) + hash
// headerKey = HeaderPrefix + KeyDelineation + num (uint64 big endian) + NumberDelineation + hash
func headerKey(number uint64, hash common.Hash) []byte {
return append(append(headerPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
return append(append(append(append(HeaderPrefix, KeyDelineation...), encodeBlockNumber(number)...), NumberDelineation...), hash.Bytes()...)
}

// headerTDKey = headerPrefix + num (uint64 big endian) + hash + headerTDSuffix
// headerTDKey = HeaderPrefix + KeyDelineation + num (uint64 big endian) + NumberDelineation + hash + KeyDelineation + headerTDSuffix
func headerTDKey(number uint64, hash common.Hash) []byte {
return append(headerKey(number, hash), headerTDSuffix...)
return append(append(headerKey(number, hash), KeyDelineation...), headerTDSuffix...)
}

// headerHashKey = headerPrefix + num (uint64 big endian) + headerHashSuffix
// headerHashKey = HeaderPrefix + KeyDelineation + num (uint64 big endian) + NumberDelineation + KeyDelineation + headerHashSuffix
func headerHashKey(number uint64) []byte {
return append(append(headerPrefix, encodeBlockNumber(number)...), headerHashSuffix...)
return append(append(append(append(append(HeaderPrefix, KeyDelineation...), encodeBlockNumber(number)...), NumberDelineation...), KeyDelineation...), headerHashSuffix...)
}

// headerNumberKey = headerNumberPrefix + hash
// headerNumberKey = headerNumberPrefix + KeyDelineation + hash
func headerNumberKey(hash common.Hash) []byte {
return append(headerNumberPrefix, hash.Bytes()...)
return append(append(headerNumberPrefix, KeyDelineation...), hash.Bytes()...)
}

// blockBodyKey = blockBodyPrefix + num (uint64 big endian) + hash
// blockBodyKey = blockBodyPrefix + KeyDelineation + num (uint64 big endian) + NumberDelineation + hash
func blockBodyKey(number uint64, hash common.Hash) []byte {
return append(append(blockBodyPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
return append(append(append(append(blockBodyPrefix, KeyDelineation...), encodeBlockNumber(number)...), NumberDelineation...), hash.Bytes()...)
}

// blockReceiptsKey = blockReceiptsPrefix + num (uint64 big endian) + hash
// blockReceiptsKey = blockReceiptsPrefix + KeyDelineation + num (uint64 big endian) + NumberDelineation + hash
func blockReceiptsKey(number uint64, hash common.Hash) []byte {
return append(append(blockReceiptsPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
return append(append(append(append(blockReceiptsPrefix, KeyDelineation...), encodeBlockNumber(number)...), NumberDelineation...), hash.Bytes()...)
}

// txLookupKey = txLookupPrefix + hash
// txLookupKey = txLookupPrefix + KeyDelineation + hash
func txLookupKey(hash common.Hash) []byte {
return append(txLookupPrefix, hash.Bytes()...)
return append(append(txLookupPrefix, KeyDelineation...), hash.Bytes()...)
}

// txMetaKey = txMetaPrefix + hash
func txMetaKey(hash common.Hash) []byte {
return append(txMetaPrefix, hash.Bytes()...)
return append(append(txMetaPrefix, KeyDelineation...), hash.Bytes()...)
}

// bloomBitsKey = bloomBitsPrefix + bit (uint16 big endian) + section (uint64 big endian) + hash
// bloomBitsKey = bloomBitsPrefix + KeyDelineation + bit (uint16 big endian) + section (uint64 big endian) + hash
func bloomBitsKey(bit uint, section uint64, hash common.Hash) []byte {
key := append(append(bloomBitsPrefix, make([]byte, 10)...), hash.Bytes()...)
key := append(append(append(bloomBitsPrefix, KeyDelineation...), make([]byte, 10)...), hash.Bytes()...)

binary.BigEndian.PutUint16(key[1:], uint16(bit))
binary.BigEndian.PutUint64(key[3:], section)
binary.BigEndian.PutUint16(key[2:], uint16(bit))
binary.BigEndian.PutUint64(key[4:], section)

return key
}

// preimageKey = preimagePrefix + hash
// preimageKey = preimagePrefix + KeyDelineation + hash
func preimageKey(hash common.Hash) []byte {
return append(preimagePrefix, hash.Bytes()...)
return append(append(PreimagePrefix, KeyDelineation...), hash.Bytes()...)
}

// configKey = configPrefix + hash
// configKey = configPrefix + KeyDelineation + hash
func configKey(hash common.Hash) []byte {
return append(configPrefix, hash.Bytes()...)
return append(append(configPrefix, KeyDelineation...), hash.Bytes()...)
}
5 changes: 5 additions & 0 deletions eth/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"runtime"
"time"

"github.com/ethereum/go-ethereum/ethdb/postgres"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/consensus/ethash"
"github.com/ethereum/go-ethereum/core"
Expand Down Expand Up @@ -160,4 +162,7 @@ type Config struct {

// MuirGlacier block override (TODO: remove after the fork)
OverrideMuirGlacier *big.Int

// Config params for Postgres database
PostgresConfig *postgres.Config
}
Loading