Skip to content

Commit

Permalink
Merge pull request #603 from singnet/new-err-codes
Browse files Browse the repository at this point in the history
better logs, add custom errs for dev-portal
  • Loading branch information
semyon-dev authored Nov 14, 2024
2 parents 7369e03 + 8cb5c1d commit c045ac9
Show file tree
Hide file tree
Showing 13 changed files with 177 additions and 94 deletions.
37 changes: 14 additions & 23 deletions blockchain/serviceMetadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/singnet/snet-daemon/v5/errs"
"math/big"
"os"
"slices"
"strings"

"github.com/bufbuild/protocompile"
Expand Down Expand Up @@ -253,15 +255,15 @@ func ServiceMetaData() *ServiceMetadata {
var metadata *ServiceMetadata
var err error
if config.GetBool(config.BlockchainEnabledKey) {
ipfsHash := string(getServiceMetaDataUrifromRegistry())
ipfsHash := string(getServiceMetaDataURIfromRegistry())
metadata, err = GetServiceMetaDataFromIPFS(ipfsHash)
if err != nil {
zap.L().Panic("error on determining service metadata from file", zap.Error(err))
zap.L().Panic("error on determining service metadata from file"+errs.ErrDescURL(errs.InvalidMetadata), zap.Error(err))
}
} else {
metadata = &ServiceMetadata{Encoding: "proto", ServiceType: "grpc"}
}
zap.L().Debug("service_type: " + metadata.GetServiceType())
zap.L().Debug("service type: " + metadata.GetServiceType())
return metadata
}

Expand Down Expand Up @@ -300,7 +302,7 @@ func GetRegistryFilterer(ethWsClient *ethclient.Client) *RegistryFilterer {
return reg
}

