Skip to content

Commit

Permalink
add message content type
Browse files Browse the repository at this point in the history
  • Loading branch information
blazingsiyan committed Jan 22, 2019
1 parent a37e335 commit c88d341
Show file tree
Hide file tree
Showing 50 changed files with 235 additions and 365 deletions.
2 changes: 1 addition & 1 deletion app/js/src/main/scala/jbok/app/ContractAddress.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import jbok.crypto._

object ContractAddress {
def getContractAddress(address: Address, nonce: UInt256) = {
val hash = (address, UInt256(nonce)).asBytes.kec256
val hash = (address, UInt256(nonce)).asValidBytes.kec256
Address.apply(hash)
}
}
4 changes: 2 additions & 2 deletions app/jvm/src/main/scala/jbok/app/FullNode.scala
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ object FullNode {

// mount rpc
publicAPI = PublicApiImpl(config.history, miner)
privateAPI <- PersonalApiImpl(keystore, history, config.history, executor.txPool)
personalAPI <- PersonalApiImpl(keystore, history, config.history, executor.txPool)
adminAPI = AdminApiImpl(peerManager)
rpc = RpcService().mountAPI(publicAPI).mountAPI(privateAPI).mountAPI(adminAPI)
rpc = RpcService().mountAPI(publicAPI).mountAPI(personalAPI).mountAPI(adminAPI)
server = WsServer.bind(config.rpc.addr, rpc.pipe, metrics, Some(rpc.handle _))
haltWhenTrue <- SignallingRef[IO, Boolean](true)
} yield FullNode[IO](config, syncManager, miner, keystore, rpc, server, haltWhenTrue)
Expand Down
4 changes: 2 additions & 2 deletions benchmark/src/test/scala/jbok/benchmark/CodecBenchmark.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class CodecBenchmark extends JbokBenchmark {
@OperationsPerInvocation(1000)
def derive_1k() =
(0 until 1000).foreach { _ =>
xs(i).asBytes
xs(i).asValidBytes
i = (i + 1) % size
}

Expand All @@ -27,7 +27,7 @@ class CodecBenchmark extends JbokBenchmark {
@OperationsPerInvocation(1000)
def derive_cached_1k() =
(0 until 1000).foreach { _ =>
xs(i).asBytes(codec)
xs(i).asValidBytes(codec)
i = (i + 1) % size
}
}
54 changes: 27 additions & 27 deletions codec/jvm/src/test/scala/jbok/codec/rlp/RlpSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,54 +10,54 @@ class RlpSpec extends JbokSpec with testkit {

"RLP Codec" should {
"encode boolean" in {
false.asBytes shouldBe hex"0x00"
true.asBytes shouldBe hex"0x01"
false.asValidBytes shouldBe hex"0x00"
true.asValidBytes shouldBe hex"0x01"

roundtrip(true)
roundtrip(false)
}

"encode empty bytes" in {
().asBytes shouldBe hex"0x80"
"".asBytes shouldBe hex"0x80"
().asValidBytes shouldBe hex"0x80"
"".asValidBytes shouldBe hex"0x80"
roundtrip("")
}

"encode scalar" in {
0.asBytes shouldBe hex"0x80"
0L.asBytes shouldBe hex"0x80"
BigInt(0).asBytes shouldBe hex"0x80"
0.asValidBytes shouldBe hex"0x80"
0L.asValidBytes shouldBe hex"0x80"
BigInt(0).asValidBytes shouldBe hex"0x80"
roundtrip(0)
roundtrip(0L)
roundtrip(BigInt(0))

0.asBytes.length shouldBe 1
Int.MaxValue.asBytes.length shouldBe 5
0.asValidBytes.length shouldBe 1
Int.MaxValue.asValidBytes.length shouldBe 5
roundtrip(Int.MaxValue)

0L.asBytes.length shouldBe 1
Long.MaxValue.asBytes.length shouldBe 9
0L.asValidBytes.length shouldBe 1
Long.MaxValue.asValidBytes.length shouldBe 9
roundtrip(Long.MaxValue)

BigInt(0).asBytes.length shouldBe 1
BigInt("115792089237316195423570985008687907853269984665640564039457584007913129639935").asBytes.length shouldBe 33
BigInt(0).asValidBytes.length shouldBe 1
BigInt("115792089237316195423570985008687907853269984665640564039457584007913129639935").asValidBytes.length shouldBe 33
}

"encode scalar to the same value" in {
forAll { l: Long =>
if (l >= 0) {
if (l.isValidInt) {
val i = l.toInt
i.asBytes shouldBe l.asBytes
i.asBytes shouldBe BigInt(i).asBytes
i.asValidBytes shouldBe l.asValidBytes
i.asValidBytes shouldBe BigInt(i).asValidBytes
roundtrip(i)
} else {
l.asBytes shouldBe BigInt(l).asBytes
l.asValidBytes shouldBe BigInt(l).asValidBytes
roundtrip(l)
}
} else {
l.asBytesF[IO].attempt.unsafeRunSync().isLeft shouldBe true
BigInt(l).asBytesF[IO].attempt.unsafeRunSync().isLeft shouldBe true
l.asBytes[IO].attempt.unsafeRunSync().isLeft shouldBe true
BigInt(l).asBytes[IO].attempt.unsafeRunSync().isLeft shouldBe true
}
}
}
Expand All @@ -72,37 +72,37 @@ class RlpSpec extends JbokSpec with testkit {

"list codec" in {
RlpCodec[List[Int]].prefixType shouldBe PrefixType.ListLenPrefix
List.empty[Int].asBytes shouldBe hex"0xc0"
List.empty[String].asBytes shouldBe hex"0xc0"
List.empty[Int].asValidBytes shouldBe hex"0xc0"
List.empty[String].asValidBytes shouldBe hex"0xc0"
roundtripAndMatch(List(1, 2, 3), hex"0xc3010203")
}

"derive product(tuple) codec" in {
(1, "abc").asBytes shouldBe hex"0xc50183616263"
(1, 1024 * 1024, "abc").asBytes shouldBe hex"0xc9018310000083616263"
(1, "abc").asValidBytes shouldBe hex"0xc50183616263"
(1, 1024 * 1024, "abc").asValidBytes shouldBe hex"0xc9018310000083616263"
}

"derive product(case class) codec" in {
case class Foo(a: Int, b: Int, c: String)
Foo(42, 1024 * 1024, "abc").asBytes shouldBe hex"0xc92a8310000083616263"
Foo(42, 1024 * 1024, "abc").asValidBytes shouldBe hex"0xc92a8310000083616263"
}

"derive coproduct(ADT) codec" in {
sealed trait Tree
case object Leaf extends Tree
case class Branch(v: Int) extends Tree

Leaf.asBytes shouldBe hex"0xc0"
Branch(42).asBytes shouldBe hex"0xc12a"
Leaf.asValidBytes shouldBe hex"0xc0"
Branch(42).asValidBytes shouldBe hex"0xc12a"

RlpCodec.encode[Tree](Leaf).require.bytes shouldBe hex"0x8200c0"
RlpCodec.encode[Tree](Branch(42)).require.bytes shouldBe hex"0x8301c12a"
}

"derive value class" in {
import RlpSpec._
WrappedInt(42).asBytes shouldBe hex"0x2a"
WrappedString("abc").asBytes shouldBe hex"0x83616263"
WrappedInt(42).asValidBytes shouldBe hex"0x2a"
WrappedString("abc").asValidBytes shouldBe hex"0x83616263"
}

"codec https://github.com/ethereum/wiki/wiki/RLP" in {
Expand Down
2 changes: 1 addition & 1 deletion codec/shared/src/main/scala/jbok/codec/rlp/RlpCodec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import scala.language.experimental.macros

sealed trait PrefixType
object PrefixType {
case object NoPrefix extends PrefixType
case object NoPrefix extends PrefixType
case object ItemLenPrefix extends PrefixType
case object ListLenPrefix extends PrefixType
}
Expand Down
6 changes: 2 additions & 4 deletions codec/shared/src/main/scala/jbok/codec/rlp/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,8 @@ package object rlp {
}

final class CodecOps[A](val a: A) extends AnyVal {
def asBits(implicit codec: RlpCodec[A]): BitVector = codec.encode(a).require
def asBytes(implicit codec: RlpCodec[A]): ByteVector = asBits.bytes
def asBitsF[F[_]: Sync](implicit codec: RlpCodec[A]): F[BitVector] = Sync[F].delay(asBits)
def asBytesF[F[_]: Sync](implicit codec: RlpCodec[A]): F[ByteVector] = Sync[F].delay(asBytes)
def asValidBytes(implicit codec: RlpCodec[A]): ByteVector = codec.encode(a).require.bytes
def asBytes[F[_]: Sync](implicit codec: RlpCodec[A]): F[ByteVector] = Sync[F].delay(asValidBytes)
}

final class BitsDecodeOps(val bits: BitVector) extends AnyVal {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ object PeerManagerPlatform {
handshaker <- AuthHandshaker[F](keyPair)
result <- handshaker.accept(conn)
localStatus <- localStatus
request <- Request[F, Status]("Status", localStatus)
request <- Request.binary[F, Status]("Status", localStatus)
_ <- conn.write(request)
remoteStatus <- conn.read.flatMap(_.bodyAs[Status])
remoteStatus <- conn.read.flatMap(_.binaryBodyAs[Status])
_ <- if (!localStatus.isCompatible(remoteStatus)) {
F.raiseError(PeerErr.Incompatible(localStatus, remoteStatus))
} else {
Expand All @@ -54,9 +54,9 @@ object PeerManagerPlatform {
handshaker <- AuthHandshaker[F](keyPair)
result <- handshaker.connect(conn, remotePk)
localStatus <- localStatus
request <- Request[F, Status]("Status", localStatus)
request <- Request.binary[F, Status]("Status", localStatus)
_ <- conn.write(request)
remoteStatus <- conn.read.flatMap(_.bodyAs[Status])
remoteStatus <- conn.read.flatMap(_.binaryBodyAs[Status])
_ <- if (!localStatus.isCompatible(remoteStatus)) {
F.raiseError(PeerErr.Incompatible(localStatus, remoteStatus))
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ final case class AuthHandshaker[F[_]](
def initiate(remotePk: KeyPair.Public): F[(ByteVector, AuthHandshaker[F])] =
for {
message <- createAuthInitiateMessageV4(remotePk)
encoded = RlpCodec.encode(message).require.bytes.toArray
encoded = message.asValidBytes.toArray
padded = encoded ++ randomBytes(Random.nextInt(MaxPadding - MinPadding) + MinPadding)
encryptedSize = padded.length + ECIES.OverheadSize
sizePrefix = ByteBuffer.allocate(2).putShort(encryptedSize.toShort).array
Expand All @@ -65,7 +65,7 @@ final case class AuthHandshaker[F[_]](
): F[AuthHandshakeResult] =
for {
(initPacket, initHandshaker) <- initiate(remotePk)
message <- Request[F, AuthPacket]("AuthPacket", AuthPacket(initPacket))
message <- Request.binary[F, AuthPacket]("AuthPacket", AuthPacket(initPacket))
resp <- conn.expect[AuthPacket](message)
_ <- F.delay(log.trace(s"write init packet ${initPacket.length}, wait for remote response"))
result <- initHandshaker.handleResponseMessageAll(resp.bytes)
Expand All @@ -78,7 +78,7 @@ final case class AuthHandshaker[F[_]](
for {
_ <- F.delay(log.trace(s"wait for remote init packet"))
req <- conn.read
data <- req.bodyAs[AuthPacket]
data <- req.binaryBodyAs[AuthPacket]
_ <- F.delay(log.trace(s"got remote init packet ${data.bytes.length}"))
(response, result) <- handleInitialMessageAll(data.bytes)
_ <- F.delay(log.trace(s"handshake accept ${result}"))
Expand Down Expand Up @@ -169,7 +169,7 @@ final case class AuthHandshaker[F[_]](
nonce = nonce,
version = ProtocolVersion
)
encodedResponse = RlpCodec.encode(response).require.toByteArray
encodedResponse = response.asValidBytes.toArray

encryptedSize = encodedResponse.length + ECIES.OverheadSize
sizePrefix = ByteBuffer.allocate(2).putShort(encryptedSize.toShort).array
Expand Down
142 changes: 0 additions & 142 deletions core/jvm/src/test/scala/jbok/core/Fixtures.scala

This file was deleted.

Loading

0 comments on commit c88d341

Please sign in to comment.