├── .github ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── ISSUE_TEMPLATE │ ├── bug_report.md │ └── feature_request.md └── PULL_REQUEST_TEMPLATE.md ├── .gitignore ├── .gitmodules ├── .swagger-codegen-ignore ├── .swagger-codegen └── VERSION ├── .travis.yml ├── CHANGELOG.md ├── Dockerfile ├── LICENSE ├── LICENSE_3RD ├── README.md ├── appveyor.yml ├── assets ├── multisig-owners.json └── semux-core-version.json ├── config ├── ipfilter.json ├── log4j2.xml └── semux.properties ├── crowdin.yml ├── docs ├── Block-Explorers.md ├── Bug-Bounty-Program.md ├── Configurations.md ├── Console.md ├── Delegate-Pools.md ├── Delegates-and-Validators.md ├── Devnet.md ├── Full-Node-Setup.md ├── IDE-Setup.md ├── Message-Encoding.md ├── Multi-Sig-Wallet.md ├── Network-Protocol.md ├── README.md ├── RESTful-API.md ├── Semux-BFT-Consensus.md ├── Semux-Virtual-Machine.md ├── Supply-and-Block-Rewards.md ├── Testnet.md ├── Versioning.md └── Virtual-Machine-Contracts.md ├── misc ├── design │ ├── splash.bmp │ └── splash.kra ├── formatter_eclipse.xml ├── formatter_intellij.xml ├── launch4j │ └── semux.l4j.ini ├── license_header.txt └── systemd │ └── semux.service ├── pom.xml ├── scripts ├── release.sh ├── semux-cli.bat ├── semux-cli.sh ├── semux-gui.bat └── semux-gui.sh └── src ├── main ├── java │ └── org │ │ └── semux │ │ ├── JvmOptions.java │ │ ├── Kernel.java │ │ ├── Launcher.java │ │ ├── Main.java │ │ ├── Network.java │ │ ├── api │ │ ├── ApiHandler.java │ │ ├── ApiHandlerImpl.java │ │ ├── ApiVersion.java │ │ ├── SemuxApiService.java │ │ ├── http │ │ │ ├── HttpChannelInitializer.java │ │ │ ├── HttpConstants.java │ │ │ └── HttpHandler.java │ │ ├── util │ │ │ └── TransactionBuilder.java │ │ └── v2 │ │ │ ├── SemuxApiImpl.java │ │ │ ├── TypeFactory.java │ │ │ ├── client │ │ │ └── SemuxApi.java │ │ │ └── server │ │ │ └── SemuxApi.java │ │ ├── cli │ │ ├── DatabaseScanner.java │ │ ├── SemuxCli.java │ │ └── SemuxOption.java │ │ ├── config │ │ ├── AbstractConfig.java │ │ ├── ChainSpec.java │ │ ├── Config.java │ │ ├── Constants.java │ │ ├── DevnetConfig.java │ │ ├── MainnetConfig.java │ │ ├── TestnetConfig.java │ │ └── exception │ │ │ └── ConfigException.java │ │ ├── consensus │ │ ├── Proof.java │ │ ├── Proposal.java │ │ ├── SemuxBft.java │ │ ├── SemuxSync.java │ │ ├── Vote.java │ │ ├── VoteSet.java │ │ ├── VoteType.java │ │ └── exception │ │ │ └── SemuxBftException.java │ │ ├── core │ │ ├── ActivatedForks.java │ │ ├── Amount.java │ │ ├── BftManager.java │ │ ├── Block.java │ │ ├── BlockHeader.java │ │ ├── BlockHeaderData.java │ │ ├── BlockPart.java │ │ ├── Blockchain.java │ │ ├── BlockchainImpl.java │ │ ├── BlockchainListener.java │ │ ├── Fork.java │ │ ├── ForkSignalSet.java │ │ ├── Genesis.java │ │ ├── PendingManager.java │ │ ├── SyncManager.java │ │ ├── Transaction.java │ │ ├── TransactionExecutor.java │ │ ├── TransactionResult.java │ │ ├── TransactionType.java │ │ ├── Unit.java │ │ ├── Wallet.java │ │ ├── event │ │ │ ├── BlockchainDatabaseUpgradingEvent.java │ │ │ └── WalletLoadingEvent.java │ │ ├── exception │ │ │ ├── BlockchainException.java │ │ │ └── WalletLockedException.java │ │ └── state │ │ │ ├── Account.java │ │ │ ├── AccountState.java │ │ │ ├── AccountStateImpl.java │ │ │ ├── Delegate.java │ │ │ ├── DelegateState.java │ │ │ └── DelegateStateImpl.java │ │ ├── crypto │ │ ├── Aes.java │ │ ├── CryptoException.java │ │ ├── Hash.java │ │ ├── Hex.java │ │ ├── Key.java │ │ ├── Native.java │ │ ├── bip32 │ │ │ ├── CoinType.java │ │ │ ├── HdKeyGenerator.java │ │ │ ├── HdKeyPair.java │ │ │ ├── Scheme.java │ │ │ ├── key │ │ │ │ ├── HdKey.java │ │ │ │ ├── HdPrivateKey.java │ │ │ │ ├── HdPublicKey.java │ │ │ │ └── KeyVersion.java │ │ │ └── util │ │ │ │ ├── BitUtil.java │ │ │ │ ├── BytesUtil.java │ │ │ │ ├── HashUtil.java │ │ │ │ ├── Hmac.java │ │ │ │ └── Secp256k1.java │ │ ├── bip39 │ │ │ ├── Dictionary.java │ │ │ ├── Language.java │ │ │ └── MnemonicGenerator.java │ │ ├── bip44 │ │ │ └── Bip44.java │ │ └── cache │ │ │ └── PublicKeyCache.java │ │ ├── db │ │ ├── Database.java │ │ ├── DatabaseFactory.java │ │ ├── DatabaseName.java │ │ ├── LeveldbDatabase.java │ │ └── exception │ │ │ └── DatabaseException.java │ │ ├── event │ │ ├── KernelBootingEvent.java │ │ ├── PubSub.java │ │ ├── PubSubEvent.java │ │ ├── PubSubFactory.java │ │ └── PubSubSubscriber.java │ │ ├── exception │ │ └── LauncherException.java │ │ ├── gui │ │ ├── Action.java │ │ ├── AddressBookEntry.java │ │ ├── ComboBoxItem.java │ │ ├── HorizontalSeparator.java │ │ ├── MainFrame.java │ │ ├── MenuBar.java │ │ ├── PlaceHolder.java │ │ ├── SemuxGui.java │ │ ├── SplashScreen.java │ │ ├── StatusBar.java │ │ ├── SwingUtil.java │ │ ├── TextContextMenuItem.java │ │ ├── TextPrompt.java │ │ ├── TransactionSender.java │ │ ├── VerticalSeparator.java │ │ ├── WelcomeFrame.java │ │ ├── dialog │ │ │ ├── AddressBookDialog.java │ │ │ ├── AddressBookUpdateDialog.java │ │ │ ├── ChangePasswordDialog.java │ │ │ ├── ConsoleDialog.java │ │ │ ├── DelegateDialog.java │ │ │ ├── ExportPrivateKeyDialog.java │ │ │ ├── InitializeHdWalletDialog.java │ │ │ ├── InputDialog.java │ │ │ ├── SelectDialog.java │ │ │ ├── TransactionDialog.java │ │ │ └── TransactionResultDialog.java │ │ ├── event │ │ │ ├── MainFrameStartedEvent.java │ │ │ └── WalletSelectionDialogShownEvent.java │ │ ├── model │ │ │ ├── WalletAccount.java │ │ │ ├── WalletDelegate.java │ │ │ └── WalletModel.java │ │ └── panel │ │ │ ├── DelegatesPanel.java │ │ │ ├── HomePanel.java │ │ │ ├── ReceivePanel.java │ │ │ ├── SendPanel.java │ │ │ ├── TransactionsPanel.java │ │ │ └── TransactionsPanelFilter.java │ │ ├── log │ │ └── LoggerConfigurator.java │ │ ├── message │ │ ├── CliMessages.java │ │ ├── GuiMessages.java │ │ ├── MessageFormatter.java │ │ └── ResourceBundles.java │ │ ├── net │ │ ├── Capability.java │ │ ├── CapabilityTreeSet.java │ │ ├── Channel.java │ │ ├── ChannelManager.java │ │ ├── ConnectionLimitHandler.java │ │ ├── Frame.java │ │ ├── NodeManager.java │ │ ├── Peer.java │ │ ├── PeerClient.java │ │ ├── PeerServer.java │ │ ├── SemuxChannelInitializer.java │ │ ├── SemuxFrameHandler.java │ │ ├── SemuxMessageHandler.java │ │ ├── SemuxP2pHandler.java │ │ ├── filter │ │ │ ├── FilterRule.java │ │ │ ├── SemuxIpFilter.java │ │ │ ├── SingleIpFilterRule.java │ │ │ └── exception │ │ │ │ └── IpFilterJsonParseException.java │ │ └── msg │ │ │ ├── Message.java │ │ │ ├── MessageCode.java │ │ │ ├── MessageException.java │ │ │ ├── MessageFactory.java │ │ │ ├── MessageQueue.java │ │ │ ├── ReasonCode.java │ │ │ ├── consensus │ │ │ ├── BlockHeaderMessage.java │ │ │ ├── BlockMessage.java │ │ │ ├── BlockPartsMessage.java │ │ │ ├── GetBlockHeaderMessage.java │ │ │ ├── GetBlockMessage.java │ │ │ ├── GetBlockPartsMessage.java │ │ │ ├── NewHeightMessage.java │ │ │ ├── NewViewMessage.java │ │ │ ├── ProposalMessage.java │ │ │ └── VoteMessage.java │ │ │ └── p2p │ │ │ ├── DisconnectMessage.java │ │ │ ├── GetNodesMessage.java │ │ │ ├── NodesMessage.java │ │ │ ├── PingMessage.java │ │ │ ├── PongMessage.java │ │ │ ├── TransactionMessage.java │ │ │ └── handshake │ │ │ ├── v1 │ │ │ ├── HandshakeMessage.java │ │ │ ├── HelloMessage.java │ │ │ └── WorldMessage.java │ │ │ └── v2 │ │ │ ├── HandshakeMessage.java │ │ │ ├── HelloMessage.java │ │ │ ├── InitMessage.java │ │ │ └── WorldMessage.java │ │ ├── tools │ │ └── DatabaseIntegrityChecker.java │ │ ├── util │ │ ├── ArrayUtil.java │ │ ├── BasicAuth.java │ │ ├── BigIntegerUtil.java │ │ ├── ByteArray.java │ │ ├── Bytes.java │ │ ├── CircularFixedSizeList.java │ │ ├── ClosableIterator.java │ │ ├── CommandParser.java │ │ ├── ConsoleUtil.java │ │ ├── FileUtil.java │ │ ├── IOUtil.java │ │ ├── MerkleTree.java │ │ ├── MerkleUtil.java │ │ ├── NullPrintStream.java │ │ ├── SimpleApiClient.java │ │ ├── SimpleDecoder.java │ │ ├── SimpleEncoder.java │ │ ├── StringUtil.java │ │ ├── SystemUtil.java │ │ ├── TimeUtil.java │ │ └── exception │ │ │ ├── BytesException.java │ │ │ ├── SimpleCodecException.java │ │ │ └── UnreachableException.java │ │ └── vm │ │ └── client │ │ ├── Conversion.java │ │ ├── SemuxBlock.java │ │ ├── SemuxBlockStore.java │ │ ├── SemuxInternalTransaction.java │ │ ├── SemuxPrecompiledContracts.java │ │ ├── SemuxRepository.java │ │ ├── SemuxSpec.java │ │ └── SemuxTransaction.java ├── native │ ├── CMakeLists.txt │ ├── README.md │ ├── cmake │ │ ├── toolchain-Darwin-x86_64.cmake │ │ ├── toolchain-Linux-aarch64.cmake │ │ ├── toolchain-Linux-x86_64.cmake │ │ └── toolchain-Windows-x86_64.cmake │ └── crypto │ │ ├── CMakeLists.txt │ │ ├── jni │ │ ├── jni.h │ │ └── jni_md.h │ │ ├── org_semux_crypto_Native.cpp │ │ ├── org_semux_crypto_Native.h │ │ ├── ripemd160.c │ │ └── ripemd160.h └── resources │ ├── genesis │ ├── devnet.json │ ├── mainnet.json │ └── testnet.json │ ├── log4j2.xml │ ├── logo.icns │ ├── native │ ├── Darwin-x86_64 │ │ └── libsemuxcrypto.dylib │ ├── Linux-aarch64 │ │ └── libsemuxcrypto.so │ ├── Linux-x86_64 │ │ └── libsemuxcrypto.so │ └── Windows-x86_64 │ │ └── libsemuxcrypto.dll │ ├── org │ └── semux │ │ ├── api │ │ ├── favicon-16x16.png │ │ ├── favicon-32x32.png │ │ ├── index.html │ │ ├── mime.types │ │ └── swagger │ │ │ ├── v1.0.0.json │ │ │ ├── v1.0.1.json │ │ │ ├── v2.0.0.json │ │ │ ├── v2.1.0.json │ │ │ ├── v2.2.0.json │ │ │ ├── v2.3.0.json │ │ │ ├── v2.4.0.json │ │ │ ├── v2.5.0.json │ │ │ └── v2.5.0.yml │ │ ├── cli │ │ └── messages.properties │ │ └── gui │ │ ├── banner.png │ │ ├── contract.png │ │ ├── cycle.png │ │ ├── delegates.png │ │ ├── home.png │ │ ├── inbound.png │ │ ├── lock.png │ │ ├── logo.ico │ │ ├── logo.png │ │ ├── messages.properties │ │ ├── messages_ar_SA.properties │ │ ├── messages_de_DE.properties │ │ ├── messages_es_ES.properties │ │ ├── messages_fr_FR.properties │ │ ├── messages_id_ID.properties │ │ ├── messages_it_IT.properties │ │ ├── messages_ja_JP.properties │ │ ├── messages_ko_KR.properties │ │ ├── messages_ru_RU.properties │ │ ├── messages_sl_SI.properties │ │ ├── messages_tr_TR.properties │ │ ├── messages_vi_VN.properties │ │ ├── messages_zh_CN.properties │ │ ├── messages_zh_TW.properties │ │ ├── outbound.png │ │ ├── receive.png │ │ ├── send.png │ │ ├── splash.png │ │ └── transactions.png │ └── wordlists │ ├── english.txt │ ├── french.txt │ ├── italian.txt │ ├── japanese.txt │ ├── korean.txt │ └── spanish.txt └── test ├── java └── org │ └── semux │ ├── IntegrationTest.java │ ├── KernelMock.java │ ├── KernelTest.java │ ├── LauncherTest.java │ ├── TestLoggingAppender.java │ ├── TestUtils.java │ ├── api │ ├── SemuxApiMock.java │ ├── SemuxApiServiceTest.java │ ├── SemuxApiTestBase.java │ ├── http │ │ └── HttpHandlerTest.java │ ├── util │ │ └── TransactionBuilderTest.java │ └── v2 │ │ ├── SemuxApiErrorTest.java │ │ ├── SemuxApiTest.java │ │ └── SemuxApiTestBase.java │ ├── bench │ ├── ApiPerformance.java │ ├── BlockchainPerformance.java │ ├── CompressPerformance.java │ ├── CryptoPerformance.java │ ├── DBPerformance.java │ ├── SemuxPerformance.java │ ├── VMPerformance.java │ └── ValidatorDistributionChart.java │ ├── cli │ └── SemuxCliTest.java │ ├── config │ ├── DevnetConfigTest.java │ ├── MainnetConfigTest.java │ ├── TestnetConfigTest.java │ └── UnitTestnetConfig.java │ ├── consensus │ ├── ProposalTest.java │ ├── SemuxBftOnNewHeightTest.java │ ├── SemuxBftTest.java │ ├── SemuxBftValidateBlockTest.java │ ├── VoteSetTest.java │ └── VoteTest.java │ ├── core │ ├── AmountTest.java │ ├── BlockHeaderDataTest.java │ ├── BlockHeaderTest.java │ ├── BlockTest.java │ ├── BlockchainImplMigrationTest.java │ ├── BlockchainImplTest.java │ ├── BlockchainImportTest.java │ ├── CorePerformanceTest.java │ ├── ForkSignalSetTest.java │ ├── GenesisTest.java │ ├── PendingManagerTest.java │ ├── TransactionExecutorTest.java │ ├── TransactionResultTest.java │ ├── TransactionTest.java │ ├── WalletTest.java │ ├── WalletVersionTest.java │ └── state │ │ ├── AccountStateTest.java │ │ └── DelegateStateTest.java │ ├── crypto │ ├── AesTest.java │ ├── CryptoExceptionTest.java │ ├── HashTest.java │ ├── HexTest.java │ ├── KeyTest.java │ ├── NativeTest.java │ ├── bip32 │ │ ├── Base58.java │ │ ├── BaseVectorTest.java │ │ ├── Bip32Ed25519Test.java │ │ ├── BitUtilTest.java │ │ ├── PublicKeyChainTest.java │ │ ├── Slip10Test.java │ │ ├── VectorOneTest.java │ │ ├── VectorThreeTest.java │ │ └── VectorTwoTest.java │ ├── bip39 │ │ ├── MnemonicGeneratorTest.java │ │ ├── Vector.java │ │ ├── VectorReader.java │ │ └── VectorTest.java │ ├── bip44 │ │ └── Bip44Test.java │ └── cache │ │ └── PublicKeyCacheTest.java │ ├── db │ └── LeveldbDatabaseTest.java │ ├── event │ └── PubSubTest.java │ ├── gui │ ├── BaseTestApplication.java │ ├── MainFrameTest.java │ ├── MenuBarTest.java │ ├── MenuBarTestApplication.java │ ├── SemuxGuiTest.java │ ├── SplashScreenTest.java │ ├── SplashScreenTestApplication.java │ ├── StatusBarTest.java │ ├── StatusBarTestApplication.java │ ├── SwingUtilTest.java │ ├── WalletModelRule.java │ ├── WelcomeFrameTest.java │ ├── dialog │ │ ├── AddressBookDialogTest.java │ │ ├── AddressBookDialogTestApplication.java │ │ ├── ConsoleDialogTest.java │ │ ├── ConsoleDialogTestApplication.java │ │ ├── DelegateDialogTest.java │ │ ├── DelegateDialogTestApplication.java │ │ ├── TransactionDialogTest.java │ │ ├── TransactionDialogTestApplication.java │ │ ├── TransactionResultDialogTest.java │ │ └── TransactionResultDialogTestApplication.java │ ├── model │ │ ├── WalletAccountTest.java │ │ ├── WalletDelegateTest.java │ │ └── WalletModelTest.java │ └── panel │ │ ├── DelegatesPanelTest.java │ │ ├── DelegatesPanelTestApplication.java │ │ ├── HomePanelTest.java │ │ ├── HomePanelTestApplication.java │ │ ├── ReceivePanelTest.java │ │ ├── ReceivePanelTestApplication.java │ │ ├── TransactionsPanelTest.java │ │ └── TransactionsPanelTestApplication.java │ ├── integration │ ├── ConnectionTest.java │ ├── SyncingTest.java │ ├── TransactTest.java │ └── UniformDistForkSyncingTest.java │ ├── message │ ├── CLIMessageTest.java │ ├── GUIMessageTest.java │ └── MessageTest.java │ ├── net │ ├── CapabilityTest.java │ ├── ConnectionLimitHandlerTest.java │ ├── FrameTest.java │ ├── NodeManagerTest.java │ ├── PeerClientTest.java │ ├── PeerServerMock.java │ ├── filter │ │ ├── FilterRuleExceptionTest.java │ │ ├── FilterRuleTest.java │ │ ├── SemuxIpFilterLoaderTest.java │ │ ├── SemuxIpFilterSaverTest.java │ │ ├── SemuxIpFilterTest.java │ │ └── SemuxIpFilterTestBase.java │ └── msg │ │ ├── MessageCodeTest.java │ │ ├── MessageFactoryTest.java │ │ ├── MessageQueueTest.java │ │ ├── ReasonCodeTest.java │ │ ├── consensus │ │ ├── BlockHeaderMessageTest.java │ │ ├── GetBlockHeaderMessageTest.java │ │ ├── NewHeightMessageTest.java │ │ ├── NewViewMessageTest.java │ │ ├── ProposalMessageTest.java │ │ └── VoteMessageTest.java │ │ └── p2p │ │ ├── NodesMessageTest.java │ │ ├── TransactionMessageTest.java │ │ └── handshake │ │ ├── v1 │ │ ├── HelloMessageTest.java │ │ └── WorldMessageTest.java │ │ └── v2 │ │ ├── HelloMessageTest.java │ │ ├── InitMessageTest.java │ │ └── WorldMessageTest.java │ ├── rules │ ├── KernelRule.java │ └── TemporaryDatabaseRule.java │ ├── util │ ├── ArrayUtilTest.java │ ├── BasicAuthTest.java │ ├── BigIntegerUtilTest.java │ ├── ByteArrayTest.java │ ├── BytesTest.java │ ├── CacheTest.java │ ├── CommandParserTest.java │ ├── FileUtilTest.java │ ├── IOUtilTest.java │ ├── MerkleTreeTest.java │ ├── MerkleUtilTest.java │ ├── SimpleApiClientTest.java │ ├── SimpleDecoderTest.java │ ├── SimpleEncoderTest.java │ ├── StringUtilTest.java │ ├── SystemUtilTest.java │ └── TimeUtilTest.java │ └── vm │ └── client │ ├── InternalTransactionTest.java │ ├── PrecompiledContractTest.java │ └── VmTransactionTest.java └── resources ├── config └── validators1000.csv ├── database └── database-v0.tgz ├── ipfilter ├── blacklist.json ├── empty.json ├── exception_empty_object.json ├── exception_type_cast1.json ├── exception_type_cast2.json ├── exception_type_cast3.json ├── exception_typo_in_rules1.json ├── exception_typo_in_rules2.json ├── exception_typo_in_rules3.json ├── exception_typo_in_rules4.json ├── exception_typo_in_rules5.json └── whitelist.json ├── log4j2-test.xml ├── mockito-extensions └── org.mockito.plugins.MockMaker ├── vector └── vector.json └── wallet ├── backup_from_linux ├── backup_from_macos └── backup_from_windows /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: "\U0001F41B Bug report" 3 | about: Create a bug report to help improve Semux Core 4 | title: "" 5 | labels: bug 6 | assignees: '' 7 | --- 8 | 9 | # 🐛 Bug 10 | 11 | (Describe the bug/issue here) 12 | 13 | 14 | ## To reproduce 15 | 16 | 1. 17 | 2. 18 | 3. 19 | 20 | 21 | ## Expected Behavior 22 | 23 | (Describe the expected behavior here) 24 | 25 | 26 | ## System information 27 | 28 | - Operating system: 29 | - Semux version: 30 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: "\U0001F680 Feature request" 3 | about: Suggest a new feature in Semux Core 4 | title: "" 5 | labels: enhancement 6 | assignees: '' 7 | --- 8 | 9 | # 🚀 Feature Request 10 | 11 | (Describe the feature you'd like to request) 12 | 13 | 14 | ## Motivation 15 | 16 | (Describe the motivation here) 17 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | ## Description 2 | 3 | (Write your proposed changes here.) 4 | 5 | 6 | ## Test Plan 7 | 8 | (Share your test plan here.) 9 | 10 | 11 | ## Related issues and/or PRs 12 | 13 | (Add links to any related issues or PRs) 14 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # binary files and logs 2 | *.class 3 | *.log 4 | 5 | # release binaries 6 | *.zip 7 | *.tar.gz 8 | 9 | # data folders 10 | /chaindata/ 11 | /wallet/ 12 | /log/ 13 | 14 | # cmake build dir 15 | build/ 16 | 17 | # maven generated files 18 | target/ 19 | dist/ 20 | 21 | # eclipse generated files 22 | .project 23 | .classpath 24 | .settings/ 25 | 26 | # IDEA generated files 27 | .idea 28 | *.iml 29 | 30 | # OSX generated files 31 | .DS_Store 32 | 33 | # JVM crash dumps 34 | hs_err_pid* 35 | 36 | # ignore generated files 37 | src/gen 38 | 39 | # ignore slf4j test config 40 | src/test/resources/log4j2-test.xml 41 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "src/main/native/crypto/libsodium"] 2 | path = src/main/native/crypto/libsodium 3 | url = https://github.com/jedisct1/libsodium.git 4 | [submodule "src/main/native/crypto/ed25519-donna"] 5 | path = src/main/native/crypto/ed25519-donna 6 | url = https://github.com/cryptokat/ed25519-donna.git 7 | -------------------------------------------------------------------------------- /.swagger-codegen-ignore: -------------------------------------------------------------------------------- 1 | # Do not override source files 2 | src/main/**/*.java 3 | src/test/**/*.java 4 | 5 | # Do not generate implementations 6 | **/impl/**/*.java -------------------------------------------------------------------------------- /.swagger-codegen/VERSION: -------------------------------------------------------------------------------- 1 | 3.0.20 -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | 3 | matrix: 4 | include: 5 | # License, formatter, and FindBugs check 6 | - env: NAME="License, formatter, and FindBugs check" 7 | os: linux 8 | jdk: openjdk11 9 | script: 10 | - mvn license:check 11 | - mvn formatter:validate 12 | - mvn spotbugs:check 13 | 14 | # Linux tests 15 | - env: NAME="Linux tests (Java 11)" 16 | os: linux 17 | jdk: openjdk11 18 | services: 19 | - xvfb 20 | script: 21 | - mvn test 22 | 23 | # macOS tests 24 | - env: NAME="macOS tests (Java 15, Big Sur 11.1)" 25 | os: osx 26 | osx_image: xcode12.3 27 | services: 28 | - xvfb 29 | script: 30 | - mvn test '-Dtest=!org.semux.gui.**' 31 | 32 | cache: 33 | directories: 34 | - .autoconf 35 | - $HOME/.m2 36 | - $HOME/Library/Caches/Homebrew 37 | 38 | before_install: 39 | - rm ~/.m2/settings.xml || true # remove the default settings.xml in order to use custom repos 40 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM openjdk:8-jre 2 | 3 | WORKDIR / 4 | 5 | RUN apt-get update && apt-get install --yes curl jq 6 | 7 | RUN LATEST=`curl -s https://api.github.com/repos/semuxproject/semux-core/releases/latest | jq '.assets[] | select(.name | contains("linux"))'` && \ 8 | LINK=`echo ${LATEST} | jq -r '.browser_download_url'` && \ 9 | TARBALL=`echo ${LATEST} | jq -r '.name'` && \ 10 | curl -Lo ${TARBALL} ${LINK} && \ 11 | mkdir -p /semux && \ 12 | tar -xzf ${TARBALL} -C /semux --strip-components=1 && \ 13 | rm ${TARBALL} 14 | 15 | RUN apt-get remove --yes curl jq 16 | 17 | EXPOSE 5161 18 | 19 | ENTRYPOINT ["/semux/semux-cli.sh"] 20 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2017-2020 The Semux Developers 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | image: Visual Studio 2017 2 | 3 | version: "{build} ({branch})" 4 | 5 | shallow_clone: false 6 | clone_depth: 1 7 | clone_folder: C:\projects\semux 8 | 9 | environment: 10 | matrix: 11 | - JAVA_HOME: C:\Program Files\Java\jdk11 12 | Launch4j: debug 13 | MAVEN_OPTS: -Xmx1g 14 | 15 | install: 16 | - echo %PATH% 17 | - echo %M2_HOME% 18 | - mvn -X -v 19 | 20 | # disable MSBuild 21 | build: off 22 | 23 | build_script: 24 | - cd C:\projects\semux 25 | - mvn clean install -DskipTests 26 | 27 | test_script: 28 | - mvn verify 29 | 30 | cache: 31 | - C:\Users\appveyor\.m2\ 32 | -------------------------------------------------------------------------------- /assets/multisig-owners.json: -------------------------------------------------------------------------------- 1 | { 2 | "semuxgo": "0x08be88c5f839bfbd22f1505a7bb14f9928866149", 3 | "orog": "0x13db16f8b684e037aca2fd8fe5d81845c16fc6d4", 4 | "honey": "0x8b139acd0223fcee79f8371ca162e54f3d1dcd16" 5 | } 6 | -------------------------------------------------------------------------------- /assets/semux-core-version.json: -------------------------------------------------------------------------------- 1 | { 2 | "minVersion": "2.1.1", 3 | "latestVersion": "2.2.0" 4 | } -------------------------------------------------------------------------------- /config/ipfilter.json: -------------------------------------------------------------------------------- 1 | { 2 | "rules": [ 3 | ] 4 | } -------------------------------------------------------------------------------- /config/log4j2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 10 | 11 | true 12 | true 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /crowdin.yml: -------------------------------------------------------------------------------- 1 | files: 2 | - source: /src/main/resources/org/semux/gui/messages.properties 3 | translation: >- 4 | /src/main/resources/org/semux/gui/messages_%locale_with_underscore%.properties 5 | -------------------------------------------------------------------------------- /docs/Block-Explorers.md: -------------------------------------------------------------------------------- 1 | # Block Explorers 2 | 3 | A block explorer is a website that allows querying the blockchain outside the client. 4 | 5 | Public Semux explorers: 6 | - https://semux.info/explorer 7 | -------------------------------------------------------------------------------- /docs/Bug-Bounty-Program.md: -------------------------------------------------------------------------------- 1 | # Bug Bounties 2 | 3 | Semux is happy to reward finding bugs with a SEM bounty. We value community contributions. 4 | 5 | * Submit issue tickets at https://github.com/semuxproject/semux-core/issues. 6 | * The reward is 100 ~ 100,000 SEM based on its severity and impact. 7 | * Please call out on Discord for claiming rewords. -------------------------------------------------------------------------------- /docs/Console.md: -------------------------------------------------------------------------------- 1 | #Semux Console 2 | 3 | When running the Semux GUI, you can navigate to `Help|Console` to get an interactive terminal to run commands against. 4 | 5 | This provides the same functionality as the [RESTful API](./RESTful-API.md) 6 | 7 | Type `help` to see a list of commands. -------------------------------------------------------------------------------- /docs/Delegate-Pools.md: -------------------------------------------------------------------------------- 1 | # Delegate Pools 2 | 3 | A pool is a [validator](./Delegates-and-Validators.md) that shares a portion of forged earnings with voters. 4 | 5 | Each pool has its own fees and payout structure, but most pay daily. Not all validators are pools, many are 6 | privately owned, or private pools by invite only. The pool stats site below documents currently known public pools. 7 | 8 | ## Delegate Pools 9 | 10 | - https://semux.info/pool 11 | - https://github.com/orogvany/semux-pool 12 | 13 | ## Pool Stats 14 | 15 | - http://pool.7u.org/ 16 | -------------------------------------------------------------------------------- /docs/Devnet.md: -------------------------------------------------------------------------------- 1 | # Devnet 2 | 3 | Devnet is a feature similiar to [Bitcoin's Regtest Mode](https://bitcoin.org/en/glossary/regression-test-mode) that 4 | allows developers to run a local blockchain with a single Semux node for testing purpose. 5 | 6 | To start a devnet, run: 7 | 8 | ```bash 9 | ./semux-cli.sh --network devnet 10 | ``` 11 | 12 | ### Devnet Validator 13 | 14 | There is a default validator that allows you to forge blocks on Devnet: 15 | 16 | - Public Key = `0x23a6049381fd2cfb0661d9de206613b83d53d7df` 17 | - Private Key = `0x302e020100300506032b657004220420acbd5f2cb2b6053f704376d12df99f2aa163d267a755c7f1d9fe55d2a2dc5405` 18 | 19 | To start forging blocks on Devnet, run: 20 | ```bash 21 | ./semux-cli.sh --importprivatekey 302e020100300506032b657004220420acbd5f2cb2b6053f704376d12df99f2aa163d267a755c7f1d9fe55d2a2dc5405 22 | ``` 23 | then specify `0x23a6049381fd2cfb0661d9de206613b83d53d7df` as your coinbase. 24 | 25 | ### Testnet 26 | 27 | If you prefer using a public testing network, see [Testnet](./Testnet.md). 28 | -------------------------------------------------------------------------------- /docs/IDE-Setup.md: -------------------------------------------------------------------------------- 1 | # IDE Setup 2 | 3 | This guide helps you set up IDE environments, i.e. Eclipse and IntelliJ. 4 | 5 | 6 | ## Eclipse 7 | 8 | 1. Clone the semux project via `git clone https://github.com/semuxproject/semux-core`. 9 | 2. Import the cloned code as a Maven project. 10 | 3. Go to `Preferences` > `Java` > `Code Style` > `Formatter`; import the 11 | [Eclipse formatter XML](https://raw.githubusercontent.com/semuxproject/semux-core/master/misc/formatter_eclipse.xml). 12 | 4. Now your workspace is ready! 13 | 14 | 15 | ## IntelliJ 16 | 17 | 1. Clone the semux project via `git clone https://github.com/semuxproject/semux-core`. 18 | 2. Import the cloned code as a Maven project. 19 | 3. Go to `Preferences` > `Code Style` > `Java`; import the 20 | [Eclipse formatter XML](https://raw.githubusercontent.com/semuxproject/semux-core/master/misc/formatter_eclipse.xml) and 21 | the [IntelliJ formatter XML](https://raw.githubusercontent.com/semuxproject/semux-core/master/misc/formatter_intellij.xml) (both are required). 22 | 4. Now your workspace is ready! 23 | -------------------------------------------------------------------------------- /docs/Message-Encoding.md: -------------------------------------------------------------------------------- 1 | # Message Encoding 2 | Semux network messages format. 3 | 4 | ## Signatures 5 | A signed message takes the message bytes and signs it, then appends this signature to the message. 6 | 7 | ## Types 8 | 9 | ### boolean 10 | 1 bit 11 | 12 | ### long 13 | 8 bytes 14 | 15 | ### int 16 | 4 bytes 17 | 18 | ### byte[] 19 | First write the number of bytes using VLQ, then write each byte. 20 | 21 | ### string 22 | First write the number of bytes using VLQ, then write each byte of the string. 23 | 24 | ## VLQ 25 | Variable length quantity. This encoding allows for writing both large and small numbers in an efficient manner. 26 | 27 | -------------------------------------------------------------------------------- /docs/README.md: -------------------------------------------------------------------------------- 1 | Welcome to the Semux Documentation! 2 | 3 | ### How it Works 4 | * [Supply and Block Rewards](./Supply-and-Block-Rewards.md) 5 | * [Delegates and Validators](./Delegates-and-Validators.md) 6 | * [Semux BFT Consensus](./Semux-BFT-Consensus.md) 7 | * [Semux Virtual Machine](./Semux-Virtual-Machine.md) 8 | 9 | ### Using Semux 10 | * [Configurations](./Configurations.md) 11 | * [Block Explorers](./Block-Explorers.md) 12 | * [Delegate Pools](./Delegate-Pools.md) 13 | * [Full Node Setup](./Full-Node-Setup.md) 14 | * [Using the Virtual Machine](./Virtual-Machine-Contracts.md) 15 | 16 | ### Development 17 | * [Bug Bounty Program](./Bug-Bounty-Program.md) 18 | * [Devnet](./Devnet.md) 19 | * [Testnet](./Testnet.md) 20 | * [IDE Setup](./IDE-Setup.md) 21 | * [RESTful API](./RESTful-API.md) 22 | * [API Console](./Console.md) 23 | * [Network Protocol](./Network-Protocol.md) 24 | -------------------------------------------------------------------------------- /docs/RESTful-API.md: -------------------------------------------------------------------------------- 1 | # RESTful API 2 | 3 | Semux provides a rich set of RESTful APIs for interacting with the blockchain programmatically. 4 | 5 | Note that You can also access the same set of APIs via the [Console](./Console.md) 6 | 7 | ## Configuration 8 | 9 | Your local Semux API server can be enabled by changing following properties in your `config/semux.properties`: 10 | ``` 11 | # Be sure to set up authentication first before enabling API 12 | api.enabled = true 13 | 14 | # Listening address and port 15 | api.listenIp = 127.0.0.1 16 | api.listenPort = 5171 17 | 18 | # Basic authentication 19 | api.username = YOUR_API_USERNAME 20 | api.password = YOUR_API_PASSWORD 21 | ``` 22 | 23 | ## Base Unit 24 | 25 | The base unit of Semux API is Nano SEM (10-9 SEM) 26 | 27 | ## API Explorer 28 | 29 | Once you've successfully started Semux API server, a Swagger UI page is available locally as an API explorer at: 30 | http://localhost:5171/ 31 | 32 | ## Web API Explorers 33 | 34 | If you want to interact with the semux network without having a local client running, you can access many (but not all) 35 | of the API calls at some hosted APIs. This gives you a swagger client similar to a locally running one. 36 | 37 | - Mainnet: https://api.semux.info/ 38 | 39 | ## API Clients 40 | 41 | Semux provides a [Swagger API Spec in JSON](../src/main/resources/org/semux/api/swagger/v2.2.0.json) since API version 2. 42 | You can either generate an API client by yourself using [Swagger Codegen](https://github.com/swagger-api/swagger-codegen) 43 | or use the following pre-generated clients: 44 | 45 | - Javascript: https://github.com/semuxproject/semux-js 46 | - Java: https://github.com/orogvany/semux-java-client 47 | -------------------------------------------------------------------------------- /docs/Semux-Virtual-Machine.md: -------------------------------------------------------------------------------- 1 | # Semux Virtual Machine 2 | 3 | The Semux VM allows users to create programmatic logic on the blockchain. 4 | 5 | Semux VM will be launched with the 1.5.0 release. It is currently available in the [Testnet](./Testnet.md) 6 | 7 | It utilizes the well known ethereum VM format/language for contracts, so it is compatible with Solidity, and soon will 8 | be support web3js. 9 | 10 | It uses gas and gas prices in the same mechanism as used for ethereum. 11 | 12 | It supports all of the Byzantium ethereum constructs, as well as two new calls specific to Semux, Vote and Unvote. 13 | 14 | Please be patient as we update these documents. More to come. 15 | -------------------------------------------------------------------------------- /docs/Testnet.md: -------------------------------------------------------------------------------- 1 | # Testnet 2 | 3 | Testnet is, as the name implies, for testing features of the blockchain without spending 'real' semux. 4 | 5 | It allows developers of tools and wallets to integrate and test features without risk. 6 | 7 | ### How to connect Semux Testnet 8 | 9 | You can connect to the testnet via the following command 10 | ``` 11 | ./semux-gui.sh --network testnet 12 | ``` 13 | 14 | ### Resources 15 | 16 | * Testnet faucet: https://www.semux.org/testnetfaucet 17 | * Testnet explorer: https://testnet.semux.info/ 18 | 19 | ### Devnet 20 | 21 | If you prefer using a local testing blockchain, see [Devnet](./Devnet.md). 22 | -------------------------------------------------------------------------------- /docs/Versioning.md: -------------------------------------------------------------------------------- 1 | # Versioning 2 | 3 | ### Summary 4 | 5 | In general, the semantic versioning schema should be followed. 6 | 7 | > Given a version number MAJOR.MINOR.PATCH, increment the: 8 | > - MAJOR version when you make incompatible API changes, 9 | > - MINOR version when you add functionality in a backwards-compatible manner, and 10 | > - PATCH version when you make backwards-compatible bug fixes. 11 | 12 | ### Kernel version 13 | 14 | * Increase MAJOR version when introducing major changes: e.g., new virtual machine, significant consensus changes and new p2p protocol 15 | * Increase MINOR version when making scheduled/incremental changes: e.g., new feature and enhancements 16 | * Increase PATCH version for bugfixes only 17 | 18 | ### Api Version 19 | 20 | * Increase MAJOR version when the API becomes incompatible 21 | * Increase MINOR version when adding new endpoints 22 | * Reserved 23 | -------------------------------------------------------------------------------- /misc/design/splash.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/semuxproject/semux-core/3ac4bf663f3eb4e0ab4976bb049ba30ffe8181f4/misc/design/splash.bmp -------------------------------------------------------------------------------- /misc/design/splash.kra: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/semuxproject/semux-core/3ac4bf663f3eb4e0ab4976bb049ba30ffe8181f4/misc/design/splash.kra -------------------------------------------------------------------------------- /misc/launch4j/semux.l4j.ini: -------------------------------------------------------------------------------- 1 | # Additional JVM options can be added here 2 | # -Xms512m 3 | # -Xmx2048m 4 | -Dlog4j2.garbagefreeThreadContextMap=true 5 | -Dlog4j2.shutdownHookEnabled=false 6 | -Dlog4j2.disableJmx=true -------------------------------------------------------------------------------- /misc/license_header.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2017-2020 The Semux Developers 2 | 3 | Distributed under the MIT software license, see the accompanying file 4 | LICENSE or https://opensource.org/licenses/mit-license.php -------------------------------------------------------------------------------- /misc/systemd/semux.service: -------------------------------------------------------------------------------- 1 | [Unit] 2 | Description=Semux 3 | After=network.target 4 | 5 | [Service] 6 | User=username 7 | Group=groupname 8 | Type=simple 9 | Restart=on-failure 10 | 11 | # For Mainnet node 12 | ExecStart=/home/username/semux/semux-cli.sh 13 | 14 | # For Testnet node 15 | # ExecStart=/home/username/semux/semux-cli.sh --network testnet 16 | 17 | # SEMUX_WALLET_PASSWORD environment variable is required to automatically unlock your wallet.data file. 18 | # Please ensure sure that the access permission of this service unit file is properly configured when you put your password here. 19 | Environment=SEMUX_WALLET_PASSWORD=YOUR_WALLET_PASSWORD 20 | 21 | [Install] 22 | WantedBy=multi-user.target 23 | -------------------------------------------------------------------------------- /scripts/release.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # Quick on failure 4 | set -e 5 | 6 | # Change the work directory 7 | cd "$(dirname "$0")/.." 8 | 9 | # Extract the name, version and revision 10 | name=semux 11 | version=`cat pom.xml | grep '^ .*$' | awk -F'[><]' '{print $3}'` 12 | revision=`git rev-parse --short=7 HEAD` 13 | label="$version-$revision" 14 | 15 | # Make a clean build 16 | mvn clean install -DskipTests 17 | 18 | # Navigate to the dist folder 19 | cd dist 20 | 21 | # Download JVM 22 | wget -nc https://github.com/AdoptOpenJDK/openjdk11-binaries/releases/download/jdk-11.0.11%2B9/OpenJDK11U-jre_x64_windows_hotspot_11.0.11_9.zip 23 | unzip OpenJDK11U-jre_x64_windows_hotspot_11.0.11_9.zip 24 | mv jdk-11.0.11+9-jre ./windows/jvm 25 | 26 | wget -nc https://github.com/AdoptOpenJDK/openjdk11-binaries/releases/download/jdk-11.0.11%2B9/OpenJDK11U-jre_x64_linux_hotspot_11.0.11_9.tar.gz 27 | tar -xvf OpenJDK11U-jre_x64_linux_hotspot_11.0.11_9.tar.gz 28 | mv jdk-11.0.11+9-jre ./linux/jvm 29 | 30 | wget -nc https://github.com/AdoptOpenJDK/openjdk11-binaries/releases/download/jdk-11.0.11%2B9/OpenJDK11U-jre_x64_mac_hotspot_11.0.11_9.tar.gz 31 | tar -xvf OpenJDK11U-jre_x64_mac_hotspot_11.0.11_9.tar.gz 32 | mv jdk-11.0.11+9-jre/Contents/Home ./macos/jvm 33 | rm -fr jdk-11.0.11+9-jre 34 | 35 | # Make a Windows release 36 | folder=$name-windows-$label 37 | archive=$folder.zip 38 | mv windows $folder 39 | zip -r $archive $folder 40 | rm -fr $folder 41 | 42 | # Make a Linux release 43 | folder=$name-linux-$label 44 | archive=$folder.tar.gz 45 | mv linux $folder 46 | tar -czvf $archive $folder 47 | rm -fr $folder 48 | 49 | # Make a macOS release 50 | folder=$name-macos-$label 51 | archive=$folder.tar.gz 52 | mv macos $folder 53 | tar -czvf $archive $folder 54 | rm -fr $folder -------------------------------------------------------------------------------- /scripts/semux-cli.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | set java_bin=.\jvm\bin\java 4 | 5 | %java_bin% -cp .\lib\semux.jar org.semux.JvmOptions --cli > jvm_options.txt 6 | set /p jvm_options= jvm_options.txt 6 | set /p jvm_options= startArgs = new ArrayList<>(); 22 | boolean startGui = true; 23 | for (String arg : args) { 24 | if (CLI.equals(arg)) { 25 | startGui = false; 26 | } else if (GUI.equals(arg)) { 27 | startGui = true; 28 | } else { 29 | startArgs.add(arg); 30 | } 31 | } 32 | 33 | if (startGui) { 34 | SemuxGui.main(startArgs.toArray(new String[0])); 35 | } else { 36 | SemuxCli.main(startArgs.toArray(new String[0])); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/org/semux/Network.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux; 8 | 9 | import java.util.HashMap; 10 | import java.util.Map; 11 | 12 | public enum Network { 13 | 14 | MAINNET((byte) 0, "mainnet"), 15 | 16 | TESTNET((byte) 1, "testnet"), 17 | 18 | DEVNET((byte) 2, "devnet"); 19 | 20 | Network(byte id, String label) { 21 | this.id = id; 22 | this.label = label; 23 | } 24 | 25 | private final byte id; 26 | private final String label; 27 | 28 | private static final Map labels = new HashMap<>(); 29 | private static final Map ids = new HashMap<>(); 30 | 31 | static { 32 | for (Network net : Network.values()) { 33 | labels.put(net.label, net); 34 | ids.put(net.id, net); 35 | } 36 | } 37 | 38 | public byte id() { 39 | return id; 40 | } 41 | 42 | public String label() { 43 | return label; 44 | } 45 | 46 | @Override 47 | public String toString() { 48 | return label; 49 | } 50 | 51 | public static Network of(byte networkId) { 52 | return ids.get(networkId); 53 | } 54 | 55 | public static Network of(String label) { 56 | return labels.get(label); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/org/semux/api/ApiHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.api; 8 | 9 | import java.util.Map; 10 | 11 | import javax.ws.rs.core.Response; 12 | 13 | import io.netty.handler.codec.http.HttpHeaders; 14 | import io.netty.handler.codec.http.HttpMethod; 15 | 16 | /** 17 | * Semux RESTful API handler. 18 | */ 19 | public interface ApiHandler { 20 | 21 | /** 22 | * Service object. 23 | * 24 | * @param method 25 | * the method 26 | * @param path 27 | * the path 28 | * @param params 29 | * the params 30 | * @param headers 31 | * the headers 32 | * @return the response object 33 | */ 34 | Response service(HttpMethod method, String path, Map params, HttpHeaders headers); 35 | 36 | boolean isAuthRequired(HttpMethod method, String path); 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/org/semux/api/ApiVersion.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.api; 8 | 9 | import java.util.HashMap; 10 | import java.util.Map; 11 | 12 | /** 13 | * Represents an API version, in the format of MAJOR.MINOR.PATCH. 14 | * 15 | *
    16 | *
  • MAJOR version increases when backward compatibility is lost.
  • 17 | *
  • MINOR version increases when incremental improvements is introduced.
  • 18 | *
  • PATCH version is reserved for security patches
  • 19 | *
