Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compare 0box tables with sharder tables #1076

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
145 changes: 145 additions & 0 deletions internal/api/model/compare.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package model

import (
"time"

"github.com/0chain/common/core/currency"
"github.com/0chain/gosdk/core/common"
)

type Blobber struct {
ID string `gorm:"primaryKey" json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DelegateWallet string `json:"delegate_wallet"`
NumDelegates int `json:"num_delegates"`
ServiceCharge float64 `json:"service_charge"`
TotalStake currency.Coin `json:"total_stake"`
Downtime uint64 `json:"downtime"`
LastHealthCheck common.Timestamp `json:"last_health_check"`
IsKilled bool `json:"is_killed"`
IsShutdown bool `json:"is_shutdown"`
BaseURL string `json:"base_url" gorm:"uniqueIndex"`
ReadPrice currency.Coin `json:"read_price"`
WritePrice currency.Coin `json:"write_price"`
Capacity int64 `json:"capacity"`
Allocated int64 `json:"allocated"`
SavedData int64 `json:"saved_data"`
ReadData int64 `json:"read_data"`
NotAvailable bool `json:"not_available"`
IsRestricted bool `json:"is_restricted"`
OffersTotal currency.Coin `json:"offers_total"`
TotalServiceCharge currency.Coin `json:"total_service_charge"`
ChallengesPassed uint64 `json:"challenges_passed"`
ChallengesCompleted uint64 `json:"challenges_completed"`
OpenChallenges uint64 `json:"open_challenges"`
TotalBlockRewards currency.Coin `json:"total_block_rewards"`
TotalStorageIncome currency.Coin `json:"total_storage_income"`
TotalReadIncome currency.Coin `json:"total_read_income"`
TotalSlashedStake currency.Coin `json:"total_slashed_stake"`
CreationRound int64 `json:"creation_round"`
}
type Miner struct {
ID string `json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DelegateWallet string `json:"delegate_wallet"`
NumDelegates int `json:"num_delegates"`
ServiceCharge float64 `json:"service_charge"`
TotalStake int64 `json:"total_stake"`
Downtime int64 `json:"downtime"`
LastHealthCheck int64 `json:"last_health_check"`
IsKilled bool `json:"is_killed"`
IsShutdown bool `json:"is_shutdown"`
N2NHost string `json:"n2n_host"`
Host string `json:"host"`
Port int64 `json:"port"`
Path string `json:"path"`
PublicKey string `json:"public_key"`
ShortName string `json:"short_name"`
BuildTag string `json:"build_tag"`
Delete bool `json:"delete"`
Fees currency.Coin `json:"fees"`
Active bool `json:"active"`
BlocksFinalised int64 `json:"blocks_finalised"`

Check failure on line 64 in internal/api/model/compare.go

View workflow job for this annotation

GitHub Actions / lint

`finalised` is a misspelling of `finalized` (misspell)
CreationRound int64 `json:"creation_round" gorm:"index:idx_miner_creation_round"`
}

