From 5e48c0d176503d81bc14715fd1ee824366922fe0 Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Wed, 19 May 2021 23:54:36 +0300 Subject: [PATCH] Move blockchain-related code into separate package --- {pool => blockchain/pool}/blockpool.go | 13 +++++++------ {pool => blockchain/pool}/mempool.go | 14 +++++++------- {types => blockchain/types}/block.go | 0 {types => blockchain/types}/transaction.go | 2 +- 4 files changed, 15 insertions(+), 14 deletions(-) rename {pool => blockchain/pool}/blockpool.go (88%) rename {pool => blockchain/pool}/mempool.go (86%) rename {types => blockchain/types}/block.go (100%) rename {types => blockchain/types}/transaction.go (81%) diff --git a/pool/blockpool.go b/blockchain/pool/blockpool.go similarity index 88% rename from pool/blockpool.go rename to blockchain/pool/blockpool.go index b2c8ba1..f8938f1 100644 --- a/pool/blockpool.go +++ b/blockchain/pool/blockpool.go @@ -4,8 +4,9 @@ import ( "encoding/hex" "errors" - "github.com/Secured-Finance/dione/types" + types2 "github.com/Secured-Finance/dione/blockchain/types" "github.com/fxamacker/cbor/v2" + "github.com/ledgerwatch/lmdb-go/lmdb" ) @@ -58,7 +59,7 @@ func NewBlockPool(path string) (*BlockPool, error) { return pool, nil } -func (bp *BlockPool) StoreBlock(block *types.Block) error { +func (bp *BlockPool) StoreBlock(block *types2.Block) error { return bp.dbEnv.Update(func(txn *lmdb.Txn) error { data, err := cbor.Marshal(block) if err != nil { @@ -98,8 +99,8 @@ func (bp *BlockPool) HasBlock(blockHash string) (bool, error) { return blockExists, nil } -func (bp *BlockPool) FetchBlock(blockHash string) (*types.Block, error) { - var block types.Block +func (bp *BlockPool) FetchBlock(blockHash string) (*types2.Block, error) { + var block types2.Block err := bp.dbEnv.View(func(txn *lmdb.Txn) error { data, err := txn.Get(bp.db, []byte(DefaultBlockPrefix+blockHash)) if err != nil { @@ -117,8 +118,8 @@ func (bp *BlockPool) FetchBlock(blockHash string) (*types.Block, error) { return &block, nil } -func (bp *BlockPool) FetchBlockHeader(blockHash string) (*types.BlockHeader, error) { - var blockHeader types.BlockHeader +func (bp *BlockPool) FetchBlockHeader(blockHash string) (*types2.BlockHeader, error) { + var blockHeader types2.BlockHeader err := bp.dbEnv.View(func(txn *lmdb.Txn) error { data, err := txn.Get(bp.db, []byte(DefaultBlockHeaderPrefix+blockHash)) if err != nil { diff --git a/pool/mempool.go b/blockchain/pool/mempool.go similarity index 86% rename from pool/mempool.go rename to blockchain/pool/mempool.go index a5c8f47..0d7fe5d 100644 --- a/pool/mempool.go +++ b/blockchain/pool/mempool.go @@ -6,9 +6,9 @@ import ( "sync" "time" - "github.com/Secured-Finance/dione/consensus/policy" + types2 "github.com/Secured-Finance/dione/blockchain/types" - "github.com/Secured-Finance/dione/types" + "github.com/Secured-Finance/dione/consensus/policy" "github.com/Secured-Finance/dione/cache" ) @@ -39,7 +39,7 @@ func NewMempool(c cache.Cache) (*Mempool, error) { return mp, nil } -func (mp *Mempool) StoreTx(tx *types.Transaction) error { +func (mp *Mempool) StoreTx(tx *types2.Transaction) error { mp.m.Lock() defer mp.m.Unlock() @@ -50,15 +50,15 @@ func (mp *Mempool) StoreTx(tx *types.Transaction) error { return err } -func (mp *Mempool) GetTxsForNewBlock() []*types.Transaction { +func (mp *Mempool) GetTxsForNewBlock() []*types2.Transaction { mp.m.Lock() defer mp.m.Unlock() - var txForBlock []*types.Transaction - var allTxs []*types.Transaction + var txForBlock []*types2.Transaction + var allTxs []*types2.Transaction for i, v := range mp.txDescriptors { - var tx types.Transaction + var tx types2.Transaction err := mp.cache.Get(DefaultTxPrefix+v, &tx) if err != nil { if err == cache.ErrNilValue { diff --git a/types/block.go b/blockchain/types/block.go similarity index 100% rename from types/block.go rename to blockchain/types/block.go diff --git a/types/transaction.go b/blockchain/types/transaction.go similarity index 81% rename from types/transaction.go rename to blockchain/types/transaction.go index 463eac2..6ea276c 100644 --- a/types/transaction.go +++ b/blockchain/types/transaction.go @@ -17,7 +17,7 @@ type Transaction struct { func CreateTransaction(data []byte) *Transaction { timestamp := time.Now() encodedData := hex.EncodeToString(data) - hash := crypto.Keccak256([]byte(fmt.Sprintf("%d_%s", timestamp, encodedData))) + hash := crypto.Keccak256([]byte(fmt.Sprintf("%d_%s", timestamp.Unix(), encodedData))) return &Transaction{ Hash: hash, Timestamp: timestamp,