@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.
Files changed (193) hide show
  1. package/MAINTAINERS.md +3 -0
  2. package/Makefile +67 -41
  3. package/agoric/config.go +35 -0
  4. package/ante/ante.go +65 -0
  5. package/ante/expected_keepers.go +26 -0
  6. package/ante/inbound.go +137 -0
  7. package/ante/inbound_test.go +228 -0
  8. package/ante/vm_admission.go +65 -0
  9. package/app/app.go +396 -211
  10. package/app/export.go +13 -9
  11. package/app/sim_test.go +4 -4
  12. package/app/upgrade.go +215 -0
  13. package/binding.gyp.in +29 -4
  14. package/buf.work.yaml +4 -0
  15. package/cmd/agd/agvm.go +42 -0
  16. package/cmd/agd/find_binary.go +22 -7
  17. package/cmd/agd/main.go +133 -10
  18. package/cmd/libdaemon/main.go +67 -53
  19. package/cmd/libdaemon/main_test.go +37 -0
  20. package/daemon/cmd/genaccounts.go +22 -18
  21. package/daemon/cmd/root.go +253 -52
  22. package/daemon/cmd/root_test.go +190 -2
  23. package/daemon/cmd/testnet.go +17 -6
  24. package/daemon/main.go +6 -3
  25. package/e2e_test/Makefile +29 -0
  26. package/e2e_test/README.md +100 -0
  27. package/e2e_test/go.mod +217 -0
  28. package/e2e_test/go.sum +1323 -0
  29. package/e2e_test/ibc_conformance_test.go +56 -0
  30. package/e2e_test/pfm_test.go +613 -0
  31. package/e2e_test/util.go +271 -0
  32. package/git-revision.txt +1 -0
  33. package/go.mod +207 -0
  34. package/go.sum +1877 -0
  35. package/package.json +13 -21
  36. package/proto/agoric/swingset/genesis.proto +14 -0
  37. package/proto/agoric/swingset/swingset.proto +6 -3
  38. package/proto/agoric/vlocalchain/.clang-format +7 -0
  39. package/proto/agoric/vlocalchain/vlocalchain.proto +31 -0
  40. package/proto/agoric/vstorage/query.proto +53 -1
  41. package/proto/agoric/vtransfer/genesis.proto +18 -0
  42. package/scripts/protocgen.sh +16 -6
  43. package/third_party/proto/buf.yaml +1 -0
  44. package/third_party/proto/cosmos/base/query/v1beta1/pagination.proto +4 -1
  45. package/third_party/proto/cosmos/base/v1beta1/coin.proto +7 -4
  46. package/third_party/proto/cosmos/upgrade/v1beta1/upgrade.proto +16 -6
  47. package/third_party/proto/cosmos_proto/cosmos.proto +97 -0
  48. package/third_party/proto/google/api/annotations.proto +1 -1
  49. package/third_party/proto/google/api/http.proto +181 -120
  50. package/third_party/proto/google/api/httpbody.proto +9 -6
  51. package/third_party/proto/google/protobuf/any.proto +1 -7
  52. package/third_party/proto/ibc/core/channel/v1/channel.proto +15 -1
  53. package/third_party/proto/ibc/core/client/v1/client.proto +9 -6
  54. package/tools/tools.go +11 -0
  55. package/types/events.go +41 -0
  56. package/types/kv_entry.go +114 -0
  57. package/types/kv_entry_helpers.go +276 -0
  58. package/types/kv_entry_helpers_test.go +237 -0
  59. package/types/kv_entry_test.go +143 -0
  60. package/upgradegaia.sh +75 -0
  61. package/vm/action.go +134 -0
  62. package/vm/action_test.go +149 -0
  63. package/vm/client.go +113 -0
  64. package/vm/client_test.go +182 -0
  65. package/vm/controller.go +49 -0
  66. package/vm/core_proposals.go +51 -0
  67. package/vm/jsonrpcconn/jsonrpcconn.go +160 -0
  68. package/vm/jsonrpcconn/jsonrpcconn_test.go +126 -0
  69. package/vm/proto_json.go +38 -0
  70. package/vm/proto_json_test.go +103 -0
  71. package/vm/server.go +124 -0
  72. package/x/swingset/abci.go +26 -37
  73. package/x/swingset/alias.go +9 -6
  74. package/x/swingset/client/cli/query.go +2 -2
  75. package/x/swingset/client/cli/tx.go +52 -33
  76. package/x/swingset/client/proposal_handler.go +2 -17
  77. package/x/swingset/genesis.go +163 -37
  78. package/x/swingset/handler.go +2 -1
  79. package/x/swingset/keeper/extension_snapshotter.go +309 -0
  80. package/x/swingset/keeper/extension_snapshotter_test.go +104 -0
  81. package/x/swingset/keeper/keeper.go +95 -48
  82. package/x/swingset/keeper/keeper_test.go +77 -0
  83. package/x/swingset/keeper/migrations.go +7 -2
  84. package/x/swingset/keeper/msg_server.go +71 -56
  85. package/x/swingset/keeper/proposal.go +15 -9
  86. package/x/swingset/keeper/querier.go +24 -15
  87. package/x/swingset/keeper/swing_store_exports_handler.go +872 -0
  88. package/x/swingset/keeper/swing_store_exports_handler_test.go +247 -0
  89. package/x/swingset/keeper/test_utils.go +16 -0
  90. package/x/swingset/module.go +33 -11
  91. package/x/swingset/proposal_handler.go +5 -4
  92. package/x/swingset/swingset.go +73 -0
  93. package/x/swingset/testing/queue.go +17 -0
  94. package/x/swingset/types/codec.go +2 -2
  95. package/x/swingset/types/default-params.go +23 -17
  96. package/x/swingset/types/expected_keepers.go +14 -2
  97. package/x/swingset/types/genesis.pb.go +361 -18
  98. package/x/swingset/types/msgs.go +87 -26
  99. package/x/swingset/types/msgs.pb.go +16 -16
  100. package/x/swingset/types/params.go +76 -1
  101. package/x/swingset/types/params_test.go +116 -0
  102. package/x/swingset/types/proposal.go +10 -9
  103. package/x/swingset/types/swingset.pb.go +83 -81
  104. package/x/swingset/types/types.go +30 -28
  105. package/x/vbank/genesis.go +0 -2
  106. package/x/vbank/handler.go +2 -1
  107. package/x/vbank/keeper/keeper.go +3 -2
  108. package/x/vbank/keeper/querier.go +9 -4
  109. package/x/vbank/keeper/rewards.go +1 -1
  110. package/x/vbank/module.go +0 -5
  111. package/x/vbank/types/msgs.go +0 -12
  112. package/x/vbank/vbank.go +34 -32
  113. package/x/vbank/vbank_test.go +20 -20
  114. package/x/vibc/alias.go +3 -0
  115. package/x/vibc/handler.go +17 -15
  116. package/x/vibc/keeper/keeper.go +113 -89
  117. package/x/vibc/keeper/triggers.go +101 -0
  118. package/x/vibc/module.go +5 -8
  119. package/x/vibc/types/expected_keepers.go +28 -6
  120. package/x/vibc/types/ibc_module.go +336 -0
  121. package/x/vibc/types/msgs.go +1 -1
  122. package/x/vibc/types/msgs.pb.go +1 -1
  123. package/x/vibc/types/receiver.go +170 -0
  124. package/x/vlocalchain/alias.go +19 -0
  125. package/x/vlocalchain/handler.go +21 -0
  126. package/x/vlocalchain/keeper/keeper.go +279 -0
  127. package/x/vlocalchain/keeper/keeper_test.go +97 -0
  128. package/x/vlocalchain/types/codec.go +34 -0
  129. package/x/vlocalchain/types/key.go +27 -0
  130. package/x/vlocalchain/types/msgs.go +16 -0
  131. package/x/vlocalchain/types/vlocalchain.pb.go +1072 -0
  132. package/x/vlocalchain/vlocalchain.go +114 -0
  133. package/x/vlocalchain/vlocalchain_test.go +434 -0
  134. package/x/vstorage/README.md +138 -0
  135. package/x/vstorage/capdata/capdata.go +298 -0
  136. package/x/vstorage/capdata/capdata_test.go +352 -0
  137. package/x/vstorage/client/cli/query.go +51 -4
  138. package/x/vstorage/genesis.go +0 -3
  139. package/x/vstorage/handler.go +2 -1
  140. package/x/vstorage/keeper/grpc_query.go +220 -0
  141. package/x/vstorage/keeper/keeper.go +75 -57
  142. package/x/vstorage/keeper/keeper_grpc_test.go +300 -0
  143. package/x/vstorage/keeper/keeper_test.go +40 -62
  144. package/x/vstorage/keeper/querier.go +39 -14
  145. package/x/vstorage/keeper/querier_test.go +113 -0
  146. package/x/vstorage/module.go +0 -5
  147. package/x/vstorage/testing/queue.go +27 -0
  148. package/x/vstorage/types/path_keys.go +22 -10
  149. package/x/vstorage/types/path_keys_test.go +84 -18
  150. package/x/vstorage/types/query.pb.go +646 -36
  151. package/x/vstorage/types/query.pb.gw.go +119 -0
  152. package/x/vstorage/types/types.go +0 -66
  153. package/x/vstorage/vstorage.go +33 -35
  154. package/x/vstorage/vstorage_test.go +19 -19
  155. package/x/vtransfer/alias.go +13 -0
  156. package/x/vtransfer/genesis.go +39 -0
  157. package/x/vtransfer/genesis_test.go +12 -0
  158. package/x/vtransfer/handler.go +20 -0
  159. package/x/vtransfer/ibc_middleware.go +186 -0
  160. package/x/vtransfer/ibc_middleware_test.go +448 -0
  161. package/x/vtransfer/keeper/keeper.go +281 -0
  162. package/x/vtransfer/module.go +124 -0
  163. package/x/vtransfer/types/expected_keepers.go +38 -0
  164. package/x/vtransfer/types/genesis.pb.go +327 -0
  165. package/x/vtransfer/types/key.go +9 -0
  166. package/x/vtransfer/types/msgs.go +9 -0
  167. package/app/const.go +0 -6
  168. package/cmd/helper/main.go +0 -21
  169. package/proto/agoric/lien/genesis.proto +0 -25
  170. package/proto/agoric/lien/lien.proto +0 -25
  171. package/x/lien/alias.go +0 -17
  172. package/x/lien/genesis.go +0 -58
  173. package/x/lien/genesis_test.go +0 -101
  174. package/x/lien/keeper/account.go +0 -285
  175. package/x/lien/keeper/keeper.go +0 -254
  176. package/x/lien/keeper/keeper_test.go +0 -623
  177. package/x/lien/lien.go +0 -203
  178. package/x/lien/lien_test.go +0 -529
  179. package/x/lien/module.go +0 -115
  180. package/x/lien/spec/01_concepts.md +0 -146
  181. package/x/lien/spec/02_messages.md +0 -96
  182. package/x/lien/types/accountkeeper.go +0 -81
  183. package/x/lien/types/accountstate.go +0 -27
  184. package/x/lien/types/expected_keepers.go +0 -18
  185. package/x/lien/types/genesis.pb.go +0 -567
  186. package/x/lien/types/key.go +0 -25
  187. package/x/lien/types/lien.pb.go +0 -403
  188. package/x/swingset/keeper/snapshotter.go +0 -491
  189. package/x/swingset/keeper/snapshotter_test.go +0 -185
  190. package/x/swingset/legacy/v32/params.go +0 -37
  191. package/x/swingset/legacy/v32/params_test.go +0 -133
  192. package/x/vibc/ibc.go +0 -442
  193. /package/{src/index.cjs → index.cjs} +0 -0
