namespace Novacoin
{
+ [Table("ChainState")]
+ public class ChainState
+ {
+ [PrimaryKey, AutoIncrement]
+ public long itemId { get; set; }
+
+ /// <summary>
+ /// Hash of top block in the best chain
+ /// </summary>
+ public byte[] HashBestChain { get; set; }
+
+ /// <summary>
+ /// Total trust score of best chain
+ /// </summary>
+ public byte[] BestChainTrust { get; set; }
+
+ public uint nBestHeight { get; set;}
+
+ [Ignore]
+ public uint256 nBestChainTrust
+ {
+ get { return BestChainTrust; }
+ set { BestChainTrust = value; }
+ }
+
+ [Ignore]
+ public uint256 nHashBestChain
+ {
+ get { return HashBestChain; }
+ set { HashBestChain = value; }
+ }
+ }
+
[Table("BlockStorage")]
public class CBlockStoreItem : IBlockStorageItem
{
private ConcurrentDictionary<COutPoint, uint> mapStakeSeen = new ConcurrentDictionary<COutPoint, uint>();
private ConcurrentDictionary<COutPoint, uint> mapStakeSeenOrphan = new ConcurrentDictionary<COutPoint, uint>();
- /// <summary>
- /// Trust score for the longest chain.
- /// </summary>
- private uint256 nBestChainTrust = 0;
/// <summary>
- /// Top block of the best chain.
+ /// Copy of chain state object.
/// </summary>
- private uint256 nHashBestChain = 0;
+ private ChainState ChainParams;
/// <summary>
/// Cursor which is pointing us to the end of best chain.
/// Block file stream with read/write access
/// </summary>
private Stream fStreamReadWrite;
- private uint nBestHeight;
private uint nTimeBestReceived;
private int nTransactionsUpdated;
dbConn.CreateTable<CBlockStoreItem>(CreateFlags.AutoIncPK);
dbConn.CreateTable<CMerkleNode>(CreateFlags.AutoIncPK);
dbConn.CreateTable<TxOutItem>(CreateFlags.ImplicitPK);
+ dbConn.CreateTable<ChainState>(CreateFlags.AutoIncPK);
+
+ ChainParams = new ChainState()
+ {
+ nBestChainTrust = 0,
+ nBestHeight = 0,
+ nHashBestChain = 0
+ };
+
+ dbConn.Insert(ChainParams);
var genesisBlock = new CBlock(
Interop.HexToArray(
mapStakeSeen.TryAdd(item.prevoutStake, item.nStakeTime);
}
}
+
+ // Load data about the top node.
+ ChainParams = dbConn.Table<ChainState>().First();
}
}
return false; // blockMap add failed
}
- if (itemTemplate.nChainTrust > nBestChainTrust)
+ if (itemTemplate.nChainTrust > ChainParams.nBestChainTrust)
{
// New best chain
{
genesisBlockCursor = cursor;
}
- else if (nHashBestChain == (uint256)cursor.prevHash)
+ else if (ChainParams.nHashBestChain == (uint256)cursor.prevHash)
{
if (!SetBestChainInner(cursor))
{
}
}
- nHashBestChain = cursor.Hash;
bestBlockCursor = cursor;
- nBestHeight = cursor.nHeight;
- nBestChainTrust = cursor.nChainTrust;
nTimeBestReceived = Interop.GetTime();
nTransactionsUpdated++;
}
}
- if (!WriteHashBestChain(cursorIntermediate.Hash))
+ if (!UpdateTopChain(cursorIntermediate))
{
- return false; // WriteHashBestChain failed
+ return false; // UpdateTopChain failed
}
// Make sure it's successfully written to disk
}
// Adding to current best branch
- if (!ConnectBlock(cursor, ref block) || !WriteHashBestChain(hash))
+ if (!ConnectBlock(cursor, ref block) || !UpdateTopChain(cursor))
{
InvalidChainFound(cursor);
return false;
return false; // Unable to save outpoints
}
- // TODO: the remaining stuff lol :D
-
- throw new NotImplementedException();
+ return true;
}
/// <summary>
throw new NotImplementedException();
}
- private bool WriteHashBestChain(uint256 hash)
+ /// <summary>
+ /// Set new top node or current best chain.
+ /// </summary>
+ /// <param name="cursor"></param>
+ /// <returns></returns>
+ private bool UpdateTopChain(CBlockStoreItem cursor)
{
- throw new NotImplementedException();
+ ChainParams.HashBestChain = cursor.Hash;
+ ChainParams.nBestChainTrust = cursor.nChainTrust;
+ ChainParams.nBestHeight = cursor.nHeight;
+
+ return dbConn.Update(ChainParams) != 0;
}
/// <summary>