Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

rewriting tree struture with intervals

  • Loading branch information...
commit d39a40c54b20e72c71a4a73f72ff8a0133421032 1 parent 5f8b389
grondilu authored

Showing 1 changed file with 87 additions and 44 deletions. Show diff stats Hide diff stats

  1. +87 44 Bitcoin/Database/mysql-schema.sql
131 Bitcoin/Database/mysql-schema.sql
... ... @@ -1,7 +1,8 @@
1   -CREATE TABLE block (
  1 +-- Table "blocks" actually only contains block headers
  2 +CREATE TABLE blocks (
2 3 hash char(32) binary primary key,
3 4
4   - version integer,
  5 + version integer default 1,
5 6 hashPrev char(32) binary not null,
6 7 hashMerkleRoot char(32) binary not null,
7 8 nTime integer unsigned not null,
@@ -12,12 +13,44 @@ CREATE TABLE block (
12 13 key (hashPrev)
13 14 );
14 15
15   -CREATE TABLE transaction (
  16 +-- We'll insert the genesis block here as some triggers won't behave well
  17 +-- with an empty 'blocks' table.
  18 +INSERT INTO blocks values (
  19 + unhex("6FE28C0AB6F1B372C1A6A246AE63F74F931E8365E15A089C68D6190000000000"),
  20 +
  21 + 1,
  22 + unhex("0000000000000000000000000000000000000000000000000000000000000000"),
  23 + unhex("3BA3EDFD7A7B12B27AC72C3E67768F617FC81BC3888A51323A9FB8AA4B1E5E4A"),
  24 + 1231006505,
  25 + 486604799,
  26 + 2083236893
  27 +);
  28 +
  29 +-- A view of orphan blocks
  30 +-- (notice that we don't use any "view" prefix in the name here)
  31 +CREATE VIEW orphan_blocks AS
  32 +SELECT a.*
  33 +FROM blocks a LEFT JOIN blocks b
  34 +ON a.hashPrev = b.hash
  35 +WHERE b.hash IS NULL;
  36 +
  37 +-- Merkle transaction trees are stored in their own table
  38 +CREATE TABLE Merkle_trees (
  39 + root char(32) binary not null,
  40 + idx integer unsigned not null,
  41 + hash char(32) binary,
  42 + primary key (root, idx),
  43 + key (root)
  44 +);
  45 +
  46 +-- Transactions
  47 +CREATE TABLE transactions (
16 48 hash char(32) binary primary key,
17 49 version integer,
18 50 lockTime integer unsigned,
19 51 );
20 52
  53 +-- Transaction inputs
21 54 CREATE TABLE tx_in (
22 55 hash char(32) binary,
23 56 prevout_hash char(32) binary,
@@ -29,6 +62,7 @@ CREATE TABLE tx_in (
29 62 key(hash)
30 63 )
31 64
  65 +-- Transaction outputs
32 66 CREATE TABLE tx_out (
33 67 tx_out_id integer unsigned primary key auto_increment,
34 68 hash char(32) binary,
@@ -38,23 +72,9 @@ CREATE TABLE tx_out (
38 72 key (hash)
39 73 );
40 74
41   -CREATE TABLE Merkle_tree (
42   - root char(32) binary not null,
43   - hash char(32) binary,
44   - idx integer unsigned not null,
45   - primary key (root, idx),
46   - key (root)
47   -);
48   -
49   -CREATE TABLE chain (
50   - hash char(32) binary,
51   - parent char(32) binary,
52   - distance integer unsigned,
53   - work float ,
54   - PRIMARY KEY (hash, parent)
55   -);
56   -
57   -CREATE VIEW view_block AS
  75 +-- hashes are stored in binary so a few views are needed
  76 +-- to get a human-readable format
  77 +CREATE VIEW view_blocks AS
58 78 SELECT
59 79 HEX(hash) as hash,
60 80 version,
@@ -63,36 +83,59 @@ HEX(hashMerkleRoot) as hashMerkleRoot,
63 83 nTime,
64 84 nBits,
65 85 nNonce
66   -FROM block;
  86 +FROM blocks;
67 87
68   -CREATE VIEW view_merkle_tree AS
69   -SELECT
70   -HEX(root) as root,
71   -HEX(hash) as hash,
72   -idx
73   -FROM Merkle_tree;
  88 +CREATE VIEW view_orphan_blocks AS
  89 +SELECT view_blocks.*
  90 +FROM view_blocks INNER JOIN orphan_blocks
  91 +ON view_blocks.hash = HEX(orphan_blocks.hash);
74 92
75   -CREATE VIEW view_chain AS
76   -SELECT
77   -HEX(hash) as hash,
78   -HEX(parent) as parent,
79   -distance,
80   -work
81   -FROM chain;
  93 +CREATE VIEW view_Merkle_trees AS
  94 +SELECT HEX(root) as root, HEX(hash) as hash, idx
  95 +FROM Merkle_trees;
82 96
  97 +-- A function to compute target from nBits
83 98 CREATE FUNCTION target (bits float)
84 99 RETURNS REAL DETERMINISTIC
85 100 RETURN mod(bits, 0x1000000) * pow( 256, bits div 0x1000000 - 3 );
86 101
87   -CREATE TRIGGER trigger_chain BEFORE INSERT ON block
88   -FOR EACH ROW BEGIN
89   - SET @newWork = 256 - log2(target(new.nBits));
90   - INSERT INTO chain (hash, parent, distance, work)
91   - SELECT new.hash, new.hash, 0, @newWork;
  102 +-- To create the block tree structure,
  103 +-- we'll use the interval model.
  104 +-- Each node (i.e. each block) will have a left
  105 +-- and a right edge. We must ensure that descending
  106 +-- blocks have edges inside its parent's edges.
  107 +
  108 +CREATE TABLE block_tree (
  109 + node char(32) binary primary key,
  110 + L integer unsigned not null,
  111 + R integer unsigned not null check (R > L),
  112 + height integer unsigned not null
  113 +);
  114 +
  115 +-- We insert the genesis node manually.
  116 +-- Left edge is 0, right edge is 1, height is 0.
  117 +INSERT INTO block_tree values (
  118 + unhex("6FE28C0AB6F1B372C1A6A246AE63F74F931E8365E15A089C68D6190000000000"),
  119 + 0,
  120 + 1,
  121 + 0
  122 +);
92 123
93   - INSERT INTO chain (hash, parent, distance, work)
94   - SELECT new.hash, parent, distance + 1, work + @newWork
95   - FROM chain WHERE hash = new.hashPrev;
  124 +CREATE TRIGGER add_block_in_tree AFTER INSERT ON blocks
  125 +FOR EACH ROW
  126 +BEGIN
  127 + UPDATE block_tree t, block_tree r
  128 + SET t.L=t.L+2
  129 + WHERE r.node = new.hashPrev
  130 + AND t.L >= r.D
  131 +
  132 + UPDATE block_tree t, block_tree r
  133 + SET t.D=t.D+2
  134 + WHERE r.node = new.hashPrev
  135 + AND t.D >= r.D
  136 +
  137 + INSERT INTO block_tree (node, L, R, height)
  138 + SELECT new.hash, r.D, r.D + 1, r.height + 1
  139 + FROM block_tree r
  140 + WHERE r.node = new.hashPrev
96 141 END;
97   -
98   -# vim: ft=mysql

0 comments on commit d39a40c

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