public void AddNode(string address, ushort port)
        {
            mNodeLock.WaitOne();
            foreach (NodeConnection n in mNodes)
            {
                if (n.Address == address && n.Port == port)
                {
                    return;
                }
            }

            NodeConnection node = new NodeConnection(this, address, port, mNetworkVersion, mNetworkID);

            mNodes.Add(node);
            mNodeLock.ReleaseMutex();
        }
        public void HandleHeadersPacket(NodeConnection from, byte[] payload)
        {
            MemoryStream stream = new MemoryStream(payload);
            BinaryReader br     = new BinaryReader(stream);

            List <BlockHeader> headers = new List <BlockHeader>();

            ulong count = Utils.ReadVarInt(br);

            Console.WriteLine("Got Headers: " + count);
            for (ulong i = 0; i < count; i++)
            {
                uint   version   = br.ReadUInt32();
                byte[] prevBlock = br.ReadBytes(32);
                byte[] merkle    = br.ReadBytes(32);
                uint   time      = br.ReadUInt32();
                uint   bits      = br.ReadUInt32();
                uint   nonce     = br.ReadUInt32();
                ulong  txn       = Utils.ReadVarInt(br);

                if (!mIgnoreSigLen)
                {
                    ulong siglen = Utils.ReadVarInt(br);
                    br.ReadBytes((int)siglen);
                }

                BlockHeader header = new BlockHeader(version, prevBlock, merkle, time, bits, nonce);
                if (headers.Count > 0)
                {
                    headers[headers.Count - 1].mHash = prevBlock;
                }
                headers.Add(header);
            }
            br.Close();

            if (!mPickedHashType)
            {
                mUseScryptHash  = headers[0].ValidateHash();
                mPickedHashType = true;
            }

            headers[headers.Count - 1].ComputeHash(mUseScryptHash);

            mPendingHeaders = headers;
            mLastNode       = from;
        }
        public void BadConnection(NodeConnection n)
        {
            mNodeLock.WaitOne();
            mNodes.Remove(n);
            bool alreadyDead = false;

            for (int i = 0; i < mDeadNodes.Count; i++)
            {
                if (mDeadNodes[i] == n)
                {
                    alreadyDead = true;
                    break;
                }
            }
            if (!alreadyDead)
            {
                mDeadNodes.Add(n);
            }
            mNodeLock.ReleaseMutex();
        }
        public void HandleBlockPacket(NodeConnection from, byte[] payload)
        {
            MemoryStream stream = new MemoryStream(payload);
            BinaryReader br     = new BinaryReader(stream);

            uint version = br.ReadUInt32();

            byte[] prevBlock = br.ReadBytes(32);
            byte[] merkle    = br.ReadBytes(32);
            uint   time      = br.ReadUInt32();
            uint   bits      = br.ReadUInt32();
            uint   nonce     = br.ReadUInt32();
            ulong  txn       = Utils.ReadVarInt(br);

            BlockHeader h = new BlockHeader(version, prevBlock, merkle, time, bits, nonce);

            h.ComputeHash(mUseScryptHash);

            Block block = FindBlock(h.mHash);

            if (block == null)
            {
                // Create a new block
                string hash = Utils.ByteArrayToHexString(h.mHash);
                Console.WriteLine("does a new block ever get introduced this way!?? " + hash);
                return;
            }

            block.mTransactions.Clear();
            for (ulong j = 0; j < txn; j++)
            {
                Transaction tx = new Transaction();
                tx.mVersion = br.ReadUInt32();
                if (mTransactionTimeStamp)
                {
                    tx.mTimestamp = br.ReadUInt32();
                }

                ulong inCount = Utils.ReadVarInt(br);
                for (ulong k = 0; k < inCount; k++)
                {
                    TransactionInput ti = new TransactionInput();
                    ti.mPrevOuptutHash  = br.ReadBytes(32);
                    ti.mPrevOutputIndex = br.ReadUInt32();

                    ulong scriptLen = Utils.ReadVarInt(br);
                    ti.mScript = br.ReadBytes((int)scriptLen);

                    ti.mSequence = br.ReadUInt32();

                    tx.mInputs.Add(ti);
                }

                ulong outCount = Utils.ReadVarInt(br);
                for (ulong k = 0; k < outCount; k++)
                {
                    TransactionOutput to = new TransactionOutput();
                    to.mValue     = br.ReadUInt64();
                    to.mRealValue = (double)to.mValue / 1000000.0;

                    ulong scriptLen = Utils.ReadVarInt(br);
                    to.mScript = br.ReadBytes((int)scriptLen);

                    tx.mOutputs.Add(to);
                }
                tx.mLockTime = br.ReadUInt32();
                if (tx.mVersion > 1)
                {
                    ulong commentLen = Utils.ReadVarInt(br);
                    tx.mComment = br.ReadBytes((int)commentLen);
                }

                block.mTransactions.Add(tx);
            }
            br.Close();
        }
 public void VersionRecieved(NodeConnection from)
 {
 }
 public void HandleInvPacket(NodeConnection from, byte[] payload)
 {
 }
