@@ -47,6 +47,7 @@ import (
4747 "github.com/bsv-blockchain/teranode/test/utils/wait"
4848 "github.com/bsv-blockchain/teranode/ulogger"
4949 "github.com/bsv-blockchain/teranode/util"
50+ libp2pPeer "github.com/libp2p/go-libp2p/core/peer"
5051 "github.com/stretchr/testify/assert"
5152 "github.com/stretchr/testify/require"
5253 tc "github.com/testcontainers/testcontainers-go/modules/compose"
@@ -109,7 +110,7 @@ func (je *JSONError) Error() string {
109110
110111// NewTestDaemon creates a new TestDaemon instance with the provided options.
111112func NewTestDaemon (t * testing.T , opts TestOptions ) * TestDaemon {
112- ctx , cancel := context .WithCancel (context . Background ())
113+ ctx , cancel := context .WithCancel (t . Context ())
113114
114115 var (
115116 composeDependencies tc.ComposeStack
@@ -460,8 +461,13 @@ func NewTestDaemon(t *testing.T, opts TestOptions) *TestDaemon {
460461 blockAssembler , ok := blockAssemblyService .(* blockassembly.BlockAssembly )
461462 require .True (t , ok )
462463
464+ assetURL := fmt .Sprintf ("http://127.0.0.1:%d" , appSettings .Asset .HTTPPort )
465+ if appSettings .Asset .APIPrefix != "" {
466+ assetURL += appSettings .Asset .APIPrefix
467+ }
468+
463469 return & TestDaemon {
464- AssetURL : fmt . Sprintf ( "http://127.0.0.1:%d" , appSettings . Asset . HTTPPort ) ,
470+ AssetURL : assetURL ,
465471 BlockAssembler : blockAssembler .GetBlockAssembler (),
466472 BlockAssemblyClient : blockAssemblyClient ,
467473 BlockValidationClient : blockValidationClient ,
@@ -1294,6 +1300,13 @@ func (td *TestDaemon) WaitForBlockStateChange(t *testing.T, expectedBlock *model
12941300 }
12951301}
12961302
1303+ func (td * TestDaemon ) WaitForBlockhash (t * testing.T , blockHash * chainhash.Hash , timeout time.Duration ) {
1304+ require .Eventually (t , func () bool {
1305+ _ , err := td .BlockchainClient .GetBlock (td .Ctx , blockHash )
1306+ return err == nil
1307+ }, timeout , 100 * time .Millisecond , "Timeout waiting for block with hash %s" , blockHash .String ())
1308+ }
1309+
12971310func (td * TestDaemon ) WaitForBlock (t * testing.T , expectedBlock * model.Block , timeout time.Duration , skipVerifyChain ... bool ) {
12981311 ctx , cancel := context .WithTimeout (td .Ctx , timeout )
12991312 defer cancel ()
@@ -1865,6 +1878,25 @@ func (td *TestDaemon) DisconnectFromPeer(t *testing.T, peer *TestDaemon) {
18651878 require .NoError (t , err , "Failed to disconnect from peer" )
18661879}
18671880
1881+ func (td * TestDaemon ) InjectPeer (t * testing.T , peer * TestDaemon ) {
1882+ peerID , err := libp2pPeer .Decode (peer .Settings .P2P .PeerID )
1883+ require .NoError (t , err , "Failed to decode peer ID" )
1884+
1885+ p2pService , err := td .d .ServiceManager .GetService ("P2P" )
1886+ require .NoError (t , err , "Failed to get P2P service" )
1887+
1888+ p2pServer , ok := p2pService .(* p2p.Server )
1889+ require .True (t , ok , "Failed to cast P2P service to Server" )
1890+
1891+ // Inject my peer info to other peer...
1892+ header , meta , err := peer .BlockchainClient .GetBestBlockHeader (td .Ctx )
1893+ require .NoError (t , err , "Failed to get best block header" )
1894+
1895+ p2pServer .InjectPeerForTesting (peerID , peer .Settings .Context , peer .AssetURL , meta .Height , header .Hash ())
1896+
1897+ t .Logf ("Injected peer %s into %s's registry (PeerID: %s)" , peer .Settings .Context , td .Settings .Context , peerID )
1898+ }
1899+
18681900func peerAddress (peer * TestDaemon ) string {
18691901 return fmt .Sprintf ("/dns/127.0.0.1/tcp/%d/p2p/%s" , peer .Settings .P2P .Port , peer .Settings .P2P .PeerID )
18701902}
0 commit comments