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

clear responsibilities #2125

Merged
merged 2 commits into from
Nov 5, 2024
Merged
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
12 changes: 5 additions & 7 deletions tools/walletextension/cache/RistrettoCache.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ import (
)

const (
numCounters = 1e7 // number of keys to track frequency of (10M).
maxCost = 1_000_000 // 1 million entries
bufferItems = 64 // number of keys per Get buffer.
defaultCost = 1 // default cost of cache.
bufferItems = 64 // number of keys per Get buffer.
defaultCost = 1 // default cost of cache.
)

type ristrettoCache struct {
Expand All @@ -23,10 +21,10 @@ type ristrettoCache struct {
}

// NewRistrettoCacheWithEviction returns a new ristrettoCache.
func NewRistrettoCacheWithEviction(logger log.Logger) (Cache, error) {
func NewRistrettoCacheWithEviction(nrElems int, logger log.Logger) (Cache, error) {
cache, err := ristretto.NewCache(&ristretto.Config{
NumCounters: numCounters,
MaxCost: maxCost,
NumCounters: int64(nrElems * 10),
MaxCost: int64(nrElems),
BufferItems: bufferItems,
Metrics: true,
})
Expand Down
4 changes: 2 additions & 2 deletions tools/walletextension/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ type Cache interface {
Remove(key []byte)
}

func NewCache(logger log.Logger) (Cache, error) {
return NewRistrettoCacheWithEviction(logger)
func NewCache(nrElems int, logger log.Logger) (Cache, error) {
return NewRistrettoCacheWithEviction(nrElems, logger)
}

type Strategy uint8
Expand Down
13 changes: 0 additions & 13 deletions tools/walletextension/common/db_types.go

This file was deleted.

28 changes: 28 additions & 0 deletions tools/walletextension/common/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package common

import (
"github.com/ten-protocol/go-ten/go/common/viewingkey"

"github.com/ethereum/go-ethereum/common"
)

type GWAccount struct {
User *GWUser
Address *common.Address
Signature []byte
SignatureType viewingkey.SignatureType
}

type GWUser struct {
UserID []byte
Accounts map[common.Address]*GWAccount
UserKey []byte
}

func (u GWUser) GetAllAddresses() []*common.Address {
accts := make([]*common.Address, 0)
for _, acc := range u.Accounts {
accts = append(accts, acc.Address)
}
return accts
}
2 changes: 1 addition & 1 deletion tools/walletextension/httpapi/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ func revokeRequestHandler(walletExt *services.Services, conn UserConn) {
}

// delete user and accounts associated with it from the database
err = walletExt.DeleteUser(userID)
err = walletExt.Storage.DeleteUser(userID)
if err != nil {
handleError(conn, walletExt.Logger(), fmt.Errorf("internal error"))
walletExt.Logger().Error("unable to delete user", "userID", userID, log.ErrKey, err)
Expand Down
14 changes: 8 additions & 6 deletions tools/walletextension/rpcapi/blockchain_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"encoding/json"
"fmt"

wecommon "github.com/ten-protocol/go-ten/tools/walletextension/common"

"github.com/ten-protocol/go-ten/tools/walletextension/cache"

"github.com/ten-protocol/go-ten/tools/walletextension/services"
Expand Down Expand Up @@ -185,7 +187,7 @@ func (api *BlockChainAPI) GetStorageAt(ctx context.Context, address gethcommon.A
return nil, err
}

_, err = api.we.GetUser(userID)
_, err = api.we.Storage.GetUser(userID)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -255,10 +257,10 @@ func (api *BlockChainAPI) Call(ctx context.Context, args gethapi.TransactionArgs
return cacheBlockNumberOrHash(blockNrOrHash)
},
},
computeFromCallback: func(user *services.GWUser) *gethcommon.Address {
computeFromCallback: func(user *wecommon.GWUser) *gethcommon.Address {
return searchFromAndData(user.GetAllAddresses(), args)
},
adjustArgs: func(acct *services.GWAccount) []any {
adjustArgs: func(acct *wecommon.GWAccount) []any {
argsClone := populateFrom(acct, args)
return []any{argsClone, blockNrOrHash, overrides, blockOverrides}
},
Expand All @@ -280,10 +282,10 @@ func (api *BlockChainAPI) EstimateGas(ctx context.Context, args gethapi.Transact
return cache.LatestBatch
},
},
computeFromCallback: func(user *services.GWUser) *gethcommon.Address {
computeFromCallback: func(user *wecommon.GWUser) *gethcommon.Address {
return searchFromAndData(user.GetAllAddresses(), args)
},
adjustArgs: func(acct *services.GWAccount) []any {
adjustArgs: func(acct *wecommon.GWAccount) []any {
argsClone := populateFrom(acct, args)
return []any{argsClone, blockNrOrHash, overrides}
},
Expand All @@ -296,7 +298,7 @@ func (api *BlockChainAPI) EstimateGas(ctx context.Context, args gethapi.Transact
return *resp, err
}

func populateFrom(acct *services.GWAccount, args gethapi.TransactionArgs) gethapi.TransactionArgs {
func populateFrom(acct *wecommon.GWAccount, args gethapi.TransactionArgs) gethapi.TransactionArgs {
// clone the args
argsClone := cloneArgs(args)
// set the from
Expand Down
14 changes: 7 additions & 7 deletions tools/walletextension/rpcapi/filter_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (api *FilterAPI) Logs(ctx context.Context, crit common.FilterCriteria) (*rp
errorChannels := make([]<-chan error, 0)
backendSubscriptions := make([]*rpc.ClientSubscription, 0)
for _, address := range candidateAddresses {
rpcWSClient, err := services.ConnectWS(ctx, user.Accounts[*address], api.we.Logger())
rpcWSClient, err := api.we.BackendRPC.ConnectWS(ctx, user.Accounts[*address])
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -152,11 +152,11 @@ func (api *FilterAPI) closeConnections(backendSubscriptions []*rpc.ClientSubscri
backendSub.Unsubscribe()
}
for _, connection := range backendWSConnections {
_ = services.ReturnConn(api.we.RpcWSConnPool, connection.BackingClient(), api.logger)
_ = api.we.BackendRPC.ReturnConnWS(connection.BackingClient())
}
}

func getUserAndNotifier(ctx context.Context, api *FilterAPI) (*rpc.Notifier, *services.GWUser, error) {
func getUserAndNotifier(ctx context.Context, api *FilterAPI) (*rpc.Notifier, *wecommon.GWUser, error) {
subNotifier, supported := rpc.NotifierFromContext(ctx)
if !supported {
return nil, nil, fmt.Errorf("creation of subscriptions is not supported")
Expand All @@ -167,7 +167,7 @@ func getUserAndNotifier(ctx context.Context, api *FilterAPI) (*rpc.Notifier, *se
return nil, nil, fmt.Errorf("illegal access")
}

user, err := api.we.GetUser(subNotifier.UserID)
user, err := api.we.Storage.GetUser(subNotifier.UserID)
if err != nil {
return nil, nil, fmt.Errorf("illegal access: %s, %w", subNotifier.UserID, err)
}
Expand Down Expand Up @@ -208,7 +208,7 @@ func (api *FilterAPI) GetLogs(ctx context.Context, crit common.FilterCriteria) (
}

res, err := cache.WithCache(
api.we.Cache,
api.we.RPCResponsesCache,
&cache.Cfg{
DynamicType: func() cache.Strategy {
if crit.ToBlock != nil && crit.ToBlock.Int64() > 0 {
Expand All @@ -223,7 +223,7 @@ func (api *FilterAPI) GetLogs(ctx context.Context, crit common.FilterCriteria) (
},
generateCacheKey([]any{userID, method, common.SerializableFilterCriteria(crit)}),
func() (*[]*types.Log, error) { // called when there is no entry in the cache
user, err := api.we.GetUser(userID)
user, err := api.we.Storage.GetUser(userID)
if err != nil {
return nil, err
}
Expand All @@ -233,7 +233,7 @@ func (api *FilterAPI) GetLogs(ctx context.Context, crit common.FilterCriteria) (
// execute the get_Logs function
// dedupe and concatenate the results
for _, acct := range user.Accounts {
eventLogs, err := services.WithEncRPCConnection(ctx, api.we, acct, func(rpcClient *tenrpc.EncRPCClient) (*[]*types.Log, error) {
eventLogs, err := services.WithEncRPCConnection(ctx, api.we.BackendRPC, acct, func(rpcClient *tenrpc.EncRPCClient) (*[]*types.Log, error) {
var result []*types.Log

// wrap the context with a timeout to prevent long executions
Expand Down
20 changes: 11 additions & 9 deletions tools/walletextension/rpcapi/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"strings"
"time"

"github.com/ten-protocol/go-ten/tools/walletextension/common"

"github.com/ten-protocol/go-ten/tools/walletextension/cache"

"github.com/ten-protocol/go-ten/tools/walletextension/services"
Expand Down Expand Up @@ -42,11 +44,11 @@ var rpcNotImplemented = fmt.Errorf("rpc endpoint not implemented")
type ExecCfg struct {
// these 4 fields specify the account(s) that should make the backend call
account *gethcommon.Address
computeFromCallback func(user *services.GWUser) *gethcommon.Address
computeFromCallback func(user *common.GWUser) *gethcommon.Address
tryAll bool
tryUntilAuthorised bool

adjustArgs func(acct *services.GWAccount) []any
adjustArgs func(acct *common.GWAccount) []any
cacheCfg *cache.Cfg
timeout time.Duration
}
Expand All @@ -60,8 +62,8 @@ func UnauthenticatedTenRPCCall[R any](ctx context.Context, w *services.Services,
cacheArgs := []any{method}
cacheArgs = append(cacheArgs, args...)

res, err := cache.WithCache(w.Cache, cfg, generateCacheKey(cacheArgs), func() (*R, error) {
return services.WithPlainRPCConnection(ctx, w, func(client *rpc.Client) (*R, error) {
res, err := cache.WithCache(w.RPCResponsesCache, cfg, generateCacheKey(cacheArgs), func() (*R, error) {
return services.WithPlainRPCConnection(ctx, w.BackendRPC, func(client *rpc.Client) (*R, error) {
var resp *R
var err error

Expand Down Expand Up @@ -94,8 +96,8 @@ func ExecAuthRPC[R any](ctx context.Context, w *services.Services, cfg *ExecCfg,
cacheArgs := []any{userID, method}
cacheArgs = append(cacheArgs, args...)

res, err := cache.WithCache(w.Cache, cfg.cacheCfg, generateCacheKey(cacheArgs), func() (*R, error) {
user, err := w.GetUser(userID)
res, err := cache.WithCache(w.RPCResponsesCache, cfg.cacheCfg, generateCacheKey(cacheArgs), func() (*R, error) {
user, err := w.Storage.GetUser(userID)
if err != nil {
return nil, err
}
Expand All @@ -112,7 +114,7 @@ func ExecAuthRPC[R any](ctx context.Context, w *services.Services, cfg *ExecCfg,
var rpcErr error
for i := range candidateAccts {
acct := candidateAccts[i]
result, err := services.WithEncRPCConnection(ctx, w, acct, func(rpcClient *tenrpc.EncRPCClient) (*R, error) {
result, err := services.WithEncRPCConnection(ctx, w.BackendRPC, acct, func(rpcClient *tenrpc.EncRPCClient) (*R, error) {
var result *R
adjustedArgs := args
if cfg.adjustArgs != nil {
Expand Down Expand Up @@ -151,8 +153,8 @@ func ExecAuthRPC[R any](ctx context.Context, w *services.Services, cfg *ExecCfg,
return res, err
}

func getCandidateAccounts(user *services.GWUser, _ *services.Services, cfg *ExecCfg) ([]*services.GWAccount, error) {
candidateAccts := make([]*services.GWAccount, 0)
func getCandidateAccounts(user *common.GWUser, _ *services.Services, cfg *ExecCfg) ([]*common.GWAccount, error) {
candidateAccts := make([]*common.GWAccount, 0)
// for users with multiple accounts try to determine a candidate account based on the available information
switch {
case cfg.account != nil:
Expand Down
Loading