Exemplo n.º 1
0
        public static CheckStakeResult CheckStakeKernelHash(MintTemplate template, UInt32 txTime, UInt64 stakeModifier)
        {
            var retobj = new CheckStakeResult
            {
                Id        = template.Id,
                OfAddress = template.OfAddress,
                txTime    = txTime
            };

            if (txTime < template.PrevTxTime)
            {
                // Transaction timestamp violation
                return(retobj);
            }


            if (template.BlockFromTime + PeercoinConstants.StakeMinAge > txTime)
            {
                // Min age requirement
                return(retobj);
            }

            var bnTargetPerCoinDay = Mint.CompactToBig(template.Bits.Value);

            long nTimeWeight = txTime - template.PrevTxTime;

            if (nTimeWeight > PeercoinConstants.StakeMaxAge)
            {
                nTimeWeight = PeercoinConstants.StakeMaxAge;
            }

            long timeReduction = PeercoinConstants.StakeMinAge;

            nTimeWeight -= timeReduction;

            var t1 = new BigInteger(24 * 60 * 60);
            var t2 = new BigInteger(PeercoinConstants.Coin);
            var t3 = new BigInteger(template.PrevTxOutValue);
            var t4 = new BigInteger(nTimeWeight);

            BigInteger bnCoinDayWeight = (((t3 * t4) / t2)) / t1;


            BigInteger targetInt = bnCoinDayWeight * (bnTargetPerCoinDay);

            //byte[] array = new byte[28];
            var buffer = new byte[28];

            var bufferindex = 0;


            byte[] arrStakemodifier = BitConverter.GetBytes(stakeModifier);

            //put stakemodifier in buffer
            for (var i = 0; i < 8; i++)
            {
                buffer[bufferindex] = arrStakemodifier[i];
                bufferindex++;
            }

            //put other data in buffer
            new List <UInt32> {
                (UInt32)template.BlockFromTime, (UInt32)template.PrevTxOffset, (UInt32)template.PrevTxTime, (UInt32)template.PrevTxOutIndex, (UInt32)txTime
            }
            .ForEach(num => {
                var dn = num;
                for (var i = 0; i < 4; i++)
                {
                    buffer[bufferindex] = (byte)(dn & 0xff);
                    dn >>= 8;
                    bufferindex++;
                }
            });


            //no reverse so keep it in little endian
            var hashProofOfStake = Mint.DoubleSha256(buffer.ToList()).ToArray();               //keep it in little-endian .Reverse().ToArray();

            //add zero to last in array to make it unsigned:
            // https://docs.microsoft.com/en-us/dotnet/api/system.numerics.biginteger.-ctor?view=netframework-4.5.2#System_Numerics_BigInteger__ctor_System_Byte___
            if ((hashProofOfStake[hashProofOfStake.Length - 1] & 0x80) > 0)
            {
                byte[] temp = new byte[hashProofOfStake.Length];
                Array.Copy(hashProofOfStake, temp, hashProofOfStake.Length);
                hashProofOfStake = new byte[temp.Length + 1];
                Array.Copy(temp, hashProofOfStake, temp.Length);
            }

            var hashProofOfStakeInt = new BigInteger(hashProofOfStake);

            if (hashProofOfStakeInt > targetInt)
            {
                return(retobj);
            }

            //yeah, below target!

            retobj.minTarget = (hashProofOfStakeInt / bnCoinDayWeight) - new BigInteger(1);
            retobj.success   = true;
            retobj.hash      = hashProofOfStake;
            retobj.Id        = template.Id;


            var comp = Mint.IncCompact(
                Mint.BigToCompact(retobj.minTarget)
                );

            retobj.minimumDifficulty = Mint.CompactToDiff(comp);

            return(retobj);
        }
Exemplo n.º 2
0
        private void ParseTxos(CancellationToken canceltoken)
        {
            if (this.unspents != null)
            {
                var blockhashes = this.unspents.Select(un => un.blockhash).Distinct().ToList();
                var count       = blockhashes.Count;
                var counter     = 0;
                SetStatus("Importing blocks");
                blockhashes.ForEach(hash => {
                    SetProgress(counter, count);
                    blockParser.Parse(hash);
                    counter++;

                    if (canceltoken.IsCancellationRequested)
                    {
                        return;
                    }
                });


                var addresses = this.unspents.Select(un => un.address).Distinct().ToList();
                addresses.ForEach(address => {
                    if (canceltoken.IsCancellationRequested)
                    {
                        return;
                    }

                    var unspentsbyaddress = transactionRepository.GetUnspents(address);
                    unspentsbyaddress.ForEach(unspent => {
                        if (canceltoken.IsCancellationRequested)
                        {
                            return;
                        }


                        var m = new MintTemplate(
                            unspent.Id,
                            address,
                            unspent.BlockFromTime,
                            unspent.PrevTxOffset,
                            unspent.PrevTxTime,
                            unspent.PrevTxOutIndex,
                            (uint)unspent.PrevTxOutValue);

                        m.SetBitsWithDifficulty(((Convert.ToSingle(this.currentState.Diff) - this.minMarginDifficulty)));

                        if (this.templates.All(t => t.Id != m.Id))
                        {
                            this.templates.Add(m);
                        }
                    });
                });



                if (canceltoken.IsCancellationRequested)
                {
                    return;
                }

                SetStatus("Ready");
                SetProgress(0, 100);
                FillGrid();
            }
        }
