Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// Requires txindex=1 option set in your zcash.conf
package main
import (
"encoding/json"
"fmt"
"log"
"os/exec"
"strconv"
"time"
)
//limited Block struct for JSON unmarshaling
type Block struct {
Tx []TX
Time int64
}
type TX struct {
Txid string
Vout []TXOutput
}
type TXOutput struct {
N int
ScriptPubKey SKP
}
type SKP struct {
Asm string
Type string
}
func main() {
//Start height is 638745, end height is 647340, poll lasted 8595 blocks
//Start time is 2019-11-16 00:01:31 +0000 UTC, end time is 2019-11-30 23:57:28 +0000 UTC
getPolls()
//getBTCtest()
return
}
//Test function using Bitcoin to demonstrate nulldata logic
func getBTCtest() {
var currentBlock Block
//block hash is first segwit block, must contain coinbase OP_RETURN
out, err := exec.Command("bitcoin-cli", "getblock", "0000000000000000001c8018d9cb3b742ef25114f27563e3fc4a1902167f9893", "2").Output()
if err != nil {
log.Fatal(err)
}
err = json.Unmarshal(out, &currentBlock)
if err != nil {
log.Fatal(err)
}
//check all the outputs in the coinbase
for _, vout := range currentBlock.Tx[0].Vout {
if vout.ScriptPubKey.Type == "nulldata" {
fmt.Printf("Coinbase TX %v contained %v\n", currentBlock.Tx[0].Txid, vout.ScriptPubKey.Asm)
}
}
}
//finds OP_RETURNs from coinbase transactions in polling period...no votes recorded
func getPolls() {
for height := 638745; height <= 647340; height++ {
var currentBlock Block
out, err := exec.Command("zcash-cli", "getblock", strconv.Itoa(height), "2").Output()
if err != nil {
log.Fatal(err)
}
err = json.Unmarshal(out, &currentBlock)
if err != nil {
log.Fatal(err)
}
//check all the outputs in the coinbase
for _, vout := range currentBlock.Tx[0].Vout {
if vout.ScriptPubKey.Type == "nulldata" {
fmt.Printf("Coinbase TX %v contained %v\n", currentBlock.Tx[0].Txid, vout.ScriptPubKey.Asm)
}
}
}
fmt.Println("Scan complete from blocks 638745 to 647340")
return
}
//calculates blockheights for use in the polling period
func calcHeights() (startHeight int, endHeight int, startTime time.Time, endTime time.Time) {
begin := time.Date(2019, time.November, 15, 23, 59, 59, 59, time.UTC)
end := time.Date(2019, time.December, 1, 0, 0, 0, 0, time.UTC)
var lastTime time.Time
for height := 638700; endHeight == 0; height++ {
var currentBlock Block
out, err := exec.Command("zcash-cli", "getblock", strconv.Itoa(height), "2").Output()
if err != nil {
log.Fatal(err)
}
err = json.Unmarshal(out, &currentBlock)
if err != nil {
log.Fatal(err)
}
currentTime := time.Unix(currentBlock.Time, 0).UTC()
if startHeight == 0 && currentTime.After(begin) {
startHeight = height
startTime = currentTime
}
if currentTime.After(end) {
endHeight = height - 1
endTime = lastTime
return
}
lastTime = currentTime
}
fmt.Printf("Start height is %v, end height is %v\n", startHeight, endHeight)
fmt.Printf("Start time is %v, end time is %v\n", startTime, endTime)
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.