Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transactions deserialization fix. #102

Merged
merged 1 commit into from Jun 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -1,7 +1,5 @@
package scorex.transaction

import java.math.BigInteger

import com.google.common.primitives.{Bytes, Ints, Longs}
import play.api.libs.json.{JsObject, Json}
import scorex.account.Account
Expand All @@ -10,7 +8,7 @@ import scorex.crypto.hash.FastCryptographicHash._
import scorex.serialization.Deser
import scorex.transaction.LagonakiTransaction.TransactionType

import scala.util.Try
import scala.util.{Failure, Try}


case class GenesisTransaction(override val recipient: Account,
Expand Down Expand Up @@ -87,7 +85,16 @@ object GenesisTransaction extends Deser[GenesisTransaction] {
Bytes.concat(h, h)
}

def parseBytes(data: Array[Byte]): Try[GenesisTransaction] = Try {
def parseBytes(data: Array[Byte]): Try[GenesisTransaction] = {
data.head match {
case transactionType: Byte if transactionType == TransactionType.GenesisTransaction.id =>
parseTail(data.tail)
case transactionType =>
Failure(new Exception(s"Incorrect transaction type '$transactionType' in GenesisTransaction data"))
}
}

def parseTail(data: Array[Byte]): Try[GenesisTransaction] = Try {
require(data.length >= BASE_LENGTH, "Data does not match base length")

var position = 0
Expand Down
Expand Up @@ -92,10 +92,10 @@ object LagonakiTransaction extends Deser[LagonakiTransaction] {
def parseBytes(data: Array[Byte]): Try[LagonakiTransaction] =
data.head match {
case txType: Byte if txType == TransactionType.GenesisTransaction.id =>
GenesisTransaction.parseBytes(data.tail)
GenesisTransaction.parseTail(data.tail)

case txType: Byte if txType == TransactionType.PaymentTransaction.id =>
PaymentTransaction.parseBytes(data.tail)
PaymentTransaction.parseTail(data.tail)

case txType => Failure(new Exception(s"Invalid transaction type: $txType"))
}
Expand Down
Expand Up @@ -10,7 +10,7 @@ import scorex.crypto.encode.Base58
import scorex.serialization.Deser
import scorex.transaction.LagonakiTransaction.TransactionType

import scala.util.Try
import scala.util.{Failure, Try}

case class PaymentTransaction(sender: PublicKeyAccount,
override val recipient: Account,
Expand Down Expand Up @@ -91,7 +91,16 @@ object PaymentTransaction extends Deser[PaymentTransaction] {
PaymentTransaction(sender, recipient, amount, fee, timestamp, sig)
}

def parseBytes(data: Array[Byte]): Try[PaymentTransaction] = Try {
def parseBytes(data: Array[Byte]): Try[PaymentTransaction] = {
data.head match {
case transactionType: Byte if transactionType == TransactionType.PaymentTransaction.id =>
parseTail(data.tail)
case transactionType =>
Failure(new Exception(s"Incorrect transaction type '$transactionType' in PaymentTransaction data"))
}
}

def parseTail(data: Array[Byte]): Try[PaymentTransaction] = Try {
require(data.length >= BaseLength, "Data does not match base length")

var position = 0
Expand Down
Expand Up @@ -26,7 +26,7 @@ class GenesisTransactionSpecification extends PropSpec with PropertyChecks with
val expectedTransaction = new GenesisTransaction(defaultRecipient, balance, timestamp)

val bytes = Base58.decode("5GoidY2PcCc7ENdrcapZcmmdq2H57NuiXEdgVkpfnnzkB4o8R575WVR1Xw").get
val actualTransaction = GenesisTransaction.parseBytes(bytes.tail).get
val actualTransaction = GenesisTransaction.parseBytes(bytes).get

actualTransaction should equal(expectedTransaction)
}
Expand All @@ -38,7 +38,7 @@ class GenesisTransactionSpecification extends PropSpec with PropertyChecks with
val recipient = new PrivateKeyAccount(recipientSeed)
val source = new GenesisTransaction(recipient, amount, time)
val bytes = source.bytes
val dest = GenesisTransaction.parseBytes(bytes.tail).get
val dest = GenesisTransaction.parseBytes(bytes).get

source should equal(dest)
}
Expand Down
Expand Up @@ -72,7 +72,7 @@ with Matchers {
val sender = new PrivateKeyAccount(senderSeed)
val recipient = new PrivateKeyAccount(recipientSeed)
val tx = PaymentTransaction(sender, recipient, amount, fee, time)
val txAfter = LagonakiTransaction.parseBytes(tx.bytes).get
val txAfter = PaymentTransaction.parseBytes(tx.bytes).get

txAfter.getClass.shouldBe(tx.getClass)

Expand All @@ -86,4 +86,30 @@ with Matchers {
txAfter.signatureValid shouldEqual true
}
}

property("PaymentTransaction should deserialize to LagonakiTransaction") {
forAll {
(senderSeed: Array[Byte],
recipientSeed: Array[Byte],
time: Long,
amount: Long,
fee: Long) =>

val sender = new PrivateKeyAccount(senderSeed)
val recipient = new PrivateKeyAccount(recipientSeed)
val tx = PaymentTransaction(sender, recipient, amount, fee, time)
val txAfter = LagonakiTransaction.parseBytes(tx.bytes).get

txAfter.getClass.shouldBe(tx.getClass)

tx.dataLength shouldEqual txAfter.dataLength
tx.signature shouldEqual txAfter.signature
tx.sender shouldEqual txAfter.asInstanceOf[PaymentTransaction].sender
tx.recipient shouldEqual txAfter.recipient
tx.timestamp shouldEqual txAfter.timestamp
tx.amount shouldEqual txAfter.amount
tx.fee shouldEqual txAfter.fee
txAfter.signatureValid shouldEqual true
}
}
}