Skip to content

Commit

Permalink
Merge branch 'main' into realu/sg-perp
Browse files Browse the repository at this point in the history
  • Loading branch information
Unique-Divine authored Dec 30, 2023
2 parents 40aec00 + f235fe2 commit b7fc955
Show file tree
Hide file tree
Showing 7 changed files with 491 additions and 51 deletions.
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* [#1573](https://github.com/NibiruChain/nibiru/pull/1573) - feat(perp): Close markets and compute settlement price
* [#1705](https://github.com/NibiruChain/nibiru/pull/1705) - feat(perp): Add oracle pair to market object
* [#1718](https://github.com/NibiruChain/nibiru/pull/1718) - fix: fees does not require additional funds
* [#1734](https://github.com/NibiruChain/nibiru/pull/1734) - feat(perp): MsgDonateToPerpFund
* [#1734](https://github.com/NibiruChain/nibiru/pull/1734) - feat(perp): MsgDonateToPerpFund sudo call as part of #1642
* [#1755](https://github.com/NibiruChain/nibiru/pull/1755) - feat(oracle): Add more events on validator's performance

### Non-breaking/Compatible Improvements

Expand All @@ -80,10 +81,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* [#1723](https://github.com/NibiruChain/nibiru/pull/1723) - ci(e2e-wasm.yml): rm unused workflow
* [#1728](https://github.com/NibiruChain/nibiru/pull/1728) - test(devgas-cli): CLI tests for devgas txs
* [#1735](https://github.com/NibiruChain/nibiru/pull/1735) - test(sim): fix simulation tests
* [#1754](https://github.com/NibiruChain/nibiru/pull/1754) - refactor(decode-base64): clean code improvements and fn docs

### Dependencies
- Bump `github.com/prometheus/client_golang` from 1.17.0 to 1.18.0 ([#1750](https://github.com/NibiruChain/nibiru/pull/1750))

- Bump `google.golang.org/protobuf` from 1.31.0 to 1.32.0 ([#1756](https://github.com/NibiruChain/nibiru/pull/1756))
* Bump `google.golang.org/grpc` from 1.59.0 to 1.60.0 ([#1720](https://github.com/NibiruChain/nibiru/pull/1720))
* Bump `golang.org/x/crypto` from 0.15.0 to 0.17.0 ([#1724](https://github.com/NibiruChain/nibiru/pull/1724))
* Bump `github.com/holiman/uint256` from 1.2.3 to 1.2.4 ([#1730](https://github.com/NibiruChain/nibiru/pull/1730))
Expand Down
84 changes: 65 additions & 19 deletions cmd/nibid/cmd/decode_base64.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,39 @@ import (
wasmvm "github.com/CosmWasm/wasmvm/types"
)

// YieldStargateMsgs parses the JSON and sends wasmvm.StargateMsg objects to a channel
func YieldStargateMsgs(jsonBz []byte) ([]wasmvm.StargateMsg, error) {
// YieldStargateMsgs takes a byte slice of JSON data and converts it into a slice
// of wasmvm.StargateMsg objects. This function is essential for processing
// JSON-formatted messages that contain base64-encoded protobuf messages.
//
// Args:
// - jsonBz []byte: A byte slice containing the JSON data to be parsed.
//
// Returns:
// - sgMsgs []wasmvm.StargateMsg: A slice of wasmvm.StargateMsg objects parsed
// from the provided JSON data.
// - err error: An error object, which is nil if the operation is successful.
func YieldStargateMsgs(jsonBz []byte) (sgMsgs []wasmvm.StargateMsg, err error) {
var data interface{}
if err := json.Unmarshal(jsonBz, &data); err != nil {
return nil, err
return sgMsgs, err
}

var msgs []wasmvm.StargateMsg
parseStargateMsgs(data, &msgs)
return msgs, nil
parseStargateMsgs(data, &sgMsgs)
return sgMsgs, nil
}

// parseStargateMsgs is a recursive function used by YieldStargateMsgs to
// traverse the JSON data, filter for any protobuf.Any messages in the
// "WasmVM.StargateMsg" format and decode them from base64 back to human-readable
// form as JSON objects.
//
// Args:
// - jsonData any: JSON data to parse. According to the JSON specification,
// possible value types are:
// Null, Bool, Number(f64), String, Array, or Object(Map<String, Value>)
// - msgs *[]wasmvm.StargateMsg: Mutable reference to a slice of protobuf
// messages. These are potentially altered in place if the value is an
// encoded base 64 string.
func parseStargateMsgs(jsonData any, msgs *[]wasmvm.StargateMsg) {
switch v := jsonData.(type) {
case map[string]interface{}:
Expand All @@ -48,33 +69,51 @@ func parseStargateMsgs(jsonData any, msgs *[]wasmvm.StargateMsg) {
}
}

// StargateMsgDecoded is a struct designed to hold a decoded version of a
// "wasmvm.StargateMsg".
type StargateMsgDecoded struct {
TypeURL string `json:"type_url"`
Value string `json:"value"`
}

// DecodeBase64StargateMsgs decodes a series of base64-encoded
// wasmvm.StargateMsg objects from the provided JSON byte slice (jsonBz).
// This function is vital for extracting and interpreting the contents of these
// protobuf messages.
//
// Args:
// - jsonBz []byte: JSON data containing potential base64-encoded messages.
// - clientCtx client.Context: Context for the `nibid` CLI.
//
// Returns:
// - newSgMsgs []StargateMsgDecoded: The decoded stargate messages.
// - err error: An error object, which is nil if the operation is successful.
func DecodeBase64StargateMsgs(
jsonBz []byte, context client.Context,
jsonBz []byte, clientCtx client.Context,
) (newSgMsgs []StargateMsgDecoded, err error) {
codec := context.Codec
codec := clientCtx.Codec

var data interface{}
if err := json.Unmarshal(jsonBz, &data); err != nil {
return []StargateMsgDecoded{}, err
return newSgMsgs, fmt.Errorf(
"failed to decode stargate msgs due to invalid JSON: %w", err)
}

sgMsgs, err := YieldStargateMsgs(jsonBz)
if err != nil {
return
return newSgMsgs, err
}
for _, sgMsg := range sgMsgs {
valueStr := string(sgMsg.Value)
value := strings.Replace(string(sgMsg.Value), `\"`, `"`, -1)
value = strings.Replace(value, `"{`, `{`, -1)
value = strings.Replace(value, `}"`, `}`, -1)
replacer := strings.NewReplacer(
`\"`, `"`, // old, new
`"{`, `{`,
`}"`, `}`,
)
value := replacer.Replace(string(sgMsg.Value))

if _, err := base64.StdEncoding.DecodeString(valueStr); err == nil {
protoMsg, err := context.InterfaceRegistry.Resolve(sgMsg.TypeURL)
protoMsg, err := clientCtx.InterfaceRegistry.Resolve(sgMsg.TypeURL)
if err != nil {
return newSgMsgs, err
}
Expand All @@ -92,9 +131,13 @@ func DecodeBase64StargateMsgs(
return newSgMsgs, err
}

newSgMsgs = append(newSgMsgs, StargateMsgDecoded{sgMsg.TypeURL, string(outBytes)})
newSgMsgs = append(newSgMsgs,
StargateMsgDecoded{sgMsg.TypeURL, string(outBytes)},
)
} else if _, err := json.Marshal(value); err == nil {
newSgMsgs = append(newSgMsgs, StargateMsgDecoded{sgMsg.TypeURL, string(sgMsg.Value)})
newSgMsgs = append(newSgMsgs,
StargateMsgDecoded{sgMsg.TypeURL, string(sgMsg.Value)},
)
} else {
return newSgMsgs, fmt.Errorf(
"parse error: encountered wasmvm.StargateMsg with unexpected format: %s", sgMsg)
Expand All @@ -103,7 +146,9 @@ func DecodeBase64StargateMsgs(
return newSgMsgs, nil
}

// DecodeBase64Cmd creates a cobra command for base64 decoding.
// DecodeBase64Cmd creates a Cobra command used to decode base64-encoded protobuf
// messages from a JSON input. This function enables users to input arbitrary
// JSON strings and parse the contents of base-64 encoded protobuf.Any messages.
func DecodeBase64Cmd(defaultNodeHome string) *cobra.Command {
cmd := &cobra.Command{
Use: "base64-decode",
Expand All @@ -114,8 +159,9 @@ The input should be a JSON object with 'type_url' and 'value' fields.`,
clientCtx := client.GetClientContextFromCmd(cmd)

outMessage, err := DecodeBase64StargateMsgs([]byte(args[0]), clientCtx)
fmt.Println(outMessage)

if err == nil {
fmt.Println(outMessage)
}
return err
},
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ require (
github.com/stretchr/testify v1.8.4
google.golang.org/genproto/googleapis/api v0.0.0-20231106174013-bbf56f31fb17
google.golang.org/grpc v1.60.0
google.golang.org/protobuf v1.31.0
google.golang.org/protobuf v1.32.0
gopkg.in/yaml.v2 v2.4.0
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1878,8 +1878,8 @@ google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQ
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I=
google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
22 changes: 22 additions & 0 deletions proto/nibiru/oracle/v1/event.proto
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,25 @@ message EventAggregatePrevote {
// transaction messages on behalf of the voting validator.
string feeder = 2;
}


message EventValidatorPerformance{
// Validator is the Bech32 address to which the vote will be credited.
string validator = 1;

// Tendermint consensus voting power
int64 voting_power = 2;

// RewardWeight: Weight of rewards the validator should receive in units of
// consensus power.
int64 reward_weight = 3;

// Number of valid votes for which the validator will be rewarded
int64 win_count = 4;

// Number of abstained votes for which there will be no reward or punishment
int64 abstain_count = 5;

// Number of invalid/punishable votes
int64 miss_count = 6;
}
12 changes: 12 additions & 0 deletions x/oracle/keeper/update_exchange_rates.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ func (k Keeper) UpdateExchangeRates(ctx sdk.Context) types.ValidatorPerformances
params, _ := k.Params.Get(ctx)
k.clearVotesAndPrevotes(ctx, params.VotePeriod)
k.refreshWhitelist(ctx, params.Whitelist, whitelistedPairs)

for _, validatorPerformance := range validatorPerformances {
_ = ctx.EventManager().EmitTypedEvent(&types.EventValidatorPerformance{
Validator: validatorPerformance.ValAddress.String(),
VotingPower: validatorPerformance.Power,
RewardWeight: validatorPerformance.RewardWeight,
WinCount: validatorPerformance.WinCount,
AbstainCount: validatorPerformance.AbstainCount,
MissCount: validatorPerformance.MissCount,
})
}

return validatorPerformances
}

Expand Down
Loading

0 comments on commit b7fc955

Please sign in to comment.