feat: Waku v2 bridge

Issue #12610
This commit is contained in:
Michal Iskierko
2023-11-12 13:29:38 +01:00
parent 56e7bd01ca
commit 6d31343205
6716 changed files with 1982502 additions and 5891 deletions

View File

@@ -0,0 +1,34 @@
package rpcstats
import (
"context"
)
// PublicAPI represents a set of APIs from the namespace.
type PublicAPI struct {
s *Service
}
// NewAPI creates an instance of the API.
func NewAPI(s *Service) *PublicAPI {
return &PublicAPI{s: s}
}
// Reset resets RPC usage stats
func (api *PublicAPI) Reset(context context.Context) {
resetStats()
}
type RPCStats struct {
Total uint `json:"total"`
CounterPerMethod map[string]uint `json:"methods"`
}
// GetStats retrun RPC usage stats
func (api *PublicAPI) GetStats(context context.Context) (RPCStats, error) {
total, perMethod := getStats()
return RPCStats{
Total: total,
CounterPerMethod: perMethod,
}, nil
}

View File

@@ -0,0 +1,44 @@
package rpcstats
import (
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/rpc"
)
// Service represents our own implementation of status status operations.
type Service struct{}
// New returns a new Service.
func New() *Service {
return &Service{}
}
// APIs returns a list of new APIs.
func (s *Service) APIs() []rpc.API {
return []rpc.API{
{
Namespace: "rpcstats",
Version: "1.0",
Service: NewAPI(s),
Public: true,
},
}
}
// Protocols returns list of p2p protocols.
func (s *Service) Protocols() []p2p.Protocol {
return nil
}
// Start is run when a service is started.
// It does nothing in this case but is required by `node.Service` interface.
func (s *Service) Start() error {
resetStats()
return nil
}
// Stop is run when a service is stopped.
// It does nothing in this case but is required by `node.Service` interface.
func (s *Service) Stop() error {
return nil
}

View File

@@ -0,0 +1,48 @@
package rpcstats
import (
"sync"
)
type RPCUsageStats struct {
total uint
counterPerMethod map[string]uint
rw sync.RWMutex
}
var stats *RPCUsageStats
func getInstance() *RPCUsageStats {
if stats == nil {
stats = &RPCUsageStats{
total: 0,
counterPerMethod: map[string]uint{},
}
}
return stats
}
func getStats() (uint, map[string]uint) {
stats := getInstance()
stats.rw.RLock()
defer stats.rw.RUnlock()
return stats.total, stats.counterPerMethod
}
func resetStats() {
stats := getInstance()
stats.rw.Lock()
defer stats.rw.Unlock()
stats.total = 0
stats.counterPerMethod = map[string]uint{}
}
func CountCall(method string) {
stats := getInstance()
stats.rw.Lock()
defer stats.rw.Unlock()
stats.total++
stats.counterPerMethod[method]++
}