@agoric/cosmos 0.32.3-dev-a1c0b7a.0 → 0.32.3-dev-a21863b.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agoric/cosmos",
3
- "version": "0.32.3-dev-a1c0b7a.0+a1c0b7a",
3
+ "version": "0.32.3-dev-a21863b.0+a21863b",
4
4
  "description": "Connect JS to the Cosmos blockchain SDK",
5
5
  "parsers": {
6
6
  "js": "mjs"
@@ -47,5 +47,5 @@
47
47
  "publishConfig": {
48
48
  "access": "public"
49
49
  },
50
- "gitHead": "a1c0b7a8f05d3f359d07935d0b782adeffb9af4c"
50
+ "gitHead": "a21863b9a2393251b333992e5f33073699d22150"
51
51
  }
@@ -3,7 +3,6 @@ package swingset
3
3
  import (
4
4
  // "fmt"
5
5
  // "os"
6
- "encoding/json"
7
6
  "time"
8
7
 
9
8
  "github.com/cosmos/cosmos-sdk/telemetry"
@@ -23,10 +22,6 @@ type beginBlockAction struct {
23
22
  Params types.Params `json:"params"`
24
23
  }
25
24
 
26
- type beginBlockResult struct {
27
- QueueAllowed []types.QueueSize `json:"queue_allowed"`
28
- }
29
-
30
25
  type endBlockAction struct {
31
26
  Type string `json:"type"`
32
27
  BlockHeight int64 `json:"blockHeight"`
@@ -50,20 +45,14 @@ func BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock, keeper Keeper) erro
50
45
  ChainID: ctx.ChainID(),
51
46
  Params: keeper.GetParams(ctx),
52
47
  }
53
- out, err := keeper.BlockingSend(ctx, action)
48
+ _, err := keeper.BlockingSend(ctx, action)
54
49
  // fmt.Fprintf(os.Stderr, "BEGIN_BLOCK Returned from SwingSet: %s, %v\n", out, err)
55
-
56
- if out != "" {
57
- var result beginBlockResult
58
- err := json.Unmarshal([]byte(out), &result)
59
- if err != nil {
60
- panic(err)
61
- }
62
- state := keeper.GetState(ctx)
63
- state.QueueAllowed = result.QueueAllowed
64
- keeper.SetState(ctx, state)
50
+ if err != nil {
51
+ panic(err)
65
52
  }
66
53
 
54
+ err = keeper.UpdateQueueAllowed(ctx)
55
+
67
56
  return err
68
57
  }
69
58
 
