diff --git a/node/src/main/scala/com/wavesenterprise/database/migration/MainnetMigration.scala b/node/src/main/scala/com/wavesenterprise/database/migration/MainnetMigration.scala index 8501e5b..a6576e5 100644 --- a/node/src/main/scala/com/wavesenterprise/database/migration/MainnetMigration.scala +++ b/node/src/main/scala/com/wavesenterprise/database/migration/MainnetMigration.scala @@ -1,29 +1,24 @@ package com.wavesenterprise.database.migration -import com.google.common.io.ByteArrayDataOutput -import com.google.common.io.ByteStreams.newDataOutput -import com.google.common.primitives.{Ints, Shorts} -import com.wavesenterprise.account.{Address, PublicKeyAccount} -import com.wavesenterprise.crypto +import com.google.common.primitives.Shorts import com.wavesenterprise.database.KeyHelpers.hBytes import com.wavesenterprise.database.keys.ContractCFKeys.{ContractIdsPrefix, ContractPrefix} import com.wavesenterprise.database.rocksdb.MainDBColumnFamily.ContractCF import com.wavesenterprise.database.rocksdb.{MainDBColumnFamily, MainReadWriteDB} import com.wavesenterprise.database.{InternalRocksDBSet, MainDBKey, WEKeys} -import com.wavesenterprise.docker.ContractApiVersion +import com.wavesenterprise.docker.StoredContract.DockerContract +import com.wavesenterprise.docker.{ContractApiVersion, ContractInfo} import com.wavesenterprise.docker.validator.ValidationPolicy -import com.wavesenterprise.serialization.{BinarySerializer, ModelsBinarySerializer} import com.wavesenterprise.state.ByteStr -import com.wavesenterprise.utils.DatabaseUtils.ByteArrayDataOutputExt object MainnetMigration { object KeysInfo { - def legacyContractInfoKey(contractId: ByteStr)(height: Int): MainDBKey[Option[LegacyContractInfo]] = - MainDBKey.opt("contract", ContractCF, hBytes(ContractPrefix, height, contractId.arr), parseLegacyContractInfo, writeLegacyContractInfo) + def legacyContractInfoKey(contractId: ByteStr)(height: Int): MainDBKey[Option[ContractInfo]] = + MainDBKey.opt("contract", ContractCF, hBytes(ContractPrefix, height, contractId.arr), ContractInfo.fromBytes, ContractInfo.toBytes) - def modernContractInfoKey(contractId: ByteStr)(height: Int): MainDBKey[Option[ModernContractInfo]] = - MainDBKey.opt("contract", ContractCF, hBytes(ContractPrefix, height, contractId.arr), parseModernContractInfo, writeModernContractInfo) + def modernContractInfoKey(contractId: ByteStr)(height: Int): MainDBKey[Option[ContractInfo]] = + MainDBKey.opt("contract", ContractCF, hBytes(ContractPrefix, height, contractId.arr), ContractInfo.fromBytes, ContractInfo.toBytes) } private val ContractsIdSet = new InternalRocksDBSet[ByteStr, MainDBColumnFamily]( @@ -42,117 +37,17 @@ object MainnetMigration { height <- contractHistory oldContractInfo <- rw.get(KeysInfo.legacyContractInfoKey(contractId)(height)).toSeq } yield { - val newContractInfo = ModernContractInfo( - creator = oldContractInfo.creator, - contractId = oldContractInfo.contractId, - image = oldContractInfo.image, - imageHash = oldContractInfo.imageHash, - version = oldContractInfo.version, - active = oldContractInfo.active, - validationPolicy = ValidationPolicy.Default, - apiVersion = ContractApiVersion.Initial, - isConfidential = false, - groupParticipants = Set(), - groupOwners = Set() - ) - rw.put(KeysInfo.modernContractInfoKey(contractId)(height), Some(newContractInfo)) + oldContractInfo.storedContract match { + case storedContract: DockerContract => { + val newStoredContract = storedContract.copy(apiVersion = ContractApiVersion.Initial) + val newContractInfo = oldContractInfo.copy( + storedContract = newStoredContract, + validationPolicy = ValidationPolicy.Default, + ) + + rw.put(KeysInfo.modernContractInfoKey(contractId)(height), Some(newContractInfo)) + } + } } } - - case class LegacyContractInfo(creator: PublicKeyAccount, - contractId: ByteStr, - image: String, - imageHash: String, - version: Int, - active: Boolean, - validationPolicy: ValidationPolicy, - apiVersion: ContractApiVersion) - - case class ModernContractInfo(creator: PublicKeyAccount, - contractId: ByteStr, - image: String, - imageHash: String, - version: Int, - active: Boolean, - validationPolicy: ValidationPolicy, - apiVersion: ContractApiVersion, - isConfidential: Boolean, - groupParticipants: Set[Address], - groupOwners: Set[Address]) - - def writeLegacyContractInfo(contractInfo: LegacyContractInfo): Array[Byte] = { - import contractInfo._ - val ndo = newDataOutput() - ndo.writePublicKey(creator) - ndo.writeBytes(contractId.arr) - ndo.writeString(image) - ndo.writeString(imageHash) - ndo.writeInt(version) - ndo.writeBoolean(active) - ndo.write(contractInfo.validationPolicy.bytes) - ndo.write(contractInfo.apiVersion.bytes) - ndo.toByteArray - } - - def parseLegacyContractInfo(bytes: Array[Byte]): LegacyContractInfo = { - val (creatorBytes, creatorEnd) = bytes.take(crypto.KeyLength) -> crypto.KeyLength - val (contractId, contractIdEnd) = BinarySerializer.parseShortByteStr(bytes, creatorEnd) - val (image, imageEnd) = BinarySerializer.parseShortString(bytes, contractIdEnd) - val (imageHash, imageHashEnd) = BinarySerializer.parseShortString(bytes, imageEnd) - val (version, versionEnd) = Ints.fromByteArray(bytes.slice(imageHashEnd, imageHashEnd + Ints.BYTES)) -> (imageHashEnd + Ints.BYTES) - val (active, activeEnd) = (bytes(versionEnd) == 1) -> (versionEnd + 1) - val (validationPolicy, validationPolicyEnd) = ValidationPolicy.fromBytesUnsafe(bytes, activeEnd) - val (apiVersion, _) = ContractApiVersion.fromBytesUnsafe(bytes, validationPolicyEnd) - - LegacyContractInfo(PublicKeyAccount(creatorBytes), contractId, image, imageHash, version, active, validationPolicy, apiVersion) - } - - def writeModernContractInfo(contractInfo: ModernContractInfo): Array[Byte] = { - def addressWriter(address: Address, output: ByteArrayDataOutput): Unit = { - output.write(address.bytes.arr) - } - - import contractInfo._ - val ndo = newDataOutput() - ndo.writePublicKey(creator) - ndo.writeBytes(contractId.arr) - ndo.writeString(image) - ndo.writeString(imageHash) - ndo.writeInt(version) - ndo.writeBoolean(active) - ndo.write(contractInfo.validationPolicy.bytes) - ndo.write(contractInfo.apiVersion.bytes) - ndo.writeBoolean(isConfidential) - BinarySerializer.writeShortIterable(contractInfo.groupParticipants, addressWriter, ndo) - BinarySerializer.writeShortIterable(contractInfo.groupOwners, addressWriter, ndo) - - ndo.toByteArray - } - - def parseModernContractInfo(bytes: Array[Byte]): ModernContractInfo = { - - val (creatorBytes, creatorEnd) = bytes.take(crypto.KeyLength) -> crypto.KeyLength - val (contractId, contractIdEnd) = BinarySerializer.parseShortByteStr(bytes, creatorEnd) - val (image, imageEnd) = BinarySerializer.parseShortString(bytes, contractIdEnd) - val (imageHash, imageHashEnd) = BinarySerializer.parseShortString(bytes, imageEnd) - val (version, versionEnd) = Ints.fromByteArray(bytes.slice(imageHashEnd, imageHashEnd + Ints.BYTES)) -> (imageHashEnd + Ints.BYTES) - val (active, activeEnd) = (bytes(versionEnd) == 1) -> (versionEnd + 1) - val (validationPolicy, validationPolicyEnd) = ValidationPolicy.fromBytesUnsafe(bytes, activeEnd) - val (apiVersion, apiVersionEnd) = ContractApiVersion.fromBytesUnsafe(bytes, validationPolicyEnd) - val (isConfidential, isConfidentialEnd) = (bytes(apiVersionEnd) == 1) -> (apiVersionEnd + 1) - val (groupParticipants, groupParticipantsEnd) = ModelsBinarySerializer.parseAddressesSet(bytes, isConfidentialEnd) - val (groupOwners, _) = ModelsBinarySerializer.parseAddressesSet(bytes, groupParticipantsEnd) - - ModernContractInfo(PublicKeyAccount(creatorBytes), - contractId, - image, - imageHash, - version, - active, - validationPolicy, - apiVersion, - isConfidential, - groupParticipants, - groupOwners) - } } diff --git a/project/Dependencies.scala b/project/Dependencies.scala index 97422b8..92d8b01 100644 --- a/project/Dependencies.scala +++ b/project/Dependencies.scala @@ -30,7 +30,7 @@ object Dependencies { "com.wavesenterprise" % "we-test-core" % weCoreVersion % Test ) - lazy val wevm = "com.wavesenterprise" % "wevm_2.12" % "0.2.1" + lazy val wevm = "com.wavesenterprise" % "wevm_2.12" % "0.2.2" lazy val netty = Seq("handler", "buffer", "codec", "codec-http2").map(nettyModule)