|
| 1 | +// (c) Cartesi and individual authors (see AUTHORS) |
| 2 | +// SPDX-License-Identifier: Apache-2.0 (see LICENSE) |
| 3 | + |
| 4 | +package jsonrpc |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "encoding/json" |
| 9 | + "fmt" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/cartesi/rollups-node/internal/model" |
| 13 | + "github.com/ethereum/go-ethereum/common/hexutil" |
| 14 | + "github.com/stretchr/testify/assert" |
| 15 | +) |
| 16 | + |
| 17 | +// failure: input_index not hex encoded -> invalid param |
| 18 | +func TestGetInputWithMalformedInputIndex(t *testing.T) { |
| 19 | + s := newTestService(t, functionName()) |
| 20 | + |
| 21 | + app := uint64(1) |
| 22 | + nr := uint64(0) |
| 23 | + |
| 24 | + body := s.doRequest(t, 0, fmt.Appendf([]byte{}, `{ |
| 25 | + "jsonrpc": "2.0", |
| 26 | + "method": "cartesi_getInput", |
| 27 | + "params": { |
| 28 | + "application": "%v", |
| 29 | + "input_index": "%v" |
| 30 | + }, |
| 31 | + "id": 0 |
| 32 | + }`, numberToName(app), nr)) |
| 33 | + |
| 34 | + resp := testRPCResponse[any]{} |
| 35 | + assert.Nil(t, json.Unmarshal(body, &resp)) |
| 36 | + assert.Equal(t, JSONRPC_INVALID_PARAMS, resp.Error.Code) |
| 37 | + assert.Equal(t, "invalid input_index: expected hex encoded value", resp.Error.Message) |
| 38 | +} |
| 39 | + |
| 40 | +// failure: input_index not in database -> absent input |
| 41 | +func TestGetInputWithAbsentInput(t *testing.T) { |
| 42 | + s := newTestService(t, functionName()) |
| 43 | + ctx := context.Background() |
| 44 | + |
| 45 | + app := uint64(2) |
| 46 | + enr := uint64(1) |
| 47 | + inr := uint64(0) |
| 48 | + appID := s.newTestApplication(t, ctx, 0, app) |
| 49 | + err := s.repository.CreateEpoch(ctx, &model.Epoch{ |
| 50 | + ApplicationID: appID, |
| 51 | + Index: enr, |
| 52 | + VirtualIndex: enr, |
| 53 | + Status: model.EpochStatus_ClaimAccepted, |
| 54 | + }) |
| 55 | + assert.Nil(t, err, "on test case: %v, application: %v, epoch_index: %v", 0, appID, enr) |
| 56 | + |
| 57 | + body := s.doRequest(t, 0, fmt.Appendf([]byte{}, `{ |
| 58 | + "jsonrpc": "2.0", |
| 59 | + "method": "cartesi_getInput", |
| 60 | + "params": { |
| 61 | + "application": "%v", |
| 62 | + "input_index": "%v" |
| 63 | + }, |
| 64 | + "id": 0 |
| 65 | + }`, numberToName(app), hexutil.EncodeUint64(inr))) |
| 66 | + |
| 67 | + resp := testRPCResponse[*model.Input]{} |
| 68 | + assert.Nil(t, json.Unmarshal(body, &resp)) |
| 69 | + assert.Equal(t, JSONRPC_RESOURCE_NOT_FOUND, resp.Error.Code) |
| 70 | + assert.Equal(t, "Input not found", resp.Error.Message) |
| 71 | +} |
| 72 | + |
| 73 | +// success: input_index of EvmAdvance in the database -> retrieve input |
| 74 | +func TestGetInputWithApplicationAndInput(t *testing.T) { |
| 75 | + s := newTestService(t, functionName()) |
| 76 | + ctx := context.Background() |
| 77 | + |
| 78 | + app := uint64(2) |
| 79 | + enr := uint64(1) |
| 80 | + inr := uint64(0) |
| 81 | + appID := s.newTestApplication(t, ctx, 0, app) |
| 82 | + err := s.repository.CreateEpoch(ctx, &model.Epoch{ |
| 83 | + ApplicationID: appID, |
| 84 | + Index: enr, |
| 85 | + VirtualIndex: enr, |
| 86 | + Status: model.EpochStatus_ClaimAccepted, |
| 87 | + }) |
| 88 | + assert.Nil(t, err, "on test case: %v, application: %v, epoch_index: %v", 0, appID, enr) |
| 89 | + |
| 90 | + err = s.repository.CreateInput(ctx, &model.Input{ |
| 91 | + EpochApplicationID: appID, |
| 92 | + EpochIndex: enr, |
| 93 | + Index: inr, |
| 94 | + Status: model.InputCompletionStatus_Accepted, |
| 95 | + RawData: emptyInput(), |
| 96 | + }) |
| 97 | + assert.Nil(t, err, "on test case: %v, application: %v, input_index: %v", 0, appID, inr) |
| 98 | + |
| 99 | + body := s.doRequest(t, 0, fmt.Appendf([]byte{}, `{ |
| 100 | + "jsonrpc": "2.0", |
| 101 | + "method": "cartesi_getInput", |
| 102 | + "params": { |
| 103 | + "application": "%v", |
| 104 | + "input_index": "%v" |
| 105 | + }, |
| 106 | + "id": 0 |
| 107 | + }`, numberToName(app), hexutil.EncodeUint64(inr))) |
| 108 | + |
| 109 | + resp := testRPCResponse[*model.Input]{} |
| 110 | + assert.Nil(t, json.Unmarshal(body, &resp)) |
| 111 | + assert.Equal(t, enr, resp.Result.Data.EpochIndex) |
| 112 | + assert.Equal(t, inr, resp.Result.Data.Index) |
| 113 | +} |
| 114 | + |
| 115 | +// TODO: also test DecodedInput |
0 commit comments