From 60ec8ee77e512076b2511a3a2a77776c1356f651 Mon Sep 17 00:00:00 2001 From: Lorenz Herzberger Date: Fri, 10 May 2024 14:32:07 +0200 Subject: [PATCH] feat: add logging to create account Signed-off-by: Lorenz Herzberger --- cmd/ta/main.go | 1 + config/config.go | 9 ++++++++- service/router.go | 7 +++++++ service/service.go | 9 ++++++--- 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/cmd/ta/main.go b/cmd/ta/main.go index 8d05acf..806ae40 100644 --- a/cmd/ta/main.go +++ b/cmd/ta/main.go @@ -36,6 +36,7 @@ func loadConfig(path string) (cfg *config.Config, err error) { cfg.TestnetMode = v.GetBool("TESTNET_MODE") cfg.DBPath = v.GetString("DB_PATH") cfg.PlanetmintRPCHost = v.GetString("PLANETMINT_RPC_HOST") + cfg.LogLevel = v.GetString("LOG_LEVEL") return } log.Println("no config file found") diff --git a/config/config.go b/config/config.go index 19ee60f..23db6e2 100644 --- a/config/config.go +++ b/config/config.go @@ -1,6 +1,10 @@ package config -import "sync" +import ( + "sync" + + "github.com/rddl-network/go-utils/logger" +) const DefaultConfigTemplate = ` FIRMWARE_ESP32="{{ .FirmwareESP32 }}" @@ -12,6 +16,7 @@ SERVICE_PORT={{ .ServicePort }} TESTNET_MODE={{ .TestnetMode }} DB_PATH="{{ .DBPath }}" PLANETMINT_RPC_HOST="{{ .PlanetmintRPCHost }}" +LOG_LEVEL="{{ .LogLevel }}" ` // Config defines TA's top level configuration @@ -25,6 +30,7 @@ type Config struct { TestnetMode bool `json:"testnet-mode" mapstructure:"testnet-mode"` DBPath string `json:"db-path" mapstructure:"db-path"` PlanetmintRPCHost string `json:"planetmint-rpc-host" mapstructure:"planetmint-rpc-host"` + LogLevel string `json:"log-level" mapstructure:"log-level"` } // global singleton @@ -45,6 +51,7 @@ func DefaultConfig() *Config { TestnetMode: false, DBPath: "data", PlanetmintRPCHost: "127.0.0.1:9090", + LogLevel: logger.DEBUG, } } diff --git a/service/router.go b/service/router.go index db731f6..4fea7db 100644 --- a/service/router.go +++ b/service/router.go @@ -72,6 +72,7 @@ func (s *TAAService) createAccount(c *gin.Context) { var requestBody types.PostCreateAccountRequest if err := c.BindJSON(&requestBody); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + s.logger.Error("msg", err.Error()) return } @@ -84,6 +85,7 @@ func (s *TAAService) createAccount(c *gin.Context) { if errR1 != nil { errStr = errR1.Error() + ", " } + s.logger.Error("msg", errStr+errR1.Error()) c.JSON(http.StatusBadRequest, gin.H{"error": errStr + errK1.Error()}) return } @@ -92,6 +94,7 @@ func (s *TAAService) createAccount(c *gin.Context) { // check if account already in db found, err := HasAccount(s.db, requestBody.PlmntAddress) if err != nil && !errors.Is(err, leveldb.ErrNotFound) { + s.logger.Error("msg", err.Error()) c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to read db"}) return } @@ -104,6 +107,7 @@ func (s *TAAService) createAccount(c *gin.Context) { // verify trust anchor registered taStatus, err := s.pmc.GetTrustAnchorStatus(requestBody.MachineID) if err != nil { + s.logger.Error("msg", "failed to fetch trust anchor status", "machineID", requestBody.MachineID) c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to fetch trust anchor status"}) return } @@ -116,6 +120,7 @@ func (s *TAAService) createAccount(c *gin.Context) { // verify plmnt address and not already funded account, err := s.pmc.GetAccount(requestBody.PlmntAddress) if err != nil { + s.logger.Error("msg", "failed to fetch account", "plmntAddress", requestBody.PlmntAddress) c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to fetch account"}) return } @@ -128,10 +133,12 @@ func (s *TAAService) createAccount(c *gin.Context) { err = s.pmc.FundAccount(requestBody.PlmntAddress) if err != nil { + s.logger.Error("msg", "failed to send funds", requestBody.PlmntAddress) c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to send funds"}) return } + s.logger.Info("msg", "funding successful, storing account", "plmntAddress", requestBody.PlmntAddress, "machineID", requestBody.MachineID) err = StoreAccount(s.db, requestBody) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to store account"}) diff --git a/service/service.go b/service/service.go index fb8bd6a..668a60a 100644 --- a/service/service.go +++ b/service/service.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/gin-gonic/gin" + "github.com/rddl-network/go-utils/logger" "github.com/rddl-network/ta_attest/config" "github.com/syndtr/goleveldb/leveldb" ) @@ -13,6 +14,7 @@ type TAAService struct { router *gin.Engine db *leveldb.DB pmc IPlanetmintClient + logger logger.AppLogger firmwareESP32 []byte firmwareESP32C3 []byte } @@ -20,9 +22,10 @@ type TAAService struct { func NewTrustAnchorAttestationService(cfg *config.Config, db *leveldb.DB, pmc IPlanetmintClient) *TAAService { libConfig.SetChainID(cfg.PlanetmintChainID) service := &TAAService{ - db: db, - cfg: cfg, - pmc: pmc, + db: db, + cfg: cfg, + pmc: pmc, + logger: logger.GetLogger(cfg.LogLevel), } gin.SetMode(gin.ReleaseMode)