github.com/decred/dcrlnd@v0.7.6/lntest/dcrd.go (about) 1 //go:build !spv 2 // +build !spv 3 4 package lntest 5 6 import ( 7 "context" 8 "encoding/hex" 9 "fmt" 10 "testing" 11 12 pb "decred.org/dcrwallet/v4/rpc/walletrpc" 13 "github.com/decred/dcrd/rpcclient/v8" 14 rpctest "github.com/decred/dcrtest/dcrdtest" 15 ) 16 17 // DcrdBackendConfig is an implementation of the BackendConfig interface 18 // backed by a btcd node. 19 type DcrdBackendConfig struct { 20 // rpcConfig houses the connection config to the backing dcrd 21 // instance. 22 rpcConfig rpcclient.ConnConfig 23 24 // harness is this backend's node. 25 harness *rpctest.Harness 26 27 // miner is the backing miner used during tests. 28 miner *rpctest.Harness 29 } 30 31 // GenArgs returns the arguments needed to be passed to LND at startup for 32 // using this node as a chain backend. 33 func (b DcrdBackendConfig) GenArgs() []string { 34 var args []string 35 encodedCert := hex.EncodeToString(b.rpcConfig.Certificates) 36 args = append(args, fmt.Sprintf("--dcrd.rpchost=%v", b.rpcConfig.Host)) 37 args = append(args, fmt.Sprintf("--dcrd.rpcuser=%v", b.rpcConfig.User)) 38 args = append(args, fmt.Sprintf("--dcrd.rpcpass=%v", b.rpcConfig.Pass)) 39 args = append(args, fmt.Sprintf("--dcrd.rawrpccert=%v", encodedCert)) 40 41 return args 42 } 43 44 func (b DcrdBackendConfig) StartWalletSync(loader pb.WalletLoaderServiceClient, password []byte) error { 45 req := &pb.RpcSyncRequest{ 46 NetworkAddress: b.rpcConfig.Host, 47 Username: b.rpcConfig.User, 48 Password: []byte(b.rpcConfig.Pass), 49 Certificate: b.rpcConfig.Certificates, 50 DiscoverAccounts: true, 51 PrivatePassphrase: password, 52 } 53 54 stream, err := loader.RpcSync(context.Background(), req) 55 if err != nil { 56 return err 57 } 58 59 syncDone := make(chan error) 60 go func() { 61 for { 62 resp, err := stream.Recv() 63 if err != nil { 64 syncDone <- err 65 return 66 } 67 if resp.Synced { 68 close(syncDone) 69 break 70 } 71 } 72 73 // After sync is complete, just drain the notifications until 74 // the connection is closed. 75 for { 76 _, err := stream.Recv() 77 if err != nil { 78 return 79 } 80 } 81 }() 82 83 return <-syncDone 84 } 85 86 // ConnectMiner connects the backend to the underlying miner. 87 func (b DcrdBackendConfig) ConnectMiner() error { 88 return rpctest.ConnectNode(context.Background(), b.harness, b.miner) 89 } 90 91 // DisconnectMiner disconnects the backend to the underlying miner. 92 func (b DcrdBackendConfig) DisconnectMiner() error { 93 return rpctest.RemoveNode(context.Background(), b.harness, b.miner) 94 } 95 96 // Name returns the name of the backend type. 97 func (b DcrdBackendConfig) Name() string { 98 return "dcrd" 99 } 100 101 // NewBackend starts a new rpctest.Harness and returns a DcrdBackendConfig for 102 // that node. 103 func NewBackend(t *testing.T, miner *rpctest.Harness) (*DcrdBackendConfig, func() error, error) { 104 chainBackend, cleanUp, err := newBackend(t, miner) 105 if err != nil { 106 return nil, nil, err 107 } 108 109 bd := &DcrdBackendConfig{ 110 rpcConfig: chainBackend.RPCConfig(), 111 harness: chainBackend, 112 miner: miner, 113 } 114 115 return bd, cleanUp, nil 116 }