@@ -0,0 +1,143 @@
1
+ package types
2
+
3
+ import (
4
+ "encoding/json"
5
+ "errors"
6
+ "strings"
7
+ "testing"
8
+ )
9
+
10
+ func checkEntry(t *testing.T, label string, entry KVEntry, isValidKey bool, expectedKey string, hasValue bool, expectedValue string) {
11
+ gotValidKey := entry.IsValidKey()
12
+ if gotValidKey != isValidKey {
13
+ t.Errorf("%s: valid key is %v, expected %v", label, gotValidKey, isValidKey)
14
+ }
15
+
16
+ gotKey := entry.Key()
17
+ if gotKey != expectedKey {
18
+ t.Errorf("%s: got %q, want %q", label, gotKey, expectedKey)
19
+ }
20
+
21
+ if entry.HasValue() {
22
+ if !hasValue {
23
+ t.Errorf("%s: expected has no value", label)
24
+ }
25
+
26
+ gotValue := *entry.Value()
27
+ if gotValue != expectedValue {
28
+ t.Errorf("%s: got %q, want %q", label, gotValue, expectedValue)
29
+ }
30
+ } else {
31
+ if hasValue {
32
+ t.Errorf("%s: expected has value", label)
33
+ }
34
+
35
+ gotValuePointer := entry.Value()
36
+ if gotValuePointer != nil {
37
+ t.Errorf("%s: got %#v, want nil", label, gotValuePointer)
38
+ }
39
+ }
40
+
41
+ gotValue := entry.StringValue()
42
+ if gotValue != expectedValue {
43
+ t.Errorf("%s: got %q, want %q", label, gotValue, expectedValue)
44
+ }
45
+ }
46
+
47
+ func TestKVEntry(t *testing.T) {
48
+ type testCase struct {
49
+ label string
50
+ entry KVEntry
51
+ isValidKey bool
52
+ expectedKey string
53
+ hasValue bool
54
+ expectedValue string
55
+ }
56
+ cases := []testCase{
57
+ {label: "normal", entry: NewKVEntry("foo", "bar"), isValidKey: true, expectedKey: "foo", hasValue: true, expectedValue: "bar"},
58
+ {label: "empty string value", entry: NewKVEntry("foo", ""), isValidKey: true, expectedKey: "foo", hasValue: true, expectedValue: ""},
59
+ {label: "no value", entry: NewKVEntryWithNoValue("foo"), isValidKey: true, expectedKey: "foo", hasValue: false, expectedValue: ""},
60
+ {label: "empty key", entry: NewKVEntryWithNoValue(""), isValidKey: false, expectedKey: "", hasValue: false, expectedValue: ""},
61
+ }
62
+ for _, desc := range cases {
63
+ checkEntry(t, desc.label, desc.entry, desc.isValidKey, desc.expectedKey, desc.hasValue, desc.expectedValue)
64
+ }
65
+ }
66
+
67
+ func TestKVEntryMarshall(t *testing.T) {
68
+ type testCase struct {
69
+ label string
70
+ entry KVEntry
71
+ expectedError error
72
+ expectedEncoding string
73
+ }
74
+ cases := []testCase{
75
+ {label: "normal", entry: NewKVEntry("foo", "bar"), expectedEncoding: `["foo","bar"]`},
76
+ {label: "empty string value", entry: NewKVEntry("foo", ""), expectedEncoding: `["foo",""]`},
77
+ {label: "no value", entry: NewKVEntryWithNoValue("foo"), expectedEncoding: `["foo"]`},
78
+ {label: "empty key", entry: NewKVEntryWithNoValue(""), expectedError: errors.New("cannot marshal invalid KVEntry")},
79
+ }
80
+ for _, desc := range cases {
81
+ marshalled, err := json.Marshal(desc.entry)
82
+ if desc.expectedError != nil && err == nil {
83
+ t.Errorf("%s: got nil error, expected marshal error: %q", desc.label, desc.expectedError.Error())
84
+ } else if err != nil {
85
+ if desc.expectedError == nil {
86
+ t.Errorf("%s: got error %v, expected no error", desc.label, err)
87
+ } else if !strings.Contains(err.Error(), desc.expectedError.Error()) {
88
+ t.Errorf("%s: got error %q, expected error %q", desc.label, err.Error(), desc.expectedError.Error())
89
+ }
90
+ continue
91
+ }
92
+ if string(marshalled) != desc.expectedEncoding {
93
+ t.Errorf("%s: got %q, want %q", desc.label, string(marshalled), desc.expectedEncoding)
94
+ }
95
+ }
96
+ }
97
+
98
+ func TestKVEntryUnmarshall(t *testing.T) {
99
+ type testCase struct {
100
+ label string
101
+ encoded string
102
+ expectedError error
103
+ expectedKey string
104
+ hasValue bool
105
+ expectedValue string
106
+ }
107
+ cases := []testCase{
108
+ {label: "normal", encoded: `["foo","bar"]`, expectedKey: "foo", hasValue: true, expectedValue: "bar"},
109
+ {label: "empty string value", encoded: `["foo",""]`, expectedKey: "foo", hasValue: true, expectedValue: ""},
110
+ {label: "no value", encoded: `["foo"]`, expectedKey: "foo", hasValue: false, expectedValue: ""},
111
+ {label: "null value", encoded: `["foo",null]`, expectedKey: "foo", hasValue: false, expectedValue: ""},
112
+ {label: "null", encoded: `null`, expectedError: errors.New("KVEntry cannot be null")},
113
+ {label: "string", encoded: `"foo"`, expectedError: errors.New("json")},
114
+ {label: "empty array", encoded: `[]`, expectedError: errors.New("KVEntry must be an array of length 1 or 2 (not 0)")},
115
+ {label: "[null, null] array", encoded: `[null,null]`, expectedError: errors.New("KVEntry key must be a non-empty string")},
116
+ {label: "invalid key array", encoded: `[42]`, expectedError: errors.New("json")},
117
+ {label: "empty key", encoded: `["",null]`, expectedError: errors.New("KVEntry key must be a non-empty string")},
118
+ {label: "too many entries array", encoded: `["foo","bar",null]`, expectedError: errors.New("KVEntry must be an array of length 1 or 2 (not 3)")},
119
+ {label: "invalid value array", encoded: `["foo",42]`, expectedError: errors.New("json")},
120
+ }
121
+ for _, desc := range cases {
122
+ unmarshalled := NewKVEntry("untouched", "untouched")
123
+ err := json.Unmarshal([]byte(desc.encoded), &unmarshalled)
124
+ if desc.expectedError != nil && err == nil {
125
+ t.Errorf("%s: got nil error, expected unmarshal error: %q", desc.label, desc.expectedError.Error())
126
+ } else if err != nil {
127
+ if unmarshalled.Key() != "untouched" {
128
+ t.Errorf("%s: expected error to not modify target key, got %s", desc.label, unmarshalled.Key())
129
+ }
130
+ if unmarshalled.StringValue() != "untouched" {
131
+ t.Errorf("%s: expected error to not modify target value, got %v", desc.label, unmarshalled.Value())
132
+ }
133
+ if desc.expectedError == nil {
134
+ t.Errorf("%s: got error %v, expected no error", desc.label, err)
135
+ } else if !strings.Contains(err.Error(), desc.expectedError.Error()) {
136
+ t.Errorf("%s: got error %q, expected error %q", desc.label, err.Error(), desc.expectedError.Error())
137
+ }
138
+ continue
139
+ }
140
+
141
+ checkEntry(t, desc.label, unmarshalled, true, desc.expectedKey, desc.hasValue, desc.expectedValue)
142
+ }
143
+ }
package/upgradegaia.sh ADDED
@@ -0,0 +1,75 @@
1
+ #! /bin/bash
2
+ # 3-way-merge changes from current repo to agoric-sdk/golang/cosmos
3
+ #
4
+ # Usage: ./upgradegaia.sh <FROM_BRANCH> <TO_BRANCH>
5
+ # Such as:
6
+ # cd ~/src/gaia
7
+ # /path/to/agoric-sdk/golang/cosmos/upgradegaia.sh release/v6.0.0-rc3 release/v6.0.0
8
+ set -ueo pipefail
9
+
10
+ test $# -eq 2 || {
11
+ echo "Usage: $0 <FROM_BRANCH> <TO_BRANCH>" 1>&2
12
+ exit 1
13
+ }
14
+
15
+ FROM_BRANCH="$1"
16
+ TO_BRANCH="$2"
17
+
18
+ thisdir="$(cd "$(dirname "${BASH_SOURCE[0]}")" > /dev/null && pwd)"
19
+ tmp="$(mktemp -dt upgradegaia.XXXXXX)"
20
+
21
+ echo "Get the Gaia files from $FROM_BRANCH and $TO_BRANCH"
22
+ for tag in "$FROM_BRANCH" "$TO_BRANCH"; do
23
+ qtag=${tag//\//_}
24
+ for root in Makefile app cmd/gaiad; do
25
+ case "$root" in
26
+ Makefile) echo "$root" ;;
27
+ *) git ls-tree --name-only --full-tree -r "$tag:$root" | sed -e "s!^!$root/!" ;;
28
+ esac
29
+ done | while read -r src; do
30
+ # echo "$src"
31
+ dst="$tmp/$qtag/$src"
32
+ echo "extracting $dst"
33
+ mkdir -p "$(dirname "$dst")"
34
+ git cat-file blob "$tag:$src" > "$dst"
35
+ done
36
+ done
37
+
38
+ echo "Compute 3-way diffs between Gaia and us"
39
+ (cd "$tmp/${FROM_BRANCH//\//_}" && find . -type f -print) \
40
+ | while read -r src; do
41
+ # echo "$src"
42
+ case "$src" in
43
+ ./cmd/gaiad/*) our=${src//cmd\/gaiad/daemon} ;;
44
+ *) our=$src ;;
45
+ esac
46
+
47
+ new="$tmp/diff3/$our"
48
+ echo "creating $new"
49
+ mkdir -p "$(dirname "$new")"
50
+ status=0
51
+ ourfile="$thisdir/$our"
52
+ if [ ! -f $ourfile ]; then
53
+ ourfile=/dev/null
54
+ fi
55
+ fromfile="$tmp/${FROM_BRANCH//\//_}/$src"
56
+ if [ ! -f $fromfile ]; then
57
+ fromfile=/dev/null
58
+ fi
59
+ tofile="$tmp/${TO_BRANCH//\//_}/$src"
60
+ if [ ! -f $tofile ]; then
61
+ tofile=/dev/null
62
+ fi
63
+ diff3 -mE "$ourfile" "$fromfile" "$tofile" \
64
+ > "$new" || status=$?
65
+ if [ $status -ge 2 ]; then
66
+ exit "$status"
67
+ fi
68
+ done
69
+
70
+ # Display some hints.
71
+ echo "=== Now you can run:"
72
+ set -x
73
+ : cp -a "$tmp/diff3/." "$thisdir/"
74
+ : git diff --stat
75
+ : git commit -am "Upgrade to \`$TO_BRANCH\`"
package/vm/action.go ADDED
@@ -0,0 +1,134 @@
1
+ package vm
2
+
3
+ import (
4
+ "reflect"
5
+ "strconv"
6
+ "time"
7
+
8
+ sdk "github.com/cosmos/cosmos-sdk/types"
9
+ )
10
+
11
+ var (
12
+ zeroTime = time.Time{}
13
+ actionHeaderType = reflect.TypeOf(ActionHeader{})
14
+ _ Action = &ActionHeader{}
15
+ )
16
+
17
+ // Jsonable is a value, j, that can be passed through json.Marshal(j).
18
+ type Jsonable interface{}
19
+
20
+ type Action interface {
21
+ GetActionHeader() *ActionHeader
22
+ }
23
+
24
+ // ActionPusher enqueues data for later consumption by the controller.
25
+ type ActionPusher func(ctx sdk.Context, action Action) error
26
+
27
+ // ActionHeader should be embedded in all actions. It is populated by PopulateAction.
28
+ type ActionHeader struct {
29
+ // Type defaults to the `actionType:"..."` tag of the embedder's ActionHeader field.
30
+ Type string `json:"type,omitempty"`
31
+ // BlockHeight defaults to sdk.Context.BlockHeight().
32
+ BlockHeight int64 `json:"blockHeight,omitempty"`
33
+ // BlockTime defaults to sdk.Context.BlockTime().Unix().
34
+ BlockTime int64 `json:"blockTime,omitempty"`
35
+ }
36
+
37
+ func (ah *ActionHeader) GetActionHeader() *ActionHeader {
38
+ return ah
39
+ }
40
+
41
+ // SetActionHeaderFromContext provides defaults to an ActionHeader.
42
+ func SetActionHeaderFromContext(ctx sdk.Context, actionType string, ah *ActionHeader) {
43
+ // Default the action type.
44
+ if len(ah.Type) == 0 {
45
+ ah.Type = actionType
46
+ }
47
+
48
+ // Default the block height.
49
+ if ah.BlockHeight == 0 {
50
+ ah.BlockHeight = ctx.BlockHeight()
51
+ }
52
+
53
+ if ah.BlockTime == 0 {
54
+ // Only default to a non-zero block time.
55
+ if blockTime := ctx.BlockTime(); blockTime != zeroTime {
56
+ ah.BlockTime = blockTime.Unix()
57
+ }
58
+ }
59
+ }
60
+
61
+ // PopulateAction returns a clone of action in which empty/zero-valued fields
62
+ // in its embedded ActionHeader have been populated using the corresponding
63
+ // `actionType:"..."` tag and the provided ctx, and its own empty/zero-valued
64
+ // fields have been populated as specified by their `default:"..."` tags.
65
+ func PopulateAction(ctx sdk.Context, action Action) Action {
66
+ oldActionDesc := reflect.Indirect(reflect.ValueOf(action))
67
+ if oldActionDesc.Kind() != reflect.Struct {
68
+ return action
69
+ }
70
+
71
+ // Shallow copy to a new value.
72
+ newActionDescPtr := reflect.New(oldActionDesc.Type())
73
+ newActionDesc := reflect.Indirect(newActionDescPtr)
74
+ for i := 0; i < newActionDesc.NumField(); i++ {
75
+ oldField := oldActionDesc.Field(i)
76
+ field := newActionDesc.Field(i)
77
+ fieldType := newActionDesc.Type().Field(i)
78
+ if !field.CanSet() {
79
+ continue
80
+ }
81
+
82
+ // Copy from original.
83
+ field.Set(oldField)
84
+
85
+ // Populate any ActionHeader struct.
86
+ var headerPtr *ActionHeader
87
+ if fieldType.Type == actionHeaderType {
88
+ headerPtr = field.Addr().Interface().(*ActionHeader)
89
+ } else if fieldType.Type == reflect.PtrTo(actionHeaderType) {
90
+ if field.IsNil() {
91
+ headerPtr = &ActionHeader{}
92
+ } else {
93
+ headerPtr = field.Interface().(*ActionHeader)
94
+ }
95
+ }
96
+ if headerPtr != nil {
97
+ actionTypeTag, _ := fieldType.Tag.Lookup("actionType")
98
+ newHeader := *headerPtr
99
+ SetActionHeaderFromContext(ctx, actionTypeTag, &newHeader)
100
+ if field.Kind() == reflect.Ptr {
101
+ field.Set(reflect.ValueOf(&newHeader))
102
+ } else {
103
+ field.Set(reflect.ValueOf(newHeader))
104
+ }
105
+ continue
106
+ }
107
+
108
+ // Skip any field that is already populated or lacks a "default" tag.
109
+ defaultTag, _ := fieldType.Tag.Lookup("default")
110
+ if !field.IsZero() || len(defaultTag) == 0 {
111
+ continue
112
+ }
113
+
114
+ // Populate the field from its "default" tag.
115
+ switch field.Kind() {
116
+ case reflect.String:
117
+ field.SetString(defaultTag)
118
+ case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
119
+ if val, err := strconv.ParseInt(defaultTag, 0, 64); err == nil {
120
+ field.SetInt(val)
121
+ }
122
+ case reflect.Bool:
123
+ if val, err := strconv.ParseBool(defaultTag); err == nil {
124
+ field.SetBool(val)
125
+ }
126
+ case reflect.Float32, reflect.Float64:
127
+ if val, err := strconv.ParseFloat(defaultTag, 64); err == nil {
128
+ field.SetFloat(val)
129
+ }
130
+ }
131
+ }
132
+
133
+ return newActionDescPtr.Interface().(Action)
134
+ }
@@ -0,0 +1,149 @@
1
+ package vm_test
2
+
3
+ import (
4
+ "encoding/json"
5
+ "testing"
6
+ "time"
7
+
8
+ "github.com/Agoric/agoric-sdk/golang/cosmos/vm"
9
+ sdk "github.com/cosmos/cosmos-sdk/types"
10
+ )
11
+
12
+ var (
13
+ _ vm.Action = Defaults{}
14
+ _ vm.Action = &Defaults{}
15
+ _ vm.Action = dataAction{}
16
+ _ vm.Action = &dataAction{}
17
+ _ vm.Action = &Trivial{}
18
+ )
19
+
20
+ type Trivial struct {
21
+ vm.ActionHeader
22
+ Abc int
23
+ def string
24
+ }
25
+
26
+ type Defaults struct {
27
+ String string `default:"abc"`
28
+ Int int `default:"123"`
29
+ Float float64 `default:"4.56"`
30
+ Bool bool `default:"true"`
31
+ Any interface{}
32
+ }
33
+
34
+ func (d Defaults) GetActionHeader() *vm.ActionHeader {
35
+ return &vm.ActionHeader{}
36
+ }
37
+
38
+ type dataAction struct {
39
+ *vm.ActionHeader `actionType:"DATA_ACTION"`
40
+ Data []byte
41
+ }
42
+
43
+ func TestActionContext(t *testing.T) {
44
+ emptyCtx := sdk.Context{}
45
+
46
+ testCases := []struct {
47
+ name string
48
+ ctx sdk.Context
49
+ in vm.Action
50
+ expectedOut interface{}
51
+ }{
52
+ {"nil", emptyCtx, nil, nil},
53
+ {"no context", emptyCtx,
54
+ &Trivial{Abc: 123, def: "zot"},
55
+ &Trivial{Abc: 123, def: "zot"},
56
+ },
57
+ {"not a pointer", emptyCtx,
58
+ &Trivial{Abc: 123, def: "zot"},
59
+ Trivial{Abc: 123, def: "zot"},
60
+ },
61
+ {"block height",
62
+ emptyCtx.WithBlockHeight(998),
63
+ &Trivial{Abc: 123, def: "zot"},
64
+ &Trivial{ActionHeader: vm.ActionHeader{BlockHeight: 998}, Abc: 123, def: "zot"},
65
+ },
66
+ {"block time",
67
+ emptyCtx.WithBlockTime(time.UnixMicro(1_000_000)),
68
+ &Trivial{Abc: 123, def: "zot"},
69
+ &Trivial{Abc: 123, def: "zot", ActionHeader: vm.ActionHeader{BlockTime: 1}},
70
+ },
71
+ {"default tags",
72
+ emptyCtx,
73
+ &Defaults{},
74
+ &Defaults{"abc", 123, 4.56, true, nil},
75
+ },
76
+ {"data action no pointer",
77
+ emptyCtx,
78
+ dataAction{},
79
+ dataAction{
80
+ ActionHeader: &vm.ActionHeader{Type: "DATA_ACTION"},
81
+ },
82
+ },
83
+ {"data action pointer",
84
+ emptyCtx,
85
+ &dataAction{},
86
+ &dataAction{
87
+ ActionHeader: &vm.ActionHeader{Type: "DATA_ACTION"},
88
+ },
89
+ },
90
+ {"data action defaults",
91
+ emptyCtx.WithBlockHeight(998).WithBlockTime(time.UnixMicro(1_000_000)),
92
+ &dataAction{Data: []byte("hello")},
93
+ &dataAction{Data: []byte("hello"),
94
+ ActionHeader: &vm.ActionHeader{Type: "DATA_ACTION", BlockHeight: 998, BlockTime: 1}},
95
+ },
96
+ {"data action override Type",
97
+ emptyCtx.WithBlockHeight(998).WithBlockTime(time.UnixMicro(1_000_000)),
98
+ dataAction{Data: []byte("hello2"),
99
+ ActionHeader: &vm.ActionHeader{Type: "DATA_ACTION2"}},
100
+ dataAction{Data: []byte("hello2"),
101
+ ActionHeader: &vm.ActionHeader{Type: "DATA_ACTION2", BlockHeight: 998, BlockTime: 1}},
102
+ },
103
+ {"data action override BlockHeight",
104
+ emptyCtx.WithBlockHeight(998).WithBlockTime(time.UnixMicro(1_000_000)),
105
+ dataAction{Data: []byte("hello2"),
106
+ ActionHeader: &vm.ActionHeader{BlockHeight: 999}},
107
+ dataAction{Data: []byte("hello2"),
108
+ ActionHeader: &vm.ActionHeader{Type: "DATA_ACTION", BlockHeight: 999, BlockTime: 1}},
109
+ },
110
+ {"data action override BlockTime",
111
+ emptyCtx.WithBlockHeight(998).WithBlockTime(time.UnixMicro(1_000_000)),
112
+ dataAction{Data: []byte("hello2"),
113
+ ActionHeader: &vm.ActionHeader{BlockTime: 2}},
114
+ dataAction{Data: []byte("hello2"),
115
+ ActionHeader: &vm.ActionHeader{Type: "DATA_ACTION", BlockHeight: 998, BlockTime: 2}},
116
+ },
117
+ {"data action override all defaults",
118
+ emptyCtx.WithBlockHeight(998).WithBlockTime(time.UnixMicro(1_000_000)),
119
+ dataAction{Data: []byte("hello2"),
120
+ ActionHeader: &vm.ActionHeader{Type: "DATA_ACTION2", BlockHeight: 999, BlockTime: 2}},
121
+ dataAction{Data: []byte("hello2"),
122
+ ActionHeader: &vm.ActionHeader{Type: "DATA_ACTION2", BlockHeight: 999, BlockTime: 2}},
123
+ },
124
+ }
125
+
126
+ for _, tc := range testCases {
127
+ tc := tc
128
+ t.Run(tc.name, func(t *testing.T) {
129
+ toJson := func(in interface{}) string {
130
+ bz, err := json.Marshal(in)
131
+ if err != nil {
132
+ t.Fatal(err)
133
+ }
134
+ return string(bz)
135
+ }
136
+ jsonIn := toJson(tc.in)
137
+ out := vm.PopulateAction(tc.ctx, tc.in)
138
+ jsonIn2 := toJson(tc.in)
139
+ if jsonIn != jsonIn2 {
140
+ t.Errorf("unexpected mutated input: %s to %s", jsonIn, jsonIn2)
141
+ }
142
+ jsonOut := toJson(out)
143
+ jsonExpectedOut := toJson(tc.expectedOut)
144
+ if jsonOut != jsonExpectedOut {
145
+ t.Errorf("expected %s, got %s", jsonExpectedOut, jsonOut)
146
+ }
147
+ })
148
+ }
149
+ }
package/vm/client.go ADDED
@@ -0,0 +1,113 @@
1
+ package vm
2
+
3
+ import (
4
+ "context"
5
+ "fmt"
6
+ "net/rpc"
7
+ )
8
+
9
+ // ReceiveMessageMethod is the name of the method we call in order to have the
10
+ // VM receive a Message.
11
+ const ReceiveMessageMethod = "agvm.ReceiveMessage"
12
+
13
+ // Message is what we send to the VM.
14
+ type Message struct {
15
+ Port int
16
+ Data string
17
+ NeedsReply bool
18
+ }
19
+
20
+ // ClientCodec implements rpc.ClientCodec.
21
+ var _ rpc.ClientCodec = (*ClientCodec)(nil)
22
+
23
+ // ClientCodec implements a net/rpc ClientCodec for the "bridge" between the Go
24
+ // runtime and the VM in the single-process dual-runtime configuration.
25
+ //
26
+ // We expect to call it via the legacy API with signature:
27
+ // sendToController func(needsReply bool, msg string) (string, error)
28
+ // where msg and the returned string are JSON-encoded values.
29
+ //
30
+ // Note that the net/rpc framework cannot express a call that does not expect a
31
+ // response, so we'll note such calls by sending with a reply port of 0 and
32
+ // having the WriteRequest() method fabricate a Receive() call to clear the rpc
33
+ // state.
34
+ type ClientCodec struct {
35
+ ctx context.Context
36
+ send func(port, rPort int, msg string)
37
+ outbound map[int]rpc.Request
38
+ inbound chan *rpc.Response
39
+ replies map[uint64]string
40
+ replyToRead uint64
41
+ }
42
+
43
+ // NewClientCodec creates a new ClientCodec.
44
+ func NewClientCodec(ctx context.Context, send func(int, int, string)) *ClientCodec {
45
+ return &ClientCodec{
46
+ ctx: ctx,
47
+ send: send,
48
+ outbound: make(map[int]rpc.Request),
49
+ inbound: make(chan *rpc.Response),
50
+ replies: make(map[uint64]string),
51
+ }
52
+ }
53
+
54
+ // WriteRequest sends a request to the VM.
55
+ func (cc *ClientCodec) WriteRequest(r *rpc.Request, body interface{}) error {
56
+ if r.ServiceMethod != ReceiveMessageMethod {
57
+ return fmt.Errorf("unknown method %s", r.ServiceMethod)
58
+ }
59
+
60
+ msg, ok := body.(Message)
61
+ if !ok {
62
+ return fmt.Errorf("body %T is not a Message", body)
63
+ }
64
+ rPort := int(r.Seq + 1) // rPort is 1-indexed to indicate it's required
65
+ cc.outbound[rPort] = *r
66
+ var senderReplyPort int
67
+ if msg.NeedsReply {
68
+ senderReplyPort = rPort
69
+ }
70
+ cc.send(msg.Port, senderReplyPort, msg.Data)
71
+ if !msg.NeedsReply {
72
+ return cc.Receive(rPort, false, "<no-reply-requested>")
73
+ }
74
+ return nil
75
+ }
76
+
77
+ // ReadResponseHeader decodes a response header from the VM.
78
+ func (cc *ClientCodec) ReadResponseHeader(r *rpc.Response) error {
79
+ resp := <-cc.inbound
80
+ *r = *resp
81
+ cc.replyToRead = r.Seq
82
+ return nil
83
+ }
84
+
85
+ // ReadResponseBody decodes a response body (currently just string) from the VM.
86
+ func (cc *ClientCodec) ReadResponseBody(body interface{}) error {
87
+ if body != nil {
88
+ *body.(*string) = cc.replies[cc.replyToRead]
89
+ }
90
+ delete(cc.replies, cc.replyToRead)
91
+ return nil
92
+ }
93
+
94
+ // Receive is called by the VM to send a response to the client.
95
+ func (cc *ClientCodec) Receive(rPort int, isError bool, data string) error {
96
+ outb := cc.outbound[rPort]
97
+ delete(cc.outbound, rPort)
98
+ resp := &rpc.Response{
99
+ ServiceMethod: outb.ServiceMethod,
100
+ Seq: outb.Seq,
101
+ }
102
+ if isError {
103
+ resp.Error = data
104
+ } else {
105
+ cc.replies[resp.Seq] = data
106
+ }
107
+ cc.inbound <- resp
108
+ return nil
109
+ }
110
+
111
+ func (cc *ClientCodec) Close() error {
112
+ return nil
113
+ }