func (bc *Blockchain) AddToChain(hash, prevHash []byte) error {
	length := len(hash)
	if length == 0 {
		return NewHashEmptyDBError()
	}
	emptyHash := make([]byte, length)
	return bc.DB.db.Update(func(tx *bolt.Tx) error {
		b := tx.Bucket([]byte(blockChainBucket))
		if b == nil {
			return NewDBIsNotReadyError()
		}
		hashBytes := make([]byte, length*2)
		if len(prevHash) > 0 {
			prRecTmp := b.Get(prevHash)
			if len(prRecTmp) < length*2 {
				return NewHashNotFoundDBError("Previous hash is not found in the chain")
			}
			prRec := utils.CopyBytes(prRecTmp)
			exNext := make([]byte, length)
			copy(exNext, prRec[length:])
			if bytes.Compare(exNext, emptyHash) > 0 {
				return NewHashDBError("Previous hash already has a next hash")
			}
			copy(prRec[length:], hash)
			err := b.Put(prevHash, prRec)
			if err != nil {
				return err
			}
			copy(hashBytes[0:], prevHash)
		}
		return b.Put(hash, hashBytes)
	})
}
add block to chain
package database
import (
	"bytes"
	"github.com/boltdb/bolt"
	"github.com/gelembjuk/democoin/lib/utils"
)
const blocksBucket = "blocks"
const blockChainBucket = "blockchain"
type Blockchain struct {
	DB *BoltDB
}
func (bc *Blockchain) InitDB() error {
	err := bc.DB.db.Update(func(tx *bolt.Tx) error {
		_, err := tx.CreateBucket([]byte(blocksBucket))
		if err != nil {
			return err
		}
		_, err = tx.CreateBucket([]byte(blockChainBucket))
		if err != nil {
			return err
		}
		return nil
	})
	return err
}
func (bc *Blockchain) GetTopBlock() ([]byte, error) {
	topHash, err := bc.GetTopHash()
	if err != nil {
		return nil, err
	}
	return bc.GetBlock(topHash)
}
func (bc *Blockchain) GetBlock(hash []byte) ([]byte, error) {
	var blockData []byte
	err := bc.DB.db.View(func(tx *bolt.Tx) error {
		b := tx.Bucket([]byte(blocksBucket))
		if b == nil {
			return NewDBIsNotReadyError()
		}
		blockData = b.Get(hash)
		return nil
	})
	if err != nil {
		return nil, err
	}
	if len(blockData) > 0 {
		blockData = utils.CopyBytes(blockData)
	}
	return blockData, nil
}
func (bc *Blockchain) CheckBlockExists(hash []byte) (bool, error) {
	blockData, err := bc.GetBlock(hash)
	if err != nil {
		return false, err
	}
	if len(blockData) > 0 {
		return true, nil
	}
	return false, nil
}
func (bc *Blockchain) PutBlockOnTop(hash []byte, blockdata []byte) error {
	err := bc.PutBlock(hash, blockdata)
	if err != nil {
		return err
	}
	return bc.SaveTopHash(hash)
}
func (bc *Blockchain) PutBlock(hash []byte, blockdata []byte) error {
	err := bc.DB.db.Update(func(tx *bolt.Tx) error {
		b := tx.Bucket([]byte(blocksBucket))
		if b == nil {
			return NewDBIsNotReadyError()
		}
		return b.Put(hash, blockdata)
	})
	return err
}
func (bc *Blockchain) DeleteBlock(hash []byte) error {
	err := bc.DB.db.Update(func(tx *bolt.Tx) error {
		b := tx.Bucket([]byte(blocksBucket))
		if b == nil {
			return NewDBIsNotReadyError()
		}
		return b.Delete(hash)
	})
	return err
}
func (bc *Blockchain) SaveTopHash(hash []byte) error {
	err := bc.DB.db.Update(func(tx *bolt.Tx) error {
		b := tx.Bucket([]byte(blocksBucket))
		if b == nil {
			return NewDBIsNotReadyError()
		}
		return b.Put([]byte("l"), hash)
		return nil
	})
	return err
}
func (bc *Blockchain) GetTopHash() ([]byte, error) {
	var topHash []byte
	err := bc.DB.db.View(func(tx *bolt.Tx) error {
		b := tx.Bucket([]byte(blocksBucket))
		if b == nil {
			return NewDBIsNotReadyError()
		}
		topHash = b.Get([]byte("l"))
		return nil
	})
	if err != nil {
		return nil, err
	}
	if len(topHash) > 0 {
		topHash = utils.CopyBytes(topHash)
		return topHash, nil
	}
	return nil, NewNotFoundDBError("tophash")
}
func (bc *Blockchain) SaveFirstHash(hash []byte) error {
	err := bc.DB.db.Update(func(tx *bolt.Tx) error {
		b := tx.Bucket([]byte(blocksBucket))
		if b == nil {
			return NewDBIsNotReadyError()
		}
		return b.Put([]byte("f"), hash)
	})
	return err
}
func (bc *Blockchain) GetFirstHash() ([]byte, error) {
	var firstHash []byte
	err := bc.DB.db.View(func(tx *bolt.Tx) error {
		b := tx.Bucket([]byte(blocksBucket))
		if b == nil {
			return NewDBIsNotReadyError()
		}
		firstHash = b.Get([]byte("f"))
		return nil
	})
	if err != nil {
		return nil, err
	}
	if len(firstHash) > 0 {
		firstHash = utils.CopyBytes(firstHash)
		return firstHash, nil
	}
	return nil, NewNotFoundDBError("firsthash")
}