This repository has been archived by the owner on Apr 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #178 from atoulme/les
Add LES subprotocol to Cava
- Loading branch information
Showing
17 changed files
with
1,706 additions
and
13 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
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,25 @@ | ||
dependencies { | ||
compile project(':bytes') | ||
compile project(':concurrent') | ||
compile project(':concurrent-coroutines') | ||
compile project(':crypto') | ||
compile project(':eth') | ||
compile project(':eth-repository') | ||
compile project(':kv') | ||
compile project(':rlpx') | ||
compile 'com.google.guava:guava' | ||
compile 'org.logl:logl-api' | ||
|
||
compileOnly 'io.vertx:vertx-core' | ||
compile 'org.xerial.snappy:snappy-java' | ||
compile 'org.bouncycastle:bcprov-jdk15on' | ||
|
||
testCompile project(':junit') | ||
testCompile 'io.vertx:vertx-core' | ||
testCompile 'org.bouncycastle:bcprov-jdk15on' | ||
testCompile 'org.junit.jupiter:junit-jupiter-api' | ||
testCompile 'org.junit.jupiter:junit-jupiter-params' | ||
testCompile 'org.logl:logl-logl' | ||
|
||
testRuntime 'org.junit.jupiter:junit-jupiter-engine' | ||
} |
47 changes: 47 additions & 0 deletions
47
les/src/main/kotlin/net/consensys/cava/les/BlockBodiesMessage.kt
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,47 @@ | ||
/* | ||
* Copyright 2019 ConsenSys AG. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
package net.consensys.cava.les | ||
|
||
import net.consensys.cava.bytes.Bytes | ||
import net.consensys.cava.eth.BlockBody | ||
import net.consensys.cava.rlp.RLP | ||
|
||
internal data class BlockBodiesMessage( | ||
val reqID: Long, | ||
val bufferValue: Long, | ||
val blockBodies: List<BlockBody> | ||
) { | ||
|
||
fun toBytes(): Bytes { | ||
return RLP.encodeList { writer -> | ||
writer.writeLong(reqID) | ||
writer.writeLong(bufferValue) | ||
writer.writeList(blockBodies) { eltWriter, blockBody -> blockBody.writeTo(eltWriter) } | ||
} | ||
} | ||
|
||
companion object { | ||
|
||
fun read(bytes: Bytes): BlockBodiesMessage { | ||
return RLP.decodeList( | ||
bytes | ||
) { reader -> | ||
BlockBodiesMessage( | ||
reader.readLong(), | ||
reader.readLong(), | ||
reader.readListContents { BlockBody.readFrom(it) } | ||
) | ||
} | ||
} | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
les/src/main/kotlin/net/consensys/cava/les/BlockHeadersMessage.kt
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,52 @@ | ||
/* | ||
* Copyright 2019 ConsenSys AG. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
package net.consensys.cava.les | ||
|
||
import net.consensys.cava.bytes.Bytes | ||
import net.consensys.cava.eth.BlockHeader | ||
import net.consensys.cava.rlp.RLP | ||
|
||
internal data class BlockHeadersMessage( | ||
val reqID: Long, | ||
val bufferValue: Long, | ||
val blockHeaders: List<BlockHeader> | ||
) { | ||
|
||
fun toBytes(): Bytes { | ||
return RLP.encodeList { writer -> | ||
writer.writeLong(reqID) | ||
writer.writeLong(bufferValue) | ||
writer.writeList { headersWriter -> | ||
for (bh in blockHeaders) { | ||
headersWriter.writeRLP(bh.toBytes()) | ||
} | ||
} | ||
} | ||
} | ||
|
||
companion object { | ||
|
||
fun read(bytes: Bytes): BlockHeadersMessage { | ||
return RLP.decodeList(bytes) { reader -> | ||
val reqID = reader.readLong() | ||
val bufferValue = reader.readLong() | ||
val headers = reader.readListContents { headersReader -> | ||
headersReader.readList<BlockHeader> { | ||
BlockHeader.readFrom(it) | ||
} | ||
} | ||
BlockHeadersMessage(reqID, bufferValue, headers) | ||
} | ||
} | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
les/src/main/kotlin/net/consensys/cava/les/GetBlockBodiesMessage.kt
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,40 @@ | ||
/* | ||
* Copyright 2019 ConsenSys AG. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
package net.consensys.cava.les | ||
|
||
import net.consensys.cava.bytes.Bytes | ||
import net.consensys.cava.eth.Hash | ||
import net.consensys.cava.rlp.RLP | ||
|
||
internal data class GetBlockBodiesMessage(val reqID: Long, val blockHashes: List<Hash>) { | ||
|
||
fun toBytes(): Bytes { | ||
return RLP.encodeList { writer -> | ||
writer.writeLong(reqID) | ||
writer.writeList(blockHashes) { eltWriter, hash -> eltWriter.writeValue(hash.toBytes()) } | ||
} | ||
} | ||
|
||
companion object { | ||
|
||
fun read(bytes: Bytes): GetBlockBodiesMessage { | ||
return RLP.decodeList( | ||
bytes | ||
) { reader -> | ||
GetBlockBodiesMessage( | ||
reader.readLong(), | ||
reader.readListContents { elementReader -> Hash.fromBytes(elementReader.readValue()) }) | ||
} | ||
} | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
les/src/main/kotlin/net/consensys/cava/les/GetBlockHeadersMessage.kt
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,72 @@ | ||
/* | ||
* Copyright 2019 ConsenSys AG. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
package net.consensys.cava.les | ||
|
||
import net.consensys.cava.bytes.Bytes | ||
import net.consensys.cava.bytes.Bytes32 | ||
import net.consensys.cava.rlp.RLP | ||
import net.consensys.cava.units.bigints.UInt256 | ||
|
||
internal data class GetBlockHeadersMessage(val reqID: Long, val queries: List<BlockHeaderQuery>) { | ||
|
||
internal data class BlockHeaderQuery( | ||
val blockNumberOrBlockHash: Bytes32, | ||
val maxHeaders: UInt256, | ||
val skip: UInt256, | ||
val direction: Direction | ||
) { | ||
|
||
internal enum class Direction { | ||
BACKWARDS, FORWARD | ||
} | ||
} | ||
|
||
fun toBytes(): Bytes { | ||
return RLP.encodeList { writer -> | ||
writer.writeLong(reqID) | ||
for (query in queries) { | ||
writer.writeList { queryWriter -> | ||
queryWriter.writeValue(query.blockNumberOrBlockHash) | ||
queryWriter.writeUInt256(query.maxHeaders) | ||
queryWriter.writeUInt256(query.skip) | ||
queryWriter.writeInt(if (query.direction == BlockHeaderQuery.Direction.BACKWARDS) 1 else 0) | ||
} | ||
} | ||
} | ||
} | ||
|
||
companion object { | ||
|
||
fun read(bytes: Bytes): GetBlockHeadersMessage { | ||
return RLP.decodeList(bytes) { reader -> | ||
val reqId = reader.readLong() | ||
val queries = ArrayList<BlockHeaderQuery>() | ||
while (!reader.isComplete) { | ||
queries.add( | ||
reader.readList { queryReader -> | ||
BlockHeaderQuery( | ||
Bytes32.wrap(queryReader.readValue()), | ||
queryReader.readUInt256(), | ||
queryReader.readUInt256(), | ||
if (queryReader.readInt() == 1) | ||
BlockHeaderQuery.Direction.BACKWARDS | ||
else | ||
BlockHeaderQuery.Direction.FORWARD | ||
) | ||
}) | ||
} | ||
GetBlockHeadersMessage(reqId, queries) | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.