Skip to content

Commit

Permalink
finalized funcs (#248)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lawliet-Chan authored Dec 16, 2024
1 parent 8c174a9 commit 0d78fdf
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
45 changes: 43 additions & 2 deletions core/blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type BlockChain struct {
currentBlock atomic.Pointer[Block]
lastFinalizedBlock atomic.Pointer[Block]
finalizedBlocks *lru.Cache[BlockNum, *Block]
appendedBlocks *lru.Cache[BlockNum, *Block]

chain ysql.SqlDB
ItxDB
Expand All @@ -42,7 +43,13 @@ func NewBlockChain(nodeType int, cfg *config.BlockchainConf, txdb ItxDB) *BlockC

finalizedBlocks, err := lru.New[BlockNum, *Block](cfg.CacheSize)
if err != nil {
logrus.Fatal("init cache failed: ", err)
logrus.Fatal("init finalizedBlocks cache failed: ", err)
}

appendedBlocks, err := lru.New[BlockNum, *Block](cfg.CacheSize)
if err != nil {
logrus.Fatal("init appendedBlocks cache failed: ", err)

}

return &BlockChain{
Expand All @@ -51,6 +58,7 @@ func NewBlockChain(nodeType int, cfg *config.BlockchainConf, txdb ItxDB) *BlockC
currentBlock: currentBlock,
lastFinalizedBlock: lastFinalizedBlock,
finalizedBlocks: finalizedBlocks,
appendedBlocks: appendedBlocks,
chain: chain,
ItxDB: txdb,
}
Expand Down Expand Up @@ -116,6 +124,7 @@ func (bc *BlockChain) AppendBlock(b *Block) error {
return err
}
bc.currentBlock.Store(b)
bc.appendedBlocks.Add(b.Height, b)
return nil
}

Expand Down Expand Up @@ -181,6 +190,27 @@ func (bc *BlockChain) GetBlock(blockHash Hash) (*Block, error) {
}

func (bc *BlockChain) GetCompactBlockByHeight(height BlockNum) (*CompactBlock, error) {
if block, ok := bc.appendedBlocks.Get(height); ok {
return block.Compact(), nil
}
var bs BlocksScheme
result := bc.chain.Db().Where(&BlocksScheme{
Height: height,
Finalize: true,
}).Find(&bs)
err := result.Error

if err != nil {
return nil, err
}

if result.RowsAffected == 0 {
return nil, yerror.ErrBlockNotFound
}
return bs.toBlock()
}

func (bc *BlockChain) GetFinalizedCompactBlockByHeight(height BlockNum) (*CompactBlock, error) {
if block, ok := bc.finalizedBlocks.Get(height); ok {
return block.Compact(), nil
}
Expand All @@ -202,7 +232,7 @@ func (bc *BlockChain) GetCompactBlockByHeight(height BlockNum) (*CompactBlock, e
}

func (bc *BlockChain) GetBlockByHeight(height BlockNum) (*Block, error) {
if block, ok := bc.finalizedBlocks.Get(height); ok {
if block, ok := bc.appendedBlocks.Get(height); ok {
return block, nil
}
cBlock, err := bc.GetCompactBlockByHeight(height)
Expand All @@ -212,6 +242,17 @@ func (bc *BlockChain) GetBlockByHeight(height BlockNum) (*Block, error) {
return bc.getBlockByCompact(cBlock)
}

func (bc *BlockChain) GetFinalizedBlockByHeight(height BlockNum) (*Block, error) {
if block, ok := bc.finalizedBlocks.Get(height); ok {
return block, nil
}
cBlock, err := bc.GetFinalizedCompactBlockByHeight(height)
if err != nil {
return nil, err
}
return bc.getBlockByCompact(cBlock)
}

func (bc *BlockChain) GetAllBlocksByHeight(height BlockNum) ([]*Block, error) {
cBlocks, err := bc.GetAllCompactBlocksByHeight(height)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions core/txdb/txdb.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package txdb

import (
"github.com/sirupsen/logrus"
. "github.com/yu-org/yu/common"
. "github.com/yu-org/yu/core/types"
"github.com/yu-org/yu/infra/storage/kv"
Expand Down Expand Up @@ -79,6 +80,7 @@ func (bb *TxDB) SetTxns(txns []*SignedTxn) error {
for _, txn := range txns {
txbyt, err := txn.Encode()
if err != nil {
logrus.Errorf("TxDB.SetTxns set tx(%s) failed: %v", txn.TxnHash.String(), err)
return err
}
err = kvtx.Set(txn.TxnHash.Bytes(), txbyt)
Expand Down
2 changes: 2 additions & 0 deletions core/types/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ type IBlockChain interface {
GetBlock(blockHash Hash) (*Block, error)

GetCompactBlockByHeight(height BlockNum) (*CompactBlock, error)
GetFinalizedCompactBlockByHeight(height BlockNum) (*CompactBlock, error)
GetBlockByHeight(height BlockNum) (*Block, error)
GetFinalizedBlockByHeight(height BlockNum) (*Block, error)

GetAllCompactBlocksByHeight(height BlockNum) ([]*CompactBlock, error)
GetAllBlocksByHeight(height BlockNum) ([]*Block, error)
Expand Down

0 comments on commit 0d78fdf

Please sign in to comment.