├── .gitignore ├── .travis.gofmt.sh ├── .travis.gotest.sh ├── CONTRIBUTING.md ├── LICENSE ├── Makefile ├── README.md ├── README_CN.md ├── TODO ├── account ├── account.go ├── account_test.go ├── client.go ├── client_test.go ├── file_store.go ├── file_store_test.go ├── identity.go ├── identity_test.go └── store.go ├── cmd ├── abi │ ├── abi_mgr.go │ ├── native_abi.go │ ├── native_abi_script │ │ ├── auth.json │ │ ├── global_param.json │ │ ├── governance.json │ │ ├── ong.json │ │ ├── ont.json │ │ └── ontid.json │ └── neovm_abi.go ├── account.go ├── account_cmd.go ├── common │ ├── common.go │ └── notice.go ├── config.go ├── export_cmd.go ├── import_cmd.go ├── info_cmd.go ├── sig_tx_cmd.go ├── sigsvr │ ├── common │ │ ├── common.go │ │ └── errors.go │ ├── handler.go │ ├── handlers │ │ ├── create_account.go │ │ ├── create_account_test.go │ │ ├── export_account.go │ │ ├── export_account_test.go │ │ ├── sig_data.go │ │ └── sig_data_test.go │ ├── httpsvr.go │ ├── import_wallet.go │ └── store │ │ ├── common.go │ │ └── wallet_store.go ├── tx_cmd.go ├── usage.go └── utils │ ├── export.go │ ├── flags.go │ ├── params.go │ ├── params_test.go │ ├── rpc.go │ ├── utils.go │ └── utils_test.go ├── common ├── address.go ├── codec.go ├── common.go ├── config │ └── config.go ├── constants │ └── constants.go ├── fixed64.go ├── inventory.go ├── limited_writer.go ├── log │ ├── log.go │ └── log_test.go ├── merkle_tree.go ├── password │ ├── password.go │ ├── password_test.go │ └── password_windows.go ├── safeMath.go ├── serialization │ ├── serialize.go │ └── serialize_test.go ├── sort.go ├── test │ ├── address_test.go │ ├── common_test.go │ ├── fixed64_test.go │ ├── limited_writer_test.go │ ├── merkle_tree_test.go │ ├── sort_test.go │ ├── uint256_test.go │ ├── zero_copy_sink_test.go │ └── zero_copy_source_test.go ├── uint256.go ├── zero_copy_sink.go └── zero_copy_source.go ├── consensus ├── actor │ ├── actor.go │ └── message.go ├── consensus.go ├── policy.go ├── policy_level.go ├── solo │ └── solo.go └── vbft │ ├── README.md │ ├── block_pool.go │ ├── chain_store.go │ ├── chain_store_test.go │ ├── config │ ├── config.go │ ├── config_test.go │ ├── genesis.go │ ├── genesis_test.go │ ├── types.go │ └── types_test.go │ ├── event_timer.go │ ├── event_timer_test.go │ ├── msg_builder.go │ ├── msg_builder_test.go │ ├── msg_pool.go │ ├── msg_pool_test.go │ ├── msg_types.go │ ├── msg_types_test.go │ ├── node_sync.go │ ├── node_utils.go │ ├── node_utils_test.go │ ├── peer_pool.go │ ├── peer_pool_test.go │ ├── service.go │ ├── state_mgmt.go │ ├── state_mgmt_test.go │ ├── types.go │ ├── types_test.go │ ├── utils.go │ └── utils_test.go ├── core ├── genesis │ ├── genesis.go │ └── genesis_test.go ├── ledger │ └── ledger.go ├── payload │ ├── invoke_code.go │ └── invoke_code_test.go ├── signature │ ├── signature.go │ ├── signature_test.go │ └── signer.go ├── states │ ├── bookkeeper.go │ ├── bookkeeper_test.go │ ├── interfaces.go │ ├── state_base.go │ ├── state_base_test.go │ ├── storage_item.go │ ├── storage_item_test.go │ ├── storage_key.go │ ├── storage_key_test.go │ ├── validator_state.go │ ├── validator_state_test.go │ ├── vote_state.go │ └── vote_state_test.go ├── store │ ├── common │ │ ├── data_entry_prefix.go │ │ └── store.go │ ├── ledgerstore │ │ ├── block_cache.go │ │ ├── block_store.go │ │ ├── block_store_test.go │ │ ├── event_store.go │ │ ├── ledger_store.go │ │ ├── ledger_store_test.go │ │ ├── state_cache.go │ │ ├── state_store.go │ │ ├── state_store_test.go │ │ ├── tx_handler.go │ │ └── tx_handler_test.go │ ├── leveldbstore │ │ ├── leveldb_store.go │ │ └── leveldb_store_test.go │ ├── overlaydb │ │ ├── bench_test.go │ │ ├── iterator.go │ │ ├── memdb.go │ │ ├── memdb_dot.go │ │ ├── memdb_test.go │ │ ├── overlaydb.go │ │ └── overlaydb_test.go │ └── store.go ├── test │ ├── header_test.go │ ├── transaction_test.go │ └── verify_test.go ├── types │ ├── address.go │ ├── address_test.go │ ├── block.go │ ├── header.go │ ├── header_test.go │ ├── smartcontract.go │ ├── transaction.go │ ├── transaction_attribute.go │ └── version.go ├── validation │ └── transaction_validator.go └── vote │ └── validator.go ├── docker ├── Docker.build └── Dockerfile ├── errors └── errcode.go ├── events ├── actor_event.go ├── actor_event_test.go ├── event.go ├── event_test.go ├── event_type.go ├── message │ └── message.go └── subscriber.go ├── glide.yaml ├── go.mod ├── go.sum ├── http ├── base │ ├── actor │ │ ├── consensus.go │ │ ├── event.go │ │ ├── ledger.go │ │ ├── net_server.go │ │ └── txnpool.go │ ├── common │ │ ├── common.go │ │ └── payload_to_hex.go │ ├── error │ │ └── error.go │ ├── rest │ │ ├── interfaces.go │ │ └── result.go │ └── rpc │ │ ├── interfaces.go │ │ ├── local_interfaces.go │ │ ├── result.go │ │ └── rpc.go ├── jsonrpc │ └── rpc_server.go ├── localrpc │ └── local_server.go ├── nodeinfo │ ├── ngbnodeinfo.go │ ├── server.go │ └── template.go ├── restful │ ├── restful │ │ ├── router.go │ │ └── server.go │ └── server.go ├── test │ └── func_test.go └── websocket │ ├── server.go │ ├── session │ ├── session.go │ └── sessionlist.go │ └── websocket │ └── server.go ├── main.go ├── merkle ├── file_hash_store.go ├── merkle_hasher.go ├── merkle_tree.go ├── merkle_tree_test.go └── util.go ├── native ├── event │ ├── event.go │ └── notify_event_args.go ├── native.go ├── service │ ├── cross_chain_manager │ │ ├── bsc │ │ │ └── bsc_handler.go │ │ ├── btc │ │ │ ├── btc_handler.go │ │ │ ├── btc_handler_test.go │ │ │ ├── states.go │ │ │ ├── states_test.go │ │ │ ├── utils.go │ │ │ └── utils_test.go │ │ ├── bytom │ │ │ └── bytom_handler.go │ │ ├── common │ │ │ ├── param.go │ │ │ ├── param_test.go │ │ │ └── utils.go │ │ ├── consensus_vote │ │ │ ├── states.go │ │ │ ├── utils.go │ │ │ ├── vote_handler.go │ │ │ └── vote_handler_test.go │ │ ├── cosmos │ │ │ ├── cosmos_handler.go │ │ │ ├── cosmos_handler_test.go │ │ │ └── proof.go │ │ ├── entrance.go │ │ ├── eth │ │ │ ├── eth_handler.go │ │ │ ├── eth_handler_test.go │ │ │ ├── proof.go │ │ │ ├── proof_test.go │ │ │ ├── states.go │ │ │ └── utils.go │ │ ├── harmony │ │ │ ├── harmony_handler.go │ │ │ └── harmony_handler_test.go │ │ ├── heco │ │ │ ├── heco_handler.go │ │ │ └── heco_handler_test.go │ │ ├── hsc │ │ │ ├── hsc_handler.go │ │ │ └── hsc_handler_test.go │ │ ├── msc │ │ │ └── msc_handler.go │ │ ├── neo │ │ │ ├── neo_handler.go │ │ │ ├── neo_handler_test.go │ │ │ └── utils.go │ │ ├── neo3 │ │ │ ├── neo_handler.go │ │ │ ├── neo_handler_test.go │ │ │ └── utils.go │ │ ├── neo3legacy │ │ │ ├── neo_handler.go │ │ │ ├── neo_handler_test.go │ │ │ └── utils.go │ │ ├── okex │ │ │ └── okex_handler.go │ │ ├── ont │ │ │ ├── ont_handler.go │ │ │ ├── utils.go │ │ │ └── utils_test.go │ │ ├── pixiechain │ │ │ ├── pixiechain_handler.go │ │ │ ├── pixiechain_handler_test.go │ │ │ └── types.go │ │ ├── polygon │ │ │ └── bor_handler.go │ │ ├── quorum │ │ │ ├── quorum_handler.go │ │ │ ├── quorum_handler_test.go │ │ │ └── utils.go │ │ ├── ripple │ │ │ ├── param.go │ │ │ ├── ripple_handler.go │ │ │ ├── ripple_handler_test.go │ │ │ ├── states.go │ │ │ └── utils.go │ │ ├── starcoin │ │ │ ├── event.go │ │ │ ├── starcoin_handler.go │ │ │ ├── utils.go │ │ │ └── utils_test.go │ │ ├── zilliqa │ │ │ └── zilliqa_handler.go │ │ └── zilliqalegacy │ │ │ └── zilliqa_handler.go │ ├── governance │ │ ├── neo3_state_manager │ │ │ ├── neo3_state_manager.go │ │ │ ├── neo3_state_manager_test.go │ │ │ ├── param.go │ │ │ ├── param_test.go │ │ │ └── utils.go │ │ ├── node_manager │ │ │ ├── method.go │ │ │ ├── node_manager.go │ │ │ ├── param.go │ │ │ ├── states.go │ │ │ ├── states_test.go │ │ │ └── utils.go │ │ ├── relayer_manager │ │ │ ├── param.go │ │ │ ├── param_test.go │ │ │ ├── relayer_manager.go │ │ │ ├── relayer_manager_test.go │ │ │ └── utils.go │ │ ├── replenish │ │ │ ├── param.go │ │ │ └── replenish.go │ │ ├── side_chain_manager │ │ │ ├── param.go │ │ │ ├── param_test.go │ │ │ ├── side_chain_manager.go │ │ │ ├── side_chain_manager_test.go │ │ │ ├── states.go │ │ │ ├── states_test.go │ │ │ └── utils.go │ │ └── signature_manager │ │ │ ├── param.go │ │ │ ├── signature_manager.go │ │ │ ├── signature_manager_test.go │ │ │ ├── states.go │ │ │ └── utils.go │ ├── header_sync │ │ ├── bsc │ │ │ ├── header_sync.go │ │ │ └── header_sync_test.go │ │ ├── btc │ │ │ ├── header_sync.go │ │ │ ├── header_sync_test.go │ │ │ ├── states.go │ │ │ ├── states_test.go │ │ │ ├── utils.go │ │ │ └── utils_test.go │ │ ├── bytom │ │ │ └── header_sync.go │ │ ├── common │ │ │ ├── param.go │ │ │ └── param_test.go │ │ ├── cosmos │ │ │ ├── header_sync.go │ │ │ ├── header_sync_test.go │ │ │ ├── states.go │ │ │ └── utils.go │ │ ├── entrance.go │ │ ├── eth │ │ │ ├── cache.go │ │ │ ├── header1559.go │ │ │ ├── header_sync.go │ │ │ ├── header_sync_test.go │ │ │ ├── rlp │ │ │ │ ├── decode.go │ │ │ │ ├── doc.go │ │ │ │ ├── encode.go │ │ │ │ ├── iterator.go │ │ │ │ ├── raw.go │ │ │ │ ├── safe.go │ │ │ │ ├── typecache.go │ │ │ │ └── unsafe.go │ │ │ └── utils.go │ │ ├── harmony │ │ │ ├── header_sync.go │ │ │ ├── header_sync_test.go │ │ │ ├── state.go │ │ │ ├── state_test.go │ │ │ └── utils.go │ │ ├── heco │ │ │ ├── header_sync.go │ │ │ ├── header_sync_test.go │ │ │ └── utils.go │ │ ├── hsc │ │ │ ├── header_sync.go │ │ │ └── header_sync_test.go │ │ ├── msc │ │ │ ├── header_sync.go │ │ │ ├── header_sync_test.go │ │ │ └── snapshot.go │ │ ├── neo │ │ │ ├── header_sync.go │ │ │ ├── header_sync_test.go │ │ │ ├── states.go │ │ │ ├── states_test.go │ │ │ └── utils.go │ │ ├── neo3 │ │ │ ├── header_sync.go │ │ │ ├── header_sync_test.go │ │ │ ├── states.go │ │ │ ├── states_test.go │ │ │ └── utils.go │ │ ├── neo3legacy │ │ │ ├── header_sync.go │ │ │ ├── header_sync_test.go │ │ │ ├── states.go │ │ │ ├── states_test.go │ │ │ └── utils.go │ │ ├── okex │ │ │ ├── ethsecp256k1 │ │ │ │ ├── codec.go │ │ │ │ └── ethsecp256k1.go │ │ │ └── header_sync.go │ │ ├── ont │ │ │ ├── header_sync.go │ │ │ ├── header_sync_test.go │ │ │ ├── states.go │ │ │ ├── states_test.go │ │ │ └── utils.go │ │ ├── pixiechain │ │ │ ├── common.go │ │ │ ├── hardfork.go │ │ │ ├── header_sync.go │ │ │ ├── header_sync_test.go │ │ │ ├── types.go │ │ │ └── utils.go │ │ ├── polygon │ │ │ ├── bor_header_sync.go │ │ │ ├── errors.go │ │ │ ├── header_sync_test.go │ │ │ ├── heimdall_header_sync.go │ │ │ ├── snapshot.go │ │ │ ├── span.go │ │ │ ├── types │ │ │ │ ├── common │ │ │ │ │ ├── bit_array.go │ │ │ │ │ ├── bytes.go │ │ │ │ │ ├── byteslice.go │ │ │ │ │ ├── math.go │ │ │ │ │ ├── nil.go │ │ │ │ │ └── random.go │ │ │ │ ├── heimdall_block.go │ │ │ │ ├── heimdall_canonical.go │ │ │ │ ├── heimdall_codec.go │ │ │ │ ├── heimdall_commit.go │ │ │ │ ├── heimdall_encoding_helper.go │ │ │ │ ├── heimdall_header.go │ │ │ │ ├── heimdall_side_tx.go │ │ │ │ ├── heimdall_signed_msg_type.go │ │ │ │ ├── heimdall_span.go │ │ │ │ ├── heimdall_validator.go │ │ │ │ ├── heimdall_validator_set.go │ │ │ │ ├── heimdall_vote.go │ │ │ │ └── secp256k1 │ │ │ │ │ ├── secp256k1.go │ │ │ │ │ └── secp256k1_nocgo.go │ │ │ ├── validator.go │ │ │ └── validator_set.go │ │ ├── quorum │ │ │ ├── header_sync.go │ │ │ ├── quorum_handler_test.go │ │ │ ├── quorum_tools.go │ │ │ ├── states.go │ │ │ └── utils.go │ │ ├── starcoin │ │ │ ├── header_sync.go │ │ │ ├── header_sync_test.go │ │ │ └── utils.go │ │ ├── zilliqa │ │ │ ├── header_sync.go │ │ │ ├── header_sync_test.go │ │ │ ├── test_blocks │ │ │ ├── test_genesis │ │ │ └── utils.go │ │ └── zilliqalegacy │ │ │ ├── header_sync.go │ │ │ ├── header_sync_test.go │ │ │ └── utils.go │ ├── init.go │ └── utils │ │ ├── operation.go │ │ ├── params.go │ │ └── store.go ├── states │ ├── contract.go │ └── contract_test.go └── storage │ ├── cachedb.go │ └── cachedb_test.go ├── p2pserver ├── actor │ ├── req │ │ ├── consensus.go │ │ └── txnpool.go │ └── server │ │ ├── actor.go │ │ ├── actor_test.go │ │ └── common.go ├── block_sync.go ├── common │ ├── checksum.go │ ├── checksum_test.go │ └── p2p_common.go ├── link │ ├── link.go │ └── link_test.go ├── message │ ├── msg_pack │ │ └── msg_pack.go │ ├── types │ │ ├── address.go │ │ ├── address_req.go │ │ ├── address_req_test.go │ │ ├── address_test.go │ │ ├── block.go │ │ ├── block_header.go │ │ ├── block_headers_req.go │ │ ├── block_headers_req_test.go │ │ ├── blocks_req.go │ │ ├── blocks_req_test.go │ │ ├── consensus.go │ │ ├── consensus_payload.go │ │ ├── data_req.go │ │ ├── data_req_test.go │ │ ├── disconnected.go │ │ ├── inventory.go │ │ ├── message.go │ │ ├── message_test.go │ │ ├── notfound.go │ │ ├── notfound_test.go │ │ ├── ping.go │ │ ├── ping_test.go │ │ ├── pong.go │ │ ├── pong_test.go │ │ ├── transaction.go │ │ ├── verack.go │ │ ├── verack_test.go │ │ └── version.go │ └── utils │ │ ├── msg_handler.go │ │ ├── msg_handler_test.go │ │ ├── msg_router.go │ │ └── msg_router_test.go ├── net │ ├── netserver │ │ ├── net_utils.go │ │ ├── netserver.go │ │ └── netserver_test.go │ └── protocol │ │ └── server.go ├── p2pserver.go ├── p2pserver_test.go └── peer │ ├── nbr_peer_test.go │ ├── nbr_peers.go │ ├── peer.go │ └── peer_test.go ├── scripts ├── install_dependencies.sh ├── install_linux_dependencies.sh └── install_macos_dependencies.sh ├── sigsvr.go ├── txnpool ├── common │ ├── transaction_pool.go │ ├── transaction_pool_test.go │ └── txnpool_common.go ├── proc │ ├── txnpool_actor.go │ ├── txnpool_actor_test.go │ ├── txnpool_server.go │ ├── txnpool_server_test.go │ ├── txnpool_worker.go │ └── txnpool_worker_test.go ├── server.go └── test │ └── txnpool_test.go └── validator ├── db ├── best_block.go ├── config.json ├── key_prefix.go ├── store.go ├── store_test.go └── transaction_provider.go ├── increment └── increment.go ├── stateful ├── config.json └── stateful_validator.go ├── stateless ├── stateless_validator.go └── stateless_validator_test.go └── types └── messages.go /.gitignore: -------------------------------------------------------------------------------- 1 | /idea/* 2 | /temp/* 3 | idea/* 4 | temp/* 5 | temp/ 6 | idea/ 7 | build/ 8 | tools/ 9 | wsl/* 10 | *.patch 11 | .idea/ 12 | GoOnchain.iml 13 | .log 14 | .DS_Store 15 | *.orig 16 | *.rej 17 | vendor/* 18 | !vendor/github.com/ontio/ontology-crypto/* 19 | Chain/* 20 | /config/* 21 | nodectl 22 | ontology 23 | wallet.dat 24 | poly 25 | cscope* 26 | *.exe 27 | **/*.log 28 | **/*.txt 29 | neosnapshot/* 30 | glide.lock 31 | *.iml 32 | docker/payload 33 | config.json 34 | peers.recent 35 | .vscode 36 | main 37 | -------------------------------------------------------------------------------- /.travis.gofmt.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # code from https://github.com/Seklfreak/Robyul2 3 | unset dirs files 4 | dirs=$(go list -f {{.Dir}} ./... | grep -v /vendor/) 5 | for d in $dirs 6 | do 7 | for f in $d/*.go 8 | do 9 | files="${files} $f" 10 | done 11 | done 12 | diff <(gofmt -d $files) <(echo -n) 13 | -------------------------------------------------------------------------------- /.travis.gotest.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # code from https://github.com/Seklfreak/Robyul2 3 | unset dirs files 4 | dirs=$(go list ./... | grep -v vendor/ | grep -v ontology$) 5 | set -x -e 6 | for d in $dirs 7 | do 8 | go test -v $d 9 | done 10 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | Contributing 2 | ========================================= 3 | 4 | We welcome contributions. Please follow the guidelines when opening issues and contributing code to the repo. 5 | 6 | Contributing 7 | ------------ 8 | 9 | We follow the [fork-and-pull](https://help.github.com/en/articles/about-collaborative-development-models) Git workflow: 10 | 11 | 1. **Fork** the repo on GitHub 12 | 2. **Clone** it to your own machine 13 | 3. **Commit** changes to your fork 14 | 4. **Push** changes to your fork 15 | 5. Submit a **Pull request** for review 16 | 17 | **NOTE:** Be sure to merge the latest changes before making a pull request! 18 | 19 | Pull Requests 20 | ------ 21 | As outlined in Keavy McMinn's article ["How to write the perfect pull request"](https://github.blog/2015-01-21-how-to-write-the-perfect-pull-request/), you should include: 22 | 23 | 1. The purpose of the PR 24 | 2. A brief overview of what you did 25 | 3. Tag any issues that the PR relates to and [close issues with a keyword](https://help.github.com/en/articles/closing-issues-using-keywords) 26 | 4. What kind of feedback you're looking for (if any) 27 | 5. Tag individuals you want feedback from (if any) 28 | 29 | Issues 30 | ------ 31 | 32 | Feel free to submit issues and enhancement requests [here](https://github.com/polynetwork/poly/issues/new). Please consider [how to ask a good question](https://stackoverflow.com/help/how-to-ask) and take the time to research your issue before asking for help. 33 | 34 | Duplicate questions will be closed. 35 | 36 | Any unrelated comments or questions can be asked in the [Poly Network Discord](https://discord.gg/y6MuEnq). 37 | -------------------------------------------------------------------------------- /README_CN.md: -------------------------------------------------------------------------------- 1 | 2 |

Poly

3 |

Version 1.0

4 | 5 | [![Discord](https://img.shields.io/discord/102860784329052160.svg)](https://discord.gg/y6MuEnq) 6 | 7 | [English](README.md) | 中文 -------------------------------------------------------------------------------- /TODO: -------------------------------------------------------------------------------- 1 | Automatic found the seed node via the seed broadcast 2 | -------------------------------------------------------------------------------- /account/account_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | package account 19 | 20 | import ( 21 | "github.com/polynetwork/poly/core/types" 22 | "github.com/stretchr/testify/assert" 23 | "os" 24 | "testing" 25 | ) 26 | 27 | func TestNewAccount(t *testing.T) { 28 | defer func() { 29 | os.RemoveAll("Log/") 30 | }() 31 | 32 | names := []string{ 33 | "", 34 | "SHA224withECDSA", 35 | "SHA256withECDSA", 36 | "SHA384withECDSA", 37 | "SHA512withECDSA", 38 | "SHA3-224withECDSA", 39 | "SHA3-256withECDSA", 40 | "SHA3-384withECDSA", 41 | "SHA3-512withECDSA", 42 | "RIPEMD160withECDSA", 43 | "SM3withSM2", 44 | "SHA512withEdDSA", 45 | } 46 | accounts := make([]*Account, len(names)) 47 | for k, v := range names { 48 | accounts[k] = NewAccount(v) 49 | assert.NotNil(t, accounts[k]) 50 | assert.NotNil(t, accounts[k].PrivateKey) 51 | assert.NotNil(t, accounts[k].PublicKey) 52 | assert.NotNil(t, accounts[k].Address) 53 | assert.NotNil(t, accounts[k].PrivKey()) 54 | assert.NotNil(t, accounts[k].PubKey()) 55 | assert.NotNil(t, accounts[k].Scheme()) 56 | assert.Equal(t, accounts[k].Address, types.AddressFromPubKey(accounts[k].PublicKey)) 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /account/identity_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package account 20 | 21 | import ( 22 | "encoding/hex" 23 | "testing" 24 | ) 25 | 26 | var id = "did:ont:TSS6S4Xhzt5wtvRBTm4y3QCTRqB4BnU7vT" 27 | 28 | func TestCreate(t *testing.T) { 29 | nonce, _ := hex.DecodeString("4c6b58adc6b8c6774eee0eb07dac4e198df87aae28f8932db3982edf3ff026e4") 30 | id1, err := CreateID(nonce) 31 | if err != nil { 32 | t.Fatal(err) 33 | } 34 | t.Log("result ID:", id1) 35 | if id != id1 { 36 | t.Fatal("expected ID:", id) 37 | } 38 | } 39 | 40 | func TestVerify(t *testing.T) { 41 | t.Log("verify", id) 42 | if !VerifyID(id) { 43 | t.Error("error: failed") 44 | } 45 | 46 | invalid := []string{ 47 | "did:ont:", 48 | "did:else:TSS6S4Xhzt5wtvRBTm4y3QCTRqB4BnU7vT", 49 | "TSS6S4Xhzt5wtvRBTm4y3QCTRqB4BnU7vT", 50 | "did:else:TSS6S4Xhzt5wtvRBTm4y3QCT", 51 | "did:ont:TSS6S4Xhzt5wtvRBTm4y3QCTRqB4BnU7vt", 52 | } 53 | 54 | for _, v := range invalid { 55 | t.Log("verify", v) 56 | if VerifyID(v) { 57 | t.Error("error: passed") 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /account/store.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package account 20 | 21 | import ( 22 | "github.com/polynetwork/poly/common" 23 | ) 24 | 25 | type ClientStore interface { 26 | BuildDatabase(path string) 27 | 28 | SaveStoredData(name string, value []byte) 29 | 30 | LoadStoredData(name string) []byte 31 | 32 | LoadAccount() map[common.Address]*Account 33 | } 34 | -------------------------------------------------------------------------------- /cmd/sigsvr/handler.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package sigsvr 20 | 21 | import "github.com/polynetwork/poly/cmd/sigsvr/handlers" 22 | 23 | func init() { 24 | DefCliRpcSvr.RegHandler("createaccount", handlers.CreateAccount) 25 | DefCliRpcSvr.RegHandler("exportaccount", handlers.ExportAccount) 26 | DefCliRpcSvr.RegHandler("sigdata", handlers.SigData) 27 | } 28 | -------------------------------------------------------------------------------- /cmd/sigsvr/handlers/create_account_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | package handlers 19 | 20 | import ( 21 | clisvrcom "github.com/polynetwork/poly/cmd/sigsvr/common" 22 | "testing" 23 | ) 24 | 25 | func TestCreateAccount(t *testing.T) { 26 | walletStore := clisvrcom.DefWalletStore 27 | req := &clisvrcom.CliRpcRequest{ 28 | Qid: "t", 29 | Method: "createaccount", 30 | Pwd: string(pwd), 31 | } 32 | resp := &clisvrcom.CliRpcResponse{} 33 | CreateAccount(req, resp) 34 | if resp.ErrorCode != 0 { 35 | t.Errorf("CreateAccount failed. ErrorCode:%d", resp.ErrorCode) 36 | return 37 | } 38 | createRsp, ok := resp.Result.(*CreateAccountRsp) 39 | if !ok { 40 | t.Errorf("CreateAccount resp asset to CreateAccountRsp failed") 41 | return 42 | } 43 | _, err := walletStore.GetAccountByAddress(createRsp.Account, pwd) 44 | if err != nil { 45 | t.Errorf("Test CreateAccount failed GetAccountByAddress error:%s", err) 46 | return 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /cmd/sigsvr/handlers/sig_data_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | package handlers 19 | 20 | import ( 21 | "encoding/hex" 22 | "encoding/json" 23 | clisvrcom "github.com/polynetwork/poly/cmd/sigsvr/common" 24 | "testing" 25 | ) 26 | 27 | func TestSigData(t *testing.T) { 28 | defAcc, err := testWallet.GetDefaultAccount(pwd) 29 | if err != nil { 30 | t.Errorf("GetDefaultAccount error:%s", err) 31 | return 32 | } 33 | 34 | rawData := []byte("HelloWorld") 35 | rawReq := &SigDataReq{ 36 | RawData: hex.EncodeToString(rawData), 37 | } 38 | data, err := json.Marshal(rawReq) 39 | if err != nil { 40 | t.Errorf("json.Marshal SigDataReq error:%s", err) 41 | return 42 | } 43 | req := &clisvrcom.CliRpcRequest{ 44 | Qid: "t", 45 | Method: "sigdata", 46 | Params: data, 47 | Account: defAcc.Address.ToBase58(), 48 | Pwd: string(pwd), 49 | } 50 | resp := &clisvrcom.CliRpcResponse{} 51 | SigData(req, resp) 52 | if resp.ErrorCode != 0 { 53 | t.Errorf("SigData failed. ErrorCode:%d", resp.ErrorCode) 54 | return 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /cmd/utils/utils_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package utils 20 | 21 | import ( 22 | "github.com/stretchr/testify/assert" 23 | "testing" 24 | ) 25 | 26 | func TestGenExportBlocksFileName(t *testing.T) { 27 | name := "blocks.dat" 28 | start := uint32(0) 29 | end := uint32(100) 30 | fileName := GenExportBlocksFileName(name, start, end) 31 | assert.Equal(t, "blocks_0_100.dat", fileName) 32 | name = "blocks" 33 | fileName = GenExportBlocksFileName(name, start, end) 34 | assert.Equal(t, "blocks_0_100", fileName) 35 | name = "blocks." 36 | fileName = GenExportBlocksFileName(name, start, end) 37 | assert.Equal(t, "blocks_0_100.", fileName) 38 | name = "blocks.export.dat" 39 | fileName = GenExportBlocksFileName(name, start, end) 40 | assert.Equal(t, "blocks.export_0_100.dat", fileName) 41 | } 42 | -------------------------------------------------------------------------------- /common/codec.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | package common 19 | 20 | type Serializable interface { 21 | Serialization(sink *ZeroCopySink) 22 | } 23 | 24 | func SerializeToBytes(values ...Serializable) []byte { 25 | sink := NewZeroCopySink(nil) 26 | for _, val := range values { 27 | val.Serialization(sink) 28 | } 29 | 30 | return sink.Bytes() 31 | } 32 | -------------------------------------------------------------------------------- /common/common.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package common 20 | 21 | import ( 22 | "encoding/hex" 23 | "math/rand" 24 | "os" 25 | ) 26 | 27 | // GetNonce returns random nonce 28 | func GetNonce() uint64 { 29 | // Fixme replace with the real random number generator 30 | nonce := uint64(rand.Uint32())<<32 + uint64(rand.Uint32()) 31 | return nonce 32 | } 33 | 34 | // ToHexString convert []byte to hex string 35 | func ToHexString(data []byte) string { 36 | return hex.EncodeToString(data) 37 | } 38 | 39 | // HexToBytes convert hex string to []byte 40 | func HexToBytes(value string) ([]byte, error) { 41 | return hex.DecodeString(value) 42 | } 43 | 44 | func ToArrayReverse(arr []byte) []byte { 45 | l := len(arr) 46 | x := make([]byte, 0) 47 | for i := l - 1; i >= 0; i-- { 48 | x = append(x, arr[i]) 49 | } 50 | return x 51 | } 52 | 53 | // FileExisted checks whether filename exists in filesystem 54 | func FileExisted(filename string) bool { 55 | _, err := os.Stat(filename) 56 | return err == nil || os.IsExist(err) 57 | } 58 | -------------------------------------------------------------------------------- /common/constants/constants.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package constants 20 | 21 | import ( 22 | "time" 23 | ) 24 | 25 | // genesis constants 26 | var ( 27 | //TODO: modify this when on mainnet 28 | GENESIS_BLOCK_TIMESTAMP = uint32(time.Date(2020, time.August, 10, 0, 0, 0, 0, time.UTC).Unix()) 29 | ) 30 | 31 | // multi-sig constants 32 | const MULTI_SIG_MAX_PUBKEY_SIZE = 16 33 | 34 | // transaction constants 35 | const TX_MAX_SIG_SIZE = 16 36 | 37 | // network magic number 38 | const ( 39 | NETWORK_MAGIC_MAINNET = 0x8c6077ab 40 | NETWORK_MAGIC_TESTNET = 0x2ddf8829 41 | ) 42 | 43 | // extra info change height 44 | const EXTRA_INFO_HEIGHT_MAINNET = 2917744 45 | const EXTRA_INFO_HEIGHT_TESTNET = 1664798 46 | 47 | // eth 1559 height 48 | const ETH1559_HEIGHT_MAINNET = 12965000 49 | const ETH1559_HEIGHT_TESTNET = 10499401 50 | 51 | // heco 120 height 52 | const HECO120_HEIGHT_MAINNET = 8577000 53 | const HECO120_HEIGHT_TESTNET = 8290000 54 | 55 | const POLYGON_SNAP_CHAINID_MAINNET = 16 56 | 57 | // eth arrow glacier upgrade 58 | const ETH4345_HEIGHT_MAINNET = 13_773_000 59 | -------------------------------------------------------------------------------- /common/inventory.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package common 20 | 21 | type InventoryType byte 22 | 23 | const ( 24 | TRANSACTION InventoryType = 0x01 25 | BLOCK InventoryType = 0x02 26 | CONSENSUS InventoryType = 0xe0 27 | ) 28 | 29 | //TODO: temp inventory 30 | type Inventory interface { 31 | //sig.SignableData 32 | Hash() Uint256 33 | Verify() error 34 | Type() InventoryType 35 | } 36 | -------------------------------------------------------------------------------- /common/limited_writer.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package common 20 | 21 | import ( 22 | "errors" 23 | "io" 24 | ) 25 | 26 | var ErrWriteExceedLimitedCount = errors.New("writer exceed limited count") 27 | 28 | type LimitedWriter struct { 29 | count uint64 30 | max uint64 31 | writer io.Writer 32 | } 33 | 34 | func NewLimitedWriter(w io.Writer, max uint64) *LimitedWriter { 35 | return &LimitedWriter{ 36 | writer: w, 37 | max: max, 38 | } 39 | } 40 | 41 | func (self *LimitedWriter) Write(buf []byte) (int, error) { 42 | if self.count+uint64(len(buf)) > self.max { 43 | return 0, ErrWriteExceedLimitedCount 44 | } 45 | n, err := self.writer.Write(buf) 46 | self.count += uint64(n) 47 | return n, err 48 | } 49 | 50 | // Count function return counted bytes 51 | func (self *LimitedWriter) Count() uint64 { 52 | return self.count 53 | } 54 | -------------------------------------------------------------------------------- /common/merkle_tree.go: -------------------------------------------------------------------------------- 1 | package common 2 | 3 | /* 4 | * Copyright (C) 2021 The poly network Authors 5 | * This file is part of The poly network library. 6 | * 7 | * The poly network is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU Lesser General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * The poly network is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU Lesser General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU Lesser General Public License 18 | * along with the poly network. If not, see . 19 | */ 20 | import ( 21 | "crypto/sha256" 22 | ) 23 | 24 | // param hashes will be used as workspace 25 | func ComputeMerkleRoot(hashes []Uint256) Uint256 { 26 | if len(hashes) == 0 { 27 | return Uint256{} 28 | } 29 | sha := sha256.New() 30 | var temp Uint256 31 | for len(hashes) != 1 { 32 | n := len(hashes) / 2 33 | for i := 0; i < n; i++ { 34 | sha.Reset() 35 | sha.Write(hashes[2*i][:]) 36 | sha.Write(hashes[2*i+1][:]) 37 | sha.Sum(temp[:0]) 38 | sha.Reset() 39 | sha.Write(temp[:]) 40 | sha.Sum(hashes[i][:0]) 41 | } 42 | if len(hashes) == 2*n+1 { 43 | sha.Reset() 44 | sha.Write(hashes[2*n][:]) 45 | sha.Write(hashes[2*n][:]) 46 | 47 | sha.Sum(temp[:0]) 48 | sha.Reset() 49 | sha.Write(temp[:]) 50 | sha.Sum(hashes[n][:0]) 51 | 52 | hashes = hashes[:n+1] 53 | } else { 54 | hashes = hashes[:n] 55 | } 56 | } 57 | 58 | return hashes[0] 59 | } 60 | -------------------------------------------------------------------------------- /common/password/password_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | package password 19 | 20 | import ( 21 | "github.com/stretchr/testify/assert" 22 | "testing" 23 | ) 24 | 25 | func TestGetAccountPassword(t *testing.T) { 26 | var password, err = GetAccountPassword() 27 | assert.Nil(t, password) 28 | assert.NotNil(t, err) 29 | password, err = GetPassword() 30 | assert.Nil(t, password) 31 | assert.NotNil(t, err) 32 | password, err = GetConfirmedPassword() 33 | assert.Nil(t, password) 34 | assert.NotNil(t, err) 35 | } 36 | -------------------------------------------------------------------------------- /common/safeMath.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package common 20 | 21 | import "math" 22 | 23 | const ( 24 | MAX_UINT64 = math.MaxUint64 25 | MAX_INT64 = math.MaxInt64 26 | ) 27 | 28 | func SafeSub(x, y uint64) (uint64, bool) { 29 | return x - y, x < y 30 | } 31 | 32 | func SafeAdd(x, y uint64) (uint64, bool) { 33 | return x + y, y > MAX_UINT64-x 34 | } 35 | 36 | func SafeMul(x, y uint64) (uint64, bool) { 37 | if x == 0 || y == 0 { 38 | return 0, false 39 | } 40 | return x * y, y > MAX_UINT64/x 41 | } 42 | -------------------------------------------------------------------------------- /common/sort.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | package common 19 | 20 | import "sort" 21 | 22 | type Uint64Slice []uint64 23 | 24 | func (p Uint64Slice) Len() int { return len(p) } 25 | func (p Uint64Slice) Less(i, j int) bool { return p[i] < p[j] } 26 | func (p Uint64Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] } 27 | 28 | func SortUint64s(l []uint64) { 29 | sort.Stable(Uint64Slice(l)) 30 | } 31 | -------------------------------------------------------------------------------- /common/test/address_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package test 20 | 21 | import ( 22 | "bytes" 23 | "crypto/rand" 24 | "github.com/polynetwork/poly/common" 25 | "testing" 26 | 27 | "github.com/stretchr/testify/assert" 28 | ) 29 | 30 | func TestAddressFromBase58(t *testing.T) { 31 | var addr common.Address 32 | rand.Read(addr[:]) 33 | 34 | base58 := addr.ToBase58() 35 | b1 := string(append([]byte{'X'}, []byte(base58)...)) 36 | _, err := common.AddressFromBase58(b1) 37 | 38 | assert.NotNil(t, err) 39 | 40 | b2 := string([]byte(base58)[1:10]) 41 | _, err = common.AddressFromBase58(b2) 42 | 43 | assert.NotNil(t, err) 44 | } 45 | 46 | func TestAddressParseFromBytes(t *testing.T) { 47 | var addr common.Address 48 | rand.Read(addr[:]) 49 | 50 | addr2, _ := common.AddressParseFromBytes(addr[:]) 51 | 52 | assert.Equal(t, addr, addr2) 53 | } 54 | 55 | func TestAddress_Serialize(t *testing.T) { 56 | var addr common.Address 57 | rand.Read(addr[:]) 58 | 59 | buf := bytes.NewBuffer(nil) 60 | addr.Serialize(buf) 61 | 62 | var addr2 common.Address 63 | addr2.Deserialize(buf) 64 | assert.Equal(t, addr, addr2) 65 | } 66 | -------------------------------------------------------------------------------- /common/test/common_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | package test 19 | 20 | import ( 21 | "fmt" 22 | "github.com/polynetwork/poly/common" 23 | "github.com/stretchr/testify/assert" 24 | "testing" 25 | ) 26 | 27 | func TestHexAndBytesTransfer(t *testing.T) { 28 | testBytes := []byte("10, 11, 12, 13, 14, 15, 16, 17, 18, 19") 29 | stringAfterTrans := common.ToHexString(testBytes) 30 | bytesAfterTrans, err := common.HexToBytes(stringAfterTrans) 31 | assert.Nil(t, err) 32 | assert.Equal(t, testBytes, bytesAfterTrans) 33 | } 34 | 35 | func TestGetNonce(t *testing.T) { 36 | nonce1 := common.GetNonce() 37 | nonce2 := common.GetNonce() 38 | assert.NotEqual(t, nonce1, nonce2) 39 | } 40 | 41 | func TestFileExisted(t *testing.T) { 42 | assert.True(t, common.FileExisted("common_test.go")) 43 | assert.True(t, common.FileExisted("common.go")) 44 | assert.False(t, common.FileExisted("../log/log.og")) 45 | assert.False(t, common.FileExisted("../log/log.go")) 46 | assert.True(t, common.FileExisted("./log/log.go")) 47 | } 48 | 49 | func TestBase58(t *testing.T) { 50 | addr := common.ADDRESS_EMPTY 51 | fmt.Println("emtpy addr:", addr.ToBase58()) 52 | } 53 | -------------------------------------------------------------------------------- /common/test/fixed64_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | package test 19 | 20 | import ( 21 | "github.com/polynetwork/poly/common" 22 | "github.com/stretchr/testify/assert" 23 | "testing" 24 | ) 25 | 26 | func TestFixed64_Serialize(t *testing.T) { 27 | val := common.Fixed64(10) 28 | buf := common.NewZeroCopySink(nil) 29 | val.Serialization(buf) 30 | val2 := common.Fixed64(0) 31 | val2.Deserialization(common.NewZeroCopySource(buf.Bytes())) 32 | 33 | assert.Equal(t, val, val2) 34 | } 35 | 36 | func TestFixed64_Deserialize(t *testing.T) { 37 | val := common.Fixed64(0) 38 | err := val.Deserialization(common.NewZeroCopySource([]byte{1, 2, 3})) 39 | 40 | assert.NotNil(t, err) 41 | 42 | } 43 | -------------------------------------------------------------------------------- /common/test/limited_writer_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package test 20 | 21 | import ( 22 | "bytes" 23 | "github.com/polynetwork/poly/common" 24 | "github.com/stretchr/testify/assert" 25 | "testing" 26 | ) 27 | 28 | func TestLimitedWriter_Write(t *testing.T) { 29 | bf := bytes.NewBuffer(nil) 30 | writer := common.NewLimitedWriter(bf, 5) 31 | _, err := writer.Write([]byte{1, 2, 3}) 32 | assert.Nil(t, err) 33 | assert.Equal(t, bf.Bytes(), []byte{1, 2, 3}) 34 | _, err = writer.Write([]byte{4, 5}) 35 | assert.Nil(t, err) 36 | 37 | _, err = writer.Write([]byte{6}) 38 | assert.Equal(t, err, common.ErrWriteExceedLimitedCount) 39 | } 40 | -------------------------------------------------------------------------------- /common/test/sort_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | package test 19 | 20 | import ( 21 | "github.com/polynetwork/poly/common" 22 | "testing" 23 | 24 | "github.com/stretchr/testify/assert" 25 | ) 26 | 27 | func TestUint64Slice(t *testing.T) { 28 | data1 := []uint64{3, 2, 4, 1} 29 | data2 := []uint64{1, 2, 3, 4} 30 | 31 | common.SortUint64s(data1) 32 | 33 | assert.Equal(t, data1, data2) 34 | 35 | } 36 | -------------------------------------------------------------------------------- /common/test/uint256_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | package test 19 | 20 | import ( 21 | "bytes" 22 | "github.com/polynetwork/poly/common" 23 | "github.com/stretchr/testify/assert" 24 | "testing" 25 | ) 26 | 27 | func TestUint256_Serialize(t *testing.T) { 28 | var val common.Uint256 29 | val[1] = 245 30 | buf := bytes.NewBuffer(nil) 31 | err := val.Serialize(buf) 32 | assert.Nil(t, err) 33 | } 34 | 35 | func TestUint256_Deserialize(t *testing.T) { 36 | var val common.Uint256 37 | val[1] = 245 38 | buf := bytes.NewBuffer(nil) 39 | val.Serialize(buf) 40 | 41 | var val2 common.Uint256 42 | val2.Deserialize(buf) 43 | 44 | assert.Equal(t, val, val2) 45 | 46 | buf = bytes.NewBuffer([]byte{1, 2, 3}) 47 | err := val2.Deserialize(buf) 48 | 49 | assert.NotNil(t, err) 50 | } 51 | 52 | func TestUint256ParseFromBytes(t *testing.T) { 53 | buf := []byte{1, 2, 3} 54 | 55 | _, err := common.Uint256ParseFromBytes(buf) 56 | 57 | assert.NotNil(t, err) 58 | } 59 | -------------------------------------------------------------------------------- /common/test/zero_copy_source_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | package test 19 | 20 | import ( 21 | "bytes" 22 | "crypto/rand" 23 | "github.com/polynetwork/poly/common" 24 | "github.com/polynetwork/poly/common/serialization" 25 | "testing" 26 | ) 27 | 28 | func BenchmarkZeroCopySource(b *testing.B) { 29 | const N = 12000 30 | buf := make([]byte, N) 31 | rand.Read(buf) 32 | 33 | for i := 0; i < b.N; i++ { 34 | source := common.NewZeroCopySource(buf) 35 | for j := 0; j < N/100; j++ { 36 | source.NextUint16() 37 | source.NextByte() 38 | source.NextUint64() 39 | source.NextVarUint() 40 | source.NextBytes(20) 41 | } 42 | } 43 | 44 | } 45 | 46 | func BenchmarkDerserialize(b *testing.B) { 47 | const N = 12000 48 | buf := make([]byte, N) 49 | rand.Read(buf) 50 | 51 | for i := 0; i < b.N; i++ { 52 | reader := bytes.NewBuffer(buf) 53 | for j := 0; j < N/100; j++ { 54 | serialization.ReadUint16(reader) 55 | serialization.ReadByte(reader) 56 | serialization.ReadUint64(reader) 57 | serialization.ReadVarUint(reader, 0) 58 | serialization.ReadBytes(reader, 20) 59 | } 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /consensus/actor/message.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package actor 20 | 21 | import "github.com/polynetwork/poly/core/types" 22 | 23 | type StartConsensus struct{} 24 | type StopConsensus struct{} 25 | 26 | //internal Message 27 | type TimeOut struct{} 28 | type BlockCompleted struct { 29 | Block *types.Block 30 | } 31 | -------------------------------------------------------------------------------- /consensus/consensus.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package consensus 20 | 21 | import ( 22 | "github.com/ontio/ontology-eventbus/actor" 23 | "github.com/polynetwork/poly/account" 24 | "github.com/polynetwork/poly/common/log" 25 | "github.com/polynetwork/poly/consensus/solo" 26 | "github.com/polynetwork/poly/consensus/vbft" 27 | ) 28 | 29 | type ConsensusService interface { 30 | Start() error 31 | Halt() error 32 | GetPID() *actor.PID 33 | } 34 | 35 | const ( 36 | CONSENSUS_SOLO = "solo" 37 | CONSENSUS_VBFT = "vbft" 38 | ) 39 | 40 | func NewConsensusService(consensusType string, account *account.Account, txpool *actor.PID, ledger *actor.PID, p2p *actor.PID) (ConsensusService, error) { 41 | if consensusType == "" { 42 | consensusType = CONSENSUS_SOLO 43 | } 44 | var consensus ConsensusService 45 | var err error 46 | switch consensusType { 47 | case CONSENSUS_SOLO: 48 | consensus, err = solo.NewSoloService(account, txpool) 49 | case CONSENSUS_VBFT: 50 | consensus, err = vbft.NewVbftServer(account, txpool, p2p) 51 | } 52 | log.Infof("ConsensusType:%s", consensusType) 53 | return consensus, err 54 | } 55 | -------------------------------------------------------------------------------- /consensus/policy.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package consensus 20 | 21 | import ( 22 | "github.com/polynetwork/poly/common" 23 | ) 24 | 25 | type Policy struct { 26 | PolicyLevel PolicyLevel 27 | List []common.Address 28 | } 29 | 30 | func NewPolicy() *Policy { 31 | return &Policy{} 32 | } 33 | 34 | func (p *Policy) Refresh() { 35 | //TODO: Refresh 36 | } 37 | 38 | var DefaultPolicy *Policy 39 | 40 | func InitPolicy() { 41 | DefaultPolicy := NewPolicy() 42 | DefaultPolicy.Refresh() 43 | } 44 | -------------------------------------------------------------------------------- /consensus/policy_level.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package consensus 20 | 21 | type PolicyLevel byte 22 | 23 | const ( 24 | AllowAll PolicyLevel = 0x00 25 | DenyAll PolicyLevel = 0x01 26 | AllowList PolicyLevel = 0x02 27 | DenyList PolicyLevel = 0x03 28 | ) 29 | -------------------------------------------------------------------------------- /consensus/vbft/README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | # VBFT 4 | 5 | VBFT is a VRF(virtual random function) based BFT consensus algorithm, and use PoS for its governance. 6 | 7 | ## Features 8 | 9 | - Scalable 10 | - High performance 11 | - BFT 12 | - Well defined governance protocol 13 | 14 | VBFT introduction is available [here](https://github.com/polynetwork/documentation/blob/master/vbft-intro/vbft-intro.md). 15 | 16 | 17 | -------------------------------------------------------------------------------- /consensus/vbft/config/config_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package vconfig 20 | 21 | import ( 22 | "bytes" 23 | "fmt" 24 | "testing" 25 | ) 26 | 27 | func generTestData() []byte { 28 | nodeId := "12020298fe9f22e9df64f6bfcc1c2a14418846cffdbbf510d261bbc3fa6d47073df9a2" 29 | chainPeers := make([]*PeerConfig, 0) 30 | peerconfig := &PeerConfig{ 31 | Index: 12, 32 | ID: nodeId, 33 | } 34 | chainPeers = append(chainPeers, peerconfig) 35 | 36 | tests := &ChainConfig{ 37 | Version: 1, 38 | View: 12, 39 | N: 4, 40 | C: 3, 41 | BlockMsgDelay: 1000, 42 | HashMsgDelay: 1000, 43 | PeerHandshakeTimeout: 10000, 44 | Peers: chainPeers, 45 | PosTable: []uint32{2, 3, 1, 3, 1, 3, 2, 3, 2, 3, 2, 1, 3}, 46 | } 47 | cc := new(bytes.Buffer) 48 | tests.Serialize(cc) 49 | return cc.Bytes() 50 | } 51 | func TestSerialize(t *testing.T) { 52 | res := generTestData() 53 | fmt.Println("serialize:", res) 54 | } 55 | -------------------------------------------------------------------------------- /consensus/vbft/config/types.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package vconfig 20 | 21 | import ( 22 | "encoding/hex" 23 | "encoding/json" 24 | "fmt" 25 | 26 | "github.com/ontio/ontology-crypto/keypair" 27 | "github.com/polynetwork/poly/core/types" 28 | ) 29 | 30 | // PubkeyID returns a marshaled representation of the given public key. 31 | func PubkeyID(pub keypair.PublicKey) string { 32 | nodeid := hex.EncodeToString(keypair.SerializePublicKey(pub)) 33 | return nodeid 34 | } 35 | 36 | func Pubkey(nodeid string) (keypair.PublicKey, error) { 37 | pubKey, err := hex.DecodeString(nodeid) 38 | if err != nil { 39 | return nil, err 40 | } 41 | pk, err := keypair.DeserializePublicKey(pubKey) 42 | if err != nil { 43 | return nil, fmt.Errorf("deserialize failed: %s", err) 44 | } 45 | return pk, err 46 | } 47 | 48 | func VbftBlock(header *types.Header) (*VbftBlockInfo, error) { 49 | blkInfo := &VbftBlockInfo{} 50 | if err := json.Unmarshal(header.ConsensusPayload, blkInfo); err != nil { 51 | return nil, fmt.Errorf("unmarshal blockInfo: %s", err) 52 | } 53 | return blkInfo, nil 54 | } 55 | -------------------------------------------------------------------------------- /consensus/vbft/config/types_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package vconfig 20 | 21 | import ( 22 | "encoding/hex" 23 | "testing" 24 | 25 | "github.com/ontio/ontology-crypto/keypair" 26 | ) 27 | 28 | func TestPubkeyID(t *testing.T) { 29 | bookkeeper := "120202c924ed1a67fd1719020ce599d723d09d48362376836e04b0be72dfe825e24d81" 30 | pubKey, err := hex.DecodeString(bookkeeper) 31 | if err != nil { 32 | t.Errorf("DecodeString failed: %v", err) 33 | } 34 | k, err := keypair.DeserializePublicKey(pubKey) 35 | if err != nil { 36 | t.Errorf("DeserializePublicKey failed: %v", err) 37 | } 38 | nodeID := PubkeyID(k) 39 | t.Logf("res: %v\n", nodeID) 40 | } 41 | 42 | func TestPubkey(t *testing.T) { 43 | bookkeeper := "1202027df359dff69eea8dd7d807b669dd9635292b1aae97d03ed32cb36ff30fb7e4d9" 44 | pubKey, err := hex.DecodeString(bookkeeper) 45 | if err != nil { 46 | t.Errorf("DecodeString failed: %v", err) 47 | } 48 | k, err := keypair.DeserializePublicKey(pubKey) 49 | if err != nil { 50 | t.Errorf("DeserializePublicKey failed: %v", err) 51 | } 52 | nodeID := PubkeyID(k) 53 | publickey, err := Pubkey(nodeID) 54 | if err != nil { 55 | t.Errorf("Pubkey failed: %v", err) 56 | } 57 | t.Logf("res: %v", publickey) 58 | } 59 | -------------------------------------------------------------------------------- /consensus/vbft/event_timer_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package vbft 20 | 21 | import "testing" 22 | 23 | func constructEventTimer() *EventTimer { 24 | server := constructServer() 25 | return NewEventTimer(server) 26 | } 27 | 28 | func TestStartTimer(t *testing.T) { 29 | eventtimer := constructEventTimer() 30 | err := eventtimer.StartTimer(1, 10) 31 | t.Logf("TestStartTimer: %v", err) 32 | } 33 | 34 | func TestCancelTimer(t *testing.T) { 35 | eventtimer := constructEventTimer() 36 | err := eventtimer.StartTimer(1, 10) 37 | t.Logf("TestStartTimer: %v", err) 38 | err = eventtimer.CancelTimer(1) 39 | t.Logf("TestCancelTimer: %v", err) 40 | } 41 | 42 | func TestStartEventTimer(t *testing.T) { 43 | eventtimer := constructEventTimer() 44 | err := eventtimer.startEventTimer(EventProposeBlockTimeout, 1) 45 | t.Logf("TestStartEventTimer: %v", err) 46 | } 47 | 48 | func TestCancelEventTimer(t *testing.T) { 49 | eventtimer := constructEventTimer() 50 | err := eventtimer.startEventTimer(EventProposeBlockTimeout, 1) 51 | t.Logf("startEventTimer: %v", err) 52 | err = eventtimer.cancelEventTimer(EventProposeBlockTimeout, 1) 53 | t.Logf("cancelEventTimer: %v", err) 54 | } 55 | -------------------------------------------------------------------------------- /consensus/vbft/msg_builder_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package vbft 20 | 21 | import ( 22 | "fmt" 23 | "testing" 24 | 25 | "github.com/polynetwork/poly/account" 26 | ) 27 | 28 | func constructMsg() *blockProposalMsg { 29 | acc := account.NewAccount("SHA256withECDSA") 30 | if acc == nil { 31 | fmt.Println("GetDefaultAccount error: acc is nil") 32 | return nil 33 | } 34 | msg := constructProposalMsgTest(acc) 35 | return msg 36 | } 37 | func TestSerializeVbftMsg(t *testing.T) { 38 | msg := constructMsg() 39 | _, err := SerializeVbftMsg(msg) 40 | if err != nil { 41 | t.Errorf("TestSerializeVbftMsg failed :%v", err) 42 | return 43 | } 44 | t.Logf("TestSerializeVbftMsg succ") 45 | } 46 | 47 | func TestDeserializeVbftMsg(t *testing.T) { 48 | msg := constructMsg() 49 | data, err := SerializeVbftMsg(msg) 50 | if err != nil { 51 | t.Errorf("TestSerializeVbftMsg failed :%v", err) 52 | return 53 | } 54 | _, err = DeserializeVbftMsg(data) 55 | if err != nil { 56 | t.Errorf("DeserializeVbftMsg failed :%v", err) 57 | return 58 | } 59 | t.Logf("TestDeserializeVbftMsg succ") 60 | } 61 | -------------------------------------------------------------------------------- /core/genesis/genesis_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | package genesis 19 | 20 | import ( 21 | "github.com/ontio/ontology-crypto/keypair" 22 | "github.com/polynetwork/poly/common" 23 | "github.com/polynetwork/poly/common/config" 24 | "github.com/polynetwork/poly/common/log" 25 | "github.com/stretchr/testify/assert" 26 | "os" 27 | "testing" 28 | ) 29 | 30 | func TestMain(m *testing.M) { 31 | log.InitLog(0, log.Stdout) 32 | m.Run() 33 | os.RemoveAll("./ActorLog") 34 | } 35 | 36 | func TestGenesisBlockInit(t *testing.T) { 37 | _, pub, _ := keypair.GenerateKeyPair(keypair.PK_ECDSA, keypair.P256) 38 | conf := &config.GenesisConfig{} 39 | block, err := BuildGenesisBlock([]keypair.PublicKey{pub}, conf) 40 | assert.Nil(t, err) 41 | assert.NotNil(t, block) 42 | assert.NotEqual(t, block.Header.TransactionsRoot, common.UINT256_EMPTY) 43 | } 44 | -------------------------------------------------------------------------------- /core/payload/invoke_code.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package payload 20 | 21 | import ( 22 | "fmt" 23 | "github.com/polynetwork/poly/common" 24 | ) 25 | 26 | // InvokeCode is an implementation of transaction payload for invoke smartcontract 27 | type InvokeCode struct { 28 | Code []byte 29 | } 30 | 31 | //note: InvokeCode.Code has data reference of param source 32 | func (self *InvokeCode) Deserialization(source *common.ZeroCopySource) error { 33 | code, eof := source.NextVarBytes() 34 | if eof { 35 | return fmt.Errorf("[InvokeCode] deserialize code error") 36 | } 37 | 38 | self.Code = code 39 | return nil 40 | } 41 | 42 | func (self *InvokeCode) Serialization(sink *common.ZeroCopySink) { 43 | sink.WriteVarBytes(self.Code) 44 | } 45 | -------------------------------------------------------------------------------- /core/payload/invoke_code_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | package payload 19 | 20 | import ( 21 | "github.com/polynetwork/poly/common" 22 | "testing" 23 | 24 | "github.com/stretchr/testify/assert" 25 | ) 26 | 27 | func TestInvokeCode_Serialize(t *testing.T) { 28 | code := InvokeCode{ 29 | Code: []byte{1, 2, 3}, 30 | } 31 | sink := common.NewZeroCopySink(nil) 32 | code.Serialization(sink) 33 | var code2 InvokeCode 34 | err := code2.Deserialization(common.NewZeroCopySource(sink.Bytes())) 35 | assert.NoError(t, err) 36 | assert.Equal(t, code, code2) 37 | } 38 | -------------------------------------------------------------------------------- /core/signature/signature_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | package signature 19 | 20 | import ( 21 | "testing" 22 | 23 | "github.com/ontio/ontology-crypto/keypair" 24 | "github.com/polynetwork/poly/account" 25 | "github.com/stretchr/testify/assert" 26 | ) 27 | 28 | func TestSign(t *testing.T) { 29 | acc := account.NewAccount("") 30 | data := []byte{1, 2, 3} 31 | sig, err := Sign(acc, data) 32 | assert.Nil(t, err) 33 | 34 | err = Verify(acc.PublicKey, data, sig) 35 | assert.Nil(t, err) 36 | } 37 | 38 | func TestVerifyMultiSignature(t *testing.T) { 39 | data := []byte{1, 2, 3} 40 | accs := make([]*account.Account, 0) 41 | pubkeys := make([]keypair.PublicKey, 0) 42 | N := 4 43 | for i := 0; i < N; i++ { 44 | accs = append(accs, account.NewAccount("")) 45 | } 46 | sigs := make([][]byte, 0) 47 | 48 | for _, acc := range accs { 49 | sig, _ := Sign(acc, data) 50 | sigs = append(sigs, sig) 51 | pubkeys = append(pubkeys, acc.PublicKey) 52 | } 53 | 54 | err := VerifyMultiSignature(data, pubkeys, N, sigs) 55 | assert.Nil(t, err) 56 | 57 | pubkeys[0], pubkeys[1] = pubkeys[1], pubkeys[0] 58 | err = VerifyMultiSignature(data, pubkeys, N, sigs) 59 | assert.Nil(t, err) 60 | 61 | } 62 | -------------------------------------------------------------------------------- /core/signature/signer.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package signature 20 | 21 | import ( 22 | "github.com/ontio/ontology-crypto/keypair" 23 | "github.com/ontio/ontology-crypto/signature" 24 | ) 25 | 26 | // Signer is the abstract interface of user's information(Keys) for signing data. 27 | type Signer interface { 28 | //get signer's private key 29 | PrivKey() keypair.PrivateKey 30 | 31 | //get signer's public key 32 | PubKey() keypair.PublicKey 33 | 34 | Scheme() signature.SignatureScheme 35 | } 36 | -------------------------------------------------------------------------------- /core/states/bookkeeper_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | package states 19 | 20 | import ( 21 | "testing" 22 | 23 | "bytes" 24 | 25 | "github.com/ontio/ontology-crypto/keypair" 26 | "github.com/stretchr/testify/assert" 27 | ) 28 | 29 | func TestBookkeeper_Deserialize_Serialize(t *testing.T) { 30 | _, pubKey1, _ := keypair.GenerateKeyPair(keypair.PK_ECDSA, keypair.P256) 31 | _, pubKey2, _ := keypair.GenerateKeyPair(keypair.PK_ECDSA, keypair.P256) 32 | _, pubKey3, _ := keypair.GenerateKeyPair(keypair.PK_ECDSA, keypair.P256) 33 | _, pubKey4, _ := keypair.GenerateKeyPair(keypair.PK_ECDSA, keypair.P256) 34 | 35 | bk := BookkeeperState{ 36 | StateBase: StateBase{(byte)(1)}, 37 | CurrBookkeeper: []keypair.PublicKey{pubKey1, pubKey2}, 38 | NextBookkeeper: []keypair.PublicKey{pubKey3, pubKey4}, 39 | } 40 | 41 | buf := bytes.NewBuffer(nil) 42 | bk.Serialize(buf) 43 | bs := buf.Bytes() 44 | 45 | var bk2 BookkeeperState 46 | bk2.Deserialize(buf) 47 | assert.Equal(t, bk, bk2) 48 | 49 | buf = bytes.NewBuffer(bs[:len(bs)-1]) 50 | err := bk2.Deserialize(buf) 51 | assert.NotNil(t, err) 52 | } 53 | -------------------------------------------------------------------------------- /core/states/interfaces.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package states 20 | 21 | import ( 22 | "io" 23 | ) 24 | 25 | type StateValue interface { 26 | Serialize(w io.Writer) error 27 | Deserialize(r io.Reader) error 28 | } 29 | -------------------------------------------------------------------------------- /core/states/state_base.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package states 20 | 21 | import ( 22 | "fmt" 23 | "io" 24 | 25 | "github.com/polynetwork/poly/common/serialization" 26 | ) 27 | 28 | type StateBase struct { 29 | StateVersion byte 30 | } 31 | 32 | func (this *StateBase) Serialize(w io.Writer) error { 33 | serialization.WriteByte(w, this.StateVersion) 34 | return nil 35 | } 36 | 37 | func (this *StateBase) Deserialize(r io.Reader) error { 38 | stateVersion, err := serialization.ReadByte(r) 39 | if err != nil { 40 | return fmt.Errorf("[StateBase], StateBase Deserialize failed,%s", err) 41 | } 42 | this.StateVersion = stateVersion 43 | return nil 44 | } 45 | -------------------------------------------------------------------------------- /core/states/state_base_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | package states 19 | 20 | import ( 21 | "bytes" 22 | "testing" 23 | ) 24 | 25 | func TestStateBase_Serialize_Deserialize(t *testing.T) { 26 | 27 | st := &StateBase{byte(1)} 28 | 29 | bf := new(bytes.Buffer) 30 | if err := st.Serialize(bf); err != nil { 31 | t.Fatalf("StateBase serialize error: %v", err) 32 | } 33 | 34 | var st2 = new(StateBase) 35 | if err := st2.Deserialize(bf); err != nil { 36 | t.Fatalf("StateBase deserialize error: %v", err) 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /core/states/storage_item_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | package states 19 | 20 | import ( 21 | "bytes" 22 | "testing" 23 | ) 24 | 25 | func TestStorageItem_Serialize_Deserialize(t *testing.T) { 26 | 27 | item := &StorageItem{ 28 | StateBase: StateBase{StateVersion: 1}, 29 | Value: []byte{1}, 30 | } 31 | 32 | bf := new(bytes.Buffer) 33 | if err := item.Serialize(bf); err != nil { 34 | t.Fatalf("StorageItem serialize error: %v", err) 35 | } 36 | 37 | var storage = new(StorageItem) 38 | if err := storage.Deserialize(bf); err != nil { 39 | t.Fatalf("StorageItem deserialize error: %v", err) 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /core/states/storage_key.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package states 20 | 21 | import ( 22 | "bytes" 23 | "io" 24 | 25 | "github.com/polynetwork/poly/common" 26 | "github.com/polynetwork/poly/common/serialization" 27 | ) 28 | 29 | type StorageKey struct { 30 | ContractAddress common.Address 31 | Key []byte 32 | } 33 | 34 | func (this *StorageKey) Serialize(w io.Writer) (int, error) { 35 | if err := this.ContractAddress.Serialize(w); err != nil { 36 | return 0, err 37 | } 38 | if err := serialization.WriteVarBytes(w, this.Key); err != nil { 39 | return 0, err 40 | } 41 | return 0, nil 42 | } 43 | 44 | func (this *StorageKey) Deserialize(r io.Reader) error { 45 | if err := this.ContractAddress.Deserialize(r); err != nil { 46 | return err 47 | } 48 | key, err := serialization.ReadVarBytes(r) 49 | if err != nil { 50 | return err 51 | } 52 | this.Key = key 53 | return nil 54 | } 55 | 56 | func (this *StorageKey) ToArray() []byte { 57 | b := new(bytes.Buffer) 58 | this.Serialize(b) 59 | return b.Bytes() 60 | } 61 | -------------------------------------------------------------------------------- /core/states/storage_key_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | package states 19 | 20 | import ( 21 | "testing" 22 | 23 | "bytes" 24 | "crypto/rand" 25 | 26 | "github.com/polynetwork/poly/common" 27 | "github.com/stretchr/testify/assert" 28 | ) 29 | 30 | func TestStorageKey_Deserialize_Serialize(t *testing.T) { 31 | var addr common.Address 32 | rand.Read(addr[:]) 33 | 34 | storage := StorageKey{ 35 | ContractAddress: addr, 36 | Key: []byte{1, 2, 3}, 37 | } 38 | 39 | buf := bytes.NewBuffer(nil) 40 | storage.Serialize(buf) 41 | bs := buf.Bytes() 42 | 43 | var storage2 StorageKey 44 | storage2.Deserialize(buf) 45 | assert.Equal(t, storage, storage2) 46 | 47 | buf = bytes.NewBuffer(bs[:len(bs)-1]) 48 | err := storage2.Deserialize(buf) 49 | assert.NotNil(t, err) 50 | } 51 | -------------------------------------------------------------------------------- /core/states/validator_state.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package states 20 | 21 | import ( 22 | "fmt" 23 | "io" 24 | 25 | "github.com/ontio/ontology-crypto/keypair" 26 | "github.com/polynetwork/poly/common/serialization" 27 | ) 28 | 29 | type ValidatorState struct { 30 | StateBase 31 | PublicKey keypair.PublicKey 32 | } 33 | 34 | func (this *ValidatorState) Serialize(w io.Writer) error { 35 | this.StateBase.Serialize(w) 36 | buf := keypair.SerializePublicKey(this.PublicKey) 37 | if err := serialization.WriteVarBytes(w, buf); err != nil { 38 | return err 39 | } 40 | return nil 41 | } 42 | 43 | func (this *ValidatorState) Deserialize(r io.Reader) error { 44 | err := this.StateBase.Deserialize(r) 45 | if err != nil { 46 | return fmt.Errorf("[ValidatorState], StateBase Deserialize failed, error:%s", err) 47 | } 48 | buf, err := serialization.ReadVarBytes(r) 49 | if err != nil { 50 | return fmt.Errorf("[ValidatorState], PublicKey Deserialize failed, error:%s", err) 51 | } 52 | pk, err := keypair.DeserializePublicKey(buf) 53 | if err != nil { 54 | return fmt.Errorf("[ValidatorState], PublicKey Deserialize failed, error:%s", err) 55 | } 56 | this.PublicKey = pk 57 | return nil 58 | } 59 | -------------------------------------------------------------------------------- /core/states/validator_state_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | package states 19 | 20 | import ( 21 | "testing" 22 | 23 | "bytes" 24 | 25 | "github.com/ontio/ontology-crypto/keypair" 26 | "github.com/stretchr/testify/assert" 27 | ) 28 | 29 | func TestValidatorState_Deserialize_Serialize(t *testing.T) { 30 | _, pubKey, _ := keypair.GenerateKeyPair(keypair.PK_ECDSA, keypair.P256) 31 | 32 | vs := ValidatorState{ 33 | StateBase: StateBase{(byte)(1)}, 34 | PublicKey: pubKey, 35 | } 36 | 37 | buf := bytes.NewBuffer(nil) 38 | vs.Serialize(buf) 39 | bs := buf.Bytes() 40 | 41 | var vs2 ValidatorState 42 | vs2.Deserialize(buf) 43 | assert.Equal(t, vs, vs2) 44 | 45 | buf = bytes.NewBuffer(bs[:len(bs)-1]) 46 | err := vs2.Deserialize(buf) 47 | assert.NotNil(t, err) 48 | } 49 | -------------------------------------------------------------------------------- /core/states/vote_state_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | package states 19 | 20 | import ( 21 | "testing" 22 | 23 | "bytes" 24 | 25 | "github.com/ontio/ontology-crypto/keypair" 26 | "github.com/stretchr/testify/assert" 27 | ) 28 | 29 | func TestVoteState_Deserialize_Serialize(t *testing.T) { 30 | _, pubKey1, _ := keypair.GenerateKeyPair(keypair.PK_ECDSA, keypair.P256) 31 | _, pubKey2, _ := keypair.GenerateKeyPair(keypair.PK_ECDSA, keypair.P256) 32 | 33 | vs := VoteState{ 34 | StateBase: StateBase{(byte)(1)}, 35 | PublicKeys: []keypair.PublicKey{pubKey1, pubKey2}, 36 | Count: 10, 37 | } 38 | 39 | buf := bytes.NewBuffer(nil) 40 | vs.Serialize(buf) 41 | bs := buf.Bytes() 42 | 43 | var vs2 VoteState 44 | vs2.Deserialize(buf) 45 | assert.Equal(t, vs, vs2) 46 | 47 | buf = bytes.NewBuffer(bs[:len(bs)-1]) 48 | err := vs2.Deserialize(buf) 49 | assert.NotNil(t, err) 50 | } 51 | -------------------------------------------------------------------------------- /core/store/ledgerstore/state_cache.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package ledgerstore 20 | 21 | import ( 22 | "github.com/hashicorp/golang-lru" 23 | "github.com/polynetwork/poly/core/states" 24 | ) 25 | 26 | const ( 27 | STATE_CACHE_SIZE = 100000 28 | ) 29 | 30 | type StateCache struct { 31 | stateCache *lru.ARCCache 32 | } 33 | 34 | func NewStateCache() (*StateCache, error) { 35 | stateCache, err := lru.NewARC(STATE_CACHE_SIZE) 36 | if err != nil { 37 | return nil, err 38 | } 39 | return &StateCache{ 40 | stateCache: stateCache, 41 | }, nil 42 | } 43 | 44 | func (this *StateCache) GetState(key []byte) states.StateValue { 45 | state, ok := this.stateCache.Get(string(key)) 46 | if !ok { 47 | return nil 48 | } 49 | return state.(states.StateValue) 50 | } 51 | 52 | func (this *StateCache) AddState(key []byte, state states.StateValue) { 53 | this.stateCache.Add(string(key), state) 54 | } 55 | 56 | func (this *StateCache) DeleteState(key []byte) { 57 | this.stateCache.Remove(string(key)) 58 | } 59 | -------------------------------------------------------------------------------- /core/store/ledgerstore/tx_handler_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package ledgerstore 20 | 21 | import ( 22 | "fmt" 23 | "strconv" 24 | "sync" 25 | "testing" 26 | ) 27 | 28 | func TestSyncMapRange(t *testing.T) { 29 | m := sync.Map{} 30 | 31 | for i := 0; i < 10; i++ { 32 | m.Store("k"+strconv.Itoa(i), "v"+strconv.Itoa(i)) 33 | } 34 | cnt := 0 35 | 36 | m.Range(func(key, value interface{}) bool { 37 | fmt.Printf("key :%s, val :%s\n", key, value) 38 | 39 | if key == "k5" { 40 | return false 41 | } 42 | cnt += 1 43 | return true 44 | }) 45 | 46 | fmt.Println(cnt) 47 | 48 | } 49 | 50 | //panic 51 | //func TestMapRange(t *testing.T) { 52 | // m := make(map[string]int) 53 | // 54 | // m["key"] = 10 55 | // 56 | // for i := 0; i < 1000; i++ { 57 | // go add(m, i) 58 | // } 59 | // 60 | //} 61 | 62 | func add(m map[string]int, va int) { 63 | m["key"] = va 64 | } 65 | 66 | func TestSyncMapRW(t *testing.T) { 67 | m := &sync.Map{} 68 | m.Store("key", 10) 69 | for i := 0; i < 100000; i++ { 70 | go addsync(m, i) 71 | } 72 | 73 | } 74 | 75 | func addsync(m *sync.Map, va int) { 76 | m.Store("key", va) 77 | } 78 | -------------------------------------------------------------------------------- /core/store/overlaydb/memdb_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package overlaydb 20 | 21 | import ( 22 | "github.com/stretchr/testify/assert" 23 | "testing" 24 | ) 25 | 26 | func TestIter(t *testing.T) { 27 | db := NewMemDB(0, 0) 28 | db.Put([]byte("aaa"), []byte("bbb")) 29 | iter := db.NewIterator(nil) 30 | assert.Equal(t, iter.First(), true) 31 | assert.Equal(t, iter.Last(), true) 32 | db.Delete([]byte("aaa")) 33 | assert.Equal(t, iter.First(), true) 34 | assert.Equal(t, len(iter.Value()), 0) 35 | assert.Equal(t, iter.Last(), true) 36 | assert.Equal(t, len(iter.Value()), 0) 37 | } 38 | -------------------------------------------------------------------------------- /core/types/address.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package types 20 | 21 | import ( 22 | "github.com/ontio/ontology-crypto/keypair" 23 | "github.com/polynetwork/poly/common" 24 | ) 25 | 26 | func AddressFromPubKey(pubkey keypair.PublicKey) common.Address { 27 | buf := keypair.SerializePublicKey(pubkey) 28 | return common.AddressFromVmCode(buf) 29 | } 30 | 31 | func AddressFromMultiPubKeys(pubkeys []keypair.PublicKey, m int) (common.Address, error) { 32 | sink := common.NewZeroCopySink(nil) 33 | if err := EncodeMultiPubKeyProgramInto(sink, pubkeys, uint16(m)); err != nil { 34 | return common.ADDRESS_EMPTY, nil 35 | } 36 | return common.AddressFromVmCode(sink.Bytes()), nil 37 | } 38 | 39 | func AddressFromBookkeepers(bookkeepers []keypair.PublicKey) (common.Address, error) { 40 | if len(bookkeepers) == 1 { 41 | return AddressFromPubKey(bookkeepers[0]), nil 42 | } 43 | return AddressFromMultiPubKeys(bookkeepers, len(bookkeepers)-(len(bookkeepers)-1)/3) 44 | } 45 | -------------------------------------------------------------------------------- /core/types/address_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | package types 19 | 20 | import ( 21 | "github.com/ontio/ontology-crypto/keypair" 22 | "github.com/stretchr/testify/assert" 23 | "testing" 24 | ) 25 | 26 | func TestAddressFromBookkeepers(t *testing.T) { 27 | _, pubKey1, _ := keypair.GenerateKeyPair(keypair.PK_ECDSA, keypair.P256) 28 | _, pubKey2, _ := keypair.GenerateKeyPair(keypair.PK_ECDSA, keypair.P256) 29 | _, pubKey3, _ := keypair.GenerateKeyPair(keypair.PK_ECDSA, keypair.P256) 30 | pubkeys := []keypair.PublicKey{pubKey1, pubKey2, pubKey3} 31 | 32 | addr, _ := AddressFromBookkeepers(pubkeys) 33 | addr2, _ := AddressFromMultiPubKeys(pubkeys, 3) 34 | assert.Equal(t, addr, addr2) 35 | 36 | pubkeys = []keypair.PublicKey{pubKey3, pubKey2, pubKey1} 37 | addr3, _ := AddressFromMultiPubKeys(pubkeys, 3) 38 | 39 | assert.Equal(t, addr2, addr3) 40 | } 41 | -------------------------------------------------------------------------------- /core/types/smartcontract.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package types 20 | 21 | import "github.com/polynetwork/poly/common" 22 | 23 | type SmartCodeEvent struct { 24 | TxHash common.Uint256 25 | Action string 26 | Result interface{} 27 | Error int64 28 | } 29 | -------------------------------------------------------------------------------- /core/types/version.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package types 20 | 21 | const CURR_TX_VERSION = 0 22 | const CURR_HEADER_VERSION = 0 23 | const MAX_ATTRIBUTES_LEN = 0 24 | -------------------------------------------------------------------------------- /core/vote/validator.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package vote 20 | 21 | import ( 22 | "github.com/ontio/ontology-crypto/keypair" 23 | "github.com/polynetwork/poly/core/genesis" 24 | "github.com/polynetwork/poly/core/types" 25 | ) 26 | 27 | func GetValidators(txs []*types.Transaction) ([]keypair.PublicKey, error) { 28 | // TODO implement vote 29 | return genesis.GenesisBookkeepers, nil 30 | } 31 | -------------------------------------------------------------------------------- /docker/Docker.build: -------------------------------------------------------------------------------- 1 | FROM debian:bullseye-20230703-slim 2 | RUN apt-get update && apt-get install -y git unzip wget curl build-essential 3 | RUN curl -L https://golang.org/dl/go1.20.linux-`dpkg --print-architecture`.tar.gz | tar -C /usr/local -xzf - 4 | 5 | WORKDIR /workspace 6 | 7 | ARG commit=master 8 | 9 | RUN ln -s /usr/local/go/bin/go /usr/bin/go 10 | RUN git clone https://github.com/polynetwork/poly.git && \ 11 | cd poly && git checkout ${commit} && make poly 12 | -------------------------------------------------------------------------------- /docker/Dockerfile: -------------------------------------------------------------------------------- 1 | # 1. Stage one: build ontology 2 | FROM golang:1.15 AS build 3 | WORKDIR /app 4 | RUN git clone https://github.com/polynetwork/poly.git && \ 5 | cd poly && \ 6 | make 7 | 8 | # 2. Stage two: copy compiled binary from prev builded container(referenced by name build) 9 | FROM ubuntu:18.04 10 | WORKDIR /app 11 | COPY --from=build /app/poly/poly poly 12 | 13 | 14 | EXPOSE 20334 20335 20336 20337 20338 20339 15 | #NOTE! we highly recommand that you put data dir to a mounted volume, e.g. --data-dir /data/Chain 16 | #write data to docker image is *not* a best practice 17 | CMD ["/bin/bash"] -------------------------------------------------------------------------------- /events/actor_event_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package events 20 | 21 | import ( 22 | "fmt" 23 | "testing" 24 | "time" 25 | 26 | "github.com/ontio/ontology-eventbus/actor" 27 | ) 28 | 29 | const testTopic = "test" 30 | 31 | type testMessage struct { 32 | Message string 33 | } 34 | 35 | func testSubReceive(c actor.Context) { 36 | switch msg := c.Message().(type) { 37 | case *testMessage: 38 | fmt.Printf("PID:%s receive message:%s\n", c.Self().Id, msg.Message) 39 | } 40 | } 41 | 42 | func TestActorEvent(t *testing.T) { 43 | Init() 44 | subPID1 := actor.Spawn(actor.FromFunc(testSubReceive)) 45 | subPID2 := actor.Spawn(actor.FromFunc(testSubReceive)) 46 | sub1 := NewActorSubscriber(subPID1) 47 | sub2 := NewActorSubscriber(subPID2) 48 | sub1.Subscribe(testTopic) 49 | sub2.Subscribe(testTopic) 50 | DefActorPublisher.Publish(testTopic, &testMessage{Message: "Hello"}) 51 | time.Sleep(time.Millisecond) 52 | DefActorPublisher.Publish(testTopic, &testMessage{Message: "Word"}) 53 | } 54 | -------------------------------------------------------------------------------- /events/event_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package events 20 | 21 | import ( 22 | "fmt" 23 | "testing" 24 | ) 25 | 26 | func TestNewEvent(t *testing.T) { 27 | event := NewEvent() 28 | 29 | var subscriber1 EventFunc = func(v interface{}) { 30 | fmt.Println("subscriber1 event func.") 31 | } 32 | 33 | var subscriber2 EventFunc = func(v interface{}) { 34 | fmt.Println("subscriber2 event func.") 35 | } 36 | 37 | fmt.Println("Subscribe...") 38 | sub1 := event.Subscribe(EventReplyTx, subscriber1) 39 | event.Subscribe(EventSaveBlock, subscriber2) 40 | 41 | fmt.Println("Notify...") 42 | event.Notify(EventReplyTx, nil) 43 | 44 | fmt.Println("Notify All...") 45 | event.NotifyAll() 46 | 47 | event.UnSubscribe(EventReplyTx, sub1) 48 | fmt.Println("Notify All after unsubscribe sub1...") 49 | event.NotifyAll() 50 | 51 | } 52 | -------------------------------------------------------------------------------- /events/event_type.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package events 20 | 21 | type EventType int16 22 | 23 | const ( 24 | EventSaveBlock EventType = 0 25 | EventReplyTx EventType = 1 26 | EventBlockPersistCompleted EventType = 2 27 | EventNewInventory EventType = 3 28 | EventNodeDisconnect EventType = 4 29 | EventSmartCode EventType = 5 30 | EventNodeConsensusDisconnect EventType = 6 31 | ) 32 | -------------------------------------------------------------------------------- /events/message/message.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package message 20 | 21 | import ( 22 | "github.com/polynetwork/poly/common" 23 | "github.com/polynetwork/poly/core/types" 24 | ) 25 | 26 | const ( 27 | TOPIC_SAVE_BLOCK_COMPLETE = "svblkcmp" 28 | TOPIC_NEW_INVENTORY = "newinv" 29 | TOPIC_NODE_DISCONNECT = "noddis" 30 | TOPIC_NODE_CONSENSUS_DISCONNECT = "nodcnsdis" 31 | TOPIC_SMART_CODE_EVENT = "scevt" 32 | ) 33 | 34 | type SaveBlockCompleteMsg struct { 35 | Block *types.Block 36 | } 37 | 38 | type NewInventoryMsg struct { 39 | Inventory *common.Inventory 40 | } 41 | 42 | type SmartCodeEventMsg struct { 43 | Event *types.SmartCodeEvent 44 | } 45 | 46 | type BlockConsensusComplete struct { 47 | Block *types.Block 48 | } 49 | -------------------------------------------------------------------------------- /events/subscriber.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package events 20 | 21 | type EventFunc func(v interface{}) 22 | 23 | type Subscriber chan interface{} 24 | -------------------------------------------------------------------------------- /glide.yaml: -------------------------------------------------------------------------------- 1 | package: github.com/ontio/ontology 2 | import: 3 | - package: github.com/ontio/ontology-eventbus 4 | version: v0.9.1 5 | - package: github.com/ontio/ontology-crypto 6 | version: v1.0.4 7 | - package: github.com/howeyc/gopass 8 | - package: github.com/gorilla/websocket 9 | version: v1.2.0 10 | - package: github.com/itchyny/base58-go 11 | - package: github.com/pborman/uuid 12 | version: v1.1 13 | - package: github.com/syndtr/goleveldb 14 | subpackages: 15 | - leveldb 16 | - leveldb/errors 17 | - leveldb/filter 18 | - leveldb/iterator 19 | - leveldb/opt 20 | - leveldb/util 21 | - package: github.com/urfave/cli 22 | version: v1.20.0 23 | - package: golang.org/x/text 24 | repo: https://github.com/golang/text.git 25 | - package: golang.org/x/crypto 26 | repo: https://github.com/golang/crypto.git 27 | subpackages: 28 | - ripemd160 29 | - package: github.com/hashicorp/golang-lru 30 | - package: github.com/ethereum/go-ethereum 31 | version: v1.8.23 32 | subpackages: 33 | - common/fdlimit 34 | - package: github.com/gosuri/uiprogress 35 | - package: golang.org/x/sys 36 | repo: https://github.com/golang/sys.git 37 | subpackages: 38 | - unix 39 | - package: golang.org/x/net 40 | repo: https://github.com/golang/net.git 41 | ignore: 42 | - golang.org/x/sys/unix 43 | -------------------------------------------------------------------------------- /http/base/actor/consensus.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package actor 20 | 21 | import ( 22 | "github.com/ontio/ontology-eventbus/actor" 23 | cactor "github.com/polynetwork/poly/consensus/actor" 24 | ) 25 | 26 | var consensusSrvPid *actor.PID 27 | 28 | func SetConsensusPid(actr *actor.PID) { 29 | consensusSrvPid = actr 30 | } 31 | 32 | //start consensus to consensus actor 33 | func ConsensusSrvStart() error { 34 | if consensusSrvPid != nil { 35 | consensusSrvPid.Tell(&cactor.StartConsensus{}) 36 | } 37 | return nil 38 | } 39 | 40 | //halt consensus to consensus actor 41 | func ConsensusSrvHalt() error { 42 | if consensusSrvPid != nil { 43 | consensusSrvPid.Tell(&cactor.StopConsensus{}) 44 | } 45 | return nil 46 | } 47 | -------------------------------------------------------------------------------- /http/base/rest/result.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | // Package rest privides functions to for restful server call 20 | package rest 21 | 22 | func ResponsePack(errCode int64) map[string]interface{} { 23 | resp := map[string]interface{}{ 24 | "Action": "", 25 | "Result": "", 26 | "Error": errCode, 27 | "Desc": "", 28 | "Version": "1.0.0", 29 | } 30 | return resp 31 | } 32 | -------------------------------------------------------------------------------- /http/base/rpc/result.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package rpc 20 | 21 | import ( 22 | Err "github.com/polynetwork/poly/http/base/error" 23 | ) 24 | 25 | func responseSuccess(result interface{}) map[string]interface{} { 26 | return responsePack(Err.SUCCESS, result) 27 | } 28 | func responsePack(errcode int64, result interface{}) map[string]interface{} { 29 | resp := map[string]interface{}{ 30 | "error": errcode, 31 | "desc": Err.ErrMap[errcode], 32 | "result": result, 33 | } 34 | return resp 35 | } 36 | -------------------------------------------------------------------------------- /http/localrpc/local_server.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | // Package localrpc privides a function to start local rpc server 20 | package localrpc 21 | 22 | import ( 23 | "net/http" 24 | "strconv" 25 | 26 | "fmt" 27 | cfg "github.com/polynetwork/poly/common/config" 28 | "github.com/polynetwork/poly/common/log" 29 | "github.com/polynetwork/poly/http/base/rpc" 30 | ) 31 | 32 | const ( 33 | LOCAL_HOST string = "127.0.0.1" 34 | LOCAL_DIR string = "/local" 35 | ) 36 | 37 | func StartLocalServer() error { 38 | log.Debug() 39 | http.HandleFunc(LOCAL_DIR, rpc.Handle) 40 | 41 | rpc.HandleFunc("getneighbor", rpc.GetNeighbor) 42 | rpc.HandleFunc("getnodestate", rpc.GetNodeState) 43 | rpc.HandleFunc("startconsensus", rpc.StartConsensus) 44 | rpc.HandleFunc("stopconsensus", rpc.StopConsensus) 45 | rpc.HandleFunc("setdebuginfo", rpc.SetDebugInfo) 46 | 47 | // TODO: only listen to local host 48 | err := http.ListenAndServe(":"+strconv.Itoa(int(cfg.DefConfig.Rpc.HttpLocalPort)), nil) 49 | if err != nil { 50 | return fmt.Errorf("ListenAndServe error:%s", err) 51 | } 52 | return nil 53 | } 54 | -------------------------------------------------------------------------------- /http/nodeinfo/ngbnodeinfo.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package nodeinfo 20 | 21 | import "strings" 22 | 23 | type NgbNodeInfo struct { 24 | NgbId string //neighbor node id 25 | NgbType string 26 | NgbAddr string 27 | HttpInfoAddr string 28 | HttpInfoPort uint16 29 | HttpInfoStart bool 30 | NgbVersion string 31 | } 32 | 33 | type NgbNodeInfoSlice []NgbNodeInfo 34 | 35 | func (n NgbNodeInfoSlice) Len() int { 36 | return len(n) 37 | } 38 | 39 | func (n NgbNodeInfoSlice) Swap(i, j int) { 40 | n[i], n[j] = n[j], n[i] 41 | } 42 | 43 | func (n NgbNodeInfoSlice) Less(i, j int) bool { 44 | if 0 <= strings.Compare(n[i].HttpInfoAddr, n[j].HttpInfoAddr) { 45 | return false 46 | } else { 47 | return true 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /http/restful/server.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | // Package restful privides a function to start restful server 20 | package restful 21 | 22 | import ( 23 | "github.com/polynetwork/poly/http/restful/restful" 24 | ) 25 | 26 | //start restful 27 | func StartServer() { 28 | rt := restful.InitRestServer() 29 | go rt.Start() 30 | } 31 | -------------------------------------------------------------------------------- /merkle/util.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package merkle 20 | 21 | // return the number of 1 bit 22 | func countBit(num uint32) uint { 23 | var count uint 24 | for num != 0 { 25 | num &= (num - 1) 26 | count += 1 27 | } 28 | return count 29 | } 30 | 31 | func isPower2(num uint32) bool { 32 | return countBit(num) == 1 33 | } 34 | 35 | // return the position of the heightest 1 bit 36 | // 1-based index 37 | func highBit(num uint32) uint { 38 | var hiBit uint 39 | for num != 0 { 40 | num >>= 1 41 | hiBit += 1 42 | } 43 | return hiBit 44 | } 45 | 46 | // return the position of the lowest 1 bit 47 | // 1-based index 48 | func lowBit(num uint32) uint { 49 | return highBit(num & -num) 50 | } 51 | -------------------------------------------------------------------------------- /native/event/event.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package event 20 | 21 | import ( 22 | "github.com/polynetwork/poly/common" 23 | "github.com/polynetwork/poly/core/types" 24 | "github.com/polynetwork/poly/events" 25 | "github.com/polynetwork/poly/events/message" 26 | ) 27 | 28 | const ( 29 | EVENT_NOTIFY = "Notify" 30 | ) 31 | 32 | // PushSmartCodeEvent push event content to socket.io 33 | func PushSmartCodeEvent(txHash common.Uint256, errCode int64, action string, result interface{}) { 34 | if events.DefActorPublisher == nil { 35 | return 36 | } 37 | smartCodeEvt := &types.SmartCodeEvent{ 38 | TxHash: txHash, 39 | Action: action, 40 | Result: result, 41 | Error: errCode, 42 | } 43 | events.DefActorPublisher.Publish(message.TOPIC_SMART_CODE_EVENT, &message.SmartCodeEventMsg{smartCodeEvt}) 44 | } 45 | -------------------------------------------------------------------------------- /native/event/notify_event_args.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package event 20 | 21 | import ( 22 | "github.com/polynetwork/poly/common" 23 | ) 24 | 25 | const ( 26 | CONTRACT_STATE_FAIL byte = 0 27 | CONTRACT_STATE_SUCCESS byte = 1 28 | ) 29 | 30 | // NotifyEventInfo describe smart contract event notify info struct 31 | type NotifyEventInfo struct { 32 | ContractAddress common.Address 33 | States interface{} 34 | } 35 | 36 | type ExecuteNotify struct { 37 | TxHash common.Uint256 38 | State byte 39 | GasConsumed uint64 40 | Notify []*NotifyEventInfo 41 | } 42 | -------------------------------------------------------------------------------- /native/service/cross_chain_manager/common/param_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with The poly network . If not, see . 16 | */ 17 | package common 18 | 19 | import ( 20 | "testing" 21 | 22 | ethcommon "github.com/ethereum/go-ethereum/common" 23 | "github.com/stretchr/testify/assert" 24 | ) 25 | 26 | func TestMakeTxParamWithSender(t *testing.T) { 27 | txParam := MakeTxParam{TxHash: []byte("hash"), 28 | CrossChainID: []byte("1"), 29 | FromContractAddress: []byte("from addr"), 30 | ToChainID: 1, 31 | ToContractAddress: []byte("to addr"), 32 | Method: "test", 33 | Args: []byte("args")} 34 | 35 | value := MakeTxParamWithSender{Sender: ethcommon.HexToAddress("abc"), MakeTxParam: txParam} 36 | data, err := value.Serialization() 37 | 38 | assert.Nil(t, err) 39 | 40 | var decoded MakeTxParamWithSender 41 | err = decoded.Deserialization(data) 42 | assert.Nil(t, err) 43 | 44 | assert.Equal(t, value, decoded) 45 | } 46 | -------------------------------------------------------------------------------- /native/service/cross_chain_manager/harmony/harmony_handler_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2022 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with The poly network . If not, see . 16 | */ 17 | 18 | package harmony 19 | 20 | -------------------------------------------------------------------------------- /native/service/cross_chain_manager/pixiechain/types.go: -------------------------------------------------------------------------------- 1 | package pixiechain 2 | 3 | import ( 4 | "math/big" 5 | 6 | ecommon "github.com/ethereum/go-ethereum/common" 7 | ) 8 | 9 | // PixieHandler ... 10 | type PixieHandler struct { 11 | } 12 | 13 | // Proof ... 14 | type Proof struct { 15 | Address string `json:"address"` 16 | Balance string `json:"balance"` 17 | CodeHash string `json:"codeHash"` 18 | Nonce string `json:"nonce"` 19 | StorageHash string `json:"storageHash"` 20 | AccountProof []string `json:"accountProof"` 21 | StorageProofs []StorageProof `json:"storageProof"` 22 | } 23 | 24 | // StorageProof ... 25 | type StorageProof struct { 26 | Key string `json:"key"` 27 | Value string `json:"value"` 28 | Proof []string `json:"proof"` 29 | } 30 | 31 | // ProofAccount ... 32 | type ProofAccount struct { 33 | Nounce *big.Int 34 | Balance *big.Int 35 | Storage ecommon.Hash 36 | Codehash ecommon.Hash 37 | } 38 | -------------------------------------------------------------------------------- /native/service/cross_chain_manager/ripple/ripple_handler_test.go: -------------------------------------------------------------------------------- 1 | package ripple 2 | 3 | import ( 4 | "encoding/json" 5 | "fmt" 6 | "github.com/polynetwork/ripple-sdk/types" 7 | "github.com/stretchr/testify/assert" 8 | "math/big" 9 | "testing" 10 | ) 11 | 12 | func TestJsonMarshall(t *testing.T) { 13 | txJson := "{\"TransactionType\":\"Payment\",\"Account\":\"rsHYGX2AoQ4tXqFywzEeeTDgXFTUfL1Fw9\",\"Sequence\":25336393,\"Fee\":\"150\",\"SigningPubKey\":\"\",\"Signers\":[{\"Account\":\"rLi6oSF38EdP7mzhdccyxhfd8vp8FWbsWF\",\"TxnSignature\":\"3044022048B1FD1B48B149B9E7A66344F758E7992C331D5EFE9A81F3F4D52477C5DBEBD50220453DC7B5A4E617CC59B15F887A7579F2B0BA8F14A65A6C416EB7C1D8A610204A\",\"SigningPubKey\":\"038B71C30DF7D4E9259732247AF169CCFACA1C0210784CEBD2884C0003B91CF33A\"}],\"Memos\":[{\"Memo\":{\"MemoType\":\"706F6C7968617368\",\"MemoData\":\"3E7C59E3954DEE9116A8148EC5CDCDB22485D55A62161B892F68ABDE4BF1A618\",\"MemoFormat\":\"\"}}],\"hash\":\"0000000000000000000000000000000000000000000000000000000000000000\",\"Destination\":\"rT4vRkeJsgaq7t6TVJJPsbrQp5oKMGRfN\",\"Amount\":\"1000000\"}" 14 | payment := new(types.MultisignPayment) 15 | err := json.Unmarshal([]byte(txJson), payment) 16 | assert.Nil(t, err) 17 | for _, s := range payment.Signers { 18 | fmt.Println(s.Signer.Account) 19 | } 20 | } 21 | 22 | func TestStringPrecise(t *testing.T) { 23 | fee_temp := new(big.Int).SetUint64(150) 24 | fee := ToStringByPrecise(fee_temp, 6) 25 | assert.Equal(t, fee, "0.00015") 26 | } -------------------------------------------------------------------------------- /native/service/governance/neo3_state_manager/param_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with The poly network . If not, see . 16 | */ 17 | 18 | package neo3_state_manager 19 | 20 | import ( 21 | "github.com/polynetwork/poly/common" 22 | "github.com/stretchr/testify/assert" 23 | "testing" 24 | ) 25 | 26 | func TestStateValidatorListParam_Serialization(t *testing.T) { 27 | params := new(StateValidatorListParam) 28 | params.StateValidators = []string{ 29 | "023e9b32ea89b94d066e649b124fd50e396ee91369e8e2a6ae1b11c170d022256d", 30 | "03009b7540e10f2562e5fd8fac9eaec25166a58b26e412348ff5a86927bfac22a2", 31 | "02ba2c70f5996f357a43198705859fae2cfea13e1172962800772b3d588a9d4abd", 32 | "03408dcd416396f64783ac587ea1e1593c57d9fea880c8a6a1920e92a259477806", 33 | } 34 | sink := common.NewZeroCopySink(nil) 35 | params.Serialization(sink) 36 | 37 | source := common.NewZeroCopySource(sink.Bytes()) 38 | var p StateValidatorListParam 39 | err := p.Deserialization(source) 40 | assert.Nil(t, err) 41 | assert.Equal(t, 4, len(p.StateValidators)) 42 | } 43 | -------------------------------------------------------------------------------- /native/service/governance/node_manager/states_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with The poly network . If not, see . 16 | */ 17 | 18 | package node_manager 19 | 20 | import ( 21 | "github.com/polynetwork/poly/common" 22 | "github.com/stretchr/testify/assert" 23 | "testing" 24 | ) 25 | 26 | func Test_Deserialize_GovernanceView(t *testing.T) { 27 | govView := &GovernanceView{ 28 | View: 0, 29 | Height: 10, 30 | TxHash: common.UINT256_EMPTY, 31 | } 32 | sink := common.NewZeroCopySink(nil) 33 | govView.Serialization(sink) 34 | 35 | source := common.NewZeroCopySource(sink.Bytes()) 36 | govView1 := new(GovernanceView) 37 | err := govView1.Deserialization(source) 38 | assert.Nil(t, err) 39 | assert.Equal(t, *govView, *govView1) 40 | } 41 | -------------------------------------------------------------------------------- /native/service/governance/relayer_manager/param_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with The poly network . If not, see . 16 | */ 17 | package relayer_manager 18 | 19 | import ( 20 | "github.com/polynetwork/poly/common" 21 | "github.com/stretchr/testify/assert" 22 | "testing" 23 | ) 24 | 25 | func TestRelayerListParam_Serialization(t *testing.T) { 26 | params := new(RelayerListParam) 27 | params.AddressList = []common.Address{{1, 2, 4, 6}, {1, 4, 5, 7}, {1, 3, 5, 7, 9}} 28 | sink := common.NewZeroCopySink(nil) 29 | params.Serialization(sink) 30 | 31 | source := common.NewZeroCopySource(sink.Bytes()) 32 | var p RelayerListParam 33 | err := p.Deserialization(source) 34 | assert.Nil(t, err) 35 | } 36 | -------------------------------------------------------------------------------- /native/service/governance/replenish/replenish.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with The poly network . If not, see . 16 | */ 17 | 18 | package replenish 19 | 20 | import ( 21 | "fmt" 22 | "github.com/polynetwork/poly/native/event" 23 | 24 | "github.com/polynetwork/poly/common" 25 | "github.com/polynetwork/poly/native" 26 | "github.com/polynetwork/poly/native/service/utils" 27 | ) 28 | 29 | const ( 30 | //function name 31 | REPLENISH_TX = "replenishTx" 32 | ) 33 | 34 | //Register methods 35 | func RegisterReplenishContract(native *native.NativeService) { 36 | native.Register(REPLENISH_TX, ReplenishTx) 37 | } 38 | 39 | func ReplenishTx(native *native.NativeService) ([]byte, error) { 40 | params := new(ReplenishTxParam) 41 | if err := params.Deserialization(common.NewZeroCopySource(native.GetInput())); err != nil { 42 | return utils.BYTE_FALSE, fmt.Errorf("ReplenishTx, contract params deserialize error: %v", err) 43 | } 44 | 45 | native.AddNotify( 46 | &event.NotifyEventInfo{ 47 | ContractAddress: utils.ReplenishContractAddress, 48 | States: []interface{}{"ReplenishTx", params.TxHashes, params.ChainId}, 49 | }) 50 | return utils.BYTE_TRUE, nil 51 | } 52 | -------------------------------------------------------------------------------- /native/service/governance/side_chain_manager/param_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with The poly network . If not, see . 16 | */ 17 | package side_chain_manager 18 | 19 | import ( 20 | "github.com/polynetwork/poly/common" 21 | "github.com/stretchr/testify/assert" 22 | "testing" 23 | ) 24 | 25 | func TestRegisterSideChain(t *testing.T) { 26 | param := RegisterSideChainParam{ 27 | Address: common.Address{1, 2, 3}, 28 | ChainId: 123, 29 | Name: "123456", 30 | BlocksToWait: 1234, 31 | } 32 | sink := common.NewZeroCopySink(nil) 33 | err := param.Serialization(sink) 34 | assert.NoError(t, err) 35 | 36 | source := common.NewZeroCopySource(sink.Bytes()) 37 | var p RegisterSideChainParam 38 | err = p.Deserialization(source) 39 | assert.NoError(t, err) 40 | 41 | assert.Equal(t, param, p) 42 | } 43 | 44 | func TestChainidParam(t *testing.T) { 45 | p := ChainidParam{ 46 | Chainid: 123, 47 | } 48 | 49 | sink := common.NewZeroCopySink(nil) 50 | p.Serialization(sink) 51 | 52 | var param ChainidParam 53 | err := param.Deserialization(common.NewZeroCopySource(sink.Bytes())) 54 | assert.NoError(t, err) 55 | 56 | assert.Equal(t, p, param) 57 | } 58 | -------------------------------------------------------------------------------- /native/service/governance/side_chain_manager/states_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with The poly network . If not, see . 16 | */ 17 | 18 | package side_chain_manager 19 | 20 | import ( 21 | "github.com/polynetwork/poly/common" 22 | "github.com/stretchr/testify/assert" 23 | "testing" 24 | ) 25 | 26 | func TestSideChain_Serialization(t *testing.T) { 27 | paramSerialize := new(SideChain) 28 | paramSerialize.Name = "own" 29 | paramSerialize.Router = 7 30 | paramSerialize.ChainId = 8 31 | paramSerialize.BlocksToWait = 10 32 | sink := common.NewZeroCopySink(nil) 33 | err := paramSerialize.Serialization(sink) 34 | assert.Nil(t, err) 35 | 36 | paramDeserialize := new(SideChain) 37 | err = paramDeserialize.Deserialization(common.NewZeroCopySource(sink.Bytes())) 38 | assert.Nil(t, err) 39 | assert.Equal(t, paramDeserialize, paramSerialize) 40 | } 41 | -------------------------------------------------------------------------------- /native/service/header_sync/btc/states_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with The poly network . If not, see . 16 | */ 17 | 18 | package btc 19 | 20 | import ( 21 | "github.com/btcsuite/btcd/chaincfg/chainhash" 22 | "github.com/btcsuite/btcd/wire" 23 | "github.com/polynetwork/poly/common" 24 | "github.com/stretchr/testify/assert" 25 | "math/big" 26 | "testing" 27 | "time" 28 | ) 29 | 30 | func TestStoredHeader(t *testing.T) { 31 | 32 | totalWork, _ := (new(big.Int)).SetString("123", 10) 33 | 34 | header := StoredHeader{ 35 | Header: wire.BlockHeader{ 36 | Version: 1, 37 | PrevBlock: chainhash.Hash{1, 2, 3}, 38 | MerkleRoot: chainhash.Hash{2, 2, 3}, 39 | Timestamp: time.Unix(time.Now().Unix(), 0), 40 | Nonce: 100, 41 | Bits: 200, 42 | }, 43 | Height: uint32(100), 44 | totalWork: totalWork, 45 | } 46 | sink := common.NewZeroCopySink(nil) 47 | header.Serialization(sink) 48 | 49 | h := new(StoredHeader) 50 | err := h.Deserialization(common.NewZeroCopySource(sink.Bytes())) 51 | assert.NoError(t, err) 52 | 53 | assert.Equal(t, header, *h) 54 | } 55 | -------------------------------------------------------------------------------- /native/service/header_sync/common/param_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with The poly network . If not, see . 16 | */ 17 | 18 | package common 19 | 20 | import ( 21 | "github.com/polynetwork/poly/common" 22 | "github.com/stretchr/testify/assert" 23 | "testing" 24 | ) 25 | 26 | func TestSyncGenesisHeaderParam(t *testing.T) { 27 | param := SyncGenesisHeaderParam{ 28 | ChainID: 123, 29 | GenesisHeader: []byte{1, 2, 3}, 30 | } 31 | 32 | sink := common.NewZeroCopySink(nil) 33 | param.Serialization(sink) 34 | 35 | var p SyncGenesisHeaderParam 36 | err := p.Deserialization(common.NewZeroCopySource(sink.Bytes())) 37 | assert.NoError(t, err) 38 | 39 | assert.Equal(t, p, param) 40 | } 41 | 42 | func TestSyncBlockHeaderParam(t *testing.T) { 43 | p := SyncBlockHeaderParam{ 44 | ChainID: 123, 45 | Address: common.ADDRESS_EMPTY, 46 | Headers: [][]byte{{1, 2, 3}}, 47 | } 48 | 49 | sink := common.NewZeroCopySink(nil) 50 | p.Serialization(sink) 51 | 52 | var param SyncBlockHeaderParam 53 | err := param.Deserialization(common.NewZeroCopySource(sink.Bytes())) 54 | 55 | assert.NoError(t, err) 56 | 57 | assert.Equal(t, p, param) 58 | } 59 | -------------------------------------------------------------------------------- /native/service/header_sync/cosmos/states.go: -------------------------------------------------------------------------------- 1 | package cosmos 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/polynetwork/poly/common" 7 | "github.com/tendermint/tendermint/libs/bytes" 8 | "github.com/tendermint/tendermint/types" 9 | ) 10 | 11 | type CosmosEpochSwitchInfo struct { 12 | // The height where validators set changed last time. Poly only accept 13 | // header and proof signed by new validators. That means the header 14 | // can not be lower than this height. 15 | Height int64 16 | 17 | // Hash of the block at `Height`. Poly don't save the whole header. 18 | // So we can identify the content of this block by `BlockHash`. 19 | BlockHash bytes.HexBytes 20 | 21 | // The hash of new validators set which used to verify validators set 22 | // committed with proof. 23 | NextValidatorsHash bytes.HexBytes 24 | 25 | // The cosmos chain-id of this chain basing Cosmos-sdk. 26 | ChainID string 27 | } 28 | 29 | func (info *CosmosEpochSwitchInfo) Serialization(sink *common.ZeroCopySink) { 30 | sink.WriteInt64(info.Height) 31 | sink.WriteVarBytes(info.BlockHash) 32 | sink.WriteVarBytes(info.NextValidatorsHash) 33 | sink.WriteString(info.ChainID) 34 | } 35 | 36 | func (info *CosmosEpochSwitchInfo) Deserialization(source *common.ZeroCopySource) error { 37 | var eof bool 38 | info.Height, eof = source.NextInt64() 39 | if eof { 40 | return fmt.Errorf("deserialize height of CosmosEpochSwitchInfo failed") 41 | } 42 | info.BlockHash, eof = source.NextVarBytes() 43 | if eof { 44 | return fmt.Errorf("deserialize BlockHash of CosmosEpochSwitchInfo failed") 45 | } 46 | info.NextValidatorsHash, eof = source.NextVarBytes() 47 | if eof { 48 | return fmt.Errorf("deserialize NextValidatorsHash of CosmosEpochSwitchInfo failed") 49 | } 50 | info.ChainID, eof = source.NextString() 51 | if eof { 52 | return fmt.Errorf("deserialize ChainID of CosmosEpochSwitchInfo failed") 53 | } 54 | return nil 55 | } 56 | 57 | type CosmosHeader struct { 58 | Header types.Header 59 | Commit *types.Commit 60 | Valsets []*types.Validator 61 | } 62 | -------------------------------------------------------------------------------- /native/service/header_sync/eth/rlp/iterator.go: -------------------------------------------------------------------------------- 1 | // Copyright 2019 The go-ethereum Authors 2 | // This file is part of the go-ethereum library. 3 | // 4 | // The go-ethereum library is free software: you can redistribute it and/or modify 5 | // it under the terms of the GNU Lesser General Public License as published by 6 | // the Free Software Foundation, either version 3 of the License, or 7 | // (at your option) any later version. 8 | // 9 | // The go-ethereum library is distributed in the hope that it will be useful, 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | // GNU Lesser General Public License for more details. 13 | // 14 | // You should have received a copy of the GNU Lesser General Public License 15 | // along with the go-ethereum library. If not, see . 16 | 17 | package rlp 18 | 19 | type listIterator struct { 20 | data []byte 21 | next []byte 22 | err error 23 | } 24 | 25 | // NewListIterator creates an iterator for the (list) represented by data 26 | // TODO: Consider removing this implementation, as it is no longer used. 27 | func NewListIterator(data RawValue) (*listIterator, error) { 28 | k, t, c, err := readKind(data) 29 | if err != nil { 30 | return nil, err 31 | } 32 | if k != List { 33 | return nil, ErrExpectedList 34 | } 35 | it := &listIterator{ 36 | data: data[t : t+c], 37 | } 38 | return it, nil 39 | 40 | } 41 | 42 | // Next forwards the iterator one step, returns true if it was not at end yet 43 | func (it *listIterator) Next() bool { 44 | if len(it.data) == 0 { 45 | return false 46 | } 47 | _, t, c, err := readKind(it.data) 48 | it.next = it.data[:t+c] 49 | it.data = it.data[t+c:] 50 | it.err = err 51 | return true 52 | } 53 | 54 | // Value returns the current value 55 | func (it *listIterator) Value() []byte { 56 | return it.next 57 | } 58 | 59 | func (it *listIterator) Err() error { 60 | return it.err 61 | } 62 | -------------------------------------------------------------------------------- /native/service/header_sync/eth/rlp/safe.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 The go-ethereum Authors 2 | // This file is part of the go-ethereum library. 3 | // 4 | // The go-ethereum library is free software: you can redistribute it and/or modify 5 | // it under the terms of the GNU Lesser General Public License as published by 6 | // the Free Software Foundation, either version 3 of the License, or 7 | // (at your option) any later version. 8 | // 9 | // The go-ethereum library is distributed in the hope that it will be useful, 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | // GNU Lesser General Public License for more details. 13 | // 14 | // You should have received a copy of the GNU Lesser General Public License 15 | // along with the go-ethereum library. If not, see . 16 | 17 | // +build nacl js !cgo 18 | 19 | package rlp 20 | 21 | import "reflect" 22 | 23 | // byteArrayBytes returns a slice of the byte array v. 24 | func byteArrayBytes(v reflect.Value) []byte { 25 | return v.Slice(0, v.Len()).Bytes() 26 | } 27 | -------------------------------------------------------------------------------- /native/service/header_sync/eth/rlp/unsafe.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 The go-ethereum Authors 2 | // This file is part of the go-ethereum library. 3 | // 4 | // The go-ethereum library is free software: you can redistribute it and/or modify 5 | // it under the terms of the GNU Lesser General Public License as published by 6 | // the Free Software Foundation, either version 3 of the License, or 7 | // (at your option) any later version. 8 | // 9 | // The go-ethereum library is distributed in the hope that it will be useful, 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | // GNU Lesser General Public License for more details. 13 | // 14 | // You should have received a copy of the GNU Lesser General Public License 15 | // along with the go-ethereum library. If not, see . 16 | 17 | // +build !nacl,!js,cgo 18 | 19 | package rlp 20 | 21 | import ( 22 | "reflect" 23 | "unsafe" 24 | ) 25 | 26 | // byteArrayBytes returns a slice of the byte array v. 27 | func byteArrayBytes(v reflect.Value) []byte { 28 | len := v.Len() 29 | var s []byte 30 | hdr := (*reflect.SliceHeader)(unsafe.Pointer(&s)) 31 | hdr.Data = v.UnsafeAddr() 32 | hdr.Cap = len 33 | hdr.Len = len 34 | return s 35 | } 36 | -------------------------------------------------------------------------------- /native/service/header_sync/okex/ethsecp256k1/codec.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with The poly network . If not, see . 16 | */ 17 | package ethsecp256k1 18 | 19 | import ( 20 | "github.com/cosmos/cosmos-sdk/codec" 21 | cryptoamino "github.com/tendermint/tendermint/crypto/encoding/amino" 22 | ) 23 | 24 | // CryptoCodec is the default amino codec used by ethermint 25 | var CryptoCodec = codec.New() 26 | 27 | func init() { 28 | RegisterCodec(CryptoCodec) 29 | } 30 | 31 | // RegisterCodec registers all the necessary types with amino for the given 32 | // codec. 33 | func RegisterCodec(cdc *codec.Codec) { 34 | cryptoamino.RegisterAmino(cdc) 35 | cdc.RegisterConcrete(PubKey{}, PubKeyName, nil) 36 | cdc.RegisterConcrete(PrivKey{}, PrivKeyName, nil) 37 | } 38 | -------------------------------------------------------------------------------- /native/service/header_sync/ont/states_test.go: -------------------------------------------------------------------------------- 1 | package ont 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/polynetwork/poly/common" 7 | "github.com/stretchr/testify/assert" 8 | ) 9 | 10 | func TestPeer_Serialization(t *testing.T) { 11 | paramSerialize := new(Peer) 12 | paramSerialize.Index = 1 13 | paramSerialize.PeerPubkey = "abcdefg" 14 | sink := common.NewZeroCopySink(nil) 15 | paramSerialize.Serialization(sink) 16 | 17 | paramDeserialize := new(Peer) 18 | err := paramDeserialize.Deserialization(common.NewZeroCopySource(sink.Bytes())) 19 | assert.Nil(t, err) 20 | assert.Equal(t, paramDeserialize, paramSerialize) 21 | } 22 | 23 | func TestKeyHeights_Serialization(t *testing.T) { 24 | paramSerialize := new(KeyHeights) 25 | paramSerialize.HeightList = []uint32{1, 3, 5} 26 | sink := common.NewZeroCopySink(nil) 27 | paramSerialize.Serialization(sink) 28 | 29 | paramDeserialize := new(KeyHeights) 30 | err := paramDeserialize.Deserialization(common.NewZeroCopySource(sink.Bytes())) 31 | assert.Nil(t, err) 32 | assert.Equal(t, paramDeserialize, paramSerialize) 33 | } 34 | 35 | func TestConsensusPeers_Serialization(t *testing.T) { 36 | paramSerialize := new(ConsensusPeers) 37 | paramSerialize.Height = 1 38 | paramSerialize.ChainID = 0 39 | peer1 := &Peer{Index: 1, PeerPubkey: "abcd"} 40 | peer2 := &Peer{Index: 2, PeerPubkey: "efgh"} 41 | paramSerialize.PeerMap = make(map[string]*Peer) 42 | paramSerialize.PeerMap[peer1.PeerPubkey] = peer1 43 | paramSerialize.PeerMap[peer2.PeerPubkey] = peer2 44 | sink := common.NewZeroCopySink(nil) 45 | paramSerialize.Serialization(sink) 46 | 47 | paramDeserialize := new(ConsensusPeers) 48 | err := paramDeserialize.Deserialization(common.NewZeroCopySource(sink.Bytes())) 49 | assert.Nil(t, err) 50 | assert.Equal(t, paramDeserialize, paramSerialize) 51 | } 52 | -------------------------------------------------------------------------------- /native/service/header_sync/pixiechain/common.go: -------------------------------------------------------------------------------- 1 | package pixiechain 2 | 3 | import ( 4 | "math/big" 5 | 6 | "github.com/ethereum/go-ethereum/core/types" 7 | "github.com/ethereum/go-ethereum/crypto" 8 | ) 9 | 10 | const ( 11 | extraVanity = 32 // Fixed number of extra-data prefix bytes reserved for signer vanity 12 | extraSeal = crypto.SignatureLength // Fixed number of extra-data suffix bytes reserved for signer seal 13 | GasLimitMax = uint64(0x7fffffffffffffff) // GasLimit maximum ( GasLimit <= 2^63-1) 14 | ) 15 | 16 | var ( 17 | uncleHash = types.CalcUncleHash(nil) // Always Keccak256(RLP([])) as uncles are meaningless outside of PoW. 18 | diffInTurn = big.NewInt(2) // Block difficulty for in-turn signatures 19 | diffNoTurn = big.NewInt(1) // Block difficulty for out-of-turn signatures 20 | ) 21 | -------------------------------------------------------------------------------- /native/service/header_sync/pixiechain/hardfork.go: -------------------------------------------------------------------------------- 1 | package pixiechain 2 | 3 | // PixieChain Will active a hard fork in the future. 4 | /* 5 | import ( 6 | "fmt" 7 | "math/big" 8 | 9 | "github.com/ethereum/go-ethereum/common" 10 | "github.com/polynetwork/poly/native/service/header_sync/eth" 11 | ) 12 | 13 | var ( 14 | isTest bool 15 | testNextHardforkHeight uint64 16 | ) 17 | 18 | // isNext returns true if it's the next hard fork of PixieChain 19 | func isNext(h *eth.Header) bool { 20 | if isTest { 21 | return h.Number.Uint64() >= testNextHardforkHeight 22 | } 23 | return h.BaseFee != nil || h.Number.Uint64() >= config.GetPixieChainNextHardforkHeight(config.DefConfig.P2PNode.NetworkId) 24 | } 25 | 26 | // VerifyEip1559Header verifies some header attributes which were changed in EIP-1559, 27 | // - gas limit check 28 | // - basefee check 29 | func VerifyEip1559Header(parent, header *eth.Header) error { 30 | // Verify that the gas limit remains within allowed bounds 31 | parentGasLimit := parent.GasLimit 32 | 33 | if err := VerifyGaslimit(parentGasLimit, header.GasLimit); err != nil { 34 | return err 35 | } 36 | // Verify the header is not malformed 37 | if header.BaseFee == nil { 38 | return fmt.Errorf("header is missing baseFee") 39 | } 40 | // Verify the baseFee is correct based on the parent header. 41 | expectedBaseFee := CalcBaseFee(parent) 42 | if header.BaseFee.Cmp(expectedBaseFee) != 0 { 43 | return fmt.Errorf("invalid baseFee: have %s, want %s, parentBaseFee %s, parentGasUsed %d", 44 | expectedBaseFee, header.BaseFee, parent.BaseFee, parent.GasUsed) 45 | } 46 | return nil 47 | } 48 | 49 | // CalcBaseFee calculates the basefee of the header. 50 | func CalcBaseFee(parent *eth.Header) *big.Int { 51 | return common.Big0 52 | } 53 | */ 54 | -------------------------------------------------------------------------------- /native/service/header_sync/pixiechain/types.go: -------------------------------------------------------------------------------- 1 | package pixiechain 2 | 3 | import ( 4 | "math/big" 5 | 6 | ecommon "github.com/ethereum/go-ethereum/common" 7 | "github.com/polynetwork/poly/native/service/header_sync/eth" 8 | ) 9 | 10 | // Handler ... 11 | type Handler struct { 12 | } 13 | 14 | // GenesisHeader ... 15 | type GenesisHeader struct { 16 | Header eth.Header 17 | PrevValidators []HeightAndValidators 18 | } 19 | 20 | // ExtraInfo ... 21 | type ExtraInfo struct { 22 | ChainID *big.Int // chainId of pixie chain. mainnet: 6626, testnet: 666 23 | Period uint64 24 | } 25 | 26 | // Context ... 27 | type Context struct { 28 | ExtraInfo ExtraInfo 29 | ChainID uint64 30 | } 31 | 32 | // HeaderWithChainID ... 33 | type HeaderWithChainID struct { 34 | Header *HeaderWithDifficultySum 35 | ChainID uint64 36 | } 37 | 38 | // HeaderWithDifficultySum ... 39 | type HeaderWithDifficultySum struct { 40 | Header *eth.Header `json:"header"` 41 | DifficultySum *big.Int `json:"difficultySum"` 42 | EpochParentHash *ecommon.Hash `json:"epochParentHash"` 43 | } 44 | -------------------------------------------------------------------------------- /native/service/header_sync/pixiechain/utils.go: -------------------------------------------------------------------------------- 1 | package pixiechain 2 | 3 | import ( 4 | "errors" 5 | "fmt" 6 | 7 | "github.com/ethereum/go-ethereum/params" 8 | ) 9 | 10 | // VerifyGaslimit verifies the header gas limit according increase/decrease 11 | // in relation to the parent gas limit. 12 | func VerifyGaslimit(parentGasLimit, headerGasLimit uint64) error { 13 | // Verify that the gas limit remains within allowed bounds 14 | diff := int64(parentGasLimit) - int64(headerGasLimit) 15 | if diff < 0 { 16 | diff *= -1 17 | } 18 | limit := parentGasLimit / params.GasLimitBoundDivisor 19 | if uint64(diff) >= limit { 20 | return fmt.Errorf("invalid gas limit: have %d, want %d +-= %d", headerGasLimit, parentGasLimit, limit-1) 21 | } 22 | if headerGasLimit < params.MinGasLimit { 23 | return errors.New("invalid gas limit below 5000") 24 | } 25 | return nil 26 | } 27 | -------------------------------------------------------------------------------- /native/service/header_sync/polygon/errors.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with The poly network . If not, see . 16 | */ 17 | package polygon 18 | 19 | import "fmt" 20 | 21 | // TotalVotingPowerExceededError is returned when the maximum allowed total voting power is exceeded 22 | type TotalVotingPowerExceededError struct { 23 | Sum int64 24 | Validators []*Validator 25 | } 26 | 27 | func (e *TotalVotingPowerExceededError) Error() string { 28 | return fmt.Sprintf( 29 | "Total voting power should be guarded to not exceed %v; got: %v; for validator set: %v", 30 | MaxTotalVotingPower, 31 | e.Sum, 32 | e.Validators, 33 | ) 34 | } 35 | -------------------------------------------------------------------------------- /native/service/header_sync/polygon/types/common/byteslice.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with The poly network . If not, see . 16 | */ 17 | package common 18 | 19 | // Fingerprint returns the first 6 bytes of a byte slice. 20 | // If the slice is less than 6 bytes, the fingerprint 21 | // contains trailing zeroes. 22 | func Fingerprint(slice []byte) []byte { 23 | fingerprint := make([]byte, 6) 24 | copy(fingerprint, slice) 25 | return fingerprint 26 | } 27 | -------------------------------------------------------------------------------- /native/service/header_sync/polygon/types/common/math.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with The poly network . If not, see . 16 | */ 17 | package common 18 | 19 | func MaxInt64(a, b int64) int64 { 20 | if a > b { 21 | return a 22 | } 23 | return b 24 | } 25 | 26 | func MaxInt(a, b int) int { 27 | if a > b { 28 | return a 29 | } 30 | return b 31 | } 32 | 33 | //----------------------------------------------------------------------------- 34 | 35 | func MinInt64(a, b int64) int64 { 36 | if a < b { 37 | return a 38 | } 39 | return b 40 | } 41 | 42 | func MinInt(a, b int) int { 43 | if a < b { 44 | return a 45 | } 46 | return b 47 | } 48 | -------------------------------------------------------------------------------- /native/service/header_sync/polygon/types/common/nil.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with The poly network . If not, see . 16 | */ 17 | package common 18 | 19 | import "reflect" 20 | 21 | // Go lacks a simple and safe way to see if something is a typed nil. 22 | // See: 23 | // - https://dave.cheney.net/2017/08/09/typed-nils-in-go-2 24 | // - https://groups.google.com/forum/#!topic/golang-nuts/wnH302gBa4I/discussion 25 | // - https://github.com/golang/go/issues/21538 26 | func IsTypedNil(o interface{}) bool { 27 | rv := reflect.ValueOf(o) 28 | switch rv.Kind() { 29 | case reflect.Chan, reflect.Func, reflect.Map, reflect.Ptr, reflect.Slice: 30 | return rv.IsNil() 31 | default: 32 | return false 33 | } 34 | } 35 | 36 | // Returns true if it has zero length. 37 | func IsEmpty(o interface{}) bool { 38 | rv := reflect.ValueOf(o) 39 | switch rv.Kind() { 40 | case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice, reflect.String: 41 | return rv.Len() == 0 42 | default: 43 | return false 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /native/service/header_sync/polygon/types/heimdall_block.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with The poly network . If not, see . 16 | */ 17 | package types 18 | 19 | import ( 20 | "bytes" 21 | 22 | "github.com/polynetwork/poly/native/service/header_sync/polygon/types/common" 23 | ) 24 | 25 | // BlockID defines the unique ID of a block as its Hash and its PartSetHeader 26 | type BlockID struct { 27 | Hash common.HexBytes `json:"hash"` 28 | PartsHeader PartSetHeader `json:"parts"` 29 | } 30 | 31 | // IsZero returns true if this is the BlockID of a nil block. 32 | func (blockID BlockID) IsZero() bool { 33 | return len(blockID.Hash) == 0 && 34 | blockID.PartsHeader.IsZero() 35 | } 36 | 37 | // Equals returns true if the BlockID matches the given BlockID 38 | func (blockID BlockID) Equals(other BlockID) bool { 39 | return bytes.Equal(blockID.Hash, other.Hash) && 40 | blockID.PartsHeader.Equals(other.PartsHeader) 41 | } 42 | 43 | // CommitSig is a vote included in a Commit. 44 | // For now, it is identical to a vote, 45 | // but in the future it will contain fewer fields 46 | // to eliminate the redundancy in commits. 47 | // See https://github.com/tendermint/tendermint/issues/1648. 48 | type CommitSig Vote 49 | -------------------------------------------------------------------------------- /native/service/header_sync/polygon/types/heimdall_codec.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with The poly network . If not, see . 16 | */ 17 | package types 18 | 19 | import ( 20 | "github.com/cosmos/cosmos-sdk/codec" 21 | "github.com/polynetwork/poly/native/service/header_sync/polygon/types/secp256k1" 22 | "github.com/tendermint/tendermint/crypto" 23 | ) 24 | 25 | // NewCDC ... 26 | func NewCDC() *codec.Codec { 27 | cdc := codec.New() 28 | 29 | cdc.RegisterInterface((*crypto.PubKey)(nil), nil) 30 | cdc.RegisterConcrete(secp256k1.PubKeySecp256k1{}, secp256k1.PubKeyAminoName, nil) 31 | 32 | return cdc 33 | } 34 | 35 | var cdc = NewCDC() 36 | -------------------------------------------------------------------------------- /native/service/header_sync/polygon/types/heimdall_encoding_helper.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with The poly network . If not, see . 16 | */ 17 | package types 18 | 19 | import "github.com/polynetwork/poly/native/service/header_sync/polygon/types/common" 20 | 21 | // cdcEncode returns nil if the input is nil, otherwise returns 22 | // cdc.MustMarshalBinaryBare(item) 23 | func cdcEncode(item interface{}) []byte { 24 | if item != nil && !common.IsTypedNil(item) && !common.IsEmpty(item) { 25 | return cdc.MustMarshalBinaryBare(item) 26 | } 27 | return nil 28 | } 29 | -------------------------------------------------------------------------------- /native/service/header_sync/polygon/types/heimdall_signed_msg_type.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with The poly network . If not, see . 16 | */ 17 | package types 18 | 19 | // SignedMsgType is a type of signed message in the consensus. 20 | type SignedMsgType byte 21 | 22 | const ( 23 | // Votes 24 | PrevoteType SignedMsgType = 0x01 25 | PrecommitType SignedMsgType = 0x02 26 | 27 | // Proposals 28 | ProposalType SignedMsgType = 0x20 29 | ) 30 | 31 | // IsVoteTypeValid returns true if t is a valid vote type. 32 | func IsVoteTypeValid(t SignedMsgType) bool { 33 | switch t { 34 | case PrevoteType, PrecommitType: 35 | return true 36 | default: 37 | return false 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /native/service/header_sync/polygon/types/heimdall_vote.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with The poly network . If not, see . 16 | */ 17 | package types 18 | 19 | import "time" 20 | 21 | // Vote represents a prevote, precommit, or commit vote from validators for 22 | // consensus. 23 | type Vote struct { 24 | Type SignedMsgType `json:"type"` 25 | Height int64 `json:"height"` 26 | Round int `json:"round"` 27 | BlockID BlockID `json:"block_id"` // zero if vote is nil. 28 | Timestamp time.Time `json:"timestamp"` 29 | ValidatorAddress Address `json:"validator_address"` 30 | ValidatorIndex int `json:"validator_index"` 31 | Signature []byte `json:"signature"` 32 | 33 | SideTxResults []SideTxResult `json:"side_tx_results"` // side-tx result [peppermint] 34 | } 35 | 36 | func (vote *Vote) SignBytes(chainID string) []byte { 37 | // [peppermint] converted from amino to rlp 38 | bz, err := cdc.MarshalBinaryLengthPrefixed(CanonicalizeVote(chainID, vote)) 39 | if err != nil { 40 | panic(err) 41 | } 42 | return bz 43 | } 44 | -------------------------------------------------------------------------------- /native/states/contract_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | package states 19 | 20 | import ( 21 | "testing" 22 | 23 | "github.com/polynetwork/poly/common" 24 | ) 25 | 26 | func TestContract_Serialize_Deserialize(t *testing.T) { 27 | addr := common.AddressFromVmCode([]byte{1}) 28 | 29 | c := &ContractInvokeParam{ 30 | Version: 0, 31 | Address: addr, 32 | Method: "init", 33 | Args: []byte{2}, 34 | } 35 | sink := common.NewZeroCopySink(nil) 36 | c.Serialization(sink) 37 | 38 | v := new(ContractInvokeParam) 39 | if err := v.Deserialization(common.NewZeroCopySource(sink.Bytes())); err != nil { 40 | t.Fatalf("ContractInvokeParam deserialize error: %v", err) 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /p2pserver/actor/req/consensus.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package req 20 | 21 | import ( 22 | "github.com/ontio/ontology-eventbus/actor" 23 | ) 24 | 25 | var ConsensusPid *actor.PID 26 | 27 | func SetConsensusPid(conPid *actor.PID) { 28 | ConsensusPid = conPid 29 | } 30 | -------------------------------------------------------------------------------- /p2pserver/actor/req/txnpool.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package req 20 | 21 | import ( 22 | "time" 23 | 24 | "github.com/ontio/ontology-eventbus/actor" 25 | "github.com/polynetwork/poly/common/log" 26 | "github.com/polynetwork/poly/core/types" 27 | p2pcommon "github.com/polynetwork/poly/p2pserver/common" 28 | tc "github.com/polynetwork/poly/txnpool/common" 29 | ) 30 | 31 | const txnPoolReqTimeout = p2pcommon.ACTOR_TIMEOUT * time.Second 32 | 33 | var txnPoolPid *actor.PID 34 | 35 | func SetTxnPoolPid(txnPid *actor.PID) { 36 | txnPoolPid = txnPid 37 | } 38 | 39 | //add txn to txnpool 40 | func AddTransaction(transaction *types.Transaction) { 41 | if txnPoolPid == nil { 42 | log.Error("[p2p]net_server AddTransaction(): txnpool pid is nil") 43 | return 44 | } 45 | txReq := &tc.TxReq{ 46 | Tx: transaction, 47 | Sender: tc.NetSender, 48 | TxResultCh: nil, 49 | } 50 | txnPoolPid.Tell(txReq) 51 | } 52 | -------------------------------------------------------------------------------- /p2pserver/common/checksum.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | package common 19 | 20 | import ( 21 | "crypto/sha256" 22 | "hash" 23 | ) 24 | 25 | // checksum implement hash.Hash interface and io.Writer 26 | type checksum struct { 27 | hash.Hash 28 | } 29 | 30 | func (self *checksum) Size() int { 31 | return CHECKSUM_LEN 32 | } 33 | 34 | func (self *checksum) Sum(b []byte) []byte { 35 | temp := self.Hash.Sum(nil) 36 | h := sha256.Sum256(temp) 37 | 38 | return append(b, h[:CHECKSUM_LEN]...) 39 | } 40 | 41 | func NewChecksum() hash.Hash { 42 | return &checksum{sha256.New()} 43 | } 44 | 45 | func Checksum(data []byte) [CHECKSUM_LEN]byte { 46 | var checksum [CHECKSUM_LEN]byte 47 | t := sha256.Sum256(data) 48 | s := sha256.Sum256(t[:]) 49 | 50 | copy(checksum[:], s[:]) 51 | 52 | return checksum 53 | } 54 | -------------------------------------------------------------------------------- /p2pserver/common/checksum_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | package common 19 | 20 | import ( 21 | "github.com/stretchr/testify/assert" 22 | "testing" 23 | ) 24 | 25 | func TestChecksum(t *testing.T) { 26 | data := []byte{1, 2, 3} 27 | cs := Checksum(data) 28 | 29 | writer := NewChecksum() 30 | writer.Write(data) 31 | checksum2 := writer.Sum(nil) 32 | assert.Equal(t, cs[:], checksum2) 33 | 34 | } 35 | -------------------------------------------------------------------------------- /p2pserver/message/types/address_req.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package types 20 | 21 | import ( 22 | "github.com/polynetwork/poly/common" 23 | comm "github.com/polynetwork/poly/p2pserver/common" 24 | ) 25 | 26 | type AddrReq struct{} 27 | 28 | //Serialize message payload 29 | func (this AddrReq) Serialization(sink *common.ZeroCopySink) error { 30 | return nil 31 | } 32 | 33 | func (this *AddrReq) CmdType() string { 34 | return comm.GetADDR_TYPE 35 | } 36 | 37 | //Deserialize message payload 38 | func (this *AddrReq) Deserialization(source *common.ZeroCopySource) error { 39 | return nil 40 | } 41 | -------------------------------------------------------------------------------- /p2pserver/message/types/address_req_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package types 20 | 21 | import ( 22 | "testing" 23 | ) 24 | 25 | func TestAddrReqSerializationDeserialization(t *testing.T) { 26 | var msg AddrReq 27 | 28 | MessageTest(t, &msg) 29 | } 30 | -------------------------------------------------------------------------------- /p2pserver/message/types/address_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with the poly network. If not, see . 16 | */ 17 | 18 | package types 19 | 20 | import ( 21 | "bytes" 22 | "net" 23 | "testing" 24 | 25 | "github.com/polynetwork/poly/common" 26 | comm "github.com/polynetwork/poly/p2pserver/common" 27 | "github.com/stretchr/testify/assert" 28 | ) 29 | 30 | func MessageTest(t *testing.T, msg Message) { 31 | sink := common.NewZeroCopySink(nil) 32 | err := WriteMessage(sink, msg) 33 | assert.Nil(t, err) 34 | 35 | demsg, _, err := ReadMessage(bytes.NewBuffer(sink.Bytes())) 36 | assert.Nil(t, err) 37 | 38 | assert.Equal(t, msg, demsg) 39 | } 40 | 41 | func TestAddressSerializationDeserialization(t *testing.T) { 42 | var msg Addr 43 | var addr [16]byte 44 | ip := net.ParseIP("192.168.0.1") 45 | ip.To16() 46 | copy(addr[:], ip[:16]) 47 | nodeAddr := comm.PeerAddr{ 48 | Time: 12345678, 49 | Services: 100, 50 | IpAddr: addr, 51 | Port: 8080, 52 | ConsensusPort: 8081, 53 | ID: 987654321, 54 | } 55 | msg.NodeAddrs = append(msg.NodeAddrs, nodeAddr) 56 | 57 | MessageTest(t, &msg) 58 | } 59 | -------------------------------------------------------------------------------- /p2pserver/message/types/block.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package types 20 | 21 | import ( 22 | "fmt" 23 | "github.com/polynetwork/poly/common" 24 | ct "github.com/polynetwork/poly/core/types" 25 | comm "github.com/polynetwork/poly/p2pserver/common" 26 | ) 27 | 28 | type Block struct { 29 | Blk *ct.Block 30 | MerkleRoot common.Uint256 31 | } 32 | 33 | //Serialize message payload 34 | func (this *Block) Serialization(sink *common.ZeroCopySink) error { 35 | err := this.Blk.Serialization(sink) 36 | if err != nil { 37 | return fmt.Errorf("serialize error. err:%v", err) 38 | } 39 | sink.WriteHash(this.MerkleRoot) 40 | return nil 41 | } 42 | 43 | func (this *Block) CmdType() string { 44 | return comm.BLOCK_TYPE 45 | } 46 | 47 | //Deserialize message payload 48 | func (this *Block) Deserialization(source *common.ZeroCopySource) error { 49 | this.Blk = new(ct.Block) 50 | err := this.Blk.Deserialization(source) 51 | if err != nil { 52 | return fmt.Errorf("read Blk error. err:%v", err) 53 | } 54 | 55 | eof := false 56 | this.MerkleRoot, eof = source.NextHash() 57 | if eof { 58 | // to accept old node's block 59 | this.MerkleRoot = common.UINT256_EMPTY 60 | } 61 | return nil 62 | } 63 | -------------------------------------------------------------------------------- /p2pserver/message/types/block_headers_req.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package types 20 | 21 | import ( 22 | "io" 23 | 24 | "github.com/polynetwork/poly/common" 25 | comm "github.com/polynetwork/poly/p2pserver/common" 26 | ) 27 | 28 | type HeadersReq struct { 29 | Len uint8 30 | HashStart common.Uint256 31 | HashEnd common.Uint256 32 | } 33 | 34 | //Serialize message payload 35 | func (this *HeadersReq) Serialization(sink *common.ZeroCopySink) error { 36 | sink.WriteUint8(this.Len) 37 | sink.WriteHash(this.HashStart) 38 | sink.WriteHash(this.HashEnd) 39 | return nil 40 | } 41 | 42 | func (this *HeadersReq) CmdType() string { 43 | return comm.GET_HEADERS_TYPE 44 | } 45 | 46 | //Deserialize message payload 47 | func (this *HeadersReq) Deserialization(source *common.ZeroCopySource) error { 48 | var eof bool 49 | this.Len, eof = source.NextUint8() 50 | this.HashStart, eof = source.NextHash() 51 | this.HashEnd, eof = source.NextHash() 52 | if eof { 53 | return io.ErrUnexpectedEOF 54 | } 55 | 56 | return nil 57 | } 58 | -------------------------------------------------------------------------------- /p2pserver/message/types/block_headers_req_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with the poly network. If not, see . 16 | */ 17 | 18 | package types 19 | 20 | import ( 21 | "testing" 22 | 23 | cm "github.com/polynetwork/poly/common" 24 | ) 25 | 26 | func TestBlkHdrReqSerializationDeserialization(t *testing.T) { 27 | var msg HeadersReq 28 | msg.Len = 1 29 | 30 | hashstr := "8932da73f52b1e22f30c609988ed1f693b6144f74fed9a2a20869afa7abfdf5e" 31 | msg.HashStart, _ = cm.Uint256FromHexString(hashstr) 32 | 33 | MessageTest(t, &msg) 34 | } 35 | -------------------------------------------------------------------------------- /p2pserver/message/types/blocks_req.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package types 20 | 21 | import ( 22 | "io" 23 | 24 | comm "github.com/polynetwork/poly/common" 25 | "github.com/polynetwork/poly/p2pserver/common" 26 | ) 27 | 28 | type BlocksReq struct { 29 | HeaderHashCount uint8 30 | HashStart comm.Uint256 31 | HashStop comm.Uint256 32 | } 33 | 34 | //Serialize message payload 35 | func (this *BlocksReq) Serialization(sink *comm.ZeroCopySink) error { 36 | sink.WriteUint8(this.HeaderHashCount) 37 | sink.WriteHash(this.HashStart) 38 | sink.WriteHash(this.HashStop) 39 | 40 | return nil 41 | } 42 | 43 | func (this *BlocksReq) CmdType() string { 44 | return common.GET_BLOCKS_TYPE 45 | } 46 | 47 | //Deserialize message payload 48 | func (this *BlocksReq) Deserialization(source *comm.ZeroCopySource) error { 49 | var eof bool 50 | this.HeaderHashCount, eof = source.NextUint8() 51 | this.HashStart, eof = source.NextHash() 52 | this.HashStop, eof = source.NextHash() 53 | 54 | if eof { 55 | return io.ErrUnexpectedEOF 56 | } 57 | return nil 58 | } 59 | -------------------------------------------------------------------------------- /p2pserver/message/types/blocks_req_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with the poly network. If not, see . 16 | */ 17 | 18 | package types 19 | 20 | import ( 21 | "testing" 22 | 23 | cm "github.com/polynetwork/poly/common" 24 | ) 25 | 26 | func TestBlkReqSerializationDeserialization(t *testing.T) { 27 | var msg BlocksReq 28 | msg.HeaderHashCount = 1 29 | 30 | hashstr := "8932da73f52b1e22f30c609988ed1f693b6144f74fed9a2a20869afa7abfdf5e" 31 | msg.HashStart, _ = cm.Uint256FromHexString(hashstr) 32 | 33 | MessageTest(t, &msg) 34 | } 35 | -------------------------------------------------------------------------------- /p2pserver/message/types/consensus.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package types 20 | 21 | import ( 22 | comm "github.com/polynetwork/poly/common" 23 | "github.com/polynetwork/poly/p2pserver/common" 24 | ) 25 | 26 | type Consensus struct { 27 | Cons ConsensusPayload 28 | } 29 | 30 | //Serialize message payload 31 | func (this *Consensus) Serialization(sink *comm.ZeroCopySink) error { 32 | return this.Cons.Serialization(sink) 33 | } 34 | 35 | func (this *Consensus) CmdType() string { 36 | return common.CONSENSUS_TYPE 37 | } 38 | 39 | //Deserialize message payload 40 | func (this *Consensus) Deserialization(source *comm.ZeroCopySource) error { 41 | return this.Cons.Deserialization(source) 42 | } 43 | -------------------------------------------------------------------------------- /p2pserver/message/types/data_req.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package types 20 | 21 | import ( 22 | "io" 23 | 24 | "github.com/polynetwork/poly/common" 25 | comm "github.com/polynetwork/poly/p2pserver/common" 26 | ) 27 | 28 | type DataReq struct { 29 | DataType common.InventoryType 30 | Hash common.Uint256 31 | } 32 | 33 | //Serialize message payload 34 | func (this DataReq) Serialization(sink *common.ZeroCopySink) error { 35 | sink.WriteByte(byte(this.DataType)) 36 | sink.WriteHash(this.Hash) 37 | 38 | return nil 39 | } 40 | 41 | func (this *DataReq) CmdType() string { 42 | return comm.GET_DATA_TYPE 43 | } 44 | 45 | //Deserialize message payload 46 | func (this *DataReq) Deserialization(source *common.ZeroCopySource) error { 47 | ty, eof := source.NextByte() 48 | this.DataType = common.InventoryType(ty) 49 | 50 | this.Hash, eof = source.NextHash() 51 | if eof { 52 | return io.ErrUnexpectedEOF 53 | } 54 | 55 | return nil 56 | } 57 | -------------------------------------------------------------------------------- /p2pserver/message/types/data_req_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * 14 | * You should have received a copy of the GNU Lesser General Public License 15 | * along with the poly network. If not, see . 16 | */ 17 | 18 | package types 19 | 20 | import ( 21 | "testing" 22 | 23 | cm "github.com/polynetwork/poly/common" 24 | ) 25 | 26 | func TestDataReqSerializationDeserialization(t *testing.T) { 27 | var msg DataReq 28 | msg.DataType = 0x02 29 | 30 | hashstr := "8932da73f52b1e22f30c609988ed1f693b6144f74fed9a2a20869afa7abfdf5e" 31 | bhash, _ := cm.HexToBytes(hashstr) 32 | copy(msg.Hash[:], bhash) 33 | 34 | MessageTest(t, &msg) 35 | } 36 | -------------------------------------------------------------------------------- /p2pserver/message/types/disconnected.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package types 20 | 21 | import ( 22 | comm "github.com/polynetwork/poly/common" 23 | "github.com/polynetwork/poly/p2pserver/common" 24 | ) 25 | 26 | type Disconnected struct{} 27 | 28 | //Serialize message payload 29 | func (this Disconnected) Serialization(sink *comm.ZeroCopySink) error { 30 | return nil 31 | } 32 | 33 | func (this Disconnected) CmdType() string { 34 | return common.DISCONNECT_TYPE 35 | } 36 | 37 | //Deserialize message payload 38 | func (this *Disconnected) Deserialization(source *comm.ZeroCopySource) error { 39 | return nil 40 | } 41 | -------------------------------------------------------------------------------- /p2pserver/message/types/notfound.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package types 20 | 21 | import ( 22 | "io" 23 | 24 | "github.com/polynetwork/poly/common" 25 | comm "github.com/polynetwork/poly/p2pserver/common" 26 | ) 27 | 28 | type NotFound struct { 29 | Hash common.Uint256 30 | } 31 | 32 | //Serialize message payload 33 | func (this NotFound) Serialization(sink *common.ZeroCopySink) error { 34 | sink.WriteHash(this.Hash) 35 | return nil 36 | } 37 | 38 | func (this NotFound) CmdType() string { 39 | return comm.NOT_FOUND_TYPE 40 | } 41 | 42 | //Deserialize message payload 43 | func (this *NotFound) Deserialization(source *common.ZeroCopySource) error { 44 | var eof bool 45 | this.Hash, eof = source.NextHash() 46 | if eof { 47 | return io.ErrUnexpectedEOF 48 | } 49 | 50 | return nil 51 | } 52 | -------------------------------------------------------------------------------- /p2pserver/message/types/notfound_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package types 20 | 21 | import ( 22 | "testing" 23 | 24 | cm "github.com/polynetwork/poly/common" 25 | ) 26 | 27 | func Uint256ParseFromBytes(f []byte) cm.Uint256 { 28 | if len(f) != 32 { 29 | return cm.Uint256{} 30 | } 31 | 32 | var hash [32]uint8 33 | for i := 0; i < 32; i++ { 34 | hash[i] = f[i] 35 | } 36 | return cm.Uint256(hash) 37 | } 38 | 39 | func TestNotFoundSerializationDeserialization(t *testing.T) { 40 | var msg NotFound 41 | str := "123456" 42 | hash := []byte(str) 43 | msg.Hash = Uint256ParseFromBytes(hash) 44 | 45 | MessageTest(t, &msg) 46 | } 47 | -------------------------------------------------------------------------------- /p2pserver/message/types/ping.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package types 20 | 21 | import ( 22 | "io" 23 | 24 | comm "github.com/polynetwork/poly/common" 25 | "github.com/polynetwork/poly/p2pserver/common" 26 | ) 27 | 28 | type Ping struct { 29 | Height uint64 30 | } 31 | 32 | //Serialize message payload 33 | func (this Ping) Serialization(sink *comm.ZeroCopySink) error { 34 | sink.WriteUint64(this.Height) 35 | return nil 36 | } 37 | 38 | func (this *Ping) CmdType() string { 39 | return common.PING_TYPE 40 | } 41 | 42 | //Deserialize message payload 43 | func (this *Ping) Deserialization(source *comm.ZeroCopySource) error { 44 | var eof bool 45 | this.Height, eof = source.NextUint64() 46 | if eof { 47 | return io.ErrUnexpectedEOF 48 | } 49 | 50 | return nil 51 | } 52 | -------------------------------------------------------------------------------- /p2pserver/message/types/ping_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package types 20 | 21 | import ( 22 | "testing" 23 | ) 24 | 25 | func TestPingSerializationDeserialization(t *testing.T) { 26 | var msg Ping 27 | msg.Height = 1 28 | 29 | MessageTest(t, &msg) 30 | } 31 | -------------------------------------------------------------------------------- /p2pserver/message/types/pong.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package types 20 | 21 | import ( 22 | "io" 23 | 24 | comm "github.com/polynetwork/poly/common" 25 | "github.com/polynetwork/poly/p2pserver/common" 26 | ) 27 | 28 | type Pong struct { 29 | Height uint64 30 | } 31 | 32 | //Serialize message payload 33 | func (this Pong) Serialization(sink *comm.ZeroCopySink) error { 34 | sink.WriteUint64(this.Height) 35 | return nil 36 | } 37 | 38 | func (this Pong) CmdType() string { 39 | return common.PONG_TYPE 40 | } 41 | 42 | //Deserialize message payload 43 | func (this *Pong) Deserialization(source *comm.ZeroCopySource) error { 44 | var eof bool 45 | this.Height, eof = source.NextUint64() 46 | if eof { 47 | return io.ErrUnexpectedEOF 48 | } 49 | 50 | return nil 51 | } 52 | -------------------------------------------------------------------------------- /p2pserver/message/types/pong_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package types 20 | 21 | import ( 22 | "testing" 23 | ) 24 | 25 | func TestPongSerializationDeserialization(t *testing.T) { 26 | var msg Pong 27 | msg.Height = 1 28 | 29 | MessageTest(t, &msg) 30 | } 31 | -------------------------------------------------------------------------------- /p2pserver/message/types/transaction.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package types 20 | 21 | import ( 22 | comm "github.com/polynetwork/poly/common" 23 | "github.com/polynetwork/poly/core/types" 24 | "github.com/polynetwork/poly/p2pserver/common" 25 | ) 26 | 27 | // Transaction message 28 | type Trn struct { 29 | Txn *types.Transaction 30 | } 31 | 32 | //Serialize message payload 33 | func (this Trn) Serialization(sink *comm.ZeroCopySink) error { 34 | return this.Txn.Serialization(sink) 35 | } 36 | 37 | func (this *Trn) CmdType() string { 38 | return common.TX_TYPE 39 | } 40 | 41 | //Deserialize message payload 42 | func (this *Trn) Deserialization(source *comm.ZeroCopySource) error { 43 | tx := &types.Transaction{} 44 | err := tx.Deserialization(source) 45 | if err != nil { 46 | return err 47 | } 48 | 49 | this.Txn = tx 50 | return nil 51 | } 52 | -------------------------------------------------------------------------------- /p2pserver/message/types/verack.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package types 20 | 21 | import ( 22 | "io" 23 | 24 | comm "github.com/polynetwork/poly/common" 25 | "github.com/polynetwork/poly/p2pserver/common" 26 | ) 27 | 28 | type VerACK struct { 29 | IsConsensus bool 30 | } 31 | 32 | //Serialize message payload 33 | func (this *VerACK) Serialization(sink *comm.ZeroCopySink) error { 34 | sink.WriteBool(this.IsConsensus) 35 | return nil 36 | } 37 | 38 | func (this *VerACK) CmdType() string { 39 | return common.VERACK_TYPE 40 | } 41 | 42 | //Deserialize message payload 43 | func (this *VerACK) Deserialization(source *comm.ZeroCopySource) error { 44 | var eof bool 45 | this.IsConsensus, eof = source.NextBool() 46 | if eof { 47 | return io.ErrUnexpectedEOF 48 | } 49 | return nil 50 | } 51 | -------------------------------------------------------------------------------- /p2pserver/message/types/verack_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package types 20 | 21 | import ( 22 | "testing" 23 | ) 24 | 25 | func TestVerackSerializationDeserialization(t *testing.T) { 26 | var msg VerACK 27 | msg.IsConsensus = false 28 | 29 | MessageTest(t, &msg) 30 | } 31 | -------------------------------------------------------------------------------- /p2pserver/message/utils/msg_router_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package utils 20 | 21 | import ( 22 | "github.com/ontio/ontology-eventbus/actor" 23 | "github.com/polynetwork/poly/common/log" 24 | "github.com/polynetwork/poly/p2pserver/message/types" 25 | "github.com/polynetwork/poly/p2pserver/net/netserver" 26 | "github.com/polynetwork/poly/p2pserver/net/protocol" 27 | "github.com/stretchr/testify/assert" 28 | "testing" 29 | ) 30 | 31 | func testHandler(data *types.MsgPayload, p2p p2p.P2P, pid *actor.PID, args ...interface{}) { 32 | log.Info("Test handler") 33 | } 34 | 35 | // TestMsgRouter tests a basic function of a message router 36 | func TestMsgRouter(t *testing.T) { 37 | network := netserver.NewNetServer() 38 | msgRouter := NewMsgRouter(network) 39 | assert.NotNil(t, msgRouter) 40 | 41 | msgRouter.RegisterMsgHandler("test", testHandler) 42 | msgRouter.UnRegisterMsgHandler("test") 43 | msgRouter.Start() 44 | msgRouter.Stop() 45 | } 46 | -------------------------------------------------------------------------------- /p2pserver/p2pserver_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package p2pserver 20 | 21 | import ( 22 | "fmt" 23 | "testing" 24 | 25 | "github.com/polynetwork/poly/common/log" 26 | "github.com/polynetwork/poly/p2pserver/common" 27 | ) 28 | 29 | func init() { 30 | log.InitLog(log.InfoLog) 31 | fmt.Println("Start test the netserver...") 32 | 33 | } 34 | func TestNewP2PServer(t *testing.T) { 35 | log.Init(log.Stdout) 36 | fmt.Println("Start test new p2pserver...") 37 | 38 | p2p := NewServer() 39 | 40 | if p2p.GetVersion() != common.PROTOCOL_VERSION { 41 | t.Error("TestNewP2PServer p2p version error", p2p.GetVersion()) 42 | } 43 | 44 | if p2p.GetVersion() != common.PROTOCOL_VERSION { 45 | t.Error("TestNewP2PServer p2p version error") 46 | } 47 | sync, cons := p2p.GetPort() 48 | if sync != 20338 { 49 | t.Error("TestNewP2PServer sync port error") 50 | } 51 | 52 | if cons != 20339 { 53 | t.Error("TestNewP2PServer consensus port error") 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /scripts/install_dependencies.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | case $(uname | tr '[:upper:]' '[:lower:]') in 4 | linux*) 5 | ./scripts/install_linux_dependencies.sh 6 | ;; 7 | darwin*) 8 | ./scripts/install_macos_dependencies.sh 9 | ;; 10 | *) 11 | echo "Unsupported os" 12 | ;; 13 | esac -------------------------------------------------------------------------------- /scripts/install_linux_dependencies.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | echo "Preparing dependencies for linux/ubuntu" 4 | 5 | if [ "$(grep -Ei 'debian|buntu|mint' /etc/*release)" ]; then 6 | apt update 7 | apt install -y libgmp-dev libssl-dev make gcc g++ 8 | else 9 | yum install glibc-static gmp-devel gmp-static openssl-libs openssl-static gcc-c++ 10 | fi 11 | 12 | # Prepare temp directory 13 | mkdir -p temp 14 | pushd temp 15 | 16 | # Prepare harmony dependencies 17 | git clone https://github.com/harmony-one/bls.git 18 | pushd bls 19 | git checkout 2b7e49894c0f15f5c40cf74046505b7f74946e52 20 | popd 21 | 22 | git clone https://github.com/harmony-one/mcl.git 23 | pushd mcl 24 | git checkout 99e9aa76e84415e753956c618cbc662b2f373df1 25 | popd 26 | 27 | export CGO_CFLAGS="-I$PWD/bls/include -I$PWD/mcl/include" 28 | export CGO_LDFLAGS="-L$PWD/bls/lib" 29 | export LD_LIBRARY_PATH=$PWD/bls/lib:$PWD/mcl/lib 30 | export LIBRARY_PATH=$LD_LIBRARY_PATH 31 | export DYLD_FALLBACK_LIBRARY_PATH=$LD_LIBRARY_PATH 32 | 33 | echo "making mcl" 34 | make -C $PWD/mcl -j8 35 | echo "making bls" 36 | make -C $PWD/bls minimised_static BLS_SWAP_G=1 -j8 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /scripts/install_macos_dependencies.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | echo "Preparing dependencies for macos" 4 | 5 | brew install gmp 6 | brew install openssl@1.1 7 | sudo ln -sf /opt/homebrew/opt/openssl@1.1/include/openssl /usr/local/include/openssl 8 | sudo mkdir -p /usr/local/opt/openssl 9 | sudo ln -sf /opt/homebrew/opt/openssl@1.1/lib /usr/local/opt/openssl/lib 10 | sudo mkdir -p /usr/local/opt/gmp 11 | sudo ln -sf /opt/homebrew/opt/gmp/include /usr/local/include/gmp 12 | sudo ln -sf /opt/homebrew/opt/gmp/lib /usr/local/opt/gmp/lib 13 | 14 | # Prepare temp directory 15 | mkdir -p temp 16 | pushd temp 17 | 18 | # Prepare harmony dependencies 19 | git clone --depth=1 https://github.com/harmony-one/bls.git 20 | pushd bls 21 | git checkout 2b7e49894c0f15f5c40cf74046505b7f74946e52 22 | popd 23 | 24 | git clone --depth=1 https://github.com/harmony-one/mcl.git 25 | pushd mcl 26 | git checkout 99e9aa76e84415e753956c618cbc662b2f373df1 27 | popd 28 | 29 | export CGO_CFLAGS="-I$PWD/bls/include -I$PWD/mcl/include -I/usr/local/include" 30 | export CGO_LDFLAGS="-L$PWD/bls/lib -L/usr/local/opt/openssl/lib" 31 | export LD_LIBRARY_PATH="$PWD/bls/lib:$PWD/mcl/lib:/usr/local/opt/openssl/lib:/usr/local/opt/gmp/lib:/usr/local/lib" 32 | export LIBRARY_PATH=$LD_LIBRARY_PATH 33 | export DYLD_FALLBACK_LIBRARY_PATH=$LD_LIBRARY_PATH 34 | 35 | echo "making mcl" 36 | make -C $PWD/mcl -j8 37 | echo "making bls" 38 | make -C $PWD/bls BLS_SWAP_G=1 -j8 39 | 40 | sudo ln -sf $PWD/bls/lib/libbls384_256.dylib /usr/local/lib/libbls384_256.dylib 41 | sudo ln -sf $PWD/mcl/lib/libmcl.dylib /usr/local/lib/libmcl.dylib 42 | -------------------------------------------------------------------------------- /validator/db/best_block.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package db 20 | 21 | import ( 22 | "github.com/polynetwork/poly/common" 23 | "github.com/polynetwork/poly/core/types" 24 | ) 25 | 26 | type BestBlock struct { 27 | Height uint32 28 | Hash common.Uint256 29 | } 30 | 31 | type BestStateProvider interface { 32 | GetBestBlock() (BestBlock, error) 33 | GetBestHeader() (*types.Header, error) 34 | } 35 | -------------------------------------------------------------------------------- /validator/db/config.json: -------------------------------------------------------------------------------- 1 | { 2 | "Configuration": { 3 | "Magic": 7630401, 4 | "Version": 23, 5 | "SeedList": [ 6 | "139.219.65.178:10338", 7 | "139.219.99.201:10338", 8 | "139.219.96.154:10338" 9 | ], 10 | "Bookkeepers": [ 11 | "0322cfdb6a20401c2e44ede40b5282b2925fcff21cdc3814d782fd26026f1d023d", 12 | "02b639c019537839ba30b7c8c0396095da8838993492c07fe6ca11a5cf7b8fd2ca", 13 | "032c842494feba4e3dec3b9b7d9ad080ce63c81a41f7d79d2bbb5d499d16322907", 14 | "03d36828a99547184452276116f1b5171861931ff439a6da2316fddf1f3f428850" 15 | ], 16 | "HttpInfoPort": 20333, 17 | "HttpInfoStart": true, 18 | "HttpRestPort": 20334, 19 | "HttpWsPort":20335, 20 | "HttpJsonPort": 20336, 21 | "HttpLocalPort": 20337, 22 | "NoticeServerUrl":"", 23 | "OauthServerUrl":"", 24 | "NodePort": 20338, 25 | "PrintLevel": 3, 26 | "IsTLS": false, 27 | "CertPath": "./sample-cert.pem", 28 | "KeyPath": "./sample-cert-key.pem", 29 | "CAPath": "./sample-ca.pem", 30 | "MultiCoreNum": 4, 31 | "SystemFee": { 32 | "RegisterAsset": 10000, 33 | "IssueAsset": 10000 34 | }, 35 | "ConsensusType":"solo" 36 | } 37 | } -------------------------------------------------------------------------------- /validator/db/key_prefix.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package db 20 | 21 | import ( 22 | pool "github.com/valyala/bytebufferpool" 23 | 24 | "github.com/polynetwork/poly/common" 25 | ) 26 | 27 | // DataEntryPrefix 28 | type KeyPrefix byte 29 | 30 | const ( 31 | //SYSTEM 32 | SYS_VERSION KeyPrefix = 0 33 | SYS_GENESIS_BLOCK KeyPrefix = 1 // key: prefix, value: gensisBlock 34 | 35 | SYS_BEST_BLOCK KeyPrefix = 2 // key : prefix, value: bestblock 36 | SYS_BEST_BLOCK_HEADER KeyPrefix = 3 // key: prefix, value: BlockHeader 37 | 38 | // DATA 39 | //DATA_Block KeyPrefix = iota 40 | //DATA_Header 41 | DATA_TRANSACTION KeyPrefix = 10 // key: prefix+txid, value: height + tx 42 | ) 43 | 44 | func GenGenesisBlockKey() *pool.ByteBuffer { 45 | key := keyPool.Get() 46 | key.WriteByte(byte(SYS_GENESIS_BLOCK)) 47 | return key 48 | } 49 | 50 | func GenBestBlockHeaderKey() *pool.ByteBuffer { 51 | key := keyPool.Get() 52 | key.WriteByte(byte(SYS_BEST_BLOCK_HEADER)) 53 | return key 54 | } 55 | 56 | func GenDataTransactionKey(hash common.Uint256) *pool.ByteBuffer { 57 | key := keyPool.Get() 58 | key.WriteByte(byte(DATA_TRANSACTION)) 59 | key.Write(hash.ToArray()) 60 | return key 61 | } 62 | -------------------------------------------------------------------------------- /validator/db/store_test.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package db 20 | 21 | import ( 22 | "testing" 23 | 24 | "github.com/stretchr/testify/assert" 25 | ) 26 | 27 | func TestNewStore(t *testing.T) { 28 | store, err := NewStore("temp.db") 29 | assert.Nil(t, err) 30 | 31 | _, err = store.GetBestBlock() 32 | assert.NotNil(t, err) 33 | } 34 | -------------------------------------------------------------------------------- /validator/db/transaction_provider.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package db 20 | 21 | import ( 22 | "github.com/polynetwork/poly/common" 23 | "github.com/polynetwork/poly/core/types" 24 | ) 25 | 26 | type TransactionProvider interface { 27 | BestStateProvider 28 | ContainTransaction(hash common.Uint256) bool 29 | GetTransactionBytes(hash common.Uint256) ([]byte, error) 30 | GetTransaction(hash common.Uint256) (*types.Transaction, error) 31 | PersistBlock(block *types.Block) error 32 | } 33 | -------------------------------------------------------------------------------- /validator/stateful/config.json: -------------------------------------------------------------------------------- 1 | { 2 | "Configuration": { 3 | "Magic": 7630401, 4 | "Version": 23, 5 | "SeedList": [ 6 | "139.219.65.178:10338", 7 | "139.219.99.201:10338", 8 | "139.219.96.154:10338" 9 | ], 10 | "Bookkeepers": [ 11 | "0322cfdb6a20401c2e44ede40b5282b2925fcff21cdc3814d782fd26026f1d023d", 12 | "02b639c019537839ba30b7c8c0396095da8838993492c07fe6ca11a5cf7b8fd2ca", 13 | "032c842494feba4e3dec3b9b7d9ad080ce63c81a41f7d79d2bbb5d499d16322907", 14 | "03d36828a99547184452276116f1b5171861931ff439a6da2316fddf1f3f428850" 15 | ], 16 | "HttpInfoPort": 20333, 17 | "HttpInfoStart": true, 18 | "HttpRestPort": 20334, 19 | "HttpWsPort":20335, 20 | "HttpJsonPort": 20336, 21 | "HttpLocalPort": 20337, 22 | "NoticeServerUrl":"", 23 | "OauthServerUrl":"", 24 | "NodePort": 20338, 25 | "PrintLevel": 3, 26 | "IsTLS": false, 27 | "CertPath": "./sample-cert.pem", 28 | "KeyPath": "./sample-cert-key.pem", 29 | "CAPath": "./sample-ca.pem", 30 | "MultiCoreNum": 4, 31 | "SystemFee": { 32 | "RegisterAsset": 10000, 33 | "IssueAsset": 10000 34 | }, 35 | "ConsensusType":"solo" 36 | } 37 | } -------------------------------------------------------------------------------- /validator/types/messages.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2021 The poly network Authors 3 | * This file is part of The poly network library. 4 | * 5 | * The poly network is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU Lesser General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * The poly network is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public License 16 | * along with the poly network. If not, see . 17 | */ 18 | 19 | package types 20 | 21 | import ( 22 | "github.com/ontio/ontology-eventbus/actor" 23 | "github.com/polynetwork/poly/common" 24 | "github.com/polynetwork/poly/core/types" 25 | "github.com/polynetwork/poly/errors" 26 | ) 27 | 28 | // message 29 | type RegisterValidator struct { 30 | Sender *actor.PID 31 | Type VerifyType 32 | Id string 33 | } 34 | 35 | type UnRegisterValidator struct { 36 | Id string 37 | Type VerifyType 38 | } 39 | 40 | type UnRegisterAck struct { 41 | Id string 42 | Type VerifyType 43 | } 44 | 45 | type CheckTx struct { 46 | WorkerId uint8 47 | Tx *types.Transaction 48 | } 49 | 50 | type CheckResponse struct { 51 | WorkerId uint8 52 | Type VerifyType 53 | Hash common.Uint256 54 | Height uint32 55 | ErrCode errors.ErrCode 56 | } 57 | 58 | // VerifyType of validator 59 | type VerifyType uint8 60 | 61 | const ( 62 | Stateless VerifyType = iota 63 | Stateful VerifyType = iota 64 | ) 65 | --------------------------------------------------------------------------------