erigon-pulse/ethdb/storage_mode.go
ledgerwatch 59e5502744
Initial work on RPC streaming (#1880)
* Initial work on RPC streaming

* Create the stream and pass it on

* trace_filter expressed as streamable (not finished)

* Reenable call trace index

* Add new line

* Remove storage mode override

* Tool for overriding storage mode

* Diagnostics

* Make trace_filter more streamy

* Streaming debug_traceTransaction and debug_traceCall

* Fix test

* Log error on stream flush

* Enable streaming for http

* Flush the stream too

* Make trace_filter flush too

* Stop streaming if client is not interested

* Try to quiet annoying test|

* Revert "Try to quiet annoying test|"

This reverts commit 42849257bfa52e90140aa535af34b957cd97a222.

* Debug log for test

* Proceed with handshake regardless of whether peer notification worked

Co-authored-by: Alexey Sharp <alexeysharp@Alexeys-iMac.local>
Co-authored-by: Alex Sharp <alexsharp@Alexs-MacBook-Pro.local>
2021-05-06 18:37:38 +01:00

169 lines
3.2 KiB
Go

package ethdb
import (
"fmt"
"github.com/ledgerwatch/turbo-geth/common/dbutils"
)
type StorageMode struct {
History bool
Receipts bool
TxIndex bool
CallTraces bool
}
var DefaultStorageMode = StorageMode{History: true, Receipts: true, TxIndex: true, CallTraces: false}
func (m StorageMode) ToString() string {
modeString := ""
if m.History {
modeString += "h"
}
if m.Receipts {
modeString += "r"
}
if m.TxIndex {
modeString += "t"
}
if m.CallTraces {
modeString += "c"
}
return modeString
}
func StorageModeFromString(flags string) (StorageMode, error) {
mode := StorageMode{}
for _, flag := range flags {
switch flag {
case 'h':
mode.History = true
case 'r':
mode.Receipts = true
case 't':
mode.TxIndex = true
case 'c':
mode.CallTraces = true
default:
return mode, fmt.Errorf("unexpected flag found: %c", flag)
}
}
return mode, nil
}
func GetStorageModeFromDB(db KVGetter) (StorageMode, error) {
var (
sm StorageMode
v []byte
err error
)
v, err = db.GetOne(dbutils.DatabaseInfoBucket, dbutils.StorageModeHistory)
if err != nil {
return StorageMode{}, err
}
sm.History = len(v) == 1 && v[0] == 1
v, err = db.GetOne(dbutils.DatabaseInfoBucket, dbutils.StorageModeReceipts)
if err != nil {
return StorageMode{}, err
}
sm.Receipts = len(v) == 1 && v[0] == 1
v, err = db.GetOne(dbutils.DatabaseInfoBucket, dbutils.StorageModeTxIndex)
if err != nil {
return StorageMode{}, err
}
sm.TxIndex = len(v) == 1 && v[0] == 1
v, err = db.GetOne(dbutils.DatabaseInfoBucket, dbutils.StorageModeCallTraces)
if err != nil {
return StorageMode{}, err
}
sm.CallTraces = len(v) == 1 && v[0] == 1
return sm, nil
}
func OverrideStorageMode(db Database, sm StorageMode) error {
var (
err error
)
err = setMode(db, dbutils.StorageModeHistory, sm.History)
if err != nil {
return err
}
err = setMode(db, dbutils.StorageModeReceipts, sm.Receipts)
if err != nil {
return err
}
err = setMode(db, dbutils.StorageModeTxIndex, sm.TxIndex)
if err != nil {
return err
}
err = setMode(db, dbutils.StorageModeCallTraces, sm.CallTraces)
if err != nil {
return err
}
return nil
}
func SetStorageModeIfNotExist(db Database, sm StorageMode) error {
var (
err error
)
err = setModeOnEmpty(db, dbutils.StorageModeHistory, sm.History)
if err != nil {
return err
}
err = setModeOnEmpty(db, dbutils.StorageModeReceipts, sm.Receipts)
if err != nil {
return err
}
err = setModeOnEmpty(db, dbutils.StorageModeTxIndex, sm.TxIndex)
if err != nil {
return err
}
err = setModeOnEmpty(db, dbutils.StorageModeCallTraces, sm.CallTraces)
if err != nil {
return err
}
return nil
}
func setMode(db Database, key []byte, currentValue bool) error {
val := []byte{2}
if currentValue {
val = []byte{1}
}
if err := db.Put(dbutils.DatabaseInfoBucket, key, val); err != nil {
return err
}
return nil
}
func setModeOnEmpty(db Database, key []byte, currentValue bool) error {
mode, err := db.GetOne(dbutils.DatabaseInfoBucket, key)
if err != nil {
return err
}
if len(mode) == 0 {
val := []byte{2}
if currentValue {
val = []byte{1}
}
if err = db.Put(dbutils.DatabaseInfoBucket, key, val); err != nil {
return err
}
}
return nil
}