type Sharder struct {
ID string `json:"id"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
DelegateWallet string `json:"delegate_wallet"`
NumDelegates string `json:"num_delegates"`
ServiceCharge float64 `json:"service_charge"`
TotalStake string `json:"total_stake"`
Downtime uint64 `json:"downtime"`
LastHealthCheck common.Timestamp `json:"last_health_check"`
IsKilled bool `json:"is_killed"`
IsShutdown bool `json:"is_shutdown"`
N2NHost string `json:"n2n_host"`
Host string `json:"host"`
Port int `json:"port"`
Path string `json:"path"`
PublicKey string `json:"public_key"`
ShortName string `json:"short_name"`
BuildTag string `json:"build_tag"`
Delete bool `json:"delete"`
Fees currency.Coin `json:"fees"`
Active bool `json:"active"`
CreationRound int64 `json:"creation_round" gorm:"index:idx_sharder_creation_round"`
}

type Validator struct {
ID string `gorm:"primaryKey" json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DelegateWallet string `json:"delegate_wallet"`
NumDelegates int `json:"num_delegates"`
ServiceCharge float64 `json:"service_charge"`
TotalStake currency.Coin `json:"total_stake"`
Downtime uint64 `json:"downtime"`
LastHealthCheck common.Timestamp `json:"last_health_check"`
IsKilled bool `json:"is_killed"`
IsShutdown bool `json:"is_shutdown"`
Url string `json:"base_url"`
PublicKey string `json:"public_key"`
CreationRound int64 `json:"creation_round"`
}
type Authorizer struct {
ID string `gorm:"primaryKey" json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DelegateWallet string `json:"delegate_wallet"`
NumDelegates int `json:"num_delegates"`
ServiceCharge float64 `json:"service_charge"`
TotalStake currency.Coin `json:"total_stake"`
Downtime uint64 `json:"downtime"`
LastHealthCheck common.Timestamp `json:"last_health_check"`
IsKilled bool `json:"is_killed"`
IsShutdown bool `json:"is_shutdown"`
URL string `json:"url"`
Fee currency.Coin `json:"fee"`
TotalMint currency.Coin `json:"total_mint"`
TotalBurn currency.Coin `json:"total_burn"`
CreationRound int64 `json:"creation_round"`
}
type User struct {
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
UserID string `json:"user_id" gorm:"uniqueIndex"`
TxnHash string `json:"txn_hash"`
Balance currency.Coin `json:"balance"`
Round int64 `json:"round"`
Nonce int64 `json:"nonce"`
MintNonce int64 `json:"mint_nonce"`
}
type ProviderRewards struct {
ID uint `json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
ProviderID string `json:"provider_id"`
Rewards currency.Coin `json:"rewards"`
TotalRewards currency.Coin `json:"total_rewards"`
RoundServiceChargeLastUpdated int64 `json:"round_service_charge_last_updated"`
}
72 changes: 72 additions & 0 deletions internal/api/util/client/api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"log"
"net/http"
"net/url"
"reflect"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -60,6 +61,7 @@ const (
PartitionSizeFrequency = "/v1/screst/:sc_address/parition-size-frequency"
BlobberPartitionSelectionFrequency = "/v1/screst/:sc_address/blobber-selection-frequency"
GetAllChallenges = "/v1/screst/:sc_address/all-challenges"
GetQueryData = "/v1/screst/:sc_address/query-data"
)

// Contains all used service providers
Expand Down Expand Up @@ -2119,3 +2121,73 @@ func (c *APIClient) BurnZcn(t *test.SystemTest, wallet *model.Wallet, address st
wallet.IncNonce()
return burnZcnTransactionGetConfirmationResponse.Hash
}

func (c *APIClient) QueryDataFromSharder(t *test.SystemTest, tableName string) ([]interface{}, *resty.Response, error) {
t.Log("Querying data from Sharder...")

extractFields := func(model interface{}) string {
val := reflect.ValueOf(model)
if val.Kind() == reflect.Ptr {
val = val.Elem()
}
if val.Kind() != reflect.Struct {
return ""
}
var fieldNames []string
typ := val.Type()
for i := 0; i < val.NumField(); i++ {
field := typ.Field(i)
jsonTag := field.Tag.Get("json")
if jsonTag != "" && jsonTag != "-" {
tagParts := strings.Split(jsonTag, ",")
fieldNames = append(fieldNames, tagParts[0])
} else {
fieldNames = append(fieldNames, field.Name)
}
}
return strings.Join(fieldNames, ",")
}
urlBuilder := NewURLBuilder().
SetPath(GetQueryData).
SetPathVariable("sc_address", StorageSmartContractAddress)

var data interface{}
var tableEntity interface{}
switch tableName {
case "blobber":
tableEntity = model.Blobber{}
case "miner":
tableEntity = model.Miner{}
case "authorizer":
tableEntity = model.Authorizer{}
case "validator":
tableEntity = model.Validator{}
case "sharder":
tableEntity = model.Sharder{}
case "user":
tableEntity = model.User{}
case "provider_rewards":
tableEntity = model.ProviderRewards{}

}
urlBuilder.queries.Set("entity", tableName)
urlBuilder.queries.Set("fields", extractFields(tableEntity))
resp, err := c.executeForAllServiceProviders(t, urlBuilder, &model.ExecutionRequest{
Dst: &data,
RequiredStatusCode: 200,
Headers: map[string]string{
"Accept-Encoding": "",
},
}, HttpGETMethod, SharderServiceProvider)
var result []interface{}
switch v := data.(type) {
case []interface{}:
for _, value := range v {
result = append(result, value)
}
default:
t.Error("Invalid response from Sharder")
}

return result, resp, err
}
101 changes: 58 additions & 43 deletions internal/api/util/client/zbox_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1289,57 +1289,72 @@ func (c *ZboxClient) GetGraphBlobberTotalRewards(t *test.SystemTest, blobberId s
return &data, resp, err
}

