Skip to content

Commit

Permalink
clean up blockdao (#4164)
Browse files Browse the repository at this point in the history
* clean up blockdao

* address comments

* fix contract address

* address conflict
  • Loading branch information
CoderZhi authored Mar 11, 2024
1 parent 9505d91 commit e7c3787
Show file tree
Hide file tree
Showing 27 changed files with 507 additions and 477 deletions.
40 changes: 24 additions & 16 deletions action/protocol/execution/protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (
"github.com/iotexproject/iotex-core/blockchain"
"github.com/iotexproject/iotex-core/blockchain/block"
"github.com/iotexproject/iotex-core/blockchain/blockdao"
"github.com/iotexproject/iotex-core/blockchain/filedao"
"github.com/iotexproject/iotex-core/blockchain/genesis"
"github.com/iotexproject/iotex-core/blockindex"
"github.com/iotexproject/iotex-core/config"
Expand Down Expand Up @@ -362,11 +363,19 @@ func (sct *SmartContractTest) runExecutions(
if err := bc.CommitBlock(blk); err != nil {
return nil, nil, err
}
receipts := []*action.Receipt{}
for _, hash := range hashes {
receipt, err := dao.GetReceiptByActionHash(hash, blk.Height())
if err != nil {
return nil, nil, err
receipts, err := dao.GetReceipts(blk.Height())
if err != nil {
return nil, nil, err
}
receiptMap := make(map[hash.Hash256]*action.Receipt, len(receipts))
for _, receipt := range receipts {
receiptMap[receipt.ActionHash] = receipt
}
receipts = receipts[:0]
for _, h := range hashes {
receipt, ok := receiptMap[h]
if !ok {
return nil, nil, errors.Errorf("failed to find receipt for action %x", h)
}
receipts = append(receipts, receipt)
}
Expand Down Expand Up @@ -459,7 +468,9 @@ func (sct *SmartContractTest) prepareBlockchain(
indexer, err := blockindex.NewIndexer(db.NewMemKVStore(), cfg.Genesis.Hash())
r.NoError(err)
// create BlockDAO
dao := blockdao.NewBlockDAOInMemForTest([]blockdao.BlockIndexer{sf, indexer})
store, err := filedao.NewFileDAOInMemForTest()
r.NoError(err)
dao := blockdao.NewBlockDAOWithIndexersAndCache(store, []blockdao.BlockIndexer{sf, indexer}, cfg.DB.MaxCacheSize)
r.NotNil(dao)
bc := blockchain.NewBlockchain(
cfg.Chain,
Expand Down Expand Up @@ -704,7 +715,9 @@ func TestProtocol_Handle(t *testing.T) {
require.NoError(err)
// create BlockDAO
cfg.DB.DbPath = cfg.Chain.ChainDBPath
dao := blockdao.NewBlockDAOInMemForTest([]blockdao.BlockIndexer{sf, indexer})
store, err := filedao.NewFileDAOInMemForTest()
require.NoError(err)
dao := blockdao.NewBlockDAOWithIndexersAndCache(store, []blockdao.BlockIndexer{sf, indexer}, cfg.DB.MaxCacheSize)
require.NotNil(dao)
bc := blockchain.NewBlockchain(
cfg.Chain,
Expand Down Expand Up @@ -743,8 +756,7 @@ func TestProtocol_Handle(t *testing.T) {

eHash, err := selp.Hash()
require.NoError(err)
r, _ := dao.GetReceiptByActionHash(eHash, blk.Height())
require.NotNil(r)
r := blk.Receipts[0]
require.Equal(eHash, r.ActionHash)
contract, err := address.FromString(r.ContractAddress)
require.NoError(err)
Expand All @@ -758,7 +770,7 @@ func TestProtocol_Handle(t *testing.T) {
require.NoError(err)
require.Equal(data[31:], c)

exe, _, err := dao.GetActionByActionHash(eHash, blk.Height())
exe, _, err := blk.ActionByHash(eHash)
require.NoError(err)
exeHash, err := exe.Hash()
require.NoError(err)
Expand Down Expand Up @@ -809,9 +821,7 @@ func TestProtocol_Handle(t *testing.T) {
*/
eHash, err = selp.Hash()
require.NoError(err)
r, err = dao.GetReceiptByActionHash(eHash, blk.Height())
require.NoError(err)
require.Equal(eHash, r.ActionHash)
require.Equal(eHash, blk.Receipts[0].ActionHash)

// read from key 0
data, err = hex.DecodeString("6d4ce63c")
Expand All @@ -835,9 +845,7 @@ func TestProtocol_Handle(t *testing.T) {

eHash, err = selp.Hash()
require.NoError(err)
r, err = dao.GetReceiptByActionHash(eHash, blk.Height())
require.NoError(err)
require.Equal(eHash, r.ActionHash)
require.Equal(eHash, blk.Receipts[0].ActionHash)

data, _ = hex.DecodeString("608060405234801561001057600080fd5b5060df8061001f6000396000f3006080604052600436106049576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806360fe47b114604e5780636d4ce63c146078575b600080fd5b348015605957600080fd5b5060766004803603810190808035906020019092919050505060a0565b005b348015608357600080fd5b50608a60aa565b6040518082815260200191505060405180910390f35b8060008190555050565b600080549050905600a165627a7a7230582002faabbefbbda99b20217cf33cb8ab8100caf1542bf1f48117d72e2c59139aea0029")
execution1, err := action.NewExecution(action.EmptyAddress, 4, big.NewInt(0), uint64(100000), big.NewInt(10), data)
Expand Down
43 changes: 35 additions & 8 deletions api/coreservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -781,11 +781,15 @@ func (core *coreService) ReceiptByActionHash(h hash.Hash256) (*action.Receipt, e
if err != nil {
return nil, errors.Wrap(ErrNotFound, err.Error())
}
receipt, err := core.dao.GetReceiptByActionHash(h, actIndex.BlockHeight())

receipts, err := core.dao.GetReceipts(actIndex.BlockHeight())
if err != nil {
return nil, errors.Wrap(ErrNotFound, err.Error())
return nil, err
}
if receipt := filterReceipts(receipts, h); receipt != nil {
return receipt, nil
}
return receipt, nil
return nil, errors.Wrapf(ErrNotFound, "failed to find receipt for action %x", h)
}

// TransactionLogByActionHash returns transaction log by action hash
Expand Down Expand Up @@ -1089,7 +1093,7 @@ func (core *coreService) ActionByActionHash(h hash.Hash256) (*action.SealedEnvel
if err != nil {
return nil, hash.ZeroHash256, 0, 0, errors.Wrap(ErrNotFound, err.Error())
}
selp, index, err := core.dao.GetActionByActionHash(h, actIndex.BlockHeight())
selp, index, err := blk.ActionByHash(h)
if err != nil {
return nil, hash.ZeroHash256, 0, 0, errors.Wrap(ErrNotFound, err.Error())
}
Expand Down Expand Up @@ -1237,10 +1241,14 @@ func (core *coreService) committedAction(selp *action.SealedEnvelope, blkHash ha
return nil, err
}
sender := selp.SenderAddress()
receipt, err := core.dao.GetReceiptByActionHash(actHash, blkHeight)
receipts, err := core.dao.GetReceipts(blkHeight)
if err != nil {
return nil, err
}
receipt := filterReceipts(receipts, actHash)
if receipt == nil {
return nil, errors.Wrapf(ErrNotFound, "failed to find receipt for action %x", actHash)
}

gas := new(big.Int)
gas = gas.Mul(selp.GasPrice(), big.NewInt(int64(receipt.GasConsumed)))
Expand Down Expand Up @@ -1301,22 +1309,32 @@ func (core *coreService) reverseActionsInBlock(blk *block.Block, reverseStart, c
if reverseStart > size || count == 0 {
return nil
}
// TODO (saito): fix overflow
start := size - (reverseStart + count)
if start < 0 {
start = 0
}
end := size - 1 - reverseStart
res := make([]*iotexapi.ActionInfo, 0, start-end+1)
receipts, err := core.dao.GetReceipts(blkHeight)
if err != nil {
log.Logger("api").Debug("Skipping action due to failing to get receipt", zap.Error(err))
return nil
}
receiptMap := make(map[hash.Hash256]*action.Receipt, len(receipts))
for _, receipt := range receipts {
receiptMap[receipt.ActionHash] = receipt
}
for idx := start; idx <= end; idx++ {
selp := blk.Actions[idx]
actHash, err := selp.Hash()
if err != nil {
log.Logger("api").Debug("Skipping action due to hash error", zap.Error(err))
continue
}
receipt, err := core.dao.GetReceiptByActionHash(actHash, blkHeight)
if err != nil {
log.Logger("api").Debug("Skipping action due to failing to get receipt", zap.Error(err))
receipt, ok := receiptMap[actHash]
if !ok {
log.Logger("api").With(zap.String("actionHash", hex.EncodeToString(actHash[:]))).Debug("Skipping action due to failing to get receipt")
continue
}
gas := new(big.Int).Mul(selp.GasPrice(), big.NewInt(int64(receipt.GasConsumed)))
Expand Down Expand Up @@ -1819,3 +1837,12 @@ func (core *coreService) simulateExecution(ctx context.Context, addr address.Add
})
return core.sf.SimulateExecution(ctx, addr, exec)
}

func filterReceipts(receipts []*action.Receipt, actHash hash.Hash256) *action.Receipt {
for _, r := range receipts {
if r.ActionHash == actHash {
return r
}
}
return nil
}
8 changes: 6 additions & 2 deletions api/grpcserver_integrity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1470,7 +1470,9 @@ func TestGrpcServer_ReadContractIntegrity(t *testing.T) {
require.NoError(err)
ai, err := indexer.GetActionIndex(hash[:])
require.NoError(err)
exec, _, err := dao.GetActionByActionHash(hash, ai.BlockHeight())
blk, err := dao.GetBlockByHeight(ai.BlockHeight())
require.NoError(err)
exec, _, err := blk.ActionByHash(hash)
require.NoError(err)
request := &iotexapi.ReadContractRequest{
Execution: exec.Proto().GetCore().GetExecution(),
Expand Down Expand Up @@ -1522,7 +1524,9 @@ func TestGrpcServer_EstimateGasForActionIntegrity(t *testing.T) {
require.NoError(err)
ai, err := indexer.GetActionIndex(hash[:])
require.NoError(err)
act, _, err := dao.GetActionByActionHash(hash, ai.BlockHeight())
blk, err := dao.GetBlockByHeight(ai.BlockHeight())
require.NoError(err)
act, _, err := blk.ActionByHash(hash)
require.NoError(err)
request := &iotexapi.EstimateGasForActionRequest{Action: act.Proto()}

Expand Down
26 changes: 14 additions & 12 deletions api/serverV2_integrity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/iotexproject/iotex-core/blockchain"
"github.com/iotexproject/iotex-core/blockchain/block"
"github.com/iotexproject/iotex-core/blockchain/blockdao"
"github.com/iotexproject/iotex-core/blockchain/filedao"
"github.com/iotexproject/iotex-core/blockchain/genesis"
"github.com/iotexproject/iotex-core/blockindex"
"github.com/iotexproject/iotex-core/consensus"
Expand Down Expand Up @@ -225,6 +226,10 @@ func deployContractV2(bc blockchain.Blockchain, dao blockdao.BlockDAO, actPool a
if err != nil {
return "", err
}
h1, err := ex1.Hash()
if err != nil {
return "", err
}
if err := actPool.Add(context.Background(), ex1); err != nil {
return "", err
}
Expand All @@ -237,19 +242,12 @@ func deployContractV2(bc blockchain.Blockchain, dao blockdao.BlockDAO, actPool a
}
actPool.Reset()
// get deployed contract address
var contract string
if dao != nil {
ex1Hash, err := ex1.Hash()
if err != nil {
return "", err
for _, receipt := range blk.Receipts {
if receipt.ActionHash == h1 {
return receipt.ContractAddress, nil
}
r, err := dao.GetReceiptByActionHash(ex1Hash, height+1)
if err != nil {
return "", err
}
contract = r.ContractAddress
}
return contract, nil
return "", errors.New("failed to find execution receipt")
}

func addActsToActPool(ctx context.Context, ap actpool.ActPool) error {
Expand Down Expand Up @@ -313,7 +311,11 @@ func setupChain(cfg testConfig) (blockchain.Blockchain, blockdao.BlockDAO, block
return nil, nil, nil, nil, nil, nil, nil, "", errors.New("failed to create bloomfilter indexer")
}
// create BlockDAO
dao := blockdao.NewBlockDAOInMemForTest([]blockdao.BlockIndexer{sf, indexer, bfIndexer})
store, err := filedao.NewFileDAOInMemForTest()
if err != nil {
return nil, nil, nil, nil, nil, nil, nil, "", errors.Wrap(err, "failed to create dao in memory")
}
dao := blockdao.NewBlockDAOWithIndexersAndCache(store, []blockdao.BlockIndexer{sf, indexer, bfIndexer}, 16)
if dao == nil {
return nil, nil, nil, nil, nil, nil, nil, "", errors.New("failed to create blockdao")
}
Expand Down
15 changes: 15 additions & 0 deletions blockchain/block/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package block
import (
"time"

"github.com/iotexproject/go-pkgs/hash"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
"github.com/pkg/errors"
"go.uber.org/zap"
Expand Down Expand Up @@ -95,3 +96,17 @@ func (b *Block) TransactionLog() *BlkTransactionLog {
}
return &blkLog
}

// ActionByHash returns the action of a given hash
func (b *Block) ActionByHash(h hash.Hash256) (*action.SealedEnvelope, uint32, error) {
for i, act := range b.Actions {
actHash, err := act.Hash()
if err != nil {
return nil, 0, errors.Errorf("hash failed for action %d", i)
}
if actHash == h {
return act, uint32(i), nil
}
}
return nil, 0, errors.Errorf("block does not have action %x", h)
}
Loading

0 comments on commit e7c3787

Please sign in to comment.