@agoric/cosmos 0.34.1 → 0.34.2-calypso-dev-dabccdc.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/MAINTAINERS.md +3 -0
- package/Makefile +67 -41
- package/agoric/config.go +35 -0
- package/ante/ante.go +65 -0
- package/ante/expected_keepers.go +26 -0
- package/ante/inbound.go +137 -0
- package/ante/inbound_test.go +228 -0
- package/ante/vm_admission.go +65 -0
- package/app/app.go +396 -211
- package/app/export.go +13 -9
- package/app/sim_test.go +4 -4
- package/app/upgrade.go +215 -0
- package/binding.gyp.in +29 -4
- package/buf.work.yaml +4 -0
- package/cmd/agd/agvm.go +42 -0
- package/cmd/agd/find_binary.go +22 -7
- package/cmd/agd/main.go +133 -10
- package/cmd/libdaemon/main.go +67 -53
- package/cmd/libdaemon/main_test.go +37 -0
- package/daemon/cmd/genaccounts.go +22 -18
- package/daemon/cmd/root.go +253 -52
- package/daemon/cmd/root_test.go +190 -2
- package/daemon/cmd/testnet.go +17 -6
- package/daemon/main.go +6 -3
- package/e2e_test/Makefile +29 -0
- package/e2e_test/README.md +100 -0
- package/e2e_test/go.mod +217 -0
- package/e2e_test/go.sum +1323 -0
- package/e2e_test/ibc_conformance_test.go +56 -0
- package/e2e_test/pfm_test.go +613 -0
- package/e2e_test/util.go +271 -0
- package/git-revision.txt +1 -0
- package/go.mod +207 -0
- package/go.sum +1877 -0
- package/package.json +13 -21
- package/proto/agoric/swingset/genesis.proto +14 -0
- package/proto/agoric/swingset/swingset.proto +6 -3
- package/proto/agoric/vlocalchain/.clang-format +7 -0
- package/proto/agoric/vlocalchain/vlocalchain.proto +31 -0
- package/proto/agoric/vstorage/query.proto +53 -1
- package/proto/agoric/vtransfer/genesis.proto +18 -0
- package/scripts/protocgen.sh +16 -6
- package/third_party/proto/buf.yaml +1 -0
- package/third_party/proto/cosmos/base/query/v1beta1/pagination.proto +4 -1
- package/third_party/proto/cosmos/base/v1beta1/coin.proto +7 -4
- package/third_party/proto/cosmos/upgrade/v1beta1/upgrade.proto +16 -6
- package/third_party/proto/cosmos_proto/cosmos.proto +97 -0
- package/third_party/proto/google/api/annotations.proto +1 -1
- package/third_party/proto/google/api/http.proto +181 -120
- package/third_party/proto/google/api/httpbody.proto +9 -6
- package/third_party/proto/google/protobuf/any.proto +1 -7
- package/third_party/proto/ibc/core/channel/v1/channel.proto +15 -1
- package/third_party/proto/ibc/core/client/v1/client.proto +9 -6
- package/tools/tools.go +11 -0
- package/types/events.go +41 -0
- package/types/kv_entry.go +114 -0
- package/types/kv_entry_helpers.go +276 -0
- package/types/kv_entry_helpers_test.go +237 -0
- package/types/kv_entry_test.go +143 -0
- package/upgradegaia.sh +75 -0
- package/vm/action.go +134 -0
- package/vm/action_test.go +149 -0
- package/vm/client.go +113 -0
- package/vm/client_test.go +182 -0
- package/vm/controller.go +49 -0
- package/vm/core_proposals.go +51 -0
- package/vm/jsonrpcconn/jsonrpcconn.go +160 -0
- package/vm/jsonrpcconn/jsonrpcconn_test.go +126 -0
- package/vm/proto_json.go +38 -0
- package/vm/proto_json_test.go +103 -0
- package/vm/server.go +124 -0
- package/x/swingset/abci.go +26 -37
- package/x/swingset/alias.go +9 -6
- package/x/swingset/client/cli/query.go +2 -2
- package/x/swingset/client/cli/tx.go +52 -33
- package/x/swingset/client/proposal_handler.go +2 -17
- package/x/swingset/genesis.go +163 -37
- package/x/swingset/handler.go +2 -1
- package/x/swingset/keeper/extension_snapshotter.go +309 -0
- package/x/swingset/keeper/extension_snapshotter_test.go +104 -0
- package/x/swingset/keeper/keeper.go +95 -48
- package/x/swingset/keeper/keeper_test.go +77 -0
- package/x/swingset/keeper/migrations.go +7 -2
- package/x/swingset/keeper/msg_server.go +71 -56
- package/x/swingset/keeper/proposal.go +15 -9
- package/x/swingset/keeper/querier.go +24 -15
- package/x/swingset/keeper/swing_store_exports_handler.go +872 -0
- package/x/swingset/keeper/swing_store_exports_handler_test.go +247 -0
- package/x/swingset/keeper/test_utils.go +16 -0
- package/x/swingset/module.go +33 -11
- package/x/swingset/proposal_handler.go +5 -4
- package/x/swingset/swingset.go +73 -0
- package/x/swingset/testing/queue.go +17 -0
- package/x/swingset/types/codec.go +2 -2
- package/x/swingset/types/default-params.go +23 -17
- package/x/swingset/types/expected_keepers.go +14 -2
- package/x/swingset/types/genesis.pb.go +361 -18
- package/x/swingset/types/msgs.go +87 -26
- package/x/swingset/types/msgs.pb.go +16 -16
- package/x/swingset/types/params.go +76 -1
- package/x/swingset/types/params_test.go +116 -0
- package/x/swingset/types/proposal.go +10 -9
- package/x/swingset/types/swingset.pb.go +83 -81
- package/x/swingset/types/types.go +30 -28
- package/x/vbank/genesis.go +0 -2
- package/x/vbank/handler.go +2 -1
- package/x/vbank/keeper/keeper.go +3 -2
- package/x/vbank/keeper/querier.go +9 -4
- package/x/vbank/keeper/rewards.go +1 -1
- package/x/vbank/module.go +0 -5
- package/x/vbank/types/msgs.go +0 -12
- package/x/vbank/vbank.go +34 -32
- package/x/vbank/vbank_test.go +20 -20
- package/x/vibc/alias.go +3 -0
- package/x/vibc/handler.go +17 -15
- package/x/vibc/keeper/keeper.go +113 -89
- package/x/vibc/keeper/triggers.go +101 -0
- package/x/vibc/module.go +5 -8
- package/x/vibc/types/expected_keepers.go +28 -6
- package/x/vibc/types/ibc_module.go +336 -0
- package/x/vibc/types/msgs.go +1 -1
- package/x/vibc/types/msgs.pb.go +1 -1
- package/x/vibc/types/receiver.go +170 -0
- package/x/vlocalchain/alias.go +19 -0
- package/x/vlocalchain/handler.go +21 -0
- package/x/vlocalchain/keeper/keeper.go +279 -0
- package/x/vlocalchain/keeper/keeper_test.go +97 -0
- package/x/vlocalchain/types/codec.go +34 -0
- package/x/vlocalchain/types/key.go +27 -0
- package/x/vlocalchain/types/msgs.go +16 -0
- package/x/vlocalchain/types/vlocalchain.pb.go +1072 -0
- package/x/vlocalchain/vlocalchain.go +114 -0
- package/x/vlocalchain/vlocalchain_test.go +434 -0
- package/x/vstorage/README.md +138 -0
- package/x/vstorage/capdata/capdata.go +298 -0
- package/x/vstorage/capdata/capdata_test.go +352 -0
- package/x/vstorage/client/cli/query.go +51 -4
- package/x/vstorage/genesis.go +0 -3
- package/x/vstorage/handler.go +2 -1
- package/x/vstorage/keeper/grpc_query.go +220 -0
- package/x/vstorage/keeper/keeper.go +75 -57
- package/x/vstorage/keeper/keeper_grpc_test.go +300 -0
- package/x/vstorage/keeper/keeper_test.go +40 -62
- package/x/vstorage/keeper/querier.go +39 -14
- package/x/vstorage/keeper/querier_test.go +113 -0
- package/x/vstorage/module.go +0 -5
- package/x/vstorage/testing/queue.go +27 -0
- package/x/vstorage/types/path_keys.go +22 -10
- package/x/vstorage/types/path_keys_test.go +84 -18
- package/x/vstorage/types/query.pb.go +646 -36
- package/x/vstorage/types/query.pb.gw.go +119 -0
- package/x/vstorage/types/types.go +0 -66
- package/x/vstorage/vstorage.go +33 -35
- package/x/vstorage/vstorage_test.go +19 -19
- package/x/vtransfer/alias.go +13 -0
- package/x/vtransfer/genesis.go +39 -0
- package/x/vtransfer/genesis_test.go +12 -0
- package/x/vtransfer/handler.go +20 -0
- package/x/vtransfer/ibc_middleware.go +186 -0
- package/x/vtransfer/ibc_middleware_test.go +448 -0
- package/x/vtransfer/keeper/keeper.go +281 -0
- package/x/vtransfer/module.go +124 -0
- package/x/vtransfer/types/expected_keepers.go +38 -0
- package/x/vtransfer/types/genesis.pb.go +327 -0
- package/x/vtransfer/types/key.go +9 -0
- package/x/vtransfer/types/msgs.go +9 -0
- package/app/const.go +0 -6
- package/cmd/helper/main.go +0 -21
- package/proto/agoric/lien/genesis.proto +0 -25
- package/proto/agoric/lien/lien.proto +0 -25
- package/x/lien/alias.go +0 -17
- package/x/lien/genesis.go +0 -58
- package/x/lien/genesis_test.go +0 -101
- package/x/lien/keeper/account.go +0 -285
- package/x/lien/keeper/keeper.go +0 -254
- package/x/lien/keeper/keeper_test.go +0 -623
- package/x/lien/lien.go +0 -203
- package/x/lien/lien_test.go +0 -529
- package/x/lien/module.go +0 -115
- package/x/lien/spec/01_concepts.md +0 -146
- package/x/lien/spec/02_messages.md +0 -96
- package/x/lien/types/accountkeeper.go +0 -81
- package/x/lien/types/accountstate.go +0 -27
- package/x/lien/types/expected_keepers.go +0 -18
- package/x/lien/types/genesis.pb.go +0 -567
- package/x/lien/types/key.go +0 -25
- package/x/lien/types/lien.pb.go +0 -403
- package/x/swingset/keeper/snapshotter.go +0 -491
- package/x/swingset/keeper/snapshotter_test.go +0 -185
- package/x/swingset/legacy/v32/params.go +0 -37
- package/x/swingset/legacy/v32/params_test.go +0 -133
- package/x/vibc/ibc.go +0 -442
- /package/{src/index.cjs → index.cjs} +0 -0
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
package vm_test
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"context"
|
|
5
|
+
"encoding/json"
|
|
6
|
+
"fmt"
|
|
7
|
+
"net/rpc"
|
|
8
|
+
"testing"
|
|
9
|
+
"time"
|
|
10
|
+
|
|
11
|
+
"github.com/Agoric/agoric-sdk/golang/cosmos/vm"
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
type errorWrapper struct {
|
|
15
|
+
Error string `json:"error"`
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// ConnectVMClientCodec creates an RPC client codec and a sender to the
|
|
19
|
+
// in-process implementation of the VM.
|
|
20
|
+
func ConnectVMClientCodec(ctx context.Context, nodePort int, sendFunc func(int, int, string)) (*vm.ClientCodec, vm.Sender) {
|
|
21
|
+
vmClientCodec := vm.NewClientCodec(ctx, sendFunc)
|
|
22
|
+
vmClient := rpc.NewClientWithCodec(vmClientCodec)
|
|
23
|
+
|
|
24
|
+
sendToNode := func(ctx context.Context, needReply bool, jsonRequest string) (jsonReply string, err error) {
|
|
25
|
+
if jsonRequest == "shutdown" {
|
|
26
|
+
return "", vmClientCodec.Close()
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
msg := vm.Message{
|
|
30
|
+
Port: nodePort,
|
|
31
|
+
NeedsReply: needReply,
|
|
32
|
+
Data: jsonRequest,
|
|
33
|
+
}
|
|
34
|
+
var reply string
|
|
35
|
+
err = vmClient.Call(vm.ReceiveMessageMethod, msg, &reply)
|
|
36
|
+
return reply, err
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return vmClientCodec, sendToNode
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
type Fixture struct {
|
|
43
|
+
SendToNode vm.Sender
|
|
44
|
+
SendToGo func(port int, msgStr string) string
|
|
45
|
+
ReplyToGo func(replyPort int, isError bool, respStr string) int
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
func NewFixture(t *testing.T) *Fixture {
|
|
49
|
+
nodePort := 42
|
|
50
|
+
|
|
51
|
+
f := &Fixture{}
|
|
52
|
+
agdServer := vm.NewAgdServer()
|
|
53
|
+
|
|
54
|
+
sendFunc := func(port int, reply int, str string) {
|
|
55
|
+
switch str {
|
|
56
|
+
case "ping":
|
|
57
|
+
time.AfterFunc(100*time.Millisecond, func() {
|
|
58
|
+
fmt.Printf("sendFunc: port=%d, reply=%d, str=%s\n", port, reply, str)
|
|
59
|
+
if reply != 0 {
|
|
60
|
+
f.ReplyToGo(reply, false, "pong")
|
|
61
|
+
}
|
|
62
|
+
})
|
|
63
|
+
case "wait":
|
|
64
|
+
time.AfterFunc(300*time.Millisecond, func() {
|
|
65
|
+
fmt.Printf("sendFunc: port=%d, reply=%d, str=%s\n", port, reply, str)
|
|
66
|
+
if reply != 0 {
|
|
67
|
+
f.ReplyToGo(reply, false, "done-waiting")
|
|
68
|
+
}
|
|
69
|
+
})
|
|
70
|
+
default:
|
|
71
|
+
t.Errorf("Unexpected message %s", str)
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
vmClientCodec, sendToNode := ConnectVMClientCodec(
|
|
76
|
+
context.Background(),
|
|
77
|
+
int(nodePort),
|
|
78
|
+
sendFunc,
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
f.SendToNode = sendToNode
|
|
82
|
+
f.SendToGo = func(port int, msgStr string) string {
|
|
83
|
+
fmt.Println("Send to Go", msgStr)
|
|
84
|
+
var respStr string
|
|
85
|
+
message := &vm.Message{
|
|
86
|
+
Port: int(port),
|
|
87
|
+
NeedsReply: true,
|
|
88
|
+
Data: msgStr,
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
err := agdServer.ReceiveMessage(message, &respStr)
|
|
92
|
+
if err == nil {
|
|
93
|
+
return respStr
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// fmt.Fprintln(os.Stderr, "Cannot receive from controller", err)
|
|
97
|
+
errResp := errorWrapper{
|
|
98
|
+
Error: err.Error(),
|
|
99
|
+
}
|
|
100
|
+
respBytes, err := json.Marshal(&errResp)
|
|
101
|
+
if err != nil {
|
|
102
|
+
panic(err)
|
|
103
|
+
}
|
|
104
|
+
return string(respBytes)
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
f.ReplyToGo = func(replyPort int, isError bool, respStr string) int {
|
|
108
|
+
if err := vmClientCodec.Receive(replyPort, isError, respStr); err != nil {
|
|
109
|
+
return 1
|
|
110
|
+
}
|
|
111
|
+
return 0
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return f
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
func TestClient_oneOutbound(t *testing.T) {
|
|
118
|
+
f := NewFixture(t)
|
|
119
|
+
|
|
120
|
+
// Try one blocking call.
|
|
121
|
+
ret, err := f.SendToNode(context.Background(), true, "ping")
|
|
122
|
+
if err != nil {
|
|
123
|
+
t.Error(err)
|
|
124
|
+
}
|
|
125
|
+
if ret != "pong" {
|
|
126
|
+
t.Errorf("ping want pong, got %s", ret)
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
func TestClient_ShortBg(t *testing.T) {
|
|
131
|
+
|
|
132
|
+
f := NewFixture(t)
|
|
133
|
+
|
|
134
|
+
// Try a short background call with a long overlapping call.
|
|
135
|
+
done := make(chan struct{})
|
|
136
|
+
go func() {
|
|
137
|
+
defer close(done)
|
|
138
|
+
ret, err := f.SendToNode(context.Background(), true, "ping")
|
|
139
|
+
if err != nil {
|
|
140
|
+
t.Error(err)
|
|
141
|
+
}
|
|
142
|
+
if ret != "pong" {
|
|
143
|
+
t.Errorf("ping want pong, got %s", ret)
|
|
144
|
+
}
|
|
145
|
+
}()
|
|
146
|
+
|
|
147
|
+
ret, err := f.SendToNode(context.Background(), true, "wait")
|
|
148
|
+
if err != nil {
|
|
149
|
+
t.Error(err)
|
|
150
|
+
}
|
|
151
|
+
if ret != "done-waiting" {
|
|
152
|
+
t.Errorf("wait want done-waiting, got %s", ret)
|
|
153
|
+
}
|
|
154
|
+
<-done
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
func TestClient_LongBg(t *testing.T) {
|
|
158
|
+
|
|
159
|
+
f := NewFixture(t)
|
|
160
|
+
|
|
161
|
+
// Try a long background call with a short overlapping call.
|
|
162
|
+
done := make(chan struct{})
|
|
163
|
+
go func() {
|
|
164
|
+
defer close(done)
|
|
165
|
+
ret, err := f.SendToNode(context.Background(), true, "wait")
|
|
166
|
+
if err != nil {
|
|
167
|
+
t.Error(err)
|
|
168
|
+
}
|
|
169
|
+
if ret != "done-waiting" {
|
|
170
|
+
t.Errorf("ping want done-waiting, got %s", ret)
|
|
171
|
+
}
|
|
172
|
+
}()
|
|
173
|
+
|
|
174
|
+
ret, err := f.SendToNode(context.Background(), true, "ping")
|
|
175
|
+
if err != nil {
|
|
176
|
+
t.Error(err)
|
|
177
|
+
}
|
|
178
|
+
if ret != "pong" {
|
|
179
|
+
t.Errorf("wait want pong, got %s", ret)
|
|
180
|
+
}
|
|
181
|
+
<-done
|
|
182
|
+
}
|
package/vm/controller.go
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
package vm
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"context"
|
|
5
|
+
"fmt"
|
|
6
|
+
|
|
7
|
+
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
8
|
+
)
|
|
9
|
+
|
|
10
|
+
// Sender makes a request of our associated VM.
|
|
11
|
+
type Sender func(ctx context.Context, needReply bool, jsonRequest string) (jsonReply string, err error)
|
|
12
|
+
|
|
13
|
+
type ControllerAdmissionMsg interface {
|
|
14
|
+
sdk.Msg
|
|
15
|
+
CheckAdmissibility(sdk.Context, interface{}) error
|
|
16
|
+
|
|
17
|
+
// GetInboundMsgCount returns the number of Swingset messages which will
|
|
18
|
+
// be added to the inboundQueue.
|
|
19
|
+
GetInboundMsgCount() int32
|
|
20
|
+
|
|
21
|
+
// IsHighPriority returns whether the message should be considered for
|
|
22
|
+
// high priority processing, including bypass of some inbound checks
|
|
23
|
+
// and queueing on higher priority queues.
|
|
24
|
+
IsHighPriority(sdk.Context, interface{}) (bool, error)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
type PortHandler interface {
|
|
28
|
+
Receive(context.Context, string) (string, error)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
type protectedPortHandler struct {
|
|
32
|
+
inner PortHandler
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
func (h protectedPortHandler) Receive(ctx context.Context, str string) (ret string, err error) {
|
|
36
|
+
defer func() {
|
|
37
|
+
if r := recover(); r != nil {
|
|
38
|
+
// Propagate just the string, not the error stack or we will invite
|
|
39
|
+
// nondeterminism.
|
|
40
|
+
err = fmt.Errorf("panic: %s", r)
|
|
41
|
+
}
|
|
42
|
+
}()
|
|
43
|
+
ret, err = h.inner.Receive(ctx, str)
|
|
44
|
+
return
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
func NewProtectedPortHandler(inner PortHandler) PortHandler {
|
|
48
|
+
return protectedPortHandler{inner}
|
|
49
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
package vm
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"encoding/json"
|
|
5
|
+
"fmt"
|
|
6
|
+
)
|
|
7
|
+
|
|
8
|
+
// CoreProposalStep is a set of core proposal configs which are executed
|
|
9
|
+
// concurrently
|
|
10
|
+
type CoreProposalStep []Jsonable
|
|
11
|
+
|
|
12
|
+
// CoreProposals is one possible shape for core proposals expressed as a series
|
|
13
|
+
// of sequential steps
|
|
14
|
+
// see SequentialCoreProposals in packages/deploy-script-support/src/extract-proposal.js
|
|
15
|
+
type CoreProposals struct {
|
|
16
|
+
Steps []CoreProposalStep `json:"steps"`
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
type ArbitraryCoreProposal struct {
|
|
20
|
+
Json json.RawMessage
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
func NewArbitraryCoreProposal(jsonStr string) *ArbitraryCoreProposal {
|
|
24
|
+
return &ArbitraryCoreProposal{Json: []byte(jsonStr)}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// CoreProposalStepForModules generates a single core proposal step from
|
|
28
|
+
// the given modules, which will be executed concurrently during that step
|
|
29
|
+
func CoreProposalStepForModules(modules ...Jsonable) CoreProposalStep {
|
|
30
|
+
step := make([]Jsonable, len(modules))
|
|
31
|
+
for i := range modules {
|
|
32
|
+
switch m := modules[i].(type) {
|
|
33
|
+
case ArbitraryCoreProposal:
|
|
34
|
+
step[i] = m.Json
|
|
35
|
+
case string:
|
|
36
|
+
step[i] = m
|
|
37
|
+
default:
|
|
38
|
+
panic(fmt.Errorf("unexpected step type %T", m))
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return step
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// CoreProposalsFromSteps returns a CoreProposals from the given steps
|
|
45
|
+
func CoreProposalsFromSteps(steps ...CoreProposalStep) *CoreProposals {
|
|
46
|
+
if steps == nil {
|
|
47
|
+
// Deal with https://github.com/golang/go/issues/37711
|
|
48
|
+
return &CoreProposals{Steps: []CoreProposalStep{}}
|
|
49
|
+
}
|
|
50
|
+
return &CoreProposals{Steps: steps}
|
|
51
|
+
}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Package jsonrpcconn provides a way to multiplex an io stream into two
|
|
3
|
+
streams where incoming JSON-RPC requests go to one stream and incoming
|
|
4
|
+
JSON-RPC responses go to another. Outputs are merged.
|
|
5
|
+
|
|
6
|
+
The JSON-RPCv1 protocol is peer-to-peer, but the implementation in the Go
|
|
7
|
+
standard library only supports client-server. By multiplexing a single
|
|
8
|
+
io.ReadWriteCloser stream into separate server (receives requests) and
|
|
9
|
+
client (receives responses) streams, two RPC halves can share the same
|
|
10
|
+
underlying connection.
|
|
11
|
+
*/
|
|
12
|
+
package jsonrpcconn
|
|
13
|
+
|
|
14
|
+
import (
|
|
15
|
+
"encoding/json"
|
|
16
|
+
"io"
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
// jsonRpcMsg can unmarshal either a JSON-RPC
|
|
20
|
+
// request or response object.
|
|
21
|
+
type jsonRpcMsg struct {
|
|
22
|
+
Error json.RawMessage `json:"error"`
|
|
23
|
+
Id json.RawMessage `json:"id"`
|
|
24
|
+
Method *string `json:"method"`
|
|
25
|
+
Params []json.RawMessage `json:"params"`
|
|
26
|
+
Result json.RawMessage `json:"result"`
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// mux holds the underlying connection and the pipe reader/writer
|
|
30
|
+
// pairs for the server (request) and client (response) sides.
|
|
31
|
+
// Any protocol error or closing any channel will cause shutdown.
|
|
32
|
+
type mux struct {
|
|
33
|
+
conn io.ReadWriteCloser
|
|
34
|
+
reqReader *io.PipeReader
|
|
35
|
+
reqWriter *io.PipeWriter
|
|
36
|
+
respReader *io.PipeReader
|
|
37
|
+
respWriter *io.PipeWriter
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
func newMux(conn io.ReadWriteCloser) mux {
|
|
41
|
+
reqReader, reqWriter := io.Pipe()
|
|
42
|
+
respReader, respWriter := io.Pipe()
|
|
43
|
+
m := mux{
|
|
44
|
+
conn: conn,
|
|
45
|
+
reqReader: reqReader,
|
|
46
|
+
reqWriter: reqWriter,
|
|
47
|
+
respReader: respReader,
|
|
48
|
+
respWriter: respWriter,
|
|
49
|
+
}
|
|
50
|
+
go m.input()
|
|
51
|
+
return m
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
func (m mux) input() {
|
|
55
|
+
var err error
|
|
56
|
+
dec := json.NewDecoder(m.conn)
|
|
57
|
+
for {
|
|
58
|
+
// read the next JSON value, preserve its wire format
|
|
59
|
+
var raw json.RawMessage
|
|
60
|
+
err = dec.Decode(&raw)
|
|
61
|
+
if err != nil {
|
|
62
|
+
break
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// parse as JSON-RPC
|
|
66
|
+
var msg jsonRpcMsg
|
|
67
|
+
err = json.Unmarshal(raw, &msg)
|
|
68
|
+
if err != nil {
|
|
69
|
+
break
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// send to one of the outputs
|
|
73
|
+
if msg.Method != nil {
|
|
74
|
+
// presume a request, the consumer will handle any missing fields
|
|
75
|
+
_, err = m.reqWriter.Write(raw)
|
|
76
|
+
} else {
|
|
77
|
+
// presume a response, the consumer will handle any missing fields
|
|
78
|
+
_, err = m.respWriter.Write(raw)
|
|
79
|
+
}
|
|
80
|
+
if err != nil {
|
|
81
|
+
break
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
m.reqWriter.CloseWithError(err)
|
|
85
|
+
m.respWriter.CloseWithError(err)
|
|
86
|
+
m.conn.Close()
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// clientChan is a view of the mux for the client channel.
|
|
90
|
+
type clientChan mux
|
|
91
|
+
|
|
92
|
+
// Close implements the io.Closer interface.
|
|
93
|
+
func (c clientChan) Close() error {
|
|
94
|
+
return c.conn.Close()
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// Read implements the io.Reader interface.
|
|
98
|
+
func (c clientChan) Read(p []byte) (int, error) {
|
|
99
|
+
return c.respReader.Read(p)
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// Write implements the io.Writer interface.
|
|
103
|
+
func (c clientChan) Write(p []byte) (int, error) {
|
|
104
|
+
return c.conn.Write(p)
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// serverChan is a view of the mux for the server channel.
|
|
108
|
+
type serverChan mux
|
|
109
|
+
|
|
110
|
+
// Close implements the io.Closer interface.
|
|
111
|
+
func (s serverChan) Close() error {
|
|
112
|
+
return s.conn.Close()
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// Read implements the io.Reader interface.
|
|
116
|
+
func (s serverChan) Read(p []byte) (int, error) {
|
|
117
|
+
return s.reqReader.Read(p)
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// Write implements the io.Writer interface.
|
|
121
|
+
func (s serverChan) Write(p []byte) (int, error) {
|
|
122
|
+
return s.conn.Write(p)
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// ClientServerConn multiplexes an input/output stream for the JSON-RPCv1
|
|
126
|
+
// protocol into streams specific for client traffic and server traffic.
|
|
127
|
+
// Full JSON objects must be written atomically to either stream to
|
|
128
|
+
// interleave correctly.
|
|
129
|
+
func ClientServerConn(conn io.ReadWriteCloser) (clientConn io.ReadWriteCloser, serverConn io.ReadWriteCloser) {
|
|
130
|
+
m := newMux(conn)
|
|
131
|
+
clientConn = clientChan(m)
|
|
132
|
+
serverConn = serverChan(m)
|
|
133
|
+
return
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
type conn struct {
|
|
137
|
+
rd io.ReadCloser
|
|
138
|
+
wr io.WriteCloser
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// Close implments the io.Closer interface.
|
|
142
|
+
func (e conn) Close() error {
|
|
143
|
+
e.rd.Close()
|
|
144
|
+
return e.wr.Close()
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// Read implements the io.Reader interface.
|
|
148
|
+
func (e conn) Read(p []byte) (int, error) {
|
|
149
|
+
return e.rd.Read(p)
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// Write implements the io.Writer interface.
|
|
153
|
+
func (e conn) Write(p []byte) (int, error) {
|
|
154
|
+
return e.wr.Write(p)
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// NewConn returns a connection from a reader and a writer.
|
|
158
|
+
func NewConn(rd io.ReadCloser, wr io.WriteCloser) io.ReadWriteCloser {
|
|
159
|
+
return conn{rd: rd, wr: wr}
|
|
160
|
+
}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
package jsonrpcconn_test
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"fmt"
|
|
5
|
+
"net"
|
|
6
|
+
"net/rpc"
|
|
7
|
+
"net/rpc/jsonrpc"
|
|
8
|
+
"testing"
|
|
9
|
+
|
|
10
|
+
"github.com/Agoric/agoric-sdk/golang/cosmos/vm/jsonrpcconn"
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
/*
|
|
14
|
+
type testConn struct {
|
|
15
|
+
input *bytes.Buffer
|
|
16
|
+
output *bytes.Buffer
|
|
17
|
+
done chan struct{}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
func newTestConn(input string) io.ReadWriteCloser {
|
|
21
|
+
return testConn{
|
|
22
|
+
input: bytes.NewBufferString(input),
|
|
23
|
+
output: new(bytes.Buffer),
|
|
24
|
+
done: make(chan struct{}),
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
func (tc testConn) Read(p []byte) (int, error) {
|
|
29
|
+
n, err := tc.input.Read(p)
|
|
30
|
+
if err == io.EOF {
|
|
31
|
+
<-tc.done
|
|
32
|
+
}
|
|
33
|
+
return n, err
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
func (tc testConn) Write(p []byte) (int, error) {
|
|
37
|
+
return tc.output.Write(p)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
func (tc testConn) Close() error {
|
|
41
|
+
close(tc.done)
|
|
42
|
+
return nil
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
func TestMux(t *testing.T) {
|
|
46
|
+
input := `{"id": 1, "method": "foo", "params": [1, 2]}
|
|
47
|
+
{"id": "aaa", "result": true, "error": null}
|
|
48
|
+
{"id": null, "method": "bar", "params": ["string", 3, false]}`
|
|
49
|
+
tc := newTestConn(input)
|
|
50
|
+
client, server := jsonrpcconn.ClientServerConn(tc)
|
|
51
|
+
client.Read
|
|
52
|
+
var wg sync.WaitGroup
|
|
53
|
+
wg.Add(2)
|
|
54
|
+
go func() {
|
|
55
|
+
|
|
56
|
+
}()
|
|
57
|
+
wg.Wait()
|
|
58
|
+
} */
|
|
59
|
+
|
|
60
|
+
type Args struct {
|
|
61
|
+
A, B int
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
type Arith struct{}
|
|
65
|
+
|
|
66
|
+
func (a *Arith) Add(args Args, reply *int) error {
|
|
67
|
+
*reply = args.A + args.B
|
|
68
|
+
return nil
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
func (a *Arith) Mul(args Args, reply *int) error {
|
|
72
|
+
*reply = args.A * args.B
|
|
73
|
+
return nil
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
func (a *Arith) Oops(args Args, reply *int) error {
|
|
77
|
+
return fmt.Errorf("oops")
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
func TestJsonRPC(t *testing.T) {
|
|
81
|
+
left, right := net.Pipe()
|
|
82
|
+
leftClientConn, leftServerConn := jsonrpcconn.ClientServerConn(left)
|
|
83
|
+
rightClientConn, rightServerConn := jsonrpcconn.ClientServerConn(right)
|
|
84
|
+
|
|
85
|
+
leftClient := jsonrpc.NewClient(leftClientConn)
|
|
86
|
+
leftServer := rpc.NewServer()
|
|
87
|
+
err := leftServer.RegisterName("foo", new(Arith))
|
|
88
|
+
if err != nil {
|
|
89
|
+
t.Fatal(err)
|
|
90
|
+
}
|
|
91
|
+
go leftServer.ServeCodec(jsonrpc.NewServerCodec(leftServerConn))
|
|
92
|
+
|
|
93
|
+
rightClient := jsonrpc.NewClient(rightClientConn)
|
|
94
|
+
rightServer := rpc.NewServer()
|
|
95
|
+
err = rightServer.RegisterName("bar", new(Arith))
|
|
96
|
+
if err != nil {
|
|
97
|
+
t.Fatal(err)
|
|
98
|
+
}
|
|
99
|
+
go rightServer.ServeCodec(jsonrpc.NewServerCodec(rightServerConn))
|
|
100
|
+
|
|
101
|
+
var reply int
|
|
102
|
+
err = leftClient.Call("bar.Add", Args{1, 2}, &reply)
|
|
103
|
+
if err != nil {
|
|
104
|
+
t.Error(err)
|
|
105
|
+
}
|
|
106
|
+
if reply != 3 {
|
|
107
|
+
t.Errorf("bar.Add want 3, got %d", reply)
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
err = rightClient.Call("foo.Mul", Args{2, 3}, &reply)
|
|
111
|
+
if err != nil {
|
|
112
|
+
t.Error(err)
|
|
113
|
+
}
|
|
114
|
+
if reply != 6 {
|
|
115
|
+
t.Errorf("foo.Mul want 6, got %d", reply)
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
err = leftClient.Call("bar.Oops", Args{7, 11}, &reply)
|
|
119
|
+
if err == nil {
|
|
120
|
+
t.Errorf("bar.Oops want error, got reply %d", reply)
|
|
121
|
+
} else if err.Error() != "oops" {
|
|
122
|
+
t.Errorf(`bar.Oops want error "oops", got "%s"`, err.Error())
|
|
123
|
+
}
|
|
124
|
+
leftClient.Close()
|
|
125
|
+
rightClient.Close()
|
|
126
|
+
}
|
package/vm/proto_json.go
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
package vm
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"encoding/json"
|
|
5
|
+
|
|
6
|
+
"github.com/gogo/protobuf/jsonpb"
|
|
7
|
+
"github.com/gogo/protobuf/proto"
|
|
8
|
+
)
|
|
9
|
+
|
|
10
|
+
// We need jsonpb for its access to the global registry.
|
|
11
|
+
var marshaller = jsonpb.Marshaler{EmitDefaults: true}
|
|
12
|
+
|
|
13
|
+
func ProtoJSONMarshal(val interface{}) ([]byte, error) {
|
|
14
|
+
if pm, ok := val.(proto.Message); ok {
|
|
15
|
+
var s string
|
|
16
|
+
s, err := marshaller.MarshalToString(pm)
|
|
17
|
+
return []byte(s), err
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// Marshal a non-proto value to JSON.
|
|
21
|
+
return json.Marshal(val)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// ProtoJSONMarshalSlice marshals a slice of proto messages and non-proto values to
|
|
25
|
+
// a single JSON byte slice.
|
|
26
|
+
func ProtoJSONMarshalSlice(vals []interface{}) ([]byte, error) {
|
|
27
|
+
var err error
|
|
28
|
+
jsonSlice := make([]json.RawMessage, len(vals))
|
|
29
|
+
for i, val := range vals {
|
|
30
|
+
jsonSlice[i], err = ProtoJSONMarshal(val)
|
|
31
|
+
if err != nil {
|
|
32
|
+
return nil, err
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Marshal the JSON array to a single JSON byte slice.
|
|
37
|
+
return json.Marshal(jsonSlice)
|
|
38
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
package vm_test
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"bytes"
|
|
5
|
+
"strings"
|
|
6
|
+
"testing"
|
|
7
|
+
|
|
8
|
+
"github.com/Agoric/agoric-sdk/golang/cosmos/vm"
|
|
9
|
+
|
|
10
|
+
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
11
|
+
|
|
12
|
+
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
|
|
13
|
+
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
func TestProtoJSONMarshal(t *testing.T) {
|
|
17
|
+
accAddr, err := sdk.AccAddressFromHexUnsafe("0123456789")
|
|
18
|
+
if err != nil {
|
|
19
|
+
panic(err)
|
|
20
|
+
}
|
|
21
|
+
valAddr, err := sdk.ValAddressFromHex("9876543210")
|
|
22
|
+
if err != nil {
|
|
23
|
+
panic(err)
|
|
24
|
+
}
|
|
25
|
+
coin := sdk.NewInt64Coin("uatom", 1234567)
|
|
26
|
+
|
|
27
|
+
testCases := []struct {
|
|
28
|
+
name string
|
|
29
|
+
create func() interface{}
|
|
30
|
+
expected string
|
|
31
|
+
}{
|
|
32
|
+
{
|
|
33
|
+
"nil",
|
|
34
|
+
func() interface{} {
|
|
35
|
+
return nil
|
|
36
|
+
},
|
|
37
|
+
`null`,
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
"primitive number",
|
|
41
|
+
func() interface{} {
|
|
42
|
+
return 12345
|
|
43
|
+
},
|
|
44
|
+
`12345`,
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
"MsgDelegate",
|
|
48
|
+
func() interface{} {
|
|
49
|
+
return stakingtypes.NewMsgDelegate(accAddr, valAddr, coin)
|
|
50
|
+
},
|
|
51
|
+
`{"delegatorAddress":"cosmos1qy352eufjjmc9c","validatorAddress":"cosmosvaloper1npm9gvss52mlmk","amount":{"denom":"uatom","amount":"1234567"}}`,
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
"QueryDenomOwnersResponse",
|
|
55
|
+
func() interface{} {
|
|
56
|
+
return &banktypes.QueryDenomOwnersResponse{
|
|
57
|
+
DenomOwners: []*banktypes.DenomOwner{
|
|
58
|
+
{
|
|
59
|
+
Address: accAddr.String(),
|
|
60
|
+
Balance: coin,
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
Address: valAddr.String(),
|
|
64
|
+
Balance: coin.Add(coin),
|
|
65
|
+
},
|
|
66
|
+
},
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
`{"denomOwners":[{"address":"cosmos1qy352eufjjmc9c","balance":{"denom":"uatom","amount":"1234567"}},{"address":"cosmosvaloper1npm9gvss52mlmk","balance":{"denom":"uatom","amount":"2469134"}}],"pagination":null}`,
|
|
70
|
+
},
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
for _, tc := range testCases {
|
|
74
|
+
t.Run(tc.name, func(t *testing.T) {
|
|
75
|
+
val := tc.create()
|
|
76
|
+
bz, err := vm.ProtoJSONMarshal(val)
|
|
77
|
+
if err != nil {
|
|
78
|
+
t.Errorf("ProtoJSONMarshal of %q failed %v", val, err)
|
|
79
|
+
}
|
|
80
|
+
if !bytes.Equal(bz, []byte(tc.expected)) {
|
|
81
|
+
t.Errorf("ProtoJSONMarshal of %q returned %q, expected %q", val, string(bz), tc.expected)
|
|
82
|
+
}
|
|
83
|
+
})
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
t.Run("all in a slice", func(t *testing.T) {
|
|
87
|
+
vals := make([]interface{}, len(testCases))
|
|
88
|
+
expectedJson := make([]string, len(testCases))
|
|
89
|
+
for i, tc := range testCases {
|
|
90
|
+
vals[i] = tc.create()
|
|
91
|
+
expectedJson[i] = tc.expected
|
|
92
|
+
}
|
|
93
|
+
bz, err := vm.ProtoJSONMarshalSlice(vals)
|
|
94
|
+
if err != nil {
|
|
95
|
+
t.Errorf("ProtoJSONMarshalSlice of %q failed %v", vals, err)
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
expected := "[" + strings.Join(expectedJson, ",") + "]"
|
|
99
|
+
if !bytes.Equal(bz, []byte(expected)) {
|
|
100
|
+
t.Errorf("ProtoJSONMarshalSlice of %q returned %q, expected %q", vals, string(bz), expected)
|
|
101
|
+
}
|
|
102
|
+
})
|
|
103
|
+
}
|