Skip to content

Commit 9dc151a

Browse files
committed
fix(vbank): be sure to persist nonce state in the KVStore
1 parent 9d9560d commit 9dc151a

File tree

10 files changed

+210
-73
lines changed

10 files changed

+210
-73
lines changed

golang/cosmos/proto/agoric/vbank/genesis.proto

+3
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,7 @@ message GenesisState {
1111

1212
// parms defines all the parameters of the module.
1313
Params params = 1 [(gogoproto.nullable) = false];
14+
15+
// state is the current operation state.
16+
State state = 2 [(gogoproto.nullable) = false];
1417
}

golang/cosmos/proto/agoric/vbank/vbank.proto

+5-1
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,14 @@ message State {
2222
// NOTE: Tracking manually since there is no bank call for getting a
2323
// module account balance by name.
2424
repeated cosmos.base.v1beta1.Coin rewardPool = 1
25-
[(gogoproto.nullable) = false, (gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins"];
25+
[(gogoproto.nullable) = false, (gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins"
26+
];
2627

2728
// rewardRate is the amount of reward, if available, to send to the
2829
// fee collector module on every block.
2930
repeated cosmos.base.v1beta1.Coin rewardRate = 2
3031
[(gogoproto.nullable) = false, (gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins"];
32+
33+
// lastNonce is a sequence number for communicating with the VM.
34+
uint64 lastNonce = 3;
3135
}

golang/cosmos/x/vbank/genesis.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,14 @@ func DefaultGenesisState() *types.GenesisState {
2727
}
2828

2929
func InitGenesis(ctx sdk.Context, keeper Keeper, data *types.GenesisState) []abci.ValidatorUpdate {
30-
keeper.SetGenesis(ctx, *data)
30+
keeper.SetParams(ctx, data.GetParams())
31+
keeper.SetState(ctx, data.GetState())
3132
return []abci.ValidatorUpdate{}
3233
}
3334

3435
func ExportGenesis(ctx sdk.Context, k Keeper) *types.GenesisState {
35-
gs := k.GetGenesis(ctx)
36+
var gs types.GenesisState
37+
gs.Params = k.GetParams(ctx)
38+
gs.State = k.GetState(ctx)
3639
return &gs
3740
}

golang/cosmos/x/vbank/keeper/keeper.go

+7-18
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"github.com/Agoric/agoric-sdk/golang/cosmos/x/vbank/types"
88
)
99

10-
const genesisKey string = "genesis"
1110
const paramsKey string = "params"
1211
const stateKey string = "state"
1312

@@ -39,23 +38,6 @@ func NewKeeper(
3938
}
4039
}
4140

42-
func (k Keeper) GetGenesis(ctx sdk.Context) types.GenesisState {
43-
store := ctx.KVStore(k.storeKey)
44-
bz := store.Get([]byte(genesisKey))
45-
var gs types.GenesisState
46-
k.cdc.MustUnmarshalLengthPrefixed(bz, &gs)
47-
return gs
48-
}
49-
50-
func (k Keeper) SetGenesis(ctx sdk.Context, data types.GenesisState) {
51-
store := ctx.KVStore(k.storeKey)
52-
store.Set([]byte(genesisKey), k.cdc.MustMarshalLengthPrefixed(&data))
53-
params := types.Params{
54-
FeeEpochDurationBlocks: data.GetParams().FeeEpochDurationBlocks,
55-
}
56-
k.SetParams(ctx, params)
57-
}
58-
5941
func (k Keeper) GetBalance(ctx sdk.Context, addr sdk.AccAddress, denom string) sdk.Coin {
6042
return k.bankKeeper.GetBalance(ctx, addr, denom)
6143
}
@@ -113,3 +95,10 @@ func (k Keeper) SetState(ctx sdk.Context, state types.State) {
11395
bz := k.cdc.MustMarshal(&state)
11496
store.Set([]byte(stateKey), bz)
11597
}
98+
99+
func (k Keeper) GetNextNonce(ctx sdk.Context) uint64 {
100+
state := k.GetState(ctx)
101+
state.LastNonce = state.GetLastNonce() + 1
102+
k.SetState(ctx, state)
103+
return state.LastNonce
104+
}

golang/cosmos/x/vbank/module.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ func (am AppModule) EndBlock(ctx sdk.Context, req abci.RequestEndBlock) []abci.V
183183
}
184184

185185
// Dump all the addressToBalances entries to SwingSet.
186-
bz, err := marshalBalanceUpdate(addressToBalance)
186+
bz, err := marshalBalanceUpdate(ctx, am.keeper, addressToBalance)
187187
if err != nil {
188188
panic(err)
189189
}

golang/cosmos/x/vbank/types/genesis.pb.go

+68-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

golang/cosmos/x/vbank/types/msgs.go

+12
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
11
package types
22

33
const RouterKey = ModuleName // this was defined in your key.go file
4+
5+
type VbankSingleBalanceUpdate struct {
6+
Address string `json:"address"`
7+
Denom string `json:"denom"`
8+
Amount string `json:"amount"`
9+
}
10+
11+
type VbankBalanceUpdate struct {
12+
Nonce uint64 `json:"nonce"`
13+
Type string `json:"type"`
14+
Updated []VbankSingleBalanceUpdate `json:"updated"`
15+
}

golang/cosmos/x/vbank/types/vbank.pb.go

+69-23
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)