Skip to content

Commit

Permalink
feat: add logging to create account
Browse files Browse the repository at this point in the history
Signed-off-by: Lorenz Herzberger <lorenzherzberger@gmail.com>
  • Loading branch information
LaurentMontBlanc committed May 10, 2024
1 parent 8de7923 commit 60ec8ee
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
1 change: 1 addition & 0 deletions cmd/ta/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
9 changes: 8 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package config

import "sync"
import (
"sync"

"github.com/rddl-network/go-utils/logger"
)

const DefaultConfigTemplate = `
FIRMWARE_ESP32="{{ .FirmwareESP32 }}"
Expand All @@ -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
Expand All @@ -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
Expand All @@ -45,6 +51,7 @@ func DefaultConfig() *Config {
TestnetMode: false,
DBPath: "data",
PlanetmintRPCHost: "127.0.0.1:9090",
LogLevel: logger.DEBUG,
}
}

Expand Down
7 changes: 7 additions & 0 deletions service/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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"})
Expand Down
9 changes: 6 additions & 3 deletions service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -13,16 +14,18 @@ type TAAService struct {
router *gin.Engine
db *leveldb.DB
pmc IPlanetmintClient
logger logger.AppLogger
firmwareESP32 []byte
firmwareESP32C3 []byte
}

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)
Expand Down

0 comments on commit 60ec8ee

Please sign in to comment.