Skip to content

Commit

Permalink
Merge pull request #584 from Deralden/master
Browse files Browse the repository at this point in the history
Daemon release version 5.1.3
  • Loading branch information
AlbinaPomogalova authored May 21, 2024
2 parents 77959e3 + 5b85dfc commit 260c220
Show file tree
Hide file tree
Showing 40 changed files with 718 additions and 757 deletions.
8 changes: 1 addition & 7 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,9 @@ snetd.config.json
resources/blockchain/package-lock.json

# Autogenerated sources
blockchain/registry.go
blockchain/multi_party_escrow.go
blockchain/singularity_net_token.go
blockchain/snet-contracts.go
*.pb.go

# service proto
service.proto
service.proto-tmp.pb

# Binaries for programs and plugins
*.exe
*.exe~
Expand Down
72 changes: 39 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,21 @@ nonce of the channel.
It then sends and ON-Chain transaction to claim funds.The daemons continue their work independently without any
confirmation from the treasurer on the blockchain.

## Usage (without compiling)

Users interested in deploying SingularityNET services using SingularityNET
Daemon should install the appropriate [binary from releases](https://github.com/singnet/snet-daemon/releases).

## Development

These instructions are intended to facilitate the development and testing of SingularityNET Daemon. Users interested in
deploying SingularityNET services using SingularityNET Daemon should install the appropriate binary as
[released](#release).
These instructions are intended to facilitate the development and testing of SingularityNET Daemon.

### Prerequisites and dependencies

* [Go 1.21+](https://golang.org/dl/)
* [NodeJS 15+ w/npm](https://nodejs.org/en/download/)
* [Protoc v25.0+](https://github.com/protocolbuffers/protobuf/releases)

Protoc, nodejs, go and go/bin should be in environment variables.
**Protoc (libprotoc), golang and $GOPATH/bin should be in environment variables.**

### Installing

Expand All @@ -40,40 +42,46 @@ $ git clone [email protected]:singnet/snet-daemon.git
$ cd snet-daemon
```

* Install development/test dependencies
* Install dependencies and generate bindings

```bash
$ ./scripts/install
```

* Build snet-daemon. Please note using ldflags, the latest tagged version , sha1 revision and the build time are set as part of the build.
* Build snet-daemon. Please note using ldflags, the latest tagged version , sha1 revision and the build time are set as
part of the build.
You need to pass the version as shown in the example below

```bash
$ ./scripts/build <linux/windows/darwin> <amd64/arm/arm64> <version>
```

Example:

```bash
$ ./scripts/build linux amd64 v5.1.2
```

* Generate default config file snet-daemon (on Linux amd64 platform)
#### Generate default minimum config file for snet-daemon

```bash
$ ./build/snetd-linux-amd64 init
```

**** Please update the registry address in daemon config based on the test network used
#### Generate default full config file for snet-daemon

```bash
$ ./build/snetd-linux-amd64 init-full
```

#### Multi-compiling

If you want to build snetd for several platforms, run `./scripts/build-all <version>` instead
of `./scripts/build`.

You can edit the script to choose a specific platforms, but by default it will build for Linux, OSX, and Windows
You can edit the script to choose a specific platforms, but by default it will build for Linux, OSX, and Windows.

#### Run Deamon
#### Run Daemon

```bash
$ ../build/snetd-linux-amd64
Expand Down Expand Up @@ -117,6 +125,7 @@ Available Commands:
freecall Manage operations on free call users
help Help about any command
init Write default configuration to file
init-full Write full default configuration to file
list List channels, claims in progress, etc
serve Is the default option which starts the Daemon.
version List the current version of the Daemon.
Expand All @@ -141,7 +150,7 @@ can be set via environment variables or command line parameters see [table
below](#environment-variables-and-cli-parameters). Use `--config`
parameter with any command to set configuration file name. By default daemon
use configuration file in JSON format `snetd.config.json` but other formats are
also supported via [Viper](https://github.com/spf13/viper). Use `snet init`
also supported via [Viper](https://github.com/spf13/viper). Use `snet init-full`
command to save configuration file with default values. Following
configuration properties can be set using configuration file.

Expand All @@ -155,34 +164,14 @@ time.
Daemon will automatically read the Registry address associated with this network For local network ( you can also
specify the registry address manually),see the blockchain_network_config.json

* **service_credentials** (optional, for service_type http only):
Array of credentials, example:

```
"service_credentials": [
{
"key": "example_body_param",
"value": 12345,
"location": "body"
},
{
"key": "X-API-Key",
"value": "546bd7d4-d3e1-46ba-b752-bc45e4dc5b39",
"location": "header"
}
],
```

Location can be: query, header or body. Query and header values must be string.

* **daemon_end_point** (required;) -
Defines the ip and the port on which the daemon listens to.
format is :`<host>:<port>`.

* **ethereum_json_rpc_endpoint** (optional, default: `"http://127.0.0.1:8545"`) -
endpoint to which daemon sends ethereum JSON-RPC requests;
Based on the network selected blockchain_network_selected the end point is auto determined
Example `"https://kovan.infura.io"` for kovan testnet.
Example `"https://sepolia.infura.io/v3"` for sepolia testnet.

* **blockchain_provider_api_key** (optional) - basic header authorization key for blockchain providers. Tested with
infura api
Expand Down Expand Up @@ -213,10 +202,27 @@ Location can be: query, header or body. Query and header values must be string.
* **executable_path** (required if `service_type` == `executable`) -
path to executable to expose as a service.


#### Blockchain network config

You can update the registry address or ethereum_json_rpc_endpoint in `resources/blockchain_network_config.json`

#### Other properties

This options are less frequently needed.

* **service_credentials** (optional, for service_type http only):
Array of credentials, example:

```
"service_credentials": [
{"key": "example_body_param", "value": 12345,"location": "body"},
{"key": "X-API-Key", "value": "546bd7d4-d3e1-46ba-b752-bc45e4dc5b39", "location": "header"}
],
```

Location can be: query, header or body. Query and header values must be string.

* **allowed_users_flag** (optional;default:`false`) - You may need to protect the service provider 's service in test
environment from being called by anyone, only Authorized users can make calls , when this flag is defined in the
config, you can enforce this behaviour.You cannot set this flag to true
Expand Down
7 changes: 3 additions & 4 deletions authutils/auth_service_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// authutils package provides functions for all authentication and singature validation related operations
// authutils package provides functions for all authentication and singature validation related operations
package authutils

import (

"math/big"
"testing"

Expand All @@ -11,7 +10,7 @@ import (
)

func TestCompareWithLatestBlockNumber(t *testing.T) {
config.Vip().Set(config.BlockChainNetworkSelected, "ropsten")
config.Vip().Set(config.BlockChainNetworkSelected, "sepolia")
config.Validate()
currentBlockNum, _ := CurrentBlock()
err := CompareWithLatestBlockNumber(currentBlockNum.Add(currentBlockNum, big.NewInt(13)))
Expand All @@ -24,7 +23,7 @@ func TestCompareWithLatestBlockNumber(t *testing.T) {
}

func TestCheckAllowedBlockDifferenceForToken(t *testing.T) {
config.Vip().Set(config.BlockChainNetworkSelected, "ropsten")
config.Vip().Set(config.BlockChainNetworkSelected, "sepolia")
config.Validate()
currentBlockNum, _ := CurrentBlock()
err := CheckIfTokenHasExpired(currentBlockNum.Sub(currentBlockNum, big.NewInt(20000)))
Expand Down
3 changes: 1 addition & 2 deletions blockchain/blockchain.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:generate node ../resources/blockchain/scripts/generateAbi.js --contract-package singularitynet-platform-contracts --contract-name MultiPartyEscrow --go-package blockchain --output-file multi_party_escrow.go
//go:generate node ../resources/blockchain/scripts/generateAbi.js --contract-package singularitynet-platform-contracts --contract-name Registry --go-package blockchain --output-file registry.go
//go:generate go run ../resources/generate-smart-binds/main.go
package blockchain

import (
Expand Down
2 changes: 0 additions & 2 deletions blockchain/ethereum_test_util.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//go:generate node ../resources/blockchain/scripts/generateAbi.js --contract-package singularitynet-token-contracts --contract-name SingularityNetToken --go-package blockchain --output-file singularity_net_token.go

package blockchain

import (
Expand Down
100 changes: 22 additions & 78 deletions blockchain/serviceMetadata.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
package blockchain

import (
"context"
"encoding/json"
"fmt"
"github.com/bufbuild/protocompile"
pproto "github.com/emicklei/proto"
"github.com/ethereum/go-ethereum/common"
"github.com/pkg/errors"
"github.com/singnet/snet-daemon/config"
"github.com/singnet/snet-daemon/ipfsutils"
log "github.com/sirupsen/logrus"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protodesc"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry"
"google.golang.org/protobuf/types/descriptorpb"
"math/big"
"os"
"os/exec"
"path"
"strings"
)

Expand Down Expand Up @@ -356,7 +352,6 @@ func setDerivedFields(metaData *ServiceMetadata) (err error) {
setMultiPartyEscrowAddress(metaData)

return nil

}

func setGroup(metaData *ServiceMetadata) (err error) {
Expand Down Expand Up @@ -480,44 +475,6 @@ func isElementInArray(a string, list []string) bool {
return false
}

func createProtoRegistry(srcDir string, filename string) (*protoregistry.Files, error) {
// Create descriptors using the protoc binary.
// Imported dependencies are included so that the descriptors are self-contained.
tmpFile := filename + "-tmp.pb"
cmd := exec.Command("protoc",
"--include_imports",
"--descriptor_set_out="+tmpFile,
"-I"+srcDir,
path.Join(srcDir, filename))

cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
return nil, err
}
//defer os.Remove(tmpFile)

marshalledDescriptorSet, err := os.ReadFile(tmpFile)
if err != nil {
return nil, err
}
descriptorSet := descriptorpb.FileDescriptorSet{}
err = proto.Unmarshal(marshalledDescriptorSet, &descriptorSet)
if err != nil {
log.Println("can't unmarshal: ", err)
return nil, err
}

files, err := protodesc.NewFiles(&descriptorSet)
if err != nil {
log.Println(err)
return nil, err
}

return files, nil
}

func setServiceProto(metaData *ServiceMetadata) (err error) {
metaData.DynamicPriceMethodMapping = make(map[string]string, 0)
metaData.TrainingMethods = make([]string, 0)
Expand All @@ -527,27 +484,13 @@ func setServiceProto(metaData *ServiceMetadata) (err error) {
return err
}

if metaData.ServiceType == "http" {
if len(protoFiles) > 1 {
log.Fatalln("daemon support only one proto file for HTTP services!")
}
if metaData.ServiceType == "http" && len(protoFiles) > 1 {
log.Fatalln("Currently daemon support only one proto file for HTTP services!")
}

for _, file := range protoFiles {
log.Debugln("Protofile: ", file)

if metaData.ServiceType == "http" {
_, err = os.Create(serviceProto)
if err != nil {
log.Fatalln("Can't create proto file: ", err)
}

err = os.WriteFile(serviceProto, []byte(file), 0666)
if err != nil {
log.Fatalln("Can't write to proto file: ", err)
}
}

//If Dynamic pricing is enabled ,there will be mandatory checks on the service proto
//this is to ensure that the standards on how one defines the methods to invoke is followed
if config.GetBool(config.EnableDynamicPricing) {
Expand All @@ -562,25 +505,10 @@ func setServiceProto(metaData *ServiceMetadata) (err error) {
metaData.TrainingMethods = trainingMethodMap
}
}
}

if metaData.ServiceType == "http" {
files, err := createProtoRegistry(".", serviceProto)
if err != nil {
log.Println("createProtoRegistry: ", err)
}

protoFile, err := files.FindFileByPath(serviceProto)
if err != nil {
log.Println("files.FindFileByPat: ", err)
}

err = files.RegisterFile(protoFile)
if err != nil {
log.Println("files.RegisterFile(desc): ", err)
if metaData.ServiceType == "http" {
metaData.ProtoFile = getFileDescriptor(file)
}

metaData.ProtoFile = protoFile
}

return nil
Expand Down Expand Up @@ -628,3 +556,19 @@ func buildDynamicPricingMethodsMap(serviceProto *pproto.Proto) (dynamicPricingMe
}
return
}

func getFileDescriptor(protoContent string) protoreflect.FileDescriptor {

accessor := protocompile.SourceAccessorFromMap(map[string]string{
serviceProto: protoContent,
})
compiler := protocompile.Compiler{
Resolver: &protocompile.SourceResolver{Accessor: accessor},
SourceInfoMode: protocompile.SourceInfoStandard,
}
fds, err := compiler.Compile(context.Background(), serviceProto)
if err != nil {
log.Println(err)
}
return fds.FindFileByPath(serviceProto)
}
2 changes: 1 addition & 1 deletion blockchain/serviceMetadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func TestReadServiceMetaDataFromLocalFile(t *testing.T) {

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

Expand Down
Loading

0 comments on commit 260c220

Please sign in to comment.