func getServiceMetaDataUrifromRegistry() []byte {
func getServiceMetaDataURIfromRegistry() []byte {
reg := getRegistryCaller()

orgId := StringToBytes32(config.GetString(config.OrganizationId))
Expand Down Expand Up @@ -408,7 +410,7 @@ func setFreeCallData(metaData *ServiceMetadata) error {
metaData.freeCallsAllowed = metaData.defaultGroup.FreeCalls
//If the signer address is not a valid address, then return back an error
if !common.IsHexAddress(metaData.defaultGroup.FreeCallSigner) {
return fmt.Errorf("MetaData does not have 'free_call_signer_address defined correctly")
return fmt.Errorf("MetaData does not have 'free_call_signer_address defined correctly" + errs.ErrDescURL(errs.InvalidMetadata))
}
metaData.freeCallSignerAddress = common.HexToAddress(ToChecksumAddress(metaData.defaultGroup.FreeCallSigner))
}
Expand Down Expand Up @@ -453,10 +455,10 @@ func (metaData *ServiceMetadata) GetLicenses() Licenses {

// methodFullName , ex "/example_service.Calculator/add"
func (metaData *ServiceMetadata) GetDynamicPricingMethodAssociated(methodFullName string) (pricingMethod string, isDynamicPricingEligible bool) {
//Check if Method Level Options are defined , for the given Service and method,
//If Defined check if its in the format supported , then return the full method Name
// Check if Method Level Options are defined, for the given Service and method,
// If Defined check if it's in the format supported, then return the full method Name
// i.e /package.service/method format , this will be directly fed in to the grpc called to made to
//determine dynamic pricing
// determine dynamic pricing
if !config.GetBool(config.EnableDynamicPricing) {
return
}
Expand All @@ -469,23 +471,12 @@ func (metaData *ServiceMetadata) GetDynamicPricingMethodAssociated(methodFullNam
return
}

// methodFullName , ex "/example_service.Calculator/add"
// IsModelTraining methodFullName , ex "/example_service.Calculator/add"
func (metaData *ServiceMetadata) IsModelTraining(methodFullName string) (useModelTrainingEndPoint bool) {

if !config.GetBool(config.ModelTrainingEnabled) {
return false
}
useModelTrainingEndPoint = isElementInArray(methodFullName, metaData.TrainingMethods)
return
}

func isElementInArray(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
return slices.Contains(metaData.TrainingMethods, methodFullName)
}

func setServiceProto(metaData *ServiceMetadata) (err error) {
Expand Down Expand Up @@ -594,8 +585,8 @@ func getFileDescriptor(protoContent string) protoreflect.FileDescriptor {
SourceInfoMode: protocompile.SourceInfoStandard,
}
fds, err := compiler.Compile(context.Background(), serviceProto)
if err != nil {
zap.L().Error(err.Error())
if err != nil || fds == nil {
zap.L().Fatal("failed to analyze protofile"+errs.ErrDescURL(errs.InvalidProto), zap.Error(err))
}
return fds.FindFileByPath(serviceProto)
}
11 changes: 6 additions & 5 deletions blockchain/serviceMetadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package blockchain
import (
"fmt"
"math/big"
"slices"
"strings"
"testing"

Expand Down Expand Up @@ -58,6 +59,7 @@ func TestTiers(t *testing.T) {
assert.Equal(t, metaData.GetLicenses().Tiers[0].Range[0].DiscountInPercentage,
1.0)
}

func TestInitServiceMetaDataFromJson(t *testing.T) {
//Parse Bad JSON
_, err := InitServiceMetaDataFromJson([]byte(strings.Replace(testJsonData, "{", "", 1)))
Expand All @@ -68,7 +70,7 @@ func TestInitServiceMetaDataFromJson(t *testing.T) {
//Parse Bad JSON
_, err = InitServiceMetaDataFromJson([]byte(strings.Replace(testJsonData, "0x7DF35C98f41F3Af0df1dc4c7F7D4C19a71Dd059F", "", 1)))
if err != nil {
assert.Equal(t, err.Error(), "MetaData does not have 'free_call_signer_address defined correctly")
assert.Contains(t, err.Error(), "MetaData does not have 'free_call_signer_address defined correctly")
}
_, err = InitServiceMetaDataFromJson([]byte(strings.Replace(testJsonData, "default_pricing", "dummy", 1)))
if err != nil {
Expand All @@ -84,11 +86,10 @@ func TestReadServiceMetaDataFromLocalFile(t *testing.T) {
}

func Test_getServiceMetaDataUrifromRegistry(t *testing.T) {
assert.Panics(t, func() { getServiceMetaDataUrifromRegistry() })
assert.Panics(t, func() { getServiceMetaDataURIfromRegistry() })
config.Vip().Set(config.BlockChainNetworkSelected, "sepolia")
config.Validate()
assert.Panics(t, func() { getServiceMetaDataUrifromRegistry() })

assert.Panics(t, func() { getServiceMetaDataURIfromRegistry() })
}

func Test_setDefaultPricing(t *testing.T) {
Expand All @@ -112,7 +113,7 @@ func TestServiceMetadata_parseServiceProto(t *testing.T) {
assert.NotNil(t, priceMethodMap)
assert.NotNil(t, trainingMethods)
dynamicPriceMethod, ok := priceMethodMap["/example_service.Calculator/add"]
isTrainingMethod := isElementInArray("/example_service.Calculator/train_add", trainingMethods)
isTrainingMethod := slices.Contains(trainingMethods, "/example_service.Calculator/train_add")
assert.Equal(t, dynamicPriceMethod, "/example_service.Calculator/dynamic_pricing_add")
assert.True(t, ok, "true")
assert.True(t, isTrainingMethod)
Expand Down
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ func Validate() error {
switch dType := vip.GetString(DaemonTypeKey); dType {
case "grpc":
case "http":
zap.L().Warn("daemon type http is not for production mode, be careful")
default:
return fmt.Errorf("unrecognized DAEMON_TYPE '%+v'", dType)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

func (l *ContractEventListener) ListenOrganizationMetadataChanging() {
zap.L().Info("Starting contract event listener for organization metadata changing")
zap.L().Debug("Starting contract event listener for organization metadata changing")

watchOpts := &bind.WatchOpts{
Start: nil,
Expand All @@ -29,7 +29,7 @@ func (l *ContractEventListener) ListenOrganizationMetadataChanging() {
sub, err := registryFilterer.WatchOrganizationModified(watchOpts, eventContractChannel, orgIdFilter)

if err != nil {
zap.L().Fatal("Failed to subscribe to logs", zap.Error(err))
zap.L().Error("Failed to subscribe to logs", zap.Error(err))
}

for {
Expand Down
22 changes: 22 additions & 0 deletions errs/errs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package errs

import (
"fmt"
)

const devPortalURL = "https://dev.singularitynet.io/docs/products/DecentralizedAIPlatform/Daemon/error-codes/#_"

const (
_ = iota
ServiceUnavailable
InvalidMetadata
InvalidProto
HTTPRequestBuildError
InvalidServiceCredentials
InvalidConfig
ReceiveMsgError
)

func ErrDescURL(code int) string {
return fmt.Sprintf("\nAbout error & possible fixes: %s%d", devPortalURL, code)
}
1 change: 1 addition & 0 deletions escrow/free_call_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func serializeFreeCallKey(key any) (serialized string, err error) {
myKey := key.(*FreeCallUserKey)
return myKey.String(), nil
}

func (storage *FreeCallUserStorage) Get(key *FreeCallUserKey) (state *FreeCallUserData, ok bool, err error) {
value, ok, err := storage.delegate.Get(key)
if err != nil || !ok {
Expand Down
Loading

0 comments on commit c045ac9

Please sign in to comment.