2021-06-04 21:18:06 +00:00
|
|
|
package blockchain
|
2021-05-14 20:32:49 +00:00
|
|
|
|
|
|
|
import (
|
2021-05-25 15:32:06 +00:00
|
|
|
"encoding/binary"
|
2021-05-14 20:32:49 +00:00
|
|
|
"encoding/hex"
|
|
|
|
"errors"
|
2021-07-11 00:32:58 +00:00
|
|
|
"os"
|
2021-05-14 20:32:49 +00:00
|
|
|
|
2021-06-04 21:18:06 +00:00
|
|
|
"github.com/Secured-Finance/dione/blockchain/utils"
|
|
|
|
|
2021-05-19 20:54:36 +00:00
|
|
|
types2 "github.com/Secured-Finance/dione/blockchain/types"
|
2021-05-14 20:32:49 +00:00
|
|
|
"github.com/fxamacker/cbor/v2"
|
2021-05-19 20:54:36 +00:00
|
|
|
|
2021-05-14 20:32:49 +00:00
|
|
|
"github.com/ledgerwatch/lmdb-go/lmdb"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-05-25 15:32:06 +00:00
|
|
|
DefaultBlockDataPrefix = "blockdata_"
|
2021-05-14 20:32:49 +00:00
|
|
|
DefaultBlockHeaderPrefix = "header_"
|
2021-05-25 15:32:06 +00:00
|
|
|
DefaultMetadataIndexName = "metadata"
|
|
|
|
LatestBlockHeightKey = "latest_block_height"
|
2021-05-14 20:32:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-05-25 15:32:06 +00:00
|
|
|
ErrBlockNotFound = errors.New("block isn't found")
|
|
|
|
ErrLatestHeightNil = errors.New("latest block height is nil")
|
2021-05-14 20:32:49 +00:00
|
|
|
)
|
|
|
|
|
2021-06-04 21:18:06 +00:00
|
|
|
type BlockChain struct {
|
2021-05-25 15:32:06 +00:00
|
|
|
dbEnv *lmdb.Env
|
|
|
|
db lmdb.DBI
|
2021-06-04 21:18:06 +00:00
|
|
|
metadataIndex *utils.Index
|
|
|
|
heightIndex *utils.Index
|
2021-05-14 20:32:49 +00:00
|
|
|
}
|
|
|
|
|
2021-06-04 21:18:06 +00:00
|
|
|
func NewBlockChain(path string) (*BlockChain, error) {
|
|
|
|
chain := &BlockChain{}
|
2021-05-14 20:32:49 +00:00
|
|
|
|
|
|
|
// configure lmdb env
|
|
|
|
env, err := lmdb.NewEnv()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-07-11 00:32:58 +00:00
|
|
|
err = env.SetMaxDBs(1)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-14 20:32:49 +00:00
|
|
|
err = env.SetMapSize(100 * 1024 * 1024 * 1024) // 100 GB
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-07-11 00:32:58 +00:00
|
|
|
err = os.MkdirAll(path, 0755)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = env.Open(path, 0, 0755)
|
2021-05-14 20:32:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-06-04 21:18:06 +00:00
|
|
|
chain.dbEnv = env
|
2021-05-14 20:32:49 +00:00
|
|
|
|
|
|
|
var dbi lmdb.DBI
|
|
|
|
err = env.Update(func(txn *lmdb.Txn) error {
|
|
|
|
dbi, err = txn.OpenDBI("blocks", lmdb.Create)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-06-04 21:18:06 +00:00
|
|
|
chain.db = dbi
|
2021-05-14 20:32:49 +00:00
|
|
|
|
2021-05-25 15:32:06 +00:00
|
|
|
// create index instances
|
2021-06-04 21:18:06 +00:00
|
|
|
metadataIndex := utils.NewIndex(DefaultMetadataIndexName, env, dbi)
|
|
|
|
heightIndex := utils.NewIndex("height", env, dbi)
|
|
|
|
chain.metadataIndex = metadataIndex
|
|
|
|
chain.heightIndex = heightIndex
|
2021-05-25 15:32:06 +00:00
|
|
|
|
2021-06-04 21:18:06 +00:00
|
|
|
return chain, nil
|
2021-05-14 20:32:49 +00:00
|
|
|
}
|
|
|
|
|
2021-06-04 21:18:06 +00:00
|
|
|
func (bp *BlockChain) setLatestBlockHeight(height uint64) error {
|
2021-05-25 15:32:06 +00:00
|
|
|
return bp.metadataIndex.PutUint64([]byte(LatestBlockHeightKey), height)
|
2021-05-19 21:53:58 +00:00
|
|
|
}
|
|
|
|
|
2021-06-04 21:18:06 +00:00
|
|
|
func (bp *BlockChain) GetLatestBlockHeight() (uint64, error) {
|
2021-05-25 15:32:06 +00:00
|
|
|
height, err := bp.metadataIndex.GetUint64([]byte(LatestBlockHeightKey))
|
|
|
|
if err != nil {
|
2021-06-04 21:18:06 +00:00
|
|
|
if err == utils.ErrIndexKeyNotFound {
|
2021-05-25 15:32:06 +00:00
|
|
|
return 0, ErrLatestHeightNil
|
2021-05-19 21:53:58 +00:00
|
|
|
}
|
2021-05-25 15:32:06 +00:00
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return height, nil
|
2021-05-19 21:53:58 +00:00
|
|
|
}
|
|
|
|
|
2021-06-04 21:18:06 +00:00
|
|
|
func (bp *BlockChain) StoreBlock(block *types2.Block) error {
|
2021-05-25 15:32:06 +00:00
|
|
|
err := bp.dbEnv.Update(func(txn *lmdb.Txn) error {
|
|
|
|
data, err := cbor.Marshal(block.Data)
|
2021-05-14 20:32:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
headerData, err := cbor.Marshal(block.Header)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
blockHash := hex.EncodeToString(block.Header.Hash)
|
2021-05-25 15:32:06 +00:00
|
|
|
err = txn.Put(bp.db, []byte(DefaultBlockDataPrefix+blockHash), data, 0)
|
2021-05-14 20:32:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = txn.Put(bp.db, []byte(DefaultBlockHeaderPrefix+blockHash), headerData, 0) // store header separately for easy fetching
|
|
|
|
return err
|
|
|
|
})
|
2021-05-25 15:32:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// update index "height -> block hash"
|
2021-07-11 00:32:58 +00:00
|
|
|
heightBytes := make([]byte, 8)
|
2021-05-25 15:32:06 +00:00
|
|
|
binary.LittleEndian.PutUint64(heightBytes, block.Header.Height)
|
|
|
|
err = bp.heightIndex.PutBytes(heightBytes, block.Header.Hash)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// update latest block height
|
|
|
|
height, err := bp.GetLatestBlockHeight()
|
|
|
|
if err != nil && err != ErrLatestHeightNil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == ErrLatestHeightNil {
|
2021-05-26 20:58:31 +00:00
|
|
|
if err = bp.setLatestBlockHeight(block.Header.Height); err != nil {
|
2021-05-25 15:32:06 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if block.Header.Height > height {
|
2021-05-26 20:58:31 +00:00
|
|
|
if err = bp.setLatestBlockHeight(block.Header.Height); err != nil {
|
2021-05-25 15:32:06 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2021-05-14 20:32:49 +00:00
|
|
|
}
|
|
|
|
|
2021-06-04 21:18:06 +00:00
|
|
|
func (bp *BlockChain) HasBlock(blockHash []byte) (bool, error) {
|
2021-05-14 20:32:49 +00:00
|
|
|
var blockExists bool
|
|
|
|
err := bp.dbEnv.View(func(txn *lmdb.Txn) error {
|
2021-05-25 15:32:06 +00:00
|
|
|
h := hex.EncodeToString(blockHash)
|
|
|
|
_, err := txn.Get(bp.db, []byte(DefaultBlockHeaderPrefix+h)) // try to fetch block header
|
2021-05-14 20:32:49 +00:00
|
|
|
if err != nil {
|
|
|
|
if lmdb.IsNotFound(err) {
|
|
|
|
blockExists = false
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
blockExists = true
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return blockExists, nil
|
|
|
|
}
|
|
|
|
|
2021-06-04 21:18:06 +00:00
|
|
|
func (bp *BlockChain) FetchBlockData(blockHash []byte) ([]*types2.Transaction, error) {
|
2021-05-25 15:32:06 +00:00
|
|
|
var data []*types2.Transaction
|
2021-05-14 20:32:49 +00:00
|
|
|
err := bp.dbEnv.View(func(txn *lmdb.Txn) error {
|
2021-05-25 15:32:06 +00:00
|
|
|
h := hex.EncodeToString(blockHash)
|
|
|
|
blockData, err := txn.Get(bp.db, []byte(DefaultBlockDataPrefix+h))
|
2021-05-14 20:32:49 +00:00
|
|
|
if err != nil {
|
|
|
|
if lmdb.IsNotFound(err) {
|
|
|
|
return ErrBlockNotFound
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2021-05-25 15:32:06 +00:00
|
|
|
err = cbor.Unmarshal(blockData, data)
|
2021-05-14 20:32:49 +00:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-25 15:32:06 +00:00
|
|
|
return data, nil
|
2021-05-14 20:32:49 +00:00
|
|
|
}
|
|
|
|
|
2021-06-04 21:18:06 +00:00
|
|
|
func (bp *BlockChain) FetchBlockHeader(blockHash []byte) (*types2.BlockHeader, error) {
|
2021-05-19 20:54:36 +00:00
|
|
|
var blockHeader types2.BlockHeader
|
2021-05-14 20:32:49 +00:00
|
|
|
err := bp.dbEnv.View(func(txn *lmdb.Txn) error {
|
2021-05-25 15:32:06 +00:00
|
|
|
h := hex.EncodeToString(blockHash)
|
|
|
|
data, err := txn.Get(bp.db, []byte(DefaultBlockHeaderPrefix+h))
|
2021-05-14 20:32:49 +00:00
|
|
|
if err != nil {
|
|
|
|
if lmdb.IsNotFound(err) {
|
|
|
|
return ErrBlockNotFound
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = cbor.Unmarshal(data, &blockHeader)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &blockHeader, nil
|
|
|
|
}
|
2021-05-25 15:32:06 +00:00
|
|
|
|
2021-06-04 21:18:06 +00:00
|
|
|
func (bp *BlockChain) FetchBlock(blockHash []byte) (*types2.Block, error) {
|
2021-05-25 15:32:06 +00:00
|
|
|
var block types2.Block
|
|
|
|
header, err := bp.FetchBlockHeader(blockHash)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
block.Header = header
|
|
|
|
|
|
|
|
data, err := bp.FetchBlockData(blockHash)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
block.Data = data
|
|
|
|
|
|
|
|
return &block, nil
|
|
|
|
}
|
|
|
|
|
2021-06-04 21:18:06 +00:00
|
|
|
func (bp *BlockChain) FetchBlockByHeight(height uint64) (*types2.Block, error) {
|
2021-05-25 15:32:06 +00:00
|
|
|
var heightBytes []byte
|
|
|
|
binary.LittleEndian.PutUint64(heightBytes, height)
|
|
|
|
blockHash, err := bp.heightIndex.GetBytes(heightBytes)
|
|
|
|
if err != nil {
|
2021-06-04 21:18:06 +00:00
|
|
|
if err == utils.ErrIndexKeyNotFound {
|
2021-05-25 15:32:06 +00:00
|
|
|
return nil, ErrBlockNotFound
|
|
|
|
}
|
|
|
|
}
|
|
|
|
block, err := bp.FetchBlock(blockHash)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return block, nil
|
|
|
|
}
|
2021-05-26 20:58:31 +00:00
|
|
|
|
2021-06-04 21:18:06 +00:00
|
|
|
func (bp *BlockChain) FetchBlockHeaderByHeight(height uint64) (*types2.BlockHeader, error) {
|
2021-05-26 20:58:31 +00:00
|
|
|
var heightBytes []byte
|
|
|
|
binary.LittleEndian.PutUint64(heightBytes, height)
|
|
|
|
blockHash, err := bp.heightIndex.GetBytes(heightBytes)
|
|
|
|
if err != nil {
|
2021-06-04 21:18:06 +00:00
|
|
|
if err == utils.ErrIndexKeyNotFound {
|
2021-05-26 20:58:31 +00:00
|
|
|
return nil, ErrBlockNotFound
|
|
|
|
}
|
|
|
|
}
|
|
|
|
blockHeader, err := bp.FetchBlockHeader(blockHash)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return blockHeader, nil
|
|
|
|
}
|