Skip to content

Commit

Permalink
add readonly execute sc endpoint (#1310)
Browse files Browse the repository at this point in the history
  • Loading branch information
Thykof authored Jan 10, 2024
1 parent 7774470 commit 9ded6c1
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 @@ -145,6 +145,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 @@ -164,13 +217,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 @@ -44,6 +44,7 @@ func initLocalAPI(
localAPI.GetMassaStationVersionHandler = operations.GetMassaStationVersionHandlerFunc(version.Handle)

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

localAPI.EventsGetterHandler = NewEventListenerHandler(config)
localAPI.MassaStationWebAppHandler = operations.MassaStationWebAppHandlerFunc(MassaStationWebAppHandler)
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 9ded6c1

Please sign in to comment.