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

     1  package simulation
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/binary"
     6  	"fmt"
     7  
     8  	"github.com/Finschia/finschia-sdk/codec"
     9  	"github.com/Finschia/finschia-sdk/types/kv"
    10  	"github.com/Finschia/finschia-sdk/x/gov/types"
    11  )
    12  
    13  // NewDecodeStore returns a decoder function closure that unmarshals the KVPair's
    14  // Value to the corresponding gov 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.ProposalsKeyPrefix):
    19  			var proposalA types.Proposal
    20  			err := cdc.Unmarshal(kvA.Value, &proposalA)
    21  			if err != nil {
    22  				panic(err)
    23  			}
    24  			var proposalB types.Proposal
    25  			err = cdc.Unmarshal(kvB.Value, &proposalB)
    26  			if err != nil {
    27  				panic(err)
    28  			}
    29  			return fmt.Sprintf("%v\n%v", proposalA, proposalB)
    30  
    31  		case bytes.Equal(kvA.Key[:1], types.ActiveProposalQueuePrefix),
    32  			bytes.Equal(kvA.Key[:1], types.InactiveProposalQueuePrefix),
    33  			bytes.Equal(kvA.Key[:1], types.ProposalIDKey):
    34  			proposalIDA := binary.LittleEndian.Uint64(kvA.Value)
    35  			proposalIDB := binary.LittleEndian.Uint64(kvB.Value)
    36  			return fmt.Sprintf("proposalIDA: %d\nProposalIDB: %d", proposalIDA, proposalIDB)
    37  
    38  		case bytes.Equal(kvA.Key[:1], types.DepositsKeyPrefix):
    39  			var depositA, depositB types.Deposit
    40  			cdc.MustUnmarshal(kvA.Value, &depositA)
    41  			cdc.MustUnmarshal(kvB.Value, &depositB)
    42  			return fmt.Sprintf("%v\n%v", depositA, depositB)
    43  
    44  		case bytes.Equal(kvA.Key[:1], types.VotesKeyPrefix):
    45  			var voteA, voteB types.Vote
    46  			cdc.MustUnmarshal(kvA.Value, &voteA)
    47  			cdc.MustUnmarshal(kvB.Value, &voteB)
    48  			return fmt.Sprintf("%v\n%v", voteA, voteB)
    49  
    50  		default:
    51  			panic(fmt.Sprintf("invalid governance key prefix %X", kvA.Key[:1]))
    52  		}
    53  	}
    54  }