|
| 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" |
| 14 | + "github.com/ethereum/go-ethereum/common/hexutil" |
| 15 | + "github.com/stretchr/testify/assert" |
| 16 | +) |
| 17 | + |
| 18 | +// failure: epoch_index not hex encoded -> invalid param |
| 19 | +func TestGetEpochMalformedEpochIndex(t *testing.T) { |
| 20 | + s := newTestService(t, functionName()) |
| 21 | + |
| 22 | + app := uint64(1) |
| 23 | + nr := uint64(0) |
| 24 | + |
| 25 | + body := s.doRequest(t, 0, fmt.Appendf([]byte{}, `{ |
| 26 | + "jsonrpc": "2.0", |
| 27 | + "method": "cartesi_getEpoch", |
| 28 | + "params": { |
| 29 | + "application": "%v", |
| 30 | + "epoch_index": "%v" |
| 31 | + }, |
| 32 | + "id": 0 |
| 33 | + }`, numberToName(app), nr)) |
| 34 | + |
| 35 | + resp := testRPCResponse[any]{} |
| 36 | + assert.Nil(t, json.Unmarshal(body, &resp)) |
| 37 | + assert.Equal(t, JSONRPC_INVALID_PARAMS, resp.Error.Code) |
| 38 | + assert.Equal(t, "invalid epoch_index: expected hex encoded value", resp.Error.Message) |
| 39 | +} |
| 40 | + |
| 41 | +// failure: epoch not in the database -> resource not found |
| 42 | +func TestGetEpochAbsent(t *testing.T) { |
| 43 | + s := newTestService(t, functionName()) |
| 44 | + ctx := context.Background() |
| 45 | + |
| 46 | + app := uint64(1) |
| 47 | + nr := uint64(0) |
| 48 | + |
| 49 | + appID := s.newTestApplication(t, ctx, 0, app) |
| 50 | + err := s.repository.CreateEpoch(ctx, &model.Epoch{ |
| 51 | + ApplicationID: appID, |
| 52 | + Index: nr, |
| 53 | + ClaimHash: &common.Hash{}, |
| 54 | + ClaimTransactionHash: &common.Hash{}, |
| 55 | + Status: model.EpochStatus_ClaimAccepted, |
| 56 | + }) |
| 57 | + assert.Nil(t, err, "on test case: %v, application: %v, epoch_index: %v", 0, appID, nr) |
| 58 | + |
| 59 | + body := s.doRequest(t, 0, fmt.Appendf([]byte{}, `{ |
| 60 | + "jsonrpc": "2.0", |
| 61 | + "method": "cartesi_getEpoch", |
| 62 | + "params": { |
| 63 | + "application": "%v", |
| 64 | + "epoch_index": "%v" |
| 65 | + }, |
| 66 | + "id": 0 |
| 67 | + }`, numberToName(app), hexutil.EncodeUint64(nr+1))) |
| 68 | + |
| 69 | + resp := testRPCResponse[any]{} |
| 70 | + assert.Nil(t, json.Unmarshal(body, &resp)) |
| 71 | + assert.Equal(t, JSONRPC_RESOURCE_NOT_FOUND, resp.Error.Code) |
| 72 | + assert.Equal(t, "Epoch not found", resp.Error.Message) |
| 73 | +} |
| 74 | + |
| 75 | +// success: epoch is in the database -> retrieve epoch |
| 76 | +func TestGetEpochSuccess(t *testing.T) { |
| 77 | + s := newTestService(t, functionName()) |
| 78 | + ctx := context.Background() |
| 79 | + |
| 80 | + app := uint64(1) |
| 81 | + nr := uint64(1) |
| 82 | + |
| 83 | + appID := s.newTestApplication(t, ctx, 0, app) |
| 84 | + err := s.repository.CreateEpoch(ctx, &model.Epoch{ |
| 85 | + ApplicationID: appID, |
| 86 | + Index: nr, |
| 87 | + ClaimHash: &common.Hash{}, |
| 88 | + ClaimTransactionHash: &common.Hash{}, |
| 89 | + Status: model.EpochStatus_ClaimAccepted, |
| 90 | + }) |
| 91 | + assert.Nil(t, err, "on test case: %v, application: %v, epoch_index: %v", 0, appID, nr) |
| 92 | + |
| 93 | + body := s.doRequest(t, 0, fmt.Appendf([]byte{}, `{ |
| 94 | + "jsonrpc": "2.0", |
| 95 | + "method": "cartesi_getEpoch", |
| 96 | + "params": { |
| 97 | + "application": "%v", |
| 98 | + "epoch_index": "%v" |
| 99 | + }, |
| 100 | + "id": 0 |
| 101 | + }`, numberToName(app), hexutil.EncodeUint64(nr))) |
| 102 | + |
| 103 | + resp := testRPCResponse[*model.Epoch]{} |
| 104 | + assert.Nil(t, json.Unmarshal(body, &resp)) |
| 105 | + assert.Equal(t, nr, resp.Result.Data.Index) |
| 106 | +} |
0 commit comments