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

updated: added test cases for storage version #1114

Open
wants to merge 24 commits into
base: sprint-1.18
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions internal/api/model/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,13 @@ func NewRegisterBlobberTransactionData(scRegisterBlobberRequest *StorageNode) Tr
}
}

func NewKillBlobberTransactionData(killBlobberRequest *KillBlobberRequest) TransactionData {
return TransactionData{
Name: "kill_blobber",
Input: *killBlobberRequest,
}
}

func NewCreateFreeAllocationTransactionData(scRestGetFreeAllocationBlobbersResponse *SCRestGetFreeAllocationBlobbersResponse) TransactionData {
return TransactionData{
Name: "free_allocation_request",
Expand Down Expand Up @@ -527,6 +534,7 @@ type BlobberRequirements struct {
ExpirationDate int64 `json:"expiration_date"`
ReadPriceRange PriceRange `json:"read_price_range"`
WritePriceRange PriceRange `json:"write_price_range"`
StorageVersion int64 `json:"storage_version"`
}

type PriceRange struct {
Expand Down Expand Up @@ -650,6 +658,10 @@ type SCRestGetBlobberResponse struct {
ReadData int64 `json:"read_data"`
ChallengesPassed int64 `json:"challenges_passed"`
ChallengesCompleted int64 `json:"challenges_completed"`

//todo:
StorageVersion int64 `json:"storage_version"`
ManagingWallet string `json:"managing_wallet"`
}

type SCRestGetBlobbersResponse struct {
Expand Down Expand Up @@ -690,6 +702,10 @@ type FreeAllocationRequest struct {
Marker string `json:"marker,omitempty"`
}

type KillBlobberRequest struct {
ProviderID string `json:"provider_id,omitempty"`
}

type SCRestGetAllocationResponse struct {
ID string `json:"id"`
Tx string `json:"tx"`
Expand Down Expand Up @@ -732,6 +748,8 @@ type StorageNode struct {
LastHealthCheck int64 `json:"last_health_check"`
PublicKey string `json:"-"`
StakePoolSettings StakePoolSettings `json:"stake_pool_settings"`
StorageVersion int64 `json:"storage_version"`
ManagingWallet string `json:"managing_wallet"`
}

type StorageNodeGeolocation struct {
Expand Down
1 change: 1 addition & 0 deletions internal/api/model/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ func DefaultBlobberRequirements(id, publicKey string) BlobberRequirements {
},
OwnerId: id,
OwnerPublicKey: publicKey,
StorageVersion: 2,
}
}
115 changes: 115 additions & 0 deletions internal/api/util/client/api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,121 @@ func (c *APIClient) RegisterBlobber(t *test.SystemTest,
return registerBlobberTransactionPutResponse.Entity.Hash
}

func (c *APIClient) KillBlobber(t *test.SystemTest,
wallet *model.Wallet,
killBlobberRequest *model.KillBlobberRequest,
requiredTransactionStatus int) string {
t.Log("Killing blobber...")

killBlobberTransactionPutResponse, resp, err := c.V1TransactionPut(
t,
model.InternalTransactionPutRequest{
Wallet: wallet,
ToClientID: StorageSmartContractAddress,
TransactionData: model.NewKillBlobberTransactionData(killBlobberRequest),
Value: tokenomics.IntToZCN(0),
TxnType: SCTxType,
},
HttpOkStatus)
require.Nil(t, err)
require.NotNil(t, resp)
require.NotNil(t, killBlobberTransactionPutResponse)

var killBlobberTransactionGetConfirmationResponse *model.TransactionGetConfirmationResponse

wait.PoolImmediately(t, time.Minute*2, func() bool {
killBlobberTransactionGetConfirmationResponse, resp, err = c.V1TransactionGetConfirmation(
t,
model.TransactionGetConfirmationRequest{
Hash: killBlobberTransactionPutResponse.Entity.Hash,
},
HttpOkStatus)

if err != nil {
t.Log("Error killing blobber : ", err)
return false
}

if resp == nil {
fmt.Println("got nil response : ", resp)
return false
}

if killBlobberTransactionGetConfirmationResponse == nil {
fmt.Println("got nil txn confirmation response : ", killBlobberTransactionGetConfirmationResponse)
return false
}

return killBlobberTransactionGetConfirmationResponse.Status == requiredTransactionStatus
})

wallet.IncNonce()
return killBlobberTransactionPutResponse.Entity.Hash
}

func (c *APIClient) RegisterBlobberWithIdVerification(t *test.SystemTest,
wallet *model.Wallet,
storageNode *model.StorageNode,
requiredTransactionStatus int,
blobberId string) string {
t.Log("Registering blobber...")

registerBlobberTransactionPutResponse, resp, err := c.V1TransactionPut(
t,
model.InternalTransactionPutRequest{
Wallet: wallet,
ToClientID: StorageSmartContractAddress,
TransactionData: model.NewRegisterBlobberTransactionData(storageNode),
Value: tokenomics.IntToZCN(0),
TxnType: SCTxType,
},
HttpOkStatus)
require.Nil(t, err)
require.NotNil(t, resp)
require.NotNil(t, registerBlobberTransactionPutResponse)

var registerBlobberTransactionGetConfirmationResponse *model.TransactionGetConfirmationResponse

wait.PoolImmediately(t, time.Minute*2, func() bool {
registerBlobberTransactionGetConfirmationResponse, resp, err = c.V1TransactionGetConfirmation(
t,
model.TransactionGetConfirmationRequest{
Hash: registerBlobberTransactionPutResponse.Entity.Hash,
},
HttpOkStatus)

if err != nil {
t.Log("Error registering blobber : ", err)
return false
}

if resp == nil {
t.Log("got nil response : ", resp)
return false
}

if registerBlobberTransactionGetConfirmationResponse == nil {
t.Log("got nil txn confirmation response : ", registerBlobberTransactionGetConfirmationResponse)
return false
}

var storageNode model.StorageNode

// Unmarshal the JSON string into the StorageNode struct
err := json.Unmarshal([]byte(registerBlobberTransactionGetConfirmationResponse.Transaction.TransactionOutput), &storageNode)
if err != nil {
t.Log("Error unmarshalling JSON:", err)
return false
}

return registerBlobberTransactionGetConfirmationResponse.Status == requiredTransactionStatus && storageNode.ID == blobberId
})

wallet.IncNonce()

return registerBlobberTransactionPutResponse.Entity.Hash
}

func (c *APIClient) CreateFreeAllocation(t *test.SystemTest,
wallet *model.Wallet,
scRestGetFreeAllocationBlobbersResponse *model.SCRestGetFreeAllocationBlobbersResponse,
Expand Down
7 changes: 1 addition & 6 deletions internal/cli/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,6 @@ type Terms struct {
WritePrice int64 `json:"write_price"`
}

type Settings struct {
Delegate_wallet string `json:"delegate_wallet"`
Num_delegates int `json:"num_delegates"`
Service_charge float64 `json:"service_charge"`
}

type BlobberInfo struct {
Id string `json:"id"`
Url string `json:"url"`
Expand Down Expand Up @@ -386,6 +380,7 @@ type BlobberDetails struct {
IsShutdown bool `json:"is_shutdown"`
IsRestricted bool `json:"is_restricted"`
NotAvailable bool `json:"not_available"`
StorageVersion int64 `json:"storage_version"`
}

type Validator struct {
Expand Down
10 changes: 4 additions & 6 deletions tests/api_tests/config/api_tests_config.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
block_worker: https://dev.zus.network/dns
0box_url: http://0box.dev.devnet-0chain.net
block_worker: https://dev-st.devnet-0chain.net/dns
0box_url: http://0box.dev-st.devnet-0chain.net
0box_phone_number: +917696229925
zvault_url: http://zvault.dev.devnet-0chain.net
zauth_url: http://zauth.dev.devnet-0chain.net
default_test_case_timeout: 45s
zs3_server_url: https://dev.0chain.net/zs3server/
chimney_test_network: https://dev.zus.network/dns
zs3_server_url: https://dev-st.devnet-0chain.net/zs3server/
chimney_test_network: https://dev-st.devnet-0chain.net/dns
blobber_owner_wallet_mnemonics: "economy day fan flower between rebuild valid bid catch bargain vivid hybrid room permit check manage mean twelve damage summer close churn boat either"
owner_wallet_mnemonics: "cactus panther essence ability copper fox wise actual need cousin boat uncover ride diamond group jacket anchor current float rely tragic omit child payment"
ethereum_address: 0xD8c9156e782C68EE671C09b6b92de76C97948432
13 changes: 13 additions & 0 deletions tests/api_tests/config/sc_owner_wallet.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"client_id": "1746b06bb09f55ee01b33b5e2e055d6cc7a900cb57c0a3a5eaabb8a0e7745802",
"client_key": "7b630ba670dac2f22d43c2399b70eff378689a53ee03ea20957bb7e73df016200fea410ba5102558b0c39617e5afd2c1843b161a1dedec15e1ab40543a78a518",
"keys": [
{
"public_key": "7b630ba670dac2f22d43c2399b70eff378689a53ee03ea20957bb7e73df016200fea410ba5102558b0c39617e5afd2c1843b161a1dedec15e1ab40543a78a518",
"private_key": "c06b6f6945ba02d5a3be86b8779deca63bb636ce7e46804a479c50e53c864915"
}
],
"mnemonics": "cactus panther essence ability copper fox wise actual need cousin boat uncover ride diamond group jacket anchor current float rely tragic omit child payment",
"version": "1.0",
"date_created": "2023-11-26T01:23:58Z"
}
47 changes: 46 additions & 1 deletion tests/api_tests/main_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package api_tests

import (
"math/rand"
"encoding/json"
"log"
"os"
Expand Down Expand Up @@ -119,6 +120,46 @@ func TestMain(m *testing.M) {
os.Exit(m.Run())
}

func initialiseSCWallet() *model.Wallet {

// read the file sc_owner_wallet.json
fileContent, err := os.ReadFile("./config/sc_owner_wallet.json")
if err != nil {
log.Println("Error reading file:", err)
return nil
}

fileWallet := WalletFile{}

// Parse the JSON data into a list of strings
err = json.Unmarshal(fileContent, &fileWallet)

if err != nil {
log.Println("Error decoding JSON:", err)
return nil
}

wallet := &model.Wallet{
Id: fileWallet.ClientId,
Version: fileWallet.Version,
PublicKey: fileWallet.Keys[0].PublicKey,
Nonce: 0,
Keys: &model.KeyPair{},
Mnemonics: fileWallet.Mnemonics,
}

err = wallet.Keys.PublicKey.DeserializeHexStr(fileWallet.Keys[0].PublicKey)
if err != nil {
log.Println("Error decoding JSON:", err)
}
err = wallet.Keys.PrivateKey.DeserializeHexStr(fileWallet.Keys[0].PrivateKey)
if err != nil {
log.Println("Error decoding JSON:", err)
}

return wallet
}

func getConfigForZcnCoreInit(blockWorker string) string {
configMap := map[string]interface{}{
"block_worker": blockWorker,
Expand Down Expand Up @@ -151,8 +192,12 @@ type WalletFile struct {

func createWallet(t *test.SystemTest) *model.Wallet {
walletMutex.Lock()

// generate random number for wallet idx
walletIdx = int64(rand.Intn(len(initialisedWallets)))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Revert it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverted create wallet


wallet := initialisedWallets[walletIdx]
walletIdx++
//walletIdx++
balance := apiClient.GetWalletBalance(t, wallet, client.HttpOkStatus)
wallet.Nonce = int(balance.Nonce)
walletMutex.Unlock()
Expand Down
41 changes: 41 additions & 0 deletions tests/api_tests/register_blobber_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,47 @@ func TestRegisterBlobber(testSetup *testing.T) {
t := test.NewSystemTest(testSetup)
t.Parallel()

// write a test case to register a blobber with storage version
t.Run("Register blobber with storage version", func(t *test.SystemTest) {
wallet := createWallet(t)

walletBalance := apiClient.GetWalletBalance(t, wallet, client.HttpOkStatus)
t.Logf("wallet balance: %v", wallet)
wallet.Nonce = int(walletBalance.Nonce)

sn := &model.StorageNode{}

sn.ID = uuid.New().String()
sn.BaseURL = generateRandomURL()

sn.Capacity = 10240 * GB
sn.Terms.ReadPrice = 1000000000
sn.Terms.WritePrice = 1000000000

sn.StakePoolSettings.DelegateWallet = "config.Configuration.DelegateWallet"
sn.StakePoolSettings.NumDelegates = 2
sn.StakePoolSettings.ServiceCharge = 0.2

//todo: make check to this
sn.StorageVersion = 2
sn.ManagingWallet = wallet.Id

apiClient.RegisterBlobberWithIdVerification(t, wallet, sn, 1, wallet.Id)

var killBlobberReq = &model.KillBlobberRequest{
ProviderID: wallet.Id,
}

scWallet := initialiseSCWallet()

// get wallet balance
walletBalance = apiClient.GetWalletBalance(t, scWallet, client.HttpOkStatus)
scWallet.Nonce = int(walletBalance.Nonce)

// todo: check logic
apiClient.KillBlobber(t, scWallet, killBlobberReq, 1)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add it in defer func.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added

})

t.Run("Write price lower than min_write_price should not allow register", func(t *test.SystemTest) {
wallet := createWallet(t)

Expand Down
41 changes: 41 additions & 0 deletions tests/api_tests/update_blobber_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,47 @@ func TestUpdateBlobber(testSetup *testing.T) {

t.Parallel()

t.Run("update blobber version should work", func(t *test.SystemTest) {
wallet := createWallet(t)

blobberRequirements := model.DefaultBlobberRequirements(wallet.Id, wallet.PublicKey)
allocationBlobbers := apiClient.GetAllocationBlobbers(t, wallet, &blobberRequirements, client.HttpOkStatus)
allocationID := apiClient.CreateAllocation(t, wallet, allocationBlobbers, client.TxSuccessfulStatus)

allocation := apiClient.GetAllocation(t, allocationID, client.HttpOkStatus)

blobberID := getFirstUsedStorageNodeID(allocationBlobbers.Blobbers, allocation.Blobbers)
require.NotZero(t, blobberID)

blobber := apiClient.GetBlobber(t, blobberID, client.HttpOkStatus)
require.NotEqual(t, wallet.Id, blobber.StakePoolSettings.DelegateWallet)

blobber.StorageVersion = 2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Set to 1. We only have 0 and 1 for now.


apiClient.UpdateBlobber(t, wallet, blobber, client.TxUnsuccessfulStatus)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add getBlobber to verify version is added properly.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added get blopbber to verify the version

})

t.Run("update blobber: degrade version should not work", func(t *test.SystemTest) {
wallet := createWallet(t)

blobberRequirements := model.DefaultBlobberRequirements(wallet.Id, wallet.PublicKey)
allocationBlobbers := apiClient.GetAllocationBlobbers(t, wallet, &blobberRequirements, client.HttpOkStatus)

allocationID := apiClient.CreateAllocation(t, wallet, allocationBlobbers, client.TxSuccessfulStatus)

allocation := apiClient.GetAllocation(t, allocationID, client.HttpOkStatus)

blobberID := getFirstUsedStorageNodeID(allocationBlobbers.Blobbers, allocation.Blobbers)
require.NotZero(t, blobberID)

blobber := apiClient.GetBlobber(t, blobberID, client.HttpOkStatus)
require.NotEqual(t, wallet.Id, blobber.StakePoolSettings.DelegateWallet)

blobber.StorageVersion = 1
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Set to 0. We only have 0 and 1 for now.


apiClient.UpdateBlobber(t, wallet, blobber, client.TxUnsuccessfulStatus)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added get blobber to verify the version

})

t.Run("Update blobber in allocation without correct delegated client, shouldn't work", func(t *test.SystemTest) {
wallet := createWallet(t)

Expand Down
Loading