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 27c8b05
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 103 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
11 changes: 6 additions & 5 deletions int/api/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,19 @@ type yamlServerConfig struct {
type ServerConfig struct {
Domain string
APIPort int
NetworkInfos msConfig.NetworkInfos
NetworkInfos *msConfig.NetworkInfos
AllowList []string
BlockList []string
}

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
Loading

0 comments on commit 27c8b05

Please sign in to comment.