├── .circleci └── config.yml ├── .editorconfig ├── .gitattributes ├── .gitignore ├── .gitmodules ├── .idea └── codeStyles │ ├── Project.xml │ └── codeStyleConfig.xml ├── CONTRIBUTING.md ├── LICENSE ├── PACKAGES.md ├── README.md ├── build.gradle ├── bytes ├── build.gradle └── src │ ├── main │ └── java │ │ └── net │ │ └── consensys │ │ └── cava │ │ └── bytes │ │ ├── AbstractBytes.java │ │ ├── ArrayWrappingBytes.java │ │ ├── ArrayWrappingBytes32.java │ │ ├── ArrayWrappingBytes48.java │ │ ├── BufferWrappingBytes.java │ │ ├── ByteBufWrappingBytes.java │ │ ├── ByteBufferWrappingBytes.java │ │ ├── Bytes.java │ │ ├── Bytes32.java │ │ ├── Bytes48.java │ │ ├── BytesValues.java │ │ ├── ConcatenatedBytes.java │ │ ├── DelegatingBytes32.java │ │ ├── DelegatingBytes48.java │ │ ├── DelegatingMutableBytes32.java │ │ ├── DelegatingMutableBytes48.java │ │ ├── MutableArrayWrappingBytes.java │ │ ├── MutableArrayWrappingBytes32.java │ │ ├── MutableArrayWrappingBytes48.java │ │ ├── MutableBufferWrappingBytes.java │ │ ├── MutableByteBufWrappingBytes.java │ │ ├── MutableByteBufferWrappingBytes.java │ │ ├── MutableBytes.java │ │ ├── MutableBytes32.java │ │ ├── MutableBytes48.java │ │ └── package-info.java │ └── test │ └── java │ └── net │ └── consensys │ └── cava │ └── bytes │ ├── BufferBytesTest.java │ ├── ByteBufBytesTest.java │ ├── ByteBufferBytesTest.java │ ├── Bytes32Test.java │ ├── Bytes48Test.java │ ├── BytesTest.java │ ├── CommonBytesTests.java │ └── ConcatenatedBytesTest.java ├── concurrent-coroutines ├── build.gradle └── src │ ├── main │ └── kotlin │ │ └── net │ │ └── consensys │ │ └── cava │ │ └── concurrent │ │ └── coroutines │ │ ├── AsyncCompletion.kt │ │ ├── AsyncResult.kt │ │ ├── CoroutineLatch.kt │ │ └── Retryable.kt │ └── test │ └── kotlin │ └── net │ └── consensys │ └── cava │ └── concurrent │ └── coroutines │ ├── CoroutineLatchTest.kt │ └── RetryableTest.kt ├── concurrent ├── build.gradle └── src │ ├── main │ └── java │ │ └── net │ │ └── consensys │ │ └── cava │ │ └── concurrent │ │ ├── AsyncCompletion.java │ │ ├── AsyncResult.java │ │ ├── AtomicSlotMap.java │ │ ├── CompletableAsyncCompletion.java │ │ ├── CompletableAsyncResult.java │ │ ├── DefaultCompletableAsyncCompletion.java │ │ ├── DefaultCompletableAsyncResult.java │ │ ├── ExpiringMap.java │ │ ├── ExpiringSet.java │ │ └── package-info.java │ └── test │ └── java │ └── net │ └── consensys │ └── cava │ └── concurrent │ ├── AtomicSlotMapTest.java │ ├── DefaultCompletableAsyncCompletionTest.java │ ├── DefaultCompletableAsyncResultTest.java │ ├── ExpiringMapTest.java │ └── ExpiringSetTest.java ├── config ├── build.gradle └── src │ ├── main │ └── java │ │ └── net │ │ └── consensys │ │ └── cava │ │ └── config │ │ ├── Configuration.java │ │ ├── ConfigurationError.java │ │ ├── ConfigurationErrors.java │ │ ├── ConfigurationValidator.java │ │ ├── DocumentPosition.java │ │ ├── EmptyConfiguration.java │ │ ├── InvalidConfigurationPropertyTypeException.java │ │ ├── NoConfigurationPropertyException.java │ │ ├── PropertyValidator.java │ │ ├── PropertyValidators.java │ │ ├── Schema.java │ │ ├── SchemaBuilder.java │ │ ├── TomlBackedConfiguration.java │ │ ├── TomlSerializer.java │ │ └── package-info.java │ └── test │ └── java │ └── net │ └── consensys │ └── cava │ └── config │ ├── PropertyValidatorTest.java │ ├── SchemaBuilderTest.java │ └── TomlBackedConfigurationTest.java ├── crypto ├── build.gradle └── src │ ├── main │ └── java │ │ └── net │ │ └── consensys │ │ └── cava │ │ └── crypto │ │ ├── Hash.java │ │ ├── InvalidSEC256K1SecretKeyStoreException.java │ │ ├── SECP256K1.java │ │ ├── mikuli │ │ ├── AtePairing.java │ │ ├── BLS12381.java │ │ ├── G1Point.java │ │ ├── G2Point.java │ │ ├── GTPoint.java │ │ ├── Group.java │ │ ├── KeyPair.java │ │ ├── PublicKey.java │ │ ├── Scalar.java │ │ ├── SecretKey.java │ │ ├── Signature.java │ │ ├── SignatureAndPublicKey.java │ │ └── package-info.java │ │ ├── package-info.java │ │ └── sodium │ │ ├── AES256GCM.java │ │ ├── Allocated.java │ │ ├── Auth.java │ │ ├── Box.java │ │ ├── Concatenate.java │ │ ├── DefaultDetachedEncryptionResult.java │ │ ├── DetachedEncryptionResult.java │ │ ├── DiffieHelman.java │ │ ├── GenericHash.java │ │ ├── HMACSHA256.java │ │ ├── HMACSHA512.java │ │ ├── HMACSHA512256.java │ │ ├── KeyDerivation.java │ │ ├── KeyExchange.java │ │ ├── LibSodium.java │ │ ├── PasswordHash.java │ │ ├── SHA256Hash.java │ │ ├── SecretBox.java │ │ ├── SecretDecryptionStream.java │ │ ├── SecretEncryptionStream.java │ │ ├── Signature.java │ │ ├── Sodium.java │ │ ├── SodiumException.java │ │ ├── SodiumVersion.java │ │ ├── XChaCha20Poly1305.java │ │ └── package-info.java │ └── test │ └── java │ └── net │ └── consensys │ └── cava │ └── crypto │ ├── HashTest.java │ ├── SECP256K1Test.java │ ├── mikuli │ └── SignatureTest.java │ └── sodium │ ├── AES256GCMTest.java │ ├── AllocatedTest.java │ ├── AuthTest.java │ ├── BoxTest.java │ ├── ConcatenateTest.java │ ├── DiffieHelmanTest.java │ ├── GenericHashTest.java │ ├── HMACSHA256Test.java │ ├── HMACSHA512256Test.java │ ├── HMACSHA512Test.java │ ├── KeyDerivationTest.java │ ├── PasswordHashTest.java │ ├── SHA256HashTest.java │ ├── SecretBoxTest.java │ ├── SignatureTest.java │ ├── SodiumTest.java │ └── XChaCha20Poly1305Test.java ├── dependency-versions.gradle ├── devp2p ├── build.gradle └── src │ ├── main │ └── kotlin │ │ └── net │ │ └── consensys │ │ └── cava │ │ └── devp2p │ │ ├── AtomicLongProperty.kt │ │ ├── DiscoveryService.kt │ │ ├── Endpoint.kt │ │ ├── EnodeUri.kt │ │ ├── Node.kt │ │ ├── Packet.kt │ │ ├── PacketType.kt │ │ ├── Peer.kt │ │ ├── PeerRepository.kt │ │ └── PeerRoutingTable.kt │ └── test │ ├── java │ └── net │ │ └── consensys │ │ └── cava │ │ └── devp2p │ │ └── DiscoveryServiceJavaTest.java │ └── kotlin │ └── net │ └── consensys │ └── cava │ └── devp2p │ ├── DiscoveryServiceTest.kt │ ├── EndpointTest.kt │ ├── EphemeralPeerRepositoryTest.kt │ ├── FindNodePacketTest.kt │ ├── NeighborsPacketTest.kt │ ├── PingPacketTest.kt │ └── PongPacketTest.kt ├── eth-reference-tests ├── build.gradle └── src │ └── test │ └── java │ └── net │ └── consensys │ └── cava │ └── eth │ └── reference │ ├── BlockRLPTestSuite.java │ ├── MerkleTrieTestSuite.java │ ├── RLPReferenceTestSuite.java │ ├── SSZTestSuite.java │ └── TransactionTestSuite.java ├── eth-repository ├── build.gradle └── src │ ├── main │ └── kotlin │ │ └── net │ │ └── consensys │ │ └── cava │ │ └── eth │ │ └── repository │ │ ├── BlockHeaderFields.kt │ │ ├── BlockchainIndex.kt │ │ ├── BlockchainRepository.kt │ │ └── TransactionReceiptFields.kt │ └── test │ └── kotlin │ └── net │ └── consensys │ └── cava │ └── eth │ └── repository │ ├── BlockchainIndexTest.kt │ └── BlockchainRepositoryTest.kt ├── eth ├── build.gradle └── src │ ├── main │ └── java │ │ └── net │ │ └── consensys │ │ └── cava │ │ └── eth │ │ ├── Address.java │ │ ├── Block.java │ │ ├── BlockBody.java │ │ ├── BlockHeader.java │ │ ├── Hash.java │ │ ├── Log.java │ │ ├── LogsBloomFilter.java │ │ ├── Transaction.java │ │ ├── TransactionReceipt.java │ │ └── package-info.java │ └── test │ └── java │ └── net │ └── consensys │ └── cava │ └── eth │ ├── BlockBodyTest.java │ ├── BlockHeaderTest.java │ ├── BlockTest.java │ ├── LogTest.java │ ├── LogsBloomFilterTest.java │ ├── TransactionReceiptTest.java │ └── TransactionTest.java ├── gradle.properties ├── gradle ├── check-licenses.gradle ├── eclipse-java-consensys-style.xml ├── greclipse-gradle-consensys-style.properties ├── spotless.license.java └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── io ├── build.gradle └── src │ ├── main │ └── java │ │ └── net │ │ └── consensys │ │ └── cava │ │ └── io │ │ ├── Base64.java │ │ ├── IOConsumer.java │ │ ├── NullOutputStream.java │ │ ├── Resources.java │ │ ├── Streams.java │ │ ├── file │ │ ├── Files.java │ │ └── package-info.java │ │ └── package-info.java │ └── test │ ├── java │ └── net │ │ └── consensys │ │ └── cava │ │ └── io │ │ ├── Base64Test.java │ │ ├── ResourcesTest.java │ │ ├── StreamsTest.java │ │ └── file │ │ └── FilesTest.java │ └── resources │ ├── net │ └── consensys │ │ └── cava │ │ └── io │ │ └── file │ │ ├── resourceresolver │ │ ├── subdir │ │ │ └── test3.yaml │ │ ├── test1.txt │ │ └── test2.txt │ │ └── test.txt │ └── resourceresolver-test.jar ├── junit ├── build.gradle └── src │ ├── main │ └── java │ │ └── net │ │ └── consensys │ │ └── cava │ │ └── junit │ │ ├── BouncyCastleExtension.java │ │ ├── LuceneIndex.java │ │ ├── LuceneIndexWriter.java │ │ ├── LuceneIndexWriterExtension.java │ │ ├── RedisPort.java │ │ ├── RedisServerExtension.java │ │ ├── TempDirectory.java │ │ ├── TempDirectoryExtension.java │ │ ├── VertxExtension.java │ │ ├── VertxInstance.java │ │ └── package-info.java │ └── test │ └── java │ └── net │ └── consensys │ └── cava │ └── junit │ ├── LuceneIndexWriterExtensionTest.java │ ├── RedisServerExtensionTest.java │ └── TempDirectoryExtensionTest.java ├── kademlia ├── build.gradle └── src │ ├── main │ └── kotlin │ │ └── net │ │ └── consensys │ │ └── cava │ │ └── kademlia │ │ └── KademliaRoutingTable.kt │ └── test │ └── kotlin │ └── net │ └── consensys │ └── cava │ └── kademlia │ ├── KademliaRoutingTableTest.kt │ ├── LogarithmicDistanceTest.kt │ └── OrderedInsertTest.kt ├── kv ├── build.gradle └── src │ ├── main │ ├── java │ │ └── net │ │ │ └── consensys │ │ │ └── cava │ │ │ └── kv │ │ │ ├── RedisBytesCodec.java │ │ │ └── package-info.java │ └── kotlin │ │ └── net │ │ └── consensys │ │ └── cava │ │ └── kv │ │ ├── InfinispanKeyValueStore.kt │ │ ├── KeyValueStore.kt │ │ ├── LevelDBKeyValueStore.kt │ │ ├── MapDBKeyValueStore.kt │ │ ├── MapKeyValueStore.kt │ │ ├── RedisKeyValueStore.kt │ │ ├── RocksDBKeyValueStore.kt │ │ └── SQLKeyValueStore.kt │ └── test │ ├── java │ └── net │ │ └── consensys │ │ └── cava │ │ └── kv │ │ ├── KeyValueStoreTest.java │ │ └── RedisKeyValueStoreTest.java │ └── kotlin │ └── net │ └── consensys │ └── cava │ └── kv │ └── KeyValueStoreSpec.kt ├── les ├── build.gradle └── src │ ├── main │ └── kotlin │ │ └── net │ │ └── consensys │ │ └── cava │ │ └── les │ │ ├── BlockBodiesMessage.kt │ │ ├── BlockHeadersMessage.kt │ │ ├── GetBlockBodiesMessage.kt │ │ ├── GetBlockHeadersMessage.kt │ │ ├── GetReceiptsMessage.kt │ │ ├── LESPeerState.kt │ │ ├── LESSubProtocolHandler.kt │ │ ├── LESSubprotocol.kt │ │ ├── LightClient.kt │ │ ├── ReceiptsMessage.kt │ │ └── StatusMessage.kt │ └── test │ └── kotlin │ └── net │ └── consensys │ └── cava │ └── les │ ├── LESSubProtocolHandlerTest.kt │ ├── LESSubprotocolTest.kt │ └── MessagesTest.kt ├── merkle-trie ├── build.gradle └── src │ ├── main │ ├── java │ │ └── net │ │ │ └── consensys │ │ │ └── cava │ │ │ └── trie │ │ │ ├── CompactEncoding.java │ │ │ └── package-info.java │ └── kotlin │ │ └── net │ │ └── consensys │ │ └── cava │ │ └── trie │ │ ├── BranchNode.kt │ │ ├── DefaultNodeFactory.kt │ │ ├── ExtensionNode.kt │ │ ├── GetVisitor.kt │ │ ├── LeafNode.kt │ │ ├── MerklePatriciaTrie.kt │ │ ├── MerkleStorage.kt │ │ ├── MerkleStorageException.kt │ │ ├── MerkleTrie.kt │ │ ├── Node.kt │ │ ├── NodeFactory.kt │ │ ├── NodeVisitor.kt │ │ ├── NullNode.kt │ │ ├── PutVisitor.kt │ │ ├── RemoveVisitor.kt │ │ ├── StoredMerklePatriciaTrie.kt │ │ ├── StoredNode.kt │ │ └── StoredNodeFactory.kt │ └── test │ ├── java │ └── net │ │ └── consensys │ │ └── cava │ │ └── trie │ │ ├── CompactEncodingTest.java │ │ ├── MerklePatriciaTrieJavaTest.java │ │ ├── MerklePatriciaTriePerformanceTest.java │ │ └── StoredMerklePatriciaTrieJavaTest.java │ └── kotlin │ └── net │ └── consensys │ └── cava │ └── trie │ ├── MerklePatriciaTrieKotlinTest.kt │ └── StoredMerklePatriciaTrieKotlinTest.kt ├── net-coroutines ├── build.gradle └── src │ ├── main │ └── kotlin │ │ └── net │ │ └── consensys │ │ └── cava │ │ └── net │ │ └── coroutines │ │ ├── CoroutineByteChannel.kt │ │ ├── CoroutineChannelGroup.kt │ │ ├── CoroutineDatagramChannel.kt │ │ ├── CoroutineNetworkChannel.kt │ │ ├── CoroutineSelector.kt │ │ ├── CoroutineServerSocketChannel.kt │ │ └── CoroutineSocketChannel.kt │ └── test │ ├── java │ └── net │ │ └── consensys │ │ └── cava │ │ └── net │ │ └── coroutines │ │ └── SelectorTest.java │ └── kotlin │ └── net │ └── consensys │ └── cava │ └── net │ └── coroutines │ ├── CoroutineChannelGroupTest.kt │ ├── CoroutineDatagramChannelTest.kt │ ├── CoroutineSelectorTest.kt │ └── CoroutineSocketChannelTest.kt ├── net ├── build.gradle └── src │ ├── main │ └── java │ │ └── net │ │ └── consensys │ │ └── cava │ │ └── net │ │ ├── package-info.java │ │ └── tls │ │ ├── ClientFingerprintTrustManager.java │ │ ├── DelegatingTrustManagerFactory.java │ │ ├── FileBackedFingerprintRepository.java │ │ ├── FingerprintRepository.java │ │ ├── ServerFingerprintTrustManager.java │ │ ├── SingleTrustManagerFactory.java │ │ ├── TLS.java │ │ ├── TLSEnvironmentException.java │ │ ├── TrustManagerFactories.java │ │ ├── TrustManagerFactoryWrapper.java │ │ ├── VertxTrustOptions.java │ │ └── package-info.java │ └── test │ └── java │ └── net │ └── consensys │ └── cava │ └── net │ └── tls │ ├── ClientCaOrRecordTest.java │ ├── ClientCaOrTofuTest.java │ ├── ClientCaOrWhitelistTest.java │ ├── ClientRecordTest.java │ ├── ClientTofuTest.java │ ├── ClientWhitelistTest.java │ ├── FileBackedFingerprintRepositoryTest.java │ ├── InsecureTrustOptions.java │ ├── SecurityTestUtils.java │ ├── ServerCaOrRecordTest.java │ ├── ServerCaOrTofaTest.java │ ├── ServerCaOrWhitelistTest.java │ ├── ServerRecordTest.java │ ├── ServerTofaTest.java │ ├── ServerWhitelistTest.java │ └── TLSTest.java ├── plumtree ├── build.gradle └── src │ ├── main │ └── java │ │ └── net │ │ └── consensys │ │ └── cava │ │ └── plumtree │ │ ├── EphemeralPeerRepository.java │ │ ├── MessageHashing.java │ │ ├── MessageSender.java │ │ ├── MessageValidator.java │ │ ├── Peer.java │ │ ├── PeerRepository.java │ │ ├── State.java │ │ ├── package-info.java │ │ └── vertx │ │ ├── SocketPeer.java │ │ └── VertxGossipServer.java │ └── test │ └── java │ └── net │ └── consensys │ └── cava │ └── plumtree │ ├── StateTest.java │ └── vertx │ └── VertxGossipServerTest.java ├── rlp ├── build.gradle └── src │ ├── main │ └── java │ │ └── net │ │ └── consensys │ │ └── cava │ │ └── rlp │ │ ├── AccumulatingRLPWriter.java │ │ ├── ByteBufferRLPWriter.java │ │ ├── BytesRLPReader.java │ │ ├── BytesRLPWriter.java │ │ ├── DelegatingRLPWriter.java │ │ ├── EndOfRLPException.java │ │ ├── InvalidRLPEncodingException.java │ │ ├── InvalidRLPTypeException.java │ │ ├── RLP.java │ │ ├── RLPException.java │ │ ├── RLPReader.java │ │ ├── RLPWriter.java │ │ └── package-info.java │ └── test │ └── java │ └── net │ └── consensys │ └── cava │ └── rlp │ ├── ByteBufferWriterTest.java │ ├── BytesRLPReaderTest.java │ └── BytesRLPWriterTest.java ├── rlpx ├── build.gradle └── src │ ├── main │ └── java │ │ └── net │ │ └── consensys │ │ └── cava │ │ └── rlpx │ │ ├── EthereumIESEncryptionEngine.java │ │ ├── HandshakeMessage.java │ │ ├── InitiatorHandshakeMessage.java │ │ ├── InvalidMACException.java │ │ ├── MemoryWireConnectionsRepository.java │ │ ├── RLPxConnection.java │ │ ├── RLPxConnectionFactory.java │ │ ├── RLPxMessage.java │ │ ├── RLPxService.java │ │ ├── ResponderHandshakeMessage.java │ │ ├── WireConnectionRepository.java │ │ ├── package-info.java │ │ ├── vertx │ │ ├── VertxRLPxService.java │ │ └── package-info.java │ │ └── wire │ │ ├── Capability.java │ │ ├── DefaultSubProtocolIdentifier.java │ │ ├── DefaultWireConnection.java │ │ ├── DisconnectMessage.java │ │ ├── DisconnectReason.java │ │ ├── HelloMessage.java │ │ ├── PingMessage.java │ │ ├── PongMessage.java │ │ ├── SubProtocol.java │ │ ├── SubProtocolHandler.java │ │ ├── SubProtocolIdentifier.java │ │ ├── WireConnection.java │ │ ├── WireProtocolMessage.java │ │ └── package-info.java │ └── test │ └── java │ └── net │ └── consensys │ └── cava │ └── rlpx │ ├── RLPxConnectionFactoryTest.java │ ├── vertx │ ├── VertxAcceptanceTest.java │ └── VertxRLPxServiceTest.java │ └── wire │ ├── DefaultWireConnectionTest.java │ ├── DisconnectMessageTest.java │ ├── HelloMessageTest.java │ ├── PingPongTest.java │ └── RLPxConnectionMessageExchangeTest.java ├── scuttlebutt-discovery ├── build.gradle └── src │ ├── main │ └── java │ │ └── net │ │ └── consensys │ │ └── cava │ │ └── scuttlebutt │ │ └── discovery │ │ ├── LocalIdentity.java │ │ ├── ScuttlebuttLocalDiscoveryService.java │ │ └── package-info.java │ └── test │ └── java │ └── net │ └── consensys │ └── cava │ └── scuttlebutt │ └── discovery │ ├── LocalIdentityTest.java │ └── ScuttlebuttLocalDiscoveryServiceTest.java ├── scuttlebutt-handshake ├── build.gradle └── src │ ├── main │ └── java │ │ └── net │ │ └── consensys │ │ └── cava │ │ └── scuttlebutt │ │ └── handshake │ │ ├── HandshakeException.java │ │ ├── SecureScuttlebuttHandshakeClient.java │ │ ├── SecureScuttlebuttHandshakeServer.java │ │ ├── SecureScuttlebuttStream.java │ │ ├── SecureScuttlebuttStreamClient.java │ │ ├── SecureScuttlebuttStreamServer.java │ │ ├── StreamException.java │ │ ├── package-info.java │ │ └── vertx │ │ ├── ClientHandler.java │ │ ├── ClientHandlerFactory.java │ │ ├── SecureScuttlebuttVertxClient.java │ │ ├── SecureScuttlebuttVertxServer.java │ │ ├── ServerHandler.java │ │ ├── ServerHandlerFactory.java │ │ └── package-info.java │ └── test │ └── java │ └── net │ └── consensys │ └── cava │ └── scuttlebutt │ └── handshake │ ├── SecureScuttlebuttHandshakeClientTest.java │ ├── SecureScuttlebuttStreamTest.java │ └── vertx │ └── VertxIntegrationTest.java ├── scuttlebutt-rpc ├── build.gradle └── src │ ├── main │ └── java │ │ └── net │ │ └── consensys │ │ └── cava │ │ └── scuttlebutt │ │ └── rpc │ │ ├── RPCAsyncRequest.java │ │ ├── RPCCodec.java │ │ ├── RPCErrorBody.java │ │ ├── RPCFlag.java │ │ ├── RPCFunction.java │ │ ├── RPCMessage.java │ │ ├── RPCRequestBody.java │ │ ├── RPCRequestType.java │ │ ├── RPCResponse.java │ │ ├── RPCStreamRequest.java │ │ └── mux │ │ ├── Multiplexer.java │ │ ├── RPCHandler.java │ │ ├── ScuttlebuttStreamHandler.java │ │ └── exceptions │ │ ├── ConnectionClosedException.java │ │ └── RPCRequestFailedException.java │ └── test │ └── java │ └── net │ └── consensys │ └── cava │ └── scuttlebutt │ └── rpc │ ├── PatchworkIntegrationTest.java │ ├── RPCEncodingTest.java │ ├── RPCFlagTest.java │ └── mux │ └── PatchworkIntegrationTest.java ├── scuttlebutt ├── build.gradle └── src │ ├── main │ └── java │ │ └── net │ │ └── consensys │ │ └── cava │ │ └── scuttlebutt │ │ ├── Ed25519KeyPairIdentity.java │ │ ├── Ed25519PublicKeyIdentity.java │ │ ├── Identity.java │ │ ├── Invite.java │ │ ├── SECP256K1KeyPairIdentity.java │ │ ├── SECP256K1PublicKeyIdentity.java │ │ └── package-info.java │ └── test │ └── java │ └── net │ └── consensys │ └── cava │ └── scuttlebutt │ ├── IdentityTest.java │ └── InviteTest.java ├── settings.gradle ├── ssz ├── build.gradle └── src │ ├── main │ ├── java │ │ └── net │ │ │ └── consensys │ │ │ └── cava │ │ │ └── ssz │ │ │ ├── ByteBufferSSZWriter.java │ │ │ ├── BytesSSZReader.java │ │ │ ├── BytesSSZWriter.java │ │ │ ├── EndOfSSZException.java │ │ │ ├── InvalidSSZTypeException.java │ │ │ ├── SSZ.java │ │ │ ├── SSZException.java │ │ │ ├── SSZReader.java │ │ │ ├── SSZWriter.java │ │ │ └── package-info.java │ └── kotlin │ │ └── net │ │ └── consensys │ │ └── cava │ │ └── ssz │ │ └── experimental │ │ ├── BytesSSZReader.kt │ │ ├── BytesSSZWriter.kt │ │ ├── SSZ.kt │ │ ├── SSZReader.kt │ │ └── SSZWriter.kt │ └── test │ ├── java │ └── net │ │ └── consensys │ │ └── cava │ │ └── ssz │ │ ├── ByteBufferWriterTest.java │ │ ├── BytesSSZReaderTest.java │ │ ├── BytesSSZWriterTest.java │ │ └── HashTreeRootTest.java │ └── kotlin │ └── net │ └── consensys │ └── cava │ └── ssz │ └── experimental │ └── SSZTest.kt ├── toml ├── README.md ├── build.gradle └── src │ ├── main │ ├── antlr │ │ └── net │ │ │ └── consensys │ │ │ └── cava │ │ │ └── toml │ │ │ └── internal │ │ │ ├── TomlLexer.g4 │ │ │ └── TomlParser.g4 │ └── java │ │ └── net │ │ └── consensys │ │ └── cava │ │ └── toml │ │ ├── AccumulatingErrorListener.java │ │ ├── ArrayVisitor.java │ │ ├── ErrorReporter.java │ │ ├── InlineTableVisitor.java │ │ ├── JsonSerializer.java │ │ ├── KeyVisitor.java │ │ ├── LineVisitor.java │ │ ├── LocalDateVisitor.java │ │ ├── LocalTimeVisitor.java │ │ ├── MutableTomlArray.java │ │ ├── MutableTomlTable.java │ │ ├── Parser.java │ │ ├── QuotedStringVisitor.java │ │ ├── TokenName.java │ │ ├── Toml.java │ │ ├── TomlArray.java │ │ ├── TomlInvalidTypeException.java │ │ ├── TomlParseError.java │ │ ├── TomlParseResult.java │ │ ├── TomlPosition.java │ │ ├── TomlTable.java │ │ ├── TomlType.java │ │ ├── TomlVersion.java │ │ ├── ValueVisitor.java │ │ ├── ZoneOffsetVisitor.java │ │ └── package-info.java │ └── test │ ├── java │ └── net │ │ └── consensys │ │ └── cava │ │ └── toml │ │ ├── MutableTomlArrayTest.java │ │ ├── MutableTomlTableTest.java │ │ ├── TokenNameTest.java │ │ └── TomlTest.java │ └── resources │ └── net │ └── consensys │ └── cava │ └── toml │ ├── example-v0.4.0.toml │ ├── hard_example.toml │ ├── hard_example_unicode.toml │ └── toml-v0.5.0-spec-example.toml └── units ├── build.gradle └── src ├── main └── java │ └── net │ └── consensys │ └── cava │ └── units │ ├── bigints │ ├── BaseUInt256Value.java │ ├── BaseUInt384Value.java │ ├── BaseUInt64Value.java │ ├── UInt256.java │ ├── UInt256Domain.java │ ├── UInt256Value.java │ ├── UInt256ValueDomain.java │ ├── UInt256s.java │ ├── UInt384.java │ ├── UInt384Domain.java │ ├── UInt384Value.java │ ├── UInt384ValueDomain.java │ ├── UInt384s.java │ ├── UInt64.java │ ├── UInt64Domain.java │ ├── UInt64Value.java │ ├── UInt64ValueDomain.java │ ├── UInt64s.java │ └── package-info.java │ ├── ethereum │ ├── Gas.java │ ├── Wei.java │ └── package-info.java │ └── package-info.java └── test └── java └── net └── consensys └── cava └── units ├── bigints ├── BaseUInt256ValueTest.java ├── BaseUInt384ValueTest.java ├── BaseUInt64ValueTest.java ├── UInt256Test.java ├── UInt384Test.java └── UInt64Test.java └── ethereum ├── GasTest.java └── WeiTest.java /.editorconfig: -------------------------------------------------------------------------------- 1 | [*.{kt,kts}] 2 | indent_size=2 3 | continuation_indent_size=2 4 | insert_final_newline=true 5 | max_line_length=120 6 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text eol=lf 2 | *.jar -text 3 | *.bat -text 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.bak 2 | *.swp 3 | *.tmp 4 | *~.nib 5 | *.iml 6 | *.launch 7 | *.swp 8 | *.tokens 9 | .classpath 10 | .externalToolBuilders/ 11 | .gradle/ 12 | .vscode/ 13 | *.code-workspace 14 | .idea/* 15 | !.idea/codeStyles/ 16 | .loadpath 17 | .metadata 18 | .prefs 19 | .project 20 | .recommenders/ 21 | .settings 22 | .springBeans 23 | .vertx 24 | bin/ 25 | classes/ 26 | local.properties 27 | target/ 28 | tmp/ 29 | build/ 30 | out/ 31 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "eth-reference-tests/src/test/resources/tests"] 2 | path = eth-reference-tests/src/test/resources/tests 3 | url = https://github.com/ethereum/tests.git 4 | [submodule "eth-reference-tests/src/test/resources/eth2.0-tests"] 5 | path = eth-reference-tests/src/test/resources/eth2.0-tests 6 | url = https://github.com/ethereum/eth2.0-tests.git 7 | -------------------------------------------------------------------------------- /.idea/codeStyles/Project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | 8 | 12 | 13 | 15 | 16 | -------------------------------------------------------------------------------- /.idea/codeStyles/codeStyleConfig.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | -------------------------------------------------------------------------------- /bytes/build.gradle: -------------------------------------------------------------------------------- 1 | description = 'Classes and utilities for working with byte arrays.' 2 | 3 | dependencies { 4 | compile 'com.google.guava:guava' 5 | compileOnly 'io.vertx:vertx-core' 6 | 7 | testCompile 'io.vertx:vertx-core' 8 | testCompile 'org.junit.jupiter:junit-jupiter-api' 9 | testCompile 'org.junit.jupiter:junit-jupiter-params' 10 | 11 | testRuntime 'org.junit.jupiter:junit-jupiter-engine' 12 | } 13 | -------------------------------------------------------------------------------- /bytes/src/main/java/net/consensys/cava/bytes/MutableArrayWrappingBytes32.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.bytes; 14 | 15 | final class MutableArrayWrappingBytes32 extends MutableArrayWrappingBytes implements MutableBytes32 { 16 | 17 | MutableArrayWrappingBytes32(byte[] bytes) { 18 | this(bytes, 0); 19 | } 20 | 21 | MutableArrayWrappingBytes32(byte[] bytes, int offset) { 22 | super(bytes, offset, SIZE); 23 | } 24 | 25 | @Override 26 | public Bytes32 copy() { 27 | return new ArrayWrappingBytes32(toArray()); 28 | } 29 | 30 | @Override 31 | public MutableBytes32 mutableCopy() { 32 | return new MutableArrayWrappingBytes32(toArray()); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /bytes/src/main/java/net/consensys/cava/bytes/MutableArrayWrappingBytes48.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.bytes; 14 | 15 | final class MutableArrayWrappingBytes48 extends MutableArrayWrappingBytes implements MutableBytes48 { 16 | 17 | MutableArrayWrappingBytes48(byte[] bytes) { 18 | this(bytes, 0); 19 | } 20 | 21 | MutableArrayWrappingBytes48(byte[] bytes, int offset) { 22 | super(bytes, offset, SIZE); 23 | } 24 | 25 | @Override 26 | public Bytes48 copy() { 27 | return new ArrayWrappingBytes48(toArray()); 28 | } 29 | 30 | @Override 31 | public MutableBytes48 mutableCopy() { 32 | return new MutableArrayWrappingBytes48(toArray()); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /bytes/src/main/java/net/consensys/cava/bytes/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Classes and utilities for working with byte arrays. 3 | * 4 | *

5 | * These classes are included in the standard Cava distribution, or separately when using the gradle dependency 6 | * 'net.consensys.cava:cava-bytes' (cava-bytes.jar). 7 | */ 8 | @ParametersAreNonnullByDefault 9 | package net.consensys.cava.bytes; 10 | 11 | import javax.annotation.ParametersAreNonnullByDefault; 12 | -------------------------------------------------------------------------------- /bytes/src/test/java/net/consensys/cava/bytes/BufferBytesTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.bytes; 14 | 15 | import io.vertx.core.buffer.Buffer; 16 | 17 | class BufferBytesTest extends CommonBytesTests { 18 | 19 | @Override 20 | Bytes h(String hex) { 21 | return Bytes.wrapBuffer(Buffer.buffer(Bytes.fromHexString(hex).toArrayUnsafe())); 22 | } 23 | 24 | @Override 25 | MutableBytes m(int size) { 26 | return MutableBytes.wrapBuffer(Buffer.buffer(new byte[size])); 27 | } 28 | 29 | @Override 30 | Bytes w(byte[] bytes) { 31 | return Bytes.wrapBuffer(Buffer.buffer(Bytes.of(bytes).toArray())); 32 | } 33 | 34 | @Override 35 | Bytes of(int... bytes) { 36 | return Bytes.wrapBuffer(Buffer.buffer(Bytes.of(bytes).toArray())); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /bytes/src/test/java/net/consensys/cava/bytes/ByteBufBytesTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.bytes; 14 | 15 | import io.netty.buffer.Unpooled; 16 | 17 | class ByteBufBytesTest extends CommonBytesTests { 18 | 19 | @Override 20 | Bytes h(String hex) { 21 | return Bytes.wrapByteBuf(Unpooled.copiedBuffer(Bytes.fromHexString(hex).toArrayUnsafe())); 22 | } 23 | 24 | @Override 25 | MutableBytes m(int size) { 26 | return MutableBytes.wrapByteBuf(Unpooled.copiedBuffer(new byte[size])); 27 | } 28 | 29 | @Override 30 | Bytes w(byte[] bytes) { 31 | return Bytes.wrapByteBuf(Unpooled.copiedBuffer(Bytes.of(bytes).toArray())); 32 | } 33 | 34 | @Override 35 | Bytes of(int... bytes) { 36 | return Bytes.wrapByteBuf(Unpooled.copiedBuffer(Bytes.of(bytes).toArray())); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /bytes/src/test/java/net/consensys/cava/bytes/ByteBufferBytesTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.bytes; 14 | 15 | import java.nio.ByteBuffer; 16 | 17 | class ByteBufferBytesTest extends CommonBytesTests { 18 | 19 | @Override 20 | Bytes h(String hex) { 21 | return Bytes.wrapByteBuffer(ByteBuffer.wrap(Bytes.fromHexString(hex).toArrayUnsafe())); 22 | } 23 | 24 | @Override 25 | MutableBytes m(int size) { 26 | return MutableBytes.wrapByteBuffer(ByteBuffer.allocate(size)); 27 | } 28 | 29 | @Override 30 | Bytes w(byte[] bytes) { 31 | return Bytes.wrapByteBuffer(ByteBuffer.wrap(Bytes.of(bytes).toArray())); 32 | } 33 | 34 | @Override 35 | Bytes of(int... bytes) { 36 | return Bytes.wrapByteBuffer(ByteBuffer.wrap(Bytes.of(bytes).toArray())); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /concurrent-coroutines/build.gradle: -------------------------------------------------------------------------------- 1 | description = 'Kotlin coroutine extensions for cava concurrency primitives.' 2 | 3 | dependencies { 4 | compile project(':concurrent') 5 | compile 'org.jetbrains.kotlinx:kotlinx-coroutines-core' 6 | 7 | testCompile 'org.junit.jupiter:junit-jupiter-api' 8 | testCompile 'org.junit.jupiter:junit-jupiter-params' 9 | 10 | testRuntime 'org.junit.jupiter:junit-jupiter-engine' 11 | } 12 | -------------------------------------------------------------------------------- /concurrent/build.gradle: -------------------------------------------------------------------------------- 1 | description = 'Classes and utilities for working with concurrency.' 2 | 3 | dependencies { 4 | compile 'com.google.guava:guava' 5 | compileOnly 'io.vertx:vertx-core' 6 | 7 | testCompile 'org.junit.jupiter:junit-jupiter-api' 8 | testCompile 'org.junit.jupiter:junit-jupiter-params' 9 | testCompile 'org.assertj:assertj-core' 10 | 11 | testRuntime 'org.junit.jupiter:junit-jupiter-engine' 12 | } 13 | -------------------------------------------------------------------------------- /concurrent/src/main/java/net/consensys/cava/concurrent/CompletableAsyncCompletion.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.concurrent; 14 | 15 | /** 16 | * An {@link AsyncCompletion} that can later be completed successfully or with a provided exception. 17 | */ 18 | public interface CompletableAsyncCompletion extends AsyncCompletion { 19 | 20 | /** 21 | * Complete this completion. 22 | * 23 | * @return {@code true} if this invocation caused this completion to transition to a completed state, else 24 | * {@code false}. 25 | */ 26 | boolean complete(); 27 | 28 | /** 29 | * Complete this completion with the given exception. 30 | * 31 | * @param ex The exception to complete this result with. 32 | * @return {@code true} if this invocation caused this completion to transition to a completed state, else 33 | * {@code false}. 34 | */ 35 | boolean completeExceptionally(Throwable ex); 36 | } 37 | -------------------------------------------------------------------------------- /concurrent/src/main/java/net/consensys/cava/concurrent/CompletableAsyncResult.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.concurrent; 14 | 15 | import javax.annotation.Nullable; 16 | 17 | /** 18 | * An {@link AsyncResult} that can be later completed successfully with a provided value, or completed with an 19 | * exception. 20 | * 21 | * @param The type of the value returned by this result. 22 | */ 23 | public interface CompletableAsyncResult extends AsyncResult { 24 | 25 | /** 26 | * Complete this result with the given value. 27 | * 28 | * @param value The value to complete this result with. 29 | * @return {@code true} if this invocation caused this result to transition to a completed state, else {@code false}. 30 | */ 31 | boolean complete(@Nullable T value); 32 | 33 | /** 34 | * Complete this result with the given exception. 35 | * 36 | * @param ex The exception to complete this result with. 37 | * @return {@code true} if this invocation caused this result to transition to a completed state, else {@code false}. 38 | */ 39 | boolean completeExceptionally(Throwable ex); 40 | } 41 | -------------------------------------------------------------------------------- /concurrent/src/main/java/net/consensys/cava/concurrent/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Classes and utilities for working with concurrency. 3 | * 4 | *

5 | * These classes are included in the standard Cava distribution, or separately when using the gradle dependency 6 | * 'net.consensys.cava:cava-concurrent' (cava-concurrent.jar). 7 | */ 8 | @ParametersAreNonnullByDefault 9 | package net.consensys.cava.concurrent; 10 | 11 | import javax.annotation.ParametersAreNonnullByDefault; 12 | -------------------------------------------------------------------------------- /config/build.gradle: -------------------------------------------------------------------------------- 1 | description = 'Classes and utilities for working with configuration.' 2 | 3 | dependencies { 4 | compile project(':toml') 5 | compile 'com.google.guava:guava' 6 | 7 | testCompile 'org.junit.jupiter:junit-jupiter-api' 8 | testCompile 'org.junit.jupiter:junit-jupiter-params' 9 | 10 | testRuntime 'org.junit.jupiter:junit-jupiter-engine' 11 | } 12 | -------------------------------------------------------------------------------- /config/src/main/java/net/consensys/cava/config/ConfigurationValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.config; 14 | 15 | import java.util.List; 16 | 17 | /** 18 | * A validator for a configuration. 19 | * 20 | *

21 | * Validators of this type are invoked during verification after all property validators. However, errors returned by 22 | * property validators do not prevent this validator being evaluated, so properties of the configuration may be missing 23 | * or invalid. 24 | */ 25 | public interface ConfigurationValidator { 26 | 27 | /** 28 | * Validate a configuration. 29 | * 30 | * @param configuration The value associated with the configuration entry. 31 | * @return A list of error messages. If no errors are found, an empty list should be returned. 32 | */ 33 | List validate(Configuration configuration); 34 | } 35 | -------------------------------------------------------------------------------- /config/src/main/java/net/consensys/cava/config/InvalidConfigurationPropertyTypeException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.config; 14 | 15 | import javax.annotation.Nullable; 16 | 17 | /** 18 | * An exception thrown when an invalid type is encountered. 19 | */ 20 | public final class InvalidConfigurationPropertyTypeException extends RuntimeException { 21 | 22 | @Nullable 23 | private final DocumentPosition position; 24 | 25 | InvalidConfigurationPropertyTypeException(@Nullable DocumentPosition position, String message) { 26 | super(message); 27 | this.position = position; 28 | } 29 | 30 | InvalidConfigurationPropertyTypeException( 31 | @Nullable DocumentPosition position, 32 | String message, 33 | @Nullable Throwable cause) { 34 | super(message, cause); 35 | this.position = position; 36 | } 37 | 38 | /** 39 | * @return The position of the property in the configuration document, or {@code null} if there is no position 40 | * available. 41 | */ 42 | @Nullable 43 | public DocumentPosition position() { 44 | return position; 45 | } 46 | 47 | @Override 48 | public String toString() { 49 | if (position == null) { 50 | return getMessage(); 51 | } 52 | return getMessage() + " (" + position + ")"; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /config/src/main/java/net/consensys/cava/config/NoConfigurationPropertyException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.config; 14 | 15 | /** 16 | * An exception thrown when a requested configuration property is not found. 17 | * 18 | *

19 | * This exception can be avoided by using a schema that provides a default value or asserts that a value has been 20 | * provided in the configuration. 21 | */ 22 | public final class NoConfigurationPropertyException extends RuntimeException { 23 | 24 | NoConfigurationPropertyException(String message) { 25 | super(message); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /config/src/main/java/net/consensys/cava/config/PropertyValidators.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.config; 14 | 15 | import static net.consensys.cava.config.ConfigurationErrors.noErrors; 16 | import static net.consensys.cava.config.ConfigurationErrors.singleError; 17 | 18 | final class PropertyValidators { 19 | private PropertyValidators() {} 20 | 21 | static final PropertyValidator IS_PRESENT = (key, position, value) -> { 22 | if (value == null) { 23 | return singleError(position, "Required property '" + key + "' is missing"); 24 | } 25 | return noErrors(); 26 | }; 27 | } 28 | -------------------------------------------------------------------------------- /config/src/main/java/net/consensys/cava/config/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * A general-purpose library for managing configuration data. 3 | *

4 | * These classes are included in the standard Cava distribution, or separately when using the gradle dependency 5 | * 'net.consensys.cava:cava-config' (cava-config.jar). 6 | */ 7 | @ParametersAreNonnullByDefault 8 | package net.consensys.cava.config; 9 | 10 | import javax.annotation.ParametersAreNonnullByDefault; 11 | -------------------------------------------------------------------------------- /crypto/build.gradle: -------------------------------------------------------------------------------- 1 | description = 'Classes and utilities for working with cryptography.' 2 | 3 | javadoc { exclude '**/LibSodium*' } 4 | 5 | dependencies { 6 | compile project(':bytes') 7 | compile project(':io') 8 | compile project(':units') 9 | compile 'com.google.guava:guava' 10 | compile 'com.github.jnr:jnr-ffi' 11 | 12 | compileOnly 'org.bouncycastle:bcprov-jdk15on' 13 | compileOnly 'org.miracl.milagro.amcl:milagro-crypto-java' 14 | 15 | testCompile project(':junit') 16 | testCompile 'org.bouncycastle:bcprov-jdk15on' 17 | testCompile 'org.junit.jupiter:junit-jupiter-api' 18 | testCompile 'org.junit.jupiter:junit-jupiter-params' 19 | testCompile 'org.miracl.milagro.amcl:milagro-crypto-java' 20 | 21 | testRuntime 'org.junit.jupiter:junit-jupiter-engine' 22 | } 23 | -------------------------------------------------------------------------------- /crypto/src/main/java/net/consensys/cava/crypto/InvalidSEC256K1SecretKeyStoreException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.crypto; 14 | 15 | /** 16 | * Exception thrown when reading a store that contains an invalid SEC256K1 private keys. 17 | */ 18 | public final class InvalidSEC256K1SecretKeyStoreException extends RuntimeException { 19 | } 20 | -------------------------------------------------------------------------------- /crypto/src/main/java/net/consensys/cava/crypto/mikuli/AtePairing.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.crypto.mikuli; 14 | 15 | import org.apache.milagro.amcl.BLS381.FP12; 16 | import org.apache.milagro.amcl.BLS381.PAIR; 17 | 18 | /** 19 | * Function that maps 2 points on an elliptic curve to a number. 20 | */ 21 | final class AtePairing { 22 | 23 | /** 24 | * 25 | * @param p1 the point in Group1, not null 26 | * @param p2 the point in Group2, not null 27 | * @return GTPoint 28 | */ 29 | static GTPoint pair(G1Point p1, G2Point p2) { 30 | FP12 e = PAIR.ate(p2.ecp2Point(), p1.ecpPoint()); 31 | return new GTPoint(PAIR.fexp(e)); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /crypto/src/main/java/net/consensys/cava/crypto/mikuli/GTPoint.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.crypto.mikuli; 14 | 15 | import java.util.Objects; 16 | 17 | import org.apache.milagro.amcl.BLS381.FP12; 18 | 19 | /** 20 | * GT is the object that holds the result of the pairing operation. Points in GT are elements of Fq12. 21 | */ 22 | final class GTPoint { 23 | 24 | private final FP12 point; 25 | 26 | GTPoint(FP12 point) { 27 | this.point = point; 28 | } 29 | 30 | @Override 31 | public int hashCode() { 32 | final int prime = 31; 33 | int result = 1; 34 | result = prime * result + ((point == null) ? 0 : point.hashCode()); 35 | return result; 36 | } 37 | 38 | @Override 39 | public boolean equals(Object obj) { 40 | if (Objects.isNull(obj)) { 41 | return false; 42 | } 43 | if (this == obj) { 44 | return true; 45 | } 46 | if (!(obj instanceof GTPoint)) { 47 | return false; 48 | } 49 | GTPoint other = (GTPoint) obj; 50 | return point.equals(other.point); 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /crypto/src/main/java/net/consensys/cava/crypto/mikuli/Group.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.crypto.mikuli; 14 | 15 | /** 16 | * Group is an interface that define the allowed mathematical operators 17 | */ 18 | interface Group { 19 | 20 | G add(G g); 21 | 22 | G mul(Scalar scalar); 23 | } 24 | -------------------------------------------------------------------------------- /crypto/src/main/java/net/consensys/cava/crypto/mikuli/Scalar.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.crypto.mikuli; 14 | 15 | import java.util.Objects; 16 | 17 | import org.apache.milagro.amcl.BLS381.BIG; 18 | 19 | /** 20 | * This class represents an ordinary scalar value. 21 | */ 22 | final class Scalar { 23 | 24 | private final BIG value; 25 | 26 | Scalar(BIG value) { 27 | this.value = value; 28 | } 29 | 30 | BIG value() { 31 | return value; 32 | } 33 | 34 | @Override 35 | public boolean equals(Object o) { 36 | if (this == o) 37 | return true; 38 | if (o == null || getClass() != o.getClass()) 39 | return false; 40 | Scalar scalar = (Scalar) o; 41 | return Objects.equals(value.toString(), scalar.value.toString()); 42 | } 43 | 44 | @Override 45 | public int hashCode() { 46 | return Objects.hash(value); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /crypto/src/main/java/net/consensys/cava/crypto/mikuli/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Classes and utilities for working with cryptography. 3 | * 4 | *

5 | * These classes are included in the standard Cava distribution, or separately when using the gradle dependency 6 | * 'net.consensys.cava:cava-crypto' (cava-crypto.jar). 7 | */ 8 | @ParametersAreNonnullByDefault 9 | package net.consensys.cava.crypto.mikuli; 10 | 11 | import javax.annotation.ParametersAreNonnullByDefault; 12 | -------------------------------------------------------------------------------- /crypto/src/main/java/net/consensys/cava/crypto/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Classes and utilities for working with cryptography. 3 | * 4 | *

5 | * These classes are included in the standard Cava distribution, or separately when using the gradle dependency 6 | * 'net.consensys.cava:cava-crypto' (cava-crypto.jar). 7 | */ 8 | package net.consensys.cava.crypto; 9 | -------------------------------------------------------------------------------- /crypto/src/main/java/net/consensys/cava/crypto/sodium/DefaultDetachedEncryptionResult.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.crypto.sodium; 14 | 15 | import net.consensys.cava.bytes.Bytes; 16 | 17 | final class DefaultDetachedEncryptionResult implements DetachedEncryptionResult { 18 | 19 | private final byte[] cipherText; 20 | private final byte[] mac; 21 | 22 | DefaultDetachedEncryptionResult(byte[] cipherText, byte[] mac) { 23 | this.cipherText = cipherText; 24 | this.mac = mac; 25 | } 26 | 27 | @Override 28 | public Bytes cipherText() { 29 | return Bytes.wrap(cipherText); 30 | } 31 | 32 | @Override 33 | public byte[] cipherTextArray() { 34 | return cipherText; 35 | } 36 | 37 | @Override 38 | public Bytes mac() { 39 | return Bytes.wrap(mac); 40 | } 41 | 42 | @Override 43 | public byte[] macArray() { 44 | return mac; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /crypto/src/main/java/net/consensys/cava/crypto/sodium/DetachedEncryptionResult.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.crypto.sodium; 14 | 15 | import net.consensys.cava.bytes.Bytes; 16 | 17 | /** 18 | * The result from a detached encryption. 19 | */ 20 | public interface DetachedEncryptionResult { 21 | 22 | /** 23 | * @return The cipher text. 24 | */ 25 | Bytes cipherText(); 26 | 27 | /** 28 | * @return The cipher text. 29 | */ 30 | byte[] cipherTextArray(); 31 | 32 | /** 33 | * @return The message authentication code. 34 | */ 35 | Bytes mac(); 36 | 37 | /** 38 | * @return The message authentication code. 39 | */ 40 | byte[] macArray(); 41 | } 42 | -------------------------------------------------------------------------------- /crypto/src/main/java/net/consensys/cava/crypto/sodium/SecretDecryptionStream.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.crypto.sodium; 14 | 15 | import net.consensys.cava.bytes.Bytes; 16 | 17 | import javax.security.auth.Destroyable; 18 | 19 | /** 20 | * Used to decrypt a sequence of messages, or a single message split into arbitrary chunks. 21 | */ 22 | public interface SecretDecryptionStream extends Destroyable { 23 | 24 | /** 25 | * Pull a message from this secret stream. 26 | * 27 | * @param cipherText The encrypted message. 28 | * @return The clear text. 29 | */ 30 | default Bytes pull(Bytes cipherText) { 31 | return Bytes.wrap(pull(cipherText.toArrayUnsafe())); 32 | } 33 | 34 | /** 35 | * Pull a message from this secret stream. 36 | * 37 | * @param cipherText The encrypted message. 38 | * @return The clear text. 39 | */ 40 | byte[] pull(byte[] cipherText); 41 | 42 | /** @return {@code true} if no more messages should be decrypted by this stream */ 43 | boolean isComplete(); 44 | } 45 | -------------------------------------------------------------------------------- /crypto/src/main/java/net/consensys/cava/crypto/sodium/SodiumException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.crypto.sodium; 14 | 15 | /** 16 | * An exception that is thrown when an error occurs using the native sodium library. 17 | */ 18 | public final class SodiumException extends RuntimeException { 19 | 20 | /** 21 | * @param message The exception message. 22 | */ 23 | public SodiumException(String message) { 24 | super(message); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /crypto/src/main/java/net/consensys/cava/crypto/sodium/SodiumVersion.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.crypto.sodium; 14 | 15 | /** 16 | * Details of a sodium native library version. 17 | */ 18 | public final class SodiumVersion implements Comparable { 19 | private final int major; 20 | private final int minor; 21 | private final String name; 22 | 23 | SodiumVersion(int major, int minor, String name) { 24 | this.major = major; 25 | this.minor = minor; 26 | this.name = name; 27 | } 28 | 29 | /** 30 | * The major version number. 31 | * 32 | * @return The major version number. 33 | */ 34 | public int major() { 35 | return major; 36 | } 37 | 38 | /** 39 | * The minor version number. 40 | * 41 | * @return The minor version number. 42 | */ 43 | public int minor() { 44 | return minor; 45 | } 46 | 47 | @Override 48 | public String toString() { 49 | return name; 50 | } 51 | 52 | @Override 53 | public int compareTo(SodiumVersion other) { 54 | if (this.major == other.major) { 55 | return Integer.compare(this.minor, other.minor); 56 | } 57 | return Integer.compare(this.major, other.major); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /crypto/src/main/java/net/consensys/cava/crypto/sodium/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Classes and utilities for working with the sodium native library. 3 | * 4 | *

5 | * Classes and utilities in this package provide an interface to the native Sodium crypto library 6 | * (https://www.libsodium.org/), which must be installed on the same system as the JVM. It will be searched for in 7 | * common library locations, or it can be loaded explicitly using 8 | * {@link net.consensys.cava.crypto.sodium.Sodium#searchLibrary(java.nio.file.Path...)} or 9 | * {@link net.consensys.cava.crypto.sodium.Sodium#loadLibrary(java.nio.file.Path)}. 10 | * 11 | *

12 | * Classes in this package also depend upon the JNR-FFI library being available on the classpath, along with its 13 | * dependencies. See https://github.com/jnr/jnr-ffi. JNR-FFI can be included using the gradle dependency 14 | * 'com.github.jnr:jnr-ffi'. 15 | */ 16 | @ParametersAreNonnullByDefault 17 | package net.consensys.cava.crypto.sodium; 18 | 19 | import javax.annotation.ParametersAreNonnullByDefault; 20 | -------------------------------------------------------------------------------- /crypto/src/test/java/net/consensys/cava/crypto/sodium/AuthTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.crypto.sodium; 14 | 15 | import static java.nio.charset.StandardCharsets.UTF_8; 16 | import static org.junit.jupiter.api.Assertions.assertFalse; 17 | import static org.junit.jupiter.api.Assertions.assertTrue; 18 | import static org.junit.jupiter.api.Assumptions.assumeTrue; 19 | 20 | import org.junit.jupiter.api.BeforeAll; 21 | import org.junit.jupiter.api.Test; 22 | 23 | class AuthTest { 24 | 25 | @BeforeAll 26 | static void checkAvailable() { 27 | assumeTrue(Sodium.isAvailable(), "Sodium native library is not available"); 28 | } 29 | 30 | @Test 31 | void checkAuthenticateAndVerify() { 32 | Auth.Key key = Auth.Key.random(); 33 | 34 | byte[] input = "An input to authenticate".getBytes(UTF_8); 35 | byte[] tag = Auth.auth(input, key); 36 | 37 | assertTrue(Auth.verify(tag, input, key)); 38 | assertFalse(Auth.verify(new byte[tag.length], input, key)); 39 | assertFalse(Auth.verify(tag, "An invalid input".getBytes(UTF_8), key)); 40 | assertFalse(Auth.verify(tag, input, Auth.Key.random())); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /crypto/src/test/java/net/consensys/cava/crypto/sodium/ConcatenateTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.crypto.sodium; 14 | 15 | import static org.junit.jupiter.api.Assertions.assertEquals; 16 | import static org.junit.jupiter.api.Assumptions.assumeTrue; 17 | 18 | import net.consensys.cava.bytes.Bytes; 19 | 20 | import org.junit.jupiter.api.BeforeAll; 21 | import org.junit.jupiter.api.Test; 22 | 23 | class ConcatenateTest { 24 | 25 | @BeforeAll 26 | static void checkAvailable() { 27 | assumeTrue(Sodium.isAvailable(), "Sodium native library is not available"); 28 | } 29 | 30 | @Test 31 | void testConcatenateTwoValues() { 32 | Concatenate concatenate = new Concatenate(); 33 | Bytes random = Bytes.random(32); 34 | 35 | concatenate.add(Signature.PublicKey.fromBytes(random)); 36 | concatenate.add(Signature.PublicKey.fromBytes(random)); 37 | 38 | Allocated result = concatenate.concatenate(); 39 | 40 | assertEquals(Bytes.concatenate(random, random), result.bytes()); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /crypto/src/test/java/net/consensys/cava/crypto/sodium/DiffieHelmanTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.crypto.sodium; 14 | 15 | import static org.junit.jupiter.api.Assertions.assertEquals; 16 | import static org.junit.jupiter.api.Assumptions.assumeTrue; 17 | 18 | import org.junit.jupiter.api.BeforeAll; 19 | import org.junit.jupiter.api.Test; 20 | 21 | class DiffieHelmanTest { 22 | 23 | @BeforeAll 24 | static void checkAvailable() { 25 | assumeTrue(Sodium.isAvailable(), "Sodium native library is not available"); 26 | } 27 | 28 | @Test 29 | void testScalarMultiplication() { 30 | DiffieHelman.KeyPair keyPair = DiffieHelman.KeyPair.random(); 31 | DiffieHelman.KeyPair secondKeyPair = DiffieHelman.KeyPair.random(); 32 | 33 | DiffieHelman.Secret scalar1 = DiffieHelman.Secret.forKeys(keyPair.secretKey(), secondKeyPair.publicKey()); 34 | DiffieHelman.Secret scalar2 = DiffieHelman.Secret.forKeys(secondKeyPair.secretKey(), keyPair.publicKey()); 35 | 36 | assertEquals(scalar1, scalar2); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /crypto/src/test/java/net/consensys/cava/crypto/sodium/GenericHashTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.crypto.sodium; 14 | 15 | import static org.junit.jupiter.api.Assertions.assertEquals; 16 | import static org.junit.jupiter.api.Assertions.assertNotNull; 17 | import static org.junit.jupiter.api.Assumptions.assumeTrue; 18 | 19 | import net.consensys.cava.bytes.Bytes; 20 | 21 | import org.junit.jupiter.api.BeforeAll; 22 | import org.junit.jupiter.api.Test; 23 | 24 | class GenericHashTest { 25 | 26 | @BeforeAll 27 | static void checkAvailable() { 28 | assumeTrue(Sodium.isAvailable(), "Sodium native library is not available"); 29 | } 30 | 31 | @Test 32 | void hashValue() { 33 | GenericHash.Hash output = GenericHash.hash(64, GenericHash.Input.fromBytes(Bytes.random(384))); 34 | assertNotNull(output); 35 | assertEquals(64, output.bytes().size()); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /crypto/src/test/java/net/consensys/cava/crypto/sodium/KeyDerivationTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.crypto.sodium; 14 | 15 | import static org.junit.jupiter.api.Assertions.assertEquals; 16 | import static org.junit.jupiter.api.Assertions.assertNotEquals; 17 | import static org.junit.jupiter.api.Assumptions.assumeTrue; 18 | 19 | import net.consensys.cava.bytes.Bytes; 20 | import net.consensys.cava.crypto.sodium.KeyDerivation.MasterKey; 21 | 22 | import org.junit.jupiter.api.BeforeAll; 23 | import org.junit.jupiter.api.Test; 24 | 25 | class KeyDerivationTest { 26 | 27 | @BeforeAll 28 | static void checkAvailable() { 29 | assumeTrue(Sodium.isAvailable(), "Sodium native library is not available"); 30 | assumeTrue(KeyDerivation.isAvailable(), "KeyDerivation support is not available (requires >= 10.0.12"); 31 | } 32 | 33 | @Test 34 | void differentIdsShouldGenerateDifferentKeys() { 35 | MasterKey masterKey = MasterKey.random(); 36 | 37 | Bytes subKey1 = masterKey.deriveKey(40, 1, "abcdefg"); 38 | assertEquals(subKey1, masterKey.deriveKey(40, 1, "abcdefg")); 39 | 40 | assertNotEquals(subKey1, masterKey.deriveKey(40, 2, "abcdefg")); 41 | assertNotEquals(subKey1, masterKey.deriveKey(40, 1, new byte[KeyDerivation.contextLength()])); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /crypto/src/test/java/net/consensys/cava/crypto/sodium/SHA256HashTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.crypto.sodium; 14 | 15 | import static org.junit.jupiter.api.Assertions.assertEquals; 16 | import static org.junit.jupiter.api.Assertions.assertNotNull; 17 | import static org.junit.jupiter.api.Assumptions.assumeTrue; 18 | 19 | import net.consensys.cava.bytes.Bytes; 20 | 21 | import org.junit.jupiter.api.BeforeAll; 22 | import org.junit.jupiter.api.Test; 23 | 24 | class SHA256HashTest { 25 | 26 | @BeforeAll 27 | static void checkAvailable() { 28 | assumeTrue(Sodium.isAvailable(), "Sodium native library is not available"); 29 | } 30 | 31 | @Test 32 | void hashValue() { 33 | SHA256Hash.Hash output = SHA256Hash.hash(SHA256Hash.Input.fromBytes(Bytes.random(384))); 34 | assertNotNull(output); 35 | assertEquals(32, output.bytes().size()); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /devp2p/build.gradle: -------------------------------------------------------------------------------- 1 | description = 'Ethereum ÐΞVp2p implementation.' 2 | 3 | dependencies { 4 | compile project(':bytes') 5 | compile project(':concurrent') 6 | compile project(':concurrent-coroutines') 7 | compile project(':crypto') 8 | compile project(':kademlia') 9 | compile project(':net-coroutines') 10 | compile project(':rlp') 11 | compile 'io.vertx:vertx-core' 12 | compile 'org.jetbrains.kotlinx:kotlinx-coroutines-core' 13 | compile 'org.logl:logl-api' 14 | 15 | testCompile project(':junit') 16 | testCompile 'org.bouncycastle:bcprov-jdk15on' 17 | testCompile 'org.junit.jupiter:junit-jupiter-api' 18 | testCompile 'org.junit.jupiter:junit-jupiter-params' 19 | testCompile 'org.logl:logl-logl' 20 | 21 | testRuntime 'org.junit.jupiter:junit-jupiter-engine' 22 | } 23 | -------------------------------------------------------------------------------- /devp2p/src/main/kotlin/net/consensys/cava/devp2p/AtomicLongProperty.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.devp2p 14 | 15 | import java.util.concurrent.atomic.AtomicLong 16 | import kotlin.reflect.KProperty 17 | 18 | // Extension methods that allow an AtomicLong to be treated as a Long property 19 | 20 | internal operator fun AtomicLong.getValue(thisRef: Any?, property: KProperty<*>): Long = this.get() 21 | 22 | internal operator fun AtomicLong.setValue(thisRef: Any?, property: KProperty<*>, value: Long) { 23 | this.set(value) 24 | } 25 | 26 | internal operator fun AtomicLong.inc(): AtomicLong { 27 | this.incrementAndGet() 28 | return this 29 | } 30 | 31 | internal operator fun AtomicLong.dec(): AtomicLong { 32 | this.decrementAndGet() 33 | return this 34 | } 35 | -------------------------------------------------------------------------------- /devp2p/src/main/kotlin/net/consensys/cava/devp2p/Node.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.devp2p 14 | 15 | import net.consensys.cava.crypto.SECP256K1 16 | import net.consensys.cava.rlp.RLPReader 17 | import net.consensys.cava.rlp.RLPWriter 18 | 19 | internal data class Node( 20 | val endpoint: Endpoint, 21 | val nodeId: SECP256K1.PublicKey 22 | ) { 23 | 24 | companion object { 25 | fun readFrom(reader: RLPReader): Node { 26 | val endpoint = Endpoint.readFrom(reader) 27 | val nodeId = SECP256K1.PublicKey.fromBytes(reader.readValue()) 28 | return Node(endpoint, nodeId) 29 | } 30 | } 31 | 32 | internal fun writeTo(writer: RLPWriter) { 33 | endpoint.writeTo(writer) 34 | writer.writeValue(nodeId.bytes()) 35 | } 36 | 37 | internal fun rlpSize(): Int = 1 + endpoint.rlpSize() + 3 + 64 38 | } 39 | 40 | internal fun Peer.toNode(): Node = 41 | endpoint?.let { endpoint -> Node(endpoint, nodeId) } ?: throw IllegalArgumentException("Peer has no endpoint") 42 | -------------------------------------------------------------------------------- /eth-reference-tests/build.gradle: -------------------------------------------------------------------------------- 1 | jar { enabled = false } 2 | 3 | dependencies { 4 | testCompile project(':eth') 5 | testCompile project(':merkle-trie') 6 | testCompile project(':rlp') 7 | testCompile project(':ssz') 8 | 9 | testCompile project(':junit') 10 | testCompile 'com.fasterxml.jackson.core:jackson-databind' 11 | testCompile 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml' 12 | testCompile 'org.bouncycastle:bcprov-jdk15on' 13 | testCompile 'org.junit.jupiter:junit-jupiter-api' 14 | testCompile 'org.junit.jupiter:junit-jupiter-params' 15 | 16 | testRuntime 'org.junit.jupiter:junit-jupiter-engine' 17 | } 18 | -------------------------------------------------------------------------------- /eth-repository/build.gradle: -------------------------------------------------------------------------------- 1 | description = 'Repository for managing, storing and indexing Ethereum domain objects.' 2 | 3 | dependencies { 4 | compile project(':concurrent') 5 | compile project(':eth') 6 | compile project(':kv') 7 | compile 'org.apache.lucene:lucene-core' 8 | 9 | 10 | testCompile project(':junit') 11 | testCompile 'org.bouncycastle:bcprov-jdk15on' 12 | testCompile 'org.junit.jupiter:junit-jupiter-api' 13 | testCompile 'org.junit.jupiter:junit-jupiter-params' 14 | 15 | testRuntime 'org.junit.jupiter:junit-jupiter-engine' 16 | } 17 | -------------------------------------------------------------------------------- /eth-repository/src/main/kotlin/net/consensys/cava/eth/repository/BlockHeaderFields.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.eth.repository 14 | 15 | /** 16 | * Block header index fields. 17 | * 18 | */ 19 | enum class BlockHeaderFields 20 | /** 21 | * Default constructor. 22 | * 23 | * @param fieldName the name to use when indexing the field with Lucene. 24 | */ 25 | constructor(val fieldName: String) { 26 | PARENT_HASH("parentHash"), 27 | OMMERS_HASH("ommersHash"), 28 | COINBASE("coinbase"), 29 | STATE_ROOT("stateRoot"), 30 | DIFFICULTY("difficulty"), 31 | NUMBER("number"), 32 | GAS_LIMIT("gasLimit"), 33 | GAS_USED("gasUsed"), 34 | EXTRA_DATA("extraData"), 35 | TIMESTAMP("timestamp"), 36 | TOTAL_DIFFICULTY("totalDifficulty") 37 | } 38 | -------------------------------------------------------------------------------- /eth-repository/src/main/kotlin/net/consensys/cava/eth/repository/TransactionReceiptFields.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.eth.repository 14 | 15 | /** 16 | * Transaction receipt index fields. 17 | * 18 | */ 19 | enum class TransactionReceiptFields 20 | /** 21 | * Default constructor. 22 | * 23 | * @param fieldName the name to use when indexing the field with Lucene. 24 | */ 25 | constructor(val fieldName: String) { 26 | INDEX("index"), 27 | TRANSACTION_HASH("txHash"), 28 | BLOCK_HASH("blockHash"), 29 | LOGGER("logger"), 30 | LOG_TOPIC("logTopic"), 31 | BLOOM_FILTER("bloomFilter"), 32 | STATE_ROOT("stateRoot"), 33 | CUMULATIVE_GAS_USED("cumulativeGasUsed"), 34 | STATUS("status") 35 | } 36 | -------------------------------------------------------------------------------- /eth/build.gradle: -------------------------------------------------------------------------------- 1 | description = 'Classes and utilities for working with Ethereum.' 2 | 3 | dependencies { 4 | compile project(':bytes') 5 | compile project(':crypto') 6 | compile project(':rlp') 7 | compile project(':units') 8 | 9 | testCompile project(':junit') 10 | testCompile 'org.bouncycastle:bcprov-jdk15on' 11 | testCompile 'org.junit.jupiter:junit-jupiter-api' 12 | testCompile 'org.junit.jupiter:junit-jupiter-params' 13 | 14 | testRuntime 'org.junit.jupiter:junit-jupiter-engine' 15 | } 16 | -------------------------------------------------------------------------------- /eth/src/main/java/net/consensys/cava/eth/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Classes and utilities for working in the Ethereum domain. 3 | * 4 | *

5 | * These classes are included in the standard Cava distribution, or separately when using the gradle dependency 6 | * 'net.consensys.cava:cava-eth' (cava-eth.jar). 7 | */ 8 | @ParametersAreNonnullByDefault 9 | package net.consensys.cava.eth; 10 | 11 | import javax.annotation.ParametersAreNonnullByDefault; 12 | -------------------------------------------------------------------------------- /eth/src/test/java/net/consensys/cava/eth/BlockBodyTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.eth; 14 | 15 | import static net.consensys.cava.eth.BlockHeaderTest.generateBlockHeader; 16 | import static net.consensys.cava.eth.TransactionTest.generateTransaction; 17 | import static org.junit.jupiter.api.Assertions.assertEquals; 18 | 19 | import net.consensys.cava.bytes.Bytes; 20 | import net.consensys.cava.junit.BouncyCastleExtension; 21 | 22 | import java.util.Arrays; 23 | 24 | import org.junit.jupiter.api.Test; 25 | import org.junit.jupiter.api.extension.ExtendWith; 26 | 27 | @ExtendWith(BouncyCastleExtension.class) 28 | class BlockBodyTest { 29 | 30 | @Test 31 | void testRLPRoundtrip() { 32 | BlockBody blockBody = new BlockBody( 33 | Arrays.asList(generateTransaction(), generateTransaction(), generateTransaction(), generateTransaction()), 34 | Arrays.asList( 35 | generateBlockHeader(), 36 | generateBlockHeader(), 37 | generateBlockHeader(), 38 | generateBlockHeader(), 39 | generateBlockHeader(), 40 | generateBlockHeader())); 41 | Bytes encoded = blockBody.toBytes(); 42 | BlockBody read = BlockBody.fromBytes(encoded); 43 | assertEquals(blockBody, read); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /eth/src/test/java/net/consensys/cava/eth/LogTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.eth; 14 | 15 | import static org.junit.jupiter.api.Assertions.assertEquals; 16 | 17 | import net.consensys.cava.bytes.Bytes; 18 | import net.consensys.cava.bytes.Bytes32; 19 | import net.consensys.cava.rlp.RLP; 20 | 21 | import java.util.Arrays; 22 | 23 | import org.junit.jupiter.api.Test; 24 | 25 | class LogTest { 26 | 27 | @Test 28 | void testRLProundtrip() { 29 | Log log = new Log( 30 | Address.fromBytes(Bytes.random(20)), 31 | Bytes.of(1, 2, 3), 32 | Arrays.asList(Bytes32.random(), Bytes32.random())); 33 | Bytes rlp = RLP.encode(log::writeTo); 34 | Log read = RLP.decode(rlp, Log::readFrom); 35 | assertEquals(log, read); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | kotlin.code.style=official 2 | -------------------------------------------------------------------------------- /gradle/spotless.license.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright $YEAR ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ConsenSysMesh/cava/6bb7cf2162beffc84d6b479473a103994f006ced/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-5.0-bin.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /io/build.gradle: -------------------------------------------------------------------------------- 1 | description = 'Classes and utilities for handling file and network IO.' 2 | 3 | dependencies { 4 | compile 'com.google.guava:guava' 5 | compileOnly project(':bytes') 6 | 7 | testCompile project(':bytes') 8 | testCompile project(':junit') 9 | testCompile 'org.junit.jupiter:junit-jupiter-api' 10 | testCompile 'org.junit.jupiter:junit-jupiter-params' 11 | 12 | testRuntime files('src/test/resources/resourceresolver-test.jar') 13 | testRuntime 'org.junit.jupiter:junit-jupiter-engine' 14 | } 15 | -------------------------------------------------------------------------------- /io/src/main/java/net/consensys/cava/io/IOConsumer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.io; 14 | 15 | import java.io.IOException; 16 | 17 | /** 18 | * Represents an operation that accepts a single input argument and returns no result. 19 | */ 20 | @FunctionalInterface 21 | public interface IOConsumer { 22 | 23 | /** 24 | * Performs this operation on the given argument. 25 | * 26 | * @param t the input argument 27 | * @throws IOException If an IO error occurs. 28 | */ 29 | void accept(T t) throws IOException; 30 | } 31 | -------------------------------------------------------------------------------- /io/src/main/java/net/consensys/cava/io/NullOutputStream.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.io; 14 | 15 | import java.io.OutputStream; 16 | 17 | final class NullOutputStream extends OutputStream { 18 | static final NullOutputStream INSTANCE = new NullOutputStream(); 19 | 20 | @Override 21 | public void write(int b) { 22 | // do nothing 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /io/src/main/java/net/consensys/cava/io/file/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Classes and utilities for handling file IO. 3 | * 4 | *

5 | * These classes are included in the standard Cava distribution, or separately when using the gradle dependency 6 | * 'net.consensys.cava:cava-io' (cava-io.jar). 7 | */ 8 | @ParametersAreNonnullByDefault 9 | package net.consensys.cava.io.file; 10 | 11 | import javax.annotation.ParametersAreNonnullByDefault; 12 | -------------------------------------------------------------------------------- /io/src/main/java/net/consensys/cava/io/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Classes and utilities for handling file and network IO. 3 | * 4 | *

5 | * These classes are included in the standard Cava distribution, or separately when using the gradle dependency 6 | * 'net.consensys.cava:cava-io' (cava-io.jar). 7 | */ 8 | @ParametersAreNonnullByDefault 9 | package net.consensys.cava.io; 10 | 11 | import javax.annotation.ParametersAreNonnullByDefault; 12 | -------------------------------------------------------------------------------- /io/src/test/java/net/consensys/cava/io/Base64Test.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.io; 14 | 15 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; 16 | import static org.junit.jupiter.api.Assertions.assertEquals; 17 | 18 | import net.consensys.cava.bytes.Bytes; 19 | 20 | import org.junit.jupiter.api.Test; 21 | 22 | class Base64Test { 23 | 24 | @Test 25 | void shouldEncodeByteArray() { 26 | String s = Base64.encodeBytes(new byte[] {1, 2, 3, 4, 5, 6, 7, 8}); 27 | assertEquals("AQIDBAUGBwg=", s); 28 | } 29 | 30 | @Test 31 | void shouldEncodeBytesValue() { 32 | String s = Base64.encode(Bytes.of(1, 2, 3, 4, 5, 6, 7, 8)); 33 | assertEquals("AQIDBAUGBwg=", s); 34 | } 35 | 36 | @Test 37 | void shouldDecodeToByteArray() { 38 | byte[] bytes = Base64.decodeBytes("AQIDBAUGBwg="); 39 | assertArrayEquals(new byte[] {1, 2, 3, 4, 5, 6, 7, 8}, bytes); 40 | } 41 | 42 | @Test 43 | void shouldDecodeToBytesValue() { 44 | Bytes bytes = Base64.decode("AQIDBAUGBwg="); 45 | assertEquals(Bytes.of(1, 2, 3, 4, 5, 6, 7, 8), bytes); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /io/src/test/java/net/consensys/cava/io/StreamsTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.io; 14 | 15 | import static net.consensys.cava.io.Streams.enumerationStream; 16 | import static org.junit.jupiter.api.Assertions.*; 17 | 18 | import java.util.Arrays; 19 | import java.util.Collections; 20 | import java.util.Enumeration; 21 | import java.util.List; 22 | import java.util.stream.Collectors; 23 | 24 | import org.junit.jupiter.api.Test; 25 | 26 | class StreamsTest { 27 | 28 | @Test 29 | void shouldStreamAnEnumeration() { 30 | Enumeration enumeration = Collections.enumeration(Arrays.asList("RED", "BLUE", "GREEN")); 31 | List result = enumerationStream(enumeration).map(String::toLowerCase).collect(Collectors.toList()); 32 | assertEquals(Arrays.asList("red", "blue", "green"), result); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /io/src/test/resources/net/consensys/cava/io/file/resourceresolver/subdir/test3.yaml: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ConsenSysMesh/cava/6bb7cf2162beffc84d6b479473a103994f006ced/io/src/test/resources/net/consensys/cava/io/file/resourceresolver/subdir/test3.yaml -------------------------------------------------------------------------------- /io/src/test/resources/net/consensys/cava/io/file/resourceresolver/test1.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ConsenSysMesh/cava/6bb7cf2162beffc84d6b479473a103994f006ced/io/src/test/resources/net/consensys/cava/io/file/resourceresolver/test1.txt -------------------------------------------------------------------------------- /io/src/test/resources/net/consensys/cava/io/file/resourceresolver/test2.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ConsenSysMesh/cava/6bb7cf2162beffc84d6b479473a103994f006ced/io/src/test/resources/net/consensys/cava/io/file/resourceresolver/test2.txt -------------------------------------------------------------------------------- /io/src/test/resources/net/consensys/cava/io/file/test.txt: -------------------------------------------------------------------------------- 1 | ABCDEFGHIJKLMNOPQRSTUVWXYZ 2 | abcdefghijklmnopqrstuvwxyz 3 | 01234567890123345678901234 4 | -------------------------------------------------------------------------------- /io/src/test/resources/resourceresolver-test.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ConsenSysMesh/cava/6bb7cf2162beffc84d6b479473a103994f006ced/io/src/test/resources/resourceresolver-test.jar -------------------------------------------------------------------------------- /junit/build.gradle: -------------------------------------------------------------------------------- 1 | description = 'Utilities for better junit testing.' 2 | 3 | dependencies { 4 | compile project(':io') 5 | compileOnly 'com.github.kstyrc:embedded-redis' 6 | compileOnly 'io.vertx:vertx-core' 7 | compileOnly 'org.bouncycastle:bcprov-jdk15on' 8 | compileOnly 'org.junit.jupiter:junit-jupiter-api' 9 | compileOnly 'org.apache.lucene:lucene-core' 10 | 11 | testCompile 'com.github.kstyrc:embedded-redis' 12 | testCompile 'io.lettuce:lettuce-core' 13 | testCompile 'org.apache.lucene:lucene-core' 14 | testCompile 'org.junit.jupiter:junit-jupiter-api' 15 | testCompile 'org.junit.jupiter:junit-jupiter-params' 16 | 17 | testRuntime 'org.junit.jupiter:junit-jupiter-engine' 18 | } 19 | -------------------------------------------------------------------------------- /junit/src/main/java/net/consensys/cava/junit/BouncyCastleExtension.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.junit; 14 | 15 | import java.security.Security; 16 | 17 | import org.bouncycastle.jce.provider.BouncyCastleProvider; 18 | import org.junit.jupiter.api.extension.BeforeAllCallback; 19 | import org.junit.jupiter.api.extension.ExtensionContext; 20 | 21 | /** 22 | * A junit5 extension, that installs a BouncyCastle security provider. 23 | * 24 | */ 25 | public class BouncyCastleExtension implements BeforeAllCallback { 26 | 27 | @Override 28 | public void beforeAll(ExtensionContext context) throws Exception { 29 | Security.addProvider(new BouncyCastleProvider()); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /junit/src/main/java/net/consensys/cava/junit/LuceneIndex.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.junit; 14 | 15 | import java.lang.annotation.ElementType; 16 | import java.lang.annotation.Retention; 17 | import java.lang.annotation.RetentionPolicy; 18 | import java.lang.annotation.Target; 19 | 20 | /** 21 | * A parameter annotation for injecting a Lucene index directory into junit5 tests. 22 | */ 23 | @Target(ElementType.PARAMETER) 24 | @Retention(RetentionPolicy.RUNTIME) 25 | public @interface LuceneIndex { 26 | } 27 | -------------------------------------------------------------------------------- /junit/src/main/java/net/consensys/cava/junit/LuceneIndexWriter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.junit; 14 | 15 | import java.lang.annotation.ElementType; 16 | import java.lang.annotation.Retention; 17 | import java.lang.annotation.RetentionPolicy; 18 | import java.lang.annotation.Target; 19 | 20 | /** 21 | * A parameter annotation for injecting a Lucene index writer into junit5 tests. 22 | */ 23 | @Target(ElementType.PARAMETER) 24 | @Retention(RetentionPolicy.RUNTIME) 25 | public @interface LuceneIndexWriter { 26 | } 27 | -------------------------------------------------------------------------------- /junit/src/main/java/net/consensys/cava/junit/RedisPort.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.junit; 14 | 15 | import java.lang.annotation.ElementType; 16 | import java.lang.annotation.Retention; 17 | import java.lang.annotation.RetentionPolicy; 18 | import java.lang.annotation.Target; 19 | 20 | /** 21 | * A parameter annotation for injecting the running Redis server port into junit5 tests. 22 | */ 23 | @Target(ElementType.PARAMETER) 24 | @Retention(RetentionPolicy.RUNTIME) 25 | public @interface RedisPort { 26 | 27 | } 28 | -------------------------------------------------------------------------------- /junit/src/main/java/net/consensys/cava/junit/TempDirectory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.junit; 14 | 15 | import java.lang.annotation.ElementType; 16 | import java.lang.annotation.Retention; 17 | import java.lang.annotation.RetentionPolicy; 18 | import java.lang.annotation.Target; 19 | 20 | /** 21 | * A parameter annotation for injecting a temporary directory into junit5 tests. 22 | */ 23 | @Target(ElementType.PARAMETER) 24 | @Retention(RetentionPolicy.RUNTIME) 25 | public @interface TempDirectory { 26 | } 27 | -------------------------------------------------------------------------------- /junit/src/main/java/net/consensys/cava/junit/VertxInstance.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.junit; 14 | 15 | import java.lang.annotation.ElementType; 16 | import java.lang.annotation.Retention; 17 | import java.lang.annotation.RetentionPolicy; 18 | import java.lang.annotation.Target; 19 | 20 | /** 21 | * A parameter annotation for injecting a temporary Vert.X instance into junit5 tests. 22 | */ 23 | @Target(ElementType.PARAMETER) 24 | @Retention(RetentionPolicy.RUNTIME) 25 | public @interface VertxInstance { 26 | } 27 | -------------------------------------------------------------------------------- /junit/src/main/java/net/consensys/cava/junit/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Utilities for better junit testing. 3 | * 4 | *

5 | * These classes are included in the standard Cava distribution, or separately when using the gradle dependency 6 | * 'net.consensys.cava:cava-junit' (cava-junit.jar). 7 | */ 8 | package net.consensys.cava.junit; 9 | -------------------------------------------------------------------------------- /junit/src/test/java/net/consensys/cava/junit/LuceneIndexWriterExtensionTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.junit; 14 | 15 | import static org.junit.jupiter.api.Assertions.assertNotNull; 16 | import static org.junit.jupiter.api.Assertions.assertTrue; 17 | 18 | import org.apache.lucene.document.Document; 19 | import org.apache.lucene.index.IndexWriter; 20 | import org.apache.lucene.store.Directory; 21 | import org.junit.jupiter.api.Test; 22 | import org.junit.jupiter.api.extension.ExtendWith; 23 | 24 | @ExtendWith(LuceneIndexWriterExtension.class) 25 | class LuceneIndexWriterExtensionTest { 26 | 27 | @Test 28 | void shouldHaveAccessToLuceneIndexWriter(@LuceneIndexWriter IndexWriter indexWriter) throws Exception { 29 | assertTrue(indexWriter.isOpen()); 30 | indexWriter.addDocument(new Document()); 31 | indexWriter.commit(); 32 | } 33 | 34 | @Test 35 | void shouldHaveAccessToLuceneIndex(@LuceneIndex Directory index) throws Exception { 36 | assertNotNull(index); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /junit/src/test/java/net/consensys/cava/junit/RedisServerExtensionTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.junit; 14 | 15 | import static org.junit.jupiter.api.Assertions.assertNotNull; 16 | import static org.junit.jupiter.api.Assertions.assertTrue; 17 | 18 | import java.net.InetAddress; 19 | 20 | import io.lettuce.core.RedisClient; 21 | import io.lettuce.core.RedisURI; 22 | import io.lettuce.core.api.StatefulRedisConnection; 23 | import org.junit.jupiter.api.Test; 24 | import org.junit.jupiter.api.extension.ExtendWith; 25 | 26 | @ExtendWith(RedisServerExtension.class) 27 | class RedisServerExtensionTest { 28 | 29 | @Test 30 | void shouldHaveAccessToARedisServer(@RedisPort Integer port) { 31 | assertNotNull(port); 32 | assertTrue(port >= 32768, "Port must be more than 32768, was:" + port); 33 | RedisClient client = RedisClient.create(RedisURI.create(InetAddress.getLoopbackAddress().getHostAddress(), port)); 34 | try (StatefulRedisConnection conn = client.connect()) { 35 | assertTrue(conn.isOpen()); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /junit/src/test/java/net/consensys/cava/junit/TempDirectoryExtensionTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.junit; 14 | 15 | import static org.junit.jupiter.api.Assertions.assertTrue; 16 | 17 | import java.nio.file.Files; 18 | import java.nio.file.Path; 19 | 20 | import org.junit.jupiter.api.Test; 21 | import org.junit.jupiter.api.extension.ExtendWith; 22 | 23 | @ExtendWith(TempDirectoryExtension.class) 24 | class TempDirectoryExtensionTest { 25 | 26 | @Test 27 | void shouldHaveAccessToATemporaryDirectory(@TempDirectory Path tempDir) throws Exception { 28 | assertTrue(Files.exists(tempDir)); 29 | assertTrue(Files.isDirectory(tempDir)); 30 | Files.createFile(tempDir.resolve("file")); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /kademlia/build.gradle: -------------------------------------------------------------------------------- 1 | description = 'Distributed hash table implementations' 2 | 3 | dependencies { 4 | compile "com.google.guava:guava" 5 | compile "org.jetbrains.kotlin:kotlin-stdlib" 6 | 7 | testCompile project(':junit') 8 | testCompile 'org.junit.jupiter:junit-jupiter-api' 9 | testCompile 'org.junit.jupiter:junit-jupiter-params' 10 | 11 | testRuntime 'org.junit.jupiter:junit-jupiter-engine' 12 | } 13 | -------------------------------------------------------------------------------- /kv/build.gradle: -------------------------------------------------------------------------------- 1 | description = 'Key value store implementations.' 2 | 3 | dependencies { 4 | compile project(':bytes') 5 | compile project(':concurrent-coroutines') 6 | compile 'org.jetbrains.kotlinx:kotlinx-coroutines-core' 7 | compile 'org.jetbrains.kotlinx:kotlinx-coroutines-guava' 8 | compile 'org.jetbrains.kotlinx:kotlinx-coroutines-jdk8' 9 | compile 'org.jetbrains.kotlin:kotlin-stdlib-jdk8' 10 | compileOnly 'com.jolbox:bonecp' 11 | compileOnly 'io.lettuce:lettuce-core' 12 | compileOnly 'org.fusesource.leveldbjni:leveldbjni-all' 13 | compileOnly 'org.infinispan:infinispan-core' 14 | compileOnly 'org.mapdb:mapdb' 15 | compileOnly 'org.rocksdb:rocksdbjni' 16 | 17 | testCompile project(':concurrent') 18 | testCompile project(':junit') 19 | testCompile 'com.jolbox:bonecp' 20 | testCompile 'com.github.kstyrc:embedded-redis' 21 | testCompile 'com.h2database:h2' 22 | testCompile 'com.winterbe:expekt' 23 | testCompile 'io.lettuce:lettuce-core' 24 | testCompile 'org.fusesource.leveldbjni:leveldbjni-all' 25 | testCompile 'org.infinispan:infinispan-core' 26 | testCompile 'org.jetbrains.spek:spek-api' 27 | testCompile 'org.junit.jupiter:junit-jupiter-api' 28 | testCompile 'org.junit.jupiter:junit-jupiter-params' 29 | testCompile 'org.mapdb:mapdb' 30 | testCompile 'org.rocksdb:rocksdbjni' 31 | 32 | testRuntime 'org.jetbrains.spek:spek-junit-platform-engine' 33 | testRuntime 'org.junit.jupiter:junit-jupiter-engine' 34 | } 35 | -------------------------------------------------------------------------------- /kv/src/main/java/net/consensys/cava/kv/RedisBytesCodec.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.kv; 14 | 15 | import net.consensys.cava.bytes.Bytes; 16 | 17 | import java.nio.ByteBuffer; 18 | import javax.annotation.Nullable; 19 | 20 | import io.lettuce.core.codec.RedisCodec; 21 | 22 | class RedisBytesCodec implements RedisCodec { 23 | 24 | @Override 25 | public ByteBuffer encodeKey(@Nullable Bytes key) { 26 | if (key == null) { 27 | return ByteBuffer.allocate(0); 28 | } 29 | return ByteBuffer.wrap(key.toArrayUnsafe()); 30 | } 31 | 32 | @Override 33 | public Bytes decodeKey(@Nullable ByteBuffer bytes) { 34 | if (bytes == null) { 35 | return null; 36 | } 37 | return Bytes.wrapByteBuffer(bytes); 38 | } 39 | 40 | @Override 41 | public ByteBuffer encodeValue(@Nullable Bytes value) { 42 | if (value == null) { 43 | return ByteBuffer.allocate(0); 44 | } 45 | return ByteBuffer.wrap(value.toArrayUnsafe()); 46 | } 47 | 48 | @Override 49 | public Bytes decodeValue(@Nullable ByteBuffer bytes) { 50 | if (bytes == null) { 51 | return null; 52 | } 53 | return Bytes.wrapByteBuffer(bytes); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /kv/src/main/java/net/consensys/cava/kv/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Classes and utilities for working with key/value stores. 3 | * 4 | *

5 | * These classes are included in the complete Cava distribution, or separately when using the gradle dependency 6 | * `net.consensys.cava:cava-kv` (`cava-kv.jar`). 7 | */ 8 | @ParametersAreNonnullByDefault 9 | package net.consensys.cava.kv; 10 | 11 | import javax.annotation.ParametersAreNonnullByDefault; 12 | -------------------------------------------------------------------------------- /kv/src/main/kotlin/net/consensys/cava/kv/InfinispanKeyValueStore.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.kv 14 | 15 | import kotlinx.coroutines.future.await 16 | import net.consensys.cava.bytes.Bytes 17 | import org.infinispan.Cache 18 | 19 | /** 20 | * A key-value store backed by [Infinispan](https://infinispan.org) 21 | * 22 | */ 23 | class InfinispanKeyValueStore constructor(private val cache: Cache) : KeyValueStore { 24 | 25 | companion object { 26 | 27 | /** 28 | * Open an Infinispan key-value store. 29 | * 30 | * @param cache The backing cache for this store. 31 | * @return A key-value store. 32 | */ 33 | @JvmStatic 34 | fun open(cache: Cache) = InfinispanKeyValueStore(cache) 35 | } 36 | 37 | override suspend fun get(key: Bytes): Bytes? = cache.getAsync(key).await() 38 | 39 | override suspend fun put(key: Bytes, value: Bytes) { 40 | cache.putAsync(key, value).await() 41 | } 42 | 43 | /** 44 | * The cache is managed outside the scope of this key-value store. 45 | */ 46 | override fun close() { 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /les/build.gradle: -------------------------------------------------------------------------------- 1 | dependencies { 2 | compile project(':bytes') 3 | compile project(':concurrent') 4 | compile project(':concurrent-coroutines') 5 | compile project(':crypto') 6 | compile project(':eth') 7 | compile project(':eth-repository') 8 | compile project(':kv') 9 | compile project(':rlpx') 10 | compile 'com.google.guava:guava' 11 | compile 'org.logl:logl-api' 12 | 13 | compileOnly 'io.vertx:vertx-core' 14 | compile 'org.xerial.snappy:snappy-java' 15 | compile 'org.bouncycastle:bcprov-jdk15on' 16 | 17 | testCompile project(':junit') 18 | testCompile 'io.vertx:vertx-core' 19 | testCompile 'org.bouncycastle:bcprov-jdk15on' 20 | testCompile 'org.junit.jupiter:junit-jupiter-api' 21 | testCompile 'org.junit.jupiter:junit-jupiter-params' 22 | testCompile 'org.logl:logl-logl' 23 | 24 | testRuntime 'org.junit.jupiter:junit-jupiter-engine' 25 | } 26 | -------------------------------------------------------------------------------- /les/src/main/kotlin/net/consensys/cava/les/BlockBodiesMessage.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.les 14 | 15 | import net.consensys.cava.bytes.Bytes 16 | import net.consensys.cava.eth.BlockBody 17 | import net.consensys.cava.rlp.RLP 18 | 19 | internal data class BlockBodiesMessage( 20 | val reqID: Long, 21 | val bufferValue: Long, 22 | val blockBodies: List 23 | ) { 24 | 25 | fun toBytes(): Bytes { 26 | return RLP.encodeList { writer -> 27 | writer.writeLong(reqID) 28 | writer.writeLong(bufferValue) 29 | writer.writeList(blockBodies) { eltWriter, blockBody -> blockBody.writeTo(eltWriter) } 30 | } 31 | } 32 | 33 | companion object { 34 | 35 | fun read(bytes: Bytes): BlockBodiesMessage { 36 | return RLP.decodeList( 37 | bytes 38 | ) { reader -> 39 | BlockBodiesMessage( 40 | reader.readLong(), 41 | reader.readLong(), 42 | reader.readListContents { BlockBody.readFrom(it) } 43 | ) 44 | } 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /les/src/main/kotlin/net/consensys/cava/les/BlockHeadersMessage.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.les 14 | 15 | import net.consensys.cava.bytes.Bytes 16 | import net.consensys.cava.eth.BlockHeader 17 | import net.consensys.cava.rlp.RLP 18 | 19 | internal data class BlockHeadersMessage( 20 | val reqID: Long, 21 | val bufferValue: Long, 22 | val blockHeaders: List 23 | ) { 24 | 25 | fun toBytes(): Bytes { 26 | return RLP.encodeList { writer -> 27 | writer.writeLong(reqID) 28 | writer.writeLong(bufferValue) 29 | writer.writeList { headersWriter -> 30 | for (bh in blockHeaders) { 31 | headersWriter.writeRLP(bh.toBytes()) 32 | } 33 | } 34 | } 35 | } 36 | 37 | companion object { 38 | 39 | fun read(bytes: Bytes): BlockHeadersMessage { 40 | return RLP.decodeList(bytes) { reader -> 41 | val reqID = reader.readLong() 42 | val bufferValue = reader.readLong() 43 | val headers = reader.readListContents { headersReader -> 44 | headersReader.readList { 45 | BlockHeader.readFrom(it) 46 | } 47 | } 48 | BlockHeadersMessage(reqID, bufferValue, headers) 49 | } 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /les/src/main/kotlin/net/consensys/cava/les/GetBlockBodiesMessage.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.les 14 | 15 | import net.consensys.cava.bytes.Bytes 16 | import net.consensys.cava.eth.Hash 17 | import net.consensys.cava.rlp.RLP 18 | 19 | internal data class GetBlockBodiesMessage(val reqID: Long, val blockHashes: List) { 20 | 21 | fun toBytes(): Bytes { 22 | return RLP.encodeList { writer -> 23 | writer.writeLong(reqID) 24 | writer.writeList(blockHashes) { eltWriter, hash -> eltWriter.writeValue(hash.toBytes()) } 25 | } 26 | } 27 | 28 | companion object { 29 | 30 | fun read(bytes: Bytes): GetBlockBodiesMessage { 31 | return RLP.decodeList( 32 | bytes 33 | ) { reader -> 34 | GetBlockBodiesMessage( 35 | reader.readLong(), 36 | reader.readListContents { elementReader -> Hash.fromBytes(elementReader.readValue()) }) 37 | } 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /les/src/main/kotlin/net/consensys/cava/les/GetReceiptsMessage.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.les 14 | 15 | import net.consensys.cava.bytes.Bytes 16 | import net.consensys.cava.eth.Hash 17 | import net.consensys.cava.rlp.RLP 18 | 19 | internal data class GetReceiptsMessage(val reqID: Long, val blockHashes: List) { 20 | 21 | fun toBytes(): Bytes { 22 | return RLP.encodeList { writer -> 23 | writer.writeLong(reqID) 24 | writer.writeList(blockHashes) { eltWriter, hash -> eltWriter.writeValue(hash.toBytes()) } 25 | } 26 | } 27 | 28 | companion object { 29 | 30 | fun read(bytes: Bytes): GetReceiptsMessage { 31 | return RLP.decodeList( 32 | bytes 33 | ) { reader -> 34 | GetReceiptsMessage( 35 | reader.readLong(), 36 | reader.readListContents { elementReader -> Hash.fromBytes(elementReader.readValue()) }) 37 | } 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /les/src/main/kotlin/net/consensys/cava/les/LESPeerState.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.les 14 | 15 | import net.consensys.cava.eth.Hash 16 | import java.util.concurrent.ConcurrentHashMap 17 | 18 | internal class LESPeerState { 19 | 20 | var ourStatusMessage: StatusMessage? = null 21 | var peerStatusMessage: StatusMessage? = null 22 | val requestsCache = ConcurrentHashMap>() 23 | 24 | fun handshakeComplete(): Boolean { 25 | ourStatusMessage?.let { 26 | peerStatusMessage?.let { 27 | return true 28 | } 29 | } 30 | return false 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /les/src/main/kotlin/net/consensys/cava/les/ReceiptsMessage.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.les 14 | 15 | import net.consensys.cava.bytes.Bytes 16 | import net.consensys.cava.eth.TransactionReceipt 17 | import net.consensys.cava.rlp.RLP 18 | 19 | internal data class ReceiptsMessage( 20 | val reqID: Long, 21 | val bufferValue: Long, 22 | val receipts: List> 23 | ) { 24 | 25 | fun toBytes(): Bytes { 26 | return RLP.encodeList { writer -> 27 | writer.writeLong(reqID) 28 | writer.writeLong(bufferValue) 29 | writer.writeList(receipts) { 30 | eltWriter, listOfReceipts -> eltWriter.writeList(listOfReceipts) { 31 | txWriter, txReceipt -> txReceipt.writeTo(txWriter) 32 | } 33 | } 34 | } 35 | } 36 | 37 | companion object { 38 | 39 | fun read(bytes: Bytes): ReceiptsMessage { 40 | return RLP.decodeList( 41 | bytes 42 | ) { reader -> 43 | ReceiptsMessage( 44 | reader.readLong(), 45 | reader.readLong(), 46 | reader.readListContents { listTx -> listTx.readListContents { TransactionReceipt.readFrom(it) } } 47 | ) 48 | } 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /merkle-trie/build.gradle: -------------------------------------------------------------------------------- 1 | description = 'Patricia Merkle Trie implementations.' 2 | 3 | dependencies { 4 | compile project(':bytes') 5 | compile project(':concurrent-coroutines') 6 | compile project(':crypto') 7 | compile project(':rlp') 8 | compile 'com.google.guava:guava' 9 | compile 'org.jetbrains.kotlinx:kotlinx-coroutines-core' 10 | 11 | testCompile project(':junit') 12 | testCompile 'org.bouncycastle:bcprov-jdk15on' 13 | testCompile 'org.junit.jupiter:junit-jupiter-api' 14 | testCompile 'org.junit.jupiter:junit-jupiter-params' 15 | 16 | testRuntime 'org.junit.jupiter:junit-jupiter-engine' 17 | } 18 | -------------------------------------------------------------------------------- /merkle-trie/src/main/java/net/consensys/cava/trie/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Merkle Trie implementations. 3 | * 4 | * Implementations of the Ethereum Patricia Trie, as described at https://github.com/ethereum/wiki/wiki/Patricia-Tree. 6 | * 7 | *

8 | * These classes are included in the standard Cava distribution, or separately when using the gradle dependency 9 | * 'net.consensys.cava:cava-merkle-trie' (cava-merkle-trie.jar). 10 | */ 11 | @ParametersAreNonnullByDefault 12 | package net.consensys.cava.trie; 13 | 14 | import javax.annotation.ParametersAreNonnullByDefault; 15 | -------------------------------------------------------------------------------- /merkle-trie/src/main/kotlin/net/consensys/cava/trie/MerkleStorageException.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.trie 14 | 15 | /** 16 | * This exception is thrown when there is an issue retrieving or decoding values from [MerkleStorage]. 17 | */ 18 | class MerkleStorageException : RuntimeException { 19 | 20 | /** 21 | * Constructs a new exception with the specified detail message. 22 | * The cause is not initialized, and may subsequently be initialized by a 23 | * call to {@link #initCause}. 24 | * 25 | * @param message The detail message. 26 | */ 27 | constructor(message: String) : super(message) 28 | 29 | /** 30 | * Constructs a new exception with the specified detail message and 31 | * cause. 32 | * 33 | * @param message The detail message. 34 | * @param cause The cause. 35 | */ 36 | constructor(message: String, cause: Exception) : super(message, cause) 37 | } 38 | -------------------------------------------------------------------------------- /merkle-trie/src/main/kotlin/net/consensys/cava/trie/Node.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.trie 14 | 15 | import net.consensys.cava.bytes.Bytes 16 | import net.consensys.cava.bytes.Bytes32 17 | 18 | internal interface Node { 19 | 20 | suspend fun accept(visitor: NodeVisitor, path: Bytes): Node 21 | 22 | suspend fun path(): Bytes 23 | 24 | suspend fun value(): V? 25 | 26 | fun rlp(): Bytes 27 | 28 | fun rlpRef(): Bytes 29 | 30 | fun hash(): Bytes32 31 | 32 | suspend fun replacePath(path: Bytes): Node 33 | } 34 | -------------------------------------------------------------------------------- /merkle-trie/src/main/kotlin/net/consensys/cava/trie/NodeFactory.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.trie 14 | 15 | import net.consensys.cava.bytes.Bytes 16 | 17 | internal interface NodeFactory { 18 | 19 | suspend fun createExtension(path: Bytes, child: Node): Node 20 | 21 | suspend fun createBranch(leftIndex: Byte, left: Node, rightIndex: Byte, right: Node): Node 22 | 23 | suspend fun createBranch(newChildren: List>, value: V?): Node 24 | 25 | suspend fun createLeaf(path: Bytes, value: V): Node 26 | } 27 | -------------------------------------------------------------------------------- /merkle-trie/src/main/kotlin/net/consensys/cava/trie/NodeVisitor.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.trie 14 | 15 | import net.consensys.cava.bytes.Bytes 16 | 17 | internal interface NodeVisitor { 18 | 19 | suspend fun visit(extensionNode: ExtensionNode, path: Bytes): Node 20 | 21 | suspend fun visit(branchNode: BranchNode, path: Bytes): Node 22 | 23 | suspend fun visit(leafNode: LeafNode, path: Bytes): Node 24 | 25 | suspend fun visit(nullNode: NullNode, path: Bytes): Node 26 | } 27 | -------------------------------------------------------------------------------- /merkle-trie/src/main/kotlin/net/consensys/cava/trie/NullNode.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.trie 14 | 15 | import net.consensys.cava.bytes.Bytes 16 | import net.consensys.cava.bytes.Bytes32 17 | import net.consensys.cava.crypto.Hash.keccak256 18 | import net.consensys.cava.rlp.RLP 19 | 20 | internal class NullNode private constructor() : Node { 21 | 22 | companion object { 23 | private val RLP_NULL = RLP.encodeByteArray(ByteArray(0)) 24 | private val HASH = keccak256(RLP_NULL) 25 | private val instance = NullNode() 26 | 27 | @Suppress("UNCHECKED_CAST") 28 | fun instance(): NullNode = instance as NullNode 29 | } 30 | 31 | override suspend fun accept(visitor: NodeVisitor, path: Bytes): Node = visitor.visit(this, path) 32 | 33 | override suspend fun path(): Bytes = Bytes.EMPTY 34 | 35 | override suspend fun value(): V? = null 36 | 37 | override fun rlp(): Bytes = RLP_NULL 38 | 39 | override fun rlpRef(): Bytes = RLP_NULL 40 | 41 | override fun hash(): Bytes32 = HASH 42 | 43 | override suspend fun replacePath(path: Bytes): Node = this 44 | } 45 | -------------------------------------------------------------------------------- /net-coroutines/build.gradle: -------------------------------------------------------------------------------- 1 | description = 'Classes and utilities for coroutine based networking.' 2 | 3 | dependencies { 4 | compile 'com.google.guava:guava' 5 | compile 'org.jetbrains.kotlinx:kotlinx-coroutines-core' 6 | compile 'org.logl:logl-api' 7 | 8 | testCompile project(':junit') 9 | testCompile 'org.junit.jupiter:junit-jupiter-api' 10 | testCompile 'org.junit.jupiter:junit-jupiter-params' 11 | testCompile 'org.logl:logl-logl' 12 | 13 | testRuntime 'org.junit.jupiter:junit-jupiter-engine' 14 | } 15 | -------------------------------------------------------------------------------- /net/build.gradle: -------------------------------------------------------------------------------- 1 | description = 'Classes and utilities for working with networking.' 2 | 3 | dependencies { 4 | compile project(':bytes') 5 | compile project(':crypto') 6 | compile project(':io') 7 | compile 'com.google.guava:guava' 8 | compileOnly 'io.vertx:vertx-core' 9 | compileOnly 'org.bouncycastle:bcprov-jdk15on' 10 | compileOnly 'org.bouncycastle:bcpkix-jdk15on' 11 | 12 | testCompile project(':junit') 13 | testCompile 'com.squareup.okhttp3:okhttp' 14 | testCompile 'io.vertx:vertx-core' 15 | testCompile 'org.bouncycastle:bcprov-jdk15on' 16 | testCompile 'org.bouncycastle:bcpkix-jdk15on' 17 | testCompile 'org.junit.jupiter:junit-jupiter-api' 18 | testCompile 'org.junit.jupiter:junit-jupiter-params' 19 | 20 | testRuntime 'org.junit.jupiter:junit-jupiter-engine' 21 | } 22 | -------------------------------------------------------------------------------- /net/src/main/java/net/consensys/cava/net/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Classes and utilities for working with networking. 3 | * 4 | *

5 | * These classes are included in the standard Cava distribution, or separately when using the gradle dependency 6 | * 'net.consensys.cava:cava-net' (cava-net.jar). 7 | */ 8 | package net.consensys.cava.net; 9 | -------------------------------------------------------------------------------- /net/src/main/java/net/consensys/cava/net/tls/SingleTrustManagerFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.net.tls; 14 | 15 | import java.security.KeyStore; 16 | import javax.net.ssl.ManagerFactoryParameters; 17 | import javax.net.ssl.TrustManager; 18 | 19 | import io.netty.handler.ssl.util.SimpleTrustManagerFactory; 20 | 21 | final class SingleTrustManagerFactory extends SimpleTrustManagerFactory { 22 | 23 | private final TrustManager[] trustManagers; 24 | 25 | SingleTrustManagerFactory(TrustManager trustManager) { 26 | this.trustManagers = new TrustManager[] {trustManager}; 27 | } 28 | 29 | @Override 30 | protected void engineInit(KeyStore keyStore) {} 31 | 32 | @Override 33 | protected void engineInit(ManagerFactoryParameters managerFactoryParameters) {} 34 | 35 | @Override 36 | protected TrustManager[] engineGetTrustManagers() { 37 | return trustManagers; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /net/src/main/java/net/consensys/cava/net/tls/TLSEnvironmentException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.net.tls; 14 | 15 | final class TLSEnvironmentException extends RuntimeException { 16 | 17 | TLSEnvironmentException(String message) { 18 | super(message); 19 | } 20 | 21 | TLSEnvironmentException(String message, Throwable cause) { 22 | super(message, cause); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /net/src/main/java/net/consensys/cava/net/tls/TrustManagerFactoryWrapper.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.net.tls; 14 | 15 | import javax.net.ssl.TrustManagerFactory; 16 | 17 | import io.vertx.core.Vertx; 18 | import io.vertx.core.net.TrustOptions; 19 | 20 | final class TrustManagerFactoryWrapper implements TrustOptions { 21 | 22 | private final TrustManagerFactory trustManagerFactory; 23 | 24 | TrustManagerFactoryWrapper(TrustManagerFactory trustManagerFactory) { 25 | this.trustManagerFactory = trustManagerFactory; 26 | } 27 | 28 | @Override 29 | public TrustOptions clone() { 30 | return new TrustManagerFactoryWrapper(trustManagerFactory); 31 | } 32 | 33 | @Override 34 | public TrustManagerFactory getTrustManagerFactory(Vertx vertx) { 35 | return trustManagerFactory; 36 | } 37 | 38 | @Override 39 | public boolean equals(Object obj) { 40 | if (obj == this) { 41 | return true; 42 | } 43 | if (!(obj instanceof TrustManagerFactoryWrapper)) { 44 | return false; 45 | } 46 | TrustManagerFactoryWrapper other = (TrustManagerFactoryWrapper) obj; 47 | return trustManagerFactory.equals(other.trustManagerFactory); 48 | } 49 | 50 | @Override 51 | public int hashCode() { 52 | return trustManagerFactory.hashCode(); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /net/src/main/java/net/consensys/cava/net/tls/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Utilities for doing fingerprint based TLS certificate checking. 3 | */ 4 | @ParametersAreNonnullByDefault 5 | package net.consensys.cava.net.tls; 6 | 7 | import javax.annotation.ParametersAreNonnullByDefault; 8 | -------------------------------------------------------------------------------- /net/src/test/java/net/consensys/cava/net/tls/InsecureTrustOptions.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.net.tls; 14 | 15 | import javax.net.ssl.TrustManagerFactory; 16 | 17 | import io.netty.handler.ssl.util.InsecureTrustManagerFactory; 18 | import io.vertx.core.Vertx; 19 | import io.vertx.core.net.TrustOptions; 20 | 21 | final class InsecureTrustOptions implements TrustOptions { 22 | 23 | static InsecureTrustOptions INSTANCE = new InsecureTrustOptions(); 24 | 25 | private InsecureTrustOptions() {} 26 | 27 | @Override 28 | public TrustOptions clone() { 29 | return this; 30 | } 31 | 32 | @Override 33 | public TrustManagerFactory getTrustManagerFactory(Vertx vertx) { 34 | return InsecureTrustManagerFactory.INSTANCE; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /plumtree/build.gradle: -------------------------------------------------------------------------------- 1 | description = 'Plumtree - Push-Lazy-pUsh Multicast TREE, an implementation of Epidemic Broadcast Tree' 2 | 3 | dependencies { 4 | compile project(':bytes') 5 | compile project(':concurrent') 6 | compile project(':crypto') 7 | 8 | compileOnly 'io.vertx:vertx-core' 9 | 10 | testCompile project(':junit') 11 | testCompile 'io.vertx:vertx-core' 12 | testCompile 'org.bouncycastle:bcprov-jdk15on' 13 | testCompile 'org.junit.jupiter:junit-jupiter-api' 14 | testCompile 'org.junit.jupiter:junit-jupiter-params' 15 | testRuntime 'org.junit.jupiter:junit-jupiter-engine' 16 | } 17 | -------------------------------------------------------------------------------- /plumtree/src/main/java/net/consensys/cava/plumtree/MessageHashing.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.plumtree; 14 | 15 | import net.consensys.cava.bytes.Bytes; 16 | 17 | /** 18 | * Produces an identifiable footprint for a message (generally a hash) that can be passed on to other peers to identify 19 | * uniquely a message being propagated. 20 | */ 21 | public interface MessageHashing { 22 | 23 | public Bytes hash(Bytes message); 24 | 25 | } 26 | -------------------------------------------------------------------------------- /plumtree/src/main/java/net/consensys/cava/plumtree/MessageSender.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.plumtree; 14 | 15 | import net.consensys.cava.bytes.Bytes; 16 | 17 | import javax.annotation.Nullable; 18 | 19 | /** 20 | * Interface to sending messages to other peers. 21 | */ 22 | public interface MessageSender { 23 | 24 | /** 25 | * Types of message supported by the dialect 26 | */ 27 | enum Verb { 28 | IHAVE, GRAFT, PRUNE, GOSSIP 29 | } 30 | 31 | /** 32 | * Sends bytes to a peer. 33 | * 34 | * @param verb the type of message 35 | * @param peer the target of the message 36 | * @param hash the hash of the message 37 | * @param payload the bytes to send 38 | */ 39 | void sendMessage(Verb verb, Peer peer, Bytes hash, @Nullable Bytes payload); 40 | } 41 | -------------------------------------------------------------------------------- /plumtree/src/main/java/net/consensys/cava/plumtree/MessageValidator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.plumtree; 14 | 15 | import net.consensys.cava.bytes.Bytes; 16 | 17 | /** 18 | * Validator for a message and a peer. 19 | * 20 | * This validator is called prior to gossiping the message from that peer to other peers. 21 | */ 22 | public interface MessageValidator { 23 | 24 | /** 25 | * Validates that the message from the peer is valid. 26 | * 27 | * @param message the payload sent over the network 28 | * @param peer the peer that sent the message 29 | * @return true if the message is valid 30 | */ 31 | boolean validate(Bytes message, Peer peer); 32 | } 33 | -------------------------------------------------------------------------------- /plumtree/src/main/java/net/consensys/cava/plumtree/Peer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.plumtree; 14 | 15 | /** 16 | * A peer part of the gossip system. 17 | */ 18 | public interface Peer { 19 | } 20 | -------------------------------------------------------------------------------- /plumtree/src/main/java/net/consensys/cava/plumtree/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Library for Plumtree - Push-Lazy-pUsh Multicast TREE, an implementation of Epidemic Broadcast Tree 3 | * 4 | *

5 | * These classes are included in the standard Cava distribution, or separately when using the gradle dependency 6 | * 'net.consensys.cava:cava-plumtree' (cava-plumtree.jar). 7 | */ 8 | @ParametersAreNonnullByDefault 9 | package net.consensys.cava.plumtree; 10 | 11 | import javax.annotation.ParametersAreNonnullByDefault; 12 | -------------------------------------------------------------------------------- /plumtree/src/main/java/net/consensys/cava/plumtree/vertx/SocketPeer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.plumtree.vertx; 14 | 15 | import net.consensys.cava.plumtree.Peer; 16 | 17 | import io.vertx.core.net.NetSocket; 18 | 19 | /** 20 | * Vert.x gossip peer associated with a socket 21 | */ 22 | final class SocketPeer implements Peer { 23 | 24 | private final NetSocket socket; 25 | 26 | SocketPeer(NetSocket socket) { 27 | this.socket = socket; 28 | } 29 | 30 | NetSocket socket() { 31 | return socket; 32 | } 33 | 34 | @Override 35 | public String toString() { 36 | return socket.localAddress().toString(); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /rlp/build.gradle: -------------------------------------------------------------------------------- 1 | description = 'Recursive Length Prefix (RLP) encoding and decoding.' 2 | 3 | dependencies { 4 | compile project(':bytes') 5 | compileOnly project(':units') 6 | 7 | testCompile project(':units') 8 | testCompile 'org.junit.jupiter:junit-jupiter-api' 9 | testCompile 'org.junit.jupiter:junit-jupiter-params' 10 | 11 | testRuntime 'org.junit.jupiter:junit-jupiter-engine' 12 | } 13 | -------------------------------------------------------------------------------- /rlp/src/main/java/net/consensys/cava/rlp/BytesRLPWriter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.rlp; 14 | 15 | import net.consensys.cava.bytes.Bytes; 16 | 17 | import java.util.Deque; 18 | 19 | final class BytesRLPWriter extends DelegatingRLPWriter { 20 | 21 | BytesRLPWriter() { 22 | super(new AccumulatingRLPWriter()); 23 | } 24 | 25 | Bytes toBytes() { 26 | Deque values = delegate.values(); 27 | if (values.isEmpty()) { 28 | return Bytes.EMPTY; 29 | } 30 | return Bytes.wrap(values.stream().map(Bytes::wrap).toArray(Bytes[]::new)); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /rlp/src/main/java/net/consensys/cava/rlp/EndOfRLPException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.rlp; 14 | 15 | /** 16 | * Indicates the end of the RLP source has been reached unexpectedly. 17 | */ 18 | public class EndOfRLPException extends RLPException { 19 | public EndOfRLPException() { 20 | super("End of RLP source reached"); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /rlp/src/main/java/net/consensys/cava/rlp/InvalidRLPEncodingException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.rlp; 14 | 15 | /** 16 | * Indicates that invalid RLP encoding was encountered. 17 | */ 18 | public class InvalidRLPEncodingException extends RLPException { 19 | public InvalidRLPEncodingException(String message) { 20 | super(message); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /rlp/src/main/java/net/consensys/cava/rlp/InvalidRLPTypeException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.rlp; 14 | 15 | /** 16 | * Indicates that an unexpected type was encountered when decoding RLP. 17 | */ 18 | public class InvalidRLPTypeException extends RLPException { 19 | public InvalidRLPTypeException(String message) { 20 | super(message); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /rlp/src/main/java/net/consensys/cava/rlp/RLPException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.rlp; 14 | 15 | /** 16 | * Base type for all RLP encoding and decoding exceptions. 17 | */ 18 | public class RLPException extends RuntimeException { 19 | public RLPException(String message) { 20 | super(message); 21 | } 22 | 23 | public RLPException(Throwable cause) { 24 | super(cause); 25 | } 26 | 27 | public RLPException(String message, Throwable cause) { 28 | super(message, cause); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /rlp/src/main/java/net/consensys/cava/rlp/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Recursive Length Prefix (RLP) encoding and decoding. 3 | *

4 | * An implementation of the Ethereum Recursive Length Prefix (RLP) algorithm, as described at 5 | * https://github.com/ethereum/wiki/wiki/RLP. 6 | *

7 | * These classes are included in the standard Cava distribution, or separately when using the gradle dependency 8 | * 'net.consensys.cava:cava-rlp' (cava-rlp.jar). 9 | */ 10 | @ParametersAreNonnullByDefault 11 | package net.consensys.cava.rlp; 12 | 13 | import javax.annotation.ParametersAreNonnullByDefault; 14 | -------------------------------------------------------------------------------- /rlpx/build.gradle: -------------------------------------------------------------------------------- 1 | dependencies { 2 | compile project(':crypto') 3 | compile project(':bytes') 4 | compile project(':concurrent') 5 | compile project(':rlp') 6 | compile 'com.google.guava:guava' 7 | compile 'org.logl:logl-api' 8 | 9 | compileOnly 'io.vertx:vertx-core' 10 | compile 'org.xerial.snappy:snappy-java' 11 | compile 'org.bouncycastle:bcprov-jdk15on' 12 | 13 | testCompile project(':junit') 14 | testCompile 'io.vertx:vertx-core' 15 | testCompile 'org.bouncycastle:bcprov-jdk15on' 16 | testCompile 'org.junit.jupiter:junit-jupiter-api' 17 | testCompile 'org.junit.jupiter:junit-jupiter-params' 18 | testCompile 'org.logl:logl-logl' 19 | 20 | testRuntime 'org.junit.jupiter:junit-jupiter-engine' 21 | } 22 | -------------------------------------------------------------------------------- /rlpx/src/main/java/net/consensys/cava/rlpx/HandshakeMessage.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.rlpx; 14 | 15 | import net.consensys.cava.bytes.Bytes32; 16 | import net.consensys.cava.crypto.SECP256K1; 17 | 18 | /** 19 | * Contents of a message sent as part of a RLPx handshake. 20 | */ 21 | public interface HandshakeMessage { 22 | 23 | /** 24 | * @return the ephemeral public key included in the response 25 | */ 26 | public SECP256K1.PublicKey ephemeralPublicKey(); 27 | 28 | /** 29 | * @return the response nonce 30 | */ 31 | public Bytes32 nonce(); 32 | } 33 | -------------------------------------------------------------------------------- /rlpx/src/main/java/net/consensys/cava/rlpx/InvalidMACException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.rlpx; 14 | 15 | /** 16 | * Exception thrown when the message contents do not match the Message Authentication Code. 17 | */ 18 | public class InvalidMACException extends RuntimeException { 19 | 20 | InvalidMACException(Throwable t) { 21 | super(t); 22 | } 23 | 24 | InvalidMACException(String msg) { 25 | super(msg); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /rlpx/src/main/java/net/consensys/cava/rlpx/MemoryWireConnectionsRepository.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.rlpx; 14 | 15 | import net.consensys.cava.rlpx.wire.WireConnection; 16 | 17 | import java.util.Map; 18 | import java.util.concurrent.ConcurrentHashMap; 19 | 20 | /** 21 | * In-memory implementation of the wire connections repository. 22 | * 23 | */ 24 | public class MemoryWireConnectionsRepository implements WireConnectionRepository { 25 | 26 | private final Map connections = new ConcurrentHashMap<>(); 27 | 28 | @Override 29 | public void add(WireConnection wireConnection) { 30 | connections.put(wireConnection.id(), wireConnection); 31 | } 32 | 33 | @Override 34 | public WireConnection get(String id) { 35 | return connections.get(id); 36 | } 37 | 38 | @Override 39 | public Iterable asIterable() { 40 | return connections.values(); 41 | } 42 | 43 | @Override 44 | public void close() { 45 | connections.clear(); 46 | } 47 | 48 | public Map asMap() { 49 | return connections; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /rlpx/src/main/java/net/consensys/cava/rlpx/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Classes and utilities for working with the RLPx wire protocol. 3 | * 4 | *

5 | * These classes are included in the standard Cava distribution, or separately when using the gradle dependency 6 | * 'net.consensys.cava:cava-rlpx' (cava-rlpx.jar). 7 | */ 8 | @ParametersAreNonnullByDefault 9 | 10 | package net.consensys.cava.rlpx; 11 | 12 | import javax.annotation.ParametersAreNonnullByDefault; 13 | -------------------------------------------------------------------------------- /rlpx/src/main/java/net/consensys/cava/rlpx/vertx/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Classes and utilities for working with the RLPx wire protocol. 3 | * 4 | *

5 | * These classes are included in the standard Cava distribution, or separately when using the gradle dependency 6 | * 'net.consensys.cava:cava-rlpx' (cava-rlpx.jar). 7 | */ 8 | @ParametersAreNonnullByDefault 9 | 10 | package net.consensys.cava.rlpx.vertx; 11 | 12 | import javax.annotation.ParametersAreNonnullByDefault; 13 | -------------------------------------------------------------------------------- /rlpx/src/main/java/net/consensys/cava/rlpx/wire/Capability.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.rlpx.wire; 14 | 15 | import java.util.Objects; 16 | 17 | final class Capability { 18 | 19 | private final String name; 20 | 21 | private final int version; 22 | 23 | 24 | Capability(String name, int version) { 25 | this.name = name; 26 | this.version = version; 27 | } 28 | 29 | String name() { 30 | return name; 31 | } 32 | 33 | int version() { 34 | return version; 35 | } 36 | 37 | @Override 38 | public boolean equals(Object o) { 39 | if (this == o) 40 | return true; 41 | if (o == null || getClass() != o.getClass()) 42 | return false; 43 | Capability that = (Capability) o; 44 | return Objects.equals(name, that.name) && Objects.equals(version, that.version); 45 | } 46 | 47 | @Override 48 | public int hashCode() { 49 | return Objects.hash(name, version); 50 | } 51 | 52 | @Override 53 | public String toString() { 54 | return "Capability{" + "name='" + name + '\'' + ", version='" + version + '\'' + '}'; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /rlpx/src/main/java/net/consensys/cava/rlpx/wire/DefaultSubProtocolIdentifier.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.rlpx.wire; 14 | 15 | /** 16 | * Default implementation of a sub protocol identifier 17 | */ 18 | final class DefaultSubProtocolIdentifier implements SubProtocolIdentifier { 19 | 20 | private final String name; 21 | private final int version; 22 | 23 | public DefaultSubProtocolIdentifier(String name, int version) { 24 | this.name = name; 25 | this.version = version; 26 | } 27 | 28 | @Override 29 | public String name() { 30 | return name; 31 | } 32 | 33 | @Override 34 | public int version() { 35 | return version; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /rlpx/src/main/java/net/consensys/cava/rlpx/wire/DisconnectMessage.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.rlpx.wire; 14 | 15 | import net.consensys.cava.bytes.Bytes; 16 | import net.consensys.cava.rlp.RLP; 17 | 18 | final class DisconnectMessage implements WireProtocolMessage { 19 | 20 | private final int reason; 21 | 22 | DisconnectMessage(DisconnectReason reason) { 23 | this(reason.code); 24 | } 25 | 26 | DisconnectMessage(int reason) { 27 | this.reason = reason; 28 | } 29 | 30 | static DisconnectMessage read(Bytes data) { 31 | return RLP.decodeList(data, source -> new DisconnectMessage(source.readInt())); 32 | } 33 | 34 | @Override 35 | public Bytes toBytes() { 36 | return RLP.encodeList(writer -> writer.writeInt(reason)); 37 | } 38 | 39 | @Override 40 | public int messageType() { 41 | return 1; 42 | } 43 | 44 | int reason() { 45 | return reason; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /rlpx/src/main/java/net/consensys/cava/rlpx/wire/DisconnectReason.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.rlpx.wire; 14 | 15 | /** 16 | * Enumeration of all reasons disconnect may happen. 17 | * 18 | */ 19 | public enum DisconnectReason { 20 | REQUESTED(0), 21 | TCP_ERROR(1), 22 | PROTOCOL_BREACH(2), 23 | USELESS_PEER(3), 24 | TOO_MANY_PEERS(4), 25 | ALREADY_CONNECTED(5), 26 | INCOMPATIBLE_DEVP2P_VERSION(6), 27 | NULL_NODE_IDENTITY_RECEIVED(7), 28 | CLIENT_QUITTING(8), 29 | UNEXPECTED_IDENTITY(9), 30 | CONNECTED_TO_SELF(10), 31 | TIMEOUT(11), 32 | SUBPROTOCOL_REASON(16); 33 | 34 | DisconnectReason(int code) { 35 | this.code = code; 36 | } 37 | 38 | public final int code; 39 | } 40 | -------------------------------------------------------------------------------- /rlpx/src/main/java/net/consensys/cava/rlpx/wire/PingMessage.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.rlpx.wire; 14 | 15 | import net.consensys.cava.bytes.Bytes; 16 | 17 | final class PingMessage implements WireProtocolMessage { 18 | 19 | static PingMessage read(Bytes data) { 20 | return new PingMessage(); 21 | } 22 | 23 | @Override 24 | public Bytes toBytes() { 25 | throw new UnsupportedOperationException(); 26 | } 27 | 28 | @Override 29 | public int messageType() { 30 | throw new UnsupportedOperationException(); 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /rlpx/src/main/java/net/consensys/cava/rlpx/wire/PongMessage.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.rlpx.wire; 14 | 15 | import net.consensys.cava.bytes.Bytes; 16 | 17 | final class PongMessage implements WireProtocolMessage { 18 | 19 | static PongMessage read(Bytes data) { 20 | return new PongMessage(); 21 | } 22 | 23 | @Override 24 | public Bytes toBytes() { 25 | throw new UnsupportedOperationException(); 26 | } 27 | 28 | @Override 29 | public int messageType() { 30 | throw new UnsupportedOperationException(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /rlpx/src/main/java/net/consensys/cava/rlpx/wire/SubProtocol.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.rlpx.wire; 14 | 15 | 16 | import net.consensys.cava.rlpx.RLPxService; 17 | 18 | /** 19 | * Defines a subprotocol to be used for wire connections 20 | */ 21 | public interface SubProtocol { 22 | 23 | /** 24 | * @return the identifier of the subprotocol 25 | */ 26 | SubProtocolIdentifier id(); 27 | 28 | /** 29 | * @param subProtocolIdentifier the identifier of the subprotocol 30 | * @return true if the subprotocol ID and version are supported, false otherwise 31 | */ 32 | boolean supports(SubProtocolIdentifier subProtocolIdentifier); 33 | 34 | /** 35 | * Provides the length of the range of message types supported by the subprotocol for a given version 36 | * 37 | * @param version the version of the subprotocol to associate with the range 38 | * @return the length of the range of message types supported by the subprotocol for a given version 39 | */ 40 | int versionRange(int version); 41 | 42 | /** 43 | * Creates a new handler for the subprotocol. 44 | * 45 | * @param service the rlpx service that will use the handler 46 | * @return a new handler for the subprotocol, bound to the service. 47 | */ 48 | SubProtocolHandler createHandler(RLPxService service); 49 | } 50 | -------------------------------------------------------------------------------- /rlpx/src/main/java/net/consensys/cava/rlpx/wire/SubProtocolHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.rlpx.wire; 14 | 15 | import net.consensys.cava.bytes.Bytes; 16 | import net.consensys.cava.concurrent.AsyncCompletion; 17 | 18 | /** 19 | * Handler managing messages and new connections of peers related for a given subprotocol. 20 | */ 21 | public interface SubProtocolHandler { 22 | 23 | /** 24 | * Handle an incoming wire protocol message 25 | * 26 | * @param connectionId the peer connection identifier 27 | * @param messageType the type of the message 28 | * @param message the message to be handled 29 | * @return a handle tracking the completion of the handling of the message. 30 | */ 31 | AsyncCompletion handle(String connectionId, int messageType, Bytes message); 32 | 33 | /** 34 | * Handle a new peer connection 35 | * 36 | * @param connectionId the new peer connection identifier 37 | * @return a handle to the completion of the addition of the new peer. 38 | */ 39 | AsyncCompletion handleNewPeerConnection(String connectionId); 40 | 41 | /** 42 | * Stops a subprotocol operations. 43 | * 44 | * @return a handle to track when the subprotocol operations have stopped 45 | */ 46 | AsyncCompletion stop(); 47 | } 48 | -------------------------------------------------------------------------------- /rlpx/src/main/java/net/consensys/cava/rlpx/wire/SubProtocolIdentifier.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.rlpx.wire; 14 | 15 | /** 16 | * Identifier of a subprotocol, comprised of a name and version. 17 | */ 18 | public interface SubProtocolIdentifier { 19 | 20 | static SubProtocolIdentifier of(String name, int version) { 21 | return new DefaultSubProtocolIdentifier(name, version); 22 | } 23 | 24 | /** 25 | * 26 | * @return the name of the subprotocol 27 | */ 28 | String name(); 29 | 30 | /** 31 | * 32 | * @return the version of the subprotocol 33 | */ 34 | int version(); 35 | } 36 | -------------------------------------------------------------------------------- /rlpx/src/main/java/net/consensys/cava/rlpx/wire/WireConnection.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.rlpx.wire; 14 | 15 | 16 | /** 17 | * A stateful connection between two peers under the Devp2p wire protocol. 18 | */ 19 | public interface WireConnection { 20 | 21 | /** 22 | * 23 | * @return the identifier of this wire connection 24 | */ 25 | public String id(); 26 | } 27 | -------------------------------------------------------------------------------- /rlpx/src/main/java/net/consensys/cava/rlpx/wire/WireProtocolMessage.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.rlpx.wire; 14 | 15 | import net.consensys.cava.bytes.Bytes; 16 | 17 | /** 18 | * A set of bytes made available to a subprotocol after it has been successfully decrypted. 19 | */ 20 | interface WireProtocolMessage { 21 | 22 | 23 | /** 24 | * @return the payload of the wire message, ready for consumption. 25 | */ 26 | Bytes toBytes(); 27 | 28 | /** 29 | * @return the code associated with the message type according to the subprotocol. 30 | */ 31 | int messageType(); 32 | } 33 | -------------------------------------------------------------------------------- /rlpx/src/main/java/net/consensys/cava/rlpx/wire/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Classes and utilities for working with the RLPx wire protocol. 3 | * 4 | *

5 | * These classes are included in the standard Cava distribution, or separately when using the gradle dependency 6 | * 'net.consensys.cava:cava-rlpx' (cava-rlpx.jar). 7 | */ 8 | @ParametersAreNonnullByDefault 9 | 10 | package net.consensys.cava.rlpx.wire; 11 | 12 | import javax.annotation.ParametersAreNonnullByDefault; 13 | -------------------------------------------------------------------------------- /rlpx/src/test/java/net/consensys/cava/rlpx/wire/DisconnectMessageTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.rlpx.wire; 14 | 15 | import static org.junit.jupiter.api.Assertions.assertEquals; 16 | 17 | import net.consensys.cava.bytes.Bytes; 18 | 19 | import org.junit.jupiter.api.Test; 20 | 21 | class DisconnectMessageTest { 22 | 23 | @Test 24 | void testBytesRoundtrip() { 25 | DisconnectMessage msg = new DisconnectMessage(4); 26 | Bytes toBytes = msg.toBytes(); 27 | DisconnectMessage read = DisconnectMessage.read(toBytes); 28 | assertEquals(msg.messageType(), read.messageType()); 29 | assertEquals(msg.reason(), read.reason()); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /rlpx/src/test/java/net/consensys/cava/rlpx/wire/HelloMessageTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.rlpx.wire; 14 | 15 | import static org.junit.jupiter.api.Assertions.assertEquals; 16 | 17 | import net.consensys.cava.bytes.Bytes; 18 | 19 | import java.util.Collections; 20 | 21 | import org.junit.jupiter.api.Test; 22 | 23 | class HelloMessageTest { 24 | 25 | @Test 26 | void p2pVersion() { 27 | HelloMessage msg = HelloMessage.create(Bytes.fromHexString("deadbeef"), 10000, 3, "blah", Collections.emptyList()); 28 | HelloMessage msgRead = HelloMessage.read(msg.toBytes()); 29 | assertEquals(3, msgRead.p2pVersion()); 30 | } 31 | 32 | @Test 33 | void nodeId() { 34 | HelloMessage msg = HelloMessage.create(Bytes.fromHexString("deadbeef"), 10000, 3, "blah", Collections.emptyList()); 35 | HelloMessage msgRead = HelloMessage.read(msg.toBytes()); 36 | assertEquals(Bytes.fromHexString("deadbeef"), msgRead.nodeId()); 37 | } 38 | 39 | @Test 40 | void clientId() { 41 | HelloMessage msg = 42 | HelloMessage.create(Bytes.fromHexString("deadbeef"), 10000, 3, "foofoo", Collections.emptyList()); 43 | HelloMessage msgRead = HelloMessage.read(msg.toBytes()); 44 | assertEquals("foofoo", msgRead.clientId()); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /scuttlebutt-discovery/build.gradle: -------------------------------------------------------------------------------- 1 | description = 'Scuttlebutt Discovery library' 2 | 3 | dependencies { 4 | compile project(':bytes') 5 | compile project(':concurrent') 6 | compile project(':crypto') 7 | compile project(':scuttlebutt') 8 | compile 'io.vertx:vertx-core' 9 | compile 'org.logl:logl-api' 10 | 11 | testCompile project(':junit') 12 | 13 | testCompile 'org.junit.jupiter:junit-jupiter-api' 14 | testCompile 'org.junit.jupiter:junit-jupiter-params' 15 | 16 | testCompile 'org.logl:logl-logl' 17 | 18 | testRuntime 'org.junit.jupiter:junit-jupiter-engine' 19 | } 20 | -------------------------------------------------------------------------------- /scuttlebutt-discovery/src/main/java/net/consensys/cava/scuttlebutt/discovery/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Scuttlebutt discovery service. 3 | * 4 | * This library implements the local discovery service of the Secure Scuttlebutt protocol as defined in 5 | * the Scuttlebutt protocol guide. 6 | * 7 | *

8 | * These classes are included in the standard Cava distribution, or separately when using the gradle dependency 9 | * 'net.consensys.cava:cava-scuttlebutt-discovery' (cava-scuttlebutt-discovery.jar). 10 | */ 11 | @ParametersAreNonnullByDefault 12 | package net.consensys.cava.scuttlebutt.discovery; 13 | 14 | import javax.annotation.ParametersAreNonnullByDefault; 15 | -------------------------------------------------------------------------------- /scuttlebutt-handshake/build.gradle: -------------------------------------------------------------------------------- 1 | description = 'Scuttlebutt Handshake library' 2 | 3 | dependencies { 4 | compile project(':bytes') 5 | compile project(':concurrent') 6 | compile project(':crypto') 7 | compile project(':scuttlebutt') 8 | compileOnly 'io.vertx:vertx-core' 9 | compile 'org.logl:logl-api' 10 | 11 | testCompile project(':junit') 12 | 13 | testCompile 'org.bouncycastle:bcprov-jdk15on' 14 | testCompile 'org.junit.jupiter:junit-jupiter-api' 15 | testCompile 'org.junit.jupiter:junit-jupiter-params' 16 | 17 | testCompile 'io.vertx:vertx-core' 18 | testCompile 'org.logl:logl-logl' 19 | testCompile 'org.logl:logl-vertx' 20 | 21 | testCompile project(':scuttlebutt-rpc') 22 | 23 | testRuntime 'org.junit.jupiter:junit-jupiter-engine' 24 | } 25 | -------------------------------------------------------------------------------- /scuttlebutt-handshake/src/main/java/net/consensys/cava/scuttlebutt/handshake/HandshakeException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.scuttlebutt.handshake; 14 | 15 | /** 16 | * Exceptions thrown during handshake because of invalid messages or different network identifiers. 17 | */ 18 | public final class HandshakeException extends RuntimeException { 19 | 20 | HandshakeException(String message) { 21 | super(message); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /scuttlebutt-handshake/src/main/java/net/consensys/cava/scuttlebutt/handshake/SecureScuttlebuttStreamClient.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.scuttlebutt.handshake; 14 | 15 | import net.consensys.cava.bytes.Bytes; 16 | 17 | /** 18 | * Interface used to encrypt and decrypt messages to and from a server. 19 | */ 20 | public interface SecureScuttlebuttStreamClient { 21 | 22 | /** 23 | * Prepares a message to be sent to the server 24 | * 25 | * @param message the message to encrypt and format 26 | * @return the message, encrypted and ready to send 27 | */ 28 | Bytes sendToServer(Bytes message); 29 | 30 | /** 31 | * Prepares a goodbye message to be sent to the server 32 | * 33 | * @return the goodbye message 34 | */ 35 | Bytes sendGoodbyeToServer(); 36 | 37 | /** 38 | * Adds message bytes to the reader stream, returning the bytes that could be decrypted. 39 | * 40 | * @param message the message to decrypt 41 | * @return the message, decrypted and ready for consumption, or null if the message provided were an incomplete 42 | * message. 43 | * @throws StreamException if the message cannot be decrypted 44 | */ 45 | Bytes readFromServer(Bytes message); 46 | } 47 | -------------------------------------------------------------------------------- /scuttlebutt-handshake/src/main/java/net/consensys/cava/scuttlebutt/handshake/StreamException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.scuttlebutt.handshake; 14 | 15 | public final class StreamException extends RuntimeException { 16 | 17 | StreamException(String message) { 18 | super(message); 19 | } 20 | 21 | StreamException(Throwable ex) { 22 | super(ex); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /scuttlebutt-handshake/src/main/java/net/consensys/cava/scuttlebutt/handshake/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Scuttlebutt handshake utilities. 3 | * 4 | * This library holds core constructs to help represent Scuttlebutt identities as described in 5 | * the Scuttlebutt protocol guide. 6 | * 7 | *

8 | * These classes are included in the standard Cava distribution, or separately when using the gradle dependency 9 | * 'net.consensys.cava:cava-scuttlebutt-handshake' (cava-scuttlebutt-handshake.jar). 10 | */ 11 | @ParametersAreNonnullByDefault 12 | package net.consensys.cava.scuttlebutt.handshake; 13 | 14 | import javax.annotation.ParametersAreNonnullByDefault; 15 | -------------------------------------------------------------------------------- /scuttlebutt-handshake/src/main/java/net/consensys/cava/scuttlebutt/handshake/vertx/ClientHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.scuttlebutt.handshake.vertx; 14 | 15 | import net.consensys.cava.bytes.Bytes; 16 | 17 | /** 18 | * Handler managing a stream over SecureScuttlebutt originating from the Vert.x client 19 | */ 20 | public interface ClientHandler { 21 | 22 | /** 23 | * This method is called by the client when a new message arrives from the server. 24 | * 25 | * @param message the new message after box decoding. 26 | */ 27 | void receivedMessage(Bytes message); 28 | 29 | /** 30 | * Notifies the handler the stream is no longer active. 31 | */ 32 | void streamClosed(); 33 | } 34 | -------------------------------------------------------------------------------- /scuttlebutt-handshake/src/main/java/net/consensys/cava/scuttlebutt/handshake/vertx/ClientHandlerFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.scuttlebutt.handshake.vertx; 14 | 15 | import net.consensys.cava.bytes.Bytes; 16 | 17 | import java.util.function.Consumer; 18 | 19 | /** 20 | * Factory creating stream handlers, managing client-side connections. 21 | */ 22 | public interface ClientHandlerFactory { 23 | 24 | /** 25 | * Creates a new handler associated with a valid streaming connection. 26 | * 27 | * @param sender the function to send bytes to the server 28 | * @param terminationFunction a function to terminate the stream properly 29 | */ 30 | T createHandler(Consumer sender, Runnable terminationFunction); 31 | } 32 | -------------------------------------------------------------------------------- /scuttlebutt-handshake/src/main/java/net/consensys/cava/scuttlebutt/handshake/vertx/ServerHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.scuttlebutt.handshake.vertx; 14 | 15 | import net.consensys.cava.bytes.Bytes; 16 | 17 | /** 18 | * Handler managing a stream over SecureScuttlebutt originating from the Vert.x server 19 | */ 20 | public interface ServerHandler { 21 | 22 | /** 23 | * This method is called by the server when a new message arrives from the client. 24 | * 25 | * @param message the new message after box decoding. 26 | */ 27 | void receivedMessage(Bytes message); 28 | 29 | /** 30 | * Notifies the handler the stream is no longer active. 31 | */ 32 | void streamClosed(); 33 | } 34 | -------------------------------------------------------------------------------- /scuttlebutt-handshake/src/main/java/net/consensys/cava/scuttlebutt/handshake/vertx/ServerHandlerFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.scuttlebutt.handshake.vertx; 14 | 15 | import net.consensys.cava.bytes.Bytes; 16 | 17 | import java.util.function.Consumer; 18 | 19 | /** 20 | * Factory creating stream handlers, managing server-side connections. 21 | */ 22 | public interface ServerHandlerFactory { 23 | 24 | /** 25 | * Creates a new handler associated with a valid streaming connection. 26 | * 27 | * @param sender the function to send bytes to the client 28 | * @param terminationFunction a function to terminate the stream properly 29 | */ 30 | ServerHandler createHandler(Consumer sender, Runnable terminationFunction); 31 | } 32 | -------------------------------------------------------------------------------- /scuttlebutt-handshake/src/main/java/net/consensys/cava/scuttlebutt/handshake/vertx/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Scuttlebutt Vert.x server to contact for handshaking. 3 | * 4 | * This library holds core constructs to help represent Scuttlebutt identities as described in 5 | * the Scuttlebutt protocol guide. 6 | * 7 | *

8 | * These classes are included in the standard Cava distribution, or separately when using the gradle dependency 9 | * 'net.consensys.cava:cava-scuttlebutt-handshake' (cava-scuttlebutt-handshake.jar). 10 | */ 11 | @ParametersAreNonnullByDefault 12 | package net.consensys.cava.scuttlebutt.handshake.vertx; 13 | 14 | import javax.annotation.ParametersAreNonnullByDefault; 15 | -------------------------------------------------------------------------------- /scuttlebutt-rpc/build.gradle: -------------------------------------------------------------------------------- 1 | description = 'Scuttlebutt Handshake library' 2 | 3 | dependencies { 4 | compile project(':bytes') 5 | compile project(':concurrent') 6 | compile project(':crypto') 7 | compileOnly 'io.vertx:vertx-core' 8 | compile project(':scuttlebutt') 9 | compile project(':scuttlebutt-handshake') 10 | compile 'org.logl:logl-api' 11 | 12 | compile 'com.fasterxml.jackson.core:jackson-databind' 13 | 14 | testCompile project(':junit') 15 | testCompile project(':scuttlebutt-handshake') 16 | 17 | testCompile 'org.bouncycastle:bcprov-jdk15on' 18 | testCompile 'org.junit.jupiter:junit-jupiter-api' 19 | testCompile 'org.junit.jupiter:junit-jupiter-params' 20 | testCompile 'io.vertx:vertx-core' 21 | 22 | testCompile 'org.logl:logl-logl' 23 | testCompile 'org.logl:logl-vertx' 24 | 25 | testRuntime 'org.junit.jupiter:junit-jupiter-engine' 26 | } 27 | -------------------------------------------------------------------------------- /scuttlebutt-rpc/src/main/java/net/consensys/cava/scuttlebutt/rpc/RPCErrorBody.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.scuttlebutt.rpc; 14 | 15 | /** 16 | * An RPC message response body which contains an error 17 | */ 18 | public class RPCErrorBody { 19 | 20 | private String name; 21 | private String message; 22 | private String stack; 23 | 24 | public RPCErrorBody() { 25 | 26 | } 27 | 28 | /** 29 | * A description of an error that occurred while performing an RPC request. 30 | * 31 | * @param name the name of the error type 32 | * @param message the message describing the error 33 | * @param stack the stack trace from the error 34 | */ 35 | public RPCErrorBody(String name, String message, String stack) { 36 | this.name = name; 37 | this.message = message; 38 | this.stack = stack; 39 | } 40 | 41 | public String getName() { 42 | return name; 43 | } 44 | 45 | public void setName(String name) { 46 | this.name = name; 47 | } 48 | 49 | public String getMessage() { 50 | return message; 51 | } 52 | 53 | public void setMessage(String message) { 54 | this.message = message; 55 | } 56 | 57 | public String getStack() { 58 | return stack; 59 | } 60 | 61 | public void setStack(String stack) { 62 | this.stack = stack; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /scuttlebutt-rpc/src/main/java/net/consensys/cava/scuttlebutt/rpc/RPCFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.scuttlebutt.rpc; 14 | 15 | import java.util.ArrayList; 16 | import java.util.List; 17 | 18 | /** 19 | * A scuttlebutt RPC function namespace and name representation. 20 | */ 21 | public class RPCFunction { 22 | 23 | private final List namespace; 24 | private final String functionName; 25 | 26 | /** 27 | * 28 | * @param namespace the namespace of the function (e.g. ['blobs']. May be empty if there is no namespace for the 29 | * function. 30 | * @param functionName the function (e.g. 'add'.) 31 | */ 32 | public RPCFunction(List namespace, String functionName) { 33 | this.namespace = namespace; 34 | this.functionName = functionName; 35 | } 36 | 37 | public RPCFunction(String functionName) { 38 | this.namespace = new ArrayList<>(); 39 | this.functionName = functionName; 40 | } 41 | 42 | /** 43 | * @return The list representation of the namespace and function call. 44 | */ 45 | public List asList() { 46 | List list = new ArrayList<>(); 47 | list.addAll(namespace); 48 | list.add(functionName); 49 | return list; 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /scuttlebutt-rpc/src/main/java/net/consensys/cava/scuttlebutt/rpc/RPCRequestType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.scuttlebutt.rpc; 14 | 15 | import com.fasterxml.jackson.annotation.JsonProperty; 16 | 17 | /** 18 | * The available type of Scuttlebutt RPC requests 19 | */ 20 | public enum RPCRequestType { 21 | 22 | /** 23 | * An 'async' request, which returns one result some time in the future. 24 | */ 25 | @JsonProperty("async") 26 | ASYNC, 27 | 28 | /** 29 | * A 'source' type request, which begins a stream of results 30 | */ 31 | @JsonProperty("source") 32 | SOURCE 33 | 34 | 35 | } 36 | -------------------------------------------------------------------------------- /scuttlebutt-rpc/src/main/java/net/consensys/cava/scuttlebutt/rpc/mux/ScuttlebuttStreamHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.scuttlebutt.rpc.mux; 14 | 15 | import net.consensys.cava.scuttlebutt.rpc.RPCResponse; 16 | 17 | /** 18 | * Handles incoming items from a result stream 19 | */ 20 | public interface ScuttlebuttStreamHandler { 21 | 22 | /** 23 | * Handles a new message from the result stream. 24 | * 25 | * @param message 26 | */ 27 | void onMessage(RPCResponse message); 28 | 29 | /** 30 | * Invoked when the stream has been closed. 31 | */ 32 | void onStreamEnd(); 33 | 34 | /** 35 | * Invoked when there is an error in the stream. 36 | * 37 | * @param ex the underlying error 38 | */ 39 | void onStreamError(Exception ex); 40 | 41 | } 42 | -------------------------------------------------------------------------------- /scuttlebutt-rpc/src/main/java/net/consensys/cava/scuttlebutt/rpc/mux/exceptions/ConnectionClosedException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.scuttlebutt.rpc.mux.exceptions; 14 | 15 | public class ConnectionClosedException extends Exception { 16 | 17 | public ConnectionClosedException() { 18 | super("Connection is closed."); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /scuttlebutt-rpc/src/main/java/net/consensys/cava/scuttlebutt/rpc/mux/exceptions/RPCRequestFailedException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.scuttlebutt.rpc.mux.exceptions; 14 | 15 | public final class RPCRequestFailedException extends RuntimeException { 16 | 17 | public RPCRequestFailedException(String errorMessage) { 18 | super(errorMessage); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /scuttlebutt/build.gradle: -------------------------------------------------------------------------------- 1 | description = 'Scuttlebutt Core library' 2 | 3 | dependencies { 4 | compile project(':bytes') 5 | compile project(':crypto') 6 | 7 | testCompile project(':junit') 8 | 9 | testCompile 'org.bouncycastle:bcprov-jdk15on' 10 | testCompile 'org.junit.jupiter:junit-jupiter-api' 11 | testCompile 'org.junit.jupiter:junit-jupiter-params' 12 | 13 | testRuntime 'org.junit.jupiter:junit-jupiter-engine' 14 | } 15 | -------------------------------------------------------------------------------- /scuttlebutt/src/main/java/net/consensys/cava/scuttlebutt/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Scuttlebutt core utilities. 3 | * 4 | * This library holds core constructs to help represent Scuttlebutt identities as described in 5 | * the Scuttlebutt protocol guide. 6 | * 7 | *

8 | * These classes are included in the standard Cava distribution, or separately when using the gradle dependency 9 | * 'net.consensys.cava:cava-scuttlebutt' (cava-scuttlebutt.jar). 10 | */ 11 | @ParametersAreNonnullByDefault 12 | package net.consensys.cava.scuttlebutt; 13 | 14 | import javax.annotation.ParametersAreNonnullByDefault; 15 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name='cava' 2 | include 'bytes' 3 | include 'concurrent' 4 | include 'concurrent-coroutines' 5 | include 'config' 6 | include 'crypto' 7 | include 'devp2p' 8 | include 'eth' 9 | include 'eth-reference-tests' 10 | include 'eth-repository' 11 | include 'io' 12 | include 'junit' 13 | include 'kademlia' 14 | include 'kv' 15 | include 'les' 16 | include 'merkle-trie' 17 | include 'net' 18 | include 'net-coroutines' 19 | include 'plumtree' 20 | include 'rlp' 21 | include 'rlpx' 22 | include 'scuttlebutt' 23 | include 'scuttlebutt-discovery' 24 | include 'scuttlebutt-handshake' 25 | include 'scuttlebutt-rpc' 26 | include 'ssz' 27 | include 'toml' 28 | include 'units' 29 | -------------------------------------------------------------------------------- /ssz/build.gradle: -------------------------------------------------------------------------------- 1 | description = 'Simple Serialize (SSZ) encoding and decoding.' 2 | 3 | dependencies { 4 | compile project(':bytes') 5 | compile project(':crypto') 6 | compileOnly project(':units') 7 | compile 'org.jetbrains.kotlin:kotlin-stdlib' 8 | 9 | testCompile project(':units') 10 | testCompile project(':junit') 11 | 12 | testCompile 'org.bouncycastle:bcprov-jdk15on' 13 | testCompile 'org.junit.jupiter:junit-jupiter-api' 14 | testCompile 'org.junit.jupiter:junit-jupiter-params' 15 | 16 | testRuntime 'org.junit.jupiter:junit-jupiter-engine' 17 | } 18 | -------------------------------------------------------------------------------- /ssz/src/main/java/net/consensys/cava/ssz/ByteBufferSSZWriter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.ssz; 14 | 15 | import net.consensys.cava.bytes.Bytes; 16 | 17 | import java.nio.ByteBuffer; 18 | 19 | final class ByteBufferSSZWriter implements SSZWriter { 20 | 21 | private ByteBuffer buffer; 22 | 23 | ByteBufferSSZWriter(ByteBuffer buffer) { 24 | this.buffer = buffer; 25 | } 26 | 27 | @Override 28 | public void writeSSZ(Bytes value) { 29 | buffer.put(value.toArrayUnsafe()); 30 | } 31 | 32 | @Override 33 | public void writeSSZ(byte[] value) { 34 | buffer.put(value); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /ssz/src/main/java/net/consensys/cava/ssz/BytesSSZWriter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.ssz; 14 | 15 | import net.consensys.cava.bytes.Bytes; 16 | 17 | import java.util.ArrayList; 18 | import java.util.List; 19 | 20 | final class BytesSSZWriter implements SSZWriter { 21 | 22 | private final List values = new ArrayList<>(); 23 | 24 | @Override 25 | public void writeSSZ(Bytes value) { 26 | values.add(value); 27 | } 28 | 29 | Bytes toBytes() { 30 | if (values.isEmpty()) { 31 | return Bytes.EMPTY; 32 | } 33 | return Bytes.wrap(values.toArray(new Bytes[0])); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /ssz/src/main/java/net/consensys/cava/ssz/EndOfSSZException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.ssz; 14 | 15 | /** 16 | * Indicates the end of the SSZ source has been reached unexpectedly. 17 | */ 18 | public class EndOfSSZException extends SSZException { 19 | public EndOfSSZException() { 20 | super("End of SSZ source reached"); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /ssz/src/main/java/net/consensys/cava/ssz/InvalidSSZTypeException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.ssz; 14 | 15 | /** 16 | * Indicates that an unexpected type was encountered when decoding SSZ. 17 | */ 18 | public class InvalidSSZTypeException extends SSZException { 19 | public InvalidSSZTypeException(String message) { 20 | super(message); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /ssz/src/main/java/net/consensys/cava/ssz/SSZException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.ssz; 14 | 15 | /** 16 | * Base type for all SSZ encoding and decoding exceptions. 17 | */ 18 | public class SSZException extends RuntimeException { 19 | public SSZException(String message) { 20 | super(message); 21 | } 22 | 23 | public SSZException(Throwable cause) { 24 | super(cause); 25 | } 26 | 27 | public SSZException(String message, Throwable cause) { 28 | super(message, cause); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /ssz/src/main/java/net/consensys/cava/ssz/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Simple Serialize (SSZ) encoding and decoding. 3 | *

4 | * An implementation of the Ethereum Simple Serialize (SSZ) algorithm, as described at https://github.com/ethereum/eth2.0-specs/blob/master/specs/simple-serialize.md. 6 | *

7 | * These classes are included in the standard Cava distribution, or separately when using the gradle dependency 8 | * 'net.consensys.cava:cava-ssz' (cava-ssz.jar). 9 | */ 10 | @ParametersAreNonnullByDefault 11 | package net.consensys.cava.ssz; 12 | 13 | import javax.annotation.ParametersAreNonnullByDefault; 14 | -------------------------------------------------------------------------------- /ssz/src/test/kotlin/net/consensys/cava/ssz/experimental/SSZTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.ssz.experimental 14 | 15 | import net.consensys.cava.bytes.Bytes.fromHexString 16 | import org.junit.jupiter.api.Assertions.assertEquals 17 | import org.junit.jupiter.api.Test 18 | 19 | @ExperimentalUnsignedTypes 20 | class SSZTest { 21 | 22 | @Test 23 | fun shouldEncodeUnsigned() { 24 | assertEquals(fromHexString("0000"), SSZ.encodeUInt16(0.toUInt())) 25 | assertEquals(fromHexString("00000000"), SSZ.encodeUInt32(0.toUInt())) 26 | assertEquals(fromHexString("0000000000000000"), SSZ.encodeUInt64(0L.toULong())) 27 | assertEquals(fromHexString("0000000000000000"), SSZ.encodeUInt64(0L.toULong())) 28 | 29 | assertEquals(fromHexString("FFFF"), SSZ.encodeUInt16(65535.toUInt())) 30 | assertEquals(fromHexString("FFFF0000"), SSZ.encodeUInt32(65535.toUInt())) 31 | } 32 | 33 | @Test 34 | fun shouldWriteUnsigned() { 35 | assertEquals(fromHexString("FFFF"), SSZ.encode { w -> w.writeUInt16(65535.toUInt()) }) 36 | assertEquals(fromHexString("FFFF0000"), SSZ.encode { w -> w.writeUInt32(65535.toUInt()) }) 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /toml/build.gradle: -------------------------------------------------------------------------------- 1 | description = 'A parser for Tom\'s Obvious, Minimal Language (TOML).' 2 | 3 | apply plugin: 'antlr' 4 | 5 | generateGrammarSource { 6 | outputDirectory file("${project.buildDir}/generated-src/antlr/main/net/consensys/cava/toml/internal") 7 | arguments << "-visitor" << "-long-messages" 8 | arguments << "-Xexact-output-dir" 9 | } 10 | 11 | javadoc { exclude '**/internal/**' } 12 | 13 | configurations { 14 | compile { 15 | extendsFrom = extendsFrom.findAll { it != configurations.antlr } 16 | } 17 | } 18 | 19 | dependencies { 20 | antlr 'org.antlr:antlr4' 21 | 22 | compile 'org.antlr:antlr4-runtime' 23 | compile 'com.google.code.findbugs:jsr305' 24 | 25 | testCompile 'org.junit.jupiter:junit-jupiter-api' 26 | testCompile 'org.junit.jupiter:junit-jupiter-params' 27 | 28 | testRuntime 'org.junit.jupiter:junit-jupiter-engine' 29 | } 30 | -------------------------------------------------------------------------------- /toml/src/main/java/net/consensys/cava/toml/ErrorReporter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.toml; 14 | 15 | interface ErrorReporter { 16 | void reportError(TomlParseError error); 17 | } 18 | -------------------------------------------------------------------------------- /toml/src/main/java/net/consensys/cava/toml/KeyVisitor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.toml; 14 | 15 | import net.consensys.cava.toml.internal.TomlParser.QuotedKeyContext; 16 | import net.consensys.cava.toml.internal.TomlParser.UnquotedKeyContext; 17 | import net.consensys.cava.toml.internal.TomlParserBaseVisitor; 18 | 19 | import java.util.ArrayList; 20 | import java.util.List; 21 | 22 | final class KeyVisitor extends TomlParserBaseVisitor> { 23 | 24 | private final List keys = new ArrayList<>(); 25 | 26 | @Override 27 | public List visitUnquotedKey(UnquotedKeyContext ctx) { 28 | keys.add(ctx.getText()); 29 | return keys; 30 | } 31 | 32 | @Override 33 | public List visitQuotedKey(QuotedKeyContext ctx) { 34 | StringBuilder builder = ctx.accept(new QuotedStringVisitor()); 35 | keys.add(builder.toString()); 36 | return keys; 37 | } 38 | 39 | @Override 40 | protected List aggregateResult(List aggregate, List nextResult) { 41 | return aggregate == null ? null : nextResult; 42 | } 43 | 44 | @Override 45 | protected List defaultResult() { 46 | return keys; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /toml/src/main/java/net/consensys/cava/toml/TomlInvalidTypeException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.toml; 14 | 15 | /** 16 | * An exception thrown when an invalid type is encountered. 17 | */ 18 | public class TomlInvalidTypeException extends RuntimeException { 19 | 20 | TomlInvalidTypeException(String message) { 21 | super(message); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /toml/src/main/java/net/consensys/cava/toml/TomlParseError.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.toml; 14 | 15 | /** 16 | * An error that occurred while parsing. 17 | */ 18 | public final class TomlParseError extends RuntimeException { 19 | 20 | private final TomlPosition position; 21 | 22 | TomlParseError(String message, TomlPosition position) { 23 | super(message); 24 | this.position = position; 25 | } 26 | 27 | TomlParseError(String message, TomlPosition position, Throwable cause) { 28 | super(message, cause); 29 | this.position = position; 30 | } 31 | 32 | /** 33 | * @return The position in the input where the error occurred. 34 | */ 35 | public TomlPosition position() { 36 | return position; 37 | } 38 | 39 | @Override 40 | public String toString() { 41 | return getMessage() + " (" + position + ")"; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /toml/src/main/java/net/consensys/cava/toml/TomlParseResult.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.toml; 14 | 15 | import java.util.List; 16 | 17 | /** 18 | * The result from parsing a TOML document. 19 | */ 20 | public interface TomlParseResult extends TomlTable { 21 | 22 | /** 23 | * @return {@code true} if the TOML document contained errors. 24 | */ 25 | default boolean hasErrors() { 26 | return !(errors().isEmpty()); 27 | } 28 | 29 | /** 30 | * The errors that occurred during parsing. 31 | * 32 | * @return A list of errors. 33 | */ 34 | List errors(); 35 | } 36 | -------------------------------------------------------------------------------- /toml/src/main/java/net/consensys/cava/toml/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * A parser for Tom's Obvious, Minimal Language (TOML). 3 | *

4 | * A parser and semantic checker for Tom's Obvious, Minimal Language (TOML), as described at 5 | * https://github.com/toml-lang/toml/. 6 | *

7 | * These classes are included in the standard Cava distribution, or separately when using the gradle dependency 8 | * 'net.consensys.cava:cava-toml' (cava-toml.jar). 9 | */ 10 | @ParametersAreNonnullByDefault 11 | package net.consensys.cava.toml; 12 | 13 | import javax.annotation.ParametersAreNonnullByDefault; 14 | -------------------------------------------------------------------------------- /toml/src/test/java/net/consensys/cava/toml/TokenNameTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.toml; 14 | 15 | import static org.junit.jupiter.api.Assertions.assertTrue; 16 | 17 | import net.consensys.cava.toml.internal.TomlLexer; 18 | 19 | import java.util.ArrayList; 20 | import java.util.List; 21 | 22 | import org.junit.jupiter.api.Test; 23 | 24 | class TokenNameTest { 25 | 26 | @Test 27 | void shouldHaveTokenNameForAllTokens() { 28 | List missing = new ArrayList<>(); 29 | for (int i = 0; i < TomlLexer.VOCABULARY.getMaxTokenType(); ++i) { 30 | if (!TokenName.namesForToken(i).findFirst().isPresent()) { 31 | missing.add(TomlLexer.VOCABULARY.getSymbolicName(i)); 32 | } 33 | } 34 | assertTrue(missing.isEmpty(), () -> "No TokenName's for " + String.join(", ", missing)); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /toml/src/test/resources/net/consensys/cava/toml/hard_example.toml: -------------------------------------------------------------------------------- 1 | # Test file for TOML 2 | # Only this one tries to emulate a TOML file written by a user of the kind of parser writers probably hate 3 | # This part you'll really hate 4 | 5 | [the] 6 | test_string = "You'll hate me after this - #" # " Annoying, isn't it? 7 | 8 | [the.hard] 9 | test_array = [ "] ", " # "] # ] There you go, parse this! 10 | test_array2 = [ "Test #11 ]proved that", "Experiment #9 was a success" ] 11 | # You didn't think it'd as easy as chucking out the last #, did you? 12 | another_test_string = " Same thing, but with a string #" 13 | harder_test_string = " And when \"'s are in the string, along with # \"" # "and comments are there too" 14 | # Things will get harder 15 | 16 | [the.hard."bit#"] 17 | "what?" = "You don't think some user won't do that?" 18 | multi_line_array = [ 19 | "]", 20 | # ] Oh yes I did 21 | ] 22 | 23 | # Each of the following keygroups/key value pairs should produce an error. Uncomment to them to test 24 | 25 | #[error] if you didn't catch this, your parser is broken 26 | #string = "Anything other than tabs, spaces and newline after a keygroup or key value pair has ended should produce an error unless it is a comment" like this 27 | #array = [ 28 | # "This might most likely happen in multiline arrays", 29 | # Like here, 30 | # "or here, 31 | # and here" 32 | # ] End of array comment, forgot the # 33 | #number = 3.14 pi <--again forgot the # 34 | -------------------------------------------------------------------------------- /toml/src/test/resources/net/consensys/cava/toml/hard_example_unicode.toml: -------------------------------------------------------------------------------- 1 | # Tèƨƭ ƒïℓè ƒôř TÓM£ 2 | 3 | # Óñℓ¥ ƭλïƨ ôñè ƭřïèƨ ƭô è₥úℓáƭè á TÓM£ ƒïℓè ωřïƭƭèñ β¥ á úƨèř ôƒ ƭλè ƙïñδ ôƒ ƥářƨèř ωřïƭèřƨ ƥřôβáβℓ¥ λáƭè 4 | # Tλïƨ ƥářƭ ¥ôú'ℓℓ řèáℓℓ¥ λáƭè 5 | 6 | [the] 7 | test_string = "Ýôú'ℓℓ λáƭè ₥è áƒƭèř ƭλïƨ - #" # " Âññô¥ïñϱ, ïƨñ'ƭ ïƭ? 8 | 9 | 10 | [the.hard] 11 | test_array = [ "] ", " # "] # ] Tλèřè ¥ôú ϱô, ƥářƨè ƭλïƨ! 12 | test_array2 = [ "Tèƨƭ #11 ]ƥřôƲèδ ƭλáƭ", "Éжƥèřï₥èñƭ #9 ωáƨ á ƨúççèƨƨ" ] 13 | # Ýôú δïδñ'ƭ ƭλïñƙ ïƭ'δ áƨ èáƨ¥ áƨ çλúçƙïñϱ ôúƭ ƭλè ℓáƨƭ #, δïδ ¥ôú? 14 | another_test_string = "§á₥è ƭλïñϱ, βúƭ ωïƭλ á ƨƭřïñϱ #" 15 | harder_test_string = " Âñδ ωλèñ \"'ƨ ářè ïñ ƭλè ƨƭřïñϱ, áℓôñϱ ωïƭλ # \"" # "áñδ çô₥₥èñƭƨ ářè ƭλèřè ƭôô" 16 | # Tλïñϱƨ ωïℓℓ ϱèƭ λářδèř 17 | 18 | [the.hard."βïƭ#"] 19 | "ωλáƭ?" = "Ýôú δôñ'ƭ ƭλïñƙ ƨô₥è úƨèř ωôñ'ƭ δô ƭλáƭ?" 20 | multi_line_array = [ 21 | "]", 22 | # ] Óλ ¥èƨ Ì δïδ 23 | ] 24 | 25 | # Each of the following keygroups/key value pairs should produce an error. Uncomment to them to test 26 | 27 | #[error] ïƒ ¥ôú δïδñ'ƭ çáƭçλ ƭλïƨ, ¥ôúř ƥářƨèř ïƨ βřôƙèñ 28 | #string = "Âñ¥ƭλïñϱ ôƭλèř ƭλáñ ƭáβƨ, ƨƥáçèƨ áñδ ñèωℓïñè áƒƭèř á ƙè¥ϱřôúƥ ôř ƙè¥ Ʋáℓúè ƥáïř λáƨ èñδèδ ƨλôúℓδ ƥřôδúçè áñ èřřôř úñℓèƨƨ ïƭ ïƨ á çô₥₥èñƭ" ℓïƙè ƭλïƨ 29 | 30 | #array = [ 31 | # "Tλïƨ ₥ïϱλƭ ₥ôƨƭ ℓïƙèℓ¥ λáƥƥèñ ïñ ₥úℓƭïℓïñè ářřá¥ƨ", 32 | # £ïƙè λèřè, 33 | # "ôř λèřè, 34 | # áñδ λèřè" 35 | # ] Éñδ ôƒ ářřᥠçô₥₥èñƭ, ƒôřϱôƭ ƭλè # 36 | #number = 3.14 ƥï <--áϱáïñ ƒôřϱôƭ ƭλè # -------------------------------------------------------------------------------- /toml/src/test/resources/net/consensys/cava/toml/toml-v0.5.0-spec-example.toml: -------------------------------------------------------------------------------- 1 | # This is a TOML document. 2 | 3 | title = "TOML Example" 4 | 5 | [owner] 6 | name = "Tom Preston-Werner" 7 | dob = 1979-05-27T07:32:00-08:00 # First class dates 8 | 9 | [database] 10 | server = "192.168.1.1" 11 | ports = [ 8001, 8001, 8002 ] 12 | connection_max = 5000 13 | enabled = true 14 | 15 | [servers] 16 | 17 | # Indentation (tabs and/or spaces) is allowed but not required 18 | [servers.alpha] 19 | ip = "10.0.0.1" 20 | dc = "eqdc10" 21 | 22 | [servers.beta] 23 | ip = "10.0.0.2" 24 | dc = "eqdc10" 25 | 26 | [clients] 27 | data = [ ["gamma", "delta"], [1, 2] ] 28 | 29 | # Line breaks are OK when inside arrays 30 | hosts = [ 31 | "alpha", 32 | "omega" 33 | ] 34 | -------------------------------------------------------------------------------- /units/build.gradle: -------------------------------------------------------------------------------- 1 | description = 'Classes and utilities for working with 256 bit integers.' 2 | 3 | dependencies { 4 | compile project(':bytes') 5 | compile 'com.google.guava:guava' 6 | 7 | testCompile 'org.junit.jupiter:junit-jupiter-api' 8 | testCompile 'org.junit.jupiter:junit-jupiter-params' 9 | 10 | testRuntime 'org.junit.jupiter:junit-jupiter-engine' 11 | } 12 | -------------------------------------------------------------------------------- /units/src/main/java/net/consensys/cava/units/bigints/UInt256Domain.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.units.bigints; 14 | 15 | import com.google.common.collect.DiscreteDomain; 16 | 17 | /** 18 | * A {@link DiscreteDomain} over {@link UInt256}. 19 | */ 20 | public final class UInt256Domain extends DiscreteDomain { 21 | 22 | @Override 23 | public UInt256 next(UInt256 value) { 24 | return value.add(1); 25 | } 26 | 27 | @Override 28 | public UInt256 previous(UInt256 value) { 29 | return value.subtract(1); 30 | } 31 | 32 | @Override 33 | public long distance(UInt256 start, UInt256 end) { 34 | boolean negativeDistance = start.compareTo(end) < 0; 35 | UInt256 distance = negativeDistance ? end.subtract(start) : start.subtract(end); 36 | if (!distance.fitsLong()) { 37 | return negativeDistance ? Long.MIN_VALUE : Long.MAX_VALUE; 38 | } 39 | long distanceLong = distance.toLong(); 40 | return negativeDistance ? -distanceLong : distanceLong; 41 | } 42 | 43 | @Override 44 | public UInt256 minValue() { 45 | return UInt256.MIN_VALUE; 46 | } 47 | 48 | @Override 49 | public UInt256 maxValue() { 50 | return UInt256.MAX_VALUE; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /units/src/main/java/net/consensys/cava/units/bigints/UInt256s.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.units.bigints; 14 | 15 | /** Static utility methods on UInt256 values. */ 16 | public final class UInt256s { 17 | private UInt256s() {} 18 | 19 | /** 20 | * Returns the maximum of two UInt256 values. 21 | * 22 | * @param v1 The first value. 23 | * @param v2 The second value. 24 | * @return The maximum of {@code v1} and {@code v2}. 25 | * @param The concrete type of the two values. 26 | */ 27 | public static > T max(T v1, T v2) { 28 | return (v1.compareTo(v2)) >= 0 ? v1 : v2; 29 | } 30 | 31 | /** 32 | * Returns the minimum of two UInt256 values. 33 | * 34 | * @param v1 The first value. 35 | * @param v2 The second value. 36 | * @return The minimum of {@code v1} and {@code v2}. 37 | * @param The concrete type of the two values. 38 | */ 39 | public static > T min(T v1, T v2) { 40 | return (v1.compareTo(v2)) < 0 ? v1 : v2; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /units/src/main/java/net/consensys/cava/units/bigints/UInt384Domain.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.units.bigints; 14 | 15 | import com.google.common.collect.DiscreteDomain; 16 | 17 | /** 18 | * A {@link DiscreteDomain} over {@link UInt384}. 19 | */ 20 | public final class UInt384Domain extends DiscreteDomain { 21 | 22 | @Override 23 | public UInt384 next(UInt384 value) { 24 | return value.add(1); 25 | } 26 | 27 | @Override 28 | public UInt384 previous(UInt384 value) { 29 | return value.subtract(1); 30 | } 31 | 32 | @Override 33 | public long distance(UInt384 start, UInt384 end) { 34 | boolean negativeDistance = start.compareTo(end) < 0; 35 | UInt384 distance = negativeDistance ? end.subtract(start) : start.subtract(end); 36 | if (!distance.fitsLong()) { 37 | return negativeDistance ? Long.MIN_VALUE : Long.MAX_VALUE; 38 | } 39 | long distanceLong = distance.toLong(); 40 | return negativeDistance ? -distanceLong : distanceLong; 41 | } 42 | 43 | @Override 44 | public UInt384 minValue() { 45 | return UInt384.MIN_VALUE; 46 | } 47 | 48 | @Override 49 | public UInt384 maxValue() { 50 | return UInt384.MAX_VALUE; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /units/src/main/java/net/consensys/cava/units/bigints/UInt384s.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.units.bigints; 14 | 15 | /** Static utility methods on UInt384 values. */ 16 | public final class UInt384s { 17 | private UInt384s() {} 18 | 19 | /** 20 | * Returns the maximum of two UInt384 values. 21 | * 22 | * @param v1 The first value. 23 | * @param v2 The second value. 24 | * @return The maximum of {@code v1} and {@code v2}. 25 | * @param The concrete type of the two values. 26 | */ 27 | public static > T max(T v1, T v2) { 28 | return (v1.compareTo(v2)) >= 0 ? v1 : v2; 29 | } 30 | 31 | /** 32 | * Returns the minimum of two UInt384 values. 33 | * 34 | * @param v1 The first value. 35 | * @param v2 The second value. 36 | * @return The minimum of {@code v1} and {@code v2}. 37 | * @param The concrete type of the two values. 38 | */ 39 | public static > T min(T v1, T v2) { 40 | return (v1.compareTo(v2)) < 0 ? v1 : v2; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /units/src/main/java/net/consensys/cava/units/bigints/UInt64Domain.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.units.bigints; 14 | 15 | import com.google.common.collect.DiscreteDomain; 16 | 17 | /** 18 | * A {@link DiscreteDomain} over {@link UInt64}. 19 | */ 20 | public final class UInt64Domain extends DiscreteDomain { 21 | 22 | @Override 23 | public UInt64 next(UInt64 value) { 24 | return value.add(1); 25 | } 26 | 27 | @Override 28 | public UInt64 previous(UInt64 value) { 29 | return value.subtract(1); 30 | } 31 | 32 | @Override 33 | public long distance(UInt64 start, UInt64 end) { 34 | boolean negativeDistance = start.compareTo(end) < 0; 35 | UInt64 distance = negativeDistance ? end.subtract(start) : start.subtract(end); 36 | if (!distance.fitsLong()) { 37 | return negativeDistance ? Long.MIN_VALUE : Long.MAX_VALUE; 38 | } 39 | long distanceLong = distance.toLong(); 40 | return negativeDistance ? -distanceLong : distanceLong; 41 | } 42 | 43 | @Override 44 | public UInt64 minValue() { 45 | return UInt64.MIN_VALUE; 46 | } 47 | 48 | @Override 49 | public UInt64 maxValue() { 50 | return UInt64.MAX_VALUE; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /units/src/main/java/net/consensys/cava/units/bigints/UInt64s.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018 ConsenSys AG. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 5 | * the License. You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 11 | * specific language governing permissions and limitations under the License. 12 | */ 13 | package net.consensys.cava.units.bigints; 14 | 15 | /** Static utility methods on UInt64 values. */ 16 | public final class UInt64s { 17 | private UInt64s() {} 18 | 19 | /** 20 | * Returns the maximum of two UInt64 values. 21 | * 22 | * @param v1 The first value. 23 | * @param v2 The second value. 24 | * @return The maximum of {@code v1} and {@code v2}. 25 | * @param The concrete type of the two values. 26 | */ 27 | public static > T max(T v1, T v2) { 28 | return (v1.compareTo(v2)) >= 0 ? v1 : v2; 29 | } 30 | 31 | /** 32 | * Returns the minimum of two UInt64 values. 33 | * 34 | * @param v1 The first value. 35 | * @param v2 The second value. 36 | * @return The minimum of {@code v1} and {@code v2}. 37 | * @param The concrete type of the two values. 38 | */ 39 | public static > T min(T v1, T v2) { 40 | return (v1.compareTo(v2)) < 0 ? v1 : v2; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /units/src/main/java/net/consensys/cava/units/bigints/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Classes and utilities for working with 256 bit integers. 3 | */ 4 | @ParametersAreNonnullByDefault 5 | package net.consensys.cava.units.bigints; 6 | 7 | import javax.annotation.ParametersAreNonnullByDefault; 8 | -------------------------------------------------------------------------------- /units/src/main/java/net/consensys/cava/units/ethereum/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Classes and utilities for working with Ethereum units. 3 | */ 4 | @ParametersAreNonnullByDefault 5 | package net.consensys.cava.units.ethereum; 6 | 7 | import javax.annotation.ParametersAreNonnullByDefault; 8 | -------------------------------------------------------------------------------- /units/src/main/java/net/consensys/cava/units/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Classes and utilities for working with 256 bit integers and Ethereum units. 3 | * 4 | *

5 | * These classes are included in the standard Cava distribution, or separately when using the gradle dependency 6 | * 'net.consensys.cava:cava-units' (cava-units.jar). 7 | */ 8 | package net.consensys.cava.units; 9 | --------------------------------------------------------------------------------