Skip to content

Commit

Permalink
add readonly execute sc endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Thykof committed Jan 8, 2024
1 parent f4cfb74 commit 4d80adc
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 8 deletions.
60 changes: 53 additions & 7 deletions api/swagger/server/restapi/resource/swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,59 @@ paths:
description: Internal Server Error - The server has encountered a situation it does not know how to handle.
schema:
$ref: "#/definitions/Error"
/cmd/read-only/executesc:
post:
description: Read-only execute smart contract
operationId: cmdReadOnlyExecuteSC
consumes:
- multipart/form-data
parameters:
- in: formData
name: nickname
type: string
required: true
x-nullable: false
description: Nickname of the account used to execute the byte code.
- in: formData
name: bytecode
type: file
required: true
x-nullable: false
description: Smart contract file in a Wasm format.
- in: formData
name : coins
type: string
description: Set the number of coins that will be sent along the deployment call. (NanoMassa)
default: "0"
- in: formData
name : fee
type : string
description: Set the fee amount (in massa) that will be given to the block creator.
default: "0"
- in: formData
name: datastore
type: string
default: ""
description: base64 encoded datastore that will be sent along the smart contract.
produces:
- application/json
responses:
"200":
description: OK.
schema:
$ref: "#/definitions/ReadOnlyResult"
"400":
description: Bad request.
schema:
$ref: "#/definitions/Error"
"422":
description: Unprocessable Entity - syntax is correct, but the server was unable to process the contained instructions.
schema:
$ref: "#/definitions/Error"
"500":
description: Internal Server Error - The server has encountered a situation it does not know how to handle.
schema:
$ref: "#/definitions/Error"
/cmd/deploySC:
post:
description: Deploys the given smart contract to the blockchain network.
Expand All @@ -185,13 +238,6 @@ paths:
required: true
x-nullable: false
description: Smart contract file in a Wasm format.
- in : formData
name : gasPrice
type: integer
format: uint64
minimum: 0
description : Price of a gas unit.
default : 0
- in : formData
name : gasLimit
type : integer
Expand Down
12 changes: 12 additions & 0 deletions api/test/robot_tests/cmd/cmd.robot
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ Suite Setup Suite Setup


*** Test Cases ***
POST /cmd/read-only/executesc
${sc}= Get File For Streaming Upload ${CURDIR}/../../testSC/build/main-testSC.wasm
${data}= Create Dictionary
... nickname=${WALLET_NICKNAME}
... coins=3000000000
... fee=1000
${file}= Create Dictionary bytecode=${sc}
${response}= POST ${API_URL}/cmd/read-only/executesc data=${data} files=${file} expected_status=any
Log To Console json response: ${response.json()} # Print the response content to the test log for debugging
Should Be Equal As Integers ${response.status_code} ${STATUS_OK} # Assert the status code is 200 OK
Should Contain string(${response.json()}) TestSC is deployed at

POST a Smart Contract
${sc}= Get File For Streaming Upload ${CURDIR}/../../testSC/build/main-testSC.wasm
${data}= Create Dictionary
Expand Down
79 changes: 79 additions & 0 deletions int/api/cmd/read_only_execute_sc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package cmd

import (
"encoding/base64"
"io"

"github.com/go-openapi/runtime/middleware"
"github.com/massalabs/station/api/swagger/server/models"
"github.com/massalabs/station/api/swagger/server/restapi/operations"
"github.com/massalabs/station/int/config"
"github.com/massalabs/station/pkg/node"
sendOperation "github.com/massalabs/station/pkg/node/sendoperation"
"github.com/massalabs/station/pkg/wallet"
)

func NewReadOnlyExecuteSCHandler(config *config.NetworkInfos) operations.CmdReadOnlyExecuteSCHandler {
return &ReadOnlyExecuteSC{networkInfos: config}
}

type ReadOnlyExecuteSC struct {
networkInfos *config.NetworkInfos
}

func (e *ReadOnlyExecuteSC) Handle(params operations.CmdReadOnlyExecuteSCParams) middleware.Responder {
file, err := io.ReadAll(params.Bytecode)
if err != nil {
return operations.NewCmdDeploySCBadRequest().
WithPayload(
&models.Error{
Code: err.Error(),
Message: err.Error(),
})
}

datastore, err := base64.StdEncoding.DecodeString(*params.Datastore)
if err != nil {
return operations.NewCmdReadOnlyExecuteSCUnprocessableEntity().
WithPayload(
&models.Error{
Code: errorInvalidArgs,
Message: err.Error(),
})
}

if len(datastore) == 0 {
datastore = nil
}

acc, err := wallet.Fetch(params.Nickname)
if err != nil {
return operations.NewCmdReadOnlyExecuteSCBadRequest().WithPayload(
&models.Error{
Code: errorInvalidNickname,
Message: "Error during wallet fetch: " + err.Error(),
})
}

coins, errResponse := amountToString(models.Amount(*params.Coins), uint64(0))
if errResponse != nil {
return errResponse
}

result, err := sendOperation.ReadOnlyExecuteSC(
file,
datastore,
coins,
*params.Fee,
acc.Address,
node.NewClient(e.networkInfos.NodeURL),
)
if err != nil {
return operations.NewCmdReadOnlyExecuteSCInternalServerError().WithPayload(
&models.Error{Code: errorSendOperation, Message: "Error: read only callSC failed: " + err.Error()})
}

modelResult := CreateReadOnlyResult(*result)

return operations.NewCmdReadOnlyExecuteSCOK().WithPayload(&modelResult)
}
1 change: 1 addition & 0 deletions int/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func initLocalAPI(
localAPI.GetMassaStationVersionHandler = operations.GetMassaStationVersionHandlerFunc(version.Handle)

localAPI.CmdDeploySCHandler = cmd.NewDeploySCHandler(config)
localAPI.CmdReadOnlyExecuteSCHandler = cmd.NewReadOnlyExecuteSCHandler(config)

localAPI.WebsiteUploaderPrepareHandler = websites.NewWebsitePrepareHandler(config)
localAPI.WebsiteUploaderUploadHandler = websites.NewWebsiteUploadHandler(config)
Expand Down
2 changes: 1 addition & 1 deletion pkg/onchain/sc.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func CallFunctionSuccess(
}, nil
}

// DeploySC deploys a smart contract on the blockchain. It returns the address of the smart contract and an Error.
// DeploySC deploys a smart contract on the blockchain.
// The smart contract is deployed with the given account nickname.
func DeploySC(
networkInfos *config.NetworkInfos,
Expand Down

0 comments on commit 4d80adc

Please sign in to comment.