Skip to content

Commit

Permalink
feat(events): only single base64 encode and augment with anchors
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Jan 26, 2023
1 parent 563809d commit 1739e7d
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions golang/cosmos/types/events.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,41 @@
package types

import (
"encoding/base64"
"bytes"

sdk "github.com/cosmos/cosmos-sdk/types"
)

const (
EventTypeStateChange = "state_change"

AttributeKeyStoreName = "store_name"
AttributeKeyStoreSubkey = "store_subkey"
AttributeKeyUnprovedValue = "unproved_value"
AttributeKeyStoreName = "store"
AttributeKeyStoreSubkey = "key"
AttributeKeyAnchoredKey = "anckey"
AttributeKeyUnprovedValue = "value"

// We chose \1 so that it is not a valid character in a vstorage path.
AnchoredKeyStart = "\x01"
AnchoredKeyEnd = AnchoredKeyStart
)

func NewStateChangeEvent(storeName string, subkey, value []byte) sdk.Event {
// Bytes are base64-encoded.
// This is a hack to allow CONTAINS event queries to match the
// beginning or end of the subkey, enabling a handy OR of changes to a
// vstorage path's immediate children.
//
// FIXME: add string value ranges to Tendermint event queries instead.
anchoredKey := bytes.Join([][]byte{
[]byte(AnchoredKeyStart),
subkey,
[]byte(AnchoredKeyEnd),
}, []byte{})

return sdk.NewEvent(
EventTypeStateChange,
sdk.NewAttribute(AttributeKeyStoreName, storeName),
sdk.NewAttribute(AttributeKeyStoreSubkey, base64.StdEncoding.EncodeToString(subkey)),
sdk.NewAttribute(AttributeKeyUnprovedValue, base64.StdEncoding.EncodeToString(value)),
sdk.NewAttribute(AttributeKeyStoreSubkey, string(subkey)),
sdk.NewAttribute(AttributeKeyAnchoredKey, string(anchoredKey)),
sdk.NewAttribute(AttributeKeyUnprovedValue, string(value)),
)
}

0 comments on commit 1739e7d

Please sign in to comment.