Example #1
0
        public async Task insert_commit_given_hash_is_emitting_an_event()
        {
            SingleReleaseSpecProvider spec    = new SingleReleaseSpecProvider(ConstantinopleFix.Instance, 1);
            TestRpcBlockchain         testRpc = await TestRpcBlockchain.ForTest(SealEngineType.NethDev).Build(spec);

            BaselineModule baselineModule = CreateBaselineModule(testRpc);
            BaselineTree   baselineTree   = new ShaBaselineTree(new MemDb(), new MemDb(), new byte[] { }, 0, LimboNoErrorLogger.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_insertCommit(
                    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);
        }
Example #2
0
        public async Task insert_commits_given_hash_is_emitting_an_event(int leafCount)
        {
            SingleReleaseSpecProvider spec    = new SingleReleaseSpecProvider(ConstantinopleFix.Instance, 1);
            TestRpcBlockchain         testRpc = await TestRpcBlockchain.ForTest(SealEngineType.NethDev).Build(spec);

            BaselineModule baselineModule = CreateBaselineModule(testRpc);

            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[] leaves             = Enumerable.Repeat(TestItem.KeccakH, leafCount).ToArray();
            Keccak   insertLeavesTxHash = (await baselineModule.baseline_insertCommits(
                                               TestItem.Addresses[1],
                                               receipt.ContractAddress,
                                               leaves)).Data;
            await testRpc.AddBlock();

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

            insertLeafReceipt.Logs.Should().HaveCount(1);
            insertLeafReceipt.Logs[0].Data.Length.Should().Be(128 + leafCount * 32);
        }
        public async Task can_get_siblings_is_protected_against_overflow(long leafIndex)
        {
            SingleReleaseSpecProvider spec = new(ConstantinopleFix.Instance, 1);

            using TestRpcBlockchain testRpc = await TestRpcBlockchain.ForTest <BaseLineRpcBlockchain>(SealEngineType.NethDev).Build(spec);

            BaselineModule baselineModule = CreateBaselineModule(testRpc);

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

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

            await baselineModule.baseline_insertCommit(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();
        }
Example #4
0
        public async Task can_get_commits_fails_if_any_index_invalid()
        {
            SingleReleaseSpecProvider spec    = new SingleReleaseSpecProvider(ConstantinopleFix.Instance, 1);
            TestRpcBlockchain         testRpc = await TestRpcBlockchain.ForTest(SealEngineType.NethDev).Build(spec);

            BaselineModule baselineModule = CreateBaselineModule(testRpc);

            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_insertCommit(TestItem.Addresses[1], receipt.ContractAddress, TestItem.KeccakH);

            await testRpc.AddBlock();

            var result = await baselineModule.baseline_getCommits(
                receipt.ContractAddress, new UInt256[] { 0, (UInt256)uint.MaxValue + 1 });

            await testRpc.AddBlock();

            result.Result.ResultType.Should().Be(ResultType.Failure);
            result.ErrorCode.Should().Be(ErrorCodes.InvalidInput);
        }
        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_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 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);
        }
Example #8
0
        public async Task can_get_commits_after_commit_is_added()
        {
            SingleReleaseSpecProvider spec    = new SingleReleaseSpecProvider(ConstantinopleFix.Instance, 1);
            TestRpcBlockchain         testRpc = await TestRpcBlockchain.ForTest(SealEngineType.NethDev).Build(spec);

            BaselineModule baselineModule = CreateBaselineModule(testRpc);

            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_insertCommit(TestItem.Addresses[1], receipt.ContractAddress, TestItem.KeccakH);

            await testRpc.AddBlock();

            await baselineModule.baseline_track(receipt.ContractAddress);

            var result = await baselineModule.baseline_getCommits(receipt.ContractAddress, new UInt256[] { 0, 1 });

            await testRpc.AddBlock();

            result.Result.ResultType.Should().Be(ResultType.Success);
            result.Result.Error.Should().Be(null);
            result.ErrorCode.Should().Be(0);
            result.Data[0].Hash.Should().Be(TestItem.KeccakH);
            result.Data[1].Hash.Should().Be(Keccak.Zero);
        }
        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 deploy_deploys_the_contract()
        {
            var spec = new SingleReleaseSpecProvider(ConstantinopleFix.Instance, 1);
            TestRpcBlockchain testRpc = await TestRpcBlockchain.ForTest(SealEngineType.NethDev).Build(spec);

            testRpc.TestWallet.UnlockAccount(TestItem.Addresses[0], new SecureString());
            await testRpc.AddFunds(TestItem.Addresses[0], 1.Ether());

            DepositModule depositModule = new DepositModule(
                testRpc.TxPoolBridge,
                testRpc.LogFinder,
                new DepositConfig()
            {
                DepositContractAddress = TestItem.AddressA.ToString()
            },
                LimboLogs.Instance);

            var result = await depositModule.deposit_deploy(TestItem.Addresses[0]);

            result.Data.Should().NotBe(null);
            result.ErrorCode.Should().Be(0);
            result.Result.Error.Should().BeNull();
            result.Result.ResultType.Should().Be(ResultType.Success);

            await testRpc.AddBlock();

            testRpc.BlockTree.Head.Number.Should().Be(5);
            testRpc.BlockTree.Head.Transactions.Should().Contain(tx => tx.IsContractCreation);

            var code = testRpc.StateReader
                       .GetCode(testRpc.BlockTree.Head.StateRoot, ContractAddress.From(TestItem.Addresses[0], 0));

            code.Should().NotBeEmpty();
        }
