Exemplo n.º 1
0
        /// <summary>
        ///  Saves block header to local storage
        /// </summary>
        /// <param name="block_header">Block header to save</param>
        /// <exception cref="Exception">Exception occured while saving block header.</exception>
        public static bool saveBlockHeader(BlockHeader block_header)
        {
            if (stopped)
            {
                return(false);
            }
            lock (lockObject)
            {
                ulong block_num = block_header.blockNum;

                ulong file_block_num = ((ulong)(block_num / CoreConfig.maxBlockHeadersPerDatabase)) * CoreConfig.maxBlockHeadersPerDatabase;

                string db_path = path + Path.DirectorySeparatorChar + "0000" + Path.DirectorySeparatorChar + file_block_num + ".dat";

                FileStream fs = getStorageFile(db_path, true); // File.Open(db_path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);

                if (fs.Length > 0)
                {
                    fs.Seek(-8, SeekOrigin.End);

                    byte[] file_block_header_bytes = new byte[8];
                    fs.Read(file_block_header_bytes, 0, 8);
                    if (BitConverter.ToUInt64(file_block_header_bytes, 0) + 1 != block_num)
                    {
                        // TODO should probably delete the file and reset lastBlockHeader
                        return(false);
                    }
                }

                try
                {
                    // insert dummy block for first block
                    if (block_num == 1)
                    {
                        BlockHeader dummy_block_header = new BlockHeader();
                        dummy_block_header.blockNum      = 0;
                        dummy_block_header.blockChecksum = new byte[1];

                        byte[] dummy_block_header_bytes     = dummy_block_header.getBytes();
                        byte[] dummy_block_header_len_bytes = BitConverter.GetBytes(dummy_block_header_bytes.Length);

                        fs.Write(dummy_block_header_len_bytes, 0, dummy_block_header_len_bytes.Length);
                        fs.Write(dummy_block_header_bytes, 0, dummy_block_header_bytes.Length);

                        byte[] dummy_block_num_bytes = BitConverter.GetBytes(dummy_block_header.blockNum);
                        fs.Write(dummy_block_num_bytes, 0, dummy_block_num_bytes.Length);
                    }

                    byte[] block_header_bytes     = block_header.getBytes();
                    byte[] block_header_len_bytes = BitConverter.GetBytes(block_header_bytes.Length);

                    fs.Write(block_header_len_bytes, 0, block_header_len_bytes.Length);
                    fs.Write(block_header_bytes, 0, block_header_bytes.Length);

                    byte[] block_num_bytes = BitConverter.GetBytes(block_header.blockNum);
                    fs.Write(block_num_bytes, 0, block_num_bytes.Length);

                    lastBlockHeaderTime = Clock.getTimestamp();
                }
                catch (Exception e)
                {
                    Logging.error("Exception occured while saving block header: {0}", e);
                    return(false);
                }

                return(true);
            }
        }