Skip to content

Commit

Permalink
compute network specific storage costs
Browse files Browse the repository at this point in the history
  • Loading branch information
peterjah committed Oct 19, 2023
1 parent 3a4bbbc commit e665edf
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 64 deletions.
4 changes: 3 additions & 1 deletion api/test/robot_tests/cmd/cmd.robot
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ Suite Setup Suite Setup
*** Test Cases ***
POST a Smart Contract
${sc}= Get File For Streaming Upload ${CURDIR}/../../testSC/build/main-testSC.wasm
${data}= Create Dictionary walletNickname=${WALLET_NICKNAME}
${data}= Create Dictionary
... walletNickname=${WALLET_NICKNAME}
... coins=3000000000
${file}= Create Dictionary smartContract=${sc}
${response}= POST ${API_URL}/cmd/deploySC data=${data} files=${file} expected_status=any
Log To Console json response: ${response.json()} # Print the response content to the test log for debugging
Expand Down
4 changes: 3 additions & 1 deletion api/test/robot_tests/event_manager/keywords.resource
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ Suite Setup

Deploy testSC
${sc}= Get File For Streaming Upload ${CURDIR}/../../testSC/build/main-testSC.wasm
${data}= Create Dictionary walletNickname=${WALLET_NICKNAME}
${data}= Create Dictionary
... walletNickname=${WALLET_NICKNAME}
... coins=3000000000
${file}= Create Dictionary smartContract=${sc}
${response}= POST ${API_URL}/cmd/deploySC data=${data} files=${file} expected_status=${STATUS_OK}
Should Contain ${response.json()['firstEvent']['data']} TestSC is deployed at :
Expand Down
1 change: 0 additions & 1 deletion int/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ func NewServer(flags StartServerFlags) *Server {
// Starts the server.
// This function starts the server in a new goroutine to avoid blocking the main thread.
func (server *Server) Start(networkManager *config.NetworkManager, pluginManager *plugin.Manager) {

initLocalAPI(server.localAPI, networkManager, pluginManager)
server.api.ConfigureMassaStationAPI(*networkManager.Network(), server.shutdown)

Expand Down
46 changes: 24 additions & 22 deletions int/config/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,25 +78,6 @@ func NewNetworkManager() (*NetworkManager, error) {
return networkManager, nil
}

func (n *NetworkManager) Version(nodeUrl string) (*string, error) {
client := node.NewClient(nodeUrl)
status, err := node.Status(client)
if err != nil {
return nil, fmt.Errorf("failed to retrive node status: %w", err)
}

pattern := `.+\.(\d+\.\d+)`

re := regexp.MustCompile(pattern)
matches := re.FindStringSubmatch(*status.Version)

if len(matches) != 2 {
return nil, fmt.Errorf("failed to parse node version from: %s", *status.Version)
}

return &matches[1], nil
}

// SetNetworks sets the known networks for the NetworkManager.
func (n *NetworkManager) SetNetworks(networks map[string]NetworkConfig) {
n.knownNetworks = networks
Expand Down Expand Up @@ -155,6 +136,27 @@ func (n *NetworkManager) Network() *NetworkInfos {
return &n.networkInfos
}

func (n *NetworkManager) version(nodeURL string) (string, error) {
client := node.NewClient(nodeURL)

status, err := node.Status(client)
if err != nil {
return "", fmt.Errorf("failed to retrieve node status: %w", err)
}

pattern := `.+\.(\d+\.\d+)`

re := regexp.MustCompile(pattern)
matches := re.FindStringSubmatch(*status.Version)

//nolint:gomnd
if len(matches) != 2 {
return "", fmt.Errorf("failed to parse node version from: %s", *status.Version)
}

return matches[1], nil
}

// SwitchNetwork switches the current network configuration to the specified network.
// selectedNetworkStr: The name of the network configuration to switch to.
// Returns any error encountered during the switch operation.
Expand All @@ -164,7 +166,7 @@ func (n *NetworkManager) SwitchNetwork(selectedNetworkStr string) error {
return fmt.Errorf("unknown network option: %s", selectedNetworkStr)
}

version, err := n.Version(cfg.URLs[0])
version, err := n.version(cfg.URLs[0])
if err != nil {
return fmt.Errorf("getting network version: %w", err)
}
Expand All @@ -173,10 +175,10 @@ func (n *NetworkManager) SwitchNetwork(selectedNetworkStr string) error {
NodeURL: cfg.URLs[0],
DNSAddress: cfg.DNS,
Network: selectedNetworkStr,
Version: *version,
Version: version,
})

logger.Debugf("Set current network: %s (version %s)\n", selectedNetworkStr, *version)
logger.Debugf("Set current network: %s (version %s)\n", selectedNetworkStr, version)
logger.Debugf("Network config: %+v\n", n.Network())

return nil
Expand Down
7 changes: 4 additions & 3 deletions pkg/convert/byteConverter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import (
"unicode/utf16"
)

const BytesPerUint64 = 8
const (
BytesPerUint64 = 8
BytesPerUint32 = 4
)

// Encode uint64 to byte array.
func U64ToBytes(nb int) (bytes []byte) {
Expand All @@ -18,8 +21,6 @@ func U64ToBytes(nb int) (bytes []byte) {
return
}

const BytesPerUint32 = 4

// Encode uint32 to byte array.
func U32ToBytes(nb int) (bytes []byte) {
u32 := uint32(nb)
Expand Down
52 changes: 41 additions & 11 deletions pkg/node/sendoperation/sendoperation.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
b64 "encoding/base64"
"encoding/binary"
"fmt"
"strconv"
"strings"

"github.com/massalabs/station/pkg/node"
Expand All @@ -14,17 +15,12 @@ import (
)

const (
DefaultGasLimit = 700_000_000
DefaultExpiryInSlot = 3
DefaultFee = 0
// To be updated when storage costs reduction will be deployed (testnet 26?)
AccountCreationStorageCost = 10_000_000
StorageCostPerByte = 1_000_000
// Today, the cost of storage to create a new storagekey is fixed to 10*StorageCostPerByte
// This should be updated when key creation cost will be charged for actual size.
chargedBytesForKey = 10
StorageKeyCreationCost = chargedBytesForKey * StorageCostPerByte
OneMassa = 1_000_000_000
DefaultGasLimit = 700_000_000
DefaultExpiryInSlot = 3
DefaultFee = 0
accountCreationStorageCost = 1_000_000
StorageCostPerByte = 100_000
OneMassa = 1_000_000_000
)

//nolint:tagliatelle
Expand Down Expand Up @@ -223,3 +219,37 @@ func DecodeOperationID(data []byte) (uint64, error) {

return opID, nil
}

func StorageCostForEntry(nodeVersion string, keyByteLengh, valueByteLenght int) (int, error) {
versionFloat, err := strconv.ParseFloat(nodeVersion, 64)
if err != nil {
return 0, fmt.Errorf("failed to parse nodeversion %s: %w", nodeVersion, err)
}

Check warning on line 227 in pkg/node/sendoperation/sendoperation.go

View check run for this annotation

Codecov / codecov/patch

pkg/node/sendoperation/sendoperation.go#L223-L227

Added lines #L223 - L227 were not covered by tests

//nolint:gomnd
if versionFloat < 26 {
lecagyStorageCost := 1_000_000
// key bytes are charged at the fixed price of 10 bytes
//nolint:gomnd
return (valueByteLenght + 10) * lecagyStorageCost, nil
}

Check warning on line 235 in pkg/node/sendoperation/sendoperation.go

View check run for this annotation

Codecov / codecov/patch

pkg/node/sendoperation/sendoperation.go#L230-L235

Added lines #L230 - L235 were not covered by tests

return (valueByteLenght + keyByteLengh) * StorageCostPerByte, nil

Check warning on line 237 in pkg/node/sendoperation/sendoperation.go

View check run for this annotation

Codecov / codecov/patch

pkg/node/sendoperation/sendoperation.go#L237

Added line #L237 was not covered by tests
}

func AccountCreationStorageCost(nodeVersion string) (int, error) {
versionFloat, err := strconv.ParseFloat(nodeVersion, 64)
if err != nil {
return 0, fmt.Errorf("failed to parse nodeversion %s: %w", nodeVersion, err)
}

Check warning on line 244 in pkg/node/sendoperation/sendoperation.go

View check run for this annotation

Codecov / codecov/patch

pkg/node/sendoperation/sendoperation.go#L240-L244

Added lines #L240 - L244 were not covered by tests

//nolint:gomnd
if versionFloat < 26 {
// current version is lower than 0.26.0
lecacyAccountCreationStorageCost := 10_000_000

return lecacyAccountCreationStorageCost, nil
}

Check warning on line 252 in pkg/node/sendoperation/sendoperation.go

View check run for this annotation

Codecov / codecov/patch

pkg/node/sendoperation/sendoperation.go#L247-L252

Added lines #L247 - L252 were not covered by tests

return accountCreationStorageCost, nil

Check warning on line 254 in pkg/node/sendoperation/sendoperation.go

View check run for this annotation

Codecov / codecov/patch

pkg/node/sendoperation/sendoperation.go#L254

Added line #L254 was not covered by tests
}
Binary file added pkg/onchain/website/sc/websiteDeployer_v26.wasm
Binary file not shown.
Binary file added pkg/onchain/website/sc/websiteStorer_v26.wasm
Binary file not shown.
Loading

0 comments on commit e665edf

Please sign in to comment.