Skip to content

Commit

Permalink
Merge pull request #1525 from 0chain/sprint-1.19
Browse files Browse the repository at this point in the history
Sprint 1.19
  • Loading branch information
dabasov authored Jan 24, 2025
2 parents c42f0f8 + 148fbfa commit e451fac
Show file tree
Hide file tree
Showing 12 changed files with 117 additions and 43 deletions.
34 changes: 21 additions & 13 deletions code/go/0chain.net/blobber/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,32 @@ import (
)

var (
deploymentMode int
keysFile string
mountPoint string
metadataDB string
logDir string
httpPort int
hostname string
configDir string
grpcPort int
httpsPort int
httpsKeyFile string
httpsCertFile string
hostUrl string
deploymentMode int
keysFile string
keysFilePublicKey string
keysFilePrivateKey string
keysFileClientKey string
keysFileIsSplit bool
mountPoint string
metadataDB string
logDir string
httpPort int
hostname string
configDir string
grpcPort int
httpsPort int
httpsKeyFile string
httpsCertFile string
hostUrl string
)

func init() {
flag.IntVar(&deploymentMode, "deployment_mode", 2, "deployment mode: 0=dev,1=test, 2=mainnet")
flag.StringVar(&keysFile, "keys_file", "", "keys_file")
flag.StringVar(&keysFilePublicKey, "keys_file_public_key", "", "keys_file_public_key")
flag.StringVar(&keysFilePrivateKey, "keys_file_private_key", "", "keys_file_private_key")
flag.StringVar(&keysFileClientKey, "keys_file_client_key", "", "keys_file_client_key")
flag.BoolVar(&keysFileIsSplit, "keys_file_is_split", false, "keys_file_is_split")
flag.StringVar(&mountPoint, "files_dir", "", "Mounted partition where all files will be stored")
flag.StringVar(&metadataDB, "db_dir", "", "db_dir")
flag.StringVar(&logDir, "log_dir", "", "log_dir")
Expand Down
41 changes: 31 additions & 10 deletions code/go/0chain.net/blobber/node.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bytes"
"errors"
"fmt"
"os"
Expand All @@ -13,23 +14,37 @@ import (
"go.uber.org/zap"
)

var publicKey, privateKey string
var clientKey, publicKey, privateKey string

func setupNode() error {
fmt.Println("> setup blobber")

err := readKeysFromAws()
if err != nil {
err = readKeysFromFile(&keysFile)
if err != nil {
panic(err)
var err error

if keysFilePrivateKey != "" || keysFilePublicKey != "" {
privateKey = keysFilePrivateKey
publicKey = keysFilePublicKey

if keysFileIsSplit {
clientKey = keysFileClientKey
}
fmt.Println("using blobber keys from local")

fmt.Println("using blobber keys from local string")
} else {
fmt.Println("using blobber keys from aws")
err = readKeysFromAws()
if err != nil {
err = readKeysFromFile(&keysFile)
if err != nil {
panic(err)
}
fmt.Println("using blobber keys from local")
} else {
fmt.Println("using blobber keys from aws")
}
}

node.Self.SetKeys(publicKey, privateKey)
node.Self.SetKeys(clientKey, publicKey, privateKey, keysFileIsSplit)

if node.Self.ID == "" {
return errors.New("node definition for self node doesn't exist")
} else {
Expand Down Expand Up @@ -72,6 +87,12 @@ func readKeysFromAws() error {
return nil
}

func readKeysFromString(keyFileRaw *string) error {
publicKey, privateKey, _, _ = encryption.ReadKeys(
bytes.NewBufferString(*keyFileRaw))
return nil
}

func readKeysFromFile(keysFile *string) error {
reader, err := os.Open(*keysFile)
if err != nil {
Expand All @@ -80,4 +101,4 @@ func readKeysFromFile(keysFile *string) error {
defer reader.Close()
publicKey, privateKey, _, _ = encryption.ReadKeys(reader)
return nil
}
}
4 changes: 4 additions & 0 deletions code/go/0chain.net/blobber/zcn.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ func setupServerChain() error {
return err
}

if node.Self.GetWallet().IsSplit {
zcncore.RegisterZauthServer(serverChain.ZauthServer)
}

fmt.Print(" [OK]\n")
return nil
}
33 changes: 23 additions & 10 deletions code/go/0chain.net/blobbercore/handler/auth_ticket.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package handler

import (
"context"
"fmt"
"github.com/0chain/blobber/code/go/0chain.net/core/encryption"
"github.com/0chain/blobber/code/go/0chain.net/core/node"
"github.com/0chain/common/core/common"
"github.com/0chain/gosdk/zcncore"
"net/http"
)

Expand All @@ -19,24 +22,34 @@ type AuthTicketResponse struct {
//
// parameters:
//
// +name: Zbox-Signature
// in: header
// type: string
// description: Digital signature to verify that the sender is 0box service.
// +name: client_id
// type: string
// in: query
// description: Client ID is used as a payload to the token generated. The token represents a signed version of this string by the blobber's private key.
// +name: Zbox-Signature
// in: header
// type: string
// description: Digital signature to verify that the sender is 0box service.
// +name: client_id
// type: string
// in: query
// description: Client ID is used as a payload to the token generated. The token represents a signed version of this string by the blobber's private key.
//
// responses:
// 200: AuthTicketResponse
//
// 200: AuthTicketResponse
func GenerateAuthTicket(ctx context.Context, r *http.Request) (interface{}, error) {

clientID := r.URL.Query().Get("client_id")
if clientID == "" {
return nil, common.NewError("missing_client_id", "client_id is required")
}

signature, err := node.Self.Sign(clientID)
round := r.URL.Query().Get("round")

payload := encryption.Hash(fmt.Sprintf("%s_%s", clientID, round))

if isActivated, err := zcncore.IsHardforkActivated("hermes"); err != nil || !isActivated {
payload = clientID
}

signature, err := node.Self.Sign(payload)
if err != nil {
return nil, common.NewError("signature_failed", "signature failed")
}
Expand Down
3 changes: 2 additions & 1 deletion code/go/0chain.net/blobbercore/handler/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package handler
import (
"context"
"errors"
"sync"

"github.com/0chain/gosdk/core/client"
coreTxn "github.com/0chain/gosdk/core/transaction"
"sync"

"github.com/0chain/blobber/code/go/0chain.net/blobbercore/allocation"
"github.com/0chain/blobber/code/go/0chain.net/blobbercore/config"
Expand Down
1 change: 1 addition & 0 deletions code/go/0chain.net/core/chain/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func NewChainFromConfig() *Chain {
chain.ID = common.ToKey(config.Configuration.ChainID)
chain.OwnerID = viper.GetString("server_chain.owner")
chain.BlockWorker = viper.GetString("block_worker")
chain.ZauthServer = viper.GetString("zauth_server")
return chain
}

Expand Down
32 changes: 28 additions & 4 deletions code/go/0chain.net/core/node/self_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/hex"
"encoding/json"
"fmt"

"github.com/0chain/blobber/code/go/0chain.net/core/common"
"github.com/0chain/blobber/code/go/0chain.net/core/config"
"github.com/0chain/gosdk/core/zcncrypto"
Expand All @@ -19,20 +20,43 @@ type SelfNode struct {
}

/*SetKeys - setter */
func (sn *SelfNode) SetKeys(publicKey, privateKey string) {
publicKeyBytes, err := hex.DecodeString(publicKey)
func (sn *SelfNode) SetKeys(clientKey, publicKey, privateKey string, isSplit bool) {
var (
publicKeyBytes []byte
err error
)

if isSplit {
publicKeyBytes, err = hex.DecodeString(clientKey)
} else {
publicKeyBytes, err = hex.DecodeString(publicKey)
}

if err != nil {
panic(err)
}

sn.wallet = &zcncrypto.Wallet{}
sn.wallet.ClientID = Hash(publicKeyBytes)
sn.wallet.ClientKey = publicKey

if isSplit {
sn.wallet.ClientKey = clientKey
} else {
sn.wallet.ClientKey = publicKey
}

sn.wallet.Keys = make([]zcncrypto.KeyPair, 1)
sn.wallet.Keys[0].PublicKey = publicKey
sn.wallet.Keys[0].PrivateKey = privateKey
sn.wallet.Version = zcncrypto.CryptoVersion
sn.wallet.IsSplit = isSplit

if isSplit {
sn.PublicKey = clientKey
} else {
sn.PublicKey = publicKey
}

sn.PublicKey = publicKey
sn.ID = sn.wallet.ClientID
}

Expand Down
2 changes: 1 addition & 1 deletion code/go/0chain.net/validator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func main() {
fmt.Println("using validator keys from aws")
}

node.Self.SetKeys(publicKey, privateKey)
node.Self.SetKeys("", publicKey, privateKey, false)

if len(*hostUrl) > 0 {
node.Self.URL = *hostUrl
Expand Down
2 changes: 1 addition & 1 deletion code/go/0chain.net/validatorcore/storage/models_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,6 @@ func setupModelsTest(t *testing.T) error {
return err
}

node.Self.SetKeys(wallet.Keys[0].PublicKey, wallet.Keys[0].PrivateKey)
node.Self.SetKeys("", wallet.Keys[0].PublicKey, wallet.Keys[0].PrivateKey, false)
return nil
}
2 changes: 2 additions & 0 deletions config/0chain_blobber.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ min_confirmation: 10

block_worker: https://dev.0chain.net/dns

zauth_server: https://zauth.dev.0chain.net/

rate_limiters:
# Rate limiters will use this duration to clean unused token buckets.
# If it is 0 then token will expire in 10 years.
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ toolchain go1.22.5

require (
github.com/0chain/errors v1.0.3
github.com/0chain/gosdk v1.18.18
github.com/0chain/gosdk v1.19.0-RC0
github.com/DATA-DOG/go-sqlmock v1.5.0
github.com/didip/tollbooth/v6 v6.1.2
github.com/go-openapi/runtime v0.26.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ github.com/0chain/common v1.18.3 h1:42dYOv2KyMTSanuS67iDtfv+ErbSRqR8NJ3MG72MwaI=
github.com/0chain/common v1.18.3/go.mod h1:Lapu2Tj7z5Sm4r+X141e7vsz4NDODTEypeElYAP3iSw=
github.com/0chain/errors v1.0.3 h1:QQZPFxTfnMcRdt32DXbzRQIfGWmBsKoEdszKQDb0rRM=
github.com/0chain/errors v1.0.3/go.mod h1:xymD6nVgrbgttWwkpSCfLLEJbFO6iHGQwk/yeSuYkIc=
github.com/0chain/gosdk v1.18.18 h1:zMCGWaavz3FpdJ6aw/3ftEXRkW60YFINoZCNoJDuFRo=
github.com/0chain/gosdk v1.18.18/go.mod h1:8unFy9Dx2YyPKMYPDGR3MFhUEymbAfQcRDm9bobVLGw=
github.com/0chain/gosdk v1.19.0-RC0 h1:PhHsvfEBJw9ofEFGWKqJ7UID7qMfl1LrWl2GyhIxjqE=
github.com/0chain/gosdk v1.19.0-RC0/go.mod h1:8unFy9Dx2YyPKMYPDGR3MFhUEymbAfQcRDm9bobVLGw=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/DATA-DOG/go-sqlmock v1.5.0 h1:Shsta01QNfFxHCfpW6YH2STWB0MudeXXEWMr20OEh60=
Expand Down

0 comments on commit e451fac

Please sign in to comment.