Skip to content

Commit

Permalink
Merge branch 'patch-1.14.1' into 'release-1.14'
Browse files Browse the repository at this point in the history
Patch 1.14.1

See merge request development/we/node/open-source-node!28
  • Loading branch information
mikonnikov committed Apr 1, 2024
2 parents 92b08ba + e6526ab commit bb891ae
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 124 deletions.
Original file line number Diff line number Diff line change
@@ -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](
Expand All @@ -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)
}
}
2 changes: 1 addition & 1 deletion project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit bb891ae

Please sign in to comment.