20 | */ 21 | public enum ApiVersion { 22 | 23 | @Deprecated 24 | v1_0_0("v1.0.0"), 25 | 26 | @Deprecated 27 | v1_0_1("v1.0.1"), 28 | 29 | v2_0_0("v2.0.0"), 30 | 31 | v2_1_0("v2.1.0"), 32 | 33 | v2_2_0("v2.2.0"), 34 | 35 | v2_3_0("v2.3.0"), 36 | 37 | v2_4_0("v2.4.0"), 38 | 39 | v2_5_0("v2.5.0"); 40 | 41 | public final static ApiVersion DEFAULT = v2_5_0; 42 | 43 | public final String prefix; 44 | 45 | private static Map versions = new HashMap<>(); 46 | 47 | static { 48 | for (ApiVersion version : values()) { 49 | versions.put(version.prefix, version); 50 | } 51 | } 52 | 53 | ApiVersion(String prefix) { 54 | this.prefix = prefix; 55 | } 56 | 57 | public static ApiVersion of(String prefix) { 58 | return versions.get(prefix); 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /src/main/java/org/semux/api/http/HttpChannelInitializer.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.api.http; 8 | 9 | import io.netty.channel.ChannelInitializer; 10 | import io.netty.channel.ChannelPipeline; 11 | import io.netty.channel.socket.SocketChannel; 12 | import io.netty.handler.codec.http.HttpObjectAggregator; 13 | import io.netty.handler.codec.http.HttpServerCodec; 14 | import io.netty.handler.codec.http.HttpServerKeepAliveHandler; 15 | import io.netty.handler.stream.ChunkedWriteHandler; 16 | 17 | public abstract class HttpChannelInitializer extends ChannelInitializer { 18 | 19 | @Override 20 | public void initChannel(SocketChannel ch) { 21 | ChannelPipeline p = ch.pipeline(); 22 | p.addLast(new HttpServerCodec(HttpConstants.MAX_INITIAL_LINE_LENGTH, HttpConstants.MAX_HEADER_SIZE, 23 | HttpConstants.MAX_CHUNK_SIZE)); 24 | p.addLast(new HttpServerKeepAliveHandler()); 25 | p.addLast(new HttpObjectAggregator(HttpConstants.MAX_BODY_SIZE)); 26 | p.addLast(new ChunkedWriteHandler()); 27 | p.addLast(initHandler()); 28 | } 29 | 30 | public abstract HttpHandler initHandler(); 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/org/semux/api/http/HttpConstants.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.api.http; 8 | 9 | public class HttpConstants { 10 | public static final int MAX_BODY_SIZE = 4 * 1024 * 1024; 11 | 12 | public static final int MAX_INITIAL_LINE_LENGTH = 2 * 1024 * 1024; 13 | public static final int MAX_HEADER_SIZE = 2 * 1024 * 1024; 14 | public static final int MAX_CHUNK_SIZE = 2 * 1024 * 1024; 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/org/semux/api/v2/client/SemuxApi.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.api.v2.client; 8 | 9 | public interface SemuxApi extends AccountApi, BlockchainApi, DelegateApi, NodeApi, ToolApi, WalletApi { 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/org/semux/api/v2/server/SemuxApi.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.api.v2.server; 8 | 9 | public interface SemuxApi extends AccountApi, BlockchainApi, DelegateApi, NodeApi, ToolApi, WalletApi { 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/org/semux/cli/DatabaseScanner.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.cli; 8 | 9 | import java.io.File; 10 | import java.io.IOException; 11 | import java.util.Map.Entry; 12 | 13 | import org.fusesource.leveldbjni.JniDBFactory; 14 | import org.iq80.leveldb.CompressionType; 15 | import org.iq80.leveldb.DB; 16 | import org.iq80.leveldb.DBIterator; 17 | import org.iq80.leveldb.Options; 18 | import org.semux.crypto.Hex; 19 | 20 | public class DatabaseScanner { 21 | public static void main(String[] args) throws IOException { 22 | Options options = new Options(); 23 | options.createIfMissing(false); 24 | options.cacheSize(128L * 1024L * 1024L); 25 | options.compressionType(CompressionType.NONE); 26 | 27 | File f = new File("database/testnet/index"); 28 | 29 | try (DB db = JniDBFactory.factory.open(f, options)) { 30 | DBIterator itr = db.iterator(); 31 | itr.seekToFirst(); 32 | while (itr.hasNext()) { 33 | Entry entry = itr.next(); 34 | System.out.println(Hex.encode(entry.getKey()) + " = " + Hex.encode(entry.getValue())); 35 | } 36 | itr.close(); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/org/semux/cli/SemuxOption.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.cli; 8 | 9 | /** 10 | * Semux launcher options. 11 | */ 12 | public enum SemuxOption { 13 | 14 | HELP("help"), 15 | 16 | VERSION("version"), 17 | 18 | ACCOUNT("account"), 19 | 20 | CHANGE_PASSWORD("changepassword"), 21 | 22 | DATA_DIR("datadir"), 23 | 24 | COINBASE("coinbase"), 25 | 26 | PASSWORD("password"), 27 | 28 | DUMP_PRIVATE_KEY("dumpprivatekey"), 29 | 30 | IMPORT_PRIVATE_KEY("importprivatekey"), 31 | 32 | NETWORK("network"), 33 | 34 | HD_WALLET("hdwallet"), 35 | 36 | REINDEX("reindex"); 37 | 38 | private final String name; 39 | 40 | SemuxOption(String s) { 41 | name = s; 42 | } 43 | 44 | @Override 45 | public String toString() { 46 | return this.name; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/org/semux/config/DevnetConfig.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.config; 8 | 9 | import java.util.Collections; 10 | import java.util.HashMap; 11 | import java.util.Map; 12 | 13 | import org.semux.Network; 14 | import org.semux.core.Fork; 15 | 16 | public class DevnetConfig extends AbstractConfig { 17 | 18 | public DevnetConfig(String dataDir) { 19 | super(dataDir, Network.DEVNET, Constants.DEVNET_VERSION); 20 | 21 | this.netMaxInboundConnectionsPerIp = Integer.MAX_VALUE; 22 | 23 | this.forkUniformDistributionEnabled = true; 24 | this.forkVirtualMachineEnabled = true; 25 | this.forkVotingPrecompiledUpgradeEnabled = true; 26 | 27 | // set fast blocks 28 | bftNewHeightTimeout = 1000L; 29 | bftProposeTimeout = 2000L; 30 | bftValidateTimeout = 1000L; 31 | bftPreCommitTimeout = 1000L; 32 | bftCommitTimeout = 1000L; 33 | bftFinalizeTimeout = 1000L; 34 | 35 | // huge gas limit 36 | maxBlockGasLimit = 500_000_000L; 37 | } 38 | 39 | @Override 40 | public Map checkpoints() { 41 | return Collections.emptyMap(); 42 | } 43 | 44 | @Override 45 | public Map manuallyActivatedForks() { 46 | 47 | Map forks = new HashMap<>(); 48 | forks.put(Fork.UNIFORM_DISTRIBUTION, 1l); 49 | forks.put(Fork.VIRTUAL_MACHINE, 1l); 50 | forks.put(Fork.VOTING_PRECOMPILED_UPGRADE, 1l); 51 | 52 | return forks; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/org/semux/config/TestnetConfig.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.config; 8 | 9 | import java.util.Collections; 10 | import java.util.Map; 11 | 12 | import org.semux.Network; 13 | import org.semux.core.Fork; 14 | 15 | public class TestnetConfig extends AbstractConfig { 16 | 17 | public TestnetConfig(String dataDir) { 18 | super(dataDir, Network.TESTNET, Constants.TESTNET_VERSION); 19 | 20 | this.forkUniformDistributionEnabled = true; 21 | this.forkVirtualMachineEnabled = true; 22 | this.forkVotingPrecompiledUpgradeEnabled = true; 23 | } 24 | 25 | @Override 26 | public Map checkpoints() { 27 | return Collections.emptyMap(); 28 | } 29 | 30 | @Override 31 | public Map manuallyActivatedForks() { 32 | return Collections.emptyMap(); 33 | } 34 | 35 | /** 36 | * Testnet maxes out at 10 validators to stop dead validators from breaking 37 | * concensus 38 | * 39 | * @param number 40 | * @return 41 | */ 42 | @Override 43 | public int getNumberOfValidators(long number) { 44 | return Math.min(10, super.getNumberOfValidators(number)); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/org/semux/config/exception/ConfigException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.config.exception; 8 | 9 | public class ConfigException extends RuntimeException { 10 | 11 | private static final long serialVersionUID = 1L; 12 | 13 | public ConfigException() { 14 | } 15 | 16 | public ConfigException(String message) { 17 | super(message); 18 | } 19 | 20 | public ConfigException(String message, Throwable cause) { 21 | super(message, cause); 22 | } 23 | 24 | public ConfigException(Throwable cause) { 25 | super(cause); 26 | } 27 | 28 | public ConfigException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { 29 | super(message, cause, enableSuppression, writableStackTrace); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/org/semux/consensus/VoteType.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.consensus; 8 | 9 | public enum VoteType { 10 | 11 | /** 12 | * (0x00) Vote during the VALIDATE period. 13 | */ 14 | VALIDATE(0x00), 15 | 16 | /** 17 | * (0x01) Vote during the PRECOMMIT period. 18 | */ 19 | PRECOMMIT(0x01), 20 | 21 | /** 22 | * (0x02) Vote during the COMMIT period. 23 | */ 24 | COMMIT(0x02); 25 | 26 | private static final VoteType[] map = new VoteType[256]; 27 | static { 28 | for (VoteType tt : VoteType.values()) { 29 | map[tt.code] = tt; 30 | } 31 | } 32 | 33 | public static VoteType of(int code) { 34 | return map[0xff & code]; 35 | } 36 | 37 | private int code; 38 | 39 | VoteType(int code) { 40 | this.code = code; 41 | } 42 | 43 | public int getCode() { 44 | return code; 45 | } 46 | 47 | public byte toByte() { 48 | return (byte) code; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/org/semux/consensus/exception/SemuxBftException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.consensus.exception; 8 | 9 | public class SemuxBftException extends RuntimeException { 10 | private static final long serialVersionUID = 1L; 11 | 12 | public SemuxBftException() { 13 | } 14 | 15 | public SemuxBftException(String s) { 16 | super(s); 17 | } 18 | 19 | public SemuxBftException(String s, Throwable throwable) { 20 | super(s, throwable); 21 | } 22 | 23 | public SemuxBftException(Throwable throwable) { 24 | super(throwable); 25 | } 26 | 27 | public SemuxBftException(String s, Throwable throwable, boolean b, boolean b1) { 28 | super(s, throwable, b, b1); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/org/semux/core/BftManager.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.core; 8 | 9 | import org.semux.net.Channel; 10 | import org.semux.net.msg.Message; 11 | 12 | public interface BftManager { 13 | /** 14 | * Starts bft manager. 15 | * 16 | */ 17 | void start(); 18 | 19 | /** 20 | * Stops bft manager. 21 | */ 22 | void stop(); 23 | 24 | /** 25 | * Returns if the bft manager is running. 26 | * 27 | * @return 28 | */ 29 | boolean isRunning(); 30 | 31 | /** 32 | * Callback when a message is received from network. 33 | * 34 | * @param channel 35 | * the channel where the message is coming from 36 | * @param msg 37 | * the message 38 | */ 39 | void onMessage(Channel channel, Message msg); 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/org/semux/core/BlockPart.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.core; 8 | 9 | import java.util.ArrayList; 10 | import java.util.List; 11 | 12 | public enum BlockPart { 13 | HEADER(1), TRANSACTIONS(1 << 1), RESULTS(1 << 2), VOTES(1 << 3); 14 | 15 | private int code; 16 | 17 | BlockPart(int code) { 18 | this.code = code; 19 | } 20 | 21 | public static int encode(BlockPart... parts) { 22 | int result = 0; 23 | for (BlockPart part : parts) { 24 | result |= part.code; 25 | } 26 | return result; 27 | } 28 | 29 | public static List decode(int parts) { 30 | List result = new ArrayList<>(); 31 | // NOTE: values() returns an array containing all of the values of the enum type 32 | // in the order they are declared. 33 | for (BlockPart bp : BlockPart.values()) { 34 | if ((parts & bp.code) != 0) { 35 | result.add(bp); 36 | } 37 | } 38 | 39 | return result; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/org/semux/core/BlockchainListener.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.core; 8 | 9 | public interface BlockchainListener { 10 | 11 | /** 12 | * Callback when a new block was added. 13 | * 14 | * @param block 15 | */ 16 | void onBlockAdded(Block block); 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/org/semux/core/TransactionType.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.core; 8 | 9 | public enum TransactionType { 10 | 11 | /** 12 | * (0x00) Coinbase transaction 13 | */ 14 | COINBASE(0x00), 15 | 16 | /** 17 | * (0x01) Balance transfer. 18 | */ 19 | TRANSFER(0x01), 20 | 21 | /** 22 | * (0x02) Register as a delegate. 23 | */ 24 | DELEGATE(0x02), 25 | 26 | /** 27 | * (0x03) Vote for delegate. 28 | */ 29 | VOTE(0x03), 30 | 31 | /** 32 | * (0x04) Revoke a previous vote for a delegate. 33 | */ 34 | UNVOTE(0x04), 35 | 36 | /** 37 | * (0x05) Create a contract. 38 | */ 39 | CREATE(0x05), 40 | 41 | /** 42 | * (0x06) Call a contract. 43 | */ 44 | CALL(0x06); 45 | 46 | private static final TransactionType[] map = new TransactionType[256]; 47 | static { 48 | for (TransactionType tt : TransactionType.values()) { 49 | map[tt.code] = tt; 50 | } 51 | } 52 | 53 | public static TransactionType of(byte code) { 54 | return map[0xff & code]; 55 | } 56 | 57 | private int code; 58 | 59 | TransactionType(int code) { 60 | this.code = code; 61 | } 62 | 63 | public int getCode() { 64 | return code; 65 | } 66 | 67 | public byte toByte() { 68 | return (byte) code; 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/main/java/org/semux/core/Unit.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.core; 8 | 9 | import static java.util.Arrays.stream; 10 | 11 | import java.math.BigInteger; 12 | 13 | public enum Unit { 14 | NANO_SEM(0, "nSEM"), 15 | 16 | MICRO_SEM(3, "μSEM"), 17 | 18 | MILLI_SEM(6, "mSEM"), 19 | 20 | SEM(9, "SEM"), 21 | 22 | KILO_SEM(12, "kSEM"), 23 | 24 | MEGA_SEM(15, "MSEM"); 25 | 26 | public final int exp; 27 | public final long factor; 28 | public final String symbol; 29 | 30 | Unit(int exp, String symbol) { 31 | this.exp = exp; 32 | this.factor = BigInteger.TEN.pow(exp).longValueExact(); 33 | this.symbol = symbol; 34 | } 35 | 36 | /** 37 | * Decode the unit from symbol. 38 | * 39 | * @param symbol 40 | * the symbol text 41 | * @return a Unit object if valid; otherwise false 42 | */ 43 | public static Unit of(String symbol) { 44 | return stream(values()).filter(v -> v.symbol.equals(symbol)).findAny().orElse(null); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/org/semux/core/event/BlockchainDatabaseUpgradingEvent.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.core.event; 8 | 9 | import org.semux.event.PubSubEvent; 10 | 11 | public class BlockchainDatabaseUpgradingEvent implements PubSubEvent { 12 | 13 | public final Long loaded; 14 | 15 | public final Long total; 16 | 17 | public BlockchainDatabaseUpgradingEvent(Long loaded, Long total) { 18 | this.loaded = loaded; 19 | this.total = total; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/org/semux/core/event/WalletLoadingEvent.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.core.event; 8 | 9 | import org.semux.event.PubSubEvent; 10 | 11 | public class WalletLoadingEvent implements PubSubEvent { 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/org/semux/core/exception/BlockchainException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.core.exception; 8 | 9 | public class BlockchainException extends RuntimeException { 10 | private static final long serialVersionUID = 1L; 11 | 12 | public BlockchainException() { 13 | } 14 | 15 | public BlockchainException(String s) { 16 | super(s); 17 | } 18 | 19 | public BlockchainException(String s, Throwable throwable) { 20 | super(s, throwable); 21 | } 22 | 23 | public BlockchainException(Throwable throwable) { 24 | super(throwable); 25 | } 26 | 27 | public BlockchainException(String s, Throwable throwable, boolean b, boolean b1) { 28 | super(s, throwable, b, b1); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/org/semux/core/exception/WalletLockedException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.core.exception; 8 | 9 | public class WalletLockedException extends RuntimeException { 10 | 11 | private static final long serialVersionUID = 1L; 12 | 13 | public WalletLockedException() { 14 | super("Wallet is locked"); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/org/semux/crypto/CryptoException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.crypto; 8 | 9 | public class CryptoException extends RuntimeException { 10 | 11 | private static final long serialVersionUID = 1L; 12 | 13 | public CryptoException() { 14 | } 15 | 16 | public CryptoException(String msg) { 17 | super(msg); 18 | } 19 | 20 | public CryptoException(Throwable cause) { 21 | super(cause); 22 | } 23 | 24 | public CryptoException(String msg, Throwable cause) { 25 | super(msg, cause); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/org/semux/crypto/bip32/CoinType.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.crypto.bip32; 8 | 9 | public enum CoinType { 10 | BITCOIN(Scheme.BIP32, 0, false), 11 | 12 | SEMUX_SLIP10(Scheme.SLIP10_ED25519, 7562605, true), 13 | 14 | SEMUX_BIP32_ED25519(Scheme.BIP32_ED25519, 7562605, false); 15 | 16 | private final Scheme scheme; 17 | private final long coinType; 18 | private boolean alwaysHardened; 19 | 20 | CoinType(Scheme scheme, long coinType, boolean alwaysHardened) { 21 | 22 | this.scheme = scheme; 23 | this.coinType = coinType; 24 | this.alwaysHardened = alwaysHardened; 25 | } 26 | 27 | /** 28 | * Get the curve 29 | * 30 | * @return curve 31 | */ 32 | public Scheme getScheme() { 33 | return scheme; 34 | } 35 | 36 | /** 37 | * get the coin type 38 | * 39 | * @return coin type 40 | */ 41 | public long getCoinType() { 42 | return coinType; 43 | } 44 | 45 | /** 46 | * get whether the addresses must always be hardened 47 | * 48 | * @return always hardened 49 | */ 50 | public boolean getAlwaysHardened() { 51 | return alwaysHardened; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/org/semux/crypto/bip32/HdKeyPair.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.crypto.bip32; 8 | 9 | import org.semux.crypto.bip32.key.HdPrivateKey; 10 | import org.semux.crypto.bip32.key.HdPublicKey; 11 | 12 | /** 13 | * A HD pub/private key 14 | */ 15 | public class HdKeyPair { 16 | 17 | private final HdPrivateKey privateKey; 18 | private final HdPublicKey publicKey; 19 | private final CoinType coinType; 20 | private final String path; 21 | 22 | public HdKeyPair(HdPrivateKey privateKey, HdPublicKey publicKey, CoinType coinType, String path) { 23 | this.privateKey = privateKey; 24 | this.publicKey = publicKey; 25 | this.coinType = coinType; 26 | this.path = path; 27 | } 28 | 29 | public HdPrivateKey getPrivateKey() { 30 | return privateKey; 31 | } 32 | 33 | public HdPublicKey getPublicKey() { 34 | return publicKey; 35 | } 36 | 37 | public CoinType getCoinType() { 38 | return coinType; 39 | } 40 | 41 | public String getPath() { 42 | return path; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/org/semux/crypto/bip32/Scheme.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.crypto.bip32; 8 | 9 | public enum Scheme { 10 | /** 11 | * Reference: https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki 12 | */ 13 | BIP32("Bitcoin seed"), 14 | 15 | /** 16 | * Reference: https://github.com/satoshilabs/slips/blob/master/slip-0010.md 17 | */ 18 | SLIP10_ED25519("ed25519 seed"), 19 | 20 | /** 21 | * Reference: https://cardanolaunch.com/assets/Ed25519_BIP.pdf 22 | * 23 | * Implementation: 24 | * https://github.com/LedgerHQ/orakolo/blob/master/src/python/orakolo/HDEd25519.py 25 | */ 26 | BIP32_ED25519("ed25519 seed"); 27 | 28 | private final String seed; 29 | 30 | Scheme(String seed) { 31 | this.seed = seed; 32 | } 33 | 34 | public String getSeed() { 35 | return seed; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/org/semux/crypto/bip32/key/HdPrivateKey.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.crypto.bip32.key; 8 | 9 | /** 10 | * Defines a key with a given private key 11 | */ 12 | public class HdPrivateKey extends HdKey { 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/org/semux/crypto/bip32/key/HdPublicKey.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.crypto.bip32.key; 8 | 9 | /** 10 | * Defines a key with a given public key 11 | */ 12 | public class HdPublicKey extends HdKey { 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/org/semux/crypto/bip32/key/KeyVersion.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.crypto.bip32.key; 8 | 9 | import org.semux.crypto.Hex; 10 | 11 | /** 12 | * Defined network values for key generation 13 | */ 14 | public enum KeyVersion { 15 | MAINNET("0x0488ADE4", "0x0488B21E"), 16 | 17 | TESTNET("0x04358394", "0x043587CF"); 18 | 19 | private final byte[] privatePrefix; 20 | private final byte[] publicPrefix; 21 | 22 | KeyVersion(String privatePrefix, String publicPrefix) { 23 | this.privatePrefix = Hex.decode0x(privatePrefix); 24 | this.publicPrefix = Hex.decode0x(publicPrefix); 25 | } 26 | 27 | public byte[] getPrivateKeyVersion() { 28 | return privatePrefix; 29 | } 30 | 31 | public byte[] getPublicKeyVersion() { 32 | return publicPrefix; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/org/semux/crypto/bip32/util/BitUtil.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.crypto.bip32.util; 8 | 9 | public class BitUtil { 10 | 11 | /** 12 | * Checks bit value from the left, 1 based 13 | * 14 | * @param data 15 | * data 16 | * @param index 17 | * index to check 18 | * @return true if set 19 | */ 20 | public static boolean checkBit(byte data, int index) { 21 | byte bit = (byte) ((data >> (8 - index)) & 1); 22 | return bit == 0x1; 23 | } 24 | 25 | /** 26 | * Set a bit of a byte 27 | * 28 | * @param data 29 | * data 30 | * @param index 31 | * index to set 32 | * @return byte with bit set 33 | */ 34 | public static byte setBit(byte data, int index) { 35 | data |= 1 << (8 - index); 36 | return data; 37 | } 38 | 39 | /** 40 | * Unset a bit of a byte 41 | * 42 | * @param data 43 | * data 44 | * @param index 45 | * index to clear 46 | * @return byte with bit unset 47 | */ 48 | public static byte unsetBit(byte data, int index) { 49 | data &= ~(1 << (8 - index)); 50 | return data; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/org/semux/crypto/bip39/Dictionary.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.crypto.bip39; 8 | 9 | import java.io.BufferedReader; 10 | import java.io.IOException; 11 | import java.io.InputStream; 12 | import java.io.InputStreamReader; 13 | import java.nio.charset.StandardCharsets; 14 | import java.util.ArrayList; 15 | import java.util.List; 16 | import java.util.Objects; 17 | 18 | public class Dictionary { 19 | 20 | private List words = new ArrayList<>(); 21 | 22 | public Dictionary(Language language) throws IOException { 23 | 24 | InputStream wordStream = this.getClass().getClassLoader() 25 | .getResourceAsStream("wordlists/" + language.name().toLowerCase() + ".txt"); 26 | 27 | BufferedReader reader = new BufferedReader( 28 | new InputStreamReader(Objects.requireNonNull(wordStream), StandardCharsets.UTF_8)); 29 | String word; 30 | 31 | while ((word = reader.readLine()) != null) { 32 | words.add(word); 33 | } 34 | } 35 | 36 | public String getWord(int wordIdx) { 37 | return words.get(wordIdx); 38 | } 39 | 40 | public int indexOf(String word) { 41 | return words.indexOf(word); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/org/semux/crypto/bip39/Language.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.crypto.bip39; 8 | 9 | public enum Language { 10 | ENGLISH, FRENCH, ITALIAN, JAPANESE, KOREAN, SPANISH 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/org/semux/db/DatabaseFactory.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.db; 8 | 9 | import static java.nio.file.StandardCopyOption.ATOMIC_MOVE; 10 | import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; 11 | 12 | import java.io.IOException; 13 | import java.nio.file.Files; 14 | import java.nio.file.Path; 15 | 16 | public interface DatabaseFactory { 17 | /** 18 | * Returns a KVDB instance for the specified database. 19 | * 20 | * @param name 21 | * @return 22 | */ 23 | Database getDB(DatabaseName name); 24 | 25 | /** 26 | * Close all opened resources. 27 | */ 28 | void close(); 29 | 30 | /** 31 | * Returns the data directory of created databases. 32 | * 33 | * @return 34 | */ 35 | Path getDataDir(); 36 | 37 | /** 38 | * @param path 39 | * the destination path. 40 | */ 41 | default void moveTo(Path path) throws IOException { 42 | Files.move(getDataDir(), path, REPLACE_EXISTING, ATOMIC_MOVE); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/org/semux/db/DatabaseName.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.db; 8 | 9 | public enum DatabaseName { 10 | 11 | /** 12 | * Block and transaction index. 13 | */ 14 | INDEX, 15 | 16 | /** 17 | * Block raw data. 18 | */ 19 | BLOCK, 20 | 21 | /** 22 | * Account related data. 23 | */ 24 | ACCOUNT, 25 | 26 | /** 27 | * Delegate core data. 28 | */ 29 | DELEGATE, 30 | 31 | /** 32 | * Delegate vote data. 33 | */ 34 | VOTE 35 | } -------------------------------------------------------------------------------- /src/main/java/org/semux/db/exception/DatabaseException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.db.exception; 8 | 9 | public class DatabaseException extends RuntimeException { 10 | private static final long serialVersionUID = 1L; 11 | 12 | public DatabaseException() { 13 | } 14 | 15 | public DatabaseException(String s) { 16 | super(s); 17 | } 18 | 19 | public DatabaseException(String s, Throwable throwable) { 20 | super(s, throwable); 21 | } 22 | 23 | public DatabaseException(Throwable throwable) { 24 | super(throwable); 25 | } 26 | 27 | public DatabaseException(String s, Throwable throwable, boolean b, boolean b1) { 28 | super(s, throwable, b, b1); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/org/semux/event/KernelBootingEvent.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.event; 8 | 9 | public class KernelBootingEvent implements PubSubEvent { 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/org/semux/event/PubSubEvent.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.event; 8 | 9 | public interface PubSubEvent { 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/org/semux/event/PubSubFactory.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.event; 8 | 9 | import java.util.concurrent.ConcurrentHashMap; 10 | 11 | public class PubSubFactory { 12 | 13 | private static final PubSub defaultInstance = new PubSub("default"); 14 | 15 | private static final ConcurrentHashMap instances = new ConcurrentHashMap<>(); 16 | 17 | private PubSubFactory() { 18 | } 19 | 20 | public static PubSub getDefault() { 21 | return defaultInstance; 22 | } 23 | 24 | public static PubSub get(String name) { 25 | return instances.computeIfAbsent(name, PubSub::new); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/org/semux/event/PubSubSubscriber.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.event; 8 | 9 | public interface PubSubSubscriber { 10 | 11 | void onPubSubEvent(PubSubEvent event); 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/org/semux/exception/LauncherException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.exception; 8 | 9 | public class LauncherException extends RuntimeException { 10 | 11 | private static final long serialVersionUID = 1L; 12 | 13 | public LauncherException() { 14 | } 15 | 16 | public LauncherException(String message) { 17 | super(message); 18 | } 19 | 20 | public LauncherException(String message, Throwable cause) { 21 | super(message, cause); 22 | } 23 | 24 | public LauncherException(Throwable cause) { 25 | super(cause); 26 | } 27 | 28 | public LauncherException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { 29 | super(message, cause, enableSuppression, writableStackTrace); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/org/semux/gui/Action.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.gui; 8 | 9 | public enum Action { 10 | CREATE_ACCOUNT, 11 | 12 | RECOVER_ACCOUNTS, 13 | 14 | RESET_MNEMONIC, 15 | 16 | SCAN_HD_WALLETS, 17 | 18 | OK, 19 | 20 | CANCEL, 21 | 22 | SHOW_HOME, 23 | 24 | SHOW_SEND, 25 | 26 | SHOW_RECEIVE, 27 | 28 | SHOW_CONTRACT, 29 | 30 | SHOW_TRANSACTIONS, 31 | 32 | SHOW_DELEGATES, 33 | 34 | LOCK, 35 | 36 | SEND, 37 | 38 | CLEAR, 39 | 40 | COPY_ADDRESS, 41 | 42 | NEW_ACCOUNT, 43 | 44 | DELETE_ACCOUNT, 45 | 46 | VOTE, 47 | 48 | UNVOTE, 49 | 50 | DELEGATE, 51 | 52 | REFRESH, 53 | 54 | SELECT_ACCOUNT, 55 | 56 | SELECT_DELEGATE, 57 | 58 | BACKUP_WALLET, 59 | 60 | CHANGE_PASSWORD, 61 | 62 | EXIT, 63 | 64 | ABOUT, 65 | 66 | CONSOLE, 67 | 68 | API_EXPLORER, 69 | 70 | HELP, 71 | 72 | SHOW_ADDRESS_BOOK, 73 | 74 | ADD_ADDRESS, 75 | 76 | EDIT_ADDRESS, 77 | 78 | DELETE_ADDRESS, 79 | 80 | IMPORT_PRIVATE_KEY, 81 | 82 | EXPORT_PRIVATE_KEY, 83 | 84 | COPY_PRIVATE_KEY, 85 | 86 | SHOW_TRANSACTION_RESULT 87 | } 88 | -------------------------------------------------------------------------------- /src/main/java/org/semux/gui/ComboBoxItem.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.gui; 8 | 9 | import java.util.Objects; 10 | 11 | public class ComboBoxItem implements Comparable> { 12 | private final String displayName; 13 | private final T value; 14 | 15 | public ComboBoxItem(String displayName, T value) { 16 | this.displayName = displayName; 17 | this.value = value; 18 | } 19 | 20 | public String getDisplayName() { 21 | return displayName; 22 | } 23 | 24 | public T getValue() { 25 | return value; 26 | } 27 | 28 | @Override 29 | public String toString() { 30 | return displayName; 31 | } 32 | 33 | @Override 34 | public boolean equals(Object o) { 35 | if (this == o) 36 | return true; 37 | if (o == null || getClass() != o.getClass()) 38 | return false; 39 | 40 | ComboBoxItem that = (ComboBoxItem) o; 41 | 42 | return Objects.equals(displayName, that.displayName); 43 | } 44 | 45 | @Override 46 | public int hashCode() { 47 | return displayName != null ? displayName.hashCode() : 0; 48 | } 49 | 50 | @Override 51 | public int compareTo(ComboBoxItem o) { 52 | return displayName.compareTo(o.displayName); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/org/semux/gui/HorizontalSeparator.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.gui; 8 | 9 | import java.awt.Color; 10 | import java.awt.Graphics; 11 | 12 | import javax.swing.JComponent; 13 | 14 | public class HorizontalSeparator extends JComponent { 15 | 16 | private static final long serialVersionUID = -5330205221592957711L; 17 | 18 | private final Color leftColor; 19 | private final Color rightColor; 20 | 21 | public HorizontalSeparator() { 22 | this.leftColor = Color.GRAY; 23 | this.rightColor = Color.WHITE; 24 | setOpaque(false); 25 | } 26 | 27 | @Override 28 | protected void paintComponent(Graphics g) { 29 | g.setColor(leftColor); 30 | g.drawLine(0, 0, getWidth(), 0); 31 | g.setColor(rightColor); 32 | g.drawLine(0, 1, getWidth(), 1); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/org/semux/gui/PlaceHolder.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.gui; 8 | 9 | import javax.swing.text.JTextComponent; 10 | 11 | /** 12 | * Placeholder of a Swing text component based on ${@link TextPrompt}. 13 | */ 14 | public class PlaceHolder extends TextPrompt { 15 | 16 | private static final long serialVersionUID = -1350764114359129512L; 17 | 18 | public PlaceHolder(String text, JTextComponent component) { 19 | super(text, component); 20 | changeAlpha(0.5f); 21 | } 22 | } -------------------------------------------------------------------------------- /src/main/java/org/semux/gui/TextContextMenuItem.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.gui; 8 | 9 | import javax.swing.text.DefaultEditorKit; 10 | import javax.swing.text.TextAction; 11 | 12 | import org.semux.message.GuiMessages; 13 | import org.semux.util.exception.UnreachableException; 14 | 15 | /** 16 | * This enum maintains mappings of text context menu. 17 | */ 18 | enum TextContextMenuItem { 19 | CUT, COPY, PASTE; 20 | 21 | public TextAction toAction() { 22 | switch (this) { 23 | case CUT: 24 | return new DefaultEditorKit.CutAction(); 25 | case COPY: 26 | return new DefaultEditorKit.CopyAction(); 27 | case PASTE: 28 | return new DefaultEditorKit.PasteAction(); 29 | } 30 | throw new UnreachableException(); 31 | } 32 | 33 | @Override 34 | public String toString() { 35 | switch (this) { 36 | case CUT: 37 | return GuiMessages.get("Cut"); 38 | case COPY: 39 | return GuiMessages.get("Copy"); 40 | case PASTE: 41 | return GuiMessages.get("Paste"); 42 | } 43 | throw new UnreachableException(); 44 | } 45 | } -------------------------------------------------------------------------------- /src/main/java/org/semux/gui/TransactionSender.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.gui; 8 | 9 | import org.semux.Kernel; 10 | import org.semux.Network; 11 | import org.semux.core.Amount; 12 | import org.semux.core.PendingManager; 13 | import org.semux.core.Transaction; 14 | import org.semux.core.TransactionType; 15 | import org.semux.gui.model.WalletAccount; 16 | import org.semux.util.TimeUtil; 17 | 18 | public class TransactionSender { 19 | 20 | public static PendingManager.ProcessingResult send(Kernel kernel, WalletAccount account, TransactionType type, 21 | byte[] to, Amount value, Amount fee, byte[] data) { 22 | return send(kernel, account, type, to, value, fee, data, 0, Amount.ZERO); 23 | } 24 | 25 | public static PendingManager.ProcessingResult send(Kernel kernel, WalletAccount account, TransactionType type, 26 | byte[] to, Amount value, Amount fee, byte[] data, long gas, Amount gasPrice) { 27 | PendingManager pendingMgr = kernel.getPendingManager(); 28 | 29 | Network network = kernel.getConfig().network(); 30 | byte[] from = account.getKey().toAddress(); 31 | long nonce = pendingMgr.getNonce(from); 32 | long timestamp = TimeUtil.currentTimeMillis(); 33 | Transaction tx = new Transaction(network, type, to, value, fee, nonce, timestamp, data, gas, gasPrice); 34 | tx.sign(account.getKey()); 35 | 36 | return pendingMgr.addTransactionSync(tx); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/org/semux/gui/VerticalSeparator.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.gui; 8 | 9 | import java.awt.Color; 10 | import java.awt.Graphics; 11 | 12 | import javax.swing.JComponent; 13 | 14 | public class VerticalSeparator extends JComponent { 15 | 16 | private static final long serialVersionUID = -5802537037684892071L; 17 | 18 | private final Color leftColor; 19 | private final Color rightColor; 20 | 21 | public VerticalSeparator() { 22 | this.leftColor = Color.GRAY; 23 | this.rightColor = Color.WHITE; 24 | setOpaque(false); 25 | } 26 | 27 | @Override 28 | protected void paintComponent(Graphics g) { 29 | g.setColor(leftColor); 30 | g.drawLine(0, 0, 0, getHeight()); 31 | g.setColor(rightColor); 32 | g.drawLine(1, 0, 1, getHeight()); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/org/semux/gui/event/MainFrameStartedEvent.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.gui.event; 8 | 9 | import org.semux.event.PubSubEvent; 10 | 11 | public class MainFrameStartedEvent implements PubSubEvent { 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/org/semux/gui/event/WalletSelectionDialogShownEvent.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.gui.event; 8 | 9 | import org.semux.event.PubSubEvent; 10 | 11 | public class WalletSelectionDialogShownEvent implements PubSubEvent { 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/org/semux/message/CliMessages.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.message; 8 | 9 | import java.util.ResourceBundle; 10 | 11 | public final class CliMessages { 12 | 13 | private static final ResourceBundle RESOURCES = ResourceBundles.getDefaultBundle(ResourceBundles.CLI_MESSAGES); 14 | 15 | private CliMessages() { 16 | } 17 | 18 | public static String get(String key, Object... args) { 19 | return MessageFormatter.get(RESOURCES, key, args); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/org/semux/message/GuiMessages.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.message; 8 | 9 | import java.util.ResourceBundle; 10 | 11 | public final class GuiMessages { 12 | 13 | private static final ResourceBundle RESOURCES = ResourceBundles.getDefaultBundle(ResourceBundles.GUI_MESSAGES); 14 | 15 | private GuiMessages() { 16 | } 17 | 18 | public static String get(String key, Object... args) { 19 | return MessageFormatter.get(RESOURCES, key, args); 20 | } 21 | } -------------------------------------------------------------------------------- /src/main/java/org/semux/message/MessageFormatter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.message; 8 | 9 | import java.text.MessageFormat; 10 | import java.util.ResourceBundle; 11 | 12 | public final class MessageFormatter { 13 | 14 | private MessageFormatter() { 15 | } 16 | 17 | /** 18 | * Gets a value from this bundle for the given {@code key}. Any second arguments 19 | * will be used to format the value. 20 | * 21 | * @param resourceBundle 22 | * the resource bundle of messages 23 | * @param key 24 | * the bundle key 25 | * @param args 26 | * objects used to format the value. 27 | * @return the formatted value for the given key. 28 | */ 29 | public static String get(ResourceBundle resourceBundle, String key, Object... args) { 30 | String value = resourceBundle.getString(key); 31 | return MessageFormat.format(value, args); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/org/semux/message/ResourceBundles.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.message; 8 | 9 | import java.util.Locale; 10 | import java.util.ResourceBundle; 11 | 12 | /** 13 | * This enum encapsulates available resource bundles of messages and an utility 14 | * function getDefaultBundle for deciding the default locale 15 | * 16 | *

17 | * The locale used is the current value of the default locale for this instance 18 | * of the Java Virtual Machine. 19 | *

20 | */ 21 | public enum ResourceBundles { 22 | 23 | GUI_MESSAGES("org/semux/gui/messages"), CLI_MESSAGES("org/semux/cli/messages"); 24 | 25 | private final String bundleName; 26 | 27 | ResourceBundles(String bundleName) { 28 | this.bundleName = bundleName; 29 | } 30 | 31 | public String getBundleName() { 32 | return bundleName; 33 | } 34 | 35 | public static ResourceBundle getDefaultBundle(ResourceBundles bundleName) { 36 | ResourceBundle defaultBundle = ResourceBundle.getBundle(bundleName.getBundleName(), Locale.getDefault()); 37 | return defaultBundle == null ? ResourceBundle.getBundle(bundleName.getBundleName(), Locale.ENGLISH) 38 | : defaultBundle; 39 | } 40 | 41 | @Override 42 | public String toString() { 43 | return bundleName; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/org/semux/net/Capability.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.net; 8 | 9 | import org.semux.net.msg.ReasonCode; 10 | 11 | /** 12 | * This enum represents the available capabilities in current version of Semux 13 | * wallet. One peer should be disconnected by 14 | * ${@link ReasonCode#BAD_NETWORK_VERSION} if the peer doesn't support the 15 | * required set of capabilities. 16 | */ 17 | public enum Capability { 18 | 19 | // NOTE: legacy issues 20 | // 1) Different names have been used historically; 21 | // 2) Old handshake messages first converts String to Capability, and then to 22 | // Capability set; 23 | // 3) Unknown capability will lead to INVALID_HANDSHAKE; 24 | // 4) Unsorted capability set will lead to INVALID_HANDSHAKE. 25 | 26 | /** 27 | * Mandatory for all network. 28 | */ 29 | SEMUX, 30 | 31 | /** 32 | * This client supports the FAST_SYNC protocol. 33 | */ 34 | FAST_SYNC, 35 | 36 | /** 37 | * This client supports the LIGHT protocol. 38 | */ 39 | LIGHT; 40 | 41 | public static Capability of(String name) { 42 | try { 43 | return valueOf(name); 44 | } catch (IllegalArgumentException | NullPointerException ex) { 45 | return null; 46 | } 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/org/semux/net/filter/exception/IpFilterJsonParseException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.net.filter.exception; 8 | 9 | public class IpFilterJsonParseException extends IllegalArgumentException { 10 | 11 | private static final long serialVersionUID = 1L; 12 | 13 | public IpFilterJsonParseException() { 14 | } 15 | 16 | public IpFilterJsonParseException(String s) { 17 | super(s); 18 | } 19 | 20 | public IpFilterJsonParseException(String message, Throwable cause) { 21 | super(message, cause); 22 | } 23 | 24 | public IpFilterJsonParseException(Throwable cause) { 25 | super(cause); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/org/semux/net/msg/MessageException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.net.msg; 8 | 9 | import java.io.IOException; 10 | 11 | public class MessageException extends IOException { 12 | private static final long serialVersionUID = 1L; 13 | 14 | public MessageException() { 15 | } 16 | 17 | public MessageException(String s) { 18 | super(s); 19 | } 20 | 21 | public MessageException(String s, Throwable throwable) { 22 | super(s, throwable); 23 | } 24 | 25 | public MessageException(Throwable throwable) { 26 | super(throwable); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/org/semux/net/msg/consensus/BlockHeaderMessage.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.net.msg.consensus; 8 | 9 | import org.semux.core.BlockHeader; 10 | import org.semux.net.msg.Message; 11 | import org.semux.net.msg.MessageCode; 12 | 13 | public class BlockHeaderMessage extends Message { 14 | 15 | private final BlockHeader header; 16 | 17 | public BlockHeaderMessage(BlockHeader header) { 18 | super(MessageCode.BLOCK_HEADER, null); 19 | 20 | this.header = header; 21 | 22 | this.body = header.toBytes(); 23 | } 24 | 25 | public BlockHeaderMessage(byte[] body) { 26 | super(MessageCode.BLOCK_HEADER, null); 27 | 28 | this.header = BlockHeader.fromBytes(body); 29 | 30 | this.body = body; 31 | } 32 | 33 | public BlockHeader getHeader() { 34 | return header; 35 | } 36 | 37 | @Override 38 | public String toString() { 39 | return "BlockHeaderMessage [header=" + header + "]"; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/org/semux/net/msg/consensus/BlockMessage.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.net.msg.consensus; 8 | 9 | import org.semux.core.Block; 10 | import org.semux.net.msg.Message; 11 | import org.semux.net.msg.MessageCode; 12 | 13 | public class BlockMessage extends Message { 14 | 15 | private final Block block; 16 | 17 | public BlockMessage(Block block) { 18 | super(MessageCode.BLOCK, null); 19 | 20 | this.block = block; 21 | 22 | this.body = block.toBytes(); 23 | } 24 | 25 | public BlockMessage(byte[] body) { 26 | super(MessageCode.BLOCK, null); 27 | 28 | this.block = Block.fromBytes(body); 29 | 30 | this.body = body; 31 | } 32 | 33 | public Block getBlock() { 34 | return block; 35 | } 36 | 37 | @Override 38 | public String toString() { 39 | return "BlockMessage [block=" + block + "]"; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/org/semux/net/msg/consensus/GetBlockHeaderMessage.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.net.msg.consensus; 8 | 9 | import org.semux.net.msg.Message; 10 | import org.semux.net.msg.MessageCode; 11 | import org.semux.util.SimpleDecoder; 12 | import org.semux.util.SimpleEncoder; 13 | 14 | public class GetBlockHeaderMessage extends Message { 15 | 16 | private final long number; 17 | 18 | public GetBlockHeaderMessage(long number) { 19 | super(MessageCode.GET_BLOCK_HEADER, BlockHeaderMessage.class); 20 | 21 | this.number = number; 22 | 23 | SimpleEncoder enc = new SimpleEncoder(); 24 | enc.writeLong(number); 25 | this.body = enc.toBytes(); 26 | } 27 | 28 | public GetBlockHeaderMessage(byte[] body) { 29 | super(MessageCode.GET_BLOCK_HEADER, BlockHeaderMessage.class); 30 | 31 | SimpleDecoder dec = new SimpleDecoder(body); 32 | this.number = dec.readLong(); 33 | 34 | this.body = body; 35 | } 36 | 37 | public long getNumber() { 38 | return number; 39 | } 40 | 41 | @Override 42 | public String toString() { 43 | return "GetBlockHeaderMessage [number=" + number + "]"; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/org/semux/net/msg/consensus/GetBlockMessage.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.net.msg.consensus; 8 | 9 | import org.semux.net.msg.Message; 10 | import org.semux.net.msg.MessageCode; 11 | import org.semux.util.SimpleDecoder; 12 | import org.semux.util.SimpleEncoder; 13 | 14 | public class GetBlockMessage extends Message { 15 | 16 | private final long number; 17 | 18 | public GetBlockMessage(long number) { 19 | super(MessageCode.GET_BLOCK, BlockMessage.class); 20 | this.number = number; 21 | 22 | SimpleEncoder enc = new SimpleEncoder(); 23 | enc.writeLong(number); 24 | this.body = enc.toBytes(); 25 | } 26 | 27 | public GetBlockMessage(byte[] body) { 28 | super(MessageCode.GET_BLOCK, BlockMessage.class); 29 | 30 | SimpleDecoder dec = new SimpleDecoder(body); 31 | this.number = dec.readLong(); 32 | 33 | this.body = body; 34 | } 35 | 36 | public long getNumber() { 37 | return number; 38 | } 39 | 40 | @Override 41 | public String toString() { 42 | return "GetBlockMessage [number=" + number + "]"; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/org/semux/net/msg/consensus/GetBlockPartsMessage.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.net.msg.consensus; 8 | 9 | import org.semux.net.msg.Message; 10 | import org.semux.net.msg.MessageCode; 11 | import org.semux.util.SimpleDecoder; 12 | import org.semux.util.SimpleEncoder; 13 | 14 | public class GetBlockPartsMessage extends Message { 15 | 16 | private final long number; 17 | private final int parts; 18 | 19 | public GetBlockPartsMessage(long number, int parts) { 20 | super(MessageCode.GET_BLOCK_PARTS, BlockPartsMessage.class); 21 | 22 | this.number = number; 23 | this.parts = parts; 24 | 25 | SimpleEncoder enc = new SimpleEncoder(); 26 | enc.writeLong(number); 27 | enc.writeInt(parts); 28 | this.body = enc.toBytes(); 29 | } 30 | 31 | public GetBlockPartsMessage(byte[] body) { 32 | super(MessageCode.GET_BLOCK_PARTS, BlockPartsMessage.class); 33 | 34 | SimpleDecoder dec = new SimpleDecoder(body); 35 | this.number = dec.readLong(); 36 | this.parts = dec.readInt(); 37 | 38 | this.body = body; 39 | } 40 | 41 | public long getNumber() { 42 | return number; 43 | } 44 | 45 | public int getParts() { 46 | return parts; 47 | } 48 | 49 | @Override 50 | public String toString() { 51 | return "GetBlockPartsMessage [number=" + number + ", parts = " + parts + "]"; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/org/semux/net/msg/consensus/NewHeightMessage.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.net.msg.consensus; 8 | 9 | import org.semux.net.msg.Message; 10 | import org.semux.net.msg.MessageCode; 11 | import org.semux.util.SimpleDecoder; 12 | import org.semux.util.SimpleEncoder; 13 | 14 | public class NewHeightMessage extends Message { 15 | 16 | private final long height; 17 | 18 | public NewHeightMessage(long height) { 19 | super(MessageCode.BFT_NEW_HEIGHT, null); 20 | this.height = height; 21 | 22 | SimpleEncoder enc = new SimpleEncoder(); 23 | enc.writeLong(height); 24 | this.body = enc.toBytes(); 25 | } 26 | 27 | public NewHeightMessage(byte[] body) { 28 | super(MessageCode.BFT_NEW_HEIGHT, null); 29 | 30 | SimpleDecoder dec = new SimpleDecoder(body); 31 | this.height = dec.readLong(); 32 | 33 | this.body = body; 34 | } 35 | 36 | public long getHeight() { 37 | return height; 38 | } 39 | 40 | @Override 41 | public String toString() { 42 | return "BFTNewHeightMessage [height=" + height + "]"; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/org/semux/net/msg/consensus/NewViewMessage.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.net.msg.consensus; 8 | 9 | import org.semux.consensus.Proof; 10 | import org.semux.net.msg.Message; 11 | import org.semux.net.msg.MessageCode; 12 | 13 | public class NewViewMessage extends Message { 14 | 15 | private final Proof proof; 16 | 17 | public NewViewMessage(Proof proof) { 18 | super(MessageCode.BFT_NEW_VIEW, null); 19 | 20 | this.proof = proof; 21 | 22 | // TODO: consider wrapping by simple codec 23 | this.body = proof.toBytes(); 24 | } 25 | 26 | public NewViewMessage(byte[] body) { 27 | super(MessageCode.BFT_NEW_VIEW, null); 28 | 29 | this.proof = Proof.fromBytes(body); 30 | 31 | this.body = body; 32 | } 33 | 34 | public Proof getProof() { 35 | return proof; 36 | } 37 | 38 | public long getHeight() { 39 | return proof.getHeight(); 40 | } 41 | 42 | public int getView() { 43 | return proof.getView(); 44 | } 45 | 46 | @Override 47 | public String toString() { 48 | return "BFTNewViewMessage [proof=" + proof + "]"; 49 | } 50 | } -------------------------------------------------------------------------------- /src/main/java/org/semux/net/msg/consensus/ProposalMessage.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.net.msg.consensus; 8 | 9 | import org.semux.consensus.Proposal; 10 | import org.semux.net.msg.Message; 11 | import org.semux.net.msg.MessageCode; 12 | 13 | public class ProposalMessage extends Message { 14 | 15 | private final Proposal proposal; 16 | 17 | public ProposalMessage(Proposal proposal) { 18 | super(MessageCode.BFT_PROPOSAL, null); 19 | 20 | this.proposal = proposal; 21 | 22 | // TODO: consider wrapping by simple codec 23 | this.body = proposal.toBytes(); 24 | } 25 | 26 | public ProposalMessage(byte[] body) { 27 | super(MessageCode.BFT_PROPOSAL, null); 28 | 29 | this.proposal = Proposal.fromBytes(body); 30 | 31 | this.body = body; 32 | } 33 | 34 | public Proposal getProposal() { 35 | return proposal; 36 | } 37 | 38 | @Override 39 | public String toString() { 40 | return "BFTProposalMessage: " + proposal; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/org/semux/net/msg/consensus/VoteMessage.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.net.msg.consensus; 8 | 9 | import org.semux.consensus.Vote; 10 | import org.semux.net.msg.Message; 11 | import org.semux.net.msg.MessageCode; 12 | 13 | public class VoteMessage extends Message { 14 | 15 | private final Vote vote; 16 | 17 | public VoteMessage(Vote vote) { 18 | super(MessageCode.BFT_VOTE, null); 19 | 20 | this.vote = vote; 21 | 22 | // TODO: consider wrapping by simple codec 23 | this.body = vote.toBytes(); 24 | } 25 | 26 | public VoteMessage(byte[] body) { 27 | super(MessageCode.BFT_VOTE, null); 28 | 29 | this.vote = Vote.fromBytes(body); 30 | 31 | this.body = body; 32 | } 33 | 34 | public Vote getVote() { 35 | return vote; 36 | } 37 | 38 | @Override 39 | public String toString() { 40 | return "BFTVoteMessage: " + vote; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/org/semux/net/msg/p2p/DisconnectMessage.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.net.msg.p2p; 8 | 9 | import org.semux.net.msg.Message; 10 | import org.semux.net.msg.MessageCode; 11 | import org.semux.net.msg.ReasonCode; 12 | import org.semux.util.SimpleDecoder; 13 | import org.semux.util.SimpleEncoder; 14 | 15 | public class DisconnectMessage extends Message { 16 | 17 | private final ReasonCode reason; 18 | 19 | /** 20 | * Create a DISCONNECT message. 21 | * 22 | * @param reason 23 | */ 24 | public DisconnectMessage(ReasonCode reason) { 25 | super(MessageCode.DISCONNECT, null); 26 | 27 | this.reason = reason; 28 | 29 | SimpleEncoder enc = new SimpleEncoder(); 30 | enc.writeByte(reason.toByte()); 31 | this.body = enc.toBytes(); 32 | } 33 | 34 | /** 35 | * Parse a DISCONNECT message from byte array. 36 | * 37 | * @param body 38 | */ 39 | public DisconnectMessage(byte[] body) { 40 | super(MessageCode.DISCONNECT, null); 41 | 42 | SimpleDecoder dec = new SimpleDecoder(body); 43 | this.reason = ReasonCode.of(dec.readByte()); 44 | 45 | this.body = body; 46 | } 47 | 48 | public ReasonCode getReason() { 49 | return reason; 50 | } 51 | 52 | @Override 53 | public String toString() { 54 | return "DisconnectMessage [reason=" + reason + "]"; 55 | } 56 | } -------------------------------------------------------------------------------- /src/main/java/org/semux/net/msg/p2p/GetNodesMessage.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.net.msg.p2p; 8 | 9 | import org.semux.net.msg.Message; 10 | import org.semux.net.msg.MessageCode; 11 | import org.semux.util.SimpleEncoder; 12 | 13 | // NOTE: GetNodesMessage is encoded into a single empty frame. 14 | 15 | public class GetNodesMessage extends Message { 16 | 17 | /** 18 | * Create a GET_NODES message. 19 | * 20 | */ 21 | public GetNodesMessage() { 22 | super(MessageCode.GET_NODES, NodesMessage.class); 23 | 24 | SimpleEncoder enc = new SimpleEncoder(); 25 | this.body = enc.toBytes(); 26 | } 27 | 28 | /** 29 | * Parse a GET_NODES message from byte array. 30 | * 31 | * @param body 32 | */ 33 | public GetNodesMessage(byte[] body) { 34 | super(MessageCode.GET_NODES, NodesMessage.class); 35 | 36 | this.body = body; 37 | } 38 | 39 | @Override 40 | public String toString() { 41 | return "GetNodesMessage"; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/org/semux/net/msg/p2p/PingMessage.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.net.msg.p2p; 8 | 9 | import org.semux.net.msg.Message; 10 | import org.semux.net.msg.MessageCode; 11 | import org.semux.util.SimpleDecoder; 12 | import org.semux.util.SimpleEncoder; 13 | import org.semux.util.TimeUtil; 14 | 15 | public class PingMessage extends Message { 16 | 17 | private final long timestamp; 18 | 19 | /** 20 | * Create a PING message. 21 | * 22 | */ 23 | public PingMessage() { 24 | super(MessageCode.PING, PongMessage.class); 25 | 26 | this.timestamp = TimeUtil.currentTimeMillis(); 27 | 28 | SimpleEncoder enc = new SimpleEncoder(); 29 | enc.writeLong(timestamp); 30 | this.body = enc.toBytes(); 31 | } 32 | 33 | /** 34 | * Parse a PING message from byte array. 35 | * 36 | * @param body 37 | */ 38 | public PingMessage(byte[] body) { 39 | super(MessageCode.PING, PongMessage.class); 40 | 41 | SimpleDecoder dec = new SimpleDecoder(body); 42 | this.timestamp = dec.readLong(); 43 | 44 | this.body = body; 45 | } 46 | 47 | @Override 48 | public String toString() { 49 | return "PingMessage [timestamp=" + timestamp + "]"; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/org/semux/net/msg/p2p/PongMessage.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.net.msg.p2p; 8 | 9 | import org.semux.net.msg.Message; 10 | import org.semux.net.msg.MessageCode; 11 | import org.semux.util.SimpleDecoder; 12 | import org.semux.util.SimpleEncoder; 13 | import org.semux.util.TimeUtil; 14 | 15 | public class PongMessage extends Message { 16 | 17 | private final long timestamp; 18 | 19 | /** 20 | * Create a PONG message. 21 | */ 22 | public PongMessage() { 23 | super(MessageCode.PONG, null); 24 | 25 | this.timestamp = TimeUtil.currentTimeMillis(); 26 | 27 | SimpleEncoder enc = new SimpleEncoder(); 28 | enc.writeLong(timestamp); 29 | this.body = enc.toBytes(); 30 | } 31 | 32 | /** 33 | * Parse a PONG message from byte array. 34 | * 35 | * @param body 36 | */ 37 | public PongMessage(byte[] body) { 38 | super(MessageCode.PONG, null); 39 | 40 | SimpleDecoder dec = new SimpleDecoder(body); 41 | this.timestamp = dec.readLong(); 42 | 43 | this.body = body; 44 | } 45 | 46 | @Override 47 | public String toString() { 48 | return "PongMessage [timestamp=" + timestamp + "]"; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/org/semux/net/msg/p2p/TransactionMessage.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.net.msg.p2p; 8 | 9 | import org.semux.core.Transaction; 10 | import org.semux.net.msg.Message; 11 | import org.semux.net.msg.MessageCode; 12 | 13 | public class TransactionMessage extends Message { 14 | 15 | private final Transaction transaction; 16 | 17 | /** 18 | * Create a TRANSACTION message. 19 | * 20 | */ 21 | public TransactionMessage(Transaction transaction) { 22 | super(MessageCode.TRANSACTION, null); 23 | 24 | this.transaction = transaction; 25 | 26 | // TODO: consider wrapping by simple codec 27 | this.body = transaction.toBytes(); 28 | } 29 | 30 | /** 31 | * Parse a TRANSACTION message from byte array. 32 | * 33 | * @param body 34 | */ 35 | public TransactionMessage(byte[] body) { 36 | super(MessageCode.TRANSACTION, null); 37 | 38 | this.transaction = Transaction.fromBytes(body); 39 | 40 | this.body = body; 41 | } 42 | 43 | public Transaction getTransaction() { 44 | return transaction; 45 | } 46 | 47 | @Override 48 | public String toString() { 49 | return "TransactionMessage [tx=" + transaction + "]"; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/org/semux/net/msg/p2p/handshake/v1/HelloMessage.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.net.msg.p2p.handshake.v1; 8 | 9 | import org.semux.Network; 10 | import org.semux.crypto.Key; 11 | import org.semux.net.msg.MessageCode; 12 | 13 | public class HelloMessage extends HandshakeMessage { 14 | 15 | /** 16 | * Create a HELLO message. 17 | */ 18 | public HelloMessage(Network network, short networkVersion, String peerId, String ip, int port, 19 | String clientId, long latestBlockNumber, Key coinbase) { 20 | super(MessageCode.HELLO, WorldMessage.class, 21 | network, networkVersion, peerId, ip, port, clientId, 22 | latestBlockNumber, coinbase); 23 | } 24 | 25 | /** 26 | * Parse a HELLO message from byte array. 27 | * 28 | * @param body 29 | */ 30 | public HelloMessage(byte[] body) { 31 | super(MessageCode.HELLO, WorldMessage.class, body); 32 | } 33 | 34 | @Override 35 | public String toString() { 36 | return "WorldMessage [peer=" + peer + "]"; 37 | } 38 | } -------------------------------------------------------------------------------- /src/main/java/org/semux/net/msg/p2p/handshake/v1/WorldMessage.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.net.msg.p2p.handshake.v1; 8 | 9 | import org.semux.Network; 10 | import org.semux.crypto.Key; 11 | import org.semux.net.msg.MessageCode; 12 | 13 | public class WorldMessage extends HandshakeMessage { 14 | 15 | /** 16 | * Create a WORLD message. 17 | */ 18 | public WorldMessage(Network network, short networkVersion, String peerId, String ip, int port, 19 | String clientId, long latestBlockNumber, Key coinbase) { 20 | super(MessageCode.WORLD, null, 21 | network, networkVersion, peerId, ip, port, clientId, latestBlockNumber, coinbase); 22 | } 23 | 24 | /** 25 | * Parse a WORLD message from byte array. 26 | * 27 | * @param body 28 | */ 29 | public WorldMessage(byte[] body) { 30 | super(MessageCode.WORLD, null, body); 31 | } 32 | 33 | @Override 34 | public String toString() { 35 | return "WorldMessage [peer=" + peer + "]"; 36 | } 37 | } -------------------------------------------------------------------------------- /src/main/java/org/semux/net/msg/p2p/handshake/v2/HelloMessage.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.net.msg.p2p.handshake.v2; 8 | 9 | import java.util.Arrays; 10 | 11 | import org.semux.Network; 12 | import org.semux.crypto.Hex; 13 | import org.semux.crypto.Key; 14 | import org.semux.net.msg.MessageCode; 15 | 16 | public class HelloMessage extends HandshakeMessage { 17 | 18 | public HelloMessage(Network network, short networkVersion, String peerId, int port, 19 | String clientId, String[] capabilities, long latestBlockNumber, 20 | byte[] secret, Key coinbase) { 21 | super(MessageCode.HANDSHAKE_HELLO, WorldMessage.class, network, networkVersion, peerId, port, clientId, 22 | capabilities, latestBlockNumber, secret, coinbase); 23 | } 24 | 25 | public HelloMessage(byte[] encoded) { 26 | super(MessageCode.HANDSHAKE_HELLO, WorldMessage.class, encoded); 27 | } 28 | 29 | @Override 30 | public String toString() { 31 | return "HelloMessage{" + 32 | "peer=" + network + 33 | ", networkVersion=" + networkVersion + 34 | ", peerId='" + peerId + '\'' + 35 | ", port=" + port + 36 | ", clientId='" + clientId + '\'' + 37 | ", capabilities=" + Arrays.toString(capabilities) + 38 | ", latestBlockNumber=" + latestBlockNumber + 39 | ", secret=" + Hex.encode(secret) + 40 | ", timestamp=" + timestamp + 41 | '}'; 42 | } 43 | } -------------------------------------------------------------------------------- /src/main/java/org/semux/net/msg/p2p/handshake/v2/WorldMessage.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.net.msg.p2p.handshake.v2; 8 | 9 | import java.util.Arrays; 10 | 11 | import org.semux.Network; 12 | import org.semux.crypto.Hex; 13 | import org.semux.crypto.Key; 14 | import org.semux.net.msg.MessageCode; 15 | 16 | public class WorldMessage extends HandshakeMessage { 17 | 18 | public WorldMessage(Network network, short networkVersion, String peerId, int port, 19 | String clientId, String[] capabilities, long latestBlockNumber, 20 | byte[] secret, Key coinbase) { 21 | super(MessageCode.HANDSHAKE_WORLD, null, network, networkVersion, peerId, port, clientId, 22 | capabilities, latestBlockNumber, secret, coinbase); 23 | } 24 | 25 | public WorldMessage(byte[] encoded) { 26 | super(MessageCode.HANDSHAKE_WORLD, null, encoded); 27 | } 28 | 29 | @Override 30 | public String toString() { 31 | return "WorldMessage{" + 32 | "network=" + network + 33 | ", networkVersion=" + networkVersion + 34 | ", peerId='" + peerId + '\'' + 35 | ", port=" + port + 36 | ", clientId='" + clientId + '\'' + 37 | ", capabilities=" + Arrays.toString(capabilities) + 38 | ", latestBlockNumber=" + latestBlockNumber + 39 | ", secret=" + Hex.encode(secret) + 40 | ", timestamp=" + timestamp + 41 | '}'; 42 | } 43 | } -------------------------------------------------------------------------------- /src/main/java/org/semux/util/ArrayUtil.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.util; 8 | 9 | import java.util.Random; 10 | import java.util.concurrent.ThreadLocalRandom; 11 | 12 | public class ArrayUtil { 13 | 14 | /** 15 | * Generate a random permutation of [0...n) 16 | * 17 | * @param n 18 | * @return 19 | */ 20 | public static int[] permutation(int n) { 21 | int[] arr = new int[n]; 22 | for (int i = 0; i < n; i++) { 23 | arr[i] = i; 24 | } 25 | 26 | shuffle(arr); 27 | return arr; 28 | } 29 | 30 | /** 31 | * Shuffle an integer array. 32 | * 33 | * @param arr 34 | */ 35 | public static void shuffle(int[] arr) { 36 | Random r = ThreadLocalRandom.current(); 37 | 38 | for (int i = arr.length - 1; i > 0; i--) { 39 | int index = r.nextInt(i + 1); 40 | 41 | int tmp = arr[index]; 42 | arr[index] = arr[i]; 43 | arr[i] = tmp; 44 | } 45 | } 46 | 47 | private ArrayUtil() { 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/org/semux/util/BasicAuth.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.util; 8 | 9 | import java.util.Base64; 10 | 11 | import org.apache.commons.lang3.tuple.Pair; 12 | 13 | /** 14 | * Basic authentication helper. 15 | * 16 | */ 17 | public class BasicAuth { 18 | 19 | /** 20 | * Parses the username and password from the AUTHORIZATION header. 21 | * 22 | * @param auth 23 | * @return a pair of username and password if success, otherwise null 24 | */ 25 | public static Pair parseAuth(String auth) { 26 | try { 27 | if (auth != null && auth.startsWith("Basic ")) { 28 | String str = Bytes.toString(Base64.getDecoder().decode(auth.substring(6))); 29 | int idx = str.indexOf(':'); 30 | if (idx != -1) { 31 | return Pair.of(str.substring(0, idx), str.substring(idx + 1)); 32 | } 33 | } 34 | } catch (IllegalArgumentException e) { 35 | // invalid base64 string 36 | } 37 | 38 | return null; 39 | } 40 | 41 | /** 42 | * Generates the AUTHORIZATION header. 43 | * 44 | * @param username 45 | * @param password 46 | * @return 47 | */ 48 | public static String generateAuth(String username, String password) { 49 | return "Basic " + Base64.getEncoder().encodeToString(Bytes.of(username + ":" + password)); 50 | } 51 | 52 | private BasicAuth() { 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/org/semux/util/CircularFixedSizeList.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.util; 8 | 9 | import java.util.LinkedList; 10 | 11 | public class CircularFixedSizeList { 12 | 13 | private int size; 14 | int index = 0; 15 | private LinkedList values = new LinkedList<>(); 16 | 17 | public CircularFixedSizeList(int size) { 18 | super(); 19 | this.size = size; 20 | 21 | } 22 | 23 | /** 24 | * push to current location in list 25 | * 26 | * @param object 27 | * @return 28 | */ 29 | public void add(T object) { 30 | while (values.size() >= size) { 31 | values.removeLast(); 32 | } 33 | values.add(index, object); 34 | } 35 | 36 | public T forward() { 37 | if (values.isEmpty()) { 38 | return null; 39 | } 40 | index = (index + 1) % values.size(); 41 | return values.get(index); 42 | } 43 | 44 | public T back() { 45 | if (values.isEmpty()) { 46 | return null; 47 | } 48 | index = (index - 1) % values.size(); 49 | if (index < 0) { 50 | index = values.size() - 1; 51 | } 52 | return values.get(index); 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/org/semux/util/ClosableIterator.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.util; 8 | 9 | import java.util.Iterator; 10 | 11 | public interface ClosableIterator extends Iterator { 12 | 13 | /** 14 | * Closes the underlying resources for this iterator. 15 | */ 16 | void close(); 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/org/semux/util/CommandParser.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.util; 8 | 9 | import java.util.ArrayList; 10 | import java.util.List; 11 | import java.util.regex.Matcher; 12 | import java.util.regex.Pattern; 13 | 14 | public class CommandParser { 15 | 16 | public static List parseInput(String input) { 17 | List commandArguments = new ArrayList<>(); 18 | Pattern ptrn = Pattern.compile("\"[^\"\\\\]*(?:\\\\.[^\"\\\\]*)*\"|\\S+"); 19 | Matcher matcher = ptrn.matcher(input); 20 | while (matcher.find()) { 21 | commandArguments.add(sanitize(matcher.group(0))); 22 | } 23 | return commandArguments; 24 | } 25 | 26 | private static String sanitize(String value) { 27 | if (value.startsWith("\"") && value.endsWith("\"")) { 28 | value = value.substring(1, value.length() - 1); 29 | } 30 | return value.replace("\\\"", "\""); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/org/semux/util/MerkleUtil.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.util; 8 | 9 | import java.util.ArrayList; 10 | import java.util.List; 11 | 12 | import org.semux.core.Transaction; 13 | import org.semux.core.TransactionResult; 14 | import org.semux.crypto.Hash; 15 | 16 | public class MerkleUtil { 17 | 18 | /** 19 | * Compute the Merkle root of transactions. 20 | * 21 | * @param txs 22 | * transactions 23 | * @return 24 | */ 25 | public static byte[] computeTransactionsRoot(List txs) { 26 | List hashes = new ArrayList<>(); 27 | for (Transaction tx : txs) { 28 | hashes.add(tx.getHash()); 29 | } 30 | return new MerkleTree(hashes).getRootHash(); 31 | } 32 | 33 | /** 34 | * Computes the Merkle root of results. 35 | * 36 | * @param results 37 | * transaction results 38 | * @return 39 | */ 40 | public static byte[] computeResultsRoot(List results) { 41 | List hashes = new ArrayList<>(); 42 | for (TransactionResult tx : results) { 43 | hashes.add(Hash.h256(tx.toBytesForMerkle())); 44 | } 45 | return new MerkleTree(hashes).getRootHash(); 46 | } 47 | 48 | private MerkleUtil() { 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/org/semux/util/NullPrintStream.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.util; 8 | 9 | import java.io.ByteArrayOutputStream; 10 | import java.io.IOException; 11 | import java.io.OutputStream; 12 | import java.io.PrintStream; 13 | import java.io.UnsupportedEncodingException; 14 | import java.nio.charset.StandardCharsets; 15 | 16 | public class NullPrintStream extends PrintStream { 17 | 18 | public NullPrintStream() throws UnsupportedEncodingException { 19 | super(new NullByteArrayOutputStream(), false, StandardCharsets.UTF_8.name()); 20 | } 21 | 22 | private static class NullByteArrayOutputStream extends ByteArrayOutputStream { 23 | 24 | @Override 25 | public void write(int b) { 26 | // do nothing 27 | } 28 | 29 | @Override 30 | public void write(byte[] b, int off, int len) { 31 | // do nothing 32 | } 33 | 34 | @Override 35 | public void writeTo(OutputStream out) throws IOException { 36 | // do nothing 37 | } 38 | 39 | } 40 | 41 | } -------------------------------------------------------------------------------- /src/main/java/org/semux/util/StringUtil.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.util; 8 | 9 | public class StringUtil { 10 | 11 | /** 12 | * Returns if the given string is null or empty. 13 | * 14 | * @param str 15 | * @return 16 | */ 17 | public static boolean isNullOrEmpty(String str) { 18 | return str == null || str.isEmpty(); 19 | } 20 | 21 | private StringUtil() { 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/org/semux/util/exception/BytesException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.util.exception; 8 | 9 | public class BytesException extends RuntimeException { 10 | 11 | private static final long serialVersionUID = 1L; 12 | 13 | public BytesException() { 14 | } 15 | 16 | public BytesException(String s) { 17 | super(s); 18 | } 19 | 20 | public BytesException(String s, Throwable throwable) { 21 | super(s, throwable); 22 | } 23 | 24 | public BytesException(Throwable throwable) { 25 | super(throwable); 26 | } 27 | 28 | public BytesException(String s, Throwable throwable, boolean b, boolean b1) { 29 | super(s, throwable, b, b1); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/org/semux/util/exception/SimpleCodecException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.util.exception; 8 | 9 | public class SimpleCodecException extends RuntimeException { 10 | private static final long serialVersionUID = 1L; 11 | 12 | public SimpleCodecException() { 13 | } 14 | 15 | public SimpleCodecException(String s) { 16 | super(s); 17 | } 18 | 19 | public SimpleCodecException(String s, Throwable throwable) { 20 | super(s, throwable); 21 | } 22 | 23 | public SimpleCodecException(Throwable throwable) { 24 | super(throwable); 25 | } 26 | 27 | public SimpleCodecException(String s, Throwable throwable, boolean b, boolean b1) { 28 | super(s, throwable, b, b1); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/org/semux/util/exception/UnreachableException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.util.exception; 8 | 9 | public class UnreachableException extends RuntimeException { 10 | 11 | private static final long serialVersionUID = 1L; 12 | 13 | public UnreachableException() { 14 | super("Should never reach here"); 15 | } 16 | 17 | public UnreachableException(String message, Throwable cause, boolean enableSuppression, 18 | boolean writableStackTrace) { 19 | super(message, cause, enableSuppression, writableStackTrace); 20 | } 21 | 22 | public UnreachableException(String message, Throwable cause) { 23 | super(message, cause); 24 | } 25 | 26 | public UnreachableException(String message) { 27 | super(message); 28 | } 29 | 30 | public UnreachableException(Throwable cause) { 31 | super(cause); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/org/semux/vm/client/Conversion.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.vm.client; 8 | 9 | import java.math.BigInteger; 10 | 11 | import org.semux.core.Amount; 12 | import org.semux.core.Unit; 13 | 14 | /** 15 | * Conversion between ETH and SEM. The idea is to make 1 SEM = 1 ETH from a 16 | * smart contract viewpoint. 17 | */ 18 | public class Conversion { 19 | 20 | private static final BigInteger TEN_POW_NINE = BigInteger.TEN.pow(9); 21 | 22 | public static Amount weiToAmount(BigInteger value) { 23 | BigInteger nanoSEM = value.divide(TEN_POW_NINE); 24 | return Amount.of(nanoSEM.longValue(), Unit.NANO_SEM); 25 | } 26 | 27 | public static BigInteger amountToWei(Amount value) { 28 | return value.toBigInteger().multiply(TEN_POW_NINE); 29 | } 30 | 31 | public static BigInteger amountToWei(long nanoSEM) { 32 | return BigInteger.valueOf(nanoSEM).multiply(TEN_POW_NINE); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/org/semux/vm/client/SemuxBlock.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.vm.client; 8 | 9 | import java.math.BigInteger; 10 | 11 | import org.ethereum.vm.client.Block; 12 | import org.semux.core.BlockHeader; 13 | 14 | /** 15 | * Facade for BlockHeader -> Block 16 | */ 17 | public class SemuxBlock implements Block { 18 | 19 | private final long blockGasLimit; 20 | private final BlockHeader blockHeader; 21 | 22 | public SemuxBlock(BlockHeader block, long blockGasLimit) { 23 | this.blockHeader = block; 24 | this.blockGasLimit = blockGasLimit; 25 | } 26 | 27 | @Override 28 | public long getGasLimit() { 29 | return blockGasLimit; 30 | } 31 | 32 | @Override 33 | public byte[] getParentHash() { 34 | return blockHeader.getParentHash(); 35 | } 36 | 37 | @Override 38 | public byte[] getCoinbase() { 39 | return blockHeader.getCoinbase(); 40 | } 41 | 42 | @Override 43 | public long getTimestamp() { 44 | return blockHeader.getTimestamp(); 45 | } 46 | 47 | @Override 48 | public long getNumber() { 49 | return blockHeader.getNumber(); 50 | } 51 | 52 | @Override 53 | public BigInteger getDifficulty() { 54 | return BigInteger.ONE; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/main/java/org/semux/vm/client/SemuxBlockStore.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.vm.client; 8 | 9 | import org.ethereum.vm.client.BlockStore; 10 | import org.semux.core.Blockchain; 11 | 12 | /** 13 | * Facade class for Blockchain to Blockstore 14 | * 15 | * Eventually we'll want to make blockchain just implement blockstore 16 | */ 17 | public class SemuxBlockStore implements BlockStore { 18 | private final Blockchain blockchain; 19 | 20 | public SemuxBlockStore(Blockchain blockchain) { 21 | this.blockchain = blockchain; 22 | } 23 | 24 | @Override 25 | public byte[] getBlockHashByNumber(long index) { 26 | return blockchain.getBlockHeader(index).getHash(); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/org/semux/vm/client/SemuxSpec.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.vm.client; 8 | 9 | import org.ethereum.vm.chainspec.ConstantinopleSpec; 10 | import org.ethereum.vm.chainspec.PrecompiledContracts; 11 | 12 | public class SemuxSpec extends ConstantinopleSpec { 13 | 14 | private static final PrecompiledContracts precompiledContracts = new SemuxPrecompiledContracts(); 15 | 16 | @Override 17 | public PrecompiledContracts getPrecompiledContracts() { 18 | return precompiledContracts; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/org/semux/vm/client/SemuxTransaction.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.vm.client; 8 | 9 | import static org.semux.vm.client.Conversion.amountToWei; 10 | 11 | import java.math.BigInteger; 12 | 13 | import org.ethereum.vm.client.Transaction; 14 | import org.semux.core.TransactionType; 15 | 16 | /** 17 | * Facade for Transaction -> Transaction 18 | */ 19 | public class SemuxTransaction implements Transaction { 20 | 21 | private final org.semux.core.Transaction transaction; 22 | 23 | public SemuxTransaction(org.semux.core.Transaction transaction) { 24 | this.transaction = transaction; 25 | } 26 | 27 | @Override 28 | public boolean isCreate() { 29 | return transaction.getType().equals(TransactionType.CREATE); 30 | } 31 | 32 | @Override 33 | public byte[] getFrom() { 34 | return transaction.getFrom(); 35 | } 36 | 37 | @Override 38 | public byte[] getTo() { 39 | return transaction.getTo(); 40 | } 41 | 42 | @Override 43 | public long getNonce() { 44 | return transaction.getNonce(); 45 | } 46 | 47 | @Override 48 | public BigInteger getValue() { 49 | return amountToWei(transaction.getValue()); 50 | } 51 | 52 | @Override 53 | public byte[] getData() { 54 | return transaction.getData(); 55 | } 56 | 57 | @Override 58 | public long getGas() { 59 | return transaction.getGas(); 60 | } 61 | 62 | @Override 63 | public BigInteger getGasPrice() { 64 | return amountToWei(transaction.getGasPrice()); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/main/native/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # CMakeList.txt : Top-level CMake project file, do global configuration 2 | # and include sub-projects here. 3 | # 4 | cmake_minimum_required (VERSION 3.5) 5 | 6 | project ("semux") 7 | 8 | # Include sub-projects. 9 | add_subdirectory ("crypto") 10 | -------------------------------------------------------------------------------- /src/main/native/cmake/toolchain-Darwin-x86_64.cmake: -------------------------------------------------------------------------------- 1 | set(CMAKE_SYSTEM_NAME Darwin) 2 | set(CMAKE_SYSTEM_PROCESSOR x86_64) 3 | SET(CMAKE_C_COMPILER clang) 4 | SET(CMAKE_CXX_COMPILER clang++) 5 | SET(MACOSX_VERSION_MIN 10.8) 6 | 7 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mmacosx-version-min=${MACOSX_VERSION_MIN} -arch ${CMAKE_SYSTEM_PROCESSOR} -march=core2") 8 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mmacosx-version-min=${MACOSX_VERSION_MIN} -arch ${CMAKE_SYSTEM_PROCESSOR} -march=core2 -std=c++98 -stdlib=libc++") 9 | set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -mmacosx-version-min=${MACOSX_VERSION_MIN} -arch ${CMAKE_SYSTEM_PROCESSOR} -march=core2 -Wl,-dead_strip -Wl,-undefined,error -stdlib=libc++ -lc++") -------------------------------------------------------------------------------- /src/main/native/crypto/org_semux_crypto_Native.h: -------------------------------------------------------------------------------- 1 | /* DO NOT EDIT THIS FILE - it is machine generated */ 2 | #include 3 | /* Header for class org_semux_crypto_Native */ 4 | 5 | #ifndef _Included_org_semux_crypto_Native 6 | #define _Included_org_semux_crypto_Native 7 | #ifdef __cplusplus 8 | extern "C" { 9 | #endif 10 | /* 11 | * Class: org_semux_crypto_Native 12 | * Method: h256 13 | * Signature: ([B)[B 14 | */ 15 | JNIEXPORT jbyteArray JNICALL Java_org_semux_crypto_Native_h256 16 | (JNIEnv *, jclass, jbyteArray); 17 | 18 | /* 19 | * Class: org_semux_crypto_Native 20 | * Method: h160 21 | * Signature: ([B)[B 22 | */ 23 | JNIEXPORT jbyteArray JNICALL Java_org_semux_crypto_Native_h160 24 | (JNIEnv *, jclass, jbyteArray); 25 | 26 | /* 27 | * Class: org_semux_crypto_Native 28 | * Method: sign 29 | * Signature: ([B[B)[B 30 | */ 31 | JNIEXPORT jbyteArray JNICALL Java_org_semux_crypto_Native_sign 32 | (JNIEnv *, jclass, jbyteArray, jbyteArray); 33 | 34 | /* 35 | * Class: org_semux_crypto_Native 36 | * Method: verify 37 | * Signature: ([B[B[B)Z 38 | */ 39 | JNIEXPORT jboolean JNICALL Java_org_semux_crypto_Native_verify 40 | (JNIEnv *, jclass, jbyteArray, jbyteArray, jbyteArray); 41 | 42 | /* 43 | * Class: org_semux_crypto_Native 44 | * Method: verifyBatch 45 | * Signature: ([[B[[B[[B)Z 46 | */ 47 | JNIEXPORT jboolean JNICALL Java_org_semux_crypto_Native_verifyBatch 48 | (JNIEnv *, jclass, jobjectArray, jobjectArray, jobjectArray); 49 | 50 | #ifdef __cplusplus 51 | } 52 | #endif 53 | #endif 54 | -------------------------------------------------------------------------------- /src/main/native/crypto/ripemd160.h: -------------------------------------------------------------------------------- 1 | #ifndef RIPEMD160_H_ 2 | #define RIPEMD160_H_ 3 | 4 | #include 5 | 6 | #ifdef __cplusplus 7 | extern "C" 8 | { 9 | #endif 10 | 11 | typedef struct { 12 | uint64_t length; 13 | union { 14 | uint32_t w[16]; 15 | uint8_t b[64]; 16 | } buf; 17 | uint32_t h[5]; 18 | uint8_t bufpos; 19 | } ripemd160_state; 20 | 21 | void ripemd160_init(ripemd160_state *md); 22 | void ripemd160_process(ripemd160_state *md, const unsigned char *in, unsigned long inlen); 23 | void ripemd160_done(ripemd160_state *md, unsigned char *out); 24 | void ripemd160(const void *in, unsigned long length, void *out); 25 | 26 | #ifdef __cplusplus 27 | } 28 | #endif 29 | 30 | #endif -------------------------------------------------------------------------------- /src/main/resources/genesis/devnet.json: -------------------------------------------------------------------------------- 1 | { 2 | "number": 0, 3 | "coinbase": "0x0000000000000000000000000000000000000000", 4 | "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000", 5 | "timestamp": 1504742400000, 6 | "data": "semux", 7 | "premine": [ 8 | { 9 | "address": "0x23a6049381fd2cfb0661d9de206613b83d53d7df", 10 | "amount": 1000000000000000, 11 | "note": "test" 12 | } 13 | ], 14 | "delegates": { 15 | "semux1": "0x23a6049381fd2cfb0661d9de206613b83d53d7df" 16 | }, 17 | "config": { 18 | 19 | } 20 | } -------------------------------------------------------------------------------- /src/main/resources/genesis/mainnet.json: -------------------------------------------------------------------------------- 1 | { 2 | "number": 0, 3 | "coinbase": "0x0000000000000000000000000000000000000000", 4 | "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000", 5 | "timestamp": 1516406400000, 6 | "data": "semux", 7 | "premine": [ 8 | { 9 | "address": "0xe30c510f3efc6e2bf98ff8f725548e6ece568f89", 10 | "amount": 5000000000000000, 11 | "note": "founder" 12 | }, 13 | { 14 | "address": "0x541365fe0818ea0d2d7ab7f7bc79f719f5f72227", 15 | "amount": 10000000000000000, 16 | "note": "development, marketing, promotion, bounties" 17 | }, 18 | { 19 | "address": "0x1504263ee17446ea5f8b288e1c35d05749c0e47d", 20 | "amount": 10000000000000000, 21 | "note": "community distribution" 22 | } 23 | ], 24 | "delegates": { 25 | "semux1": "0x230fe26286ef1c588a29fc57e8fa54ee2409c57b", 26 | "semux2": "0xe11145b4e9e05ae06ec606af4a77ae1103661025", 27 | "semux3": "0xd3063743e9906ce8ea05158e4c83590d40c6a4c9", 28 | "semux4": "0xd45d3b25fd1d72e9da4dab7637814f138437f447" 29 | }, 30 | "config": { 31 | 32 | } 33 | } -------------------------------------------------------------------------------- /src/main/resources/genesis/testnet.json: -------------------------------------------------------------------------------- 1 | { 2 | "number": 0, 3 | "coinbase": "0x0000000000000000000000000000000000000000", 4 | "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000", 5 | "timestamp": 1504742400000, 6 | "data": "semux", 7 | "premine": [ 8 | { 9 | "address": "0xc583b6ad1d1cccfc00ae9113db6408f022822b20", 10 | "amount": 1000000000000000, 11 | "note": "test" 12 | }, 13 | { 14 | "address": "0xc5f32c2231ff2b60cdfa9c0d3a9c2e71774de472", 15 | "amount": 1000000000000000, 16 | "note": "test" 17 | }, 18 | { 19 | "address": "0xfe81ddefe30d7ab28f2ce73bb37d3d42603c4ddf", 20 | "amount": 1000000000000000, 21 | "note": "test" 22 | }, 23 | { 24 | "address": "0x837c1e6ceca4ccad20e59849cb7a9678394f7313", 25 | "amount": 1000000000000000, 26 | "note": "test" 27 | } 28 | ], 29 | "delegates": { 30 | "semux1": "0xc583b6ad1d1cccfc00ae9113db6408f022822b20", 31 | "semux2": "0xc5f32c2231ff2b60cdfa9c0d3a9c2e71774de472", 32 | "semux3": "0xfe81ddefe30d7ab28f2ce73bb37d3d42603c4ddf", 33 | "semux4": "0x837c1e6ceca4ccad20e59849cb7a9678394f7313" 34 | }, 35 | "config": { 36 | 37 | } 38 | } -------------------------------------------------------------------------------- /src/main/resources/log4j2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /src/main/resources/logo.icns: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/semuxproject/semux-core/3ac4bf663f3eb4e0ab4976bb049ba30ffe8181f4/src/main/resources/logo.icns -------------------------------------------------------------------------------- /src/main/resources/native/Darwin-x86_64/libsemuxcrypto.dylib: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/semuxproject/semux-core/3ac4bf663f3eb4e0ab4976bb049ba30ffe8181f4/src/main/resources/native/Darwin-x86_64/libsemuxcrypto.dylib -------------------------------------------------------------------------------- /src/main/resources/native/Linux-aarch64/libsemuxcrypto.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/semuxproject/semux-core/3ac4bf663f3eb4e0ab4976bb049ba30ffe8181f4/src/main/resources/native/Linux-aarch64/libsemuxcrypto.so -------------------------------------------------------------------------------- /src/main/resources/native/Linux-x86_64/libsemuxcrypto.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/semuxproject/semux-core/3ac4bf663f3eb4e0ab4976bb049ba30ffe8181f4/src/main/resources/native/Linux-x86_64/libsemuxcrypto.so -------------------------------------------------------------------------------- /src/main/resources/native/Windows-x86_64/libsemuxcrypto.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/semuxproject/semux-core/3ac4bf663f3eb4e0ab4976bb049ba30ffe8181f4/src/main/resources/native/Windows-x86_64/libsemuxcrypto.dll -------------------------------------------------------------------------------- /src/main/resources/org/semux/api/favicon-16x16.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/semuxproject/semux-core/3ac4bf663f3eb4e0ab4976bb049ba30ffe8181f4/src/main/resources/org/semux/api/favicon-16x16.png -------------------------------------------------------------------------------- /src/main/resources/org/semux/api/favicon-32x32.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/semuxproject/semux-core/3ac4bf663f3eb4e0ab4976bb049ba30ffe8181f4/src/main/resources/org/semux/api/favicon-32x32.png -------------------------------------------------------------------------------- /src/main/resources/org/semux/api/mime.types: -------------------------------------------------------------------------------- 1 | type=text/javascript exts=js 2 | type=text/css exts=css 3 | type=text/html exts=html,htm 4 | type=application/json exts=json,map 5 | type=application/x-yaml exts=yml 6 | type=image/png exts=png 7 | type=image/jpeg exts=jpg 8 | type=image/gif exts=gif -------------------------------------------------------------------------------- /src/main/resources/org/semux/gui/banner.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/semuxproject/semux-core/3ac4bf663f3eb4e0ab4976bb049ba30ffe8181f4/src/main/resources/org/semux/gui/banner.png -------------------------------------------------------------------------------- /src/main/resources/org/semux/gui/contract.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/semuxproject/semux-core/3ac4bf663f3eb4e0ab4976bb049ba30ffe8181f4/src/main/resources/org/semux/gui/contract.png -------------------------------------------------------------------------------- /src/main/resources/org/semux/gui/cycle.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/semuxproject/semux-core/3ac4bf663f3eb4e0ab4976bb049ba30ffe8181f4/src/main/resources/org/semux/gui/cycle.png -------------------------------------------------------------------------------- /src/main/resources/org/semux/gui/delegates.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/semuxproject/semux-core/3ac4bf663f3eb4e0ab4976bb049ba30ffe8181f4/src/main/resources/org/semux/gui/delegates.png -------------------------------------------------------------------------------- /src/main/resources/org/semux/gui/home.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/semuxproject/semux-core/3ac4bf663f3eb4e0ab4976bb049ba30ffe8181f4/src/main/resources/org/semux/gui/home.png -------------------------------------------------------------------------------- /src/main/resources/org/semux/gui/inbound.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/semuxproject/semux-core/3ac4bf663f3eb4e0ab4976bb049ba30ffe8181f4/src/main/resources/org/semux/gui/inbound.png -------------------------------------------------------------------------------- /src/main/resources/org/semux/gui/lock.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/semuxproject/semux-core/3ac4bf663f3eb4e0ab4976bb049ba30ffe8181f4/src/main/resources/org/semux/gui/lock.png -------------------------------------------------------------------------------- /src/main/resources/org/semux/gui/logo.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/semuxproject/semux-core/3ac4bf663f3eb4e0ab4976bb049ba30ffe8181f4/src/main/resources/org/semux/gui/logo.ico -------------------------------------------------------------------------------- /src/main/resources/org/semux/gui/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/semuxproject/semux-core/3ac4bf663f3eb4e0ab4976bb049ba30ffe8181f4/src/main/resources/org/semux/gui/logo.png -------------------------------------------------------------------------------- /src/main/resources/org/semux/gui/outbound.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/semuxproject/semux-core/3ac4bf663f3eb4e0ab4976bb049ba30ffe8181f4/src/main/resources/org/semux/gui/outbound.png -------------------------------------------------------------------------------- /src/main/resources/org/semux/gui/receive.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/semuxproject/semux-core/3ac4bf663f3eb4e0ab4976bb049ba30ffe8181f4/src/main/resources/org/semux/gui/receive.png -------------------------------------------------------------------------------- /src/main/resources/org/semux/gui/send.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/semuxproject/semux-core/3ac4bf663f3eb4e0ab4976bb049ba30ffe8181f4/src/main/resources/org/semux/gui/send.png -------------------------------------------------------------------------------- /src/main/resources/org/semux/gui/splash.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/semuxproject/semux-core/3ac4bf663f3eb4e0ab4976bb049ba30ffe8181f4/src/main/resources/org/semux/gui/splash.png -------------------------------------------------------------------------------- /src/main/resources/org/semux/gui/transactions.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/semuxproject/semux-core/3ac4bf663f3eb4e0ab4976bb049ba30ffe8181f4/src/main/resources/org/semux/gui/transactions.png -------------------------------------------------------------------------------- /src/test/java/org/semux/IntegrationTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux; 8 | 9 | public interface IntegrationTest { 10 | } 11 | -------------------------------------------------------------------------------- /src/test/java/org/semux/KernelTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux; 8 | 9 | import static org.awaitility.Awaitility.await; 10 | 11 | import org.junit.Rule; 12 | import org.junit.Test; 13 | import org.semux.Kernel.State; 14 | import org.semux.rules.KernelRule; 15 | 16 | public class KernelTest { 17 | 18 | @Rule 19 | public KernelRule kernelRule = new KernelRule(15160, 15170); 20 | 21 | @Test 22 | public void testStart() { 23 | Kernel kernel = kernelRule.getKernel(); 24 | 25 | // start kernel 26 | kernel.start(); 27 | await().until(() -> kernel.getNodeManager().isRunning() 28 | && kernel.getPendingManager().isRunning() 29 | && kernel.getApi().isRunning() 30 | && kernel.getP2p().isRunning() 31 | && kernel.getBftManager().isRunning() 32 | && !kernel.getSyncManager().isRunning()); 33 | 34 | // stop kernel 35 | kernel.stop(); 36 | await().until(() -> kernel.state == State.STOPPED 37 | && !kernel.getNodeManager().isRunning() 38 | && !kernel.getPendingManager().isRunning() 39 | && !kernel.getApi().isRunning() 40 | && !kernel.getP2p().isRunning() 41 | && !kernel.getBftManager().isRunning() 42 | && !kernel.getSyncManager().isRunning()); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/test/java/org/semux/LauncherTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux; 8 | 9 | import static org.mockito.ArgumentMatchers.any; 10 | import static org.mockito.Mockito.mock; 11 | import static org.mockito.Mockito.when; 12 | 13 | import org.apache.commons.cli.Option; 14 | import org.apache.commons.cli.Options; 15 | import org.apache.commons.cli.ParseException; 16 | import org.junit.Rule; 17 | import org.junit.Test; 18 | import org.junit.contrib.java.lang.system.ExpectedSystemExit; 19 | import org.semux.cli.SemuxOption; 20 | import org.semux.message.CliMessages; 21 | import org.semux.util.SystemUtil; 22 | 23 | public class LauncherTest { 24 | 25 | @Rule 26 | public final ExpectedSystemExit exit = ExpectedSystemExit.none(); 27 | 28 | @Test 29 | public void testInvalidNetworkOption() throws ParseException { 30 | Launcher launcher = mock(Launcher.class); 31 | 32 | // mock options 33 | Options options = new Options(); 34 | Option networkOption = Option.builder() 35 | .longOpt(SemuxOption.NETWORK.toString()).desc(CliMessages.get("SpecifyNetwork")) 36 | .hasArg(true).numberOfArgs(1).optionalArg(false).argName("name").type(String.class) 37 | .build(); 38 | options.addOption(networkOption); 39 | 40 | when(launcher.getOptions()).thenReturn(options); 41 | when(launcher.parseOptions(any())).thenCallRealMethod(); 42 | 43 | exit.expectSystemExitWithStatus(SystemUtil.Code.INVALID_NETWORK_LABEL); 44 | launcher.parseOptions(new String[] { "--network", "unknown" }); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/test/java/org/semux/api/SemuxApiServiceTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.api; 8 | 9 | import static org.junit.Assert.assertEquals; 10 | 11 | import org.junit.After; 12 | import org.junit.Before; 13 | import org.junit.Rule; 14 | import org.junit.Test; 15 | import org.semux.rules.KernelRule; 16 | 17 | public class SemuxApiServiceTest { 18 | 19 | @Rule 20 | public KernelRule kernelRule = new KernelRule(51610, 51710); 21 | 22 | SemuxApiMock apiMock; 23 | 24 | @Before 25 | public void setUp() { 26 | apiMock = new SemuxApiMock(kernelRule.getKernel()); 27 | apiMock.start(); 28 | } 29 | 30 | @After 31 | public void tearDown() { 32 | apiMock.stop(); 33 | } 34 | 35 | @Test 36 | public void testGetApiBaseUrl() { 37 | assertEquals(String.format("http://127.0.0.1:51710/%s/", ApiVersion.DEFAULT.prefix), 38 | apiMock.getApi().getApiBaseUrl()); 39 | } 40 | 41 | @Test 42 | public void testGetApiExplorerUrl() { 43 | assertEquals("http://127.0.0.1:51710/index.html", 44 | apiMock.getApi().getApiExplorerUrl()); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /src/test/java/org/semux/api/v2/SemuxApiTestBase.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.api.v2; 8 | 9 | import javax.ws.rs.client.Client; 10 | import javax.ws.rs.client.ClientBuilder; 11 | import javax.ws.rs.client.WebTarget; 12 | 13 | import org.glassfish.jersey.client.ClientConfig; 14 | import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature; 15 | import org.glassfish.jersey.client.proxy.WebResourceFactory; 16 | import org.glassfish.jersey.jackson.JacksonFeature; 17 | import org.junit.Before; 18 | import org.semux.api.ApiVersion; 19 | 20 | public abstract class SemuxApiTestBase extends org.semux.api.SemuxApiTestBase { 21 | 22 | protected org.semux.api.v2.client.SemuxApi api; 23 | 24 | @Before 25 | public void setUp() { 26 | super.setUp(); 27 | 28 | HttpAuthenticationFeature feature = HttpAuthenticationFeature.basicBuilder() 29 | .nonPreemptive() 30 | .credentials(config.apiUsername(), config.apiPassword()) 31 | .build(); 32 | ClientConfig clientConfig = new ClientConfig(); 33 | clientConfig.register(feature); 34 | clientConfig.register(JacksonFeature.class); 35 | 36 | Client client = ClientBuilder.newClient(clientConfig); 37 | 38 | WebTarget target = client.target("http://localhost:51710/" + ApiVersion.DEFAULT.prefix); 39 | api = WebResourceFactory.newResource(org.semux.api.v2.client.SemuxApi.class, target); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/test/java/org/semux/bench/ApiPerformance.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.bench; 8 | 9 | import java.io.IOException; 10 | 11 | import org.junit.Rule; 12 | import org.junit.Test; 13 | import org.semux.api.SemuxApiMock; 14 | import org.semux.config.Config; 15 | import org.semux.rules.KernelRule; 16 | import org.semux.util.SimpleApiClient; 17 | import org.slf4j.Logger; 18 | import org.slf4j.LoggerFactory; 19 | 20 | /** 21 | * TODO: investigate, significant performance decrease noticed. 22 | */ 23 | public class ApiPerformance { 24 | private static final Logger logger = LoggerFactory.getLogger(ApiPerformance.class); 25 | 26 | @Rule 27 | public KernelRule kernelRule = new KernelRule(51610, 51710); 28 | 29 | @Test 30 | public void testBasic() throws IOException { 31 | SemuxApiMock api = new SemuxApiMock(kernelRule.getKernel()); 32 | api.start(); 33 | 34 | try { 35 | int repeat = 1000; 36 | 37 | Config c = api.getKernel().getConfig(); 38 | long t1 = System.nanoTime(); 39 | for (int i = 0; i < repeat; i++) { 40 | SimpleApiClient a = new SimpleApiClient(c.apiListenIp(), c.apiListenPort(), c.apiUsername(), 41 | c.apiPassword()); 42 | a.get("/info"); 43 | } 44 | long t2 = System.nanoTime(); 45 | logger.info("Perf_api_basic: " + (t2 - t1) / 1_000 / repeat + " μs/time"); 46 | } finally { 47 | api.stop(); 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/test/java/org/semux/config/DevnetConfigTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.config; 8 | 9 | import static org.junit.Assert.assertEquals; 10 | 11 | import org.junit.Test; 12 | import org.semux.Network; 13 | 14 | public class DevnetConfigTest { 15 | 16 | @Test 17 | public void testNetworkId() { 18 | Config config = new DevnetConfig(Constants.DEFAULT_ROOT_DIR); 19 | assertEquals(Network.DEVNET, config.network()); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/test/java/org/semux/config/TestnetConfigTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.config; 8 | 9 | import static org.junit.Assert.assertEquals; 10 | 11 | import org.junit.Test; 12 | import org.semux.Network; 13 | 14 | public class TestnetConfigTest { 15 | 16 | @Test 17 | public void testNetworkId() { 18 | Config config = new TestnetConfig(Constants.DEFAULT_ROOT_DIR); 19 | assertEquals(Network.TESTNET, config.network()); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/test/java/org/semux/config/UnitTestnetConfig.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.config; 8 | 9 | import java.util.Collections; 10 | import java.util.Map; 11 | 12 | import org.semux.Network; 13 | import org.semux.core.Fork; 14 | 15 | /** 16 | * The unit tests were written with a specific configuration in mind, however 17 | * the devnet config is changeable for users needs and to aid in local 18 | * development 19 | * 20 | * So we introduce a new network configuration with no forks enabled and normal 21 | * block times. 22 | * 23 | */ 24 | public class UnitTestnetConfig extends AbstractConfig { 25 | 26 | public UnitTestnetConfig(String dataDir) { 27 | super(dataDir, Network.DEVNET, Constants.DEVNET_VERSION); 28 | 29 | this.netMaxInboundConnectionsPerIp = Integer.MAX_VALUE; 30 | 31 | this.forkUniformDistributionEnabled = true; 32 | this.forkVirtualMachineEnabled = true; 33 | this.forkVotingPrecompiledUpgradeEnabled = true; 34 | } 35 | 36 | @Override 37 | public Map checkpoints() { 38 | return Collections.emptyMap(); 39 | } 40 | 41 | @Override 42 | public Map manuallyActivatedForks() { 43 | return Collections.emptyMap(); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/test/java/org/semux/crypto/AesTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.crypto; 8 | 9 | import static org.junit.Assert.assertArrayEquals; 10 | 11 | import org.junit.Test; 12 | import org.semux.util.Bytes; 13 | 14 | public class AesTest { 15 | private static byte[] raw = Bytes.of("test"); 16 | private static byte[] key = Hex.decode("1122334455667788112233445566778811223344556677881122334455667788"); 17 | private static byte[] iv = Hex.decode("11223344556677881122334455667788"); 18 | private static byte[] encrypted = Hex.decode("182b93aa58d6291381660e5bad673dd4"); 19 | 20 | @Test 21 | public void testEncrypt() throws CryptoException { 22 | byte[] bytes = Aes.encrypt(raw, key, iv); 23 | 24 | assertArrayEquals(encrypted, bytes); 25 | } 26 | 27 | @Test 28 | public void testDecrypt() throws CryptoException { 29 | byte[] bytes = Aes.decrypt(encrypted, key, iv); 30 | 31 | assertArrayEquals(raw, bytes); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/test/java/org/semux/crypto/CryptoExceptionTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.crypto; 8 | 9 | import static org.junit.Assert.assertEquals; 10 | 11 | import org.junit.Test; 12 | import org.semux.util.Bytes; 13 | 14 | public class CryptoExceptionTest { 15 | 16 | @Test(expected = CryptoException.class) 17 | public void testCryptoException() throws CryptoException { 18 | Aes.decrypt(Bytes.EMPTY_BYTES, Bytes.EMPTY_BYTES, Bytes.EMPTY_BYTES); 19 | } 20 | 21 | @Test 22 | public void testConstructor() { 23 | String msg = "test"; 24 | Throwable th = new Throwable(); 25 | CryptoException e = new CryptoException(msg, th); 26 | assertEquals(msg, e.getMessage()); 27 | assertEquals(th, e.getCause()); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/test/java/org/semux/crypto/bip32/BaseVectorTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.crypto.bip32; 8 | 9 | import java.io.UnsupportedEncodingException; 10 | 11 | import org.semux.crypto.bip32.key.KeyVersion; 12 | 13 | public abstract class BaseVectorTest { 14 | 15 | public HdKeyPair masterNode; 16 | public HdKeyGenerator hdKeyGenerator = new HdKeyGenerator(); 17 | 18 | public BaseVectorTest() throws UnsupportedEncodingException { 19 | masterNode = hdKeyGenerator.getMasterKeyPairFromSeed(getSeed(), KeyVersion.MAINNET, CoinType.BITCOIN); 20 | } 21 | 22 | protected abstract byte[] getSeed(); 23 | } 24 | -------------------------------------------------------------------------------- /src/test/java/org/semux/crypto/bip32/BitUtilTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.crypto.bip32; 8 | 9 | import org.junit.Assert; 10 | import org.junit.Test; 11 | import org.semux.crypto.bip32.util.BitUtil; 12 | 13 | public class BitUtilTest { 14 | 15 | @Test 16 | public void testCheckBitFromLeft() { 17 | byte a = 0x01; 18 | // th 8th bit from the left is one 19 | Assert.assertTrue(BitUtil.checkBit(a, 8)); 20 | 21 | a = (byte) 0x80; 22 | Assert.assertTrue(BitUtil.checkBit(a, 1)); 23 | } 24 | 25 | @Test 26 | public void testSetBit() { 27 | byte a = 0; 28 | a = BitUtil.setBit(a, 1); 29 | Assert.assertEquals(a, (byte) 0x80); 30 | a = BitUtil.setBit(a, 5); 31 | Assert.assertEquals(a, (byte) 0x88); 32 | } 33 | 34 | @Test 35 | public void testUnsetBit() { 36 | byte a = (byte) 0x88; 37 | a = BitUtil.unsetBit(a, 1); 38 | Assert.assertEquals(a, (byte) 0x08); 39 | a = BitUtil.unsetBit(a, 5); 40 | Assert.assertEquals(a, (byte) 0x00); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/test/java/org/semux/crypto/bip32/PublicKeyChainTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.crypto.bip32; 8 | 9 | import org.junit.Assert; 10 | import org.junit.Test; 11 | import org.semux.crypto.Hex; 12 | import org.semux.crypto.bip32.key.HdPublicKey; 13 | import org.semux.crypto.bip32.key.KeyVersion; 14 | 15 | public class PublicKeyChainTest { 16 | 17 | public static final byte[] SEED = Hex.decode("000102030405060708090a0b0c0d0e0f"); 18 | 19 | HdKeyGenerator generator = new HdKeyGenerator(); 20 | 21 | @Test 22 | public void testPubKey0() { 23 | HdKeyPair rootKeyPair = generator.getMasterKeyPairFromSeed(SEED, KeyVersion.MAINNET, CoinType.BITCOIN); 24 | HdKeyPair childKeyPair = generator.getChildKeyPair(rootKeyPair, 0, false); 25 | // test that the pub key chain generated from only public key matches the other 26 | HdPublicKey pubKey = generator.getChildPublicKey(rootKeyPair.getPublicKey(), 0, false, Scheme.BIP32); 27 | Assert.assertArrayEquals(childKeyPair.getPublicKey().getKey(), pubKey.getKey()); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/test/java/org/semux/crypto/bip39/Vector.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.crypto.bip39; 8 | 9 | public class Vector { 10 | 11 | private byte[] entropy; 12 | private String mnemonic; 13 | private String seed; 14 | private String hdKey; 15 | private String passphrase; 16 | 17 | public Vector(byte[] entropy, String mnemonic, String seed, String hdKey, String passphrase) { 18 | this.entropy = entropy; 19 | this.mnemonic = mnemonic; 20 | this.seed = seed; 21 | this.hdKey = hdKey; 22 | this.passphrase = passphrase; 23 | } 24 | 25 | public byte[] getEntropy() { 26 | return entropy; 27 | } 28 | 29 | public String getMnemonic() { 30 | return mnemonic; 31 | } 32 | 33 | public String getSeed() { 34 | return seed; 35 | } 36 | 37 | public String getHdKey() { 38 | return hdKey; 39 | } 40 | 41 | public String getPassphrase() { 42 | return passphrase; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/test/java/org/semux/crypto/bip44/Bip44Test.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.crypto.bip44; 8 | 9 | import org.junit.Test; 10 | import org.semux.crypto.Hex; 11 | import org.semux.crypto.bip32.CoinType; 12 | import org.semux.crypto.bip32.HdKeyPair; 13 | import org.semux.crypto.bip32.key.KeyVersion; 14 | 15 | public class Bip44Test { 16 | private Bip44 bip44 = new Bip44(); 17 | private byte[] seed = Hex.decode("abcdef"); 18 | 19 | @Test 20 | public void testBitcoin() { 21 | HdKeyPair key = bip44.getRootKeyPairFromSeed(seed, KeyVersion.MAINNET, CoinType.BITCOIN); 22 | bip44.getChildKeyPair(key, 0); 23 | } 24 | 25 | @Test 26 | public void testSemux() { 27 | HdKeyPair key = bip44.getRootKeyPairFromSeed(seed, KeyVersion.MAINNET, CoinType.SEMUX_SLIP10); 28 | bip44.getChildKeyPair(key, 0); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/test/java/org/semux/crypto/cache/PublicKeyCacheTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.crypto.cache; 8 | 9 | import static org.junit.Assert.assertNotSame; 10 | import static org.junit.Assert.assertSame; 11 | 12 | import org.junit.Ignore; 13 | import org.junit.Test; 14 | import org.semux.crypto.Key; 15 | import org.slf4j.Logger; 16 | import org.slf4j.LoggerFactory; 17 | 18 | import net.i2p.crypto.eddsa.EdDSAPublicKey; 19 | 20 | public class PublicKeyCacheTest { 21 | 22 | private static final Logger logger = LoggerFactory.getLogger(PublicKeyCacheTest.class); 23 | 24 | @Test 25 | public void testCache() { 26 | Key key = new Key(); 27 | byte[] b1 = key.getPublicKey().clone(); 28 | byte[] b2 = key.getPublicKey().clone(); 29 | 30 | EdDSAPublicKey p1 = PublicKeyCache.computeIfAbsent(b1); 31 | EdDSAPublicKey p2 = PublicKeyCache.computeIfAbsent(b2); 32 | assertNotSame(b1, b2); 33 | assertSame(p1, p2); 34 | } 35 | 36 | @Test 37 | @Ignore 38 | public void testJvmUtilization() { 39 | // RESULTS: 40 | // Soft value based cache is no long good for us, since we automatically 41 | // allocate 80% of free memory. The JVM will be very aggressive and eats a lot 42 | // of memory. 43 | 44 | for (int i = 0; i < 1_000_000; i++) { 45 | PublicKeyCache.computeIfAbsent(new Key().getPublicKey()); 46 | if (i % 10_000 == 0) { 47 | logger.info(Runtime.getRuntime().totalMemory() / 1024 / 1024 + " MB"); 48 | } 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/test/java/org/semux/gui/BaseTestApplication.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.gui; 8 | 9 | import javax.swing.JFrame; 10 | 11 | public class BaseTestApplication extends JFrame { 12 | 13 | private static final long serialVersionUID = 1L; 14 | 15 | public BaseTestApplication() { 16 | super(); 17 | setTitle(getClass().getCanonicalName()); 18 | setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/test/java/org/semux/gui/MenuBarTestApplication.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.gui; 8 | 9 | import java.awt.Dimension; 10 | 11 | import org.semux.KernelMock; 12 | import org.semux.gui.model.WalletModel; 13 | 14 | public class MenuBarTestApplication extends BaseTestApplication { 15 | 16 | private static final long serialVersionUID = 1L; 17 | 18 | SemuxGui gui; 19 | 20 | MenuBar menuBar; 21 | 22 | MenuBarTestApplication(WalletModel walletModel, KernelMock kernelMock) { 23 | super(); 24 | gui = new SemuxGui(walletModel, kernelMock); 25 | menuBar = new MenuBar(gui, this); 26 | this.setJMenuBar(menuBar); 27 | 28 | this.setMinimumSize(new Dimension(600, 400)); 29 | } 30 | } -------------------------------------------------------------------------------- /src/test/java/org/semux/gui/SplashScreenTestApplication.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.gui; 8 | 9 | import java.awt.Dimension; 10 | 11 | public class SplashScreenTestApplication extends BaseTestApplication { 12 | 13 | private static final long serialVersionUID = 7961392121592436000L; 14 | 15 | protected SplashScreen splashScreen; 16 | 17 | SplashScreenTestApplication() { 18 | super(); 19 | this.setMinimumSize(new Dimension(960, 600)); 20 | splashScreen = new SplashScreen(); 21 | } 22 | } -------------------------------------------------------------------------------- /src/test/java/org/semux/gui/StatusBarTestApplication.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.gui; 8 | 9 | import java.awt.BorderLayout; 10 | import java.awt.Dimension; 11 | 12 | public class StatusBarTestApplication extends BaseTestApplication { 13 | 14 | private static final long serialVersionUID = 1L; 15 | 16 | protected StatusBar statusBar; 17 | 18 | StatusBarTestApplication() { 19 | super(); 20 | this.setMinimumSize(new Dimension(960, 600)); 21 | statusBar = new StatusBar(this); 22 | this.add(statusBar, BorderLayout.SOUTH); 23 | getContentPane().add(statusBar); 24 | } 25 | } -------------------------------------------------------------------------------- /src/test/java/org/semux/gui/dialog/AddressBookDialogTestApplication.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.gui.dialog; 8 | 9 | import java.util.List; 10 | 11 | import org.semux.KernelMock; 12 | import org.semux.gui.AddressBookEntry; 13 | import org.semux.gui.BaseTestApplication; 14 | import org.semux.gui.SemuxGui; 15 | import org.semux.gui.model.WalletModel; 16 | 17 | public class AddressBookDialogTestApplication extends BaseTestApplication { 18 | 19 | private static final long serialVersionUID = 1L; 20 | 21 | SemuxGui gui; 22 | 23 | AddressBookDialog addressBookDialog; 24 | 25 | AddressBookDialogTestApplication(WalletModel walletModel, KernelMock kernelMock, List entries) { 26 | super(); 27 | gui = new SemuxGui(walletModel, kernelMock); 28 | 29 | addressBookDialog = new AddressBookDialog(this, gui.getKernel().getWallet(), gui) { 30 | private static final long serialVersionUID = 1L; 31 | 32 | @Override 33 | protected List getAddressBookEntries() { 34 | return entries; 35 | } 36 | }; 37 | 38 | addressBookDialog.setVisible(true); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/test/java/org/semux/gui/dialog/ConsoleDialogTestApplication.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.gui.dialog; 8 | 9 | import org.semux.KernelMock; 10 | import org.semux.gui.BaseTestApplication; 11 | import org.semux.gui.SemuxGui; 12 | import org.semux.gui.model.WalletModel; 13 | 14 | /** 15 | */ 16 | public class ConsoleDialogTestApplication extends BaseTestApplication { 17 | 18 | private static final long serialVersionUID = 1L; 19 | 20 | SemuxGui gui; 21 | 22 | ConsoleDialog consoleDialog; 23 | 24 | ConsoleDialogTestApplication(WalletModel walletModel, KernelMock kernelMock) { 25 | super(); 26 | gui = new SemuxGui(walletModel, kernelMock); 27 | consoleDialog = new ConsoleDialog(gui, this); 28 | consoleDialog.setVisible(true); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/test/java/org/semux/gui/dialog/DelegateDialogTestApplication.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.gui.dialog; 8 | 9 | import org.semux.KernelMock; 10 | import org.semux.gui.BaseTestApplication; 11 | import org.semux.gui.SemuxGui; 12 | import org.semux.gui.model.WalletModel; 13 | 14 | public class DelegateDialogTestApplication extends BaseTestApplication { 15 | 16 | private static final long serialVersionUID = 1L; 17 | 18 | SemuxGui gui; 19 | 20 | DelegateDialog delegateDialog; 21 | 22 | DelegateDialogTestApplication(WalletModel walletModel, KernelMock kernelMock) { 23 | super(); 24 | gui = new SemuxGui(walletModel, kernelMock); 25 | delegateDialog = new DelegateDialog(gui, this, walletModel.getDelegates().get(0)); 26 | delegateDialog.setVisible(true); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/test/java/org/semux/gui/dialog/TransactionDialogTestApplication.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.gui.dialog; 8 | 9 | import org.semux.KernelMock; 10 | import org.semux.core.Transaction; 11 | import org.semux.core.TransactionResult; 12 | import org.semux.gui.BaseTestApplication; 13 | import org.semux.gui.SemuxGui; 14 | import org.semux.gui.model.WalletModel; 15 | 16 | public class TransactionDialogTestApplication extends BaseTestApplication { 17 | 18 | private static final long serialVersionUID = 1L; 19 | 20 | SemuxGui gui; 21 | 22 | TransactionDialog transactionDialog; 23 | 24 | TransactionDialogTestApplication(WalletModel walletModel, KernelMock kernelMock, 25 | Transaction tx) { 26 | super(); 27 | gui = new SemuxGui(walletModel, kernelMock); 28 | TransactionResult result = new TransactionResult(TransactionResult.Code.SUCCESS); 29 | transactionDialog = new TransactionDialog(this, tx, gui.getKernel()); 30 | transactionDialog.setVisible(true); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/test/java/org/semux/gui/dialog/TransactionResultDialogTestApplication.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.gui.dialog; 8 | 9 | import org.semux.KernelMock; 10 | import org.semux.core.Transaction; 11 | import org.semux.core.TransactionResult; 12 | import org.semux.gui.BaseTestApplication; 13 | import org.semux.gui.SemuxGui; 14 | import org.semux.gui.model.WalletModel; 15 | 16 | public class TransactionResultDialogTestApplication extends BaseTestApplication { 17 | 18 | private static final long serialVersionUID = 1L; 19 | 20 | SemuxGui gui; 21 | 22 | TransactionResultDialog dialog; 23 | 24 | TransactionResultDialogTestApplication(WalletModel walletModel, KernelMock kernelMock, 25 | Transaction tx, TransactionResult result) { 26 | super(); 27 | gui = new SemuxGui(walletModel, kernelMock); 28 | dialog = new TransactionResultDialog(this, tx, result); 29 | dialog.setVisible(true); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/test/java/org/semux/gui/panel/DelegatesPanelTestApplication.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.gui.panel; 8 | 9 | import org.semux.KernelMock; 10 | import org.semux.gui.BaseTestApplication; 11 | import org.semux.gui.SemuxGui; 12 | import org.semux.gui.model.WalletModel; 13 | 14 | public class DelegatesPanelTestApplication extends BaseTestApplication { 15 | 16 | private static final long serialVersionUID = 1L; 17 | 18 | SemuxGui gui; 19 | 20 | DelegatesPanel delegatesPanel; 21 | 22 | DelegatesPanelTestApplication(WalletModel walletModel, KernelMock kernelMock) { 23 | super(); 24 | gui = new SemuxGui(walletModel, kernelMock); 25 | delegatesPanel = new DelegatesPanel(gui, this); 26 | getContentPane().add(delegatesPanel); 27 | } 28 | } -------------------------------------------------------------------------------- /src/test/java/org/semux/gui/panel/HomePanelTestApplication.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.gui.panel; 8 | 9 | import org.semux.KernelMock; 10 | import org.semux.gui.BaseTestApplication; 11 | import org.semux.gui.SemuxGui; 12 | import org.semux.gui.model.WalletModel; 13 | 14 | public class HomePanelTestApplication extends BaseTestApplication { 15 | 16 | private static final long serialVersionUID = 1L; 17 | 18 | HomePanel homePanel; 19 | 20 | HomePanelTestApplication(WalletModel walletModel, KernelMock kernelMock) { 21 | super(); 22 | SemuxGui gui = new SemuxGui(walletModel, kernelMock); 23 | homePanel = new HomePanel(gui); 24 | getContentPane().add(homePanel); 25 | } 26 | } -------------------------------------------------------------------------------- /src/test/java/org/semux/gui/panel/ReceivePanelTestApplication.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.gui.panel; 8 | 9 | import org.semux.KernelMock; 10 | import org.semux.gui.BaseTestApplication; 11 | import org.semux.gui.SemuxGui; 12 | import org.semux.gui.model.WalletModel; 13 | 14 | public class ReceivePanelTestApplication extends BaseTestApplication { 15 | 16 | private static final long serialVersionUID = 1L; 17 | 18 | ReceivePanel receivePanel; 19 | 20 | ReceivePanelTestApplication(WalletModel walletModel, KernelMock kernelMock) { 21 | super(); 22 | SemuxGui gui = new SemuxGui(walletModel, kernelMock); 23 | receivePanel = new ReceivePanel(gui); 24 | getContentPane().add(receivePanel); 25 | } 26 | } -------------------------------------------------------------------------------- /src/test/java/org/semux/gui/panel/TransactionsPanelTestApplication.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.gui.panel; 8 | 9 | import org.semux.KernelMock; 10 | import org.semux.gui.BaseTestApplication; 11 | import org.semux.gui.SemuxGui; 12 | import org.semux.gui.model.WalletModel; 13 | 14 | public class TransactionsPanelTestApplication extends BaseTestApplication { 15 | 16 | private static final long serialVersionUID = 1L; 17 | 18 | TransactionsPanel transactionsPanel; 19 | 20 | TransactionsPanelTestApplication(WalletModel walletModel, KernelMock kernelMock) { 21 | super(); 22 | SemuxGui gui = new SemuxGui(walletModel, kernelMock); 23 | transactionsPanel = new TransactionsPanel(gui, this); 24 | getContentPane().add(transactionsPanel); 25 | } 26 | } -------------------------------------------------------------------------------- /src/test/java/org/semux/message/CLIMessageTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.message; 8 | 9 | import static org.junit.Assert.assertNotNull; 10 | 11 | import java.util.MissingResourceException; 12 | 13 | import org.junit.Test; 14 | 15 | public class CLIMessageTest { 16 | 17 | @Test 18 | public void testExists() { 19 | assertNotNull(CliMessages.get("Address")); 20 | } 21 | 22 | @Test(expected = MissingResourceException.class) 23 | public void testNotExists() { 24 | assertNotNull(CliMessages.get("NotExist")); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/test/java/org/semux/message/GUIMessageTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.message; 8 | 9 | import static org.junit.Assert.assertNotNull; 10 | 11 | import java.util.MissingResourceException; 12 | 13 | import org.junit.Test; 14 | 15 | public class GUIMessageTest { 16 | 17 | @Test 18 | public void testExists() { 19 | assertNotNull(GuiMessages.get("Address")); 20 | } 21 | 22 | @Test(expected = MissingResourceException.class) 23 | public void testNotExists() { 24 | assertNotNull(GuiMessages.get("NotExist")); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/test/java/org/semux/net/CapabilityTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.net; 8 | 9 | import static org.junit.Assert.assertEquals; 10 | import static org.junit.Assert.assertFalse; 11 | import static org.junit.Assert.assertTrue; 12 | 13 | import org.junit.Test; 14 | 15 | public class CapabilityTest { 16 | 17 | @Test 18 | public void testIsSupported() { 19 | assertFalse(CapabilityTreeSet.emptyList().isSupported(Capability.SEMUX)); 20 | assertFalse(CapabilityTreeSet.of("SEMUX").isSupported(Capability.FAST_SYNC)); 21 | assertTrue(CapabilityTreeSet.of("SEMUX").isSupported(Capability.SEMUX)); 22 | assertTrue(CapabilityTreeSet.of(Capability.SEMUX).isSupported(Capability.SEMUX)); 23 | assertEquals(CapabilityTreeSet.of(Capability.SEMUX), CapabilityTreeSet.of(Capability.SEMUX)); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/test/java/org/semux/net/FrameTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.net; 8 | 9 | import static org.assertj.core.api.Assertions.assertThat; 10 | 11 | import org.junit.Test; 12 | 13 | import io.netty.buffer.ByteBuf; 14 | import io.netty.buffer.Unpooled; 15 | 16 | public class FrameTest { 17 | 18 | @Test 19 | public void testReadAndWrite() { 20 | short version = 0x1122; 21 | byte compressType = 0x33; 22 | byte packetType = 0x44; 23 | int packetId = 0x55667788; 24 | int packetSize = 0x99aabbcc; 25 | int bodySize = 0xddeeff00; 26 | Frame frame = new Frame(version, compressType, packetType, packetId, packetSize, bodySize, null); 27 | 28 | ByteBuf buf = Unpooled.copiedBuffer(new byte[Frame.HEADER_SIZE]); 29 | 30 | buf.writerIndex(0); 31 | frame.writeHeader(buf); 32 | 33 | buf.readerIndex(0); 34 | Frame frame2 = Frame.readHeader(buf); 35 | 36 | assertThat(frame2).isEqualToComparingFieldByFieldRecursively(frame); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/test/java/org/semux/net/filter/SemuxIpFilterTestBase.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.net.filter; 8 | 9 | import java.io.File; 10 | 11 | abstract class SemuxIpFilterTestBase { 12 | 13 | /** 14 | * Get a testing ipfilter.json from resource bundle 15 | * 16 | * @param fileName 17 | * @return 18 | */ 19 | protected static File getFile(String fileName) { 20 | return new File(SemuxIpFilterLoaderTest.class.getResource("/ipfilter/" + fileName).getFile()); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/test/java/org/semux/net/msg/MessageCodeTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.net.msg; 8 | 9 | import static org.junit.Assert.assertEquals; 10 | import static org.junit.Assert.assertNull; 11 | 12 | import org.junit.Test; 13 | 14 | public class MessageCodeTest { 15 | 16 | @Test 17 | public void testConverting() { 18 | for (MessageCode code : MessageCode.values()) { 19 | assertEquals(code, MessageCode.of(code.getCode())); 20 | assertEquals(code, MessageCode.of(code.toByte())); 21 | } 22 | } 23 | 24 | @Test 25 | public void testNegativeByte() { 26 | byte b = (byte) 0xff; 27 | assertNull(MessageCode.of(b)); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/test/java/org/semux/net/msg/MessageFactoryTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.net.msg; 8 | 9 | import static org.junit.Assert.assertNull; 10 | 11 | import org.junit.Test; 12 | 13 | public class MessageFactoryTest { 14 | 15 | @Test 16 | public void testNonExist() throws MessageException { 17 | MessageFactory factory = new MessageFactory(); 18 | assertNull(factory.create((byte) 0xff, new byte[1])); 19 | } 20 | 21 | @Test(expected = MessageException.class) 22 | public void testWrongCodec() throws MessageException { 23 | MessageFactory factory = new MessageFactory(); 24 | factory.create((byte) 0x01, new byte[1]); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/test/java/org/semux/net/msg/ReasonCodeTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.net.msg; 8 | 9 | import static org.junit.Assert.assertEquals; 10 | import static org.junit.Assert.assertNull; 11 | 12 | import org.junit.Test; 13 | 14 | public class ReasonCodeTest { 15 | 16 | @Test 17 | public void testConverting() { 18 | for (ReasonCode code : ReasonCode.values()) { 19 | assertEquals(code, ReasonCode.of(code.getCode())); 20 | assertEquals(code, ReasonCode.of(code.toByte())); 21 | } 22 | } 23 | 24 | @Test 25 | public void testNegativeByte() { 26 | byte b = (byte) 0xff; 27 | assertNull(MessageCode.of(b)); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/test/java/org/semux/net/msg/consensus/GetBlockHeaderMessageTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.net.msg.consensus; 8 | 9 | import static org.assertj.core.api.Assertions.assertThat; 10 | 11 | import org.junit.Test; 12 | import org.semux.net.msg.MessageCode; 13 | 14 | public class GetBlockHeaderMessageTest { 15 | 16 | @Test 17 | public void testSerialization() { 18 | long number = 1; 19 | 20 | GetBlockHeaderMessage m = new GetBlockHeaderMessage(number); 21 | assertThat(m.getCode()).isEqualTo(MessageCode.GET_BLOCK_HEADER); 22 | assertThat(m.getResponseMessageClass()).isEqualTo(BlockHeaderMessage.class); 23 | 24 | GetBlockHeaderMessage m2 = new GetBlockHeaderMessage(m.getBody()); 25 | assertThat(m2.getCode()).isEqualTo(MessageCode.GET_BLOCK_HEADER); 26 | assertThat(m2.getResponseMessageClass()).isEqualTo(BlockHeaderMessage.class); 27 | assertThat(m2.getNumber()).isEqualTo(number); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/test/java/org/semux/net/msg/consensus/NewHeightMessageTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.net.msg.consensus; 8 | 9 | import static org.junit.Assert.assertEquals; 10 | 11 | import org.junit.Test; 12 | 13 | public class NewHeightMessageTest { 14 | @Test 15 | public void testSerialization() { 16 | int height = 1; 17 | 18 | NewHeightMessage msg = new NewHeightMessage(height); 19 | NewHeightMessage msg2 = new NewHeightMessage(msg.getBody()); 20 | assertEquals(height, msg2.getHeight()); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/test/java/org/semux/net/msg/consensus/NewViewMessageTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.net.msg.consensus; 8 | 9 | import static org.junit.Assert.assertEquals; 10 | import static org.junit.Assert.assertTrue; 11 | 12 | import java.util.Collections; 13 | 14 | import org.junit.Test; 15 | import org.semux.consensus.Proof; 16 | 17 | public class NewViewMessageTest { 18 | @Test 19 | public void testSerialization() { 20 | int height = 1; 21 | int view = 1; 22 | Proof proof = new Proof(height, view, Collections.emptyList()); 23 | 24 | NewViewMessage msg = new NewViewMessage(proof); 25 | NewViewMessage msg2 = new NewViewMessage(msg.getBody()); 26 | assertEquals(height, msg2.getHeight()); 27 | assertEquals(view, msg2.getView()); 28 | assertTrue(msg2.getProof().getVotes().isEmpty()); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/test/java/org/semux/net/msg/consensus/VoteMessageTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.net.msg.consensus; 8 | 9 | import static org.junit.Assert.assertArrayEquals; 10 | import static org.junit.Assert.assertEquals; 11 | import static org.junit.Assert.assertTrue; 12 | 13 | import org.junit.Test; 14 | import org.semux.consensus.Vote; 15 | import org.semux.consensus.VoteType; 16 | import org.semux.crypto.Key; 17 | 18 | public class VoteMessageTest { 19 | @Test 20 | public void testSerialization() { 21 | Key key = new Key(); 22 | VoteType type = VoteType.COMMIT; 23 | long height = 1; 24 | int view = 2; 25 | Vote vote = Vote.newReject(type, height, view); 26 | vote.sign(key); 27 | assertTrue(vote.validate()); 28 | 29 | VoteMessage msg = new VoteMessage(vote); 30 | VoteMessage msg2 = new VoteMessage(msg.getBody()); 31 | Vote vote2 = msg2.getVote(); 32 | assertEquals(type, vote2.getType()); 33 | assertEquals(height, vote2.getHeight()); 34 | assertEquals(view, vote2.getView()); 35 | assertTrue(vote2.validate()); 36 | 37 | assertArrayEquals(key.toAddress(), vote2.getSignature().getAddress()); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/test/java/org/semux/net/msg/p2p/NodesMessageTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.net.msg.p2p; 8 | 9 | import static org.junit.Assert.assertArrayEquals; 10 | import static org.junit.Assert.assertFalse; 11 | import static org.semux.net.msg.p2p.NodesMessage.MAX_NODES; 12 | 13 | import java.net.InetSocketAddress; 14 | import java.util.ArrayList; 15 | import java.util.List; 16 | 17 | import org.junit.Test; 18 | import org.semux.net.NodeManager.Node; 19 | 20 | public class NodesMessageTest { 21 | 22 | @Test 23 | public void testCodec() { 24 | List nodes = new ArrayList<>(); 25 | nodes.add(new Node(new InetSocketAddress("127.0.0.1", 5160))); 26 | nodes.add(new Node(new InetSocketAddress("127.0.0.1", 5161))); 27 | NodesMessage nodesMessage = new NodesMessage(new NodesMessage(nodes).getBody()); 28 | assertArrayEquals(nodes.toArray(), nodesMessage.getNodes().toArray()); 29 | } 30 | 31 | @Test 32 | public void testOverflow() { 33 | List list = new ArrayList<>(); 34 | for (int i = 0; i < MAX_NODES + 1; i++) { 35 | list.add(new Node("127.0.0.1", 1234)); 36 | } 37 | NodesMessage maliciousMessage = new NodesMessage(list); 38 | 39 | assertFalse(maliciousMessage.validate()); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/test/java/org/semux/net/msg/p2p/TransactionMessageTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.net.msg.p2p; 8 | 9 | import static org.hamcrest.Matchers.equalTo; 10 | import static org.junit.Assert.assertThat; 11 | 12 | import org.junit.Test; 13 | import org.semux.Network; 14 | import org.semux.core.Amount; 15 | import org.semux.core.Transaction; 16 | import org.semux.core.TransactionType; 17 | import org.semux.crypto.Key; 18 | import org.semux.util.Bytes; 19 | import org.semux.util.TimeUtil; 20 | 21 | public class TransactionMessageTest { 22 | @Test 23 | public void testSerialization() { 24 | Network network = Network.DEVNET; 25 | TransactionType type = TransactionType.TRANSFER; 26 | byte[] to = Bytes.random(20); 27 | Amount value = Amount.of(2); 28 | Amount fee = Amount.of(50_000_000L); 29 | long nonce = 1; 30 | long timestamp = TimeUtil.currentTimeMillis(); 31 | byte[] data = Bytes.of("data"); 32 | 33 | Transaction tx = new Transaction(network, type, to, value, fee, nonce, timestamp, data); 34 | tx.sign(new Key()); 35 | 36 | TransactionMessage msg = new TransactionMessage(tx); 37 | TransactionMessage msg2 = new TransactionMessage(msg.getBody()); 38 | assertThat(msg2.getTransaction().getHash(), equalTo(tx.getHash())); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/test/java/org/semux/net/msg/p2p/handshake/v1/HelloMessageTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.net.msg.p2p.handshake.v1; 8 | 9 | import static org.junit.Assert.assertEquals; 10 | import static org.junit.Assert.assertTrue; 11 | 12 | import org.junit.Test; 13 | import org.semux.config.Config; 14 | import org.semux.config.Constants; 15 | import org.semux.config.UnitTestnetConfig; 16 | import org.semux.crypto.Key; 17 | 18 | public class HelloMessageTest { 19 | 20 | @Test 21 | public void testCodec() { 22 | Config config = new UnitTestnetConfig(Constants.DEFAULT_ROOT_DIR); 23 | 24 | Key key = new Key(); 25 | String peerId = key.toAddressString(); 26 | HelloMessage msg = new HelloMessage(config.network(), config.networkVersion(), peerId, "127.0.0.1", 5161, 27 | config.getClientId(), 2, key); 28 | assertTrue(msg.validate(config)); 29 | assertEquals(key.toAddressString(), msg.getPeer().getPeerId()); 30 | 31 | msg = new HelloMessage(msg.getBody()); 32 | assertTrue(msg.validate(config)); 33 | assertEquals(key.toAddressString(), msg.getPeer().getPeerId()); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/test/java/org/semux/net/msg/p2p/handshake/v1/WorldMessageTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.net.msg.p2p.handshake.v1; 8 | 9 | import static org.junit.Assert.assertEquals; 10 | import static org.junit.Assert.assertTrue; 11 | 12 | import org.junit.Test; 13 | import org.semux.config.Config; 14 | import org.semux.config.Constants; 15 | import org.semux.config.UnitTestnetConfig; 16 | import org.semux.crypto.Key; 17 | 18 | public class WorldMessageTest { 19 | 20 | @Test 21 | public void testCodec() { 22 | Config config = new UnitTestnetConfig(Constants.DEFAULT_ROOT_DIR); 23 | 24 | Key key = new Key(); 25 | String peerId = key.toAddressString(); 26 | WorldMessage msg = new WorldMessage(config.network(), config.networkVersion(), peerId, "127.0.0.1", 5161, 27 | config.getClientId(), 2, key); 28 | assertTrue(msg.validate(config)); 29 | assertEquals(key.toAddressString(), msg.getPeer().getPeerId()); 30 | 31 | msg = new WorldMessage(msg.getBody()); 32 | assertTrue(msg.validate(config)); 33 | assertEquals(key.toAddressString(), msg.getPeer().getPeerId()); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/test/java/org/semux/net/msg/p2p/handshake/v2/InitMessageTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.net.msg.p2p.handshake.v2; 8 | 9 | import static org.junit.Assert.assertArrayEquals; 10 | import static org.junit.Assert.assertEquals; 11 | 12 | import org.junit.Test; 13 | import org.semux.util.Bytes; 14 | import org.semux.util.TimeUtil; 15 | 16 | public class InitMessageTest { 17 | 18 | @Test 19 | public void testCodec() { 20 | byte[] secret = Bytes.random(32); 21 | long timestamp = TimeUtil.currentTimeMillis(); 22 | 23 | InitMessage msg = new InitMessage(secret, timestamp); 24 | assertArrayEquals(secret, msg.getSecret()); 25 | assertEquals(timestamp, msg.getTimestamp()); 26 | 27 | msg = new InitMessage(msg.getBody()); 28 | assertArrayEquals(secret, msg.getSecret()); 29 | assertEquals(timestamp, msg.getTimestamp()); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/test/java/org/semux/rules/TemporaryDatabaseRule.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.rules; 8 | 9 | import java.io.File; 10 | import java.nio.file.Path; 11 | import java.util.EnumMap; 12 | 13 | import org.junit.rules.TemporaryFolder; 14 | import org.semux.config.Constants; 15 | import org.semux.db.Database; 16 | import org.semux.db.DatabaseFactory; 17 | import org.semux.db.DatabaseName; 18 | import org.semux.db.LeveldbDatabase; 19 | 20 | public class TemporaryDatabaseRule extends TemporaryFolder implements DatabaseFactory { 21 | 22 | private final EnumMap databases = new EnumMap<>(DatabaseName.class); 23 | 24 | @Override 25 | public void before() throws Throwable { 26 | create(); 27 | } 28 | 29 | @Override 30 | public void after() { 31 | close(); 32 | delete(); 33 | } 34 | 35 | @Override 36 | public Database getDB(DatabaseName name) { 37 | return databases.computeIfAbsent(name, k -> { 38 | File file = new File(getRoot(), k.toString().toLowerCase()); 39 | return new LeveldbDatabase(file); 40 | }); 41 | } 42 | 43 | @Override 44 | public void close() { 45 | for (Database db : databases.values()) { 46 | db.close(); 47 | } 48 | } 49 | 50 | @Override 51 | public Path getDataDir() { 52 | return super.getRoot().toPath(); 53 | } 54 | } -------------------------------------------------------------------------------- /src/test/java/org/semux/util/ArrayUtilTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.util; 8 | 9 | import static org.junit.Assert.assertEquals; 10 | import static org.junit.Assert.assertFalse; 11 | 12 | import java.util.Arrays; 13 | import java.util.TreeSet; 14 | 15 | import org.junit.Test; 16 | 17 | public class ArrayUtilTest { 18 | 19 | @Test 20 | public void testPermutation() { 21 | int n = 100; 22 | int[] arr = ArrayUtil.permutation(n); 23 | 24 | TreeSet set = new TreeSet<>(); 25 | for (int i : arr) { 26 | set.add(i); 27 | } 28 | 29 | assertEquals(0, set.first().intValue()); 30 | assertEquals(n - 1, set.last().intValue()); 31 | } 32 | 33 | @Test 34 | public void testShuffle() { 35 | int n = 100; 36 | 37 | int[] arr = new int[n]; 38 | for (int i = 0; i < n; i++) { 39 | arr[i] = i; 40 | } 41 | 42 | int[] arr2 = arr.clone(); 43 | ArrayUtil.shuffle(arr2); 44 | assertFalse(Arrays.equals(arr, arr2)); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/test/java/org/semux/util/BasicAuthTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.util; 8 | 9 | import static org.junit.Assert.assertEquals; 10 | import static org.junit.Assert.assertNull; 11 | 12 | import org.apache.commons.lang3.tuple.Pair; 13 | import org.junit.Test; 14 | 15 | public class BasicAuthTest { 16 | 17 | @Test 18 | public void testAuth() { 19 | String username = "name"; 20 | String password = "password"; 21 | String auth = BasicAuth.generateAuth(username, password); 22 | 23 | Pair p = BasicAuth.parseAuth(auth); 24 | assertEquals(username, p.getKey()); 25 | assertEquals(password, p.getValue()); 26 | } 27 | 28 | @Test 29 | public void testInvalid() { 30 | assertNull(BasicAuth.parseAuth("invalid_auth_string")); 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/test/java/org/semux/util/ByteArrayTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.util; 8 | 9 | import static org.hamcrest.Matchers.equalTo; 10 | import static org.junit.Assert.assertEquals; 11 | import static org.junit.Assert.assertFalse; 12 | import static org.junit.Assert.assertThat; 13 | import static org.junit.Assert.assertTrue; 14 | 15 | import java.io.IOException; 16 | import java.util.Arrays; 17 | import java.util.HashMap; 18 | 19 | import org.junit.Test; 20 | import org.semux.crypto.Hex; 21 | 22 | public class ByteArrayTest { 23 | 24 | @Test 25 | public void testInHashMap() { 26 | byte[] b1 = Bytes.random(20); 27 | byte[] b2 = Bytes.random(20); 28 | byte[] b3 = Arrays.copyOf(b1, b1.length); 29 | 30 | HashMap map = new HashMap<>(); 31 | map.put(ByteArray.of(b1), true); 32 | 33 | assertFalse(map.containsKey(ByteArray.of(b2))); 34 | assertTrue(map.containsKey(ByteArray.of(b3))); 35 | } 36 | 37 | @Test 38 | public void testToString() { 39 | byte[] b = Bytes.random(20); 40 | assertEquals(Hex.encode(b), ByteArray.of(b).toString()); 41 | } 42 | 43 | @Test 44 | public void testByteArrayKeyDeserializer() throws IOException { 45 | byte[] x = Bytes.random(3); 46 | ByteArray.ByteArrayKeyDeserializer d = new ByteArray.ByteArrayKeyDeserializer(); 47 | Object y = d.deserializeKey(Hex.encode0x(x), null); 48 | assertTrue(y instanceof ByteArray); 49 | assertThat(ByteArray.of(x), equalTo(y)); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/test/java/org/semux/util/CommandParserTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.util; 8 | 9 | import java.util.List; 10 | 11 | import org.junit.Assert; 12 | import org.junit.Test; 13 | 14 | public class CommandParserTest { 15 | 16 | @Test 17 | public void testDelimiting() { 18 | String input = "foo and \"bar stuff\" \"stuff \\\" with quotes \\\" \" more \"\""; 19 | List parsed = CommandParser.parseInput(input); 20 | 21 | Assert.assertEquals(6, parsed.size()); 22 | Assert.assertEquals("foo", parsed.get(0)); 23 | Assert.assertEquals("and", parsed.get(1)); 24 | Assert.assertEquals("bar stuff", parsed.get(2)); 25 | Assert.assertEquals("stuff \" with quotes \" ", parsed.get(3)); 26 | Assert.assertEquals("more", parsed.get(4)); 27 | Assert.assertEquals("", parsed.get(5)); 28 | } 29 | 30 | @Test 31 | public void testBasic() { 32 | String input = "getBlockByNumber 1"; 33 | List parsed = CommandParser.parseInput(input); 34 | Assert.assertEquals(2, parsed.size()); 35 | Assert.assertEquals("getBlockByNumber", parsed.get(0)); 36 | Assert.assertEquals("1", parsed.get(1)); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/test/java/org/semux/util/FileUtilTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.util; 8 | 9 | import static org.junit.Assert.assertFalse; 10 | 11 | import java.io.File; 12 | import java.io.IOException; 13 | 14 | import org.junit.AfterClass; 15 | import org.junit.BeforeClass; 16 | import org.junit.Test; 17 | 18 | public class FileUtilTest { 19 | 20 | private static final File dir = new File("test"); 21 | 22 | @BeforeClass 23 | public static void setUp() throws IOException { 24 | dir.mkdirs(); 25 | new File(dir, "file").createNewFile(); 26 | } 27 | 28 | @Test 29 | public void testRecursiveDelete() { 30 | FileUtil.recursiveDelete(dir); 31 | assertFalse(dir.exists()); 32 | } 33 | 34 | @AfterClass 35 | public static void tearDown() { 36 | dir.delete(); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/test/java/org/semux/util/SimpleEncoderTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.util; 8 | 9 | import static org.hamcrest.Matchers.equalTo; 10 | import static org.junit.Assert.assertThat; 11 | 12 | import org.junit.Test; 13 | 14 | public class SimpleEncoderTest { 15 | 16 | @Test 17 | public void testToAppend() { 18 | byte[] append = Bytes.of("hello"); 19 | 20 | SimpleEncoder enc = new SimpleEncoder(append); 21 | enc.writeString("s"); 22 | 23 | assertThat(enc.toBytes(), equalTo(Bytes.merge(append, Bytes.of((byte) 1), Bytes.of("s")))); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/test/java/org/semux/util/StringUtilTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.util; 8 | 9 | import static org.junit.Assert.assertFalse; 10 | import static org.junit.Assert.assertTrue; 11 | 12 | import org.junit.Test; 13 | 14 | public class StringUtilTest { 15 | 16 | @Test 17 | public void testIsNullOrEmpty() { 18 | assertTrue(StringUtil.isNullOrEmpty(null)); 19 | assertTrue(StringUtil.isNullOrEmpty("")); 20 | assertFalse(StringUtil.isNullOrEmpty("abc")); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/test/java/org/semux/util/TimeUtilTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2017-2020 The Semux Developers 3 | * 4 | * Distributed under the MIT software license, see the accompanying file 5 | * LICENSE or https://opensource.org/licenses/mit-license.php 6 | */ 7 | package org.semux.util; 8 | 9 | import static org.junit.Assert.assertEquals; 10 | 11 | import java.time.Duration; 12 | import java.util.Arrays; 13 | import java.util.Collection; 14 | 15 | import org.junit.Assert; 16 | import org.junit.Test; 17 | import org.junit.runner.RunWith; 18 | import org.junit.runners.Parameterized; 19 | 20 | @RunWith(Parameterized.class) 21 | public class TimeUtilTest { 22 | 23 | @Parameterized.Parameters 24 | public static Collection data() { 25 | return Arrays.asList(new Object[][] { { Duration.ofSeconds(86400), "24:00:00" }, 26 | { Duration.ofSeconds(86400 - 1), "23:59:59" }, { Duration.ofSeconds(0), "00:00:00" }, 27 | { Duration.ofSeconds(-1), "00:00:-1" } }); 28 | } 29 | 30 | Duration duration; 31 | String formatted; 32 | 33 | public TimeUtilTest(Duration duration, String formatted) { 34 | this.duration = duration; 35 | this.formatted = formatted; 36 | } 37 | 38 | @Test 39 | public void testFormatDuration() { 40 | assertEquals(TimeUtil.formatDuration(duration), formatted); 41 | } 42 | 43 | @Test 44 | public void testNtpTime() { 45 | long currentTime = TimeUtil.currentTimeMillis(); 46 | long offset = TimeUtil.getTimeOffsetFromNtp(); 47 | // ensure the time is within the actual time offset. 48 | Assert.assertTrue(Math.abs(currentTime + offset - TimeUtil.currentTimeMillis()) < 1000); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/test/resources/database/database-v0.tgz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/semuxproject/semux-core/3ac4bf663f3eb4e0ab4976bb049ba30ffe8181f4/src/test/resources/database/database-v0.tgz -------------------------------------------------------------------------------- /src/test/resources/ipfilter/blacklist.json: -------------------------------------------------------------------------------- 1 | { 2 | "rules" : [ { 3 | "type" : "REJECT", 4 | "address" : "1.2.3.4" 5 | }, { 6 | "type" : "REJECT", 7 | "address" : "5.6.7.8" 8 | } ] 9 | } -------------------------------------------------------------------------------- /src/test/resources/ipfilter/empty.json: -------------------------------------------------------------------------------- 1 | { 2 | "rules" : [ ] 3 | } -------------------------------------------------------------------------------- /src/test/resources/ipfilter/exception_empty_object.json: -------------------------------------------------------------------------------- 1 | {} -------------------------------------------------------------------------------- /src/test/resources/ipfilter/exception_type_cast1.json: -------------------------------------------------------------------------------- 1 | ["rules"] -------------------------------------------------------------------------------- /src/test/resources/ipfilter/exception_type_cast2.json: -------------------------------------------------------------------------------- 1 | { 2 | "rules": {} 3 | } -------------------------------------------------------------------------------- /src/test/resources/ipfilter/exception_type_cast3.json: -------------------------------------------------------------------------------- 1 | { 2 | "rules": [ 3 | [] 4 | ] 5 | } -------------------------------------------------------------------------------- /src/test/resources/ipfilter/exception_typo_in_rules1.json: -------------------------------------------------------------------------------- 1 | { 2 | "rules": [ 3 | {"types": "ACCEPT", "address": "127.0.0.1/8"} 4 | ] 5 | } -------------------------------------------------------------------------------- /src/test/resources/ipfilter/exception_typo_in_rules2.json: -------------------------------------------------------------------------------- 1 | { 2 | "rules": [ 3 | {"type": "ACCEP", "address": "127.0.0.1/8"} 4 | ] 5 | } -------------------------------------------------------------------------------- /src/test/resources/ipfilter/exception_typo_in_rules3.json: -------------------------------------------------------------------------------- 1 | { 2 | "rules": [ 3 | {"type": "ACCEP", "address": "127.0.0."} 4 | ] 5 | } -------------------------------------------------------------------------------- /src/test/resources/ipfilter/exception_typo_in_rules4.json: -------------------------------------------------------------------------------- 1 | { 2 | "rules": [ 3 | {"types": "ACCEPT", "addresss": "127.0.0.1/8"} 4 | ] 5 | } -------------------------------------------------------------------------------- /src/test/resources/ipfilter/exception_typo_in_rules5.json: -------------------------------------------------------------------------------- 1 | { 2 | "rulesss": [ 3 | ] 4 | } -------------------------------------------------------------------------------- /src/test/resources/ipfilter/whitelist.json: -------------------------------------------------------------------------------- 1 | { 2 | "rules" : [ { 3 | "type" : "ACCEPT", 4 | "address" : "127.0.0.1/8" 5 | }, { 6 | "type" : "ACCEPT", 7 | "address" : "192.168.0.0/16" 8 | }, { 9 | "type" : "REJECT", 10 | "address" : "0.0.0.0/0" 11 | } ] 12 | } -------------------------------------------------------------------------------- /src/test/resources/log4j2-test.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker: -------------------------------------------------------------------------------- 1 | mock-maker-inline -------------------------------------------------------------------------------- /src/test/resources/wallet/backup_from_linux: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/semuxproject/semux-core/3ac4bf663f3eb4e0ab4976bb049ba30ffe8181f4/src/test/resources/wallet/backup_from_linux -------------------------------------------------------------------------------- /src/test/resources/wallet/backup_from_macos: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/semuxproject/semux-core/3ac4bf663f3eb4e0ab4976bb049ba30ffe8181f4/src/test/resources/wallet/backup_from_macos -------------------------------------------------------------------------------- /src/test/resources/wallet/backup_from_windows: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/semuxproject/semux-core/3ac4bf663f3eb4e0ab4976bb049ba30ffe8181f4/src/test/resources/wallet/backup_from_windows --------------------------------------------------------------------------------