// makeDestinationScriptSource creates a ChangeSource which is used to receive // all correlated previous input value. A non-change address is created by this // function. func makeDestinationScriptSource(rpcClient *dcrrpcclient.Client, accountName string) txauthor.ChangeSource { return func() ([]byte, error) { destinationAddress, err := rpcClient.GetNewAddress(accountName) if err != nil { return nil, err } return txscript.PayToAddrScript(destinationAddress) } }
// SetUp initializes the rpc test state. Initialization includes: starting up a // simnet node, creating a websocket client and connecting to the started node, // and finally: optionally generating and submitting a testchain with a configurable // number of mature coinbase outputs coinbase outputs. func (h *Harness) SetUp(createTestChain bool, numMatureOutputs uint32) error { var err error // Start the dcrd node itself. This spawns a new process which will be // managed if err = h.node.Start(); err != nil { return err } time.Sleep(200 * time.Millisecond) if err = h.connectRPCClient(); err != nil { return err } fmt.Println("Node RPC client connected.") // Start dcrwallet. This spawns a new process which will be managed if err = h.wallet.Start(); err != nil { return err } time.Sleep(1 * time.Second) // Connect walletClient so we can get the mining address var walletClient *rpc.Client walletRPCConf := h.wallet.config.rpcConnConfig() for i := 0; i < 400; i++ { if walletClient, err = rpc.New(&walletRPCConf, nil); err != nil { time.Sleep(time.Duration(math.Log(float64(i+3))) * 50 * time.Millisecond) continue } break } if walletClient == nil { return fmt.Errorf("walletClient connection timedout") } fmt.Println("Wallet RPC client connected.") h.WalletRPC = walletClient // Get a new address from the wallet to be set with dcrd's --miningaddr time.Sleep(5 * time.Second) var miningAddr dcrutil.Address for i := 0; i < 100; i++ { if miningAddr, err = walletClient.GetNewAddress("default"); err != nil { time.Sleep(time.Duration(math.Log(float64(i+3))) * 50 * time.Millisecond) continue } break } if miningAddr == nil { return fmt.Errorf("RPC not up for mining addr %v %v", h.testNodeDir, h.testWalletDir) } h.miningAddr = miningAddr var extraArgs []string miningArg := fmt.Sprintf("--miningaddr=%s", miningAddr) extraArgs = append(extraArgs, miningArg) // Stop node so we can restart it with --miningaddr if err = h.node.Stop(); err != nil { return err } config, err := newConfig(h.node.config.appDataDir, h.node.config.certFile, h.node.config.keyFile, extraArgs) if err != nil { return err } config.listen = h.node.config.listen config.rpcListen = h.node.config.rpcListen // Create the testing node bounded to the simnet. node, err := newNode(config, h.testNodeDir) if err != nil { return err } h.node = node // Restart node with mining address set if err = h.node.Start(); err != nil { return err } time.Sleep(1 * time.Second) if err := h.connectRPCClient(); err != nil { return err } fmt.Printf("Node RPC client connected, miningaddr: %v.\n", miningAddr) // Create a test chain with the desired number of mature coinbase outputs if createTestChain { numToGenerate := uint32(h.ActiveNet.CoinbaseMaturity) + numMatureOutputs fmt.Printf("Generating %v blocks...\n", numToGenerate) _, err := h.Node.Generate(numToGenerate) if err != nil { return err } fmt.Println("Block generation complete.") } // Wait for the wallet to sync up to the current height. // TODO: Figure out why this is the longest wait, about 60 sec, when it // should be almost immediate. fmt.Println("Waiting for wallet to sync to current height.") ticker := time.NewTicker(time.Millisecond * 500) desiredHeight := int64(numMatureOutputs + uint32(h.ActiveNet.CoinbaseMaturity)) out: for range ticker.C { count, err := h.WalletRPC.GetBlockCount() if err != nil { return err } if count == desiredHeight { break out } } ticker.Stop() fmt.Println("Wallet sync complete.") return nil }