Skip to content

Commit

Permalink
Merge branch 'dev' of https://github.com/singnet/snet-daemon into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
semyon-dev committed Oct 15, 2024
2 parents f58df24 + 0f3db1a commit 38eed6b
Show file tree
Hide file tree
Showing 19 changed files with 554 additions and 204 deletions.
14 changes: 8 additions & 6 deletions blockchain/orginzationMetadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,16 @@ type PaymentChannelStorageClient struct {
Endpoints []string `json:"endpoints"`
}

// Construct the Organization metadata from the JSON Passed
func InitOrganizationMetaDataFromJson(jsonData string) (metaData *OrganizationMetaData, err error) {
// InitOrganizationMetaDataFromJson Construct the Organization metadata from the JSON Passed
func InitOrganizationMetaDataFromJson(jsonData []byte) (metaData *OrganizationMetaData, err error) {
metaData = new(OrganizationMetaData)
err = json.Unmarshal([]byte(jsonData), &metaData)
err = json.Unmarshal(jsonData, &metaData)
if err != nil {
zap.L().Error("Error in unmarshalling metadata json", zap.Error(err), zap.Any("jsondata", jsonData))
return nil, err
}

// Check for mandatory validations

if err = setDerivedAttributes(metaData); err != nil {
zap.L().Error("Error in setting derived attributes", zap.Error(err))
return nil, err
Expand Down Expand Up @@ -166,7 +165,7 @@ func GetOrganizationMetaData() *OrganizationMetaData {
var err error
if config.GetBool(config.BlockchainEnabledKey) {
ipfsHash := string(getMetaDataURI())
metadata, err = GetOrganizationMetaDataFromIPFS(FormatHash(ipfsHash))
metadata, err = GetOrganizationMetaDataFromIPFS(ipfsHash)
} else {
metadata = &OrganizationMetaData{daemonGroup: &Group{}}
}
Expand All @@ -177,7 +176,10 @@ func GetOrganizationMetaData() *OrganizationMetaData {
}

func GetOrganizationMetaDataFromIPFS(hash string) (*OrganizationMetaData, error) {
jsondata := ipfsutils.GetIpfsFile(hash)
jsondata, err := ipfsutils.ReadFile(hash)
if err != nil {
return nil, err
}
return InitOrganizationMetaDataFromJson(jsondata)
}

Expand Down
53 changes: 35 additions & 18 deletions blockchain/serviceMetadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,18 +143,18 @@ import (
}
*/
const (
IpfsPrefix = "ipfs://"
serviceProto = "service.proto"
)

type ServiceMetadata struct {
Version int `json:"version"`
DisplayName string `json:"display_name"`
Encoding string `json:"encoding"`
ServiceType string `json:"service_type"`
Groups []OrganizationGroup `json:"groups"`
ModelIpfsHash string `json:"model_ipfs_hash"`
MpeAddress string `json:"mpe_address"`
Version int `json:"version"`
DisplayName string `json:"display_name"`
Encoding string `json:"encoding"`
ServiceType string `json:"service_type"`
Groups []OrganizationGroup `json:"groups"`
ModelIpfsHash string `json:"model_ipfs_hash"`
ServiceApiSource string `json:"service_api_source"`
MpeAddress string `json:"mpe_address"`

multiPartyEscrowAddress common.Address
defaultPricing Pricing
Expand Down Expand Up @@ -196,6 +196,7 @@ type Subscriptions struct {
IsActive string `json:"isActive"`
Subscription []Subscription `json:"subscription"`
}

type Tier struct {
Type string `json:"type"`
PlanName string `json:"planName"`
Expand All @@ -205,6 +206,7 @@ type Tier struct {
DetailsURL string `json:"detailsUrl"`
IsActive string `json:"isActive"`
}

type Licenses struct {
Subscriptions Subscriptions `json:"subscriptions,omitempty"`
Tiers []Tier `json:"tiers"`
Expand Down Expand Up @@ -252,7 +254,7 @@ func ServiceMetaData() *ServiceMetadata {
var err error
if config.GetBool(config.BlockchainEnabledKey) {
ipfsHash := string(getServiceMetaDataUrifromRegistry())
metadata, err = GetServiceMetaDataFromIPFS(FormatHash(ipfsHash))
metadata, err = GetServiceMetaDataFromIPFS(ipfsHash)
if err != nil {
zap.L().Panic("error on determining service metadata from file", zap.Error(err))
}
Expand All @@ -268,9 +270,7 @@ func ReadServiceMetaDataFromLocalFile(filename string) (*ServiceMetadata, error)
if err != nil {
return nil, errors.Wrapf(err, "could not read file: %v", filename)
}
strJson := string(file)
metadata, err := InitServiceMetaDataFromJson(strJson)

metadata, err := InitServiceMetaDataFromJson(file)
if err != nil {
return nil, fmt.Errorf("error reading local file service_metadata.json ")
}
Expand Down Expand Up @@ -317,13 +317,16 @@ func getServiceMetaDataUrifromRegistry() []byte {
}

func GetServiceMetaDataFromIPFS(hash string) (*ServiceMetadata, error) {
jsondata := ipfsutils.GetIpfsFile(hash)
jsondata, err := ipfsutils.ReadFile(hash)
if err != nil {
return nil, err
}
return InitServiceMetaDataFromJson(jsondata)
}

func InitServiceMetaDataFromJson(jsonData string) (*ServiceMetadata, error) {
func InitServiceMetaDataFromJson(jsonData []byte) (*ServiceMetadata, error) {
metaData := new(ServiceMetadata)
err := json.Unmarshal([]byte(jsonData), &metaData)
err := json.Unmarshal(jsonData, &metaData)
if err != nil {
zap.L().Error(err.Error(), zap.Any("jsondata", jsonData))
return nil, err
Expand All @@ -350,7 +353,7 @@ func InitServiceMetaDataFromJson(jsonData string) (*ServiceMetadata, error) {
zap.L().Error(err.Error())
}

zap.L().Debug("Traning method", zap.String("json", string(trainingMethodsJson)))
zap.L().Debug("Training method", zap.String("json", string(trainingMethodsJson)))

return metaData, err
}
Expand Down Expand Up @@ -488,8 +491,22 @@ func isElementInArray(a string, list []string) bool {
func setServiceProto(metaData *ServiceMetadata) (err error) {
metaData.DynamicPriceMethodMapping = make(map[string]string, 0)
metaData.TrainingMethods = make([]string, 0)
//This is to handler the scenario where there could be multiple protos associated with the service proto
protoFiles, err := ipfsutils.ReadFilesCompressed(ipfsutils.GetIpfsFile(metaData.ModelIpfsHash))
var rawFile []byte

// for backwards compatibility
if metaData.ModelIpfsHash != "" {
rawFile, err = ipfsutils.GetIpfsFile(metaData.ServiceApiSource)
}

if metaData.ServiceApiSource != "" {
rawFile, err = ipfsutils.ReadFile(metaData.ServiceApiSource)
}

if err != nil {
zap.L().Error("Error in retrieving file from filecoin/ipfs", zap.Error(err))
}

protoFiles, err := ipfsutils.ReadFilesCompressed(rawFile)
if err != nil {
return err
}
Expand Down
16 changes: 0 additions & 16 deletions blockchain/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ package blockchain
import (
"encoding/base64"
"fmt"
"regexp"
"strings"

"github.com/ethereum/go-ethereum/common"
"go.uber.org/zap"
)
Expand Down Expand Up @@ -52,11 +49,6 @@ func StringToBytes32(str string) [32]byte {
return byte32
}

func RemoveSpecialCharactersfromHash(pString string) string {
reg := regexp.MustCompile("[^a-zA-Z0-9=]")
return reg.ReplaceAllString(pString, "")
}

func ConvertBase64Encoding(str string) ([32]byte, error) {
var byte32 [32]byte
data, err := base64.StdEncoding.DecodeString(str)
Expand All @@ -68,14 +60,6 @@ func ConvertBase64Encoding(str string) ([32]byte, error) {
return byte32, nil
}

func FormatHash(ipfsHash string) string {
zap.L().Debug("Before Formatting", zap.String("metadataHash", ipfsHash))
ipfsHash = strings.Replace(ipfsHash, IpfsPrefix, "", -1)
ipfsHash = RemoveSpecialCharactersfromHash(ipfsHash)
zap.L().Debug("After Formatting", zap.String("metadataUri", ipfsHash))
return ipfsHash
}

func ToChecksumAddress(hexAddress string) string {
address := common.HexToAddress(hexAddress)
mixedAddress := common.NewMixedcaseAddress(address)
Expand Down
36 changes: 0 additions & 36 deletions blockchain/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,49 +11,13 @@ func TestBytesToBase64(t *testing.T) {
assert.Equal(t, "AQL+/w==", base64)
}

func TestFormatHash(t *testing.T) {
s2 := []byte("ipfs://Here is a string....+=")
hash := FormatHash(string(s2))
assert.Equal(t, hash, "Hereisastring=")
s2 = []byte("QmaGnQ3iVZPuPwdam2rEeQcCSoCYRpxjnZhQ6Z2oeeRSrp")

b4 := append(s2, make([]byte, 3)...)
assert.NotEqual(t, "QmaGnQ3iVZPuPwdam2rEeQcCSoCYRpxjnZhQ6Z2oeeRSrp", string(b4))
assert.Equal(t, "QmaGnQ3iVZPuPwdam2rEeQcCSoCYRpxjnZhQ6Z2oeeRSrp", FormatHash(string(b4)))
}
func TestConvertBase64Encoding(t *testing.T) {

if _, err := ConvertBase64Encoding("n@@###zNEetD1kzU3PZqR4nHPS8erDkrUK0hN4iCBQ4vH5U"); err != nil {
assert.Equal(t, err.Error(), "illegal base64 data at input byte 1")
}

}

func TestToChecksumAddress(t *testing.T) {

assert.Equal(t, "0xE9D09A6C296ACDd4C01b21F407aC93FDfC63e78c", ToChecksumAddress("0xe9d09A6C296aCdd4c01b21f407ac93fdfC63E78C"))

assert.Equal(t, "0xE9D09A6C296ACDd4C01b21F407aC93FDfC63e78c", ToChecksumAddress("0xe9d09A6C296aCdd4c01b21f407ac93fdfC63E78C"))
}

func TestRemoveSpecialCharactersfromHash(t *testing.T) {
testCases := []struct {
input string
expectedOutput string
}{
{"abc123", "abc123"},
{"abc123!@#", "abc123"},
{"a1b2c3 ~`!@#$%^&*()_+-={}[]|\\:;\"'<>,.?/", "a1b2c3="},
{"abc=123", "abc=123"},
{"a1!b2@c3#=4", "a1b2c3=4"},
}

for _, tc := range testCases {
t.Run(tc.input, func(t *testing.T) {
output := RemoveSpecialCharactersfromHash(tc.input)
if output != tc.expectedOutput {
t.Errorf("RemoveSpecialCharactersfromHash(%q) = %q; want %q", tc.input, output, tc.expectedOutput)
}
})
}
}
7 changes: 4 additions & 3 deletions escrow/payment_channel_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,19 +163,20 @@ func (suite *BlockchainChannelReaderSuite) TestGetChannelState() {
assert.Equal(suite.T(), suite.channel(), channel)
}

func (suite *BlockchainChannelReaderSuite) TestGetChannelStateIncorrectRecipeintAddress() {
func (suite *BlockchainChannelReaderSuite) TestGetChannelStateIncorrectRecipientAddress() {
reader := suite.reader

reader.recipientPaymentAddress = func() common.Address { return crypto.PubkeyToAddress(GenerateTestPrivateKey().PublicKey) }
channel, ok, err := reader.GetChannelStateFromBlockchain(suite.channelKey())
assert.Equal(suite.T(), errors.New("recipient Address from service metadata does not Match on what was retrieved from Channel"), err)
assert.Equal(suite.T(), errors.New("recipient Address from org metadata does not Match on what was retrieved from Channel"), err)
assert.False(suite.T(), ok)
assert.Nil(suite.T(), channel)
}

func (suite *PaymentChannelStorageSuite) TestNewPaymentChannelStorage() {
mpeStorage := storage.NewPrefixedAtomicStorage(storage.NewPrefixedAtomicStorage(suite.memoryStorage, "path1"), "path2")
mpeStorage.Put("key1", "value1")
err := mpeStorage.Put("key1", "value1")
assert.Nil(suite.T(), err)
value, _, _ := mpeStorage.Get("key1")
assert.Equal(suite.T(), value, "value1")
values, err := suite.memoryStorage.GetByKeyPrefix("path1")
Expand Down
3 changes: 2 additions & 1 deletion etcddb/etcddb_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ func NewEtcdClientFromVip(vip *viper.Viper, metaData *blockchain.OrganizationMet
var etcdv3 *clientv3.Client

if utils.CheckIfHttps(metaData.GetPaymentStorageEndPoints()) {
tlsConfig, err := getTlsConfig()
var tlsConfig *tls.Config
tlsConfig, err = getTlsConfig()
if err != nil {
return nil, err
}
Expand Down
22 changes: 11 additions & 11 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/OneOfOne/go-utils v0.0.0-20180319162427-6019ff89a94e
github.com/bufbuild/protocompile v0.14.1
github.com/emicklei/proto v1.13.2
github.com/ethereum/go-ethereum v1.14.9
github.com/ethereum/go-ethereum v1.14.11
github.com/golang-collections/collections v0.0.0-20130729185459-604e922904d3
github.com/golang-jwt/jwt/v5 v5.2.1
github.com/gorilla/handlers v1.5.2
Expand All @@ -30,7 +30,7 @@ require (
go.etcd.io/etcd/client/v3 v3.5.16
go.etcd.io/etcd/server/v3 v3.5.16
go.uber.org/zap v1.27.0
golang.org/x/crypto v0.27.0
golang.org/x/crypto v0.28.0
golang.org/x/net v0.29.0
golang.org/x/time v0.6.0
google.golang.org/grpc v1.66.2
Expand All @@ -45,7 +45,7 @@ require (
github.com/VictoriaMetrics/fastcache v1.12.2 // indirect
github.com/alecthomas/units v0.0.0-20240626203959-61d1e3462e30 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bits-and-blooms/bitset v1.13.0 // indirect
github.com/bits-and-blooms/bitset v1.14.3 // indirect
github.com/blang/semver/v4 v4.0.0 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.3.4 // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
Expand All @@ -56,20 +56,20 @@ require (
github.com/cockroachdb/pebble v1.1.2 // indirect
github.com/cockroachdb/redact v1.1.5 // indirect
github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06 // indirect
github.com/consensys/bavard v0.1.13 // indirect
github.com/consensys/gnark-crypto v0.12.1 // indirect
github.com/consensys/bavard v0.1.22 // indirect
github.com/consensys/gnark-crypto v0.14.0 // indirect
github.com/coreos/go-semver v0.3.1 // indirect
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
github.com/crackcomm/go-gitignore v0.0.0-20231225121904-e25f5bc08668 // indirect
github.com/crate-crypto/go-ipa v0.0.0-20240223125850-b1e8a79f509c // indirect
github.com/crate-crypto/go-kzg-4844 v1.0.0 // indirect
github.com/crate-crypto/go-ipa v0.0.0-20240724233137-53bbb0ceb27a // indirect
github.com/crate-crypto/go-kzg-4844 v1.1.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/deckarep/golang-set/v2 v2.6.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 // indirect
github.com/desertbit/timer v0.0.0-20180107155436-c41aec40b27f // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/ethereum/c-kzg-4844 v1.0.2 // indirect
github.com/ethereum/c-kzg-4844 v1.0.3 // indirect
github.com/ethereum/go-verkle v0.1.1-0.20240829091221-dffa7562dbe9 // indirect
github.com/facebookgo/atomicfile v0.0.0-20151019160806-2de1f203e7d5 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
Expand Down Expand Up @@ -184,7 +184,7 @@ require (
github.com/spf13/afero v1.11.0 // indirect
github.com/status-im/keycard-go v0.2.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/supranational/blst v0.3.12 // indirect
github.com/supranational/blst v0.3.13 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
Expand Down Expand Up @@ -217,8 +217,8 @@ require (
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 // indirect
golang.org/x/mod v0.19.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/text v0.18.0 // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/text v0.19.0 // indirect
golang.org/x/tools v0.23.0 // indirect
golang.org/x/xerrors v0.0.0-20240716161551-93cc26a95ae9 // indirect
gonum.org/v1/gonum v0.15.0 // indirect
Expand Down
Loading

0 comments on commit 38eed6b

Please sign in to comment.