github.com/Finschia/finschia-sdk@v0.48.1/x/evidence/types/evidence.go (about)

     1  package types
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	"github.com/Finschia/ostracon/crypto/tmhash"
     8  	ostbytes "github.com/Finschia/ostracon/libs/bytes"
     9  	abci "github.com/tendermint/tendermint/abci/types"
    10  	"gopkg.in/yaml.v2"
    11  
    12  	sdk "github.com/Finschia/finschia-sdk/types"
    13  	"github.com/Finschia/finschia-sdk/x/evidence/exported"
    14  )
    15  
    16  // Evidence type constants
    17  const (
    18  	RouteEquivocation = "equivocation"
    19  	TypeEquivocation  = "equivocation"
    20  )
    21  
    22  var _ exported.Evidence = &Equivocation{}
    23  
    24  // Route returns the Evidence Handler route for an Equivocation type.
    25  func (e *Equivocation) Route() string { return RouteEquivocation }
    26  
    27  // Type returns the Evidence Handler type for an Equivocation type.
    28  func (e *Equivocation) Type() string { return TypeEquivocation }
    29  
    30  func (e *Equivocation) String() string {
    31  	bz, _ := yaml.Marshal(e)
    32  	return string(bz)
    33  }
    34  
    35  // Hash returns the hash of an Equivocation object.
    36  func (e *Equivocation) Hash() ostbytes.HexBytes {
    37  	bz, err := e.Marshal()
    38  	if err != nil {
    39  		panic(err)
    40  	}
    41  	return tmhash.Sum(bz)
    42  }
    43  
    44  // ValidateBasic performs basic stateless validation checks on an Equivocation object.
    45  func (e *Equivocation) ValidateBasic() error {
    46  	if e.Time.Unix() <= 0 {
    47  		return fmt.Errorf("invalid equivocation time: %s", e.Time)
    48  	}
    49  	if e.Height < 1 {
    50  		return fmt.Errorf("invalid equivocation height: %d", e.Height)
    51  	}
    52  	if e.Power < 1 {
    53  		return fmt.Errorf("invalid equivocation validator power: %d", e.Power)
    54  	}
    55  	if e.ConsensusAddress == "" {
    56  		return fmt.Errorf("invalid equivocation validator consensus address: %s", e.ConsensusAddress)
    57  	}
    58  
    59  	return nil
    60  }
    61  
    62  // GetConsensusAddress returns the validator's consensus address at time of the
    63  // Equivocation infraction.
    64  func (e Equivocation) GetConsensusAddress() sdk.ConsAddress {
    65  	addr, _ := sdk.ConsAddressFromBech32(e.ConsensusAddress)
    66  	return addr
    67  }
    68  
    69  // GetHeight returns the height at time of the Equivocation infraction.
    70  func (e Equivocation) GetHeight() int64 {
    71  	return e.Height
    72  }
    73  
    74  // GetTime returns the time at time of the Equivocation infraction.
    75  func (e Equivocation) GetTime() time.Time {
    76  	return e.Time
    77  }
    78  
    79  // GetValidatorPower returns the validator's power at time of the Equivocation
    80  // infraction.
    81  func (e Equivocation) GetValidatorPower() int64 {
    82  	return e.Power
    83  }
    84  
    85  // GetTotalPower is a no-op for the Equivocation type.
    86  func (e Equivocation) GetTotalPower() int64 { return 0 }
    87  
    88  // FromABCIEvidence converts a Ostracon concrete Evidence type to
    89  // SDK Evidence using Equivocation as the concrete type.
    90  func FromABCIEvidence(e abci.Evidence) exported.Evidence {
    91  	bech32PrefixConsAddr := sdk.GetConfig().GetBech32ConsensusAddrPrefix()
    92  	consAddr, err := sdk.Bech32ifyAddressBytes(bech32PrefixConsAddr, e.Validator.Address)
    93  	if err != nil {
    94  		panic(err)
    95  	}
    96  
    97  	return &Equivocation{
    98  		Height:           e.Height,
    99  		Power:            e.Validator.Power,
   100  		ConsensusAddress: consAddr,
   101  		Time:             e.Time,
   102  	}
   103  }