Skip to content

Commit

Permalink
clean code, return an error
Browse files Browse the repository at this point in the history
  • Loading branch information
Thykof committed Dec 18, 2023
1 parent 0c8ef07 commit 56aad21
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 25 deletions.
26 changes: 10 additions & 16 deletions pkg/node/sendoperation/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@ type ReadOnlyCallParams struct {
CallerAddress string `json:"caller_address"`
}

// Read only call response (chatgpt4 generated).
type ReadOnlyCallResponse struct {
JSONRPC string `json:"jsonrpc"`
Result []ReadOnlyCallResult `json:"result"`
// ReadOnlyExecuteParams is the struct used to send a read only executeSC to the node.
type ReadOnlyExecuteParams struct {
MaxGas int `json:"max_gas"`
Coins string `json:"coins"`
Fee string `json:"fee"`
Address string `json:"address"`
Bytecode JSONableSlice `json:"bytecode"`
OperationDatastore JSONableSlice `json:"operation_datastore"`
}

type ReadOnlyCallResult struct {
type ReadOnlyResult struct {
ExecutedAt Timestamp `json:"executed_at"`
Result Result `json:"result"`
OutputEvents []Event `json:"output_events"`
Expand Down Expand Up @@ -67,7 +71,7 @@ func (r *Result) UnmarshalJSON(data []byte) error {
return nil
}

return nil // Or return an error if neither key is present
return nil

Check warning on line 74 in pkg/node/sendoperation/interfaces.go

View check run for this annotation

Codecov / codecov/patch

pkg/node/sendoperation/interfaces.go#L74

Added line #L74 was not covered by tests
}

type Timestamp struct {
Expand Down Expand Up @@ -140,13 +144,3 @@ type BitVecHeadInfo struct {
type DeferredCreditsInfo struct {
Credits map[string]interface{} `json:"credits"` // Empty in provided JSON, assumed to be a map
}

// ReadOnlyExecuteParams is the struct used to send a read only executeSC to the node.
type ReadOnlyExecuteParams struct {
MaxGas int `json:"max_gas"`
Coins string `json:"coins"`
Fee string `json:"fee"`
Address string `json:"address"`
Bytecode JSONableSlice `json:"bytecode"`
OperationDatastore JSONableSlice `json:"operation_datastore"`
}
14 changes: 5 additions & 9 deletions pkg/node/sendoperation/readonlycall.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func ReadOnlyCallSC(
fee string,
callerAddr string,
client *node.Client,
) (*ReadOnlyCallResult, error) {
) (*ReadOnlyResult, error) {

Check warning on line 66 in pkg/node/sendoperation/readonlycall.go

View check run for this annotation

Codecov / codecov/patch

pkg/node/sendoperation/readonlycall.go#L66

Added line #L66 was not covered by tests
readOnlyCallParams := [][]ReadOnlyCallParams{
{
ReadOnlyCallParams{
Expand Down Expand Up @@ -95,7 +95,7 @@ func ReadOnlyCallSC(
)
}

var resp []ReadOnlyCallResult
var resp []ReadOnlyResult

Check warning on line 98 in pkg/node/sendoperation/readonlycall.go

View check run for this annotation

Codecov / codecov/patch

pkg/node/sendoperation/readonlycall.go#L98

Added line #L98 was not covered by tests

err = rawResponse.GetObject(&resp)
if err != nil {
Expand Down Expand Up @@ -135,11 +135,7 @@ func EstimateGasCostExecuteSC(

result, err := ReadOnlyExecuteSC(contract, datastore, coinsString, feeString, acc.Address, client)
if err != nil {
logger.Warnf("ReadOnlyExecuteSC error: %s, caller address is %s", err, acc.Address)

// Do not return an error because execute_read_only_bytecode v24.1 fail with Internal error
// and on v27 fails with Invalid params
return DefaultGasLimitExecuteSC, nil
return 0, fmt.Errorf("ReadOnlyExecuteSC error: %w, caller address is %s", err, acc.Address)
}

Check warning on line 139 in pkg/node/sendoperation/readonlycall.go

View check run for this annotation

Codecov / codecov/patch

pkg/node/sendoperation/readonlycall.go#L136-L139

Added lines #L136 - L139 were not covered by tests

estimatedGasCost := uint64(result.GasCost)
Expand All @@ -156,7 +152,7 @@ func ReadOnlyExecuteSC(
fee string,
callerAddr string,
client *node.Client,
) (*ReadOnlyCallResult, error) {
) (*ReadOnlyResult, error) {
if datastore == nil {
datastore = []byte{0}
}

Check warning on line 158 in pkg/node/sendoperation/readonlycall.go

View check run for this annotation

Codecov / codecov/patch

pkg/node/sendoperation/readonlycall.go#L155-L158

Added lines #L155 - L158 were not covered by tests
Expand Down Expand Up @@ -191,7 +187,7 @@ func ReadOnlyExecuteSC(
)
}

Check warning on line 188 in pkg/node/sendoperation/readonlycall.go

View check run for this annotation

Codecov / codecov/patch

pkg/node/sendoperation/readonlycall.go#L182-L188

Added lines #L182 - L188 were not covered by tests

var resp []ReadOnlyCallResult // rename if it's the same for call sc and execute sc: ReadOnlyResult
var resp []ReadOnlyResult

err = rawResponse.GetObject(&resp)
if err != nil {
Expand Down

0 comments on commit 56aad21

Please sign in to comment.