forked from cerc-io/ipld-eth-server
Consolidate test doubles
- Migrate various mocks of core namespaces to shared version in `fakes` pkg - Err on the side of making test doubles less sophisticated - Don't pull over mocks of namespaces that are only used in example code
This commit is contained in:
@@ -1,51 +0,0 @@
|
||||
package inmemory
|
||||
|
||||
import (
|
||||
"github.com/vulcanize/vulcanizedb/pkg/core"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/datastore"
|
||||
)
|
||||
|
||||
type BlockRepository struct {
|
||||
*InMemory
|
||||
}
|
||||
|
||||
func (blockRepository *BlockRepository) CreateOrUpdateBlock(block core.Block) (int64, error) {
|
||||
blockRepository.CreateOrUpdateBlockCallCount++
|
||||
blockRepository.blocks[block.Number] = block
|
||||
for _, transaction := range block.Transactions {
|
||||
blockRepository.receipts[transaction.Hash] = transaction.Receipt
|
||||
blockRepository.logs[transaction.TxHash] = transaction.Logs
|
||||
}
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
func (blockRepository *BlockRepository) GetBlock(blockNumber int64) (core.Block, error) {
|
||||
if block, ok := blockRepository.blocks[blockNumber]; ok {
|
||||
return block, nil
|
||||
}
|
||||
return core.Block{}, datastore.ErrBlockDoesNotExist(blockNumber)
|
||||
}
|
||||
|
||||
func (blockRepository *BlockRepository) MissingBlockNumbers(startingBlockNumber int64, endingBlockNumber int64, nodeId string) []int64 {
|
||||
missingNumbers := []int64{}
|
||||
for blockNumber := int64(startingBlockNumber); blockNumber <= endingBlockNumber; blockNumber++ {
|
||||
if _, ok := blockRepository.blocks[blockNumber]; !ok {
|
||||
missingNumbers = append(missingNumbers, blockNumber)
|
||||
}
|
||||
}
|
||||
return missingNumbers
|
||||
}
|
||||
|
||||
func (blockRepository *BlockRepository) SetBlocksStatus(chainHead int64) {
|
||||
for key, block := range blockRepository.blocks {
|
||||
if key < (chainHead - blocksFromHeadBeforeFinal) {
|
||||
tmp := block
|
||||
tmp.IsFinal = true
|
||||
blockRepository.blocks[key] = tmp
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (blockRepository *BlockRepository) BlockCount() int {
|
||||
return len(blockRepository.blocks)
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
package inmemory
|
||||
|
||||
import (
|
||||
"github.com/vulcanize/vulcanizedb/pkg/core"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/datastore"
|
||||
)
|
||||
|
||||
type ContractRepostiory struct {
|
||||
*InMemory
|
||||
}
|
||||
|
||||
func (contractRepository *ContractRepostiory) ContractExists(contractHash string) bool {
|
||||
_, present := contractRepository.contracts[contractHash]
|
||||
return present
|
||||
}
|
||||
|
||||
func (contractRepository *ContractRepostiory) GetContract(contractHash string) (core.Contract, error) {
|
||||
contract, ok := contractRepository.contracts[contractHash]
|
||||
if !ok {
|
||||
return core.Contract{}, datastore.ErrContractDoesNotExist(contractHash)
|
||||
}
|
||||
for _, block := range contractRepository.blocks {
|
||||
for _, transaction := range block.Transactions {
|
||||
if transaction.To == contractHash {
|
||||
contract.Transactions = append(contract.Transactions, transaction)
|
||||
}
|
||||
}
|
||||
}
|
||||
return contract, nil
|
||||
}
|
||||
|
||||
func (contractRepository *ContractRepostiory) CreateContract(contract core.Contract) error {
|
||||
contractRepository.contracts[contract.Hash] = contract
|
||||
return nil
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
package inmemory
|
||||
|
||||
import "github.com/vulcanize/vulcanizedb/pkg/core"
|
||||
|
||||
type HeaderRepository struct {
|
||||
memory *InMemory
|
||||
}
|
||||
|
||||
func NewHeaderRepository(memory *InMemory) *HeaderRepository {
|
||||
return &HeaderRepository{memory: memory}
|
||||
}
|
||||
|
||||
func (repository *HeaderRepository) CreateOrUpdateHeader(header core.Header) (int64, error) {
|
||||
repository.memory.headers[header.BlockNumber] = header
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
func (repository *HeaderRepository) GetHeader(blockNumber int64) (core.Header, error) {
|
||||
return repository.memory.headers[blockNumber], nil
|
||||
}
|
||||
|
||||
func (repository *HeaderRepository) MissingBlockNumbers(startingBlockNumber, endingBlockNumber int64, nodeID string) []int64 {
|
||||
missingNumbers := []int64{}
|
||||
for blockNumber := int64(startingBlockNumber); blockNumber <= endingBlockNumber; blockNumber++ {
|
||||
if _, ok := repository.memory.headers[blockNumber]; !ok {
|
||||
missingNumbers = append(missingNumbers, blockNumber)
|
||||
}
|
||||
}
|
||||
return missingNumbers
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
package inmemory
|
||||
|
||||
import (
|
||||
"github.com/vulcanize/vulcanizedb/pkg/core"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/filters"
|
||||
)
|
||||
|
||||
const (
|
||||
blocksFromHeadBeforeFinal = 20
|
||||
)
|
||||
|
||||
type InMemory struct {
|
||||
CreateOrUpdateBlockCallCount int
|
||||
blocks map[int64]core.Block
|
||||
contracts map[string]core.Contract
|
||||
headers map[int64]core.Header
|
||||
logFilters map[string]filters.LogFilter
|
||||
logs map[string][]core.Log
|
||||
receipts map[string]core.Receipt
|
||||
}
|
||||
|
||||
func NewInMemory() *InMemory {
|
||||
return &InMemory{
|
||||
CreateOrUpdateBlockCallCount: 0,
|
||||
blocks: make(map[int64]core.Block),
|
||||
contracts: make(map[string]core.Contract),
|
||||
headers: make(map[int64]core.Header),
|
||||
logFilters: make(map[string]filters.LogFilter),
|
||||
logs: make(map[string][]core.Log),
|
||||
receipts: make(map[string]core.Receipt),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user