From 429e7dd3f54508d5d15aeb5aea98eb8a4e368eb9 Mon Sep 17 00:00:00 2001
From: "Gomez Goiri, Aitor" <aitor.gomez@tecnalia.com>
Date: Wed, 2 Mar 2022 16:21:23 +0100
Subject: [PATCH] Adding some debug messages and restricting access to
 functions

---
 chaincode/controller/stats/stub.go | 32 ++++++++++++++++++++++++++++--
 1 file changed, 30 insertions(+), 2 deletions(-)

diff --git a/chaincode/controller/stats/stub.go b/chaincode/controller/stats/stub.go
index 35dc18f..e20bcb7 100644
--- a/chaincode/controller/stats/stub.go
+++ b/chaincode/controller/stats/stub.go
@@ -8,8 +8,10 @@ package stats
 
 import (
 	"encoding/json"
+	"fmt"
 
 	"git.code.tecnalia.com/blockchain/hypercog/model"
+	"git.code.tecnalia.com/ledgerbuilder/sdk/core/util/logging"
 	"git.code.tecnalia.com/ledgerbuilder/sdk/shared"
 )
 
@@ -18,6 +20,7 @@ const ORG_LIST_KEY = "org_list"
 
 type OrgList []string
 
+var log = logging.NewGoLogger("stats")
 
 func getStats(stub shared.LedgerBuildrStubInterface, key string) (st *model.RawStats, err error) {
 	value, err := stub.GetState(key)
@@ -61,6 +64,26 @@ func getOrgList(stub shared.LedgerBuildrStubInterface) (st *OrgList, err error)
 	return st, nil
 }
 
+func resetStats(stub shared.LedgerBuildrStubInterface) (error) {
+	err := stub.DelState(GLOBAL_KEY)
+	if err != nil {
+		return err
+	}
+
+	orgs, err := getOrgList(stub)
+	if len(*orgs) > 0 {
+		for _, org := range *orgs {
+			err = stub.DelState(org)
+			if err != nil {
+				return err
+			}
+		}
+	}
+
+	err = stub.DelState(ORG_LIST_KEY)
+	return err
+}
+
 func (orgs OrgList) contains(org string) (bool) {
 	for _, o := range orgs {
         if o == org {
@@ -70,7 +93,7 @@ func (orgs OrgList) contains(org string) (bool) {
 	return false
 }
 
-func AddOrgIfNotExist(stub shared.LedgerBuildrStubInterface, org string) (err error) {
+func addOrgIfNotExist(stub shared.LedgerBuildrStubInterface, org string) (err error) {
 	orgs, err := getOrgList(stub)
 	if err != nil {
 		return err
@@ -88,6 +111,8 @@ func AddOrgIfNotExist(stub shared.LedgerBuildrStubInterface, org string) (err er
 		if err != nil {
 			return err
 		}
+
+		log.Debug("Added stat for new org")
 	}
 
 	return nil
@@ -129,11 +154,12 @@ func upgradeStats(stub shared.LedgerBuildrStubInterface, globalStats *model.RawS
 		return err
 	}
 
-	err = AddOrgIfNotExist(stub, org)
+	err = addOrgIfNotExist(stub, org)
 	if err != nil {
 		return err
 	}
 
+	log.Debug("Upgrading stats")
 	return nil
 }
 
@@ -164,6 +190,8 @@ func RegisterSale(stub shared.LedgerBuildrStubInterface, quantity uint32, price
 	orgStats.UpgradeSell(quantity, price, units)
 	orgStats.UpgradeSell(quantity, price, units)
 
+	log.Debug(fmt.Sprintf("Sale registered in stats (quantity: %s, price: %s, units: %s)", quantity, price, units))
+
 	err = upgradeStats(stub, globalStats, orgStats)
 	if err != nil {
 		return err
-- 
GitLab