using SQLiteNetExtensions.Attributes;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
+using System.Text;
namespace Novacoin
{
- /// <summary>
- /// Block headers table
- /// </summary>
[Table("BlockStorage")]
- public class CBlockStoreItem
+ public class CBlockStoreItem : IBlockStorageItem
{
+ #region IBlockStorageItem
/// <summary>
/// Item ID in the database
/// </summary>
/// <summary>
/// Version of block schema
/// </summary>
+ [Column("nVersion")]
public uint nVersion { get; set; }
/// <summary>
/// Previous block hash.
/// </summary>
+ [Column("prevHash")]
public byte[] prevHash { get; set; }
/// <summary>
/// Merkle root hash.
/// </summary>
+ [Column("merkleRoot")]
public byte[] merkleRoot { get; set; }
/// <summary>
/// Block timestamp.
/// </summary>
+ [Column("nTime")]
public uint nTime { get; set; }
/// <summary>
/// Compressed difficulty representation.
/// </summary>
+ [Column("nBits")]
public uint nBits { get; set; }
/// <summary>
/// Nonce counter.
/// </summary>
+ [Column("nNonce")]
public uint nNonce { get; set; }
/// <summary>
/// Next block hash.
/// </summary>
+ [Column("nextHash")]
public byte[] nextHash { get; set; }
/// <summary>
/// Block type flags
/// </summary>
+ [Column("BlockTypeFlag")]
public BlockType BlockTypeFlag { get; set; }
/// <summary>
/// Stake modifier
/// </summary>
+ [Column("nStakeModifier")]
public long nStakeModifier { get; set; }
/// <summary>
/// Proof-of-Stake hash
/// </summary>
+ [Column("hashProofOfStake")]
public byte[] hashProofOfStake { get; set; }
/// <summary>
/// Stake generation outpoint.
/// </summary>
+ [Column("prevoutStake")]
public byte[] prevoutStake { get; set; }
/// <summary>
/// Stake generation time.
/// </summary>
+ [Column("nStakeTime")]
public uint nStakeTime { get; set; }
-
+
+ /// <summary>
+ /// Block height, encoded in VarInt format
+ /// </summary>
+ [Column("Height")]
+ public byte[] Height { get; set; }
+
/// <summary>
- /// Block height
+ /// Block position in file, encoded in VarInt format
/// </summary>
- public uint nHeight { get; set; }
+ [Column("BlockPos")]
+ public byte[] BlockPos { get; set; }
/// <summary>
- /// Block position in file
+ /// Block size in bytes, encoded in VarInt format
/// </summary>
- public long nBlockPos { get; set; }
+ [Column("BlockSize")]
+ public byte[] BlockSize { get; set; }
+ #endregion
/// <summary>
- /// Block size in bytes
+ /// Accessor and mutator for BlockPos value.
/// </summary>
- public int nBlockSize { get; set; }
+ [Ignore]
+ public long nBlockPos
+ {
+ get { return (long)VarInt.DecodeVarInt(BlockPos); }
+ set { BlockPos = VarInt.EncodeVarInt(value); }
+ }
+
+ /// <summary>
+ /// Accessor and mutator for BlockSize value.
+ /// </summary>
+ [Ignore]
+ public int nBlockSize
+ {
+ get { return (int)VarInt.DecodeVarInt(BlockSize); }
+ set { BlockSize = VarInt.EncodeVarInt(value); }
+ }
+
+ /// <summary>
+ /// Accessor and mutator for Height value.
+ /// </summary>
+ [Ignore]
+ public uint nHeight
+ {
+ get { return (uint)VarInt.DecodeVarInt(Height); }
+ set { Height = VarInt.EncodeVarInt(value); }
+ }
+
/// <summary>
/// Fill database item with data from given block header.
/// <returns>Header hash</returns>
public uint256 FillHeader(CBlockHeader header)
{
- uint256 _hash;
- Hash = _hash = header.Hash;
+ uint256 _hash = header.Hash;
+
+ Hash = _hash;
nVersion = header.nVersion;
prevHash = header.prevHash;
[Ignore]
public CBlockStoreItem next
{
- get { return CBlockStore.Instance.GetCursor(nextHash); }
+ get
+ {
+ if (nextHash == null)
+ {
+ return null;
+ }
+
+ return CBlockStore.Instance.GetCursor(nextHash);
+ }
set
{
CBlockStoreItem newCursor = this;
/// <summary>
/// Transaction type.
/// </summary>
- public enum TxType
+ public enum TxFlags : byte
{
TX_COINBASE,
TX_COINSTAKE,
}
/// <summary>
- /// Transaction type.
+ /// Output flags.
/// </summary>
- public enum OutputType
+ public enum OutputFlags : byte
{
- TX_USER = (1 << 0), // User output
- TX_COINBASE = (1 << 1), // Coinbase output
- TX_COINSTAKE = (1 << 2), // Coinstake output
- TX_AVAILABLE = (2 << 0), // Unspent output
- TX_SPENT = (2 << 1) // Spent output
+ AVAILABLE, // Unspent output
+ SPENT // Spent output
}
[Table("MerkleNodes")]
- public class MerkleNode
+ public class CMerkleNode : IMerkleNode
{
+ #region IMerkleNode
+ /// <summary>
+ /// Node identifier
+ /// </summary>
[PrimaryKey, AutoIncrement]
public long nMerkleNodeID { get; set; }
public long nParentBlockID { get; set; }
/// <summary>
- /// Transaction hash
- /// </summary>
- public byte[] TransactionHash { get; set; }
-
- public static bool QueryParentBlockCursor(uint256 transactionHash, out CBlockStoreItem cursor)
- {
- throw new NotImplementedException();
- }
- }
-
- [Table("Outputs")]
- public class TxOutItem
- {
- /// <summary>
- /// Link the transaction hash with database item identifier.
- /// </summary>
- private static ConcurrentDictionary<uint256, long> outMap = new ConcurrentDictionary<uint256, long>();
-
- /// <summary>
- /// Reference to transaction item.
- /// </summary>
- [ForeignKey(typeof(MerkleNode), Name = "nMerkleNodeID")]
- public long nMerkleNodeID { get; set; }
-
- /// <summary>
- /// Output flags
+ /// Transaction type flag
/// </summary>
- public OutputType outputFlags { get; set; }
+ [Column("TransactionFlags")]
+ public TxFlags TransactionFlags { get; set; }
/// <summary>
- /// Output number in VarInt format.
+ /// Transaction hash
/// </summary>
- public byte[] OutputNumber { get; set; }
+ [Column("TransactionHash")]
+ public byte[] TransactionHash { get; set; }
/// <summary>
- /// Output value in VarInt format.
+ /// Transaction offset from the beginning of block header, encoded in VarInt format.
/// </summary>
- public byte[] OutputValue { get; set; }
+ [Column("TxOffset")]
+ public byte[] TxOffset { get; set; }
/// <summary>
- /// Second half of script which contains spending instructions.
+ /// Transaction size, encoded in VarInt format.
/// </summary>
- public byte[] scriptPubKey { get; set; }
+ [Column("TxSize")]
+ public byte[] TxSize { get; set; }
+ #endregion
/// <summary>
- /// Construct new item from provided transaction data.
+ /// Read transaction from file.
/// </summary>
- /// <param name="o"></param>
- public TxOutItem(CTransaction tx, uint nOut)
+ /// <param name="reader">Stream with read access.</param>
+ /// <param name="tx">CTransaction reference.</param>
+ /// <returns>Result</returns>
+ public bool ReadFromFile(ref Stream reader, long nBlockPos, out CTransaction tx)
{
- Contract.Requires<ArgumentException>(nOut < tx.vout.Length);
+ var buffer = new byte[CTransaction.nMaxTxSize];
+
+ tx = null;
- long nMerkleId = 0;
- if (!outMap.TryGetValue(tx.Hash, out nMerkleId))
+ try
{
- // Not in the blockchain
- nMerkleNodeID = -1;
- }
+ reader.Seek(nBlockPos + nTxOffset, SeekOrigin.Begin); // Seek to transaction offset
- OutputNumber = VarInt.EncodeVarInt(nOut);
- OutputValue = VarInt.EncodeVarInt(tx.vout[nOut].nValue);
- scriptPubKey = tx.vout[nOut].scriptPubKey;
+ if (nTxSize != reader.Read(buffer, 0, nTxSize))
+ {
+ return false;
+ }
+
+ tx = new CTransaction(buffer);
- if (tx.IsCoinBase)
+ return true;
+ }
+ catch (IOException)
{
- outputFlags |= OutputType.TX_COINBASE;
+ // I/O error
+ return false;
}
- else if (tx.IsCoinStake)
+ catch (TransactionConstructorException)
{
- outputFlags |= OutputType.TX_COINSTAKE;
+ // Constructor error
+ return false;
}
}
/// <summary>
- /// Getter for output number.
- /// </summary>
- [Ignore]
- public uint nOut
- {
- get { return (uint)VarInt.DecodeVarInt(OutputNumber); }
- }
-
- /// <summary>
- /// Getter for output value.
+ /// Transaction offset accessor
/// </summary>
[Ignore]
- public ulong nValue
+ public long nTxOffset
{
- get { return VarInt.DecodeVarInt(OutputValue); }
+ get { return (long) VarInt.DecodeVarInt(TxOffset); }
}
/// <summary>
- /// Is this a user transaction output?
+ /// Transaction size accessor
/// </summary>
[Ignore]
- public bool IsUser
+ public int nTxSize
{
- get { return (outputFlags & OutputType.TX_USER) != 0; }
+ get { return (int)VarInt.DecodeVarInt(TxSize); }
}
- /// <summary>
- /// Is this a coinbase transaction output?
- /// </summary>
- [Ignore]
- public bool IsCoinBase
- {
- get { return (outputFlags & OutputType.TX_COINBASE) != 0; }
- }
-
- /// <summary>
- /// Is this a coinstake transaction output?
- /// </summary>
- [Ignore]
- public bool IsCoinStake
- {
- get { return (outputFlags & OutputType.TX_COINSTAKE) != 0; }
- }
-
- /// <summary>
- /// Getter ans setter for IsSpent flag.
- /// </summary>
- [Ignore]
- public bool IsSpent
- {
- get { return (outputFlags & OutputType.TX_SPENT) != 0; }
- set { outputFlags |= value ? OutputType.TX_SPENT : OutputType.TX_AVAILABLE; }
- }
}
-
- [Table("TransactionStorage")]
- public class CTransactionStoreItem
+ [Table("Outputs")]
+ public class TxOutItem : ITxOutItem
{
/// <summary>
- /// Transaction hash
+ /// Reference to transaction item.
/// </summary>
- [PrimaryKey]
- public byte[] TransactionHash { get; set; }
+ [ForeignKey(typeof(CMerkleNode), Name = "nMerkleNodeID")]
+ public long nMerkleNodeID { get; set; }
/// <summary>
- /// Block hash
+ /// Output flags
/// </summary>
- [ForeignKey(typeof(CBlockStoreItem), Name = "Hash")]
- public byte[] BlockHash { get; set; }
+ public OutputFlags outputFlags { get; set; }
/// <summary>
- /// Transaction type flag
+ /// Output number in VarInt format.
/// </summary>
- public TxType txType { get; set; }
+ public byte[] OutputNumber { get; set; }
/// <summary>
- /// Tx position in file
+ /// Output value in VarInt format.
/// </summary>
- public long nTxPos { get; set; }
+ public byte[] OutputValue { get; set; }
/// <summary>
- /// Transaction size
+ /// Second half of script which contains spending instructions.
/// </summary>
- public int nTxSize { get; set; }
+ public byte[] scriptPubKey { get; set; }
/// <summary>
- /// Serialized output array
+ /// Getter for output number.
/// </summary>
- public byte[] vOut { get; set; }
+ public uint nOut
+ {
+ get { return (uint)VarInt.DecodeVarInt(OutputNumber); }
+ }
/// <summary>
- /// Read transaction from file.
+ /// Getter for output value.
/// </summary>
- /// <param name="reader">Stream with read access.</param>
- /// <param name="tx">CTransaction reference.</param>
- /// <returns>Result</returns>
- public bool ReadFromFile(ref Stream reader, out CTransaction tx)
+ public ulong nValue
{
- var buffer = new byte[CTransaction.nMaxTxSize];
- tx = null;
-
- try
- {
- reader.Seek(nTxPos, SeekOrigin.Begin); // Seek to transaction offset
-
- if (nTxSize != reader.Read(buffer, 0, nTxSize))
- {
- return false;
- }
-
- tx = new CTransaction(buffer);
-
- return true;
- }
- catch (IOException)
- {
- // I/O error
- return false;
- }
- catch (TransactionConstructorException)
- {
- // Constructor error
- return false;
- }
+ get { return VarInt.DecodeVarInt(OutputValue); }
}
/// <summary>
- /// Outputs array access
+ /// Getter ans setter for IsSpent flag.
/// </summary>
- [Ignore]
- public CTxOut[] Outputs {
- get { return CTxOut.DeserializeOutputsArray(vOut); }
- set { vOut = CTxOut.SerializeOutputsArray(value); }
+ public bool IsSpent
+ {
+ get { return (outputFlags & OutputFlags.SPENT) != 0; }
+ set { outputFlags |= value ? OutputFlags.SPENT : OutputFlags.AVAILABLE; }
}
}
/// <summary>
/// Map of block tree nodes.
+ ///
+ /// blockHash => CBlockStoreItem
/// </summary>
private ConcurrentDictionary<uint256, CBlockStoreItem> blockMap = new ConcurrentDictionary<uint256, CBlockStoreItem>();
private ConcurrentDictionary<uint256, CBlock> orphanMapByPrev = new ConcurrentDictionary<uint256, CBlock>();
/// <summary>
- /// Map of unspent items.
+ /// Unconfirmed transactions.
+ ///
+ /// TxID => Transaction
/// </summary>
- private ConcurrentDictionary<uint256, CTransactionStoreItem> txMap = new ConcurrentDictionary<uint256, CTransactionStoreItem>();
-
+ private ConcurrentDictionary<uint256, CTransaction> mapUnconfirmedTx = new ConcurrentDictionary<uint256, CTransaction>();
+
/// <summary>
/// Map of the proof-of-stake hashes. This is necessary for stake duplication checks.
/// </summary>
private ConcurrentDictionary<COutPoint, uint> mapStakeSeenOrphan = new ConcurrentDictionary<COutPoint, uint>();
/// <summary>
- /// Unconfirmed transactions.
- /// </summary>
- private ConcurrentDictionary<uint256, CTransaction> mapUnconfirmedTx = new ConcurrentDictionary<uint256, CTransaction>();
-
- /// <summary>
/// Trust score for the longest chain.
/// </summary>
private uint256 nBestChainTrust = 0;
{
// Create tables
dbConn.CreateTable<CBlockStoreItem>(CreateFlags.AutoIncPK);
- dbConn.CreateTable<CTransactionStoreItem>(CreateFlags.ImplicitPK);
+ dbConn.CreateTable<CMerkleNode>(CreateFlags.AutoIncPK);
+ dbConn.CreateTable<TxOutItem>(CreateFlags.ImplicitPK);
var genesisBlock = new CBlock(
Interop.HexToArray(
}
}
- public bool GetTransaction(uint256 TxID, ref CTransaction tx)
+ public bool GetTxOutCursor(COutPoint outpoint, ref TxOutItem txOutCursor)
{
- var reader = new BinaryReader(fStreamReadWrite).BaseStream;
- var QueryTx = dbConn.Query<CTransactionStoreItem>("select * from [TransactionStorage] where [TransactionHash] = ?", (byte[])TxID);
+ var queryResults = dbConn.Query<TxOutItem>("select o.* from [Outputs] o left join [MerkleNodes] m on (m.nMerkleNodeID = o.nMerkleNodeID) where m.[TransactionHash] = ?", (byte[])outpoint.hash);
- if (QueryTx.Count == 1)
+ if (queryResults.Count == 1)
{
- return QueryTx[0].ReadFromFile(ref reader, out tx);
+ txOutCursor = queryResults[0];
+
+ return true;
}
// Tx not found
return false;
}
+ public bool FetchInputs(ref CTransaction tx, ref Dictionary<uint256, TxOutItem> queued, out TxOutItem[] inputs, bool IsBlock, out bool Invalid)
+ {
+ Invalid = true;
+ inputs = null;
+
+ StringBuilder queryBuilder = new StringBuilder();
+
+ queryBuilder.Append("select o.* from [Outputs] o left join [MerkleNodes] m on (m.[nMerkleNodeID] = o.[nMerkleNodeID]) where ");
+
+ for (var i = 0; i < tx.vin.Length; i++)
+ {
+ queryBuilder.AppendFormat(" {0} (m.[TransactionHash] = x'{1}' and o.[OutputNumber] = x'{2}')",
+ (i > 0 ? "or" : string.Empty), Interop.ToHex(tx.vin[i].prevout.hash),
+ Interop.ToHex(VarInt.EncodeVarInt(tx.vin[i].prevout.n)
+ ));
+ }
+
+ var queryResults = dbConn.Query<TxOutItem>(queryBuilder.ToString());
+
+ if (queryResults.Count < tx.vin.Length)
+ {
+ // It seems than some transactions are being spent in the same block.
+
+ if (IsBlock)
+ {
+
+ }
+ else
+ {
+ // TODO: use mapUnconfirmed
+
+ return false;
+ }
+ }
+
+ inputs = queryResults.ToArray();
+ Invalid = false;
+
+ return true;
+ }
+
private bool AddItemToIndex(ref CBlockStoreItem itemTemplate, ref CBlock block)
{
var writer = new BinaryWriter(fStreamReadWrite).BaseStream;
*/
}
- // We have no SetBestChain and ConnectBlock/Disconnect block yet, so adding these transactions manually.
- for (int i = 0; i < block.vtx.Length; i++)
- {
- // Handle trasactions using our temporary stub algo
-
- if (!block.vtx[i].VerifyScripts())
- {
- return false;
- }
-
- var nTxOffset = itemTemplate.nBlockPos + block.GetTxOffset(i);
- TxType txnType = TxType.TX_USER;
-
- if (block.vtx[i].IsCoinBase)
- {
- txnType = TxType.TX_COINBASE;
- }
- else if (block.vtx[i].IsCoinStake)
- {
- txnType = TxType.TX_COINSTAKE;
- }
-
- var NewTxItem = new CTransactionStoreItem()
- {
- TransactionHash = block.vtx[i].Hash,
- BlockHash = blockHash,
- nTxPos = nTxOffset,
- nTxSize = block.vtx[i].Size,
- txType = txnType
- };
-
- dbConn.Insert(NewTxItem);
- }
-
return true;
}
return false;
}
+
+ /// <summary>
+ /// Interface for join
+ /// </summary>
+ interface IBlockJoinMerkle : IBlockStorageItem, IMerkleNode
+ {
+ }
+
+ /// <summary>
+ /// Get block and transaction by transaction hash.
+ /// </summary>
+ /// <param name="TxID">Transaction hash</param>
+ /// <param name="block">Block reference</param>
+ /// <param name="tx">Transaction reference</param>
+ /// <param name="nBlockPos">Block position reference</param>
+ /// <param name="nTxPos">Transaction position reference</param>
+ /// <returns>Result of operation</returns>
public bool GetBlockByTransactionID(uint256 TxID, ref CBlock block, ref CTransaction tx, ref long nBlockPos, ref long nTxPos)
{
- var QueryTx = dbConn.Query<CTransactionStoreItem>("select * from [TransactionStorage] where [TransactionHash] = ?", (byte[])TxID);
+ var queryResult = dbConn.Query<IBlockJoinMerkle>("select *, from [BlockStorage] b left join [MerkleNodes] m on (b.[ItemID] = m.[nParentBlockID]) where m.[TransactionHash] = ?", (byte[])TxID);
- if (QueryTx.Count == 1)
+ if (queryResult.Count == 1)
{
- nTxPos = QueryTx[0].nTxPos;
- return GetBlock(QueryTx[0].BlockHash, ref block, ref nBlockPos);
+ CBlockStoreItem blockCursor = (CBlockStoreItem) queryResult[0];
+ CMerkleNode txCursor = (CMerkleNode)queryResult[0];
+
+ var reader = new BinaryReader(fStreamReadWrite).BaseStream;
+
+ if (!txCursor.ReadFromFile(ref reader, blockCursor.nBlockPos, out tx))
+ {
+ return false; // Unable to read transaction
+ }
+
+ return blockCursor.ReadFromFile(ref reader, out block);
}
// Tx not found