Skip to content

Commit

Permalink
Renamed BlockNumberMappingStorage to BlockHashStorage.
Browse files Browse the repository at this point in the history
  • Loading branch information
dcaoyuan committed Jul 12, 2019
1 parent 226efb4 commit 5398dd7
Show file tree
Hide file tree
Showing 12 changed files with 45 additions and 54 deletions.
24 changes: 12 additions & 12 deletions khipu-eth/src/main/scala/khipu/domain/Blockchain.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import khipu.network.p2p.messages.PV62.BlockBody
import khipu.network.p2p.messages.PV63.MptNode
import khipu.network.p2p.messages.PV63.MptNode._
import khipu.store.BlockchainStorages
import khipu.store.TransactionMappingStorage
import khipu.store.TransactionMappingStorage.TransactionLocation
import khipu.store.TransactionStorage
import khipu.store.TransactionStorage.TransactionLocation
import khipu.store.trienode.ReadOnlyNodeStorage
import khipu.trie
import khipu.trie.MerklePatriciaTrie
Expand Down Expand Up @@ -176,8 +176,8 @@ final class Blockchain(val storages: BlockchainStorages) extends Blockchain.I[Tr
private val receiptsStorage = storages.receiptsStorage

private val totalDifficultyStorage = storages.totalDifficultyStorage
private val transactionMappingStorage = storages.transactionMappingStorage
private val blockNumberMappingStorage = storages.blockNumberMappingStorage
private val transactionStorage = storages.transactionStorage
private val blockHashStorage = storages.blockHashStorage

def getHashByBlockNumber(number: Long): Option[Hash] =
storages.getHashByBlockNumber(number)
Expand All @@ -202,24 +202,24 @@ final class Blockchain(val storages: BlockchainStorages) extends Blockchain.I[Tr

def saveBlockHeader(blockHeader: BlockHeader) {
blockHeaderStorage.put(blockHeader.hash, blockHeader)
blockNumberMappingStorage.put(blockHeader.hash, blockHeader.number)
blockHashStorage.put(blockHeader.hash, blockHeader.number)
}

def saveBlockHeader(blockHeaders: Seq[BlockHeader]) {
val kvs = blockHeaders.map(x => x.hash -> x).toMap
blockHeaderStorage.update(Set(), kvs)
val nums = kvs.map(kv => kv._1 -> kv._2.number)
blockNumberMappingStorage.update(Set(), nums)
blockHashStorage.update(Set(), nums)
}

def saveBlockHeader(hash: Hash, blockHeader: BlockHeader) {
blockHeaderStorage.put(hash, blockHeader)
blockNumberMappingStorage.put(hash, blockHeader.number)
blockHashStorage.put(hash, blockHeader.number)
}

def saveBlockHeader(kvs: Map[Hash, BlockHeader]) {
blockHeaderStorage.update(Set(), kvs)
blockNumberMappingStorage.update(Set(), kvs.map(kv => kv._1 -> kv._2.number))
blockHashStorage.update(Set(), kvs.map(kv => kv._1 -> kv._2.number))
}

def saveBlockBody(blockHash: Hash, blockBody: BlockBody) = {
Expand Down Expand Up @@ -307,20 +307,20 @@ final class Blockchain(val storages: BlockchainStorages) extends Blockchain.I[Tr
(accountNodeStorageFor(None).get(hash) orElse storageNodeStorageFor(None).get(hash)).map(_.toMptNode)

def getTransactionLocation(txHash: Hash): Option[TransactionLocation] =
transactionMappingStorage.get(txHash)
transactionStorage.get(txHash)

private def saveBlockNumberMapping(hash: Hash, number: Long): Unit =
blockNumberMappingStorage.put(hash, number)
blockHashStorage.put(hash, number)

private def saveTxsLocations(blockHash: Hash, blockBody: BlockBody) {
val kvs = blockBody.transactionList.zipWithIndex map {
case (tx, index) => (tx.hash, TransactionLocation(blockHash, index))
}
transactionMappingStorage.update(Set(), kvs.toMap)
transactionStorage.update(Set(), kvs.toMap)
}

private def removeTxsLocations(stxs: Seq[SignedTransaction]) {
stxs.map(_.hash).foreach(transactionMappingStorage.remove)
stxs.map(_.hash).foreach(transactionStorage.remove)
}
}

2 changes: 1 addition & 1 deletion khipu-eth/src/main/scala/khipu/jsonrpc/EthService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import khipu.ledger.Ledger
import khipu.mining.BlockGenerator
import khipu.util
import khipu.store.AppStateStorage
import khipu.store.TransactionMappingStorage.TransactionLocation
import khipu.store.TransactionStorage.TransactionLocation
import khipu.transactions.PendingTransactionsService
import khipu.ommers.OmmersPool
import khipu.rlp
Expand Down
13 changes: 2 additions & 11 deletions khipu-eth/src/main/scala/khipu/service/ServiceBoard.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ import akka.stream.ActorMaterializer
import java.io.File
import java.io.PrintWriter
import java.security.SecureRandom
import kesque.Kesque
import khipu.Hash
import khipu.Khipu
import khipu.NodeStatus
import khipu.ServerStatus
import khipu.blockchain.sync.HostService
Expand All @@ -22,16 +19,13 @@ import khipu.ledger.Ledger
import khipu.network.ForkResolver
import khipu.network.p2p.MessageDecoder
import khipu.network.p2p.messages.Versions
import khipu.network.p2p.messages.PV62.BlockBody
import khipu.network.rlpx.KnownNodesService.KnownNodesServiceConfig
import khipu.network.rlpx.PeerManager
import khipu.network.rlpx.discovery.DiscoveryConfig
import khipu.store.Storages
import khipu.store.datasource.KesqueDataSource
import khipu.store.datasource.LmdbBlockDataSource
import khipu.store.datasource.LmdbDataSource
import khipu.store.datasource.LmdbNodeDataSource
import khipu.store.datasource.SharedLeveldbDataSources
import khipu.store.datasource.SharedLmdbDataSources
import khipu.util
import khipu.util.BlockchainConfig
Expand All @@ -44,13 +38,10 @@ import khipu.validators.BlockValidator
import khipu.validators.OmmersValidator
import khipu.validators.SignedTransactionValidator
import khipu.validators.Validators
import org.apache.kafka.common.record.CompressionType
import org.lmdbjava.Env
import org.lmdbjava.EnvFlags
import org.spongycastle.crypto.AsymmetricCipherKeyPair
import org.spongycastle.crypto.params.ECPublicKeyParameters
import scala.concurrent.Await
import scala.concurrent.Future
import scala.concurrent.duration._
import scala.io.Source

Expand Down Expand Up @@ -132,7 +123,7 @@ class ServiceBoardExtension(system: ExtendedActorSystem) extends Extension {
lazy val storageNodeDataSource = new LmdbNodeDataSource(dbConfig.storage, env, cacheCfg.cacheSize)
lazy val evmcodeDataSource = new LmdbNodeDataSource(dbConfig.evmcode, env)

lazy val blockNumberMappingDataSource = new LmdbDataSource(dbConfig.blocknum, env)
lazy val blockHashDataSource = new LmdbDataSource(dbConfig.blocknum, env)

lazy val blockHeaderDataSource = new LmdbBlockDataSource(dbConfig.header, env)
lazy val blockBodyDataSource = new LmdbBlockDataSource(dbConfig.body, env)
Expand Down Expand Up @@ -164,7 +155,7 @@ class ServiceBoardExtension(system: ExtendedActorSystem) extends Extension {
}
}

case dbConfig.KESQUE => null // Temporary disabled KESQUE
case dbConfig.KESQUE => null // disabled KESQUE

// new Storages.DefaultStorages with SharedLeveldbDataSources {
// implicit protected val system = ServiceBoardExtension.this.system
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ import org.lmdbjava.Dbi
import org.lmdbjava.Env
import scala.collection.mutable

object BlockNumberMappingStorage {
object BlockHashStorage {
val namespace: Array[Byte] = Array[Byte]()
}
/**
* This class is used to store the blockhash -> blocknumber
*/
final class BlockNumberMappingStorage(storags: Storages, val source: DataSource)(implicit system: ActorSystem) extends SimpleMap[Hash, Long] {
type This = BlockNumberMappingStorage
final class BlockHashStorage(storags: Storages, val source: DataSource)(implicit system: ActorSystem) extends SimpleMap[Hash, Long] {
type This = BlockHashStorage

import BlockNumberMappingStorage.namespace
import BlockHashStorage.namespace

private val log = Logging(system, this.getClass)
private val lmdbSource = source.asInstanceOf[LmdbDataSource]
Expand Down Expand Up @@ -61,7 +61,7 @@ final class BlockNumberMappingStorage(storags: Storages, val source: DataSource)
source.get(namespace, key.bytes).map(x => ByteBuffer.wrap(x).getLong)
}

override def update(toRemove: Set[Hash], toUpsert: Map[Hash, Long]): BlockNumberMappingStorage = {
override def update(toRemove: Set[Hash], toUpsert: Map[Hash, Long]): BlockHashStorage = {
val remove = toRemove map { key => key.bytes }
val upsert = toUpsert map { case (key, value) => (key.bytes -> ByteBuffer.allocate(8).putLong(value).array) }
source.update(namespace, remove, upsert)
Expand All @@ -70,6 +70,6 @@ final class BlockNumberMappingStorage(storags: Storages, val source: DataSource)

def count = source.asInstanceOf[LmdbDataSource].count

protected def apply(storags: Storages, source: DataSource): BlockNumberMappingStorage = new BlockNumberMappingStorage(storags, source)
protected def apply(storags: Storages, source: DataSource): BlockHashStorage = new BlockHashStorage(storags, source)
}

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import khipu.store.datasource.BlockDataSource
import khipu.store.datasource.DataSource
import scala.collection.mutable

final class BlockNumberMapping(blockNumberMappingSource: DataSource, blockHeaderSource: BlockDataSource) {
final class BlockHashes(blockHashSource: DataSource, blockHeaderSource: BlockDataSource) {
private val blockNumberToHash = new mutable.HashMap[Long, Hash]()
private val hashToBlockNumber = new mutable.HashMap[Hash, Long]()

Expand All @@ -21,7 +21,7 @@ final class BlockNumberMapping(blockNumberMappingSource: DataSource, blockHeader

hashToBlockNumber.get(hash) match {
case None =>
blockNumberMappingSource.get(BlockNumberMappingStorage.namespace, hash.bytes).map(x => ByteBuffer.wrap(x).getLong) map { blockNumber =>
blockHashSource.get(BlockHashStorage.namespace, hash.bytes).map(x => ByteBuffer.wrap(x).getLong) map { blockNumber =>
blockNumberToHash += (blockNumber -> hash)
hashToBlockNumber += (hash -> blockNumber)
blockNumber
Expand Down
18 changes: 9 additions & 9 deletions khipu-eth/src/main/scala/khipu/store/BlockchainStorages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,32 @@ trait BlockchainStorages {
def storageNodeDataSource: NodeDataSource
def evmcodeDataSource: NodeDataSource

def blockNumberMappingDataSource: DataSource
def blockHashDataSource: DataSource

def blockHeaderDataSource: BlockDataSource
def blockBodyDataSource: BlockDataSource
def receiptsDataSource: BlockDataSource
def totalDifficultyDataSource: BlockDataSource

// -- storage
def transactionMappingStorage: TransactionMappingStorage
def transactionStorage: TransactionStorage
def totalDifficultyStorage: TotalDifficultyStorage

def accountNodeStorageFor: (Option[Long]) => NodeKeyValueStorage
def storageNodeStorageFor: (Option[Long]) => NodeKeyValueStorage
def evmcodeStorage: NodeKeyValueStorage

def blockNumberMappingStorage: BlockNumberMappingStorage
def blockHashStorage: BlockHashStorage
def blockHeaderStorage: BlockHeaderStorage
def blockBodyStorage: BlockBodyStorage
def receiptsStorage: ReceiptsStorage

// ---
def blockNumberMapping: BlockNumberMapping
def blockHashes: BlockHashes

def getBlockNumberByHash(hash: Hash) = blockNumberMapping.getBlockNumberByHash(hash)
def getHashByBlockNumber(blockNumber: Long) = blockNumberMapping.getHashByBlockNumber(blockNumber)
def getHashsByBlockNumberRange(from: Long, to: Long) = blockNumberMapping.getHashsByBlockNumberRange(from, to)
def putBlockNumber(blockNumber: Long, hash: Hash) = blockNumberMapping.putBlockNumber(blockNumber, hash)
def removeBlockNumber(key: Hash) = blockNumberMapping.removeBlockNumber(key)
def getBlockNumberByHash(hash: Hash) = blockHashes.getBlockNumberByHash(hash)
def getHashByBlockNumber(blockNumber: Long) = blockHashes.getHashByBlockNumber(blockNumber)
def getHashsByBlockNumberRange(from: Long, to: Long) = blockHashes.getHashsByBlockNumberRange(from, to)
def putBlockNumber(blockNumber: Long, hash: Hash) = blockHashes.putBlockNumber(blockNumber, hash)
def removeBlockNumber(key: Hash) = blockHashes.removeBlockNumber(key)
}
2 changes: 1 addition & 1 deletion khipu-eth/src/main/scala/khipu/store/KeyValueStorage.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ object Namespaces {
val KnownNodes = Array[Byte]('k'.toByte)
val Heights = Array[Byte]('i'.toByte)
val FastSyncState = Array[Byte]('h'.toByte)
val TransactionMapping = Array[Byte]('l'.toByte)
val Transaction = Array[Byte]('l'.toByte)
}

trait KeyValueStorage[K, V] extends SimpleMap[K, V] {
Expand Down
6 changes: 3 additions & 3 deletions khipu-eth/src/main/scala/khipu/store/Storages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,20 @@ object Storages {
lazy val storageNodeStorageFor: (Option[Long]) => NodeTableStorage = bn => new NodeTableStorage(storageNodeDataSource)
lazy val evmcodeStorage = new NodeTableStorage(evmcodeDataSource)

lazy val blockNumberMappingStorage = new BlockNumberMappingStorage(this, blockNumberMappingDataSource)
lazy val blockHashStorage = new BlockHashStorage(this, blockHashDataSource)

lazy val blockHeaderStorage = new BlockHeaderStorage(this, blockHeaderDataSource)
lazy val blockBodyStorage = new BlockBodyStorage(this, blockBodyDataSource)
lazy val receiptsStorage = new ReceiptsStorage(this, receiptsDataSource)
lazy val totalDifficultyStorage = new TotalDifficultyStorage(this, totalDifficultyDataSource)

lazy val transactionMappingStorage = new TransactionMappingStorage(transactionMappingDataSource)
lazy val transactionStorage = new TransactionStorage(transactionDataSource)

lazy val fastSyncStateStorage = new FastSyncStateStorage(fastSyncStateDataSource)
lazy val appStateStorage = new AppStateStorage(appStateDataSource)
lazy val knownNodesStorage = new KnownNodesStorage(knownNodesDataSource)

lazy val blockNumberMapping = new BlockNumberMapping(blockNumberMappingDataSource, blockHeaderDataSource)
lazy val blockHashes = new BlockHashes(blockHashDataSource, blockHeaderDataSource)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ import java.nio.ByteOrder
import khipu.Hash
import khipu.store.datasource.DataSource

object TransactionMappingStorage {
object TransactionStorage {
final case class TransactionLocation(blockHash: Hash, txIndex: Int)
}
import TransactionMappingStorage._
final class TransactionMappingStorage(val source: DataSource) extends KeyValueStorage[Hash, TransactionLocation] {
type This = TransactionMappingStorage
import TransactionStorage._
final class TransactionStorage(val source: DataSource) extends KeyValueStorage[Hash, TransactionLocation] {
type This = TransactionStorage

implicit val byteOrder = ByteOrder.BIG_ENDIAN

val namespace: Array[Byte] = Namespaces.TransactionMapping
val namespace: Array[Byte] = Namespaces.Transaction
def keySerializer: Hash => Array[Byte] = _.bytes

override def valueSerializer: TransactionLocation => Array[Byte] = tl => {
Expand All @@ -38,6 +38,6 @@ final class TransactionMappingStorage(val source: DataSource) extends KeyValueSt
TransactionLocation(blockHash, txIndex)
}

protected def apply(dataSource: DataSource): TransactionMappingStorage = new TransactionMappingStorage(dataSource)
protected def apply(dataSource: DataSource): TransactionStorage = new TransactionStorage(dataSource)
}

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ trait DataSources {

val dataSource: DataSource

val transactionMappingDataSource: DataSource
val transactionDataSource: DataSource

val blockHeightsHashesDataSource: DataSource

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ trait SharedLeveldbDataSources extends DataSources {

val dataSource = LeveldbDataSource(Config.Db.LeveldbConfig)

val transactionMappingDataSource = dataSource
val transactionDataSource = dataSource

val fastSyncStateDataSource = dataSource
val appStateDataSource = dataSource
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ trait SharedLmdbDataSources extends DataSources {

lazy val dataSource = LmdbDataSource("shared", env)

lazy val transactionMappingDataSource = dataSource
lazy val transactionDataSource = dataSource

lazy val fastSyncStateDataSource = dataSource
lazy val appStateDataSource = dataSource
Expand Down

0 comments on commit 5398dd7

Please sign in to comment.