Skip to content
Browse files

rewriting tree struture with intervals

  • Loading branch information...
1 parent 5f8b389 commit d39a40c54b20e72c71a4a73f72ff8a0133421032 @grondilu committed Jun 22, 2012
Showing with 87 additions and 44 deletions.
  1. +87 −44 Bitcoin/Database/mysql-schema.sql
View
131 Bitcoin/Database/mysql-schema.sql
@@ -1,7 +1,8 @@
-CREATE TABLE block (
+-- Table "blocks" actually only contains block headers
+CREATE TABLE blocks (
hash char(32) binary primary key,
- version integer,
+ version integer default 1,
hashPrev char(32) binary not null,
hashMerkleRoot char(32) binary not null,
nTime integer unsigned not null,
@@ -12,12 +13,44 @@ CREATE TABLE block (
key (hashPrev)
);
-CREATE TABLE transaction (
+-- We'll insert the genesis block here as some triggers won't behave well
+-- with an empty 'blocks' table.
+INSERT INTO blocks values (
+ unhex("6FE28C0AB6F1B372C1A6A246AE63F74F931E8365E15A089C68D6190000000000"),
+
+ 1,
+ unhex("0000000000000000000000000000000000000000000000000000000000000000"),
+ unhex("3BA3EDFD7A7B12B27AC72C3E67768F617FC81BC3888A51323A9FB8AA4B1E5E4A"),
+ 1231006505,
+ 486604799,
+ 2083236893
+);
+
+-- A view of orphan blocks
+-- (notice that we don't use any "view" prefix in the name here)
+CREATE VIEW orphan_blocks AS
+SELECT a.*
+FROM blocks a LEFT JOIN blocks b
+ON a.hashPrev = b.hash
+WHERE b.hash IS NULL;
+
+-- Merkle transaction trees are stored in their own table
+CREATE TABLE Merkle_trees (
+ root char(32) binary not null,
+ idx integer unsigned not null,
+ hash char(32) binary,
+ primary key (root, idx),
+ key (root)
+);
+
+-- Transactions
+CREATE TABLE transactions (
hash char(32) binary primary key,
version integer,
lockTime integer unsigned,
);
+-- Transaction inputs
CREATE TABLE tx_in (
hash char(32) binary,
prevout_hash char(32) binary,
@@ -29,6 +62,7 @@ CREATE TABLE tx_in (
key(hash)
)
+-- Transaction outputs
CREATE TABLE tx_out (
tx_out_id integer unsigned primary key auto_increment,
hash char(32) binary,
@@ -38,23 +72,9 @@ CREATE TABLE tx_out (
key (hash)
);
-CREATE TABLE Merkle_tree (
- root char(32) binary not null,
- hash char(32) binary,
- idx integer unsigned not null,
- primary key (root, idx),
- key (root)
-);
-
-CREATE TABLE chain (
- hash char(32) binary,
- parent char(32) binary,
- distance integer unsigned,
- work float ,
- PRIMARY KEY (hash, parent)
-);
-
-CREATE VIEW view_block AS
+-- hashes are stored in binary so a few views are needed
+-- to get a human-readable format
+CREATE VIEW view_blocks AS
SELECT
HEX(hash) as hash,
version,
@@ -63,36 +83,59 @@ HEX(hashMerkleRoot) as hashMerkleRoot,
nTime,
nBits,
nNonce
-FROM block;
+FROM blocks;
-CREATE VIEW view_merkle_tree AS
-SELECT
-HEX(root) as root,
-HEX(hash) as hash,
-idx
-FROM Merkle_tree;
+CREATE VIEW view_orphan_blocks AS
+SELECT view_blocks.*
+FROM view_blocks INNER JOIN orphan_blocks
+ON view_blocks.hash = HEX(orphan_blocks.hash);
-CREATE VIEW view_chain AS
-SELECT
-HEX(hash) as hash,
-HEX(parent) as parent,
-distance,
-work
-FROM chain;
+CREATE VIEW view_Merkle_trees AS
+SELECT HEX(root) as root, HEX(hash) as hash, idx
+FROM Merkle_trees;
+-- A function to compute target from nBits
CREATE FUNCTION target (bits float)
RETURNS REAL DETERMINISTIC
RETURN mod(bits, 0x1000000) * pow( 256, bits div 0x1000000 - 3 );
-CREATE TRIGGER trigger_chain BEFORE INSERT ON block
-FOR EACH ROW BEGIN
- SET @newWork = 256 - log2(target(new.nBits));
- INSERT INTO chain (hash, parent, distance, work)
- SELECT new.hash, new.hash, 0, @newWork;
+-- To create the block tree structure,
+-- we'll use the interval model.
+-- Each node (i.e. each block) will have a left
+-- and a right edge. We must ensure that descending
+-- blocks have edges inside its parent's edges.
+
+CREATE TABLE block_tree (
+ node char(32) binary primary key,
+ L integer unsigned not null,
+ R integer unsigned not null check (R > L),
+ height integer unsigned not null
+);
+
+-- We insert the genesis node manually.
+-- Left edge is 0, right edge is 1, height is 0.
+INSERT INTO block_tree values (
+ unhex("6FE28C0AB6F1B372C1A6A246AE63F74F931E8365E15A089C68D6190000000000"),
+ 0,
+ 1,
+ 0
+);
- INSERT INTO chain (hash, parent, distance, work)
- SELECT new.hash, parent, distance + 1, work + @newWork
- FROM chain WHERE hash = new.hashPrev;
+CREATE TRIGGER add_block_in_tree AFTER INSERT ON blocks
+FOR EACH ROW
+BEGIN
+ UPDATE block_tree t, block_tree r
+ SET t.L=t.L+2
+ WHERE r.node = new.hashPrev
+ AND t.L >= r.D
+
+ UPDATE block_tree t, block_tree r
+ SET t.D=t.D+2
+ WHERE r.node = new.hashPrev
+ AND t.D >= r.D
+
+ INSERT INTO block_tree (node, L, R, height)
+ SELECT new.hash, r.D, r.D + 1, r.height + 1
+ FROM block_tree r
+ WHERE r.node = new.hashPrev
END;
-
-# vim: ft=mysql

0 comments on commit d39a40c

Please sign in to comment.
Something went wrong with that request. Please try again.