Ejemplo n.º 1
0
        private static async Task <HashSet <string> > GetAddressFromInput(Executor executor, ITransaction tx)
        {
            var ret = new HashSet <string>();

            if (tx.IsCoinbase)
            {
                return(ret);
            }

            foreach (Input input in tx.Inputs)
            {
                OutputPoint previousOutput = input.PreviousOutput;

                using (DisposableApiCallResult <GetTxDataResult> getTxResult = await executor.Chain.FetchTransactionAsync(previousOutput.Hash, false))
                {
                    Utils.CheckBitprimApiErrorCode(getTxResult.ErrorCode, "FetchTransactionAsync(" + Binary.ByteArrayToHexString(previousOutput.Hash) + ") failed, check errog log");

                    Output output = getTxResult.Result.Tx.Outputs[previousOutput.Index];

                    PaymentAddress outputAddress = output.PaymentAddress(executor.UseTestnetRules);
                    if (outputAddress.IsValid)
                    {
                        ret.Add(outputAddress.Encoded);
                    }
                }
            }

            return(ret);
        }
        private async Task SetOutputSpendInfo(TransactionOutputSummary jsonOutput, byte[] txHash, UInt32 index)
        {
            using (var outPoint = new OutputPoint(txHash, index))
            {
                var fetchSpendResult = await chain_.FetchSpendAsync(outPoint);

                if (fetchSpendResult.ErrorCode == ErrorCode.NotFound)
                {
                    jsonOutput.spentTxId   = null;
                    jsonOutput.spentIndex  = null;
                    jsonOutput.spentHeight = null;
                }
                else
                {
                    Utils.CheckBitprimApiErrorCode(fetchSpendResult.ErrorCode, "FetchSpendAsync failed, check error log");
                    var spend = fetchSpendResult.Result;
                    jsonOutput.spentTxId  = Binary.ByteArrayToHexString(spend.Hash);
                    jsonOutput.spentIndex = spend.Index;
                    using (var getTxResult = await chain_.FetchTransactionAsync(spend.Hash, false))
                    {
                        Utils.CheckBitprimApiErrorCode(getTxResult.ErrorCode, "FetchTransactionAsync(" + Binary.ByteArrayToHexString(spend.Hash) + "), check error log");
                        jsonOutput.spentHeight = getTxResult.Result.TxPosition.BlockHeight;
                    }
                }
            }
        }
Ejemplo n.º 3
0
        public async Task TestFetchSpend()
        {
            await WaitUntilBlock(FIRST_NON_COINBASE_BLOCK_HEIGHT, "TestFetchSpend");

            byte[]      hash        = Binary.HexStringToByteArray("0437cd7f8525ceed2324359c2d0ba26006d92d856a9c20fa0241106ee5a597c9");
            OutputPoint outputPoint = new OutputPoint(hash, 0);
            var         ret         = await executorFixture_.Executor.Chain.FetchSpendAsync(outputPoint);

            Assert.Equal(ErrorCode.Success, ret.ErrorCode);
            Assert.NotNull(ret.Result);
            Assert.Equal("f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16", Binary.ByteArrayToHexString(ret.Result.Hash));
            Assert.Equal <UInt32>(0, ret.Result.Index);
        }
Ejemplo n.º 4
0
        private async Task <AddressBalance> GetBalance(string paymentAddress, bool includeTransactionIds, Stopwatch stopWatch = null)
        {
            statsGetAddressHistory[1] = stopWatch?.ElapsedMilliseconds ?? -1;

            using (var address = new PaymentAddress(paymentAddress))
                using (var getAddressHistoryResult = await chain_.FetchHistoryAsync(address, UInt64.MaxValue, 0))
                {
                    statsGetAddressHistory[2] = stopWatch?.ElapsedMilliseconds ?? -1;

                    Utils.CheckBitprimApiErrorCode(getAddressHistoryResult.ErrorCode, "FetchHistoryAsync(" + paymentAddress + ") failed, check error log.");

                    var history = getAddressHistoryResult.Result;

                    UInt64 received       = 0;
                    UInt64 addressBalance = 0;
                    var    txs            = new OrderedSet <string>();

                    foreach (IHistoryCompact compact in history)
                    {
                        if (compact.PointKind == PointKind.Output)
                        {
                            received += compact.ValueOrChecksum;

                            using (var outPoint = new OutputPoint(compact.Point.Hash, compact.Point.Index))
                            {
                                var getSpendResult = await chain_.FetchSpendAsync(outPoint);

                                if (getSpendResult.ErrorCode == ErrorCode.NotFound)
                                {
                                    addressBalance += compact.ValueOrChecksum;
                                }
                            }
                        }
                        txs.Add(Binary.ByteArrayToHexString(compact.Point.Hash));
                    }



                    statsGetAddressHistory[3] = stopWatch?.ElapsedMilliseconds ?? -1;

                    UInt64 totalSent = received - addressBalance;
                    return(new AddressBalance
                    {
                        Balance = addressBalance,
                        Received = received,
                        Sent = totalSent,
                        Transactions = includeTransactionIds ? txs : null,
                        TxCount = (uint)txs.Count
                    });
                }
        }