Example #7
0
 public void BadConnection(NodeConnection n)
 {
     mNodeLock.WaitOne();
     mNodes.Remove(n);
     bool alreadyDead = false;
     for (int i = 0; i < mDeadNodes.Count; i++)
     {
         if (mDeadNodes[i] == n)
         {
             alreadyDead = true;
             break;
         }
     }
     if( !alreadyDead )
         mDeadNodes.Add(n);
     mNodeLock.ReleaseMutex();
 }
        void BCUpdateThread()
        {
            LoadBlocks();

            while (true)
            {
                if (mDeadNodes.Count > 0)
                {
                    mNodeLock.WaitOne();
                    foreach (NodeConnection n in mDeadNodes)
                    {
                        n.Destroy();
                    }
                    mDeadNodes.Clear();
                    mNodeLock.ReleaseMutex();
                }

                // Merge pending headers
                if (mPendingHeaders != null)
                {
                    MergePendingHeaders();
                    mPendingHeaders = null;
                    mGettingHeaders = false;
                }

                // Check for larger block chains
                if (!mGettingHeaders)
                {
                    mNodeLock.WaitOne();
                    foreach (NodeConnection n in mNodes)
                    {
                        if (n.mRemoteHeight > mCurrentHeight)
                        {
                            byte[] originHash = null;
                            if (mBlocks.Count > 0)
                            {
                                mBlockLock.WaitOne();
                                originHash = mBlocks[mBlocks.Count - 1].mHeader.mHash;
                                mBlockLock.ReleaseMutex();
                            }
                            n.RequestHeaders(originHash);
                            Console.WriteLine("Requesting Headers " + mCurrentHeight + " / " + n.mRemoteHeight);
                            mGettingHeaders    = true;
                            mLastHeaderRequest = DateTime.Now;
                            break;
                        }
                    }
                    mNodeLock.ReleaseMutex();
                }
                else
                {
                    TimeSpan s = DateTime.Now - mLastHeaderRequest;
                    if (s.TotalSeconds > 15)
                    {
                        // Abandon this request and move this connection to the bottom
                        mNodeLock.WaitOne();
                        NodeConnection n = mNodes[0];
                        mNodes.RemoveAt(0);
                        mNodes.Add(n);
                        mNodeLock.ReleaseMutex();
                        n.FetchingHeaders = false;
                        mGettingHeaders   = false;
                    }
                }

                RequestBlocks();

                ArchiveBlocks();

                Thread.Sleep(100);
            }
        }
Example #9
0
        public void AddNode(string address, ushort port)
        {
            mNodeLock.WaitOne();
            foreach (NodeConnection n in mNodes)
            {
                if (n.Address == address && n.Port == port)
                    return;
            }

            NodeConnection node = new NodeConnection(this, address, port, mNetworkVersion, mNetworkID);
            mNodes.Add(node);
            mNodeLock.ReleaseMutex();
        }
Example #10
0
 public void VersionRecieved(NodeConnection from)
 {
 }