Example #12
0
        public async Task deploy_bytecode_validates_input(string bytecode)
        {
            var spec = new SingleReleaseSpecProvider(ConstantinopleFix.Instance, 1);
            TestRpcBlockchain testRpc = await TestRpcBlockchain.ForTest(SealEngineType.NethDev).Build(spec);

            testRpc.TestWallet.UnlockAccount(TestItem.Addresses[0], new SecureString());
            await testRpc.AddFunds(TestItem.Addresses[0], 1.Ether());

            BaselineModule baselineModule = CreateBaselineModule(testRpc);

            var result = await baselineModule.baseline_deployBytecode(
                TestItem.Addresses[0],
                bytecode); // invalid input

            result.Data.Should().Be(null);
            result.ErrorCode.Should().Be(ErrorCodes.InvalidInput);
            result.Result.Error.Should().NotBeNull();
            result.Result.ResultType.Should().Be(ResultType.Failure);
            await testRpc.AddBlock();

            testRpc.BlockTree.Head.Number.Should().Be(5);
            testRpc.BlockTree.Head.Transactions.Should().NotContain(tx => tx.IsContractCreation);

            var code = testRpc.StateReader
                       .GetCode(testRpc.BlockTree.Head.StateRoot, ContractAddress.From(TestItem.Addresses[0], 0));

            code.Should().BeEmpty();
        }
        public async Task deploy_deploys_the_contract()
        {
            var spec = new SingleReleaseSpecProvider(ConstantinopleFix.Instance, 1);
            TestRpcBlockchain testRpc = await TestRpcBlockchain.ForTest(SealEngineType.NethDev).Build(spec);

            testRpc.TestWallet.UnlockAccount(TestItem.Addresses[0], new SecureString());
            await testRpc.AddFunds(TestItem.Addresses[0], 1.Ether());

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

            var result = await baselineModule.baseline_deploy(TestItem.Addresses[0], "MerkleTreeSHA");

            result.Data.Should().NotBe(null);
            result.ErrorCode.Should().Be(0);
            result.Result.Error.Should().BeNull();
            result.Result.ResultType.Should().Be(ResultType.Success);

            await testRpc.AddBlock();

            testRpc.BlockTree.Head.Number.Should().Be(5);
            testRpc.BlockTree.Head.Transactions.Should().Contain(tx => tx.IsContractCreation);

            var code = testRpc.StateReader
                       .GetCode(testRpc.BlockTree.Head.StateRoot, ContractAddress.From(TestItem.Addresses[0], 0));

            code.Should().NotBeEmpty();
        }
Example #14
0
        private async Task <(TestRpcBlockchain TestRpc, BaselineModule BaselineModule)> InitializeTestRpc(Address address)
        {
            SingleReleaseSpecProvider spec = new SingleReleaseSpecProvider(ConstantinopleFix.Instance, 1);
            BlockBuilder      blockBuilder = Core.Test.Builders.Build.A.Block.Genesis.WithGasLimit(10000000000);
            TestRpcBlockchain testRpc      = await TestRpcBlockchain.ForTest <BaseLineTreeReorgTestBlockChain>(SealEngineType.NethDev)
                                             .WithGenesisBlockBuilder(blockBuilder)
                                             .Build(spec);

            testRpc.TestWallet.UnlockAccount(address, new SecureString());
            await testRpc.AddFunds(address, 1.Ether());

            BaselineModule baselineModule = new BaselineModule(
                testRpc.TxSender,
                testRpc.StateReader,
                testRpc.LogFinder,
                testRpc.BlockTree,
                new AbiEncoder(),
                _fileSystem,
                _baselineDb,
                _metadataBaselineDb,
                LimboLogs.Instance,
                testRpc.BlockProcessor,
                new DisposableStack());
            Keccak txHash = (await baselineModule.baseline_deploy(address, "MerkleTreeSHA")).Data;
            await testRpc.AddBlock();

            return(testRpc, baselineModule);
        }