Exemplo n.º 3
0
        public static void Findstake(Options o)
        {
            var blockRepository       = new BlockRepository();
            var transactionRepository = new TransactionRepository(blockRepository);

            var rpcclient = new RPCClient("http://127.0.0.1:" + o.Port, o.User, o.Password);
            var parser    = new BlockChainParser(rpcclient, blockRepository, transactionRepository);

            var blockcount = rpcclient.GetBlockCount();
            var difficulty = rpcclient.GetDifficulty().pos;

            if (blockcount < 999)
            {
                Console.WriteLine("Not connected to wallet" + blockcount);
                return;
            }

            var unspents  = new List <UnspentTransactionData>();
            var templates = new List <MintTemplate>();

            try
            {
                var listunspents = JsonConvert.DeserializeObject <List <Unspent> >(File.ReadAllText(System.IO.Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + o.FileListUnspent));
                unspents = listunspents
                           .Select(unspent => new UnspentTransactionData {
                    txid    = unspent.txid,
                    vout    = unspent.vout,
                    address = unspent.address
                })
                           .ToList();

                Console.WriteLine("");
                Console.Write("Importing blocks");
                using (var progress = new ProgressBar())
                {
                    var max     = unspents.Count;
                    var counter = 0;
                    unspents.ForEach(unspent => {
                        unspent.blockhash   = parser.GetBlockHash(unspent.txid);
                        unspent.blockheight = parser.Parse(unspent.blockhash);
                        parser.Parse(unspent.blockhash);
                        progress.Report((double)counter / max);
                        counter++;
                    });
                }

                float minMarginDifficulty = 2.0f;
                Console.WriteLine("");
                Console.Write("Importing adresses");
                using (var progress = new ProgressBar())
                {
                    var addresses = unspents.Select(un => un.address).Distinct().ToList();
                    var max       = addresses.Count;
                    var counter   = 0;

                    addresses.ForEach(address => {
                        progress.Report((double)counter / max);
                        var unspentsbyaddress = transactionRepository.GetUnspents(address);
                        unspentsbyaddress.ForEach(unspent => {
                            var m = new MintTemplate(
                                unspent.Id,
                                address,
                                unspent.BlockFromTime,
                                unspent.PrevTxOffset,
                                unspent.PrevTxTime,
                                unspent.PrevTxOutIndex,
                                (uint)unspent.PrevTxOutValue);

                            m.SetBitsWithDifficulty(((Convert.ToSingle(difficulty) - minMarginDifficulty)));

                            if (templates.All(t => t.Id != m.Id))
                            {
                                templates.Add(m);
                            }
                        });
                        counter++;
                    });
                }
            }
            catch (Exception)
            {
                //show error

                Console.WriteLine("Loading " + System.IO.Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + o.FileListUnspent + " listunspent data not succeeded");
                return;
            }


            Console.WriteLine("");
            Console.Write("Loading stakemodifiers");
            using (var progress = new ProgressBar())
            {
                var start = blockcount - (6 * 24 * 31) - 10;
                var end   = blockcount;
                for (uint i = start; i < end; i++)
                {
                    progress.Report((double)(i - start) / (end - start));
                    parser.Parse(i);
                }
            }

            var lastblock      = blockRepository.GetBlockState(blockcount - 6);     //not the very last
            var lastblocktime  = lastblock.bt;
            var blockModifiers = blockRepository.GetStakeModifiers(lastblock.h);

            var currentTime   = ConvertToUnixTimestamp(DateTime.UtcNow);
            var results       = new List <CheckStakeResult>();
            var resultsStakes = new List <PossibleStake>();

            Console.WriteLine("");
            Console.Write("Searching");
            using (var progress = new ProgressBar())
            {
                var start = currentTime;
                var end   = lastblocktime + PeercoinConstants.Findstakelimit;
                for (uint timestamp = start; timestamp < end; timestamp++)
                {
                    progress.Report((double)(timestamp - start) / (end - start));
                    var modifier = Mint.GetModifier(blockModifiers, timestamp);

                    foreach (var template in templates)
                    {
                        var result = Mint.CheckStakeKernelHash(template, timestamp, modifier.Value);
                        if (result.success)
                        {
                            if (results.All(r => r.Id != template.Id && r.txTime != timestamp))
                            {
                                Console.WriteLine(" " + template.OfAddress + ": " + ConvertFromUnixTimestamp(timestamp) + " difficulty: " + result.minimumDifficulty.ToString("0.00"));
                                results.Add(new CheckStakeResult()
                                {
                                    Id                = template.Id,
                                    OfAddress         = template.OfAddress,
                                    minimumDifficulty = result.minimumDifficulty,
                                    txTime            = timestamp
                                });
                                resultsStakes = Enrich(resultsStakes, templates, results);
                                ExportResults(resultsStakes, results);
                            }
                        }
                    }
                }
            }
            Console.WriteLine("Done");
            if (results.Count > 0)
            {
                Console.WriteLine("Findstake results exported to StakeDates.csv");
            }
        }//end