github.com/Finschia/finschia-sdk@v0.48.1/x/staking/simulation/decoder.go (about)

     1  package simulation
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  
     7  	"github.com/Finschia/finschia-sdk/codec"
     8  	sdk "github.com/Finschia/finschia-sdk/types"
     9  	"github.com/Finschia/finschia-sdk/types/kv"
    10  	"github.com/Finschia/finschia-sdk/x/staking/types"
    11  )
    12  
    13  // NewDecodeStore returns a decoder function closure that unmarshals the KVPair's
    14  // Value to the corresponding staking type.
    15  func NewDecodeStore(cdc codec.Codec) func(kvA, kvB kv.Pair) string {
    16  	return func(kvA, kvB kv.Pair) string {
    17  		switch {
    18  		case bytes.Equal(kvA.Key[:1], types.LastTotalPowerKey):
    19  			var powerA, powerB sdk.IntProto
    20  
    21  			cdc.MustUnmarshal(kvA.Value, &powerA)
    22  			cdc.MustUnmarshal(kvB.Value, &powerB)
    23  
    24  			return fmt.Sprintf("%v\n%v", powerA, powerB)
    25  		case bytes.Equal(kvA.Key[:1], types.ValidatorsKey):
    26  			var validatorA, validatorB types.Validator
    27  
    28  			cdc.MustUnmarshal(kvA.Value, &validatorA)
    29  			cdc.MustUnmarshal(kvB.Value, &validatorB)
    30  
    31  			return fmt.Sprintf("%v\n%v", validatorA, validatorB)
    32  		case bytes.Equal(kvA.Key[:1], types.LastValidatorPowerKey),
    33  			bytes.Equal(kvA.Key[:1], types.ValidatorsByConsAddrKey),
    34  			bytes.Equal(kvA.Key[:1], types.ValidatorsByPowerIndexKey):
    35  			return fmt.Sprintf("%v\n%v", sdk.ValAddress(kvA.Value), sdk.ValAddress(kvB.Value))
    36  
    37  		case bytes.Equal(kvA.Key[:1], types.DelegationKey):
    38  			var delegationA, delegationB types.Delegation
    39  
    40  			cdc.MustUnmarshal(kvA.Value, &delegationA)
    41  			cdc.MustUnmarshal(kvB.Value, &delegationB)
    42  
    43  			return fmt.Sprintf("%v\n%v", delegationA, delegationB)
    44  		case bytes.Equal(kvA.Key[:1], types.UnbondingDelegationKey),
    45  			bytes.Equal(kvA.Key[:1], types.UnbondingDelegationByValIndexKey):
    46  			var ubdA, ubdB types.UnbondingDelegation
    47  
    48  			cdc.MustUnmarshal(kvA.Value, &ubdA)
    49  			cdc.MustUnmarshal(kvB.Value, &ubdB)
    50  
    51  			return fmt.Sprintf("%v\n%v", ubdA, ubdB)
    52  		case bytes.Equal(kvA.Key[:1], types.RedelegationKey),
    53  			bytes.Equal(kvA.Key[:1], types.RedelegationByValSrcIndexKey):
    54  			var redA, redB types.Redelegation
    55  
    56  			cdc.MustUnmarshal(kvA.Value, &redA)
    57  			cdc.MustUnmarshal(kvB.Value, &redB)
    58  
    59  			return fmt.Sprintf("%v\n%v", redA, redB)
    60  		default:
    61  			panic(fmt.Sprintf("invalid staking key prefix %X", kvA.Key[:1]))
    62  		}
    63  	}
    64  }