Skip to content

Commit

Permalink
fix(golang): add actionQueue overflow check
Browse files Browse the repository at this point in the history
  • Loading branch information
mhofman authored and mergify[bot] committed Oct 5, 2022
1 parent 05ed02b commit 5a579d9
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions golang/cosmos/x/swingset/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"encoding/json"
"errors"
"fmt"
"strconv"

Expand Down Expand Up @@ -29,6 +30,8 @@ const (
StoragePathBundles = "bundles"
)

const MaxUint53 = 9007199254740991 // Number.MAX_SAFE_INTEGER = 2**53 - 1

// Keeper maintains the link to data vstorage and exposes getter/setter methods for the various parts of the state machine
type Keeper struct {
storeKey sdk.StoreKey
Expand Down Expand Up @@ -78,22 +81,22 @@ func NewKeeper(
// intermediate transaction state.
//
// The actionQueue's format is documented by `makeChainQueue` in
// `packages/cosmic-swingset/src/chain-main.js`.
// `packages/cosmic-swingset/src/make-queue.js`.
func (k Keeper) PushAction(ctx sdk.Context, action vm.Jsonable) error {
bz, err := json.Marshal(action)
if err != nil {
return err
}

// Get the current queue tail, defaulting to zero if its vstorage doesn't exist.
tail := uint64(0)
tailStr := k.vstorageKeeper.GetData(ctx, "actionQueue.tail")
if len(tailStr) > 0 {
// Found, so parse it.
tail, err = strconv.ParseUint(tailStr, 10, 64)
if err != nil {
return err
}
tail, err := k.actionQueueIndex(ctx, "tail")
if err != nil {
return err
}

// JS uses IEEE 754 floats so avoid overflowing integers
if tail == MaxUint53 {
return errors.New("actionQueue overflow")
}

// Set the vstorage corresponding to the queue entry for the current tail.
Expand All @@ -104,6 +107,16 @@ func (k Keeper) PushAction(ctx sdk.Context, action vm.Jsonable) error {
return nil
}

func (k Keeper) actionQueueIndex(ctx sdk.Context, name string) (uint64, error) {
index := uint64(0)
var err error
indexStr := k.vstorageKeeper.GetData(ctx, "actionQueue."+name)
if indexStr != "" {
index, err = strconv.ParseUint(indexStr, 10, 64)
}
return index, err
}

// BlockingSend sends a message to the controller and blocks the Golang process
// until the response. It is orthogonal to PushAction, and should only be used
// by SwingSet to perform block lifecycle events (BEGIN_BLOCK, END_BLOCK,
Expand Down

0 comments on commit 5a579d9

Please sign in to comment.