Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Genesis and Config] Replace the placeholders with mvp implementations #154

Closed
12 tasks
andrewnguyen22 opened this issue Aug 7, 2022 · 1 comment · Fixed by #166
Closed
12 tasks

[Genesis and Config] Replace the placeholders with mvp implementations #154

andrewnguyen22 opened this issue Aug 7, 2022 · 1 comment · Fixed by #166
Assignees
Labels
code health Nice to have code improvement core Core infrastructure - protocol related

Comments

@andrewnguyen22
Copy link
Contributor

Objective

  • Replace genesis.json with mvp genesis object json file
  • Replace config.json with mvp config object json file
  • Explicitly define and document the genesis and configuration processing lifecycle for each module
  • Explicitly define and document the genesis state exportation (state dumps) for each module

Origin Document

The current implementation is a POC that enables the mvp. In order to achieve a successful M1, true genesis and configuration processing is (P0) required.

Goals / Deliverables

  • Code complete implementation
  • Specified documentation under #objective

General issue checklist

  • Update the appropriate CHANGELOG
  • Update the README
  • If applicable, update the source code tree explanation
  • If applicable, add or update a state, sequence or flowchart diagram using mermaid
  • Update any relevant global documentation & references
  • Document small issues / TODOs along the way

Creator: @andrewnguyen22

@andrewnguyen22 andrewnguyen22 added the core Core infrastructure - protocol related label Aug 7, 2022
@Olshansk
Copy link
Member

Olshansk commented Aug 8, 2022

@andrewnguyen22 Since you plan on tackling this later this week, wanted to add something I've been wanting to fix here. Partially, I'm leaving it as a comment so I don't forget either.

Inside of shared/types/genesis/validator.go, I added the code snippet below, which was a hack I'm a bit ashamed of. It was needed in order to be able to use the same type (generated strcut from protobuf) in both our business logic and when loading a type from a json file.

However, per the comment from @deblasis here, there is an opportunity to leverage protoc-go-inject-tag to use //@gotags which will attach "metadata" to the fields so that the generated structs have them in the form of struct tags. We can then use both types without this hack.

package genesis

import (
	"encoding/hex"
	"encoding/json"

	"google.golang.org/protobuf/encoding/protojson"
)

// HACK: Since the protocol actor protobufs (e.g. validator, fisherman, etc) use `bytes` for some
// fields (e.g. `address`, `output`, `publicKey`), we need to use a helper struct to unmarshal the
// the types when they are defined via json (e.g. genesis file, testing configurations, etc...).
// Alternative solutions could include whole wrapper structs (i.e. duplication of schema definition),
// using strings instead of bytes (i.e. major change with downstream effects) or avoid defining these
// types in json altogether (i.e. limitation of usability).
type JsonBytesLoaderHelper struct {
	Address   HexData `json:"address,omitempty"`
	PublicKey HexData `json:"public_key,omitempty"`
	Output    HexData `json:"output,omitempty"`
}

type HexData []byte

func (h *HexData) UnmarshalJSON(data []byte) error {
	var s string
	if err := json.Unmarshal(data, &s); err != nil {
		return err
	}
	decoded, err := hex.DecodeString(s)
	if err != nil {
		return err
	}
	*h = HexData(decoded)
	return nil
}

func (v *Validator) UnmarshalJSON(data []byte) error {
	var jh JsonBytesLoaderHelper
	json.Unmarshal(data, &jh)

	protojson.Unmarshal(data, v)
	v.Address = jh.Address
	v.PublicKey = jh.PublicKey
	v.Output = jh.Output

	return nil
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
code health Nice to have code improvement core Core infrastructure - protocol related
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

2 participants