Skip to content

Instantly share code, notes, and snippets.

@zmanian
Created August 10, 2019 20:38
Show Gist options
  • Save zmanian/935dca8e11a9a98656b1e4cab5e47847 to your computer and use it in GitHub Desktop.
Save zmanian/935dca8e11a9a98656b1e4cab5e47847 to your computer and use it in GitHub Desktop.

Unmarshal/deserialize

val := types.MustUnmarshalValidator(cdc, res)

Now this is a very common construct in Go.

It consists of 3 key pieces

A codec object: cdc

A serialized data: res

And a helper function:types.MustUnmarshalValidator

The code below is functionally identical to the code above.

var val Validator

err = cdc.UnmarshalBinaryLengthPrefixed(res, &validator)

if err != nil{
   panic(err)
}

The codec object knows how to convert bytes to the types in cosmos-SDK

If the codec does not recognize the target receiver objects of validator is matches the bytes. Ie. You can turn the res bytes into a validator struct safety. It will error and then the error handler will panic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment