github.com/prysmaticlabs/prysm@v1.4.4/slasher/detection/attestations/types/types.go (about)

     1  // Package types includes important type definitions for
     2  // slashable objects detected by slasher.
     3  package types
     4  
     5  import (
     6  	"errors"
     7  
     8  	types "github.com/prysmaticlabs/eth2-types"
     9  	"github.com/prysmaticlabs/prysm/shared/bytesutil"
    10  )
    11  
    12  // DetectionKind defines an enum type that
    13  // gives us information on the type of slashable offense
    14  // found when analyzing validator min-max spans.
    15  type DetectionKind uint8
    16  
    17  const (
    18  	// DoubleVote denotes a slashable offense in which
    19  	// a validator cast two conflicting attestations within
    20  	// the same target epoch.
    21  	DoubleVote DetectionKind = iota
    22  	// SurroundVote denotes a slashable offense in which
    23  	// a validator surrounded or was surrounded by a previous
    24  	// attestation created by the same validator.
    25  	SurroundVote
    26  )
    27  
    28  // DetectionResult tells us the kind of slashable
    29  // offense found from detecting on min-max spans +
    30  // the slashable epoch for the offense.
    31  // Also includes the signature bytes for assistance in
    32  // finding the attestation for the slashing proof.
    33  type DetectionResult struct {
    34  	ValidatorIndex uint64
    35  	SlashableEpoch types.Epoch
    36  	Kind           DetectionKind
    37  	SigBytes       [2]byte
    38  }
    39  
    40  // Marshal the result into bytes, used for removing duplicates.
    41  func (r *DetectionResult) Marshal() []byte {
    42  	numBytes := bytesutil.ToBytes(uint64(r.SlashableEpoch), 8)
    43  	var resultBytes []byte
    44  	resultBytes = append(resultBytes, uint8(r.Kind))
    45  	resultBytes = append(resultBytes, r.SigBytes[:]...)
    46  	resultBytes = append(resultBytes, numBytes...)
    47  	return resultBytes
    48  }
    49  
    50  // Span defines the structure used for detecting surround and double votes.
    51  type Span struct {
    52  	MinSpan     uint16
    53  	MaxSpan     uint16
    54  	SigBytes    [2]byte
    55  	HasAttested bool
    56  }
    57  
    58  // SpannerEncodedLength the byte length of validator span data structure.
    59  var SpannerEncodedLength = uint64(7)
    60  
    61  // UnmarshalSpan returns a span from an encoded, flattened byte array.
    62  // Note: This is a very often used function, so it is as optimized as possible.
    63  func UnmarshalSpan(enc []byte) (Span, error) {
    64  	r := Span{}
    65  	if len(enc) != int(SpannerEncodedLength) {
    66  		return r, errors.New("wrong data length for min max span")
    67  	}
    68  	r.MinSpan = uint16(enc[0]) | uint16(enc[1])<<8
    69  	r.MaxSpan = uint16(enc[2]) | uint16(enc[3])<<8
    70  	sigB := [2]byte{}
    71  	copy(sigB[:], enc[4:6])
    72  	r.SigBytes = sigB
    73  	r.HasAttested = enc[6]&1 == 1
    74  	return r, nil
    75  }
    76  
    77  // Marshal converts the span struct into a flattened byte array.
    78  // Note: This is a very often used function, so it is as optimized as possible.
    79  func (s Span) Marshal() []byte {
    80  	var attested byte = 0
    81  	if s.HasAttested {
    82  		attested = 1
    83  	}
    84  	return []byte{
    85  		byte(s.MinSpan),
    86  		byte(s.MinSpan >> 8),
    87  		byte(s.MaxSpan),
    88  		byte(s.MaxSpan >> 8),
    89  		s.SigBytes[0],
    90  		s.SigBytes[1],
    91  		attested,
    92  	}
    93  }