Run gofmt on server/main.go and common/darkside.go

This commit is contained in:
Taylor Hornby 2020-03-09 20:30:12 -06:00 committed by Larry Ruane
parent d55d9b1c64
commit 673a9635fc
2 changed files with 140 additions and 140 deletions

View File

@ -136,11 +136,11 @@ func main() {
} }
logger.SetLevel(logrus.Level(opts.logLevel)) logger.SetLevel(logrus.Level(opts.logLevel))
filesThatShouldExist := []string{ } filesThatShouldExist := []string{}
if !opts.darkSide { if !opts.darkSide {
filesThatShouldExist = append(filesThatShouldExist, opts.zcashConfPath) filesThatShouldExist = append(filesThatShouldExist, opts.zcashConfPath)
} }
if opts.tlsCertPath != "" { if opts.tlsCertPath != "" {
filesThatShouldExist = append(filesThatShouldExist, opts.tlsCertPath) filesThatShouldExist = append(filesThatShouldExist, opts.tlsCertPath)
} }
@ -188,23 +188,23 @@ func main() {
reflection.Register(server) reflection.Register(server)
} }
if opts.darkSide { if opts.darkSide {
common.RawRequest = common.DarkSideRawRequest common.RawRequest = common.DarkSideRawRequest
} else { } else {
// Initialize Zcash RPC client. Right now (Jan 2018) this is only for // Initialize Zcash RPC client. Right now (Jan 2018) this is only for
// sending transactions, but in the future it could back a different type // sending transactions, but in the future it could back a different type
// of block streamer. // of block streamer.
rpcClient, err := frontend.NewZRPCFromConf(opts.zcashConfPath) rpcClient, err := frontend.NewZRPCFromConf(opts.zcashConfPath)
if err != nil { if err != nil {
common.Log.WithFields(logrus.Fields{ common.Log.WithFields(logrus.Fields{
"error": err, "error": err,
}).Fatal("setting up RPC connection to zcashd") }).Fatal("setting up RPC connection to zcashd")
} }
// indirect function for test mocking (so unit tests can talk to stub functions) // indirect function for test mocking (so unit tests can talk to stub functions)
common.RawRequest = rpcClient.RawRequest common.RawRequest = rpcClient.RawRequest
} }
// Get the sapling activation height from the RPC // Get the sapling activation height from the RPC
// (this first RPC also verifies that we can communicate with zcashd) // (this first RPC also verifies that we can communicate with zcashd)

View File

