@agoric/cosmos 0.34.2-dev-f6d77a3.0 → 0.34.2-upgrade-16-dev-8879538.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/git-revision.txt +1 -1
- package/package.json +2 -2
- package/x/vlocalchain/handler.go +1 -2
- package/x/vlocalchain/keeper/keeper_test.go +1 -66
- package/x/vlocalchain/vlocalchain.go +21 -26
- package/x/vlocalchain/vlocalchain_test.go +4 -133
- package/vm/proto_json.go +0 -38
- package/vm/proto_json_test.go +0 -103
package/git-revision.txt
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
8879538
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@agoric/cosmos",
|
|
3
|
-
"version": "0.34.2-dev-
|
|
3
|
+
"version": "0.34.2-upgrade-16-dev-8879538.0+8879538",
|
|
4
4
|
"description": "Connect JS to the Cosmos blockchain SDK",
|
|
5
5
|
"parsers": {
|
|
6
6
|
"js": "mjs"
|
|
@@ -39,5 +39,5 @@
|
|
|
39
39
|
"typeCoverage": {
|
|
40
40
|
"atLeast": 0
|
|
41
41
|
},
|
|
42
|
-
"gitHead": "
|
|
42
|
+
"gitHead": "8879538cd1d125a08346f02dd5701d0d70c90bb8"
|
|
43
43
|
}
|
package/x/vlocalchain/handler.go
CHANGED
|
@@ -3,7 +3,6 @@ package vlocalchain
|
|
|
3
3
|
import (
|
|
4
4
|
"fmt"
|
|
5
5
|
|
|
6
|
-
sdkioerrors "cosmossdk.io/errors"
|
|
7
6
|
"github.com/Agoric/agoric-sdk/golang/cosmos/x/vlocalchain/keeper"
|
|
8
7
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
9
8
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
|
@@ -15,7 +14,7 @@ func NewHandler(keeper keeper.Keeper) sdk.Handler {
|
|
|
15
14
|
switch msg := msg.(type) {
|
|
16
15
|
default:
|
|
17
16
|
errMsg := fmt.Sprintf("Unrecognized vlocalchain Msg type: %T", msg)
|
|
18
|
-
return nil,
|
|
17
|
+
return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, errMsg)
|
|
19
18
|
}
|
|
20
19
|
}
|
|
21
20
|
}
|
|
@@ -1,15 +1,6 @@
|
|
|
1
1
|
package keeper
|
|
2
2
|
|
|
3
|
-
import
|
|
4
|
-
"testing"
|
|
5
|
-
|
|
6
|
-
"github.com/stretchr/testify/require"
|
|
7
|
-
|
|
8
|
-
"github.com/Agoric/agoric-sdk/golang/cosmos/app/params"
|
|
9
|
-
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
10
|
-
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
|
|
11
|
-
)
|
|
12
|
-
|
|
3
|
+
import "testing"
|
|
13
4
|
|
|
14
5
|
func TestKeeper_ParseRequestTypeURL(t *testing.T) {
|
|
15
6
|
testCases := []struct {
|
|
@@ -39,59 +30,3 @@ func TestKeeper_ParseRequestTypeURL(t *testing.T) {
|
|
|
39
30
|
})
|
|
40
31
|
}
|
|
41
32
|
}
|
|
42
|
-
|
|
43
|
-
func TestKeeper_DeserializeTxMessages(t *testing.T) {
|
|
44
|
-
encodingConfig := params.MakeEncodingConfig()
|
|
45
|
-
cdc := encodingConfig.Marshaler
|
|
46
|
-
|
|
47
|
-
banktypes.RegisterInterfaces(encodingConfig.InterfaceRegistry)
|
|
48
|
-
|
|
49
|
-
keeper := NewKeeper(cdc, nil, nil, nil)
|
|
50
|
-
|
|
51
|
-
expectedMsgSend := []sdk.Msg{
|
|
52
|
-
&banktypes.MsgSend{
|
|
53
|
-
FromAddress: "cosmos1abc",
|
|
54
|
-
ToAddress: "cosmos1xyz",
|
|
55
|
-
Amount: sdk.NewCoins(sdk.NewCoin("stake", sdk.NewInt(100))),
|
|
56
|
-
},
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
testCases := []struct {
|
|
60
|
-
name string
|
|
61
|
-
json string
|
|
62
|
-
expected []sdk.Msg
|
|
63
|
-
wantErr bool
|
|
64
|
-
}{
|
|
65
|
-
{
|
|
66
|
-
name: "camelCase keys",
|
|
67
|
-
json: `{"messages":[{"@type":"/cosmos.bank.v1beta1.MsgSend","fromAddress":"cosmos1abc","toAddress":"cosmos1xyz","amount":[{"denom":"stake","amount":"100"}]}]}`,
|
|
68
|
-
expected: expectedMsgSend,
|
|
69
|
-
wantErr: false,
|
|
70
|
-
},
|
|
71
|
-
{
|
|
72
|
-
name: "snake_case keys",
|
|
73
|
-
json: `{"messages":[{"@type":"/cosmos.bank.v1beta1.MsgSend","from_address":"cosmos1abc","to_address":"cosmos1xyz","amount":[{"denom":"stake","amount":"100"}]}]}`,
|
|
74
|
-
expected: expectedMsgSend,
|
|
75
|
-
wantErr: false,
|
|
76
|
-
},
|
|
77
|
-
{
|
|
78
|
-
name: "misspelled key",
|
|
79
|
-
json: `{"messages":[{"@type":"/cosmos.bank.v1beta1.MsgSend","from_addresss":"cosmos1abc","to_address":"cosmos1xyz","amount":[{"denom":"stake","amount":"100"}]}]}`,
|
|
80
|
-
expected: expectedMsgSend,
|
|
81
|
-
wantErr: true,
|
|
82
|
-
},
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
for _, tc := range testCases {
|
|
86
|
-
t.Run(tc.name, func(t *testing.T) {
|
|
87
|
-
msgs, err := keeper.DeserializeTxMessages([]byte(tc.json))
|
|
88
|
-
|
|
89
|
-
if tc.wantErr {
|
|
90
|
-
require.Error(t, err)
|
|
91
|
-
} else {
|
|
92
|
-
require.NoError(t, err)
|
|
93
|
-
require.Equal(t, tc.expected, msgs)
|
|
94
|
-
}
|
|
95
|
-
})
|
|
96
|
-
}
|
|
97
|
-
}
|
|
@@ -6,6 +6,7 @@ import (
|
|
|
6
6
|
"fmt"
|
|
7
7
|
|
|
8
8
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
9
|
+
"github.com/gogo/protobuf/jsonpb"
|
|
9
10
|
|
|
10
11
|
"github.com/Agoric/agoric-sdk/golang/cosmos/vm"
|
|
11
12
|
"github.com/Agoric/agoric-sdk/golang/cosmos/x/vlocalchain/keeper"
|
|
@@ -55,36 +56,28 @@ func (h portHandler) Receive(cctx context.Context, str string) (ret string, err
|
|
|
55
56
|
return
|
|
56
57
|
}
|
|
57
58
|
|
|
58
|
-
|
|
59
|
-
|
|
59
|
+
// We need jsonpb for its access to the global registry.
|
|
60
|
+
marshaller := jsonpb.Marshaler{EmitDefaults: true, OrigName: true}
|
|
61
|
+
|
|
62
|
+
var s string
|
|
63
|
+
resps := make([]json.RawMessage, len(qms))
|
|
60
64
|
for i, qm := range qms {
|
|
61
65
|
var qr *types.QueryResponse
|
|
62
66
|
qr, err = h.keeper.Query(cctx, qm)
|
|
63
|
-
if err
|
|
64
|
-
|
|
65
|
-
resps[i] = qr
|
|
66
|
-
} else {
|
|
67
|
-
errs = append(errs, err) // Accumulate errors
|
|
68
|
-
resps[i] = &types.QueryResponse{Error: err.Error()}
|
|
67
|
+
if err != nil {
|
|
68
|
+
return
|
|
69
69
|
}
|
|
70
|
+
if s, err = marshaller.MarshalToString(qr); err != nil {
|
|
71
|
+
return
|
|
72
|
+
}
|
|
73
|
+
resps[i] = []byte(s)
|
|
70
74
|
}
|
|
71
75
|
|
|
72
|
-
bz
|
|
73
|
-
if err != nil {
|
|
74
|
-
return
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
switch len(errs) {
|
|
78
|
-
case 0:
|
|
79
|
-
err = nil
|
|
80
|
-
case 1:
|
|
81
|
-
err = errs[0]
|
|
82
|
-
case len(resps):
|
|
83
|
-
err = fmt.Errorf("all queries in batch failed: %v", errs)
|
|
84
|
-
default:
|
|
85
|
-
// Let them inspect the individual errors manually.
|
|
76
|
+
var bz []byte
|
|
77
|
+
if bz, err = json.Marshal(resps); err != nil {
|
|
78
|
+
return
|
|
86
79
|
}
|
|
87
|
-
|
|
80
|
+
ret = string(bz)
|
|
88
81
|
|
|
89
82
|
case "VLOCALCHAIN_EXECUTE_TX":
|
|
90
83
|
origCtx := sdk.UnwrapSDKContext(cctx)
|
|
@@ -104,9 +97,11 @@ func (h portHandler) Receive(cctx context.Context, str string) (ret string, err
|
|
|
104
97
|
return
|
|
105
98
|
}
|
|
106
99
|
|
|
107
|
-
|
|
108
|
-
bz,
|
|
109
|
-
|
|
100
|
+
var bz []byte
|
|
101
|
+
if bz, err = json.Marshal(resps); err != nil {
|
|
102
|
+
return
|
|
103
|
+
}
|
|
104
|
+
ret = string(bz)
|
|
110
105
|
default:
|
|
111
106
|
err = fmt.Errorf("unrecognized message type %s", msg.Type)
|
|
112
107
|
}
|
|
@@ -5,7 +5,6 @@ import (
|
|
|
5
5
|
"encoding/json"
|
|
6
6
|
"strings"
|
|
7
7
|
"testing"
|
|
8
|
-
"time"
|
|
9
8
|
|
|
10
9
|
"github.com/Agoric/agoric-sdk/golang/cosmos/app/params"
|
|
11
10
|
"github.com/Agoric/agoric-sdk/golang/cosmos/vm"
|
|
@@ -17,7 +16,6 @@ import (
|
|
|
17
16
|
storetypes "github.com/cosmos/cosmos-sdk/store/types"
|
|
18
17
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
19
18
|
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
|
|
20
|
-
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
|
|
21
19
|
transfertypes "github.com/cosmos/ibc-go/v6/modules/apps/transfer/types"
|
|
22
20
|
"github.com/tendermint/tendermint/libs/log"
|
|
23
21
|
|
|
@@ -79,36 +77,8 @@ func (t *mockTransfer) Transfer(cctx context.Context, msg *transfertypes.MsgTran
|
|
|
79
77
|
return &transfertypes.MsgTransferResponse{Sequence: 1}, nil
|
|
80
78
|
}
|
|
81
79
|
|
|
82
|
-
type mockStaking struct {
|
|
83
|
-
stakingtypes.UnimplementedMsgServer
|
|
84
|
-
stakingtypes.UnimplementedQueryServer
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
var _ stakingtypes.MsgServer = (*mockStaking)(nil)
|
|
88
|
-
var _ stakingtypes.QueryServer = (*mockStaking)(nil)
|
|
89
|
-
|
|
90
|
-
func (s *mockStaking) Undelegate(cctx context.Context, msg *stakingtypes.MsgUndelegate) (*stakingtypes.MsgUndelegateResponse, error) {
|
|
91
|
-
return &stakingtypes.MsgUndelegateResponse{CompletionTime: time.Now().UTC()}, nil
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
func (s *mockStaking) UnbondingDelegation(cctx context.Context, req *stakingtypes.QueryUnbondingDelegationRequest) (*stakingtypes.QueryUnbondingDelegationResponse, error) {
|
|
95
|
-
unbondingDelegation := stakingtypes.UnbondingDelegation{
|
|
96
|
-
DelegatorAddress: req.DelegatorAddr,
|
|
97
|
-
ValidatorAddress: "cosmosvaloper1gghjut3ccd8ay0zduzj64hwre2fxs9ldmqhffj",
|
|
98
|
-
Entries: []stakingtypes.UnbondingDelegationEntry{
|
|
99
|
-
{
|
|
100
|
-
CreationHeight: 100,
|
|
101
|
-
CompletionTime: time.Now().UTC().Add(time.Hour * 24 * 7),
|
|
102
|
-
InitialBalance: sdk.NewInt(1000),
|
|
103
|
-
Balance: sdk.NewInt(500),
|
|
104
|
-
},
|
|
105
|
-
},
|
|
106
|
-
}
|
|
107
|
-
return &stakingtypes.QueryUnbondingDelegationResponse{Unbond: unbondingDelegation}, nil
|
|
108
|
-
}
|
|
109
|
-
|
|
110
80
|
// makeTestKit creates a minimal Keeper and Context for use in testing.
|
|
111
|
-
func makeTestKit(bank *mockBank, transfer *mockTransfer
|
|
81
|
+
func makeTestKit(bank *mockBank, transfer *mockTransfer) (vm.PortHandler, context.Context) {
|
|
112
82
|
encodingConfig := params.MakeEncodingConfig()
|
|
113
83
|
cdc := encodingConfig.Marshaler
|
|
114
84
|
|
|
@@ -123,10 +93,6 @@ func makeTestKit(bank *mockBank, transfer *mockTransfer, staking *mockStaking) (
|
|
|
123
93
|
transfertypes.RegisterInterfaces(encodingConfig.InterfaceRegistry)
|
|
124
94
|
transfertypes.RegisterMsgServer(txRouter, transfer)
|
|
125
95
|
transfertypes.RegisterQueryServer(queryRouter, transfer)
|
|
126
|
-
stakingtypes.RegisterInterfaces(encodingConfig.InterfaceRegistry)
|
|
127
|
-
stakingtypes.RegisterMsgServer(txRouter, staking)
|
|
128
|
-
stakingtypes.RegisterQueryServer(queryRouter, staking)
|
|
129
|
-
|
|
130
96
|
|
|
131
97
|
// create a new Keeper
|
|
132
98
|
keeper := vlocalchain.NewKeeper(cdc, vlocalchainStoreKey, txRouter, queryRouter)
|
|
@@ -152,8 +118,7 @@ func makeTestKit(bank *mockBank, transfer *mockTransfer, staking *mockStaking) (
|
|
|
152
118
|
func TestAllocateAddress(t *testing.T) {
|
|
153
119
|
bank := &mockBank{}
|
|
154
120
|
transfer := &mockTransfer{}
|
|
155
|
-
|
|
156
|
-
handler, cctx := makeTestKit(bank, transfer, staking)
|
|
121
|
+
handler, cctx := makeTestKit(bank, transfer)
|
|
157
122
|
|
|
158
123
|
addrs := map[string]bool{
|
|
159
124
|
firstAddr: false,
|
|
@@ -196,8 +161,7 @@ func TestQuery(t *testing.T) {
|
|
|
196
161
|
alreadyAddr: []sdk.Coin{sdk.NewCoin("stale", sdk.NewInt(321))},
|
|
197
162
|
}}
|
|
198
163
|
transfer := &mockTransfer{}
|
|
199
|
-
|
|
200
|
-
handler, cctx := makeTestKit(bank, transfer, staking)
|
|
164
|
+
handler, cctx := makeTestKit(bank, transfer)
|
|
201
165
|
|
|
202
166
|
// get balances
|
|
203
167
|
testCases := []struct {
|
|
@@ -266,68 +230,6 @@ func TestQuery(t *testing.T) {
|
|
|
266
230
|
}
|
|
267
231
|
})
|
|
268
232
|
}
|
|
269
|
-
|
|
270
|
-
t.Run("UnbondingDelegation", func(t *testing.T) {
|
|
271
|
-
// create a new message
|
|
272
|
-
msg := `{"type":"VLOCALCHAIN_QUERY_MANY","messages":[{"@type":"/cosmos.staking.v1beta1.QueryUnbondingDelegationRequest","delegator_addr":"` + firstAddr + `","validator_addr":"cosmosvaloper1gghjut3ccd8ay0zduzj64hwre2fxs9ldmqhffj"}]}`
|
|
273
|
-
t.Logf("query request: %v", msg)
|
|
274
|
-
ret, err := handler.Receive(cctx, msg)
|
|
275
|
-
t.Logf("query response: %v", ret)
|
|
276
|
-
if err != nil {
|
|
277
|
-
t.Fatalf("unexpected error: %v", err)
|
|
278
|
-
}
|
|
279
|
-
if ret == "" {
|
|
280
|
-
t.Fatalf("expected non-empty json")
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
// Unmarshal the JSON response
|
|
284
|
-
var respJSON []map[string]interface{}
|
|
285
|
-
if err := json.Unmarshal([]byte(ret), &respJSON); err != nil {
|
|
286
|
-
t.Fatalf("unexpected error unmarshalling JSON response: %v", err)
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
// Check the response fields
|
|
290
|
-
if len(respJSON) != 1 {
|
|
291
|
-
t.Fatalf("expected 1 response, got %d", len(respJSON))
|
|
292
|
-
}
|
|
293
|
-
resp := respJSON[0]
|
|
294
|
-
|
|
295
|
-
replyAny, ok := resp["reply"].(map[string]interface{})
|
|
296
|
-
if !ok {
|
|
297
|
-
t.Fatalf("expected reply field to be a map, got %v", resp["reply"])
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
unbond, ok := replyAny["unbond"].(map[string]interface{})
|
|
301
|
-
if !ok {
|
|
302
|
-
t.Fatalf("expected unbond field to be a map, got %v", replyAny["unbond"])
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
// Check the field names and values
|
|
306
|
-
if unbond["delegatorAddress"] != firstAddr {
|
|
307
|
-
t.Errorf("expected delegatorAddress %s, got %v", firstAddr, unbond["delegator_address"])
|
|
308
|
-
}
|
|
309
|
-
if unbond["validatorAddress"] != "cosmosvaloper1gghjut3ccd8ay0zduzj64hwre2fxs9ldmqhffj" {
|
|
310
|
-
t.Errorf("expected validatorAddress cosmosvaloper1gghjut3ccd8ay0zduzj64hwre2fxs9ldmqhffj, got %v", unbond["validator_address"])
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
entries, ok := unbond["entries"].([]interface{})
|
|
314
|
-
if !ok || len(entries) != 1 {
|
|
315
|
-
t.Fatalf("expected 1 unbonding delegation entry, got %v", entries)
|
|
316
|
-
}
|
|
317
|
-
entry, ok := entries[0].(map[string]interface{})
|
|
318
|
-
if !ok {
|
|
319
|
-
t.Fatalf("expected unbonding delegation entry to be a map, got %v", entries[0])
|
|
320
|
-
}
|
|
321
|
-
if entry["creationHeight"] != "100" {
|
|
322
|
-
t.Errorf("expected creationHeight \"100\", got %v", entry["creation_height"])
|
|
323
|
-
}
|
|
324
|
-
if entry["balance"] != "500" {
|
|
325
|
-
t.Errorf("expected balance \"500\", got %v", entry["balance"])
|
|
326
|
-
}
|
|
327
|
-
if _, ok := entry["completionTime"]; !ok {
|
|
328
|
-
t.Error("expected completionTime field in the response")
|
|
329
|
-
}
|
|
330
|
-
})
|
|
331
233
|
}
|
|
332
234
|
|
|
333
235
|
func TestExecuteTx(t *testing.T) {
|
|
@@ -337,8 +239,7 @@ func TestExecuteTx(t *testing.T) {
|
|
|
337
239
|
alreadyAddr: []sdk.Coin{sdk.NewCoin("stale", sdk.NewInt(321))},
|
|
338
240
|
}}
|
|
339
241
|
transfer := &mockTransfer{}
|
|
340
|
-
|
|
341
|
-
handler, cctx := makeTestKit(bank, transfer, staking)
|
|
242
|
+
handler, cctx := makeTestKit(bank, transfer)
|
|
342
243
|
|
|
343
244
|
// create a new message
|
|
344
245
|
msg := `{"type":"VLOCALCHAIN_ALLOCATE_ADDRESS"}`
|
|
@@ -401,34 +302,4 @@ func TestExecuteTx(t *testing.T) {
|
|
|
401
302
|
}
|
|
402
303
|
})
|
|
403
304
|
}
|
|
404
|
-
|
|
405
|
-
t.Run("MsgUndelegate", func(t *testing.T) {
|
|
406
|
-
// create a new message
|
|
407
|
-
msg := `{"type":"VLOCALCHAIN_EXECUTE_TX","address":"` + addr +
|
|
408
|
-
`","messages":[{"@type":"/cosmos.staking.v1beta1.MsgUndelegate","delegatorAddress":"` +
|
|
409
|
-
addr + `","validatorAddress":"cosmosvaloper1gghjut3ccd8ay0zduzj64hwre2fxs9ldmqhffj","amount":{"denom":"stake","amount":"100"}}]}`
|
|
410
|
-
|
|
411
|
-
ret, err := handler.Receive(cctx, msg)
|
|
412
|
-
if err != nil {
|
|
413
|
-
t.Fatalf("unexpected error: %s", err)
|
|
414
|
-
}
|
|
415
|
-
if ret == "" {
|
|
416
|
-
t.Fatalf("expected non-empty json")
|
|
417
|
-
}
|
|
418
|
-
|
|
419
|
-
// Unmarshal the response
|
|
420
|
-
var resp []map[string]interface{}
|
|
421
|
-
if err := json.Unmarshal([]byte(ret), &resp); err != nil {
|
|
422
|
-
t.Fatalf("unexpected error unmarshalling response: %v", err)
|
|
423
|
-
}
|
|
424
|
-
|
|
425
|
-
// Check the response fields
|
|
426
|
-
if len(resp) != 1 {
|
|
427
|
-
t.Fatalf("expected 1 response, got %d", len(resp))
|
|
428
|
-
}
|
|
429
|
-
|
|
430
|
-
if _, ok := resp[0]["completionTime"]; !ok {
|
|
431
|
-
t.Error("expected 'completionTime' field in response")
|
|
432
|
-
}
|
|
433
|
-
})
|
|
434
305
|
}
|
package/vm/proto_json.go
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
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
|
-
}
|
package/vm/proto_json_test.go
DELETED
|
@@ -1,103 +0,0 @@
|
|
|
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
|
-
}
|