Skip to content

Commit 55c0836

Browse files
committed
feat(jsonrpc): Add jsonrpc tests
1 parent 81bff4b commit 55c0836

18 files changed

Lines changed: 1187 additions & 26 deletions
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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/stretchr/testify/assert"
14+
)
15+
16+
// failure: application not in the database -> resource not found
17+
func TestGetApplicationAbsent(t *testing.T) {
18+
s := newTestService(t, functionName())
19+
20+
nr := uint64(0)
21+
body := s.doRequest(t, 0, fmt.Appendf([]byte{}, `{
22+
"jsonrpc": "2.0",
23+
"method": "cartesi_getApplication",
24+
"params": { "application": "%v" },
25+
"id": 0
26+
}`, numberToName(nr)))
27+
28+
resp := testRPCResponse[any]{}
29+
assert.Nil(t, json.Unmarshal(body, &resp))
30+
assert.Equal(t, JSONRPC_RESOURCE_NOT_FOUND, resp.Error.Code)
31+
assert.Equal(t, "Application not found", resp.Error.Message)
32+
}
33+
34+
// success: application is in the database -> retrieve application
35+
func TestGetApplicationSuccess(t *testing.T) {
36+
s := newTestService(t, functionName())
37+
ctx := context.Background()
38+
39+
nr := uint64(1)
40+
s.newTestApplication(t, ctx, 0, nr)
41+
body := s.doRequest(t, 0, fmt.Appendf([]byte{}, `{
42+
"jsonrpc": "2.0",
43+
"method": "cartesi_getApplication",
44+
"params": { "application": "%v" },
45+
"id": 0
46+
}`, numberToName(nr)))
47+
48+
resp := testRPCResponse[model.Application]{}
49+
assert.Nil(t, json.Unmarshal(body, &resp))
50+
assert.Equal(t, nr, nameToNumber(resp.Result.Data.Name))
51+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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/evmreader"
13+
"github.com/cartesi/rollups-node/internal/model"
14+
"github.com/cartesi/rollups-node/internal/repository"
15+
"github.com/stretchr/testify/assert"
16+
)
17+
18+
func Test_getChainIdFailure(t *testing.T) {
19+
s := newTestService(t, "getChainIdFailure")
20+
21+
body := s.doRequest(t, 0, fmt.Appendf([]byte{}, `{
22+
"jsonrpc": "2.0",
23+
"method": "cartesi_getChainId",
24+
"params": {},
25+
"id": 0
26+
}`))
27+
resp := testRPCResponse[any]{}
28+
assert.Nil(t, json.Unmarshal(body, &resp))
29+
assert.Equal(t, JSONRPC_RESOURCE_NOT_FOUND, resp.Error.Code)
30+
assert.Equal(t, "EVM Reader config not found", resp.Error.Message)
31+
}
32+
33+
func Test_getChainIdSuccess(t *testing.T) {
34+
ctx := context.Background()
35+
s := newTestService(t, "getChainIdFailure")
36+
37+
// NodeConfig provision
38+
nr := uint64(0xdeadbeef)
39+
repository.SaveNodeConfig(ctx, s.repository,
40+
&model.NodeConfig[evmreader.PersistentConfig]{
41+
Key: evmreader.EvmReaderConfigKey,
42+
Value: evmreader.PersistentConfig{
43+
ChainID: nr,
44+
},
45+
},
46+
)
47+
48+
body := s.doRequest(t, 0, fmt.Appendf([]byte{}, `{
49+
"jsonrpc": "2.0",
50+
"method": "cartesi_getChainId",
51+
"params": {},
52+
"id": 0
53+
}`))
54+
resp := testRPCResponse[hex64]{}
55+
assert.Nil(t, json.Unmarshal(body, &resp))
56+
assert.Equal(t, nr, uint64(resp.Result.Data))
57+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
"encoding/json"
8+
"fmt"
9+
"testing"
10+
11+
"github.com/stretchr/testify/assert"
12+
)
13+
14+
// failure: input_index not hex encoded -> invalid param
15+
func TestGetInputWithMalformedInputIndex(t *testing.T) {
16+
s := newTestService(t, functionName())
17+
18+
app := uint64(1)
19+
nr := uint64(0)
20+
21+
body := s.doRequest(t, 0, fmt.Appendf([]byte{}, `{
22+
"jsonrpc": "2.0",
23+
"method": "cartesi_getInput",
24+
"params": {
25+
"application": "%v",
26+
"input_index": "%v"
27+
},
28+
"id": 0
29+
}`, numberToName(app), nr))
30+
31+
resp := testRPCResponse[any]{}
32+
assert.Nil(t, json.Unmarshal(body, &resp))
33+
assert.Equal(t, JSONRPC_INVALID_PARAMS, resp.Error.Code)
34+
assert.Equal(t, "invalid input_index: expected hex encoded value", resp.Error.Message)
35+
}
36+
37+
// TODO: success test needs repository.CreateInput
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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/stretchr/testify/assert"
15+
)
16+
17+
// failure: epoch not in the database -> resource not found
18+
func TestGetLastAcceptedEpochIndexAbsent(t *testing.T) {
19+
s := newTestService(t, functionName())
20+
21+
nr := uint64(0xdeadbeef)
22+
body := s.doRequest(t, 0, fmt.Appendf([]byte{}, `{
23+
"jsonrpc": "2.0",
24+
"method": "cartesi_getLastAcceptedEpochIndex",
25+
"params": { "application": "%v" },
26+
"id": 0
27+
}`, numberToName(nr)))
28+
29+
resp := testRPCResponse[hex64]{}
30+
assert.Nil(t, json.Unmarshal(body, &resp))
31+
assert.Equal(t, JSONRPC_RESOURCE_NOT_FOUND, resp.Error.Code)
32+
assert.Equal(t, "Epoch not found", resp.Error.Message)
33+
}
34+
35+
// success: epoch is in the database -> retrieve epoch index
36+
func TestGetLastAcceptedEpochIndexSuccess(t *testing.T) {
37+
s := newTestService(t, functionName())
38+
ctx := context.Background()
39+
40+
nr := uint64(0)
41+
epochIndex := uint64(0xdeadbeef)
42+
43+
appID := s.newTestApplication(t, ctx, 0, nr)
44+
err := s.repository.CreateEpoch(ctx, &model.Epoch{
45+
ApplicationID: appID,
46+
Index: epochIndex,
47+
ClaimHash: &common.Hash{},
48+
ClaimTransactionHash: &common.Hash{},
49+
Status: model.EpochStatus_ClaimAccepted,
50+
})
51+
assert.Nil(t, err, "on test case: %v, application: %v, epoch_index: %v", 0, appID, nr)
52+
53+
body := s.doRequest(t, 0, fmt.Appendf([]byte{}, `{
54+
"jsonrpc": "2.0",
55+
"method": "cartesi_getLastAcceptedEpochIndex",
56+
"params": { "application": "%v" },
57+
"id": 0
58+
}`, numberToName(nr)))
59+
60+
resp := testRPCResponse[hex64]{}
61+
assert.Nil(t, json.Unmarshal(body, &resp))
62+
assert.Equal(t, epochIndex, uint64(resp.Result.Data))
63+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
"encoding/json"
8+
"fmt"
9+
"testing"
10+
11+
"github.com/cartesi/rollups-node/internal/version"
12+
"github.com/stretchr/testify/assert"
13+
)
14+
15+
// success: -> version.BuildVersion
16+
func Test_getNodeVersionSuccess(t *testing.T) {
17+
s := newTestService(t, functionName())
18+
19+
body := s.doRequest(t, 0, fmt.Appendf([]byte{}, `{
20+
"jsonrpc": "2.0",
21+
"method": "cartesi_getNodeVersion",
22+
"params": {},
23+
"id": 0
24+
}`))
25+
26+
resp := testRPCResponse[string]{}
27+
assert.Nil(t, json.Unmarshal(body, &resp))
28+
assert.Equal(t, version.BuildVersion, resp.Result.Data)
29+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
"encoding/json"
8+
"fmt"
9+
"testing"
10+
11+
"github.com/stretchr/testify/assert"
12+
)
13+
14+
// failure: output_index not hex encoded -> invalid param
15+
func TestGetOutputWithMalformedOutputIndex(t *testing.T) {
16+
s := newTestService(t, functionName())
17+
18+
app := uint64(1)
19+
nr := uint64(0)
20+
21+
body := s.doRequest(t, 0, fmt.Appendf([]byte{}, `{
22+
"jsonrpc": "2.0",
23+
"method": "cartesi_getOutput",
24+
"params": {
25+
"application": "%v",
26+
"output_index": "%v"
27+
},
28+
"id": 0
29+
}`, numberToName(app), nr))
30+
31+
resp := testRPCResponse[any]{}
32+
assert.Nil(t, json.Unmarshal(body, &resp))
33+
assert.Equal(t, JSONRPC_INVALID_PARAMS, resp.Error.Code)
34+
assert.Equal(t, "invalid output_index: expected hex encoded value", resp.Error.Message)
35+
}
36+
37+
// TODO: success test needs repository.CreateOutput

0 commit comments

Comments
 (0)