Example #15
0
        public async Task Tree_tracker_insert_leaf([ValueSource(nameof(InsertLeafTestCases))] InsertLeafTest test)
        {
            Address address = TestItem.Addresses[0];

            (TestRpcBlockchain TestRpc, BaselineModule BaselineModule)result = await InitializeTestRpc(address);

            TestRpcBlockchain  testRpc             = result.TestRpc;
            BaselineTree       baselineTree        = BuildATree();
            Address            fromContractAddress = ContractAddress.From(address, 0L);
            BaselineTreeHelper baselineTreeHelper  = new BaselineTreeHelper(testRpc.LogFinder, _baselineDb, _metadataBaselineDb, LimboNoErrorLogger.Instance);

            new BaselineTreeTracker(fromContractAddress, baselineTree, testRpc.BlockProcessor, baselineTreeHelper, testRpc.BlockFinder, LimboNoErrorLogger.Instance);

            MerkleTreeSHAContract contract = new MerkleTreeSHAContract(_abiEncoder, fromContractAddress);
            UInt256 nonce = 1L;

            for (int i = 0; i < test.ExpectedTreeCounts.Length; i++)
            {
                nonce = await InsertLeafFromArray(test.LeavesInTransactionsAndBlocks[i], nonce, testRpc, contract, address);

                await testRpc.AddBlock();

                Assert.AreEqual(test.ExpectedTreeCounts[i], baselineTree.Count);
            }
        }
        public async Task deploy_bytecode_deploys_the_contract()
        {
            var spec = new SingleReleaseSpecProvider(ConstantinopleFix.Instance, 1);

            using TestRpcBlockchain testRpc = await TestRpcBlockchain.ForTest <BaseLineRpcBlockchain>(SealEngineType.NethDev).Build(spec);

            testRpc.TestWallet.UnlockAccount(TestItem.Addresses[0], new SecureString());
            BaselineModule baselineModule = CreateBaselineModule(testRpc);

            var result = await baselineModule.baseline_deployBytecode(
                TestItem.Addresses[0],
                File.ReadAllText("testBytecode"));

            result.Data.Should().NotBe(null);
            result.ErrorCode.Should().Be(0);
            result.Result.Error.Should().BeNull();
            result.Result.ResultType.Should().Be(ResultType.Success);

            await testRpc.AddBlock();

            testRpc.BlockTree.Head.Number.Should().Be(2);
            testRpc.BlockTree.Head.Transactions.Should().Contain(tx => tx.IsContractCreation);

            var code = testRpc.StateReader
                       .GetCode(testRpc.BlockTree.Head.StateRoot, ContractAddress.From(TestItem.Addresses[0], 0));

            code.Should().NotBeEmpty();
        }
        public async Task Suggesting_blocks_works_correctly_after_processor_restart(int suggestedBlocksAmount)
        {
            TestRpcBlockchain testRpc = await TestRpcBlockchain.ForTest(SealEngineType.NethDev).Build();

            await testRpc.BlockchainProcessor.StopAsync();

            IBlockTree tree = testRpc.BlockTree;
            long       startingBlockNumber = tree.Head !.Number;

            SuggestNumberOfBlocks(tree, suggestedBlocksAmount);

            // simulating restarts - we stopped the old blockchain processor and create the new one
            BlockchainProcessor newBlockchainProcessor = new(tree, testRpc.BlockProcessor,
                                                             testRpc.BlockPreprocessorStep, testRpc.StateReader, LimboLogs.Instance, BlockchainProcessor.Options.Default);

            newBlockchainProcessor.Start();
            testRpc.BlockchainProcessor = newBlockchainProcessor;

            // fixing after restart
            StartupBlockTreeFixer fixer = new(new SyncConfig(), tree, testRpc.DbProvider.StateDb, LimboNoErrorLogger.Instance, 5);
            await tree.Accept(fixer, CancellationToken.None);

            // waiting for N new heads
            for (int i = 0; i < suggestedBlocksAmount; ++i)
            {
                await testRpc.WaitForNewHead();
            }

            // add a new block at the end
            await testRpc.AddBlock();

            Assert.AreEqual(startingBlockNumber + suggestedBlocksAmount + 1, tree.Head !.Number);
        }
