Skip to content
Snippets Groups Projects
Select Git revision
  • b6e361bddf920be06e39ba8ba45899fa059bb83f
  • main default
2 results

stub.go

Blame
  • stub.go 2.50 KiB
    /**
     * stub.go
     *
     * COPYRIGHT: FUNDACIÓN TECNALIA RESEARCH & INNOVATION, 2022.
     */
    
    package stats
    
    import (
    	"encoding/json"
    
    	"git.code.tecnalia.com/blockchain/hypercog/model"
    	"git.code.tecnalia.com/ledgerbuilder/sdk/shared"
    )
    
    const GLOBAL_KEY = "global"
    const ORG_LIST_KEY = "org_list"
    
    type OrgList []string
    
    
    func getStats(stub shared.LedgerBuildrStubInterface, key string) (st *model.RawStats, err error) {
    	value, err := stub.GetState(key)
    	if err != nil || len(value) == 0 {
    		st = new(model.RawStats)
    	} else {
    		err = json.Unmarshal(value, &st)
    		if err != nil  {
    			return nil, err
    		}
    	}
    
    	return st, nil
    }
    
    func putStats(stub shared.LedgerBuildrStubInterface, key string, stats *model.RawStats) (error) {
    	serialized, err := json.Marshal(stats)
    	if err != nil {
    		return err
    	}
    
    	err = stub.PutState(key, serialized)
    	if err != nil {
    		return err
    	}
    
    	return nil
    }
    
    func getOrgList(stub shared.LedgerBuildrStubInterface) (st *OrgList, err error) {
    	value, err := stub.GetState(ORG_LIST_KEY)
    	if err != nil || len(value) == 0 {
    		st = new(OrgList)
    	} else {
    		err = json.Unmarshal(value, &st)
    		if err != nil  {
    			return nil, err
    		}
    	}
    
    	return st, nil
    }
    
    func (orgs OrgList) contains(org string) (bool) {
    	for _, o := range orgs {
            if o == org {
                return true
            }
        }
    	return false
    }
    
    func AddOrgIfNotExist(stub shared.LedgerBuildrStubInterface, org string) (err error) {
    	orgs, err := getOrgList(stub)
    	if err != nil {
    		return err
    	}
    
    	if !orgs.contains(org) {
    		newOrgs := append(*orgs, org)
    
    		serialized, err := json.Marshal(newOrgs)
    		if err != nil {
    			return err
    		}
    	
    		err = stub.PutState(ORG_LIST_KEY, serialized)
    		if err != nil {
    			return err
    		}
    	}
    
    	return nil
    }
    
    
    func RetrieveExistingStats(stub shared.LedgerBuildrStubInterface) (*model.RawStats, *model.RawStats, error) {
    	org, err := stub.GetOrganization()
    	if err != nil {
    		return nil, nil, err
    	}
    
    	customStats, err := getStats(stub, org)
    	if err != nil {
    		return nil, nil, err
    	}
    
    	globalStats, err := getStats(stub , GLOBAL_KEY)
    	if err != nil {
    		return nil, nil, err
    	}
    
    	return globalStats, customStats, nil
    }
    
    func UpgradeStats(stub shared.LedgerBuildrStubInterface, globalStats *model.RawStats, customStats *model.RawStats) (error) {
    	org, err := stub.GetOrganization()
    	if err != nil {
    		return err
    	}
    
    	err = putStats(stub, org, customStats)
    	if err != nil {
    		return err
    	}
    
    	err = putStats(stub, GLOBAL_KEY, globalStats)
    	if err != nil {
    		return err
    	}
    
    	err = AddOrgIfNotExist(stub, org)
    	if err != nil {
    		return err
    	}
    
    	return nil
    }