Skip to content

Commit

Permalink
Import/export
Browse files Browse the repository at this point in the history
  • Loading branch information
danil-lashin committed May 4, 2022
1 parent fa16190 commit 0bc4e02
Show file tree
Hide file tree
Showing 6 changed files with 450 additions and 68 deletions.
7 changes: 7 additions & 0 deletions module/proto/mhub2/v1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ message GenesisState {
TokenInfos token_infos = 6;
}

message Nonce {
string validator_address = 1;
uint64 last_event_nonce = 2;
}

message ExternalState {
string chain_id = 1;
repeated ExternalEventVoteRecord external_event_vote_records = 2;
Expand All @@ -123,4 +128,6 @@ message ExternalState {
repeated google.protobuf.Any outgoing_txs = 6;
repeated google.protobuf.Any confirmations = 7;
uint64 sequence = 8;
repeated Nonce nonces = 9;
SignerSetTx last_observed_valset = 10;
}
2 changes: 1 addition & 1 deletion module/x/mhub2/keeper/external_event_vote.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (k Keeper) recordEventVote(ctx sdk.Context, chainId types.ChainID, event ty
// and prevents validators from submitting two claims with the same nonce
lastEventNonce := k.getLastEventNonceByValidator(ctx, chainId, val)
expectedNonce := lastEventNonce + 1
if event.GetEventNonce() != expectedNonce && lastEventNonce != 0 {
if event.GetEventNonce() != expectedNonce && lastEventNonce != 0 { // todo - should validator be able to skip nonces?
return nil, sdkerrors.Wrapf(types.ErrInvalid,
"non contiguous event nonce expected %v observed %v for validator %v",
expectedNonce,
Expand Down
23 changes: 21 additions & 2 deletions module/x/mhub2/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ func InitGenesis(ctx sdk.Context, k Keeper, data types.GenesisState) {
// this will be easy.
k.SetExternalSignature(ctx, chainId, conf, sdk.ValAddress{})
}

for _, nonce := range externalState.Nonces {
val, _ := sdk.ValAddressFromBech32(nonce.ValidatorAddress)
k.setLastEventNonceByValidator(ctx, chainId, val, nonce.LastEventNonce)
}

k.setLastObservedSignerSetTx(ctx, chainId, *externalState.LastObservedValset)
}
}

Expand All @@ -105,15 +112,27 @@ func ExportGenesis(ctx sdk.Context, k Keeper) types.GenesisState {

for _, chainId := range chains {
var (
delegates = k.getDelegateKeys(ctx, chainId)
lastobserved = k.GetLastObservedEventNonce(ctx, chainId)
delegates = k.getDelegateKeys(ctx, chainId)
nonces = k.getNonces(ctx, chainId)
lastobserved = k.GetLastObservedEventNonce(ctx, chainId)
lastobservedvalset = k.GetLastObservedSignerSetTx(ctx, chainId)
)

if chainId == "minter" {
for i := range nonces {
nonces[i].LastEventNonce = 0
}

lastobserved = 0
}

state.ExternalStates = append(state.ExternalStates, &types.ExternalState{
ChainId: chainId.String(),
DelegateKeys: delegates,
Nonces: nonces,
LastObservedEventNonce: lastobserved,
Sequence: k.getOutgoingSequence(ctx, chainId),
LastObservedValset: lastobservedvalset,
})
}

Expand Down
21 changes: 21 additions & 0 deletions module/x/mhub2/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,27 @@ func (k Keeper) getDelegateKeys(ctx sdk.Context, chainId types.ChainID) (out []*
return out
}

func (k Keeper) getNonces(ctx sdk.Context, chainId types.ChainID) (out []*types.Nonce) {
store := ctx.KVStore(k.storeKey)
iter := prefix.NewStore(store, append([]byte{types.LastEventNonceByValidatorKey}, chainId.Bytes()...)).Iterator(nil, nil)
for ; iter.Valid(); iter.Next() {
out = append(out, &types.Nonce{
ValidatorAddress: sdk.ValAddress(iter.Key()).String(),
LastEventNonce: binary.BigEndian.Uint64(iter.Value()),
})
}
iter.Close()

// we iterated over a map, so now we have to sort to ensure the
// output here is deterministic, eth address chosen for no particular
// reason
sort.Slice(out[:], func(i, j int) bool {
return out[i].ValidatorAddress < out[j].ValidatorAddress
})

return out
}

// GetUnbondingvalidators returns UnbondingValidators.
// Adding here in mhub2 keeper as cdc is available inside endblocker.
func (k Keeper) GetUnbondingvalidators(unbondingVals []byte) stakingtypes.ValAddresses {
Expand Down
Loading

0 comments on commit 0bc4e02

Please sign in to comment.