Example #11
0
 public void HandleInvPacket(NodeConnection from, byte[] payload)
 {
 }
Example #12
0
        public void HandleHeadersPacket(NodeConnection from, byte[] payload)
        {
            MemoryStream stream = new MemoryStream(payload);
            BinaryReader br = new BinaryReader(stream);

            List<BlockHeader> headers = new List<BlockHeader>();

            ulong count = Utils.ReadVarInt(br);
            Console.WriteLine("Got Headers: " + count);
            for (ulong i = 0; i < count; i++)
            {
                uint version = br.ReadUInt32();
                byte[] prevBlock = br.ReadBytes(32);
                byte[] merkle = br.ReadBytes(32);
                uint time = br.ReadUInt32();
                uint bits = br.ReadUInt32();
                uint nonce = br.ReadUInt32();
                ulong txn = Utils.ReadVarInt(br);

                if (!mIgnoreSigLen)
                {
                    ulong siglen = Utils.ReadVarInt(br);
                    br.ReadBytes((int)siglen);
                }

                BlockHeader header = new BlockHeader(version, prevBlock, merkle, time, bits, nonce);
                if (headers.Count > 0)
                    headers[headers.Count - 1].mHash = prevBlock;
                headers.Add(header);
            }
            br.Close();

            if (!mPickedHashType)
            {
                mUseScryptHash = headers[0].ValidateHash();
                mPickedHashType = true;
            }

            headers[headers.Count - 1].ComputeHash(mUseScryptHash);

            mPendingHeaders = headers;
            mLastNode = from;
        }
Example #13
0
        public void HandleBlockPacket(NodeConnection from, byte[] payload)
        {
            MemoryStream stream = new MemoryStream(payload);
            BinaryReader br = new BinaryReader(stream);

            uint version = br.ReadUInt32();
            byte[] prevBlock = br.ReadBytes(32);
            byte[] merkle = br.ReadBytes(32);
            uint time = br.ReadUInt32();
            uint bits = br.ReadUInt32();
            uint nonce = br.ReadUInt32();
            ulong txn = Utils.ReadVarInt(br);

            BlockHeader h = new BlockHeader(version, prevBlock, merkle, time, bits, nonce);
            h.ComputeHash(mUseScryptHash);

            Block block = FindBlock(h.mHash);
            if (block == null)
            {
                // Create a new block
                string hash = Utils.ByteArrayToHexString(h.mHash);
                Console.WriteLine("does a new block ever get introduced this way!?? " + hash);
                return;
            }

            block.mTransactions.Clear();
            for (ulong j = 0; j < txn; j++)
            {
                Transaction tx = new Transaction();
                tx.mVersion = br.ReadUInt32();
                if( mTransactionTimeStamp )
                    tx.mTimestamp = br.ReadUInt32();

                ulong inCount = Utils.ReadVarInt(br);
                for (ulong k = 0; k < inCount; k++)
                {
                    TransactionInput ti = new TransactionInput();
                    ti.mPrevOuptutHash = br.ReadBytes(32);
                    ti.mPrevOutputIndex = br.ReadUInt32();

                    ulong scriptLen = Utils.ReadVarInt(br);
                    ti.mScript = br.ReadBytes((int)scriptLen);

                    ti.mSequence = br.ReadUInt32();

                    tx.mInputs.Add(ti);
                }

                ulong outCount = Utils.ReadVarInt(br);
                for (ulong k = 0; k < outCount; k++)
                {
                    TransactionOutput to = new TransactionOutput();
                    to.mValue = br.ReadUInt64();
                    to.mRealValue = (double)to.mValue / 1000000.0;

                    ulong scriptLen = Utils.ReadVarInt(br);
                    to.mScript = br.ReadBytes((int)scriptLen);

                    tx.mOutputs.Add(to);
                }
                tx.mLockTime = br.ReadUInt32();
                if (tx.mVersion > 1)
                {
                    ulong commentLen = Utils.ReadVarInt(br);
                    tx.mComment = br.ReadBytes((int)commentLen);
                }

                block.mTransactions.Add(tx);
            }
            br.Close();
        }