From 8ed54ffeb4cc2f646bf3c62f15d7d1a693181708 Mon Sep 17 00:00:00 2001 From: Nathan Seva Date: Tue, 21 Nov 2023 14:09:32 +0100 Subject: [PATCH] Massa v27 compatible (#1274) * 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 --- Taskfile.yml | 2 +- .../server/restapi/resource/swagger.yml | 4 +- int/api/cmd/deploySC.go | 1 + int/api/cmd/execute_function.go | 3 +- int/api/events.go | 2 +- pkg/node/events.go | 6 +-- pkg/node/sendoperation/callsc/callsc.go | 2 +- pkg/node/sendoperation/sendoperation.go | 40 +++++-------------- .../sendoperation/transaction/transaction.go | 2 +- pkg/onchain/dns/dns.go | 3 +- pkg/onchain/sc.go | 4 ++ pkg/onchain/website/uploader.go | 33 +++++++-------- 12 files changed, 44 insertions(+), 58 deletions(-) diff --git a/Taskfile.yml b/Taskfile.yml index b486b0ee2..d799ddb2f 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -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 diff --git a/api/swagger/server/restapi/resource/swagger.yml b/api/swagger/server/restapi/resource/swagger.yml index 536c0ebbd..d4619f545 100644 --- a/api/swagger/server/restapi/resource/swagger.yml +++ b/api/swagger/server/restapi/resource/swagger.yml @@ -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 @@ -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 diff --git a/int/api/cmd/deploySC.go b/int/api/cmd/deploySC.go index 416d00b8f..5e2e61460 100644 --- a/int/api/cmd/deploySC.go +++ b/int/api/cmd/deploySC.go @@ -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(). diff --git a/int/api/cmd/execute_function.go b/int/api/cmd/execute_function.go index 704ca52e6..cfb6dae85 100644 --- a/int/api/cmd/execute_function.go +++ b/int/api/cmd/execute_function.go @@ -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) @@ -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( diff --git a/int/api/events.go b/int/api/events.go index 59d445332..89163ab95 100644 --- a/int/api/events.go +++ b/int/api/events.go @@ -44,7 +44,7 @@ func (h *eventListener) Handle(params operations.EventsGetterParams) middleware. var event *node.Event for { - events, err := node.ListenEvents(client, &slotStart, nil, ¶ms.Caller, nil, nil, false) + events, err := node.ListenEvents(client, &slotStart, nil, nil, nil, ¶ms.Caller, false) if err != nil { return operations.NewEventsGetterInternalServerError(). WithPayload( diff --git a/pkg/node/events.go b/pkg/node/events.go index 51eed195f..efa773e2e 100644 --- a/pkg/node/events.go +++ b/pkg/node/events.go @@ -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( @@ -61,7 +61,7 @@ func Events(client *Client, start *Slot, end *Slot, Start: start, End: end, EmitterAddress: emitter, - OriginalCallerAddress: originaCaller, + OriginalCallerAddress: originalCaller, OriginalOperationID: operationID, }, }, @@ -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) diff --git a/pkg/node/sendoperation/callsc/callsc.go b/pkg/node/sendoperation/callsc/callsc.go index 1a8556315..be61cb6b0 100644 --- a/pkg/node/sendoperation/callsc/callsc.go +++ b/pkg/node/sendoperation/callsc/callsc.go @@ -10,7 +10,7 @@ import ( ) const ( - OpType = uint64(4) + OpType = 4 ) type OperationDetails struct { diff --git a/pkg/node/sendoperation/sendoperation.go b/pkg/node/sendoperation/sendoperation.go index 74ab171b0..3801e2ddf 100644 --- a/pkg/node/sendoperation/sendoperation.go +++ b/pkg/node/sendoperation/sendoperation.go @@ -6,7 +6,6 @@ import ( b64 "encoding/base64" "encoding/binary" "fmt" - "strconv" "strings" "github.com/massalabs/station/pkg/node" @@ -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 @@ -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 { @@ -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 + `" }` } @@ -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 } diff --git a/pkg/node/sendoperation/transaction/transaction.go b/pkg/node/sendoperation/transaction/transaction.go index 950926526..a12e3dd14 100644 --- a/pkg/node/sendoperation/transaction/transaction.go +++ b/pkg/node/sendoperation/transaction/transaction.go @@ -11,7 +11,7 @@ import ( ) const ( - OpType = uint64(0) + OpType = 0 versionByte = byte(0) ) diff --git a/pkg/onchain/dns/dns.go b/pkg/onchain/dns/dns.go index 69ff271a4..a6b396520 100644 --- a/pkg/onchain/dns/dns.go +++ b/pkg/onchain/dns/dns.go @@ -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) diff --git a/pkg/onchain/sc.go b/pkg/onchain/sc.go index 7416c772b..31887060d 100644 --- a/pkg/onchain/sc.go +++ b/pkg/onchain/sc.go @@ -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, @@ -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) @@ -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, @@ -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) diff --git a/pkg/onchain/website/uploader.go b/pkg/onchain/website/uploader.go index 9fd9224b8..67d97aa41 100644 --- a/pkg/onchain/website/uploader.go +++ b/pkg/onchain/website/uploader.go @@ -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 @@ -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) } @@ -82,7 +82,7 @@ func PrepareForUpload( operationResponse, events, err := onchain.DeploySC( client, nickname, - sendOperation.DefaultGasLimit, + sendOperation.DefaultGasLimitExecuteSC, uint64(totalStorageCost), sendOperation.DefaultFee, sendOperation.DefaultExpiryInSlot, @@ -90,6 +90,7 @@ func PrepareForUpload( nil, sendOperation.OperationBatch{NewBatch: true, CorrelationID: ""}, &signer.WalletPlugin{}, + "deploying website", ) if err != nil { return "", "", fmt.Errorf("deploying webstorage SC: %w", err) @@ -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 { @@ -162,7 +163,7 @@ 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) } @@ -170,7 +171,6 @@ func upload( 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 { @@ -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) @@ -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, @@ -262,7 +263,7 @@ 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) } @@ -270,7 +271,6 @@ func uploadMissedChunks( 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 { @@ -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)