Skip to content

Commit

Permalink
add basic file logging in the gateway
Browse files Browse the repository at this point in the history
  • Loading branch information
zkokelj committed Jan 29, 2024
1 parent 26d7db4 commit 04d5cef
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
18 changes: 18 additions & 0 deletions tools/walletextension/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
"os"

"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/crypto/ecies"
Expand Down Expand Up @@ -78,3 +79,20 @@ func (r *RPCRequest) Clone() *RPCRequest {
Params: r.Params,
}
}

// NewFileLogger is a logger factory function
func NewFileLogger() gethlog.Logger {
// Open or create your log file
file, err := os.OpenFile("gateway_logs.log", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0o644)
if err != nil {
panic(err)
}

// Create a new logger instance
logger := gethlog.New()

// Set the handler to the file
logger.SetHandler(gethlog.StreamHandler(file, gethlog.TerminalFormat(false)))

return logger
}
7 changes: 7 additions & 0 deletions tools/walletextension/wallet_extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type WalletExtension struct {
unsignedVKs map[gethcommon.Address]*viewingkey.ViewingKey // Map temporarily holding VKs that have been generated but not yet signed
storage storage.Storage
logger gethlog.Logger
fileLogger gethlog.Logger
stopControl *stopcontrol.StopControl
version string
config *config.Config
Expand All @@ -59,13 +60,15 @@ func New(
panic(err)
}
newTenClient := obsclient.NewObsClient(rpcClient)
newFileLogger := common.NewFileLogger()
return &WalletExtension{
hostAddrHTTP: hostAddrHTTP,
hostAddrWS: hostAddrWS,
userAccountManager: userAccountManager,
unsignedVKs: map[gethcommon.Address]*viewingkey.ViewingKey{},
storage: storage,
logger: logger,
fileLogger: newFileLogger,
stopControl: stopControl,
version: version,
config: config,
Expand All @@ -92,11 +95,13 @@ func (w *WalletExtension) ProxyEthRequest(request *common.RPCRequest, conn userc

// proxyRequest will find the correct client to proxy the request (or try them all if appropriate)
var rpcResp interface{}
w.fileLogger.Info(fmt.Sprintf("Request method: %s, request params: %s, encryptionToken of sender: %s", request.Method, request.Params, hexUserID))

// wallet extension can override the GetStorageAt to retrieve the current userID
if request.Method == rpc.GetStorageAt {
if interceptedResponse := w.getStorageAtInterceptor(request, hexUserID); interceptedResponse != nil {
w.logger.Info("interception successful for getStorageAt, returning userID response")
w.fileLogger.Info(fmt.Sprintf("Request method: %s, request params: %s, encryptionToken of sender: %s, response: %s", request.Method, request.Params, hexUserID, interceptedResponse))
return interceptedResponse, nil
}
}
Expand All @@ -113,6 +118,7 @@ func (w *WalletExtension) ProxyEthRequest(request *common.RPCRequest, conn userc
if errors.Is(err, rpc.ErrNilResponse) {
// if err was for a nil response then we will return an RPC result of null to the caller (this is a valid "not-found" response for some methods)
response[common.JSONKeyResult] = nil
w.fileLogger.Info(fmt.Sprintf("Request method: %s, request params: %s, encryptionToken of sender: %s, response: %s", request.Method, request.Params, hexUserID, response))
return response, nil
}
return nil, err
Expand All @@ -123,6 +129,7 @@ func (w *WalletExtension) ProxyEthRequest(request *common.RPCRequest, conn userc
// todo (@ziga) - fix this upstream on the decode
// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-658.md
adjustStateRoot(rpcResp, response)
w.fileLogger.Info(fmt.Sprintf("Request method: %s, request params: %s, encryptionToken of sender: %s, response: %s", request.Method, request.Params, hexUserID, response))

return response, nil
}
Expand Down

0 comments on commit 04d5cef

Please sign in to comment.