Skip to content

Commit

Permalink
Massa v27 compatible (#1274)
Browse files Browse the repository at this point in the history
* 1261 updagre get filtered sc output events for v27 (#1262)

* fix variable typo

* fix order of event parameter

* typos

* change op type id type

* update max gas to v27 values

* web-on-chain: improve and adjust for v27 (#1264)

* web-on-chain: reduce chunk size to 78000

* web-on-chain: add description

* roll back DefaultGasLimit

* rollback chunk size

* increase default max_gas for execute SC

* update task file

* clean code: remove if statements for v<26
  • Loading branch information
Thykof authored Nov 21, 2023
1 parent 1a8610a commit 8ed54ff
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 58 deletions.
2 changes: 1 addition & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ tasks:
- cmd: gofumpt -l -w .
- cmd: gci write .
- task: fmt-web
- cmd: golangci-lint run
- cmd: golangci-lint run --fix

fmt-web:
dir: web/massastation
Expand Down
4 changes: 2 additions & 2 deletions api/swagger/server/restapi/resource/swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ paths:
#$ref: "#/definitions/Amount"
description: Set the coin amount that will be transferred to the smartContract.
type: number # This is kept for backward compatibility
default: 0 # DefaultGasLimit
default: 0
expiry:
description: Set the expiry duration (in number of slots) of the transaction.
type: integer
Expand Down Expand Up @@ -143,7 +143,7 @@ paths:
format: uint64
minimum: 0
description: Maximum number of gas unit that a node will be able to consume.
default: 700000000
default: 1000000000 # DefaultGasLimit
- in: formData
name : coins
type: integer
Expand Down
1 change: 1 addition & 0 deletions int/api/cmd/deploySC.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func (d *deploySC) Handle(params operations.CmdDeploySCParams) middleware.Respon
decodedDatastore,
sendoperation.OperationBatch{NewBatch: false, CorrelationID: ""},
&signer.WalletPlugin{},
"",
)
if err != nil {
return operations.NewCmdDeploySCInternalServerError().
Expand Down
3 changes: 2 additions & 1 deletion int/api/cmd/execute_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (e *executeFunction) Handle(params operations.CmdExecuteFunctionParams) mid
}

// convert maxGas to uint64
maxGas := uint64(sendOperation.DefaultGasLimit)
maxGas := uint64(sendOperation.DefaultGasLimitCallSC)

if string(params.Body.MaxGas) != "" {
parsedMaxGas, err := strconv.ParseUint(string(params.Body.MaxGas), 10, 64)
Expand Down Expand Up @@ -93,6 +93,7 @@ func (e *executeFunction) Handle(params operations.CmdExecuteFunctionParams) mid
asyncReq,
sendOperation.OperationBatch{NewBatch: false, CorrelationID: ""},
&signer.WalletPlugin{},
"",
)
if err != nil {
return operations.NewCmdExecuteFunctionInternalServerError().WithPayload(
Expand Down
2 changes: 1 addition & 1 deletion int/api/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (h *eventListener) Handle(params operations.EventsGetterParams) middleware.
var event *node.Event

for {
events, err := node.ListenEvents(client, &slotStart, nil, &params.Caller, nil, nil, false)
events, err := node.ListenEvents(client, &slotStart, nil, nil, nil, &params.Caller, false)
if err != nil {
return operations.NewEventsGetterInternalServerError().
WithPayload(
Expand Down
6 changes: 3 additions & 3 deletions pkg/node/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const (
* All these criterion are optional.
*/
func Events(client *Client, start *Slot, end *Slot,
emitter *string, originaCaller *string,
emitter *string, originalCaller *string,
operationID *string,
) ([]Event, error) {
rawResponse, err := client.RPCClient.Call(
Expand All @@ -61,7 +61,7 @@ func Events(client *Client, start *Slot, end *Slot,
Start: start,
End: end,
EmitterAddress: emitter,
OriginalCallerAddress: originaCaller,
OriginalCallerAddress: originalCaller,
OriginalOperationID: operationID,
},
},
Expand All @@ -73,7 +73,7 @@ func Events(client *Client, start *Slot, end *Slot,
Start: start,
End: end,
EmitterAddress: emitter,
OriginalCallerAddress: originaCaller,
OriginalCallerAddress: originalCaller,
OriginalOperationID: operationID,
},
}, err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/node/sendoperation/callsc/callsc.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

const (
OpType = uint64(4)
OpType = 4
)

type OperationDetails struct {
Expand Down
40 changes: 9 additions & 31 deletions pkg/node/sendoperation/sendoperation.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
b64 "encoding/base64"
"encoding/binary"
"fmt"
"strconv"
"strings"

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

const (
DefaultGasLimit = 700_000_000
DefaultGasLimitExecuteSC = 1_000_000_000
DefaultGasLimitCallSC = 700_000_000
DefaultExpiryInSlot = 3
DefaultFee = 0
accountCreationStorageCost = 1_000_000
Expand Down Expand Up @@ -68,6 +68,7 @@ func Call(client *node.Client,
nickname string,
operationBatch OperationBatch,
signer signer.Signer,
description string,
) (*OperationResponse, error) {
msg, msgB64, err := MakeOperation(client, expiry, fee, operation)
if err != nil {
Expand All @@ -79,16 +80,19 @@ func Call(client *node.Client,
switch {
case operationBatch.NewBatch:
content = `{
"description": "` + description + `",
"operation": "` + msgB64 + `",
"batch": true
}`
case operationBatch.CorrelationID != "":
content = `{
"description": "` + description + `",
"operation": "` + msgB64 + `",
"correlationId": "` + operationBatch.CorrelationID + `"
}`
default:
content = `{
"description": "` + description + `",
"operation": "` + msgB64 + `"
}`
}
Expand Down Expand Up @@ -228,36 +232,10 @@ func DecodeOperationType(data []byte) (uint64, error) {
return opType, 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)
}

//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
}

return (valueByteLenght + keyByteLengh + StorageEntryBaseBytes) * StorageCostPerByte, nil
func StorageCostForEntry(keyByteLength, valueByteLength int) (int, error) {
return (valueByteLength + keyByteLength + StorageEntryBaseBytes) * StorageCostPerByte, nil
}

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)
}

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

return lecacyAccountCreationStorageCost, nil
}

func AccountCreationStorageCost() (int, error) {
return accountCreationStorageCost, nil
}
2 changes: 1 addition & 1 deletion pkg/node/sendoperation/transaction/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

const (
OpType = uint64(0)
OpType = 0
versionByte = byte(0)
)

Expand Down
3 changes: 2 additions & 1 deletion pkg/onchain/dns/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,13 @@ func SetRecord(
"dns1_setResolver",
rec,
sendoperation.DefaultFee,
sendoperation.DefaultGasLimit,
sendoperation.DefaultGasLimitCallSC,
sendoperation.OneMassa,
sendoperation.DefaultExpiryInSlot,
false,
operationBatch,
&signer.WalletPlugin{},
"Setting DNS record",
)
if err != nil {
return "", fmt.Errorf("calling setResolver at '%s': %w", addr, err)
Expand Down
4 changes: 4 additions & 0 deletions pkg/onchain/sc.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func CallFunction(client *node.Client,
async bool,
operationBatch sendOperation.OperationBatch,
signer signer.Signer,
description string,
) (*OperationWithEventResponse, error) {
callSC, err := callsc.New(addr, function, parameter,
maxGas,
Expand All @@ -48,6 +49,7 @@ func CallFunction(client *node.Client,
nickname,
operationBatch,
signer,
description,
)
if err != nil {
return nil, fmt.Errorf("calling function '%s' at '%s' with '%+v': %w", function, addr, parameter, err)
Expand Down Expand Up @@ -92,6 +94,7 @@ func DeploySC(client *node.Client,
datastore []byte,
operationBatch sendOperation.OperationBatch,
signer signer.Signer,
description string,
) (*sendOperation.OperationResponse, []node.Event, error) {
exeSC := executesc.New(
contract,
Expand All @@ -107,6 +110,7 @@ func DeploySC(client *node.Client,
nickname,
operationBatch,
signer,
"Deploying smart contract: "+description,
)
if err != nil {
return nil, nil, fmt.Errorf("calling executeSC: %w", err)
Expand Down
33 changes: 17 additions & 16 deletions pkg/onchain/website/uploader.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import (
var content embed.FS

const (
blockLength = 260000
nbChunkKey = "NB_CHUNKS"
ownerKey = "OWNER"
chunkSize = 260000
nbChunkKey = "NB_CHUNKS"
ownerKey = "OWNER"
)

//nolint:funlen,cyclop
Expand Down Expand Up @@ -60,18 +60,18 @@ func PrepareForUpload(
}

// webSiteInitCost correspond to the cost of owner initialization
//nolint:lll,gomnd
webSiteInitCost, err := sendOperation.StorageCostForEntry(network.Version, len([]byte(ownerKey)), 53 /*owner Addr max byteLenght*/)
//nolint:gomnd
webSiteInitCost, err := sendOperation.StorageCostForEntry(len([]byte(ownerKey)), 53 /*owner Addr max byteLenght*/)
if err != nil {
return "", "", fmt.Errorf("unable to compute storage cost for website init: %w", err)
}

deployCost, err := sendOperation.StorageCostForEntry(network.Version, 0, len(websiteStorer))
deployCost, err := sendOperation.StorageCostForEntry(0, len(websiteStorer))
if err != nil {
return "", "", fmt.Errorf("unable to compute storage cost for website deployment: %w", err)
}

accountCreationCost, err := sendOperation.AccountCreationStorageCost(network.Version)
accountCreationCost, err := sendOperation.AccountCreationStorageCost()
if err != nil {
return "", "", fmt.Errorf("unable to compute storage cost for account creation: %w", err)
}
Expand All @@ -82,14 +82,15 @@ func PrepareForUpload(
operationResponse, events, err := onchain.DeploySC(
client,
nickname,
sendOperation.DefaultGasLimit,
sendOperation.DefaultGasLimitExecuteSC,
uint64(totalStorageCost),
sendOperation.DefaultFee,
sendOperation.DefaultExpiryInSlot,
websiteDeployer,
nil,
sendOperation.OperationBatch{NewBatch: true, CorrelationID: ""},
&signer.WalletPlugin{},
"deploying website",
)
if err != nil {
return "", "", fmt.Errorf("deploying webstorage SC: %w", err)
Expand Down Expand Up @@ -128,7 +129,7 @@ func Upload(
nickname string,
operationBatch sendOperation.OperationBatch,
) ([]string, error) {
blocks := chunk(content, blockLength)
blocks := chunk(content, chunkSize)

operations, err := upload(network, atAddress, blocks, nickname, operationBatch)
if err != nil {
Expand Down Expand Up @@ -162,15 +163,14 @@ func upload(
// Chunk data encoding
params = append(params, chunks[chunkIndex]...)

uploadCost, err := sendOperation.StorageCostForEntry(network.Version, convert.BytesPerUint32, chunkSize)
uploadCost, err := sendOperation.StorageCostForEntry(convert.BytesPerUint32, chunkSize)
if err != nil {
return nil, fmt.Errorf("unable to compute storage cost chunk upload: %w", err)
}

if chunkIndex == 0 {
// if chunkID == 0, we need to add the cost of the key creation for the NB_CHUNKS key
chunkKeyCost, err := sendOperation.StorageCostForEntry(
network.Version,
len([]byte(nbChunkKey)),
convert.BytesPerUint32)
if err != nil {
Expand All @@ -189,12 +189,13 @@ func upload(
"appendBytesToWebsite",
params,
sendOperation.DefaultFee,
sendOperation.DefaultGasLimit,
sendOperation.DefaultGasLimitCallSC,
uint64(uploadCost),
sendOperation.DefaultExpiryInSlot,
false,
operationBatch,
&signer.WalletPlugin{},
fmt.Sprintf("Uploading website chunk %d out of %d", chunkIndex+1, nbChunks),
)
if err != nil {
return nil, fmt.Errorf("calling appendBytesToWebsite at '%s': %w", addr, err)
Expand All @@ -214,7 +215,7 @@ func UploadMissedChunks(
missedChunks string,
operationBatch sendOperation.OperationBatch,
) ([]string, error) {
blocks := chunk(content, blockLength)
blocks := chunk(content, chunkSize)

operations, err := uploadMissedChunks(
config,
Expand Down Expand Up @@ -262,15 +263,14 @@ func uploadMissedChunks(
//nolint:ineffassign,nolintlint
params = append(params, chunks[chunkID]...)

uploadCost, err := sendOperation.StorageCostForEntry(network.Version, convert.BytesPerUint32, chunkSize)
uploadCost, err := sendOperation.StorageCostForEntry(convert.BytesPerUint32, chunkSize)
if err != nil {
return nil, fmt.Errorf("unable to compute storage cost for chunk upload: %w", err)
}

if chunkID == 0 {
// if chunkID == 0, we may need to add the cost of the key creation for the NB_CHUNKS key
chunkKeyCost, err := sendOperation.StorageCostForEntry(
network.Version,
len([]byte(nbChunkKey)),
convert.BytesPerUint32)
if err != nil {
Expand All @@ -287,12 +287,13 @@ func uploadMissedChunks(
"appendBytesToWebsite",
params,
sendOperation.DefaultFee,
sendOperation.DefaultGasLimit,
sendOperation.DefaultGasLimitCallSC,
uint64(uploadCost),
sendOperation.DefaultExpiryInSlot,
false,
operationBatch,
&signer.WalletPlugin{},
fmt.Sprintf("Repairing website chunk %d out of %d", chunkID+1, len(arrMissedChunks)),
)
if err != nil {
return nil, fmt.Errorf("calling appendBytesToWebsite at '%s': %w", addr, err)
Expand Down

0 comments on commit 8ed54ff

Please sign in to comment.