diff --git a/eth/polls/polltx.go b/eth/polls/polltx.go index e1161ddcfb..d4ff9572ab 100644 --- a/eth/polls/polltx.go +++ b/eth/polls/polltx.go @@ -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" @@ -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 } @@ -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) @@ -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) } diff --git a/eth/query/block.go b/eth/query/block.go index 2d7a997939..7e8cdaec4b 100644 --- a/eth/query/block.go +++ b/eth/query/block.go @@ -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) { @@ -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") } diff --git a/receipts/leveldb/leveldb.go b/receipts/leveldb/leveldb.go index b140bd00a4..84cf7139a1 100644 --- a/receipts/leveldb/leveldb.go +++ b/receipts/leveldb/leveldb.go @@ -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) diff --git a/receipts/leveldb/leveldb_test.go b/receipts/leveldb/leveldb_test.go index e2fc61b10e..521791eb14 100644 --- a/receipts/leveldb/leveldb_test.go +++ b/receipts/leveldb/leveldb_test.go @@ -30,7 +30,6 @@ 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) @@ -38,7 +37,6 @@ func TestReceiptsCyclicDB(t *testing.T) { // 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 @@ -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()) @@ -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()) @@ -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) @@ -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) diff --git a/store/block_store_cache_test.go b/store/block_store_cache_test.go index 6edf12d98c..7404b5ae9a 100644 --- a/store/block_store_cache_test.go +++ b/store/block_store_cache_test.go @@ -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") @@ -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") @@ -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) @@ -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") @@ -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)) @@ -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") @@ -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) @@ -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") @@ -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)) diff --git a/store/evm_aux/store.go b/store/evm_aux/store.go index 41700f51ec..3d5a3c89ac 100644 --- a/store/evm_aux/store.go +++ b/store/evm_aux/store.go @@ -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) } diff --git a/store/evm_aux/store_test.go b/store/evm_aux/store_test.go index c9a140ea5c..0ece043ac8 100644 --- a/store/evm_aux/store_test.go +++ b/store/evm_aux/store_test.go @@ -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() } diff --git a/store/mock_block_store.go b/store/mock_block_store.go index d647d468d1..571234551e 100644 --- a/store/mock_block_store.go +++ b/store/mock_block_store.go @@ -13,6 +13,10 @@ import ( var _ BlockStore = &MockBlockStore{} +var ( + maxHeight = 2000 +) + type MockBlockStore struct { blocks map[int64]*ctypes.ResultBlock blockResults map[int64]*ctypes.ResultBlockResults @@ -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 { @@ -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