github.com/decred/politeia@v1.4.0/politeiad/cmd/legacypoliteia/commitmentaddrs.go (about) 1 // Copyright (c) 2022 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 "fmt" 9 10 dcrdata "github.com/decred/dcrdata/v6/api/types" 11 ) 12 13 // commitmentAddrs returns the largest commitment address for each of the 14 // provided ticket hashes. Transaction data for the ticket is retrieved from 15 // dcrdata during this process. 16 func (c *convertCmd) commitmentAddrs(tickets []string) (map[string]string, error) { 17 fmt.Printf(" Retrieving commitment addresses from dcrdata...\n") 18 19 // Fetch addresses in batches 20 var ( 21 addrs = make(map[string]string, len(tickets)) // [ticket]address 22 pageSize = 500 23 startIdx int 24 done bool 25 ) 26 for !done { 27 endIdx := startIdx + pageSize 28 if endIdx >= len(tickets) { 29 endIdx = len(tickets) 30 done = true 31 } 32 33 // startIdx is included. endIdx is excluded. 34 ts := tickets[startIdx:endIdx] 35 ttxs, err := c.trimmedTxs(ts) 36 if err != nil { 37 return nil, err 38 } 39 40 // Pull out the largest commitment address for each of the 41 // transactions. 42 for _, ttx := range ttxs { 43 var ( 44 ticket = ttx.TxID 45 addr = largestCommitmentAddr(ttx) 46 ) 47 if addr == "" { 48 return nil, fmt.Errorf("no commitment address found for %v", ticket) 49 } 50 addrs[ticket] = addr 51 } 52 53 startIdx += pageSize 54 printInPlace(fmt.Sprintf(" Retrieved addresses %v/%v", 55 len(addrs), len(tickets))) 56 } 57 fmt.Printf("\n") 58 59 return addrs, nil 60 } 61 62 // largestCommitmentAddr returns the largest commitment address that is found 63 // in the provided tx. 64 func largestCommitmentAddr(tx dcrdata.TrimmedTx) string { 65 // Best is address with largest commit amount 66 var bestAddr string 67 var bestAmount float64 68 for _, v := range tx.Vout { 69 if v.ScriptPubKeyDecoded.CommitAmt == nil { 70 continue 71 } 72 if *v.ScriptPubKeyDecoded.CommitAmt > bestAmount { 73 if len(v.ScriptPubKeyDecoded.Addresses) == 0 { 74 continue 75 } 76 bestAddr = v.ScriptPubKeyDecoded.Addresses[0] 77 bestAmount = *v.ScriptPubKeyDecoded.CommitAmt 78 } 79 } 80 if bestAmount == 0.0 { 81 // This should not happen 82 return "" 83 } 84 return bestAddr 85 }