MetacoSA / NBitcoin

Comprehensive Bitcoin library for the .NET framework.
MIT License
1.85k stars 839 forks source link

Why test code not working for using NbitcoinTest? #1164

Closed zydjohnHotmail closed 1 year ago

zydjohnHotmail commented 1 year ago

Hello: I want to run some test for using Nbitcoin.Tests & Nbitcoin.RPC. I created one xUnit Test Project, and some NUGET packages. The following is my code:

using NBitcoin.Tests; using NBitcoin; using Xunit.Abstractions; using NBitcoin.RPC;

namespace IntegrationTest1 { public class UnitTest1 { public ITestOutputHelper Logger { get; }

    public UnitTest1(ITestOutputHelper logger)
    {
        Logger = logger;
    }

    [Fact]
    public void AliceSendToBob()
    {
        using var env =
        NodeBuilder.Create(NodeDownloadData.Bitcoin.v0_20_0, Bitcoin.Instance.Regtest);
        CoreNode? miner = env.CreateNode();
        CoreNode? alice = env.CreateNode();
        CoreNode? bob = env.CreateNode();
        env.StartAll();
        Logger.WriteLine("Miner, Alice and Bob started");
        RPCClient? minerRPC = miner.CreateRPCClient();
        RPCClient? aliceRPC = alice.CreateRPCClient();
        RPCClient? bobRPC = bob.CreateRPCClient();
        minerRPC.Generate(100);
        BitcoinAddress? aliceAddress = aliceRPC.GetNewAddress();
        Logger.WriteLine("Miner just sent 1 BTC to Alice");
        minerRPC.SendToAddress(aliceAddress, Money.Coins(1.0m));
        Logger.WriteLine("Let's connect the Miner to Alice and Bob");
        miner.Sync(alice, true);
        miner.Sync(bob, true);
        Assert.Equal(Money.Coins(1.0m), aliceRPC.GetBalance());
        Logger.WriteLine("Alice check account balance!");
    }

}

}

But my test failed: Message:  NBitcoin.RPC.RPCException : Insufficient funds

I don’t quite understand, I think the miner has more than enough funds to give to Alice, but the test failed. Let me know how to change the code, so the test can succeed. Thanks,

lontivero commented 1 year ago

Because you have mature coins. A utxo must be at least 101 depth and in your test none has that.

zydjohnHotmail commented 1 year ago

Hi, Thanks for your response. I hope if I understand it correctly: minerRPC.Generate(100); This statement means miner has generated 100 blocks in block height, then how can I find how much money the miner has now, so when I want to send Alice money from miner, I can make sure that miner has enough already mature funds? Can I see it from VS2022 debug window or use some NBitcoin function? Please advise, Thanks,

zydjohnHotmail commented 1 year ago

OK. Thanks, I understand it now!