Skip to content

Commit

Permalink
Merge branch 'merge-v1.12.1' into merge-v1.12.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Tristan-Wilson committed Nov 3, 2023
2 parents bbeb229 + 890cbca commit f935bb5
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 4 deletions.
8 changes: 8 additions & 0 deletions arbitrum/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ func NewBackend(stack *node.Node, config *Config, chainDb ethdb.Database, publis
chanNewBlock: make(chan struct{}, 1),
}

if len(config.AllowMethod) > 0 {
rpcFilter := make(map[string]bool)
for _, method := range config.AllowMethod {
rpcFilter[method] = true
}
backend.stack.ApplyAPIFilter(rpcFilter)
}

backend.bloomIndexer.Start(backend.arb.BlockChain())
filterSystem, err := createRegisterAPIBackend(backend, filterConfig, config.ClassicRedirect, config.ClassicRedirectTimeout)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions arbitrum/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ type Config struct {
ClassicRedirect string `koanf:"classic-redirect"`
ClassicRedirectTimeout time.Duration `koanf:"classic-redirect-timeout"`
MaxRecreateStateDepth int64 `koanf:"max-recreate-state-depth"`

AllowMethod []string `koanf:"allow-method"`
}

type ArbDebugConfig struct {
Expand All @@ -57,6 +59,7 @@ func ConfigAddOptions(prefix string, f *flag.FlagSet) {
f.Int(prefix+".filter-log-cache-size", DefaultConfig.FilterLogCacheSize, "log filter system maximum number of cached blocks")
f.Duration(prefix+".filter-timeout", DefaultConfig.FilterTimeout, "log filter system maximum time filters stay active")
f.Int64(prefix+".max-recreate-state-depth", DefaultConfig.MaxRecreateStateDepth, "maximum depth for recreating state, measured in l2 gas (0=don't recreate state, -1=infinite, -2=use default value for archive or non-archive node (whichever is configured))")
f.StringSlice(prefix+".allow-method", DefaultConfig.AllowMethod, "list of whitelisted rpc methods")
arbDebug := DefaultConfig.ArbDebug
f.Uint64(prefix+".arbdebug.block-range-bound", arbDebug.BlockRangeBound, "bounds the number of blocks arbdebug calls may return")
f.Uint64(prefix+".arbdebug.timeout-queue-bound", arbDebug.TimeoutQueueBound, "bounds the length of timeout queues arbdebug calls may return")
Expand All @@ -81,6 +84,7 @@ var DefaultConfig = Config{
FeeHistoryMaxBlockCount: 1024,
ClassicRedirect: "",
MaxRecreateStateDepth: UninitializedMaxRecreateStateDepth, // default value should be set for depending on node type (archive / non-archive)
AllowMethod: []string{},
ArbDebug: ArbDebugConfig{
BlockRangeBound: 256,
TimeoutQueueBound: 512,
Expand Down
2 changes: 2 additions & 0 deletions eth/state_accessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ func (eth *Ethereum) StateAtBlock(ctx context.Context, block *types.Block, reexe
// Create an ephemeral trie.Database for isolating the live one. Otherwise
// the internal junks created by tracing will be persisted into the disk.
database = state.NewDatabaseWithConfig(eth.chainDb, &trie.Config{Cache: 16})
defer database.TrieDB().Close()
if statedb, err = state.New(block.Root(), database, nil); err == nil {
log.Info("Found disk backend for state trie", "root", block.Root(), "number", block.Number())
return statedb, noopReleaser, nil
Expand All @@ -100,6 +101,7 @@ func (eth *Ethereum) StateAtBlock(ctx context.Context, block *types.Block, reexe
// Create an ephemeral trie.Database for isolating the live one. Otherwise
// the internal junks created by tracing will be persisted into the disk.
database = state.NewDatabaseWithConfig(eth.chainDb, &trie.Config{Cache: 16})
defer database.TrieDB().Close()

// If we didn't check the live database, do check state over ephemeral database,
// otherwise we would rewind past a persisted block (specific corner case is
Expand Down
8 changes: 8 additions & 0 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ type Node struct {
inprocHandler *rpc.Server // In-process RPC request handler to process the API requests

databases map[*closeTrackingDB]struct{} // All open databases

apiFilter map[string]bool // Whitelisting API methods
}

const (
Expand Down Expand Up @@ -379,6 +381,11 @@ func (n *Node) obtainJWTSecret(cliParam string) ([]byte, error) {
return jwtSecret, nil
}

// ApplyAPIFilter is the first step in whitelisting given rpc methods inside apiFilter
func (n *Node) ApplyAPIFilter(apiFilter map[string]bool) {
n.apiFilter = apiFilter
}

// startRPC is a helper method to configure all the various RPC endpoints during node
// startup. It's not meant to be called at any time afterwards as it makes certain
// assumptions about the state of the node.
Expand Down Expand Up @@ -413,6 +420,7 @@ func (n *Node) startRPC() error {
rpcConfig := rpcEndpointConfig{
batchItemLimit: n.config.BatchRequestLimit,
batchResponseSizeLimit: n.config.BatchResponseMaxSize,
apiFilter: n.apiFilter,
}

initHttp := func(server *httpServer, port int) error {
Expand Down
3 changes: 3 additions & 0 deletions node/rpcstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type rpcEndpointConfig struct {
jwtSecret []byte // optional JWT secret
batchItemLimit int
batchResponseSizeLimit int
apiFilter map[string]bool
}

type rpcHandler struct {
Expand Down Expand Up @@ -309,6 +310,7 @@ func (h *httpServer) enableRPC(apis []rpc.API, config httpConfig) error {
// Create RPC server and handler.
srv := rpc.NewServer()
srv.SetBatchLimits(config.batchItemLimit, config.batchResponseSizeLimit)
srv.ApplyAPIFilter(config.apiFilter)
if err := RegisterApis(apis, config.Modules, srv); err != nil {
return err
}
Expand Down Expand Up @@ -347,6 +349,7 @@ func (h *httpServer) enableWS(apis []rpc.API, config wsConfig) error {
// Create RPC server and handler.
srv := rpc.NewServer()
srv.SetBatchLimits(config.batchItemLimit, config.batchResponseSizeLimit)
srv.ApplyAPIFilter(config.apiFilter)
if err := RegisterApis(apis, config.Modules, srv); err != nil {
return err
}
Expand Down
4 changes: 4 additions & 0 deletions rpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ func (s *Server) SetBatchLimits(itemLimit, maxResponseSize int) {
s.batchResponseLimit = maxResponseSize
}

func (s *Server) ApplyAPIFilter(apiFilter map[string]bool) {
s.services.apiFilter = apiFilter
}

// RegisterName creates a service for the given receiver type under the given name. When no
// methods on the given receiver match the criteria to be either a RPC method or a
// subscription an error is returned. Otherwise a new service is created and added to the
Expand Down
14 changes: 11 additions & 3 deletions rpc/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ var (
type serviceRegistry struct {
mu sync.Mutex
services map[string]service

apiFilter map[string]bool
}

// service represents a registered object.
Expand Down Expand Up @@ -81,11 +83,17 @@ func (r *serviceRegistry) registerName(name string, rcvr interface{}) error {
}
r.services[name] = svc
}
for name, cb := range callbacks {
for methodName, cb := range callbacks {
if r.apiFilter != nil {
key := name + "_" + methodName
if _, ok := r.apiFilter[key]; !ok {
continue
}
}
if cb.isSubscribe {
svc.subscriptions[name] = cb
svc.subscriptions[methodName] = cb
} else {
svc.callbacks[name] = cb
svc.callbacks[methodName] = cb
}
}
return nil
Expand Down
8 changes: 7 additions & 1 deletion trie/triedb/hashdb/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,13 @@ func (db *Database) Size() common.StorageSize {
}

// Close closes the trie database and releases all held resources.
func (db *Database) Close() error { return nil }
func (db *Database) Close() error {
if db.cleans != nil {
// Reset cleans cache to return mmaped memory chunks to fastcache pool
db.cleans.Reset()
}
return nil
}

// Scheme returns the node scheme used in the database.
func (db *Database) Scheme() string {
Expand Down
7 changes: 7 additions & 0 deletions trie/triedb/pathdb/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,13 @@ func (db *Database) Close() error {
db.lock.Lock()
defer db.lock.Unlock()

dl := db.tree.bottom()
if dl.cleans != nil {
// Reset cleans cache to return mmaped memory chunks to fastcache pool
// All diskLayers share the same cleans cache
dl.cleans.Reset()
}

db.readOnly = true
if db.freezer == nil {
return nil
Expand Down

0 comments on commit f935bb5

Please sign in to comment.