public async Task can_get_siblings_is_protected_against_overflow(long leafIndex)
        {
            SingleReleaseSpecProvider spec    = new SingleReleaseSpecProvider(ConstantinopleFix.Instance, 1);
            TestRpcBlockchain         testRpc = await TestRpcBlockchain.ForTest(SealEngineType.NethDev).Build(spec);

            BaselineModule baselineModule = new BaselineModule(
                testRpc.TxSender,
                testRpc.StateReader,
                testRpc.LogFinder,
                testRpc.BlockTree,
                _abiEncoder,
                _fileSystem,
                new MemDb(),
                LimboLogs.Instance);

            await testRpc.AddFunds(TestItem.Addresses[0], 1.Ether());

            Keccak txHash = (await baselineModule.baseline_deploy(TestItem.Addresses[0], "MerkleTreeSHA")).Data;
            await testRpc.AddBlock();

            ReceiptForRpc receipt = (await testRpc.EthModule.eth_getTransactionReceipt(txHash)).Data;

            await baselineModule.baseline_insertLeaf(TestItem.Addresses[1], receipt.ContractAddress, TestItem.KeccakH);

            await testRpc.AddBlock();

            var result = await baselineModule.baseline_getSiblings(receipt.ContractAddress, leafIndex);

            await testRpc.AddBlock();

            result.Result.ResultType.Should().Be(ResultType.Failure);
            result.Result.Error.Should().NotBeNull();
            result.ErrorCode.Should().Be(ErrorCodes.InvalidInput);
            result.Data.Should().BeNull();
        }
        public async Task insert_leaf_given_hash_is_emitting_an_event()
        {
            SingleReleaseSpecProvider spec    = new SingleReleaseSpecProvider(ConstantinopleFix.Instance, 1);
            TestRpcBlockchain         testRpc = await TestRpcBlockchain.ForTest(SealEngineType.NethDev).Build(spec);

            BaselineModule baselineModule = new BaselineModule(
                testRpc.TxSender,
                testRpc.StateReader,
                testRpc.LogFinder,
                testRpc.BlockTree,
                _abiEncoder,
                _fileSystem,
                new MemDb(),
                LimboLogs.Instance);

            await testRpc.AddFunds(TestItem.Addresses[0], 1.Ether());

            await testRpc.AddFunds(TestItem.Addresses[1], 1.Ether());

            Keccak txHash = (await baselineModule.baseline_deploy(TestItem.Addresses[0], "MerkleTreeSHA")).Data;
            await testRpc.AddBlock();

            ReceiptForRpc receipt = (await testRpc.EthModule.eth_getTransactionReceipt(txHash)).Data;

            Keccak insertLeafTxHash = (
                await baselineModule.baseline_insertLeaf(
                    TestItem.Addresses[1],
                    receipt.ContractAddress,
                    TestItem.KeccakH)).Data;
            await testRpc.AddBlock();

            ReceiptForRpc insertLeafReceipt = (await testRpc.EthModule.eth_getTransactionReceipt(insertLeafTxHash)).Data;

            insertLeafReceipt.Logs.Should().HaveCount(1);
        }
        public async Task can_get_leaves_fails_if_any_index_invalid()
        {
            SingleReleaseSpecProvider spec    = new SingleReleaseSpecProvider(ConstantinopleFix.Instance, 1);
            TestRpcBlockchain         testRpc = await TestRpcBlockchain.ForTest(SealEngineType.NethDev).Build(spec);

            BaselineModule baselineModule = new BaselineModule(
                testRpc.TxPoolBridge,
                testRpc.LogFinder,
                testRpc.BlockTree,
                _abiEncoder,
                _fileSystem,
                new MemDb(),
                LimboLogs.Instance);

            await testRpc.AddFunds(TestItem.Addresses[0], 1.Ether());

            Keccak txHash = (await baselineModule.baseline_deploy(TestItem.Addresses[0], "MerkleTreeSHA")).Data;
            await testRpc.AddBlock();

            ReceiptForRpc receipt = (await testRpc.EthModule.eth_getTransactionReceipt(txHash)).Data;

            await baselineModule.baseline_track(receipt.ContractAddress);

            await baselineModule.baseline_insertLeaf(TestItem.Addresses[1], receipt.ContractAddress, TestItem.KeccakH);

            await testRpc.AddBlock();

            var result = await baselineModule.baseline_getLeaves(receipt.ContractAddress, 0, (UInt256)uint.MaxValue + 1);

            await testRpc.AddBlock();

            result.Result.ResultType.Should().Be(ResultType.Failure);
            result.ErrorCode.Should().Be(ErrorCodes.InvalidInput);
        }
        private async Task RunAll(TestRpcBlockchain testRpc, BaselineModule baselineModule, int taskId)
        {
            Keccak txHash = (await baselineModule.baseline_deploy(TestItem.Addresses[0], "MerkleTreeSHA")).Data;
            await testRpc.AddBlock();

            ReceiptForRpc receipt;
            int           tries = 100;

            do
            {
                receipt = (await testRpc.EthModule.eth_getTransactionReceipt(txHash)).Data;
                await Task.Delay(10);

                tries--;
            } while (receipt != null && tries > 0);

            if (receipt == null)
            {
                throw new InvalidOperationException($"Receipt is null in task {taskId}");
            }

            Address contract = receipt.ContractAddress;

            Console.WriteLine($"Task {taskId} operating on contract {contract}");

            await baselineModule.baseline_track(contract);

            for (int i = 0; i < 16; i++)
            {
                Stopwatch stopwatch = Stopwatch.StartNew();
                await baselineModule.baseline_insertLeaf(TestItem.Addresses[taskId], contract, TestItem.Keccaks[i % TestItem.Keccaks.Length]);

                await testRpc.AddBlock();

                Block headBlock             = testRpc.BlockTree.Head;
                BaselineTreeNode[] siblings = (await baselineModule.baseline_getSiblings(
                                                   contract, 0, new BlockParameter(headBlock.Number))).Data;
                Keccak root = (await baselineModule.baseline_getRoot(
                                   contract, new BlockParameter(headBlock.Number))).Data;
                bool result = (await baselineModule.baseline_verify(
                                   contract, root, TestItem.Keccaks[0], siblings, new BlockParameter(headBlock.Number))).Data;
                if (!result)
                {
                    throw new InvalidOperationException($"Failed to verify at {contract}, task {taskId}, iteration {i}, root {root}");
                }
                else
                {
                    Console.WriteLine($"Verified at {contract}, task {taskId}, iteration {i}, root {root}");
                }
            }

            Console.WriteLine($"Finishing task {taskId}");
        }
        public async Task can_get_siblings_after_leaf_is_added()
        {
            SingleReleaseSpecProvider spec    = new SingleReleaseSpecProvider(ConstantinopleFix.Instance, 1);
            TestRpcBlockchain         testRpc = await TestRpcBlockchain.ForTest(SealEngineType.NethDev).Build(spec);

            BaselineModule baselineModule = new BaselineModule(
                testRpc.TxSender,
                testRpc.StateReader,
                testRpc.LogFinder,
                testRpc.BlockTree,
                _abiEncoder,
                _fileSystem,
                new MemDb(),
                LimboLogs.Instance);

            await testRpc.AddFunds(TestItem.Addresses[0], 1.Ether());

            await testRpc.AddFunds(TestItem.Addresses[1], 1.Ether());

            Keccak txHash = (await baselineModule.baseline_deploy(TestItem.Addresses[0], "MerkleTreeSHA")).Data;
            await testRpc.AddBlock();

            ReceiptForRpc receipt = (await testRpc.EthModule.eth_getTransactionReceipt(txHash)).Data;

            await baselineModule.baseline_insertLeaf(
                TestItem.Addresses[1], receipt.ContractAddress, TestItem.KeccakH);

            await testRpc.AddBlock();

            await baselineModule.baseline_track(receipt.ContractAddress);

            var result = await baselineModule.baseline_getSiblings(receipt.ContractAddress, 0);

            await testRpc.AddBlock();

            result.Result.ResultType.Should().Be(ResultType.Success);
            result.Result.Error.Should().Be(null);
            result.ErrorCode.Should().Be(0);
            result.Data.Should().HaveCount(32);

            Keccak root = (await baselineModule.baseline_getRoot(receipt.ContractAddress)).Data;
            bool   verificationResult =
                (await baselineModule.baseline_verify(
                     receipt.ContractAddress,
                     root,
                     TestItem.KeccakH,
                     result.Data))
                .Data;

            verificationResult.Should().Be(true);
        }
        public async Task can_work_with_many_trees()
        {
            SingleReleaseSpecProvider spec    = new SingleReleaseSpecProvider(ConstantinopleFix.Instance, 1);
            TestRpcBlockchain         testRpc = await TestRpcBlockchain.ForTest(SealEngineType.NethDev).Build(spec);

            BaselineModule baselineModule = new BaselineModule(
                testRpc.TxSender,
                testRpc.StateReader,
                testRpc.LogFinder,
                testRpc.BlockTree,
                _abiEncoder,
                _fileSystem,
                new MemDb(),
                LimboLogs.Instance);

            await testRpc.AddFunds(TestItem.Addresses[0], 1.Ether());

            await testRpc.AddFunds(TestItem.Addresses[1], 1.Ether());

            Keccak txHash  = (await baselineModule.baseline_deploy(TestItem.Addresses[0], "MerkleTreeSHA")).Data;
            Keccak txHash2 = (await baselineModule.baseline_deploy(TestItem.Addresses[0], "MerkleTreeSHA")).Data;
            Keccak txHash3 = (await baselineModule.baseline_deploy(TestItem.Addresses[0], "MerkleTreeSHA")).Data;
            await testRpc.AddBlock();

            await testRpc.AddBlock();

            await testRpc.AddBlock();

            ReceiptForRpc receipt  = (await testRpc.EthModule.eth_getTransactionReceipt(txHash)).Data;
            ReceiptForRpc receipt2 = (await testRpc.EthModule.eth_getTransactionReceipt(txHash2)).Data;
            ReceiptForRpc receipt3 = (await testRpc.EthModule.eth_getTransactionReceipt(txHash3)).Data;

            receipt.Status.Should().Be(1);
            receipt2.Status.Should().Be(1);
            receipt3.Status.Should().Be(1);

            await baselineModule.baseline_insertLeaves(
                TestItem.Addresses[1], receipt.ContractAddress, TestItem.KeccakG, TestItem.KeccakH);

            await baselineModule.baseline_insertLeaves(
                TestItem.Addresses[1], receipt2.ContractAddress, TestItem.KeccakE, TestItem.KeccakF);

            await baselineModule.baseline_insertLeaf(
                TestItem.Addresses[1], receipt3.ContractAddress, TestItem.KeccakG);

            await baselineModule.baseline_insertLeaf(
                TestItem.Addresses[1], receipt3.ContractAddress, TestItem.KeccakH);

            await testRpc.AddBlock();

            await testRpc.AddBlock();

            await baselineModule.baseline_track(receipt.ContractAddress);

            await baselineModule.baseline_track(receipt2.ContractAddress);

            await baselineModule.baseline_track(receipt3.ContractAddress);

            var result = await baselineModule.baseline_getSiblings(receipt.ContractAddress, 1);

            var result2 = await baselineModule.baseline_getSiblings(receipt2.ContractAddress, 1);

            var result3 = await baselineModule.baseline_getSiblings(receipt3.ContractAddress, 1);

            await testRpc.AddBlock();

            result.Result.ResultType.Should().Be(ResultType.Success);
            result.Data.Should().HaveCount(32);

            result2.Result.ResultType.Should().Be(ResultType.Success);
            result2.Data.Should().HaveCount(32);

            result3.Result.ResultType.Should().Be(ResultType.Success);
            result3.Data.Should().HaveCount(32);

            for (int i = 1; i < 32; i++)
            {
                result.Data[i].Hash.Should().Be(Keccak.Zero);
                result2.Data[i].Hash.Should().Be(Keccak.Zero);
                result3.Data[i].Hash.Should().Be(Keccak.Zero);
            }

            result.Data[0].Hash.Should().NotBe(Keccak.Zero);
            result2.Data[0].Hash.Should().NotBe(Keccak.Zero);
            result3.Data[0].Hash.Should().NotBe(Keccak.Zero);

            result.Data[0].Hash.Should().NotBe(result2.Data[0].Hash);
            result.Data[0].Hash.Should().Be(result3.Data[0].Hash);
        }