Example #18
0
                private async Task <ScenarioBuilder> BlocksBeforeTransitionShouldHaveZeroBaseFeeAsync()
                {
                    await ExecuteAntecedentIfNeeded();

                    IBlockTree blockTree     = _testRpcBlockchain.BlockTree;
                    Block      startingBlock = blockTree.Head;

                    Assert.AreEqual(UInt256.Zero, startingBlock !.Header.BaseFeePerGas);
                    for (long i = startingBlock.Number; i < _eip1559TransitionBlock - 1; ++i)
                    {
                        await _testRpcBlockchain.AddBlock();

                        Block currentBlock = blockTree.Head;
                        Assert.AreEqual(UInt256.Zero, currentBlock !.Header.BaseFeePerGas);
                    }

                    return(this);
                }
Example #19
0
        private static async Task <Address> Deploy(TestRpcBlockchain testRpc, BaselineModule baselineModule)
        {
            Keccak txHash = (await baselineModule.baseline_deploy(TestItem.Addresses[0], "MerkleTreeSHA")).Data;
            await testRpc.AddBlock();

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

            return(receipt.ContractAddress);
        }
Example #20
0
        public async Task Tree_tracker_reorganization([ValueSource(nameof(ReorganizationTestCases))] ReorganizedInsertLeafTest test)
        {
            Address address = TestItem.Addresses[0];

            (TestRpcBlockchain TestRpc, BaselineModule BaselineModule)result = await InitializeTestRpc(address);

            TestRpcBlockchain  testRpc            = result.TestRpc;
            BaselineTree       baselineTree       = BuildATree();
            Address            contractAddress    = ContractAddress.From(address, 0L);
            BaselineTreeHelper baselineTreeHelper = new (testRpc.LogFinder, _baselineDb, _metadataBaselineDb, LimboNoErrorLogger.Instance);

            _ = new BaselineTreeTracker(contractAddress, baselineTree, testRpc.BlockProcessor, baselineTreeHelper, testRpc.BlockFinder, LimboNoErrorLogger.Instance);

            MerkleTreeSHAContract contract = new (_abiEncoder, contractAddress);

            for (int i = 0; i < test.LeavesInBlocksCounts.Length; i++)
            {
                InsertLeafFromArray(test.LeavesInTransactionsAndBlocks[i], testRpc, contract, address);

                await testRpc.AddBlock();

                Assert.AreEqual(test.LeavesInBlocksCounts[i], baselineTree.Count);
            }

            int initBlocksCount = 4;
            int allBlocksCount  = initBlocksCount + test.LeavesInBlocksCounts.Length;
            TestBlockProducer testRpcBlockProducer = (TestBlockProducer)testRpc.BlockProducer;
            Block             lastProducedBlock    = null;

            testRpcBlockProducer.BlockProduced += (o, e) => lastProducedBlock = e.Block;
            testRpcBlockProducer.BlockParent    = testRpc.BlockTree.FindHeader(allBlocksCount);

            InsertLeafFromArray(test.LeavesInMiddleOfReorganization, testRpc, contract, address);

            await testRpc.AddBlock(false);

            testRpcBlockProducer.BlockParent = lastProducedBlock.Header;

            InsertLeafFromArray(test.LeavesInAfterReorganization, testRpc, contract, address);

            await testRpc.AddBlock();

            Assert.AreEqual(test.FinalLeavesCount, baselineTree.Count);
        }
        public async Task can_get_siblings_after_commit_is_added()
        {
            SingleReleaseSpecProvider spec = new(ConstantinopleFix.Instance, 1);

            using TestRpcBlockchain testRpc = await TestRpcBlockchain.ForTest <BaseLineRpcBlockchain>(SealEngineType.NethDev).Build(spec);

            BaselineModule baselineModule = CreateBaselineModule(testRpc);

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

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

            await baselineModule.baseline_insertCommit(
                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_getLogs()
        {
            var spec = new SingleReleaseSpecProvider(ConstantinopleFix.Instance, 1);
            TestRpcBlockchain testRpc = await TestRpcBlockchain.ForTest(SealEngineType.NethDev).Build(spec);

            testRpc.TestWallet.UnlockAccount(TestItem.Addresses[0], new SecureString());
            await testRpc.AddFunds(TestItem.Addresses[0], 33.Ether());

            DepositModule depositModule = new DepositModule(
                testRpc.TxPoolBridge,
                testRpc.LogFinder,
                new DepositConfig()
            {
                DepositContractAddress = TestItem.AddressA.ToString()
            },
                LimboLogs.Instance);

            await depositModule.deposit_deploy(TestItem.Addresses[0]);

            await testRpc.AddBlock();

            var contractAddress = ContractAddress.From(TestItem.Addresses[0], 0);
            await depositModule.deposit_setContractAddress(contractAddress);

            await depositModule.deposit_make(
                TestItem.Addresses[0],
                GetNonZeroBytes(48),
                GetNonZeroBytes(32),
                GetNonZeroBytes(96));

            await testRpc.AddBlock();

            var all = depositModule.deposit_getAll();

            all.Result.Data.Length.Should().Be(1);
            ulong amount = BinaryPrimitives.ReadUInt64LittleEndian(all.Result.Data[0].Amount);

            ((BigInteger)amount).Should().Be((BigInteger)32.Ether() / (BigInteger)1.GWei());
        }
        public async Task can_deposit_32eth()
        {
            var spec = new SingleReleaseSpecProvider(ConstantinopleFix.Instance, 1);
            TestRpcBlockchain testRpc = await TestRpcBlockchain.ForTest(SealEngineType.NethDev).Build(spec);

            testRpc.TestWallet.UnlockAccount(TestItem.Addresses[0], new SecureString());
            await testRpc.AddFunds(TestItem.Addresses[0], 33.Ether());

            DepositModule depositModule = new DepositModule(
                testRpc.TxPoolBridge,
                testRpc.LogFinder,
                new DepositConfig(),
                LimboLogs.Instance);

            await depositModule.deposit_deploy(TestItem.Addresses[0]);

            await testRpc.AddBlock();

            var contractAddress = ContractAddress.From(TestItem.Addresses[0], 0);
            await depositModule.deposit_setContractAddress(contractAddress);

            var result = await depositModule.deposit_make(
                TestItem.Addresses[0],
                new byte[48],
                new byte[32],
                new byte[96]);

            result.Data.Should().NotBe(null);
            result.ErrorCode.Should().Be(0);
            result.Result.Error.Should().BeNull();
            result.Result.ResultType.Should().Be(ResultType.Success);

            await testRpc.AddBlock();

            testRpc.BlockTree.Head.Transactions.Should().Contain(tx => !tx.IsContractCreation);

            /* if status is 1 then it means that no revert has been made */
            testRpc.ReceiptStorage.Get(testRpc.BlockTree.Head)[0].StatusCode.Should().Be(1);
        }
        public async Task cannot_get_siblings_after_commit_is_added_if_not_traced()
        {
            SingleReleaseSpecProvider spec = new(ConstantinopleFix.Instance, 1);

            using TestRpcBlockchain testRpc = await TestRpcBlockchain.ForTest <BaseLineRpcBlockchain>(SealEngineType.NethDev).Build(spec);

            BaselineModule baselineModule = CreateBaselineModule(testRpc);

            Address treeAddress = await Deploy(testRpc, baselineModule);

            await baselineModule.baseline_insertCommit(TestItem.Addresses[1], treeAddress, TestItem.KeccakH);

            await testRpc.AddBlock();

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

            await testRpc.AddBlock();

            result.Result.ResultType.Should().Be(ResultType.Failure);
            result.Result.Error.Should().NotBe(null);
            result.ErrorCode.Should().NotBe(0);
            result.Data.Should().BeNull();
        }
        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);
        }
        public async Task can_work_with_many_trees()
        {
            SingleReleaseSpecProvider spec = new(ConstantinopleFix.Instance, 1);

            using TestRpcBlockchain testRpc = await TestRpcBlockchain.ForTest <BaseLineRpcBlockchain>(SealEngineType.NethDev).Build(spec);

            BaselineModule baselineModule = CreateBaselineModule(testRpc);

            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.EthRpcModule.eth_getTransactionReceipt(txHash)).Data;
            ReceiptForRpc receipt2 = (await testRpc.EthRpcModule.eth_getTransactionReceipt(txHash2)).Data;
            ReceiptForRpc receipt3 = (await testRpc.EthRpcModule.eth_getTransactionReceipt(txHash3)).Data;

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

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

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

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

            await baselineModule.baseline_insertCommit(
                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);
        }