@ -1,157 +1,157 @@
package common package common
import ( import (
"errors" "bufio"
"encoding/json" "encoding/hex"
"os" "encoding/json"
"bufio" "errors"
"strconv" "os"
"time" "strconv"
"encoding/hex" "time"
) )
type DarksideZcashdState struct { type DarksideZcashdState struct {
start_height int start_height int
sapling_activation int sapling_activation int
branch_id string branch_id string
chain_name string chain_name string
// Should always be nonempty. Index 0 is the block at height start_height. // Should always be nonempty. Index 0 is the block at height start_height.
blocks []string blocks []string
incoming_transactions [][]byte incoming_transactions [][]byte
server_start time.Time server_start time.Time
} }
var state *DarksideZcashdState = nil var state *DarksideZcashdState = nil
func DarkSideRawRequest(method string, params []json.RawMessage) (json.RawMessage, error) { func DarkSideRawRequest(method string, params []json.RawMessage) (json.RawMessage, error) {
if state == nil { if state == nil {
state = &DarksideZcashdState{ state = &DarksideZcashdState{
start_height: 1000, start_height: 1000,
sapling_activation: 1000, sapling_activation: 1000,
branch_id: "2bb40e60", branch_id: "2bb40e60",
chain_name: "main", chain_name: "main",
blocks: make([]string, 0), blocks: make([]string, 0),
incoming_transactions: make([][]byte, 0), incoming_transactions: make([][]byte, 0),
server_start: time.Now(), server_start: time.Now(),
} }
testBlocks, err := os.Open("./testdata/default-darkside-blocks") testBlocks, err := os.Open("./testdata/default-darkside-blocks")
if err != nil { if err != nil {
Log.Fatal("Error loading default darksidewalletd blocks") Log.Fatal("Error loading default darksidewalletd blocks")
} }
scan := bufio.NewScanner(testBlocks) scan := bufio.NewScanner(testBlocks)
for scan.Scan() { // each line (block) for scan.Scan() { // each line (block)
block := scan.Bytes() block := scan.Bytes()
state.blocks = append(state.blocks, string(block)) state.blocks = append(state.blocks, string(block))
} }
} }
if time.Now().Sub(state.server_start).Minutes() >= 30 { if time.Now().Sub(state.server_start).Minutes() >= 30 {
Log.Fatal("Shutting down darksidewalletd to prevent accidental deployment in production.") Log.Fatal("Shutting down darksidewalletd to prevent accidental deployment in production.")
} }
switch method { switch method {
case "getblockchaininfo": case "getblockchaininfo":
// TODO: there has got to be a better way to construct this! // TODO: there has got to be a better way to construct this!
data := make(map[string]interface{}) data := make(map[string]interface{})
data["chain"] = state.chain_name data["chain"] = state.chain_name
data["upgrades"] = make(map[string]interface{}) data["upgrades"] = make(map[string]interface{})
data["upgrades"].(map[string]interface{})["76b809bb"] = make(map[string]interface{}) data["upgrades"].(map[string]interface{})["76b809bb"] = make(map[string]interface{})
data["upgrades"].(map[string]interface{})["76b809bb"].(map[string]interface{})["activationheight"] = state.sapling_activation data["upgrades"].(map[string]interface{})["76b809bb"].(map[string]interface{})["activationheight"] = state.sapling_activation
data["headers"] = state.start_height + len(state.blocks) - 1 data["headers"] = state.start_height + len(state.blocks) - 1
data["consensus"] = make(map[string]interface{}) data["consensus"] = make(map[string]interface{})
data["consensus"].(map[string]interface{})["nextblock"] = state.branch_id data["consensus"].(map[string]interface{})["nextblock"] = state.branch_id
return json.Marshal(data) return json.Marshal(data)
case "getblock": case "getblock":
var height string var height string
err := json.Unmarshal(params[0], &height) err := json.Unmarshal(params[0], &height)
if err != nil { if err != nil {
return nil, errors.New("Failed to parse getblock request.") return nil, errors.New("Failed to parse getblock request.")
} }
height_i, err := strconv.Atoi(height) height_i, err := strconv.Atoi(height)
if err != nil { if err != nil {
return nil, errors.New("Error parsing height as integer.") return nil, errors.New("Error parsing height as integer.")
} }
index := height_i - state.start_height index := height_i - state.start_height
if index == len(state.blocks) { if index == len(state.blocks) {
// The current ingestor keeps going until it sees this error, // The current ingestor keeps going until it sees this error,
// meaning it's up to the latest height. // meaning it's up to the latest height.
return nil, errors.New("-8:") return nil, errors.New("-8:")
} }
if index < 0 || index > len(state.blocks) { if index < 0 || index > len(state.blocks) {
// If an integration test can reach this, it could be a bug, so generate an error. // If an integration test can reach this, it could be a bug, so generate an error.
Log.Errorf("getblock request made for out-of-range height %d (have %d to %d)", height_i, state.start_height, state.start_height + len(state.blocks) - 1) Log.Errorf("getblock request made for out-of-range height %d (have %d to %d)", height_i, state.start_height, state.start_height+len(state.blocks)-1)
return nil, errors.New("-8:") return nil, errors.New("-8:")
} }
return []byte("\"" + state.blocks[index] + "\""), nil return []byte("\"" + state.blocks[index] + "\""), nil
case "getaddresstxids": case "getaddresstxids":
// Not required for minimal reorg testing. // Not required for minimal reorg testing.
return nil, errors.New("Not implemented yet.") return nil, errors.New("Not implemented yet.")
case "getrawtransaction": case "getrawtransaction":
// Not required for minimal reorg testing. // Not required for minimal reorg testing.
return nil, errors.New("Not implemented yet.") return nil, errors.New("Not implemented yet.")
case "sendrawtransaction": case "sendrawtransaction":
var rawtx string var rawtx string
err := json.Unmarshal(params[0], &rawtx) err := json.Unmarshal(params[0], &rawtx)
if err != nil { if err != nil {
return nil, errors.New("Failed to parse sendrawtransaction JSON.") return nil, errors.New("Failed to parse sendrawtransaction JSON.")
} }
txbytes, err := hex.DecodeString(rawtx) txbytes, err := hex.DecodeString(rawtx)
if err != nil { if err != nil {
return nil, errors.New("Failed to parse sendrawtransaction value as a hex string.") return nil, errors.New("Failed to parse sendrawtransaction value as a hex string.")
} }
state.incoming_transactions = append(state.incoming_transactions, txbytes) state.incoming_transactions = append(state.incoming_transactions, txbytes)
return nil, nil return nil, nil
case "x_setstate": case "x_setstate":
var new_state map[string]interface{} var new_state map[string]interface{}
err := json.Unmarshal(params[0], &new_state) err := json.Unmarshal(params[0], &new_state)
if err != nil { if err != nil {
Log.Fatal("Could not unmarshal the provided state.") Log.Fatal("Could not unmarshal the provided state.")
} }
block_strings := make([]string, 0) block_strings := make([]string, 0)
for _, block_str := range new_state["blocks"].([]interface{}) { for _, block_str := range new_state["blocks"].([]interface{}) {
block_strings = append(block_strings, block_str.(string)) block_strings = append(block_strings, block_str.(string))
} }
state = &DarksideZcashdState{ state = &DarksideZcashdState{
start_height: int(new_state["start_height"].(float64)), start_height: int(new_state["start_height"].(float64)),
sapling_activation: int(new_state["sapling_activation"].(float64)), sapling_activation: int(new_state["sapling_activation"].(float64)),
branch_id: new_state["branch_id"].(string), branch_id: new_state["branch_id"].(string),
chain_name: new_state["chain_name"].(string), chain_name: new_state["chain_name"].(string),
blocks: block_strings, blocks: block_strings,
incoming_transactions: state.incoming_transactions, incoming_transactions: state.incoming_transactions,
server_start: state.server_start, server_start: state.server_start,
} }
return nil, nil return nil, nil
case "x_getincomingtransactions": case "x_getincomingtransactions":
txlist := "[" txlist := "["
for i, tx := range state.incoming_transactions { for i, tx := range state.incoming_transactions {
txlist += "\"" + hex.EncodeToString(tx) + "\"" txlist += "\"" + hex.EncodeToString(tx) + "\""
// add commas after all but the last // add commas after all but the last
if i < len(state.incoming_transactions) - 1 { if i < len(state.incoming_transactions)-1 {
txlist += ", " txlist += ", "
} }
} }
txlist += "]" txlist += "]"
return []byte(txlist), nil return []byte(txlist), nil
default: default:
return nil, errors.New("There was an attempt to call an unsupported RPC.") return nil, errors.New("There was an attempt to call an unsupported RPC.")
} }
} }