-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(events): only single base64 encode and augment with anchors
- Loading branch information
1 parent
563809d
commit 1739e7d
Showing
1 changed file
with
23 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)), | ||
) | ||
} |