Beispiel #1
0
        private static MockCellDep[] GetMockCellDep(Client client, CellDep cellDep)
        {
            TransactionWithStatus?txWithStatus = client.GetTransaction(cellDep.OutPoint.TxHash);

            if (txWithStatus == null)
            {
                throw new Exception($"Cannot find cell dep: {{ tx_hash: {cellDep.OutPoint.TxHash}, index: {cellDep.OutPoint.Index} }}!");
            }
            int index = (int)TypesConvert.HexToUInt32(cellDep.OutPoint.Index);

            string data   = txWithStatus.Transaction.OutputsData[index];
            Output output = txWithStatus.Transaction.Outputs[index];

            List <MockCellDep> mockCellDeps = new();

            mockCellDeps.Add(new MockCellDep()
            {
                CellDep = cellDep,
                Output  = output,
                Data    = data,
            });

            if (cellDep.DepType == "dep_group")
            {
                CellDep[] cellDeps = UnpackDepGroup(data);
                foreach (CellDep dep in cellDeps)
                {
                    mockCellDeps.AddRange(GetMockCellDep(client, dep));
                }
            }

            return(mockCellDeps.ToArray());
        }
Beispiel #2
0
        private static MockInput GetMockInput(Client client, Input input)
        {
            TransactionWithStatus?txWithStatus = client.GetTransaction(input.PreviousOutput.TxHash);

            if (txWithStatus == null)
            {
                throw new Exception("Cannot find input cell");
            }
            int index = (int)TypesConvert.HexToUInt32(input.PreviousOutput.Index);

            MockInput mockInput = new MockInput
            {
                Input  = input,
                Output = txWithStatus.Transaction.Outputs[index],
                Data   = txWithStatus.Transaction.OutputsData[index],
            };

            return(mockInput);
        }
Beispiel #3
0
        public async Task <IActionResult> OnGet(string?txHash, string?ioType, int?ioIndex, int?scriptType, string?filePath, int?txId = null)
        {
            if (ioType != "input" && ioType != "output")
            {
                throw new Exception("ioType must be `input` or `output`!");
            }
            if (ioIndex == null)
            {
                throw new Exception("ioIndex cannot be null!");
            }
            if (scriptType == null)
            {
                throw new Exception("scriptType cannot be null!");
            }
            if (txId == null && (txHash == null || txHash == ""))
            {
                throw new Exception("txId and txHash cannot be all null!");
            }

            Client client = Rpc();

            Transaction transaction;

            if (txId != null)
            {
                RecordedTransaction?failedTransaction = await DbContext.RecordedTransactions.FirstOrDefaultAsync(t => t.Id == txId);

                if (failedTransaction == null)
                {
                    throw new Exception($"Failed transaction not found, check your txID: {txId}");
                }
                try
                {
                    transaction = Transaction.FromJson(failedTransaction.RawTransaction);
                }
                catch
                {
                    throw new Exception("Failed Transaction cannot parsed to Transaction!");
                }
            }
            else
            {
                TransactionWithStatus?txWithStatus = client.GetTransaction(txHash !);
                if (txWithStatus == null)
                {
                    throw new Exception($"Transaction not found: {txHash}");
                }
                transaction = txWithStatus.Transaction;
            }

            Output output = transaction.Outputs[(int)ioIndex];

            if (ioType == "input")
            {
                Input input = transaction.Inputs[(int)ioIndex];
                TransactionWithStatus originTx = client.GetTransaction(input.PreviousOutput.TxHash) !;
                output = originTx.Transaction.Outputs[TypesConvert.HexToUInt32(input.PreviousOutput.Index)];
            }
            if (scriptType == 1 && output.Type == null)
            {
                throw new Exception($"Type script not found in {{tx_hash: {txHash}, index: {ioIndex}, ioType: {ioType}}}");
            }

            Script          script     = scriptType == 0 ? output.Lock : output.Type;
            string          scriptHash = ComputeScriptHash(script);
            MockTransaction mockTx     = DumpTransaction(client, transaction);

            mockTx.Tx.Hash = null;

            string targetContractData = GetCellDepData(mockTx, script);

            string binaryFilePath = WriteToFile(scriptHash, targetContractData);

            string?binaryForDebugger = null;

            if (filePath != null)
            {
                binaryFilePath    = filePath;
                binaryForDebugger = filePath;
            }

            string mockTxFilePath = WriteMockTx(scriptHash, mockTx.ToJson());

            string scriptGroupType = scriptType == 0 ? "lock" : "type";

            try
            {
                DebuggerProcessManager.Start(ActiveProject !, scriptGroupType, scriptHash, mockTxFilePath, binaryFilePath, ioType, (int)ioIndex, binaryForDebugger);
            }
            catch (System.InvalidOperationException e)
            {
                TempData["ErrorMessage"] = e.Message;
            }

            return(Page());
        }