Skip to content

Commit

Permalink
Small fixes (#71)
Browse files Browse the repository at this point in the history
* Fix a small security concern in box

* Update websiteDeployer.wasm with upgradeSC function

* Compute chunk upload cost with lastUpdate key storage cost

* Improve nil handling when fetching chunks and timestamps from website SC

* Fix edit with less chunks
  • Loading branch information
thomas-senechal authored Aug 12, 2024
1 parent 11087e4 commit 40b21f8
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 7 deletions.
32 changes: 31 additions & 1 deletion cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/massalabs/DeWeb/pkg/website"
msConfig "github.com/massalabs/station/int/config"
"github.com/massalabs/station/pkg/logger"
"github.com/massalabs/station/pkg/node"
"github.com/urfave/cli/v2"
)

Expand Down Expand Up @@ -123,6 +124,12 @@ func main() {
logger.Fatalf("failed to process file for upload: %v", err)
}

err = deleteIfRequired(config, siteAddress, len(bytecode))
if err != nil {
logger.Errorf("failed to check if delete is required: %v", err)
logger.Warnf("continuing with edit operation for website %s", siteAddress)
}

err = uploadChunks(bytecode, siteAddress, config)
if err != nil {
logger.Fatalf("failed to upload chunks: %v", err)
Expand Down Expand Up @@ -221,14 +228,37 @@ func deployWebsite(config *yamlConfig.Config, filepath string) (string, error) {

func uploadChunks(chunks [][]byte, address string, config *yamlConfig.Config) error {
for i, chunk := range chunks {
logger.Infof("Uploading chunk %d out of %d...", i+1, len(chunks))
logger.Debugf("Uploading chunk %d with size: %d", i, len(chunk))

operationID, err := website.UploadChunk(address, config.WalletConfig, &config.NetworkConfig, &config.SCConfig, chunk, i)
if err != nil {
return fmt.Errorf("failed to upload chunk %d: %v", i, err)
}

logger.Infof("Chunk %d out of %d uploaded with operation ID: %s", i, len(chunks), operationID)
logger.Infof("Chunk %d out of %d uploaded with operation ID: %s", i+1, len(chunks), operationID)
}

return nil
}

// deleteIfRequired checks if the website has more chunks than the new website
func deleteIfRequired(config *yamlConfig.Config, siteAddress string, editChunksNbr int) error {
client := node.NewClient(config.NetworkConfig.NodeURL)

deployedChunks, err := website.GetNumberOfChunks(client, siteAddress)
if err != nil {
return fmt.Errorf("failed to get number of chunks for website %s: %v", siteAddress, err)
}

logger.Debugf("Website %s has %d deployed chunks", siteAddress, deployedChunks)

if deployedChunks > int32(editChunksNbr) {
logger.Infof("Website %s has more chunks than the new website, deleting and redeploying", siteAddress)

if err = deleteWebsite(siteAddress, config); err != nil {
return fmt.Errorf("failed to delete website %s: %v", siteAddress, err)
}
}

return nil
Expand Down
13 changes: 10 additions & 3 deletions int/api/box.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import (
pkgConfig "github.com/massalabs/DeWeb/pkg/config"
)

var networkDocURL = "https://docs.massa.net/docs/build/networks-faucets/public-networks"
const (
UnknownNetwork = "Unknown"
networkDocURL = "https://docs.massa.net/docs/build/networks-faucets/public-networks"
)

//go:embed massa_logomark.svg
var massaLogomark []byte
Expand Down Expand Up @@ -84,8 +87,10 @@ func getChainName(chainID uint64) string {
switch chainID {
case pkgConfig.BuildnetChainID:
return pkgConfig.BuildnetName
default:
case pkgConfig.MainnetChainID:
return pkgConfig.MainnetName
default:
return UnknownNetwork
}
}

Expand All @@ -94,7 +99,9 @@ func getChainDocURL(chainID uint64) string {
switch chainID {
case pkgConfig.BuildnetChainID:
return networkDocURL + "#buildnet"
default:
case pkgConfig.MainnetChainID:
return networkDocURL + "#mainnet"
default:
return networkDocURL
}
}
7 changes: 7 additions & 0 deletions pkg/website/chunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ func ComputeChunkCost(chunkIndex int, chunkSize int) (int, error) {
}

uploadCost += chunkKeyCost

lastUpdateKeyCost, err := sendoperation.StorageCostForEntry(len([]byte(lastUpdateTimestampKey)), convert.BytesPerUint64)
if err != nil {
return 0, fmt.Errorf("unable to compute storage cost for lastUpdate key creation: %w", err)
}

uploadCost += lastUpdateKeyCost
}

return uploadCost, nil
Expand Down
18 changes: 15 additions & 3 deletions pkg/website/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
func Fetch(network *config.NetworkInfos, websiteAddress string) ([]byte, error) {
client := node.NewClient(network.NodeURL)

chunkNumber, err := getNumberOfChunks(client, websiteAddress)
chunkNumber, err := GetNumberOfChunks(client, websiteAddress)
if err != nil {
return nil, fmt.Errorf("fetching number of chunks: %w", err)
}
Expand All @@ -25,13 +25,17 @@ func Fetch(network *config.NetworkInfos, websiteAddress string) ([]byte, error)
return dataStore, nil
}

// getNumberOfChunks fetches and returns the number of chunks for the website.
func getNumberOfChunks(client *node.Client, websiteAddress string) (int32, error) {
// GetNumberOfChunks fetches and returns the number of chunks for the website.
func GetNumberOfChunks(client *node.Client, websiteAddress string) (int32, error) {
nbChunkResponse, err := node.FetchDatastoreEntry(client, websiteAddress, convert.ToBytes(nbChunkKey))
if err != nil {
return 0, fmt.Errorf("fetching website number of chunks: %w", err)
}

if nbChunkResponse.FinalValue == nil {
return 0, nil
}

chunkNumber, err := convert.BytesToI32(nbChunkResponse.FinalValue)
if err != nil {
return 0, fmt.Errorf("converting fetched data for key '%s': %w", nbChunkKey, err)
Expand Down Expand Up @@ -84,6 +88,10 @@ func GetFirstCreationTimestamp(network *config.NetworkInfos, websiteAddress stri
return 0, fmt.Errorf("fetching website first creation timestamp: %w", err)
}

if firstCreationTimestampResponse.FinalValue == nil {
return 0, nil
}

castedFCTimestamp, err := convert.BytesToU64(firstCreationTimestampResponse.FinalValue)
if err != nil {
return 0, fmt.Errorf("converting website first creation timestamp: %w", err)
Expand All @@ -100,6 +108,10 @@ func GetLastUpdateTimestamp(network *config.NetworkInfos, websiteAddress string)
return 0, fmt.Errorf("fetching website last update timestamp: %w", err)
}

if lastUpdateTimestampResponse.FinalValue == nil {
return 0, nil
}

castedLUTimestamp, err := convert.BytesToU64(lastUpdateTimestampResponse.FinalValue)
if err != nil {
return 0, fmt.Errorf("converting website last update timestamp: %w", err)
Expand Down
Binary file modified pkg/website/sc/websiteDeployer.wasm
Binary file not shown.

0 comments on commit 40b21f8

Please sign in to comment.