Skip to content

Commit

Permalink
clarify configs
Browse files Browse the repository at this point in the history
  • Loading branch information
pivilartisant committed Aug 6, 2024
1 parent 9de00c5 commit 15752a9
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 102 deletions.
55 changes: 31 additions & 24 deletions cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
pkgConfig "github.com/massalabs/DeWeb/pkg/config"
"github.com/massalabs/DeWeb/pkg/webmanager"
"github.com/massalabs/DeWeb/pkg/website"
msConfig "github.com/massalabs/station/int/config"
"github.com/massalabs/station/pkg/logger"
"github.com/urfave/cli/v2"
)
Expand All @@ -35,20 +36,24 @@ func main() {
Name: "upload",
Aliases: []string{"u"},
Usage: "Upload a website",
ArgsUsage: "<wallet nickname> <website zip file path>",
ArgsUsage: "<website zip file path>",
Action: func(cCtx *cli.Context) error {
if cCtx.Args().Len() < 2 {
if cCtx.Args().Len() < 1 {
return fmt.Errorf("invalid number of arguments\nUsage: %s %s", cCtx.App.Name, cCtx.Command.ArgsUsage)
}

filepath := cCtx.Args().Get(1)
filepath := cCtx.Args().Get(0)
if !zipper.IsValidZipFile(filepath) {
return fmt.Errorf("invalid zip file: %s", filepath)
}

config := pkgConfig.DefaultConfig(cCtx.Args().Get(0), "https://buildnet.massa.net/api/v2")
walletConfig := pkgConfig.DefaultWalletConfig()
networkInfos := pkgConfig.DefaultNetworkConfig()
scConfig := pkgConfig.DefaultScConfig()

fmt.Printf("networkInfos: %+v\n", networkInfos)

siteAddress, err := deployWebsite(config, filepath)
siteAddress, err := deployWebsite(walletConfig.WalletNickname, networkInfos, scConfig, filepath)
if err != nil {
logger.Fatalf("failed to deploy website: %v", err)
}
Expand All @@ -68,7 +73,7 @@ func main() {
return fmt.Errorf("invalid number of arguments\nUsage: %s %s", cCtx.App.Name, cCtx.Command.ArgsUsage)
}

config := pkgConfig.DefaultConfig(cCtx.Args().Get(0), "https://buildnet.massa.net/api/v2")
scConfig := pkgConfig.DefaultScConfig()
siteAddress := cCtx.Args().Get(1)
filepath := cCtx.Args().Get(2)

Expand All @@ -81,7 +86,7 @@ func main() {
logger.Fatalf("failed to process file for upload: %v", err)
}

err = uploadChunks(bytecode, siteAddress, config)
err = uploadChunks(bytecode, siteAddress, scConfig)
if err != nil {
logger.Fatalf("failed to upload chunks: %v", err)
}
Expand All @@ -101,10 +106,10 @@ func main() {
return fmt.Errorf("invalid number of arguments\nUsage: %s %s", cCtx.App.Name, cCtx.Command.ArgsUsage)
}

config := pkgConfig.DefaultConfig(cCtx.Args().Get(0), "https://buildnet.massa.net/api/v2")
networkInfos := pkgConfig.DefaultNetworkConfig()
siteAddress := cCtx.Args().Get(1)

err := viewWebsite(siteAddress, config)
err := viewWebsite(siteAddress, networkInfos)
if err != nil {
logger.Fatalf("An error occured while attempting to view website %s: %v", siteAddress, err)
}
Expand All @@ -122,10 +127,12 @@ func main() {
return fmt.Errorf("invalid number of arguments\nUsage: %s %s", cCtx.App.Name, cCtx.Command.ArgsUsage)
}

config := pkgConfig.DefaultConfig(cCtx.Args().Get(0), "https://buildnet.massa.net/api/v2")
scConfig := pkgConfig.DefaultScConfig()
WalletConfig := pkgConfig.DefaultWalletConfig()
networkInfos := pkgConfig.DefaultNetworkConfig()
siteAddress := cCtx.Args().Get(1)

err := deleteWebsite(siteAddress, config)
err := deleteWebsite(siteAddress, scConfig, WalletConfig, networkInfos)
if err != nil {
logger.Fatalf("An error occured while attempting to delete website %s: %v", siteAddress, err)
}
Expand All @@ -146,10 +153,10 @@ func main() {
}
}

func deployWebsite(config *pkgConfig.Config, filepath string) (string, error) {
logger.Debugf("Deploying website contract with config: %+v", config)
func deployWebsite(walletNickname string, networkInfos *msConfig.NetworkInfos, scConfig *pkgConfig.ScConfig, filepath string) (string, error) {
logger.Debugf("Deploying website contract with config: %+v", scConfig)

deploymentResult, err := website.Deploy(config)
deploymentResult, err := website.Deploy(walletNickname, networkInfos, scConfig)
if err != nil {
return "", fmt.Errorf("failed to deploy website contract: %v", err)
}
Expand All @@ -163,19 +170,19 @@ func deployWebsite(config *pkgConfig.Config, filepath string) (string, error) {

logger.Debugf("Uploading %d chunks to website at address: %s", len(chunks), deploymentResult.Address)

err = uploadChunks(chunks, deploymentResult.Address, config)
err = uploadChunks(chunks, deploymentResult.Address, scConfig)
if err != nil {
return "", fmt.Errorf("failed to upload chunks: %v", err)
}

return deploymentResult.Address, nil
}

func uploadChunks(chunks [][]byte, address string, config *pkgConfig.Config) error {
func uploadChunks(chunks [][]byte, address string, scConfig *pkgConfig.ScConfig) error {
for i, chunk := range chunks {
logger.Debugf("Uploading chunk %d with size: %d", i, len(chunk))

operationID, err := website.UploadChunk(address, config, chunk, i)
operationID, err := website.UploadChunk(address, scConfig, chunk, i)
if err != nil {
return fmt.Errorf("failed to upload chunk %d: %v", i, err)
}
Expand All @@ -195,25 +202,25 @@ func processFileForUpload(filepath string) ([][]byte, error) {
return website.DivideIntoChunks(websiteBytes, website.ChunkSize), nil
}

func viewWebsite(scAddress string, config *pkgConfig.Config) error {
owner, err := website.GetOwner(&config.NetworkInfos, scAddress)
func viewWebsite(scAddress string, networkInfos *msConfig.NetworkInfos) error {
owner, err := website.GetOwner(networkInfos, scAddress)
if err != nil {
logger.Warnf("failed to get owner of %s: %v", scAddress, err)
}

logger.Infof("Website owner: %s", owner)

zipFile, err := webmanager.RequestWebsite(scAddress, &config.NetworkInfos)
zipFile, err := webmanager.RequestWebsite(scAddress, networkInfos)
if err != nil {
return fmt.Errorf("failed to request website: %v", err)
}

firstCreationTimestamp, err := website.GetFirstCreationTimestamp(&config.NetworkInfos, scAddress)
firstCreationTimestamp, err := website.GetFirstCreationTimestamp(networkInfos, scAddress)
if err != nil {
logger.Warnf("failed to get first creation timestamp of %s: %v", scAddress, err)
}

lastUpdateTimestamp, err := website.GetLastUpdateTimestamp(&config.NetworkInfos, scAddress)
lastUpdateTimestamp, err := website.GetLastUpdateTimestamp(networkInfos, scAddress)
if err != nil {
logger.Warnf("failed to get last update timestamp of %s: %v", scAddress, err)
}
Expand All @@ -232,8 +239,8 @@ func viewWebsite(scAddress string, config *pkgConfig.Config) error {
return nil
}

func deleteWebsite(siteAddress string, config *pkgConfig.Config) error {
operationID, err := website.Delete(config, siteAddress)
func deleteWebsite(siteAddress string, scConfig *pkgConfig.ScConfig, walletConfig *pkgConfig.WalletConfig, networkInfos *msConfig.NetworkInfos) error {
operationID, err := website.Delete(scConfig, walletConfig, networkInfos, siteAddress)
if err != nil {
return fmt.Errorf("error while deleting website %s: %v", siteAddress, err)
}
Expand Down
9 changes: 5 additions & 4 deletions int/api/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@ type ServerConfig struct {
}

func DefaultConfig() *ServerConfig {
nodeConf := pkgConfig.DefaultConfig("", DefaultNetworkNodeURL)
// unsure if this will work
networkInfos := pkgConfig.DefaultNetworkConfig()

return &ServerConfig{
Domain: DefaultDomain,
APIPort: DefaultAPIPort,
NetworkInfos: nodeConf.NetworkInfos,
NetworkInfos: *networkInfos,
AllowList: []string{},
BlockList: []string{},
}
Expand Down Expand Up @@ -79,12 +80,12 @@ func LoadServerConfig(configPath string) (*ServerConfig, error) {
yamlConf.APIPort = DefaultAPIPort
}

nodeConf := pkgConfig.DefaultConfig("", yamlConf.NetworkNodeURL)
networkInfos := pkgConfig.DefaultNetworkConfig()

return &ServerConfig{
Domain: yamlConf.Domain,
APIPort: yamlConf.APIPort,
NetworkInfos: nodeConf.NetworkInfos,
NetworkInfos: *networkInfos,
AllowList: yamlConf.AllowList,
BlockList: yamlConf.BlockList,
}, nil
Expand Down
156 changes: 104 additions & 52 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
package config

import (
"fmt"
"os"

dewebUtils "github.com/massalabs/DeWeb/int/utils"
"github.com/massalabs/station-massa-wallet/pkg/utils"
msConfig "github.com/massalabs/station/int/config"
"github.com/massalabs/station/pkg/logger"
"github.com/massalabs/station/pkg/node"
"gopkg.in/yaml.v2"
)

// Wallet config

// Call function config

// Deploy function Config
const (
defaultMinimalFees = uint64(100_000_000)
defaultMaxGas = uint64(3_980_167_295)
Expand All @@ -18,86 +28,128 @@ const (

MainnetChainID = 77658377
BuildnetChainID = 77658366
NoCoins = uint64(0)

DefaultNodeURL = "https://buildnet.massa.net/api/v2"

NoCoins = uint64(0)
)

type Config struct {
type ScConfig struct {
MinimalFees uint64
MaxGas uint64
MaxCoins uint64
Expiry uint64
}

type yamlWalletConfig struct {
WalletNickname string `yaml:"wallet_nickname"`
NodeUrl string `yaml:"node_url"`
}

type WalletConfig struct {
WalletNickname string
MinimalFees uint64
MaxGas uint64
MaxCoins uint64
Expiry uint64
NetworkInfos msConfig.NetworkInfos
NodeUrl string
}

// DefaultConfig returns a new Config with default values.
func DefaultConfig(walletNickname string, nodeURL string) *Config {
return NewConfig(walletNickname, defaultMaxGas, defaultMaxCoins, defaultExpiry, nodeURL)
// TODO: Change default node url to mainnet
// TODO: Replace by first of list in MW
func DefaultWalletConfig() *WalletConfig {
return &WalletConfig{
WalletNickname: "faker1",
NodeUrl: DefaultNetworkConfig().NodeURL,
}
}

// NewConfig returns a new Config with information returned by the given node.
func NewConfig(walletNickname string, maxGas, maxCoins, expiry uint64, nodeURL string) *Config {
client := node.NewClient(nodeURL)
func DefaultNetworkConfig() *msConfig.NetworkInfos {
networkManager, err := msConfig.NewNetworkManager()
if err != nil {
logger.Fatalf("failed to create network manager: %v", err)
}

minimalFees := defaultMinimalFees
chainID := uint64(0)
networkName := "unknown"
nodeVersion := "unknown"
networkManager.SwitchNetwork(buildnetName)

Check failure on line 69 in pkg/config/config.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `networkManager.SwitchNetwork` is not checked (errcheck)

client := node.NewClient(networkManager.Network().NodeURL)

status, err := node.Status(client)
if err != nil {
logger.Errorf("unable to get node status: %v", err)
logger.Warnf("Using default values for minimal fees, chain ID, and network version")
} else {
minimalFees = getMinimalFees(status)
chainID, networkName = getChainIDAndNetworkName(status)
nodeVersion = getNodeVersion(status)
}

return &Config{
WalletNickname: walletNickname,
MinimalFees: minimalFees,
MaxGas: maxGas,
MaxCoins: maxCoins,
Expiry: expiry,
NetworkInfos: msConfig.NetworkInfos{
Network: networkName,
NodeURL: nodeURL,
Version: nodeVersion,
ChainID: chainID,
},
return &msConfig.NetworkInfos{
Network: networkManager.Network().Network,
NodeURL: networkManager.Network().NodeURL,
Version: networkManager.Network().Version,
ChainID: getChainID(status),
}
}

func getNodeVersion(status *node.State) string {
nodeVersion := "unknown"
// Returns a new Config for call sc's
func DefaultScConfig() *ScConfig {
client := node.NewClient(DefaultNetworkConfig().NodeURL)
var minimalFees uint64
status, err := node.Status(client)

if status.Version != nil {
nodeVersion = *status.Version
} else {
logger.Warnf("Node version is nil, setting '%s' as network version", nodeVersion)
if err != nil {

Check failure on line 92 in pkg/config/config.go

View workflow job for this annotation

GitHub Actions / lint

if statements that check an error must be cuddled with the statement that assigned the error (wsl)
logger.Errorf("unable to get node status: %v", err)
}

return nodeVersion
minimalFees = getMinimalFees(status)

return &ScConfig{
MinimalFees: minimalFees,
MaxGas: defaultMaxGas,
MaxCoins: defaultMaxCoins,
Expiry: defaultExpiry,
}
}

// Load yaml wallet config
func LoadYamlWalletConfig(configPath string) (*WalletConfig, error) {
if configPath == "" {
return nil, fmt.Errorf("config path is empty")
}

if _, err := os.Stat(configPath); os.IsNotExist(err) {
return DefaultWalletConfig(), nil
}

filebytes, err := dewebUtils.ReadFileBytes(configPath)
if err != nil {
return nil, fmt.Errorf("failed to read file bytes: %w", err)
}

var yamlConf yamlWalletConfig

err = yaml.Unmarshal(filebytes, &yamlConf)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal YAML data: %w", err)
}

// Set default values if not specified in the YAML file
if yamlConf.WalletNickname == "" {
yamlConf.WalletNickname = ""
}

if yamlConf.NodeUrl == "" {
yamlConf.NodeUrl = DefaultNodeURL
}

return &WalletConfig{
WalletNickname: yamlConf.WalletNickname,
NodeUrl: yamlConf.NodeUrl,
}, nil
}

func getChainIDAndNetworkName(status *node.State) (uint64, string) {
func getChainID(status *node.State) uint64 {
chainID := uint64(0)
networkName := "unknown"

if status.ChainID != nil {
chainID = uint64(*status.ChainID)
switch chainID {
case MainnetChainID:
networkName = mainnetName
case BuildnetChainID:
networkName = buildnetName
default:
logger.Warnf("Unknown chain ID: %d", chainID)
}
} else {
logger.Warnf("Unknown chain ID: %d", chainID)
}

return chainID, networkName
return chainID
}

func getMinimalFees(status *node.State) uint64 {
Expand Down
Loading

0 comments on commit 15752a9

Please sign in to comment.