Ejemplo n.º 5
0
        public Output GenerateOutput(List <Point> orderedPoints)
        {
            List <OutputPoint> resultList = new List <OutputPoint>();

            foreach (Point point in orderedPoints)
            {
                List <double> pointValues = new List <double>();
                pointValues.Add(point.GetX());
                pointValues.Add(point.GetY());
                OutputPoint temp = new OutputPoint(point.GetPointNumber(), pointValues);
                resultList.Add(temp);
            }

            return(new Output(resultList));
        }
Ejemplo n.º 6
0
        public void ThreeStepWithInputAndOutputPoints()
        {
            List <string>          results = new List <string>();
            InputPoint <int>       s       = new InputPoint <int>();
            TaskNode <int, string> filter  = Helpers.GetFilter();

            OutputPoint <string> outpoint = new OutputPoint <string>();

            Flow flow = Helpers.ConnectStartFilterEnd(s, filter, outpoint);

            flow.Start();
            s.Send(1, 2, 3, 4, 5, 6, 7, 8);
            s.Send(new int[] { 9, 10, 11, 12, 13, 14, 15 });
            string firstResult = outpoint.Output.Receive();

            Assert.AreEqual("3.00", firstResult);
        }
        private async Task <List <Utxo> > GetUtxo(string paymentAddress)
        {
            Utils.CheckIfChainIsFresh(chain_, config_.AcceptStaleRequests);

            using (var address = new PaymentAddress(paymentAddress))
                using (var getAddressHistoryResult = await chain_.FetchHistoryAsync(address, UInt64.MaxValue, 0))
                {
                    Utils.CheckBitprimApiErrorCode(getAddressHistoryResult.ErrorCode, "FetchHistoryAsync(" + paymentAddress + ") failed, check error log.");

                    var history = getAddressHistoryResult.Result;

                    var utxo = new List <Utxo>();

                    var getLastHeightResult = await chain_.FetchLastHeightAsync();

                    Utils.CheckBitprimApiErrorCode(getLastHeightResult.ErrorCode, "FetchLastHeightAsync failed, check error log");

                    var topHeight = getLastHeightResult.Result;

                    foreach (HistoryCompact compact in history)
                    {
                        if (compact.PointKind == PointKind.Output)
                        {
                            using (var outPoint = new OutputPoint(compact.Point.Hash, compact.Point.Index))
                            {
                                var getSpendResult = await chain_.FetchSpendAsync(outPoint);

                                if (getSpendResult.ErrorCode == ErrorCode.NotFound) //Unspent = it's an utxo
                                {
                                    //Get the tx to get the script
                                    using (var getTxResult = await chain_.FetchTransactionAsync(compact.Point.Hash, true))
                                    {
                                        Utils.CheckBitprimApiErrorCode(getTxResult.ErrorCode, "FetchTransactionAsync (" + Binary.ByteArrayToHexString(outPoint.Hash) + ") failed, check error log");
                                        utxo.Add(new Utxo(address, compact.Point, getTxResult.ErrorCode, getTxResult.Result.Tx, compact, topHeight));
                                    }
                                }
                            }
                        }
                    }
                    utxo.AddRange(GetUnconfirmedUtxo(address));
                    return(utxo);
                }
        }
        private void SetRow(DataGridViewRow row, OutputPoint point)
        {
            if (row == null || point == null)
            {
                return;
            }

            row.SetValue(DescriptionColumn, point.Description);
            row.SetValue(AutoManualColumn, point.AutoManual);
            row.SetValue(HOASwitchColumn, point.HwSwitchStatus);
            row.SetCell(ValueColumn, TViewUtilities.GetValueCellForUnit(
                            point.Value.ToString(),
                            point.Value.Unit));
            row.SetValue(UnitColumn, point.Value.Unit);
            row.SetValue(RangeColumn, point.Value.Value);
            row.SetValue(RangeTextColumn, point.Value.Unit);
            row.SetValue(LowVColumn, point.LowVoltage);
            row.SetValue(HighVColumn, point.HighVoltage);
            row.SetValue(PWMPeriodColumn, point.PwmPeriod);
            row.SetValue(StatusColumn, point.Control);
            row.SetValue(LabelColumn, point.Label);
        }
        private async Task <AddressBalance> GetBalance(string paymentAddress)
        {
            using (var address = new PaymentAddress(paymentAddress))
                using (var getAddressHistoryResult = await chain_.FetchHistoryAsync(address, UInt64.MaxValue, 0))
                {
                    Utils.CheckBitprimApiErrorCode(getAddressHistoryResult.ErrorCode, "FetchHistoryAsync(" + paymentAddress + ") failed, check error log.");

                    var history = getAddressHistoryResult.Result;

                    UInt64 received       = 0;
                    UInt64 addressBalance = 0;
                    var    txs            = new OrderedSet <string>();

                    foreach (HistoryCompact compact in history)
                    {
                        if (compact.PointKind == PointKind.Output)
                        {
                            received += compact.ValueOrChecksum;

                            using (var outPoint = new OutputPoint(compact.Point.Hash, compact.Point.Index))
                            {
                                var getSpendResult = await chain_.FetchSpendAsync(outPoint);

                                if (getSpendResult.ErrorCode == ErrorCode.NotFound)
                                {
                                    addressBalance += compact.ValueOrChecksum;
                                }
                            }
                        }
                        txs.Add(Binary.ByteArrayToHexString(compact.Point.Hash));
                    }

                    UInt64 totalSent = received - addressBalance;
                    return(new AddressBalance {
                        Balance = addressBalance, Received = received, Sent = totalSent, Transactions = txs
                    });
                }
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Add Output control point info
        /// </summary>
        /// <param name="output">Output Point</param>
        /// <param name="index">Index</param>
        public void Add(OutputPoint output, int index)
        {
            try
            {
                ControlPointInfo newCPInfo = new ControlPointInfo
                {
                    ControlPointName = "OUT" + index,
                    Label            = output.Label,
                    FullLabel        = output.Description,
                    Type             = IdentifierTypes.OUTS,
                    Value            = output.Value.ToString(),
                    Units            = output.Value.Unit.GetUnitsNames(null).OffOnName,
                    AutoManual       = output.AutoManual == 0 ? "Auto" : "Manual",
                    Index            = (short)index
                };


                Outputs.Add(newCPInfo);
            }
            catch (Exception ex)
            {
                ExceptionHandler.Show(ex, "Addition of new Output to ControlPointsInfo");
            }
        }
Ejemplo n.º 11
0
        public void Prg_BTUMeter()
        {
            var path = TestUtilities.GetFullPathForPrgFile("BTUMeter.prg");
            var prg  = Prg.Load(path);

            ObjectAssert.AreEqual(new CustomDigitalUnitsPoint(false, "TANK1", "TANK2"), prg.CustomUnits.Digital[0]);

            //Inputs
            {
                //IN1
                var expected = new InputPoint
                {
                    Description     = "TANK2 TOP",
                    AutoManual      = AutoManual.Automatic,
                    Value           = new VariableValue(0.683, Unit.PercentsVolts5),
                    CalibrationH    = 0.0,
                    CalibrationL    = 0.0,
                    CalibrationSign = Sign.Negative,
                    Control         = OffOn.On,
                    CustomUnits     = null,
                    DigitalAnalog   = DigitalAnalog.Analog,
                    FileVersion     = FileVersion.Rev6,
                    Filter          = 1,
                    Status          = InputStatus.Normal,
                    Jumper          = Jumper.To5V,
                    Label           = "T2_TOP",
                    SubNumber       = 0.1,
                    //Decom = 32
                };
                ObjectAssert.AreEqual(expected, prg.Inputs[0]);

                //IN2
                expected = new InputPoint
                {
                    Description     = "TANK2 BOT",
                    AutoManual      = AutoManual.Automatic,
                    Value           = new VariableValue(true, Unit.LowHigh, null, 1000),
                    CalibrationH    = 0.0,
                    CalibrationL    = 0.0,
                    CalibrationSign = Sign.Negative,
                    Control         = OffOn.On,
                    CustomUnits     = null,
                    DigitalAnalog   = DigitalAnalog.Digital,
                    FileVersion     = FileVersion.Rev6,
                    Filter          = 1,
                    Status          = InputStatus.Normal,
                    Jumper          = Jumper.Thermistor,
                    Label           = "T2_BOT",
                    SubNumber       = 0.1
                };
                ObjectAssert.AreEqual(expected, prg.Inputs[1]);

                //IN3
                expected = new InputPoint
                {
                    Description     = "IN 3",
                    AutoManual      = AutoManual.Automatic,
                    Value           = new VariableValue(19.824, Unit.Psi20),
                    CalibrationH    = 0.0,
                    CalibrationL    = 0.0,
                    CalibrationSign = Sign.Negative,
                    Control         = OffOn.On,
                    CustomUnits     = null,
                    DigitalAnalog   = DigitalAnalog.Analog,
                    FileVersion     = FileVersion.Rev6,
                    Filter          = 32,
                    Status          = InputStatus.Normal,
                    Jumper          = Jumper.Thermistor,
                    Label           = "IN3",
                    SubNumber       = 0.1
                };
                ObjectAssert.AreEqual(expected, prg.Inputs[2]);
            }

            //Outputs
            {
                //OUT1
                var expected = new OutputPoint()
                {
                    Description    = "VALVE LEFT",
                    AutoManual     = AutoManual.Manual,
                    HwSwitchStatus = SwitchStatus.Auto,
                    Value          = new VariableValue(true, Unit.OffOn, null, 1000),
                    LowVoltage     = 0,
                    HighVoltage    = 0,
                    PwmPeriod      = 0,
                    Control        = OffOn.On,
                    CustomUnits    = null,
                    DigitalAnalog  = DigitalAnalog.Digital,
                    FileVersion    = FileVersion.Rev6,
                    Label          = "VAL_LEFT",
                    SubNumber      = 0.1
                };
                ObjectAssert.AreEqual(expected, prg.Outputs[0]);

                //OUT2
                expected = new OutputPoint()
                {
                    Description    = "VALVE RIGHT",
                    AutoManual     = AutoManual.Automatic,
                    HwSwitchStatus = SwitchStatus.Auto,
                    Value          = new VariableValue(true, Unit.OffOn, null, 1000),
                    LowVoltage     = 0,
                    HighVoltage    = 0,
                    PwmPeriod      = 0,
                    Control        = OffOn.On,
                    CustomUnits    = null,
                    DigitalAnalog  = DigitalAnalog.Digital,
                    FileVersion    = FileVersion.Rev6,
                    Label          = "VAL_RIT",
                    SubNumber      = 0.1
                };
                ObjectAssert.AreEqual(expected, prg.Outputs[1]);
            }

            //Variables
            {
                //VAR1
                var expected = new VariablePoint()
                {
                    Description   = "START TEST FLAG",
                    AutoManual    = AutoManual.Automatic,
                    Value         = new VariableValue(false, Unit.OffOn, null, 1000),
                    Control       = OffOn.Off,
                    DigitalAnalog = DigitalAnalog.Digital,
                    FileVersion   = FileVersion.Rev6,
                    Label         = "INIT"
                };
                ObjectAssert.AreEqual(expected, prg.Variables[0]);

                //VAR10
                expected = new VariablePoint()
                {
                    Description   = "NOW FILLING",
                    AutoManual    = AutoManual.Automatic,
                    Value         = new VariableValue(false, Unit.CustomDigital1, null, 2000),
                    Control       = OffOn.Off,
                    DigitalAnalog = DigitalAnalog.Digital,
                    FileVersion   = FileVersion.Rev6,
                    Label         = "FILLTANK",
                };
                ObjectAssert.AreEqual(expected, prg.Variables[9]);
            }

            //Program codes
            {
                //var expected = new ProgramCode()
                //{
                //    Code = new byte[2000],
                //    FileVersion = FileVersion.Rev6
                //};
                //ObjectAssert.AreEqual(expected, prg.ProgramCodes[0]);

                //Console.WriteLine(prg.ProgramCodes[0].PropertiesText());
                //foreach (var line in prg.ProgramCodes[0].Lines)
                //{
                //Console.WriteLine(line.GetString());
                //}

                //Console.WriteLine(DebugUtilities.CompareBytes(prg.ProgramCodes[0].Code,
                //    prg.ProgramCodes[0].Code, onlyDif: false));
            }
        }
Ejemplo n.º 12
0
        public void ThreeStepWithInputAndOutputPoints()
        {
            List<string> results = new List<string>();
            InputPoint<int> s = new InputPoint<int>();
            TaskNode<int, string> filter = Helpers.GetFilter();

            OutputPoint<string> outpoint = new OutputPoint<string>();

            Flow flow = Helpers.ConnectStartFilterEnd(s, filter, outpoint);

            flow.Start();
            s.Send(1, 2, 3, 4, 5, 6, 7, 8);
            s.Send(new int[] { 9, 10, 11, 12, 13, 14, 15 });
            string firstResult = outpoint.Output.Receive();
            Assert.AreEqual("3.00", firstResult);
        }