Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate txhash list in EvmAuxStore #1536

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions eth/polls/polltx.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/gogo/protobuf/proto"
"github.com/loomnetwork/go-loom/plugin/types"
"github.com/loomnetwork/loomchain"
"github.com/loomnetwork/loomchain/eth/query"
"github.com/loomnetwork/loomchain/rpc/eth"
"github.com/loomnetwork/loomchain/store"
evmaux "github.com/loomnetwork/loomchain/store/evm_aux"
Expand Down Expand Up @@ -35,7 +36,9 @@ func (p *EthTxPoll) Poll(
if p.lastBlockRead+1 > uint64(state.Block().Height) {
return p, nil, nil
}
lastBlock, results, err := getTxHashes(state, p.lastBlockRead, readReceipt, p.evmAuxStore)
lastBlock, results, err := getTxHashes(
p.blockStore, state, p.lastBlockRead, readReceipt, p.evmAuxStore,
)
if err != nil {
return p, nil, nil
}
Expand All @@ -46,15 +49,15 @@ func (p *EthTxPoll) Poll(
func (p *EthTxPoll) AllLogs(
state loomchain.ReadOnlyState, id string, readReceipts loomchain.ReadReceiptHandler,
) (interface{}, error) {
_, results, err := getTxHashes(state, p.startBlock, readReceipts, p.evmAuxStore)
_, results, err := getTxHashes(p.blockStore, state, p.startBlock, readReceipts, p.evmAuxStore)
return eth.EncBytesArray(results), err
}

func getTxHashes(state loomchain.ReadOnlyState, lastBlockRead uint64,
func getTxHashes(blockStore store.BlockStore, state loomchain.ReadOnlyState, lastBlockRead uint64,
readReceipts loomchain.ReadReceiptHandler, evmAuxStore *evmaux.EvmAuxStore) (uint64, [][]byte, error) {
var txHashes [][]byte
for height := lastBlockRead + 1; height < uint64(state.Block().Height); height++ {
txHashList, err := evmAuxStore.GetTxHashList(height)
txHashList, err := query.GetTxHashList(blockStore, state, int64(height), evmAuxStore)

if err != nil {
return lastBlockRead, nil, errors.Wrapf(err, "reading tx hashes at height %d", height)
Expand All @@ -76,7 +79,7 @@ func (p *EthTxPoll) LegacyPoll(

var txHashes [][]byte
for height := p.lastBlockRead + 1; height < uint64(state.Block().Height); height++ {
txHashList, err := p.evmAuxStore.GetTxHashList(height)
txHashList, err := query.GetTxHashList(p.blockStore, state, int64(height), p.evmAuxStore)
if err != nil {
return p, nil, errors.Wrapf(err, "reading tx hash at heght %d", height)
}
Expand Down
22 changes: 21 additions & 1 deletion eth/query/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,26 @@ func GetBlockByNumber(
return blockInfo, nil
}

func GetTxHashList(blockStore store.BlockStore,
state loomchain.ReadOnlyState,
height int64,
evmAuxStore *evmaux.EvmAuxStore) ([][]byte, error) {
txObject, err := GetBlockByNumber(blockStore, state, height, false, evmAuxStore)
if err != nil {
return nil, err
}

var txHashList [][]byte
for _, txHashData := range txObject.Transactions {
txHash, err := eth.DecDataToBytes(txHashData.(eth.Data))
if err != nil {
return nil, errors.Wrapf(err, "unable to decode txhash %x", txHashData)
}
txHashList = append(txHashList, txHash)
}
return txHashList, nil
}

func GetTxObjectFromBlockResult(
blockResult *ctypes.ResultBlock, txResultData []byte, txIndex int64, evmAuxStore *evmaux.EvmAuxStore,
) (eth.JsonTxObject, *eth.Data, error) {
Expand Down Expand Up @@ -316,7 +336,7 @@ func DeprecatedGetBlockByNumber(
LogsBloom: evmAuxStore.GetBloomFilter(uint64(height)),
}

txHashList, err := evmAuxStore.GetTxHashList(uint64(height))
txHashList, err := GetTxHashList(blockStore, state, height, evmAuxStore)
if err != nil {
return nil, errors.Wrap(err, "getting tx hash")
}
Expand Down
2 changes: 1 addition & 1 deletion receipts/leveldb/leveldb.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func CreateEventLogs(
func (lr *LevelDbReceipts) GetReceipt(txHash []byte) (types.EvmTxReceipt, error) {
txReceiptProto, err := lr.evmAuxStore.DB().Get(txHash, nil)
if err != nil {
return types.EvmTxReceipt{}, errors.Wrapf(err, "get receipt for %s", string(txHash))
return types.EvmTxReceipt{}, errors.Wrapf(err, "get receipt for %x", txHash)
}
txReceipt := types.EvmTxReceiptListItem{}
err = proto.Unmarshal(txReceiptProto, &txReceipt)
Expand Down
30 changes: 0 additions & 30 deletions receipts/leveldb/leveldb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,13 @@ func TestReceiptsCyclicDB(t *testing.T) {
// store 5 receipts
require.NoError(t, handler.CommitBlock(receipts1, height))
confirmDbConsistency(t, handler, 5, receipts1[0].TxHash, receipts1[4].TxHash, receipts1, commit)
confirmStateConsistency(t, evmAuxStore, receipts1, height)
// db reaching max
height = 2
receipts2 := common.MakeDummyReceipts(t, 7, height)
commit = 2
// store another 7 receipts
require.NoError(t, handler.CommitBlock(receipts2, height))
confirmDbConsistency(t, handler, maxSize, receipts1[2].TxHash, receipts2[6].TxHash, append(receipts1[2:5], receipts2...), commit)
confirmStateConsistency(t, evmAuxStore, receipts2, height)

// db at max
height = 3
Expand All @@ -47,7 +45,6 @@ func TestReceiptsCyclicDB(t *testing.T) {
// store another 5 receipts
require.NoError(t, handler.CommitBlock(receipts3, height))
confirmDbConsistency(t, handler, maxSize, receipts2[2].TxHash, receipts3[4].TxHash, append(receipts2[2:7], receipts3...), commit)
confirmStateConsistency(t, evmAuxStore, receipts3, height)

require.NoError(t, handler.Close())

Expand All @@ -72,7 +69,6 @@ func TestReceiptsCommitAllInOneBlock(t *testing.T) {
require.NoError(t, handler.CommitBlock(receipts1, height))

confirmDbConsistency(t, handler, maxSize, receipts1[1].TxHash, receipts1[10].TxHash, receipts1[1:], commit)
confirmStateConsistency(t, evmAuxStore, receipts1, height)

require.NoError(t, handler.Close())

Expand Down Expand Up @@ -130,14 +126,6 @@ func confirmDbConsistency(t *testing.T, handler *LevelDbReceipts,
}
}

func confirmStateConsistency(t *testing.T, evmAuxStore *evmaux.EvmAuxStore, receipts []*types.EvmTxReceipt, height uint64) {
txHashes, err := evmAuxStore.GetTxHashList(height)
require.NoError(t, err)
for i := 0; i < len(receipts); i++ {
require.EqualValues(t, 0, bytes.Compare(txHashes[i], receipts[i].TxHash))
}
}

func TestConfirmTransactionReceipts(t *testing.T) {
evmAuxStore, err := common.NewMockEvmAuxStore()
require.NoError(t, err)
Expand All @@ -147,24 +135,6 @@ func TestConfirmTransactionReceipts(t *testing.T) {
receipts1 := common.MakeDummyReceipts(t, 5, height)
// store 5 receipts
require.NoError(t, handler.CommitBlock(receipts1, height))
txHashes, err := evmAuxStore.GetTxHashList(height)
require.NoError(t, err)
a := []byte("0xf0675dc27bC62b584Ab2E8E1D483a55CFac9E960")
b := []byte("0xe288d6eec7150D6a22FDE33F0AA2d81E06591C4d")
c := append(txHashes, a, b)

for i := 0; i < len(c); i++ {
//for i > len(c)-3 These are invalid tx hashes, so error must be returned by GetReceipt in this case
if i > len(c)-3 {
_, err1 := handler.GetReceipt(c[i])
require.Error(t, err1)
} else {
//These are valid hashes so valid txReceipt must be returned
txReceipt, err1 := handler.GetReceipt(c[i])
require.NoError(t, err1)
require.EqualValues(t, 0, bytes.Compare(c[i], txReceipt.TxHash))
}
}
require.NoError(t, handler.Close())
_, err = os.Stat(evmaux.EvmAuxDBName)
require.NoError(t, err)
Expand Down
25 changes: 13 additions & 12 deletions store/block_store_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ func TestBlockFetchAtHeightLRU(t *testing.T) {
cachedblockStore, err := NewLRUBlockStoreCache(200, b)
require.NoError(t, err)
height := int64(19)

//Cache Empty at present resulting in Cache miss
_, ok := cachedblockStore.Cache.Get(height)
require.Equal(t, ok, false, "Cache miss")
Expand All @@ -30,7 +31,7 @@ func TestBlockFetchAtHeightLRU(t *testing.T) {
require.Equal(t, height, blockstoreData.Block.Height, "Block height matches requested height")

//request for a block for more than maximum height, error is returned by Cache and no caching occurs
height = int64(55)
height = int64(2100) // Maximum block height is now 2000
blockstoreData, err = cachedblockStore.GetBlockByHeight(&height)
require.Error(t, err, "Cache Gives Error as block fetched is greater than maximum height")

Expand All @@ -40,14 +41,14 @@ func TestBlockFetchAtHeightLRU(t *testing.T) {
require.Error(t, err, "Cache Gives Error as block fetched is for height <= 0")

//block at maximum height not present in cache
height = int64(50)
height = int64(maxHeight)
_, ok = cachedblockStore.Cache.Get(height)
require.Equal(t, ok, false, "Cache miss")

//request for a block for nil height, maximum height data is returned and cached
blockstoreData, err = cachedblockStore.GetBlockByHeight(nil)
require.NoError(t, err, "Gives maximum height block")
require.Equal(t, int64(50), blockstoreData.Block.Height, "maximum height block was fetched")
require.Equal(t, int64(maxHeight), blockstoreData.Block.Height, "maximum height block was fetched")

//block at maximum height present in cache
_, ok = cachedblockStore.Cache.Get(height)
Expand Down Expand Up @@ -149,7 +150,7 @@ func TestGetBlockResultsLRU(t *testing.T) {
require.Equal(t, height, blockstoreData.Height, "Expecting data from Cache,Block Height stored in structure ctypes.ResultBlock equal to fetched from API for Cache api data accuracy check")

//request for a block for more than maximum height, error is returned by Cache and no caching occurs
height = int64(55)
height = int64(2100)
blockstoreData, err = cachedblockStore.GetBlockResults(&height)
require.Error(t, err, "Cache Gives Error as block fetched is greater than maximum height")

Expand All @@ -164,14 +165,14 @@ func TestGetBlockResultsLRU(t *testing.T) {
require.Error(t, err, "Cache Gives Error as block fetched is for height <= 0")

//blockresult at maximum height not present in cache
height = int64(50)
height = int64(maxHeight)
_, ok = cachedblockStore.Cache.Get(blockResultKey(height))
require.Equal(t, ok, false, "Cache miss")

//request for a block for nil height, maximum height data is returned and cached
blockstoreData, err = cachedblockStore.GetBlockResults(nil)
require.NoError(t, err, "Gives maximum height block result info")
require.Equal(t, int64(50), blockstoreData.Height, "Expecting blockstore height 50 as maximum height block is fetched")
require.Equal(t, int64(maxHeight), blockstoreData.Height, "Expecting blockstore height 50 as maximum height block is fetched")

//blockresult at maximum height present in cache
_, ok = cachedblockStore.Cache.Get(blockResultKey(height))
Expand Down Expand Up @@ -203,7 +204,7 @@ func TestBlockFetchAtHeight2Q(t *testing.T) {
require.Equal(t, height, blockstoreData.Block.Height, "Expecting data from Cache,Block Height stored in structure ctypes.ResultBlock equal to fetched from API for Cache api data accuracy check")

//request for a block for more than maximum height, error is returned by Cache and no caching occurs
height = int64(55)
height = int64(2100)
blockstoreData, err = cachedblockStore.GetBlockByHeight(&height)
require.Error(t, err, "Cache Gives Error as block fetched is greater than maximum height,this is default functionality of corresponding tendermint blockstore API also")

Expand All @@ -213,14 +214,14 @@ func TestBlockFetchAtHeight2Q(t *testing.T) {
require.Error(t, err, "Cache Gives Error as block fetched is for height <= 0,this is default functionality of corresponding tendermint blockstore API also")

//block at maximum height not present in cache
height = int64(50)
height = int64(maxHeight)
_, ok = cachedblockStore.TwoQueueCache.Get(height)
require.Equal(t, ok, false, "Cache miss")

//request for a block for nil height, maximum height data is returned and cached
blockstoreData, err = cachedblockStore.GetBlockByHeight(nil)
require.NoError(t, err, "Gives maximum height block")
require.Equal(t, int64(50), blockstoreData.Block.Height, "Expecting blockstore height 50 as maximum height block is fetched,this is default functionality of corresponding tendermint blockstore API also")
require.Equal(t, int64(maxHeight), blockstoreData.Block.Height, "Expecting blockstore height 50 as maximum height block is fetched,this is default functionality of corresponding tendermint blockstore API also")

//block at maximum height present in cache
_, ok = cachedblockStore.TwoQueueCache.Get(height)
Expand Down Expand Up @@ -318,7 +319,7 @@ func TestGetBlockResults2Q(t *testing.T) {
require.Equal(t, height, blockstoreData.Height, "Expecting data from Cache,Block Height stored in structure ctypes.ResultBlock equal to fetched from API for Cache api data accuracy check")

//request for a block for more than maximum height, error is returned by Cache and no caching occurs
height = int64(55)
height = int64(2100)
blockstoreData, err = cachedblockStore.GetBlockResults(&height)
require.Error(t, err, "Cache Gives Error as block fetched is greater than maximum height")

Expand All @@ -333,14 +334,14 @@ func TestGetBlockResults2Q(t *testing.T) {
require.Error(t, err, "Cache Gives Error as block fetched is for height <= 0")

//blockresult at maximum height not present in cache
height = int64(50)
height = int64(maxHeight)
_, ok = cachedblockStore.TwoQueueCache.Get(blockResultKey(height))
require.Equal(t, ok, false, "Cache miss")

//request for a block for nil height, maximum height data is returned and cached
blockstoreData, err = cachedblockStore.GetBlockResults(nil)
require.NoError(t, err, "Gives maximum height block result info")
require.Equal(t, int64(50), blockstoreData.Height, "Expecting blockstore height 50 as maximum height block is fetched")
require.Equal(t, int64(maxHeight), blockstoreData.Height, "Expecting blockstore height 50 as maximum height block is fetched")

//blockresult at maximum height present in cache
_, ok = cachedblockStore.TwoQueueCache.Get(blockResultKey(height))
Expand Down
13 changes: 0 additions & 13 deletions store/evm_aux/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,19 +107,6 @@ func (s *EvmAuxStore) GetBloomFilter(height uint64) []byte {
return filter
}

func (s *EvmAuxStore) GetTxHashList(height uint64) ([][]byte, error) {
protHashList, err := s.db.Get(evmTxHashKey(height), nil)
if err != nil && err != leveldb.ErrNotFound {
return nil, err
}
if err == leveldb.ErrNotFound {
return [][]byte{}, nil
}
txHashList := types.EthTxHashList{}
err = proto.Unmarshal(protHashList, &txHashList)
return txHashList.EthTxHash, err
}

func (s *EvmAuxStore) SetBloomFilter(tran *leveldb.Transaction, filter []byte, height uint64) error {
return tran.Put(bloomFilterKey(height), filter, nil)
}
Expand Down
8 changes: 0 additions & 8 deletions store/evm_aux/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,11 @@ func TestTxHashOperation(t *testing.T) {
}
evmAuxStore, err := LoadStore()
require.NoError(t, err)
txHashList, err := evmAuxStore.GetTxHashList(40)
require.NoError(t, err)
require.Equal(t, 0, len(txHashList))
db := evmAuxStore.DB()
tran, err := db.OpenTransaction()
require.NoError(t, err)
evmAuxStore.SetTxHashList(tran, txHashList1, 30)
tran.Commit()
txHashList, err = evmAuxStore.GetTxHashList(30)
require.NoError(t, err)
require.Equal(t, 2, len(txHashList))
require.Equal(t, true, bytes.Equal(txHashList1[0], txHashList1[0]))
require.Equal(t, true, bytes.Equal(txHashList1[1], txHashList1[1]))
evmAuxStore.ClearData()
}

Expand Down
8 changes: 6 additions & 2 deletions store/mock_block_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import (

var _ BlockStore = &MockBlockStore{}

var (
maxHeight = 2000
)

type MockBlockStore struct {
blocks map[int64]*ctypes.ResultBlock
blockResults map[int64]*ctypes.ResultBlockResults
Expand All @@ -27,7 +31,7 @@ func NewMockBlockStore() *MockBlockStore {

func (s *MockBlockStore) GetBlockByHeight(height *int64) (*ctypes.ResultBlock, error) {
//Taken as max blockchain height
h := int64(50)
h := int64(maxHeight)
//Get Height added to emulate error handling and nil height case covered in tendermint blockstore
h, err := getHeight(h, height)
if err != nil {
Expand Down Expand Up @@ -82,7 +86,7 @@ func (s *MockBlockStore) GetBlockRangeByHeight(minHeight, maxHeight int64) (*cty
}

func (s *MockBlockStore) GetBlockResults(height *int64) (*ctypes.ResultBlockResults, error) {
h := int64(50)
h := int64(maxHeight)
h, err := getHeight(h, height)
if err != nil {
return nil, err
Expand Down