github.com/decred/politeia@v1.4.0/politeiawww/cmd/cmswww/startvote.go (about)

     1  // Copyright (c) 2017-2019 The Decred developers
     2  // Use of this source code is governed by an ISC
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"encoding/hex"
     9  	"encoding/json"
    10  
    11  	v1 "github.com/decred/politeia/politeiawww/api/cms/v1"
    12  	"github.com/decred/politeia/politeiawww/cmd/shared"
    13  	"github.com/decred/politeia/util"
    14  )
    15  
    16  // StartVoteCmd starts the voting period on the specified proposal.
    17  type StartVoteCmd struct {
    18  	Args struct {
    19  		Token            string `positional-arg-name:"token" required:"true"`
    20  		Duration         uint32 `positional-arg-name:"duration"`
    21  		QuorumPercentage uint32 `positional-arg-name:"quorumpercentage"`
    22  		PassPercentage   uint32 `positional-arg-name:"passpercentage"`
    23  	} `positional-args:"true"`
    24  }
    25  
    26  // Execute executes the start vote command.
    27  func (cmd *StartVoteCmd) Execute(args []string) error {
    28  	// Check for user identity
    29  	if cfg.Identity == nil {
    30  		return shared.ErrUserIdentityNotFound
    31  	}
    32  
    33  	// Setup vote params
    34  	var (
    35  		// Default values
    36  		duration uint32 = 2016
    37  		quorum   uint32 = 20
    38  		pass     uint32 = 60
    39  	)
    40  	if cmd.Args.Duration != 0 {
    41  		duration = cmd.Args.Duration
    42  	}
    43  	if cmd.Args.QuorumPercentage != 0 {
    44  		quorum = cmd.Args.QuorumPercentage
    45  	}
    46  	if cmd.Args.PassPercentage != 0 {
    47  		pass = cmd.Args.PassPercentage
    48  	}
    49  
    50  	// Create StartVote
    51  	vote := v1.Vote{
    52  		Token:            cmd.Args.Token,
    53  		Mask:             0x03, // bit 0 no, bit 1 yes
    54  		Duration:         duration,
    55  		QuorumPercentage: quorum,
    56  		PassPercentage:   pass,
    57  		Options: []v1.VoteOption{
    58  			{
    59  				Id:          "no",
    60  				Description: "Don't approve proposal",
    61  				Bits:        0x01,
    62  			},
    63  			{
    64  				Id:          "yes",
    65  				Description: "Approve proposal",
    66  				Bits:        0x02,
    67  			},
    68  		},
    69  	}
    70  	vb, err := json.Marshal(vote)
    71  	if err != nil {
    72  		return err
    73  	}
    74  	msg := hex.EncodeToString(util.Digest(vb))
    75  	sig := cfg.Identity.SignMessage([]byte(msg))
    76  	sv := v1.StartVote{
    77  		Vote:      vote,
    78  		PublicKey: hex.EncodeToString(cfg.Identity.Public.Key[:]),
    79  		Signature: hex.EncodeToString(sig[:]),
    80  	}
    81  
    82  	// Print request details
    83  	err = shared.PrintJSON(sv)
    84  	if err != nil {
    85  		return err
    86  	}
    87  
    88  	// Send request
    89  	svr, err := client.StartVoteDCC(sv)
    90  	if err != nil {
    91  		return err
    92  	}
    93  
    94  	// Remove ticket snapshot from the response so that the output
    95  	// is legible
    96  	svr.UserWeights = []string{"removed by piwww for readability"}
    97  
    98  	// Print response details
    99  	return shared.PrintJSON(svr)
   100  }
   101  
   102  // startVoteHelpMsg is the output of the help command when 'startvote' is
   103  // specified.
   104  var startVoteHelpMsg = `startvote <token> <duration> <quorumpercentage> <passpercentage>
   105  
   106  Start voting period for a dcc. Requires admin privileges.  The optional
   107  arguments must either all be used or none be used.
   108  
   109  Arguments:
   110  1. token              (string, required)  Proposal censorship token
   111  2. duration           (uint32, optional)  Duration of vote in blocks (default: 2016)
   112  3. quorumpercentage   (uint32, optional)  Percent of votes required for quorum (default: 10)
   113  4. passpercentage     (uint32, optional)  Percent of votes required to pass (default: 60)
   114  
   115  Result:
   116  
   117  {
   118    "startblockheight"     (string)    Block height at start of vote
   119    "startblockhash"       (string)    Hash of first block of vote interval
   120    "endheight"            (string)    Height of vote end
   121    "userweights"          ([]string)  Valid voting users and their weights   
   122  }`