2018-11-07 21:50:43 +00:00
|
|
|
// VulcanizeDB
|
2019-03-12 15:46:42 +00:00
|
|
|
// Copyright © 2019 Vulcanize
|
2018-11-07 21:50:43 +00:00
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2018-05-02 16:17:02 +00:00
|
|
|
package level_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
. "github.com/onsi/ginkgo"
|
2018-05-03 21:58:45 +00:00
|
|
|
. "github.com/onsi/gomega"
|
2020-01-29 19:00:07 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/eth/datastore/ethereum/level"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/eth/fakes"
|
2018-05-02 16:17:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ = Describe("Level database", func() {
|
2018-05-03 21:58:45 +00:00
|
|
|
Describe("Getting a block", func() {
|
|
|
|
It("converts block number to uint64 and hash to common.Hash to fetch block from reader", func() {
|
2018-05-02 16:17:02 +00:00
|
|
|
mockReader := fakes.NewMockLevelDatabaseReader()
|
|
|
|
ldb := level.NewLevelDatabase(mockReader)
|
2018-05-03 21:58:45 +00:00
|
|
|
blockHash := []byte{5, 4, 3, 2, 1}
|
2018-05-02 16:17:02 +00:00
|
|
|
blockNumber := int64(12345)
|
|
|
|
|
2018-05-03 21:58:45 +00:00
|
|
|
ldb.GetBlock(blockHash, blockNumber)
|
2018-05-02 16:17:02 +00:00
|
|
|
|
2018-05-03 21:58:45 +00:00
|
|
|
expectedBlockHash := common.BytesToHash(blockHash)
|
2018-05-02 16:17:02 +00:00
|
|
|
expectedBlockNumber := uint64(blockNumber)
|
2018-05-03 21:58:45 +00:00
|
|
|
mockReader.AssertGetBlockCalledWith(expectedBlockHash, expectedBlockNumber)
|
2018-05-02 16:17:02 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2018-05-03 21:58:45 +00:00
|
|
|
Describe("Getting a block hash", func() {
|
|
|
|
It("converts block number to uint64 to fetch hash from reader", func() {
|
2018-05-02 16:17:02 +00:00
|
|
|
mockReader := fakes.NewMockLevelDatabaseReader()
|
|
|
|
ldb := level.NewLevelDatabase(mockReader)
|
|
|
|
blockNumber := int64(12345)
|
|
|
|
|
2018-05-03 21:58:45 +00:00
|
|
|
ldb.GetBlockHash(blockNumber)
|
2018-05-02 16:17:02 +00:00
|
|
|
|
|
|
|
expectedBlockNumber := uint64(blockNumber)
|
2018-05-03 21:58:45 +00:00
|
|
|
mockReader.AssertGetCanonicalHashCalledWith(expectedBlockNumber)
|
2018-05-02 16:17:02 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Describe("Getting a block's receipts", func() {
|
|
|
|
It("converts block number to uint64 and hash to common.Hash to fetch receipts from reader", func() {
|
|
|
|
mockReader := fakes.NewMockLevelDatabaseReader()
|
|
|
|
ldb := level.NewLevelDatabase(mockReader)
|
|
|
|
blockHash := []byte{5, 4, 3, 2, 1}
|
|
|
|
blockNumber := int64(12345)
|
|
|
|
|
|
|
|
ldb.GetBlockReceipts(blockHash, blockNumber)
|
|
|
|
|
|
|
|
expectedBlockHash := common.BytesToHash(blockHash)
|
|
|
|
expectedBlockNumber := uint64(blockNumber)
|
|
|
|
mockReader.AssertGetBlockReceiptsCalledWith(expectedBlockHash, expectedBlockNumber)
|
|
|
|
})
|
|
|
|
})
|
2018-05-03 21:58:45 +00:00
|
|
|
|
|
|
|
Describe("Getting the latest block number", func() {
|
|
|
|
It("invokes the database reader to get the latest block number by hash and converts result to int64", func() {
|
|
|
|
mockReader := fakes.NewMockLevelDatabaseReader()
|
|
|
|
fakeHash := common.BytesToHash([]byte{1, 2, 3, 4, 5})
|
|
|
|
mockReader.SetHeadBlockHashReturnHash(fakeHash)
|
|
|
|
fakeBlockNumber := uint64(123456789)
|
|
|
|
mockReader.SetReturnBlockNumber(fakeBlockNumber)
|
|
|
|
ldb := level.NewLevelDatabase(mockReader)
|
|
|
|
|
|
|
|
result := ldb.GetHeadBlockNumber()
|
|
|
|
|
|
|
|
mockReader.AssertGetHeadBlockHashCalled()
|
|
|
|
mockReader.AssertGetBlockNumberCalledWith(fakeHash)
|
|
|
|
Expect(result).To(Equal(int64(fakeBlockNumber)))
|
|
|
|
})
|
|
|
|
})
|
2018-05-02 16:17:02 +00:00
|
|
|
})
|