func (c *ZboxClient) CreateJwtToken(t *test.SystemTest, headers map[string]string) (*model.ZboxJwtToken, *resty.Response, error) {
t.Logf("creating jwt token for userID [%v] using 0box...", headers["X-App-User-ID"])
var zboxJwtToken *model.ZboxJwtToken

urlBuilder := NewURLBuilder()
err := urlBuilder.MustShiftParse(c.zboxEntrypoint)
require.NoError(t, err, "URL parse error")
urlBuilder.SetPath("/v2/jwt/token")

resp, err := c.executeForServiceProvider(t, urlBuilder.String(), model.ExecutionRequest{
Dst: &zboxJwtToken,
Headers: headers,
RequiredStatusCode: 201,
}, HttpPOSTMethod)

return zboxJwtToken, resp, err
}

func (c *ZboxClient) RefreshJwtToken(t *test.SystemTest, token string, headers map[string]string) (*model.ZboxJwtToken, *resty.Response, error) {
t.Logf("refreshing jwt token for userID [%v] and token [%v] using 0box...", headers["X-App-User-ID"], token)
var zboxJwtToken *model.ZboxJwtToken

headers["X-Jwt-Token"] = token
func (c *ZboxClient) QueryDataFrom0box(t *test.SystemTest, tableName string) ([]interface{}, *resty.Response, error) {
t.Logf("Querying data from 0box...")

extractFields := func(model interface{}) string {
val := reflect.ValueOf(model)
if val.Kind() == reflect.Ptr {
val = val.Elem()
}
if val.Kind() != reflect.Struct {
return ""
}
var fieldNames []string
typ := val.Type()
for i := 0; i < val.NumField(); i++ {
field := typ.Field(i)
jsonTag := field.Tag.Get("json")
if jsonTag != "" && jsonTag != "-" {
tagParts := strings.Split(jsonTag, ",")
fieldNames = append(fieldNames, tagParts[0])
} else {
fieldNames = append(fieldNames, field.Name)
}
}
return strings.Join(fieldNames, ",")
}

urlBuilder := NewURLBuilder()
err := urlBuilder.MustShiftParse(c.zboxEntrypoint)
require.NoError(t, err, "URL parse error")
urlBuilder.SetPath("/v2/jwt/token")
urlBuilder.SetPath("/v2/queryData")
var data interface{}
var tableEntity interface{}
switch tableName {
case "blobber":
tableEntity = model.Blobber{}
case "miner":
tableEntity = model.Miner{}
case "authorizer":
tableEntity = model.Authorizer{}
case "validator":
tableEntity = model.Validator{}
case "sharder":
tableEntity = model.Sharder{}
case "user":
tableEntity = model.User{}
case "provider_rewards":
tableEntity = model.ProviderRewards{}

resp, err := c.executeForServiceProvider(t, urlBuilder.String(), model.ExecutionRequest{
Dst: &zboxJwtToken,
Headers: headers,
RequiredStatusCode: 201,
}, HttpPUTMethod)

return zboxJwtToken, resp, err
}

func (c *ZboxClient) GetTransactionsList(t *test.SystemTest, pitId string) (*model.ZboxTransactionsDataResponse, *resty.Response, error) {
t.Logf("Getting transactions data with pitid using 0box...")
var txnData model.ZboxTransactionsDataResponse
urlBuilder := NewURLBuilder()
err := urlBuilder.MustShiftParse(c.zboxEntrypoint)
require.NoError(t, err, "URL parse error")
urlBuilder.SetPath("/v2/transactions")
urlBuilder.queries.Set("pit_id", pitId)
}
urlBuilder.queries.Set("table", tableName)
urlBuilder.queries.Set("fields", extractFields(tableEntity))

resp, err := c.executeForServiceProvider(t, urlBuilder.String(), model.ExecutionRequest{
Dst: &txnData,
Dst: &data,
RequiredStatusCode: 200,
}, HttpGETMethod)

return &txnData, resp, err
var result []interface{}
switch v := data.(type) {
case []interface{}:
for _, value := range v {
result = append(result, value)
}
default:
t.Error("Invalid response from Sharder")
}

return result, resp, err
}
Loading
Loading