Move blockchain-related code into separate package

This commit is contained in:
ChronosX88 2021-05-19 23:54:36 +03:00
parent 3a53295fd2
commit 5e48c0d176
Signed by: ChronosXYZ
GPG Key ID: 085A69A82C8C511A
4 changed files with 15 additions and 14 deletions

View File

@ -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 {

View File

@ -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 {

View File

@ -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,