-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'WE-8753-invalid-block-fix' into 'release-1.13'
WE-8753 - Invalid block fix Closes WE-8756 See merge request development/we/node/open-source-node!25
- Loading branch information
Showing
10 changed files
with
329 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
node/src/main/scala/com/wavesenterprise/api/ValidLong.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package com.wavesenterprise.api | ||
|
||
import akka.http.scaladsl.server.Directives.complete | ||
import akka.http.scaladsl.server.StandardRoute | ||
import cats.data.{NonEmptyList, Validated} | ||
import cats.implicits.catsSyntaxEither | ||
import cats.instances.list.catsStdInstancesForList | ||
import cats.syntax.traverse.toTraverseOps | ||
import com.wavesenterprise.api.http.ApiError | ||
import enumeratum.{Enum, EnumEntry} | ||
|
||
import scala.collection.immutable | ||
|
||
sealed trait ValidLong extends EnumEntry { | ||
protected def validation: Long => Boolean | ||
protected def description: String | ||
|
||
protected final def validate(i: Long): Validated[String, Long] = { | ||
Validated.cond(validation(i), i, s"'$i' must be $description") | ||
} | ||
|
||
protected final def validateStr(str: String): Validated[String, Long] = { | ||
Validated | ||
.catchOnly[NumberFormatException](str.toLong) | ||
.leftMap(_ => s"Unable to parse Long from '$str'") | ||
.andThen(validate) | ||
} | ||
} | ||
|
||
object ValidLong extends Enum[ValidLong] { | ||
override val values: immutable.IndexedSeq[ValidLong] = findValues | ||
|
||
case object PositiveLong extends ValidLong { | ||
override protected val validation: Long => Boolean = _ > 0 | ||
override protected val description: String = "positive" | ||
|
||
def apply(str: String): Validated[String, Long] = validateStr(str) | ||
def apply(i: Long): Validated[String, Long] = validate(i) | ||
} | ||
|
||
case object NonNegativeLong extends ValidLong { | ||
override protected val validation: Long => Boolean = _ >= 0 | ||
override protected val description: String = "non-negative" | ||
|
||
def apply(str: String): Validated[String, Long] = validateStr(str) | ||
def apply(i: Long): Validated[String, Long] = validate(i) | ||
} | ||
|
||
implicit class ValidatedLongListExt(private val v: List[Validated[String, Long]]) extends AnyVal { | ||
def toApiError: Either[ApiError, List[Long]] = { | ||
v.traverse(_.leftMap(NonEmptyList.of(_))).toEither.leftMap { errors => | ||
ApiError.CustomValidationError(s"Invalid parameters: [${errors.toList.mkString(", ")}]") | ||
} | ||
} | ||
|
||
def processRoute(f: List[Long] => StandardRoute): StandardRoute = { | ||
v.toApiError match { | ||
case Right(validLongs) => f(validLongs) | ||
case Left(apiError) => complete(apiError) | ||
} | ||
} | ||
} | ||
|
||
implicit class ValidatedLongExt(private val v: Validated[String, Long]) extends AnyVal { | ||
def toApiError[T]: Either[ApiError, Long] = { | ||
v.toEither.leftMap(err => ApiError.CustomValidationError(s"Invalid parameter: $err")) | ||
} | ||
|
||
def processRoute(f: Long => StandardRoute): StandardRoute = { | ||
v.toApiError match { | ||
case Right(validLong) => f(validLong) | ||
case Left(apiError) => complete(apiError) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
158 changes: 158 additions & 0 deletions
158
node/src/main/scala/com/wavesenterprise/database/migration/MainnetMigration.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
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.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.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 modernContractInfoKey(contractId: ByteStr)(height: Int): MainDBKey[Option[ModernContractInfo]] = | ||
MainDBKey.opt("contract", ContractCF, hBytes(ContractPrefix, height, contractId.arr), parseModernContractInfo, writeModernContractInfo) | ||
} | ||
|
||
private val ContractsIdSet = new InternalRocksDBSet[ByteStr, MainDBColumnFamily]( | ||
name = "contract-ids", | ||
columnFamily = ContractCF, | ||
prefix = Shorts.toByteArray(ContractIdsPrefix), | ||
itemEncoder = (_: ByteStr).arr, | ||
itemDecoder = ByteStr(_), | ||
keyConstructors = MainDBKey | ||
) | ||
|
||
def apply(rw: MainReadWriteDB): Unit = { | ||
for { | ||
contractId <- ContractsIdSet.members(rw) | ||
contractHistory = rw.get(WEKeys.contractHistory(contractId)) | ||
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)) | ||
} | ||
} | ||
|
||
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.