Skip to content

Commit

Permalink
These changes are a terrible, incredibly hacky solution to storing ou…
Browse files Browse the repository at this point in the history
…r fetched headers, I will revise.
  • Loading branch information
zv committed Jun 3, 2013
1 parent 8501df6 commit f91d26c
Showing 1 changed file with 80 additions and 18 deletions.
98 changes: 80 additions & 18 deletions blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ func (bh *BlockHandle) BlockSha() (btcwire.ShaHash, error) {
func (chain *BlockChain) Save(header *btcwire.BlockHeader) (*BlockHandle, error) {
parent, err := chain.Get(&header.PrevBlock)
if err != nil {
fmt.Errorf("No parent found for: %#v", header)
log.Fatal("No parent found for: %#v", header)
return nil, err
}
// save a string to store our 'real' block with
block_sha, err := header.BlockSha(btcwire.ProtocolVersion)
if err != nil {
fmt.Errorf("Something went wrong deriving the block hash for: %#v", header)
log.Fatal("Something went wrong deriving the block hash for: %#v", header)
return nil, err
}
bls_elems := []string{"blk", block_sha.String()}
Expand Down Expand Up @@ -89,20 +89,62 @@ func (chain *BlockChain) FindParent(bh *BlockHandle) (*BlockHandle, error) {
}

func InitializeBlockChain() (*BlockChain, error) {
bc := BlockChain{Last: btcwire.GenesisHash, ChainHead: btcwire.GenesisHash, Genesis: btcwire.GenesisHash}
gob.Register(btcwire.ShaHash{})
db, err := gocask.NewGocask("bitcoin")
bc.Database = db
if err != nil {
return nil, err
}

genesis := BlockHandle{Hash: btcwire.GenesisHash, Header: btcwire.GenesisBlock.Header}
var block_bytes bytes.Buffer
encoder := gob.NewEncoder(&block_bytes)
encoder.Encode(genesis)
err = bc.Database.Put(btcwire.GenesisHash.String(), block_bytes.Bytes())

return &bc, nil
chain_bytes, err := db.Get("chainhead")
if err == nil {
var bc BlockChain
bc.Database = db
head_hash, err := btcwire.NewShaHash(chain_bytes)
if err != nil {
return nil, err
}
bc.Last = *head_hash
bc.ChainHead = *head_hash
chainhead_handle, err := bc.Get(head_hash)
if err != nil {
log.Fatal("Couldn't derive Chain Head")
return nil, nil
}

bc.ChainHeadDepth = chainhead_handle.Depth
bc.Genesis = btcwire.GenesisHash
fmt.Printf("Loaded previous blockchain with a depth of: %d\n", bc.ChainHeadDepth)
return &bc, nil
} else {
bc := BlockChain{Last: btcwire.GenesisHash, ChainHead: btcwire.GenesisHash, Genesis: btcwire.GenesisHash}
err = db.Put("chainhead", btcwire.GenesisHash.Bytes())
if err != nil {
return nil, err
}
bc.Database = db

// save a string to store our 'real' block with
block_sha, err := btcwire.GenesisBlock.Header.BlockSha(btcwire.ProtocolVersion)
if err != nil {
log.Fatal("Something went wrong deriving the genesis block hash")
return nil, err
}
bls_elems := []string{"blk", block_sha.String()}
block_lookup_string := strings.Join(bls_elems, "")
block := BlockHandle{Hash: block_sha,
Header: btcwire.GenesisBlock.Header,
Depth: 0,
Block: block_lookup_string}
var block_bytes bytes.Buffer
encoder := gob.NewEncoder(&block_bytes)
encoder.Encode(block)
err = db.Put(block_sha.String(), block_bytes.Bytes())
if err != nil {
log.Fatal("Couldn't save Genesis Block Header")
return nil, err
}

// genesis := BlockHandle{Hash: btcwire.GenesisHash, Header: btcwire.GenesisBlock.Header}
return &bc, nil

}
return nil, nil
}

// create a block from a header we've read and add it to our blockchain
Expand All @@ -113,10 +155,30 @@ func (chain *BlockChain) InitializeBlock(header *btcwire.BlockHeader) (*BlockHan
return nil, err
}
// add a reference to our current chain head
if chain.ChainHeadDepth < block.Depth {
chain.ChainHead, _ = block.BlockSha()
chain.ChainHeadDepth = block.Depth
chain_head_bytes, err := chain.Database.Get("chainhead")
if err != nil {
return nil, err
}

chain_head_hash, err := btcwire.NewShaHash(chain_head_bytes)
if err != nil {
return nil, err
}

chain_head, err := chain.Get(chain_head_hash)
if err != nil {
return nil, err
}

if chain_head.Depth < block.Depth {
chain_head_sha, _ := block.BlockSha()
chain.ChainHead = chain_head_sha
// we should defer this to only write when we're about to exit because of the
// append only nature of bitcask
chain.Database.Put("chainhead", chain_head_sha.Bytes())
chain.ChainHeadDepth = block.Depth
}

return block, nil
}

Expand Down

0 comments on commit f91d26c

Please sign in to comment.