@@ -4,11 +4,13 @@ import (
4
4
  "encoding/json"
5
5
  "errors"
6
6
  "fmt"
7
+ stdlog "log"
7
8
  "math"
8
- "strconv"
9
+ "math/big"
9
10
 
10
11
  "github.com/tendermint/tendermint/libs/log"
11
12
 
13
+ "github.com/cosmos/cosmos-sdk/baseapp"
12
14
  "github.com/cosmos/cosmos-sdk/codec"
13
15
  sdk "github.com/cosmos/cosmos-sdk/types"
14
16
  bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
@@ -32,10 +34,30 @@ const (
32
34
  StoragePathBundles = "bundles"
33
35
  )
34
36
 
35
- const MaxUint53 = 9007199254740991 // Number.MAX_SAFE_INTEGER = 2**53 - 1
37
+ // 2 ** 256 - 1
38
+ var MaxSDKInt = sdk.NewIntFromBigInt(new(big.Int).Sub(new(big.Int).Exp(big.NewInt(2), big.NewInt(256), nil), big.NewInt(1)))
36
39
 
37
40
  const stateKey string = "state"
38
41
 
42
+ // Contextual information about the message source of an action on the queue.
43
+ // This context should be unique per actionQueueRecord.
44
+ type actionContext struct {
45
+ // The block height in which the corresponding action was enqueued
46
+ BlockHeight int64 `json:"blockHeight"`
47
+ // The hash of the cosmos transaction that included the message
48
+ // If the action didn't result from a transaction message, a substitute value
49
+ // may be used. For example the VBANK_BALANCE_UPDATE actions use `x/vbank`.
50
+ TxHash string `json:"txHash"`
51
+ // The index of the message within the transaction. If the action didn't
52
+ // result from a cosmos transaction, a number should be chosen to make the
53
+ // actionContext unique. (for example a counter per block and source module).
54
+ MsgIdx int `json:"msgIdx"`
55
+ }
56
+ type actionQueueRecord struct {
57
+ Action vm.Jsonable `json:"action"`
58
+ Context actionContext `json:"context"`
59
+ }
60
+
39
61
  // Keeper maintains the link to data vstorage and exposes getter/setter methods for the various parts of the state machine
40
62
  type Keeper struct {
41
63
  storeKey sdk.StoreKey
@@ -87,40 +109,55 @@ func NewKeeper(
87
109
  // The actionQueue's format is documented by `makeChainQueue` in
88
110
  // `packages/cosmic-swingset/src/make-queue.js`.
89
111
  func (k Keeper) PushAction(ctx sdk.Context, action vm.Jsonable) error {
90
- bz, err := json.Marshal(action)
112
+ txHash, txHashOk := ctx.Context().Value(baseapp.TxHashContextKey).(string)
113
+ if !txHashOk {
114
+ txHash = "unknown"
115
+ }
116
+ msgIdx, msgIdxOk := ctx.Context().Value(baseapp.TxMsgIdxContextKey).(int)
117
+ if !txHashOk || !msgIdxOk {
118
+ stdlog.Printf("error while extracting context for action %q\n", action)
119
+ }
120
+ record := actionQueueRecord{Action: action, Context: actionContext{BlockHeight: ctx.BlockHeight(), TxHash: txHash, MsgIdx: msgIdx}}
121
+ bz, err := json.Marshal(record)
91
122
  if err != nil {
92
123
  return err
93
124
  }
94
125
 
95
126
  // Get the current queue tail, defaulting to zero if its vstorage doesn't exist.
127
+ // The `tail` is the value of the next index to be inserted
96
128
  tail, err := k.actionQueueIndex(ctx, "tail")
97
129
  if err != nil {
98
130
  return err
99
131
  }
100
132
 
101
- // JS uses IEEE 754 floats so avoid overflowing integers
102
- if tail == MaxUint53 {
133
+ if tail.Equal(MaxSDKInt) {
103
134
  return errors.New(StoragePathActionQueue + " overflow")
104
135
  }
136
+ nextTail := tail.Add(sdk.NewInt(1))
105
137
 
106
138
  // Set the vstorage corresponding to the queue entry for the current tail.
107
- path := StoragePathActionQueue + "." + strconv.FormatUint(tail, 10)
139
+ path := StoragePathActionQueue + "." + tail.String()
108
140
  k.vstorageKeeper.SetStorage(ctx, vstoragetypes.NewStorageEntry(path, string(bz)))
109
141
 
110
142
  // Update the tail to point to the next available entry.
111
143
  path = StoragePathActionQueue + ".tail"
112
- k.vstorageKeeper.SetStorage(ctx, vstoragetypes.NewStorageEntry(path, strconv.FormatUint(tail+1, 10)))
144
+ k.vstorageKeeper.SetStorage(ctx, vstoragetypes.NewStorageEntry(path, nextTail.String()))
113
145
  return nil
114
146
  }
115
147
 
116
- func (k Keeper) actionQueueIndex(ctx sdk.Context, name string) (uint64, error) {
117
- index := uint64(0)
118
- var err error
119
- indexEntry := k.vstorageKeeper.GetEntry(ctx, StoragePathActionQueue+"."+name)
120
- if indexEntry.HasData() {
121
- index, err = strconv.ParseUint(indexEntry.StringValue(), 10, 64)
148
+ func (k Keeper) actionQueueIndex(ctx sdk.Context, position string) (sdk.Int, error) {
149
+ // Position should be either "head" or "tail"
150
+ path := StoragePathActionQueue + "." + position
151
+ indexEntry := k.vstorageKeeper.GetEntry(ctx, path)
152
+ if !indexEntry.HasData() {
153
+ return sdk.NewInt(0), nil
122
154
  }
123
- return index, err
155
+
156
+ index, ok := sdk.NewIntFromString(indexEntry.StringValue())
157
+ if !ok {
158
+ return index, fmt.Errorf("couldn't parse %s as Int: %s", path, indexEntry.StringValue())
159
+ }
160
+ return index, nil
124
161
  }
125
162
 
126
163
  func (k Keeper) ActionQueueLength(ctx sdk.Context) (int32, error) {
@@ -132,11 +169,50 @@ func (k Keeper) ActionQueueLength(ctx sdk.Context) (int32, error) {
132
169
  if err != nil {
133
170
  return 0, err
134
171
  }
135
- size := tail - head
136
- if size > math.MaxInt32 {
172
+ // The tail index is exclusive
173
+ size := tail.Sub(head)
174
+ if !size.IsInt64() {
175
+ return 0, fmt.Errorf("%s size too big: %s", StoragePathActionQueue, size)
176
+ }
177
+
178
+ int64Size := size.Int64()
179
+ if int64Size > math.MaxInt32 {
137
180
  return math.MaxInt32, nil
138
181
  }
139
- return int32(size), nil
182
+ return int32(int64Size), nil
183
+ }
184
+
185
+ func (k Keeper) UpdateQueueAllowed(ctx sdk.Context) error {
186
+ params := k.GetParams(ctx)
187
+ inboundQueueMax, found := types.QueueSizeEntry(params.QueueMax, types.QueueInbound)
188
+ if !found {
189
+ return errors.New("could not find max inboundQueue size in params")
190
+ }
191
+ inboundMempoolQueueMax := inboundQueueMax / 2
192
+
193
+ inboundQueueSize, err := k.ActionQueueLength(ctx)
194
+ if err != nil {
195
+ return err
196
+ }
197
+
198
+ var inboundQueueAllowed int32
199
+ if inboundQueueMax > inboundQueueSize {
200
+ inboundQueueAllowed = inboundQueueMax - inboundQueueSize
201
+ }
202
+
203
+ var inboundMempoolQueueAllowed int32
204
+ if inboundMempoolQueueMax > inboundQueueSize {
205
+ inboundMempoolQueueAllowed = inboundMempoolQueueMax - inboundQueueSize
206
+ }
207
+
208
+ state := k.GetState(ctx)
209
+ state.QueueAllowed = []types.QueueSize{
210
+ {Key: types.QueueInbound, Size_: inboundQueueAllowed},
211
+ {Key: types.QueueInboundMempool, Size_: inboundMempoolQueueAllowed},
212
+ }
213
+ k.SetState(ctx, state)
214
+
215
+ return nil
140
216
  }
141
217
 
142
218
  // BlockingSend sends a message to the controller and blocks the Golang process
package/x/vbank/vbank.go CHANGED
@@ -1,12 +1,14 @@
1
1
  package vbank
2
2
 
3
3
  import (
4
+ "context"
4
5
  "encoding/json"
5
6
  "fmt"
6
7
  stdlog "log"
7
8
  "sort"
8
9
 
9
10
  "github.com/Agoric/agoric-sdk/golang/cosmos/vm"
11
+ "github.com/cosmos/cosmos-sdk/baseapp"
10
12
  sdk "github.com/cosmos/cosmos-sdk/types"
11
13
  )
12
14
 
@@ -241,5 +243,11 @@ func (ch portHandler) Receive(ctx *vm.ControllerContext, str string) (ret string
241
243
  }
242
244
 
243
245
  func (am AppModule) PushAction(ctx sdk.Context, action vm.Jsonable) error {
246
+ // vbank actions are not triggered by a swingset message in a transaction, so we need to
247
+ // synthesize unique context information.
248
+ // We use a fixed placeholder value for the txHash context, and can simply use `0` for the
249
+ // message index as there is only one such action per block.
250
+ ctx = ctx.WithContext(context.WithValue(ctx.Context(), baseapp.TxHashContextKey, "x/vbank"))
251
+ ctx = ctx.WithContext(context.WithValue(ctx.Context(), baseapp.TxMsgIdxContextKey, 0))
244
252
  return am.keeper.PushAction(ctx, action)
245
253
  }