github.com/protolambda/zssz@v0.1.5/types/ssz_bool.go (about) 1 package types 2 3 import ( 4 "fmt" 5 . "github.com/protolambda/zssz/dec" 6 . "github.com/protolambda/zssz/enc" 7 . "github.com/protolambda/zssz/htr" 8 . "github.com/protolambda/zssz/pretty" 9 "unsafe" 10 ) 11 12 type SSZBool struct{} 13 14 func (v SSZBool) FuzzMinLen() uint64 { 15 return 1 16 } 17 18 func (v SSZBool) FuzzMaxLen() uint64 { 19 return 1 20 } 21 22 func (v SSZBool) MinLen() uint64 { 23 return 1 24 } 25 26 func (v SSZBool) MaxLen() uint64 { 27 return 1 28 } 29 30 func (v SSZBool) FixedLen() uint64 { 31 return 1 32 } 33 34 func (v SSZBool) IsFixed() bool { 35 return true 36 } 37 38 func (v SSZBool) SizeOf(p unsafe.Pointer) uint64 { 39 return 1 40 } 41 42 func (v SSZBool) Encode(eb *EncodingWriter, p unsafe.Pointer) error { 43 return eb.WriteByte(*(*byte)(p)) 44 } 45 46 func (v SSZBool) Decode(dr *DecodingReader, p unsafe.Pointer) error { 47 b, err := dr.ReadByte() 48 if err != nil { 49 return err 50 } 51 if b == 0x00 { 52 *(*bool)(p) = false 53 return nil 54 } else if b == 0x01 { 55 *(*bool)(p) = true 56 return nil 57 } else { 58 if dr.IsFuzzMode() { 59 // just make a valid random bool 60 *(*bool)(p) = b&1 != 0 61 return nil 62 } else { 63 return fmt.Errorf("bool value is invalid") 64 } 65 } 66 } 67 68 func (v SSZBool) DryCheck(dr *DecodingReader) error { 69 b, err := dr.ReadByte() 70 if err != nil { 71 return err 72 } 73 if b > 1 { 74 return fmt.Errorf("bool value is invalid") 75 } 76 return nil 77 } 78 79 func (v SSZBool) HashTreeRoot(h MerkleFn, p unsafe.Pointer) (out [32]byte) { 80 out[0] = *(*byte)(p) 81 return 82 } 83 84 func (v SSZBool) Pretty(indent uint32, w *PrettyWriter, p unsafe.Pointer) { 85 w.WriteIndent(indent) 86 if *(*bool)(p) { 87 w.Write("True") 88 } else { 89 w.Write("False") 90 } 91 }