github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/evidence/internal/types/evidence.go (about)

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