BitSharper.Store.BoundedOverheadBlockStore.SetChainHead C# (CSharp) Method

SetChainHead() public method

public SetChainHead ( StoredBlock chainHead ) : void
chainHead StoredBlock
return void
        public void SetChainHead(StoredBlock chainHead)
        {
            lock (this)
            {
                try
                {
                    _chainHead = chainHead.Header.Hash;
                    // Write out new hash to the first 32 bytes of the file past one (first byte is version number).
                    var originalPos = _channel.Position;
                    _channel.Position = 1;
                    var bytes = _chainHead.Bytes;
                    _channel.Write(bytes, 0, bytes.Length);
                    _channel.Position = originalPos;
                }
                catch (IOException e)
                {
                    throw new BlockStoreException(e);
                }
            }
        }

Usage Example

        public void TestStorage()
        {
            var temp = new FileInfo(Path.GetTempFileName());
            try
            {
                Console.WriteLine(temp.FullName);
                var @params = NetworkParameters.UnitTests();
                var to = new EcKey().ToAddress(@params);
                StoredBlock b1;
                using (var store = new BoundedOverheadBlockStore(@params, temp))
                {
                    // Check the first block in a new store is the genesis block.
                    var genesis = store.GetChainHead();
                    Assert.AreEqual(@params.GenesisBlock, genesis.Header);

                    // Build a new block.
                    b1 = genesis.Build(genesis.Header.CreateNextBlock(to).CloneAsHeader());
                    store.Put(b1);
                    store.SetChainHead(b1);
                }
                // Check we can get it back out again if we rebuild the store object.
                using (var store = new BoundedOverheadBlockStore(@params, temp))
                {
                    var b2 = store.Get(b1.Header.Hash);
                    Assert.AreEqual(b1, b2);
                    // Check the chain head was stored correctly also.
                    Assert.AreEqual(b1, store.GetChainHead());
                }
            }
            finally
            {
                temp.Delete();
            }
        }