-
Notifications
You must be signed in to change notification settings - Fork 5
/
votetime.go
164 lines (140 loc) · 5.18 KB
/
votetime.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright (c) 2017, Jonathan Chappelow
// Under the ISC license. See LICENSE.
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"time"
"github.com/decred/dcrd/chaincfg"
"github.com/decred/dcrd/chaincfg/chainhash"
"github.com/decred/dcrd/rpcclient"
)
var host = flag.String("host", "127.0.0.1:9110", "wallet RPC host:port")
var user = flag.String("user", "dcrwallet", "wallet RPC username")
var pass = flag.String("pass", "bananas", "wallet RPC password")
var cert = flag.String("cert", "dcrwallet.cert", "wallet RPC TLS certificate (when notls=false)")
var notls = flag.Bool("notls", false, "Disable use of TLS for wallet connection")
var (
activeChainParams = &chaincfg.MainNetParams
)
func main() {
// Parse command line flags
flag.Parse()
// Connect to wallet RPC server
wcl, err := ConnectRPC(*host, *user, *pass, *cert, *notls)
if err != nil {
log.Fatalf("Unable to connect to RPC server: %v", err)
}
walletInfo, err := wcl.WalletInfo()
if err != nil {
log.Fatalf("WalletInfo failed: %v", err)
}
log.Println("Wallet connected to node? ", walletInfo.DaemonConnected)
log.Println("Listing all transaction inputs and outputs...")
allTxnIOs, err := wcl.ListTransactionsCountFrom("*", 9999999, 0)
if err != nil {
log.Fatalf("ListTransactions failed: %v", err)
}
// Transactions may occur multiple times in the list, so gather the unique
// transaction hashes with a map. We could just take the result that is
// vout[0] of an stakegen, but this easy too.
knownVotes := make(map[string]bool)
for _, tx := range allTxnIOs {
if *tx.TxType == "vote" {
knownVotes[tx.TxID] = true
}
}
log.Println("Number of votes: ", len(knownVotes))
waitSeconds := make([]float64, 0, len(knownVotes))
waitBlocks := make([]int64, 0, len(knownVotes))
for txid := range knownVotes {
// Get ticket address from previous outpoint of Vin[1] of SSGen
voteHash, err := chainhash.NewHashFromStr(txid)
if err != nil {
log.Printf("Invalid tx hash %s: %v", txid, err)
continue
}
txRaw, err := wcl.GetRawTransaction(voteHash)
if err != nil {
log.Printf("GetRawTransaction(vote) failed: %v", err)
continue
}
// Vin[1] spends the stakesubmission of the ticket purchase
prevout := txRaw.MsgTx().TxIn[1].PreviousOutPoint
ticketHash := &prevout.Hash
ticketTxOutIndex := prevout.Index
// Get block height and time for the vote
txRawVerbose, err := wcl.GetRawTransactionVerbose(voteHash)
if err != nil {
log.Fatalf("GetRawTransactionVerbose(vote) failed: %v", err)
}
voteHeight := txRawVerbose.BlockHeight
voteTime := time.Unix(txRawVerbose.Blocktime, 0)
// Get block height and time for the ticket
prevTxRaw, err := wcl.GetRawTransactionVerbose(ticketHash)
if err != nil {
log.Fatalf("GetRawTransactionVerbose(ticket) failed: %v", err)
}
// Tickets mature 256 blocks after purchase
ticketPurchaseHeight := prevTxRaw.BlockHeight
//ticketTime := time.Unix(prevTxRaw.Blocktime, 0)
ticketMaturityHeight := ticketPurchaseHeight + int64(activeChainParams.TicketMaturity)
// Get time of block at this height
ticketMaturityBlockHash, _ := wcl.GetBlockHash(ticketMaturityHeight)
ticketMaturityBlock, _ := wcl.GetBlockHeaderVerbose(ticketMaturityBlockHash)
ticketMaturityTime := time.Unix(ticketMaturityBlock.Time, 0)
// Compute time from maturity to vote
voteWaitBlocks := voteHeight - ticketMaturityHeight
voteWaitSeconds := voteTime.Sub(ticketMaturityTime)
voteWaitDays := voteWaitSeconds.Hours() / 24.0
ticketPrice := prevTxRaw.Vout[ticketTxOutIndex].Value
log.Printf("Ticket %s... (%f DCR) mined in block %d, voted %d blocks (%.2f days) after maturity.",
prevTxRaw.Txid[:8], ticketPrice, ticketPurchaseHeight, voteWaitBlocks, voteWaitDays)
waitBlocks = append(waitBlocks, voteWaitBlocks)
waitSeconds = append(waitSeconds, voteWaitSeconds.Seconds())
}
// Compute mean wait time in blocks and seconds
var avgBlockWait, avgSecondWait float64
for iv := range waitBlocks {
avgBlockWait += float64(waitBlocks[iv])
avgSecondWait += waitSeconds[iv]
}
avgBlockWait /= float64(len(waitBlocks))
avgSecondWait /= float64(len(waitBlocks))
log.Printf("Mean wait for %d votes: %.1f blocks, %.2f days.", len(knownVotes),
avgBlockWait, avgSecondWait/86400.0)
}
// ConnectRPC attempts to create a new websocket connection to a legacy RPC
// server with the given credentials.
func ConnectRPC(host, user, pass, cert string, disableTLS bool) (*rpcclient.Client, error) {
var rpcCerts []byte
var err error
if !disableTLS {
rpcCerts, err = ioutil.ReadFile(cert)
if err != nil {
return nil, fmt.Errorf("Failed to read RPC cert file at %s: %s\n",
cert, err.Error())
}
log.Printf("Attempting to connect to RPC server %s as user %s "+
"using certificate located in %s",
host, user, cert)
} else {
log.Printf("Attempting to connect to RPC server %s as user %s (no TLS)",
host, user)
}
connCfgDaemon := &rpcclient.ConnConfig{
Host: host,
Endpoint: "ws", // websocket
User: user,
Pass: pass,
Certificates: rpcCerts,
DisableTLS: disableTLS,
}
rpcClient, err := rpcclient.New(connCfgDaemon, nil)
if err != nil {
return nil, fmt.Errorf("Failed to start dcrwallet RPC client: %s", err.Error())
}
return rpcClient, nil
}