Skip to content

Commit

Permalink
feat: plumb through the genesis data to vpurse initialisation
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed May 1, 2021
1 parent 43c5db5 commit 8105589
Show file tree
Hide file tree
Showing 6 changed files with 543 additions and 26 deletions.
38 changes: 16 additions & 22 deletions golang/cosmos/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ func NewAgoricApp(

// This function is tricky to get right, so we build it ourselves.
callToController := func(ctx sdk.Context, str string) (string, error) {
app.MustInitController(ctx)
defer swingset.SetControllerContext(ctx)()
return sendToController(true, str)
}
Expand Down Expand Up @@ -423,7 +424,7 @@ func NewAgoricApp(
upgradetypes.ModuleName, minttypes.ModuleName, distrtypes.ModuleName, slashingtypes.ModuleName,
evidencetypes.ModuleName, stakingtypes.ModuleName, ibchost.ModuleName, swingset.ModuleName,
)
app.mm.SetOrderEndBlockers(swingset.ModuleName, crisistypes.ModuleName, govtypes.ModuleName, stakingtypes.ModuleName)
app.mm.SetOrderEndBlockers(vpurse.ModuleName, swingset.ModuleName, crisistypes.ModuleName, govtypes.ModuleName, stakingtypes.ModuleName)

// NOTE: The genutils module must occur after staking so that pools are
// properly initialized with tokens from genesis accounts.
Expand All @@ -433,7 +434,9 @@ func NewAgoricApp(
app.mm.SetOrderInitGenesis(
capabilitytypes.ModuleName, authtypes.ModuleName, banktypes.ModuleName, distrtypes.ModuleName, stakingtypes.ModuleName,
slashingtypes.ModuleName, govtypes.ModuleName, minttypes.ModuleName, crisistypes.ModuleName,
ibchost.ModuleName, genutiltypes.ModuleName, evidencetypes.ModuleName, swingset.ModuleName, ibctransfertypes.ModuleName,
ibchost.ModuleName, genutiltypes.ModuleName, evidencetypes.ModuleName,
ibctransfertypes.ModuleName,
vpurse.ModuleName, swingset.ModuleName,
)

app.mm.RegisterInvariants(&app.CrisisKeeper)
Expand Down Expand Up @@ -527,23 +530,16 @@ func (app *GaiaApp) MustInitController(ctx sdk.Context) {
}
app.controllerInited = true

/*
FIXME: Get these from genesis!
- name: mallory
type: local
address: agoric1z8rldx498tf49p5ze04jhakyumkh7vyxku7e0p
pubkey: agoricpub1addwnpepqtzw4lz7yjhg7qljwf0jqaykj6q94hh9epxa6nq7u0a3hxhsn7u670cgwnz
mnemonic: ""
threshold: 0
pubkeys: []
*/
bootstrapAddr, err := sdk.AccAddressFromBech32("agoric1z8rldx498tf49p5ze04jhakyumkh7vyxku7e0p")
if err != nil {
fmt.Fprintln(os.Stderr, "Cannot get bootstrap addr", err)
os.Exit(1)
var bootstrapAddr sdk.AccAddress
gs := app.VpurseKeeper.GetGenesis(ctx)
if len(gs.BootstrapAddress) > 0 {
ba, err := sdk.AccAddressFromBech32(gs.BootstrapAddress)
if err != nil {
fmt.Fprintln(os.Stderr, "Cannot get bootstrap addr", err)
os.Exit(1)
}
bootstrapAddr = ba
}
bootstrapValue := "50000000000"
donationValue := "5000000"

// Begin initializing the controller here.
action := &cosmosInitAction{
Expand All @@ -553,8 +549,8 @@ func (app *GaiaApp) MustInitController(ctx sdk.Context) {
StoragePort: swingset.GetPort("storage"),
ChainID: ctx.ChainID(),
BootstrapAddress: bootstrapAddr.String(),
BootstrapValue: bootstrapValue,
DonationValue: donationValue,
BootstrapValue: gs.BootstrapValue.String(),
DonationValue: gs.DonationValue.String(),
}
bz, err := json.Marshal(action)
if err == nil {
Expand All @@ -568,7 +564,6 @@ func (app *GaiaApp) MustInitController(ctx sdk.Context) {

// BeginBlocker application updates every begin block
func (app *GaiaApp) BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock) abci.ResponseBeginBlock {
app.MustInitController(ctx)
return app.mm.BeginBlock(ctx, req)
}

Expand Down Expand Up @@ -598,7 +593,6 @@ func updateTransferPort(gs GenesisState, reservedPort, newPort string) error {

// InitChainer application update at chain initialization
func (app *GaiaApp) InitChainer(ctx sdk.Context, req abci.RequestInitChain) abci.ResponseInitChain {
app.MustInitController(ctx)
var genesisState GenesisState
if err := tmjson.Unmarshal(req.AppStateBytes, &genesisState); err != nil {
panic(err)
Expand Down
20 changes: 20 additions & 0 deletions golang/cosmos/proto/agoric/vpurse/genesis.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
syntax = "proto3";
package agoric.vpurse;

import "gogoproto/gogo.proto";

option go_package = "github.com/Agoric/agoric-sdk/golang/cosmos/x/vpurse/types";

message GenesisState {
option (gogoproto.equal) = false;

string bootstrap_address = 1;
string bootstrap_value = 2 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];
string donation_value = 3 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];
}
61 changes: 61 additions & 0 deletions golang/cosmos/x/vpurse/genesis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package vpurse

import (
// "fmt"

"fmt"

"github.com/Agoric/agoric-sdk/golang/cosmos/x/vpurse/types"
sdk "github.com/cosmos/cosmos-sdk/types"
abci "github.com/tendermint/tendermint/abci/types"
)

func NewGenesisState() *types.GenesisState {
return &types.GenesisState{
BootstrapAddress: "",
BootstrapValue: sdk.NewInt(0),
DonationValue: sdk.NewInt(0),
}
}

func ValidateGenesis(data *types.GenesisState) error {
if data == nil {
return fmt.Errorf("vpurse genesis data cannot be nil")
}
if len(data.BootstrapAddress) > 0 {
if _, err := sdk.AccAddressFromBech32(data.BootstrapAddress); err != nil {
return fmt.Errorf("vpurse genesis invalid bootstrapAdddress %s: %w", data.BootstrapAddress, err)
}
}
if data.BootstrapValue.IsNil() {
return fmt.Errorf("vpurse genesis bootstrapValue cannot be nil")
}
if data.BootstrapValue.IsNegative() {
return fmt.Errorf("vpurse genesis bootstrapValue %s cannot be negative", data.DonationValue.String())
}
if data.DonationValue.IsNil() {
return fmt.Errorf("vpurse genesis donationValue cannot be nil")
}
if data.DonationValue.IsNegative() {
return fmt.Errorf("vpurse genesis donationValue %s cannot be negative", data.DonationValue.String())
}
return nil
}

func DefaultGenesisState() *types.GenesisState {
gs := NewGenesisState()
fmt.Println("default gen", gs)
return gs
}

func InitGenesis(ctx sdk.Context, keeper Keeper, data *types.GenesisState) []abci.ValidatorUpdate {
keeper.SetGenesis(ctx, *data)
fmt.Println("initialising gen", *data)
return []abci.ValidatorUpdate{}
}

func ExportGenesis(ctx sdk.Context, k Keeper) *types.GenesisState {
gs := k.GetGenesis(ctx)
fmt.Println("exporting gen", gs)
return &gs
}
15 changes: 15 additions & 0 deletions golang/cosmos/x/vpurse/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"github.com/Agoric/agoric-sdk/golang/cosmos/x/vpurse/types"
)

const genesis string = "genesis"

// Keeper maintains the link to data storage and exposes getter/setter methods for the various parts of the state machine
type Keeper struct {
storeKey sdk.StoreKey
Expand All @@ -35,6 +37,19 @@ func NewKeeper(
}
}

func (k Keeper) GetGenesis(ctx sdk.Context) types.GenesisState {
store := ctx.KVStore(k.storeKey)
bz := store.Get([]byte(genesis))
var gs types.GenesisState
k.cdc.MustUnmarshalBinaryLengthPrefixed(bz, &gs)
return gs
}

func (k Keeper) SetGenesis(ctx sdk.Context, data types.GenesisState) {
store := ctx.KVStore(k.storeKey)
store.Set([]byte(genesis), k.cdc.MustMarshalBinaryLengthPrefixed(&data))
}

func (k Keeper) GetBalance(ctx sdk.Context, addr sdk.AccAddress, denom string) sdk.Coin {
return k.bankKeeper.GetBalance(ctx, addr, denom)
}
Expand Down
15 changes: 11 additions & 4 deletions golang/cosmos/x/vpurse/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,16 @@ func (b AppModuleBasic) RegisterInterfaces(registry cdctypes.InterfaceRegistry)

// DefaultGenesis returns default genesis state as raw bytes for the deployment
func (AppModuleBasic) DefaultGenesis(cdc codec.JSONMarshaler) json.RawMessage {
return nil
return cdc.MustMarshalJSON(DefaultGenesisState())
}

// Validation check of the Genesis
func (AppModuleBasic) ValidateGenesis(cdc codec.JSONMarshaler, config client.TxEncodingConfig, bz json.RawMessage) error {
return nil
var data types.GenesisState
if err := cdc.UnmarshalJSON(bz, &data); err != nil {
return err
}
return ValidateGenesis(&data)
}

// Register rest routes
Expand Down Expand Up @@ -125,11 +129,14 @@ func (am AppModule) RegisterServices(cfg module.Configurator) {
// InitGenesis performs genesis initialization for the ibc-transfer module. It returns
// no validator updates.
func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONMarshaler, data json.RawMessage) []abci.ValidatorUpdate {
return []abci.ValidatorUpdate{}
var genesisState types.GenesisState
cdc.MustUnmarshalJSON(data, &genesisState)
return InitGenesis(ctx, am.keeper, &genesisState)
}

// ExportGenesis returns the exported genesis state as raw bytes for the ibc-transfer
// module.
func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONMarshaler) json.RawMessage {
return nil
gs := ExportGenesis(ctx, am.keeper)
return cdc.MustMarshalJSON(gs)
}
Loading

0 comments on commit 8105589

Please sign in to comment.