2019-08-13 15:16:03 -07:00
|
|
|
package simulation
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
|
2020-04-21 14:33:56 -07:00
|
|
|
gogotypes "github.com/gogo/protobuf/types"
|
2019-08-13 15:16:03 -07:00
|
|
|
|
2020-05-20 12:21:00 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
2020-07-30 07:53:02 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/types/kv"
|
2019-08-13 15:16:03 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth/types"
|
|
|
|
)
|
|
|
|
|
2020-05-20 12:21:00 -07:00
|
|
|
type AuthUnmarshaler interface {
|
|
|
|
UnmarshalAccount([]byte) (types.AccountI, error)
|
2020-07-24 12:04:29 -07:00
|
|
|
GetCodec() codec.BinaryMarshaler
|
2020-05-20 12:21:00 -07:00
|
|
|
}
|
|
|
|
|
2020-04-21 14:33:56 -07:00
|
|
|
// NewDecodeStore returns a decoder function closure that unmarshals the KVPair's
|
|
|
|
// Value to the corresponding auth type.
|
2020-07-30 07:53:02 -07:00
|
|
|
func NewDecodeStore(ak AuthUnmarshaler) func(kvA, kvB kv.Pair) string {
|
|
|
|
return func(kvA, kvB kv.Pair) string {
|
2020-04-21 14:33:56 -07:00
|
|
|
switch {
|
|
|
|
case bytes.Equal(kvA.Key[:1], types.AddressStoreKeyPrefix):
|
2020-05-20 12:21:00 -07:00
|
|
|
accA, err := ak.UnmarshalAccount(kvA.Value)
|
2020-04-21 14:33:56 -07:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2020-05-20 12:21:00 -07:00
|
|
|
accB, err := ak.UnmarshalAccount(kvB.Value)
|
2020-04-21 14:33:56 -07:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("%v\n%v", accA, accB)
|
|
|
|
|
|
|
|
case bytes.Equal(kvA.Key, types.GlobalAccountNumberKey):
|
|
|
|
var globalAccNumberA, globalAccNumberB gogotypes.UInt64Value
|
2020-05-20 12:21:00 -07:00
|
|
|
ak.GetCodec().MustUnmarshalBinaryBare(kvA.Value, &globalAccNumberA)
|
|
|
|
ak.GetCodec().MustUnmarshalBinaryBare(kvB.Value, &globalAccNumberB)
|
2020-04-21 14:33:56 -07:00
|
|
|
|
|
|
|
return fmt.Sprintf("GlobalAccNumberA: %d\nGlobalAccNumberB: %d", globalAccNumberA, globalAccNumberB)
|
|
|
|
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("unexpected %s key %X (%s)", types.ModuleName, kvA.Key, kvA.Key))
|
|
|
|
}
|
2019-08-13 15:16:03 -07:00
|
|
|
}
|
|
|
|
}
|