github.com/mavryk-network/mvgo@v1.19.9/mavryk/voting.go (about)

     1  // Copyright (c) 2020-2021 Blockwatch Data Inc.
     2  // Author: alex@blockwatch.cc
     3  
     4  package mavryk
     5  
     6  import (
     7  	"fmt"
     8  )
     9  
    10  // VotingPeriodKind represents a named voting period in Tezos.
    11  type VotingPeriodKind byte
    12  
    13  const (
    14  	VotingPeriodInvalid VotingPeriodKind = iota
    15  	VotingPeriodProposal
    16  	VotingPeriodExploration
    17  	VotingPeriodCooldown
    18  	VotingPeriodPromotion
    19  	VotingPeriodAdoption
    20  )
    21  
    22  var VotingPeriods = []VotingPeriodKind{
    23  	VotingPeriodProposal,
    24  	VotingPeriodExploration,
    25  	VotingPeriodCooldown,
    26  	VotingPeriodPromotion,
    27  	VotingPeriodAdoption,
    28  }
    29  
    30  const (
    31  	YAY  = "yay"
    32  	NAY  = "nay"
    33  	PASS = "pass"
    34  	ON   = "on"
    35  	OFF  = "off"
    36  )
    37  
    38  func (v VotingPeriodKind) IsValid() bool {
    39  	return v != VotingPeriodInvalid
    40  }
    41  
    42  func (v *VotingPeriodKind) UnmarshalText(data []byte) error {
    43  	vv := ParseVotingPeriod(string(data))
    44  	if !vv.IsValid() {
    45  		return fmt.Errorf("tezos: invalid voting period '%s'", string(data))
    46  	}
    47  	*v = vv
    48  	return nil
    49  }
    50  
    51  func (v VotingPeriodKind) MarshalText() ([]byte, error) {
    52  	return []byte(v.String()), nil
    53  }
    54  
    55  func (v VotingPeriodKind) Num() int {
    56  	switch v {
    57  	case VotingPeriodProposal:
    58  		return 1
    59  	case VotingPeriodExploration:
    60  		return 2
    61  	case VotingPeriodCooldown:
    62  		return 3
    63  	case VotingPeriodPromotion:
    64  		return 4
    65  	case VotingPeriodAdoption:
    66  		return 5
    67  	default:
    68  		return 1
    69  	}
    70  }
    71  
    72  func ToVotingPeriod(i int) VotingPeriodKind {
    73  	switch i {
    74  	case 2:
    75  		return VotingPeriodExploration
    76  	case 3:
    77  		return VotingPeriodCooldown
    78  	case 4:
    79  		return VotingPeriodPromotion
    80  	case 5:
    81  		return VotingPeriodAdoption
    82  	default:
    83  		return VotingPeriodProposal
    84  	}
    85  }
    86  
    87  func ParseVotingPeriod(s string) VotingPeriodKind {
    88  	switch s {
    89  	case "proposal":
    90  		return VotingPeriodProposal
    91  	case "testing_vote", "exploration":
    92  		return VotingPeriodExploration
    93  	case "testing", "cooldown":
    94  		return VotingPeriodCooldown
    95  	case "promotion_vote", "promotion":
    96  		return VotingPeriodPromotion
    97  	case "adoption":
    98  		return VotingPeriodAdoption
    99  	default:
   100  		return VotingPeriodInvalid
   101  	}
   102  }
   103  
   104  func (v VotingPeriodKind) String() string {
   105  	switch v {
   106  	case VotingPeriodProposal:
   107  		return "proposal"
   108  	case VotingPeriodExploration:
   109  		return "exploration"
   110  	case VotingPeriodCooldown:
   111  		return "cooldown"
   112  	case VotingPeriodPromotion:
   113  		return "promotion"
   114  	case VotingPeriodAdoption:
   115  		return "adoption"
   116  	default:
   117  		return ""
   118  	}
   119  }
   120  
   121  // BallotVote represents a named ballot in Tezos.
   122  type BallotVote byte
   123  
   124  const (
   125  	BallotVoteInvalid BallotVote = iota
   126  	BallotVoteYay
   127  	BallotVoteNay
   128  	BallotVotePass
   129  )
   130  
   131  func (v BallotVote) IsValid() bool {
   132  	return v != BallotVoteInvalid
   133  }
   134  
   135  func (v *BallotVote) UnmarshalText(data []byte) error {
   136  	vv := ParseBallotVote(string(data))
   137  	if !vv.IsValid() {
   138  		return fmt.Errorf("tezos: invalid ballot %q", string(data))
   139  	}
   140  	*v = vv
   141  	return nil
   142  }
   143  
   144  func (v BallotVote) MarshalText() ([]byte, error) {
   145  	return []byte(v.String()), nil
   146  }
   147  
   148  func (v *BallotVote) UnmarshalBinary(data []byte) error {
   149  	if len(data) < 1 {
   150  		return fmt.Errorf("tezos: short ballot data")
   151  	}
   152  	vv := ParseBallotTag(data[0])
   153  	if !vv.IsValid() {
   154  		return fmt.Errorf("tezos: invalid ballot tag %d", data[0])
   155  	}
   156  	*v = vv
   157  	return nil
   158  }
   159  
   160  func ParseBallotVote(s string) BallotVote {
   161  	switch s {
   162  	case YAY:
   163  		return BallotVoteYay
   164  	case NAY:
   165  		return BallotVoteNay
   166  	case PASS:
   167  		return BallotVotePass
   168  	default:
   169  		return BallotVoteInvalid
   170  	}
   171  }
   172  
   173  func (v BallotVote) String() string {
   174  	switch v {
   175  	case BallotVoteYay:
   176  		return YAY
   177  	case BallotVoteNay:
   178  		return NAY
   179  	case BallotVotePass:
   180  		return PASS
   181  	default:
   182  		return ""
   183  	}
   184  }
   185  
   186  func (v BallotVote) Tag() byte {
   187  	switch v {
   188  	case BallotVoteYay:
   189  		return 0
   190  	case BallotVoteNay:
   191  		return 1
   192  	case BallotVotePass:
   193  		return 2
   194  	default:
   195  		return 255
   196  	}
   197  }
   198  
   199  func ParseBallotTag(t byte) BallotVote {
   200  	switch t {
   201  	case 0:
   202  		return BallotVoteYay
   203  	case 1:
   204  		return BallotVoteNay
   205  	case 2:
   206  		return BallotVotePass
   207  	default:
   208  		return BallotVoteInvalid
   209  	}
   210  }
   211  
   212  // FeatureVote represents liquidity baking votes in Tezos block headers.
   213  type FeatureVote byte
   214  
   215  const (
   216  	FeatureVoteInvalid FeatureVote = iota
   217  	FeatureVoteOn
   218  	FeatureVoteOff
   219  	FeatureVotePass
   220  )
   221  
   222  func (v FeatureVote) IsValid() bool {
   223  	return v != FeatureVoteInvalid
   224  }
   225  
   226  func (v FeatureVote) String() string {
   227  	switch v {
   228  	case FeatureVoteOn:
   229  		return ON
   230  	case FeatureVoteOff:
   231  		return OFF
   232  	case FeatureVotePass:
   233  		return PASS
   234  	default:
   235  		return ""
   236  	}
   237  }
   238  
   239  func (v FeatureVote) Tag() byte {
   240  	switch v {
   241  	case FeatureVoteOn:
   242  		return 0
   243  	case FeatureVoteOff:
   244  		return 1
   245  	case FeatureVotePass:
   246  		return 2
   247  	default:
   248  		return 255
   249  	}
   250  }
   251  
   252  func ParseFeatureVoteTag(t byte) FeatureVote {
   253  	switch t {
   254  	case 0:
   255  		return FeatureVoteOn
   256  	case 1:
   257  		return FeatureVoteOff
   258  	case 2:
   259  		return FeatureVotePass
   260  	default:
   261  		return FeatureVoteInvalid
   262  	}
   263  }
   264  func (v *FeatureVote) UnmarshalJSON(data []byte) error {
   265  	if len(data) == 0 {
   266  		return nil
   267  	}
   268  	var vv FeatureVote
   269  	if data[0] == '"' {
   270  		val := string(data[1 : len(data)-1])
   271  		switch val {
   272  		case ON:
   273  			vv = FeatureVoteOn
   274  		case OFF:
   275  			vv = FeatureVoteOff
   276  		case PASS:
   277  			vv = FeatureVotePass
   278  		}
   279  	}
   280  	if !vv.IsValid() {
   281  		return fmt.Errorf("tezos: invalid lb vote %q", string(data))
   282  	}
   283  	*v = vv
   284  	return nil
   285  }
   286  
   287  func (v FeatureVote) MarshalText() ([]byte, error) {
   288  	return []byte(v.String()), nil
   289  }
   290  
   291  func (v *FeatureVote) UnmarshalBinary(data []byte) error {
   292  	if len(data) < 1 {
   293  		return fmt.Errorf("tezos: short lb vote data")
   294  	}
   295  	vv := ParseFeatureVoteTag(data[0])
   296  	if !vv.IsValid() {
   297  		return fmt.Errorf("tezos: invalid lb vote tag %d", data[0])
   298  	}
   299  	*v = vv
   300  	return nil
   301  }