Skip to content

Commit

Permalink
Merge branch 'master' into merge-v1.11.6
Browse files Browse the repository at this point in the history
  • Loading branch information
Tristan-Wilson authored Jul 31, 2023
2 parents 557ebf0 + fd56bdb commit cc75f50
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 4 deletions.
42 changes: 39 additions & 3 deletions arbitrum/recordingdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,15 @@ import (
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/metrics"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/trie"
flag "github.com/spf13/pflag"
)

var (
recordingDbSize = metrics.NewRegisteredGauge("arb/validator/recordingdb/size", nil) // note: only updating when adding state, not when removing - but should be good enough
recordingDbReferences = metrics.NewRegisteredGauge("arb/validator/recordingdb/references", nil)
)

type RecordingKV struct {
Expand Down Expand Up @@ -151,17 +158,34 @@ func (r *RecordingChainContext) GetMinBlockNumberAccessed() uint64 {
return r.minBlockNumberAccessed
}

type RecordingDatabaseConfig struct {
TrieDirtyCache int `koanf:"trie-dirty-cache"`
TrieCleanCache int `koanf:"trie-clean-cache"`
}

var DefaultRecordingDatabaseConfig = RecordingDatabaseConfig{
TrieDirtyCache: 1024,
TrieCleanCache: 16,
}

func RecordingDatabaseConfigAddOptions(prefix string, f *flag.FlagSet) {
f.Int(prefix+".trie-dirty-cache", DefaultRecordingDatabaseConfig.TrieDirtyCache, "like trie-dirty-cache for the separate, recording database (used for validation)")
f.Int(prefix+".trie-clean-cache", DefaultRecordingDatabaseConfig.TrieCleanCache, "like trie-clean-cache for the separate, recording database (used for validation)")
}

type RecordingDatabase struct {
config *RecordingDatabaseConfig
db state.Database
bc *core.BlockChain
mutex sync.Mutex // protects StateFor and Dereference
references int64
}

func NewRecordingDatabase(ethdb ethdb.Database, blockchain *core.BlockChain) *RecordingDatabase {
func NewRecordingDatabase(config *RecordingDatabaseConfig, ethdb ethdb.Database, blockchain *core.BlockChain) *RecordingDatabase {
return &RecordingDatabase{
db: state.NewDatabaseWithConfig(ethdb, &trie.Config{Cache: 16}), //TODO cache needed? configurable?
bc: blockchain,
config: config,
db: state.NewDatabaseWithConfig(ethdb, &trie.Config{Cache: config.TrieCleanCache}),
bc: blockchain,
}
}

Expand Down Expand Up @@ -194,13 +218,15 @@ func (r *RecordingDatabase) WriteStateToDatabase(header *types.Header) error {
// lock must be held when calling that
func (r *RecordingDatabase) referenceRootLockHeld(root common.Hash) {
r.references++
recordingDbReferences.Update(r.references)
r.db.TrieDB().Reference(root, common.Hash{})
}

func (r *RecordingDatabase) dereferenceRoot(root common.Hash) {
r.mutex.Lock()
defer r.mutex.Unlock()
r.references--
recordingDbReferences.Update(r.references)
r.db.TrieDB().Dereference(root)
}

Expand All @@ -215,6 +241,16 @@ func (r *RecordingDatabase) addStateVerify(statedb *state.StateDB, expected comm
return fmt.Errorf("bad root hash expected: %v got: %v", expected, result)
}
r.referenceRootLockHeld(result)

size, _ := r.db.TrieDB().Size()
limit := common.StorageSize(r.config.TrieDirtyCache) * 1024 * 1024
recordingDbSize.Update(int64(size))
if size > limit {
log.Info("Recording DB: flushing to disk", "size", size, "limit", limit)
r.db.TrieDB().Cap(limit - ethdb.IdealBatchSize)
size, _ = r.db.TrieDB().Size()
recordingDbSize.Update(int64(size))
}
return nil
}

Expand Down
20 changes: 20 additions & 0 deletions core/rawdb/accessors_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,10 @@ func ReadLogs(db ethdb.Reader, hash common.Hash, number uint64, config *params.C
}
receipts := []*receiptLogs{}
if err := rlp.DecodeBytes(data, &receipts); err != nil {
if logs := readLegacyLogs(db, hash, number, config); logs != nil {
return logs
}

log.Error("Invalid receipt array RLP", "hash", hash, "err", err)
return nil
}
Expand All @@ -745,6 +749,22 @@ func ReadLogs(db ethdb.Reader, hash common.Hash, number uint64, config *params.C
return logs
}

// readLegacyLogs is a temporary workaround for when trying to read logs
// from a block which has its receipt stored in the legacy format. It'll
// be removed after users have migrated their freezer databases.
// Arbitrum: we are keeping this to handle classic (legacy) receipts
func readLegacyLogs(db ethdb.Reader, hash common.Hash, number uint64, config *params.ChainConfig) [][]*types.Log {
receipts := ReadReceipts(db, hash, number, config)
if receipts == nil {
return nil
}
logs := make([][]*types.Log, len(receipts))
for i, receipt := range receipts {
logs[i] = receipt.Logs
}
return logs
}

// ReadBlock retrieves an entire block corresponding to the hash, assembling it
// back from the stored header and body. If either the header or body could not
// be retrieved nil is returned.
Expand Down
13 changes: 12 additions & 1 deletion node/rpcstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,11 @@ func (h *httpServer) doStop() {
h.server, h.listener = nil, nil
}

// Arbitrum: Allows injection of custom http.Handler functionality.
var WrapHTTPHandler = func(srv http.Handler) (http.Handler, error) {
return srv, nil
}

// enableRPC turns on JSON-RPC over HTTP on the server.
func (h *httpServer) enableRPC(apis []rpc.API, config httpConfig) error {
h.mu.Lock()
Expand All @@ -300,9 +305,15 @@ func (h *httpServer) enableRPC(apis []rpc.API, config httpConfig) error {
if err := RegisterApis(apis, config.Modules, srv); err != nil {
return err
}

httpHandler, err := WrapHTTPHandler(srv)
if err != nil {
return err
}

h.httpConfig = config
h.httpHandler.Store(&rpcHandler{
Handler: NewHTTPHandlerStack(srv, config.CorsAllowedOrigins, config.Vhosts, config.jwtSecret),
Handler: NewHTTPHandlerStack(httpHandler, config.CorsAllowedOrigins, config.Vhosts, config.jwtSecret),
server: srv,
})
return nil
Expand Down

0 comments on commit cc75f50

Please sign in to comment.