From 419544a7f10945579d9edc467be6feb6efc6f475 Mon Sep 17 00:00:00 2001 From: Abner Zheng Date: Mon, 11 Jan 2021 14:40:24 +0800 Subject: [PATCH 1/2] ECO-781: remove Faucet contract since SRE team will deploy it. Signed-off-by: Abner Zheng --- smart-contract/.gitignore | 2 - smart-contract/Cargo.toml | 6 --- smart-contract/Makefile | 20 -------- smart-contract/faucet-stored/Cargo.toml | 20 -------- smart-contract/faucet-stored/src/main.rs | 59 ------------------------ smart-contract/faucet/Cargo.toml | 19 -------- smart-contract/faucet/src/bin/main.rs | 7 --- smart-contract/faucet/src/lib.rs | 38 --------------- smart-contract/rust-toolchain | 1 - 9 files changed, 172 deletions(-) delete mode 100644 smart-contract/.gitignore delete mode 100644 smart-contract/Cargo.toml delete mode 100644 smart-contract/Makefile delete mode 100644 smart-contract/faucet-stored/Cargo.toml delete mode 100644 smart-contract/faucet-stored/src/main.rs delete mode 100644 smart-contract/faucet/Cargo.toml delete mode 100644 smart-contract/faucet/src/bin/main.rs delete mode 100644 smart-contract/faucet/src/lib.rs delete mode 100644 smart-contract/rust-toolchain diff --git a/smart-contract/.gitignore b/smart-contract/.gitignore deleted file mode 100644 index a9d37c56..00000000 --- a/smart-contract/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -target -Cargo.lock diff --git a/smart-contract/Cargo.toml b/smart-contract/Cargo.toml deleted file mode 100644 index 1895d8a1..00000000 --- a/smart-contract/Cargo.toml +++ /dev/null @@ -1,6 +0,0 @@ -[workspace] - -members = [ - "faucet", - "faucet-stored" -] diff --git a/smart-contract/Makefile b/smart-contract/Makefile deleted file mode 100644 index 84b83659..00000000 --- a/smart-contract/Makefile +++ /dev/null @@ -1,20 +0,0 @@ -RUST_TOOLCHAIN := $(shell cat rust-toolchain) - -prepare: - rustup toolchain install --no-self-update $(RUST_TOOLCHAIN) - rustup target add --toolchain $(RUST_TOOLCHAIN) wasm32-unknown-unknown - -build-contracts: - cargo build --release -p faucet-stored --target wasm32-unknown-unknown - -clean: - cargo clean - -copy-wasm-file-to-clarity: - mkdir -p ../contracts - cp target/wasm32-unknown-unknown/release/faucet_stored.wasm ../contracts - -all: \ - build-contracts \ - copy-wasm-file-to-clarity - diff --git a/smart-contract/faucet-stored/Cargo.toml b/smart-contract/faucet-stored/Cargo.toml deleted file mode 100644 index 00c15d76..00000000 --- a/smart-contract/faucet-stored/Cargo.toml +++ /dev/null @@ -1,20 +0,0 @@ -[package] -name = "faucet-stored" -version = "0.1.0" -authors = ["Mateusz Górski "] -edition = "2018" - -[[bin]] -name = "faucet_stored" -path = "src/main.rs" -bench = false -doctest = false -test = false - -[features] -std = ["casperlabs-contract/std", "casperlabs-types/std"] - -[dependencies] -casperlabs-contract = "0.6.0" -casperlabs-types = "0.6.0" -faucet = { path = "../faucet", package = "faucet" } diff --git a/smart-contract/faucet-stored/src/main.rs b/smart-contract/faucet-stored/src/main.rs deleted file mode 100644 index 4c00e460..00000000 --- a/smart-contract/faucet-stored/src/main.rs +++ /dev/null @@ -1,59 +0,0 @@ -#![no_std] -#![no_main] - -extern crate alloc; - -use alloc::{string::ToString, vec}; - -use casperlabs_contract::contract_api::{runtime, storage}; -use casperlabs_types::{ - account::AccountHash, CLType, CLTyped, ContractHash, ContractVersion, EntryPoint, - EntryPointAccess, EntryPointType, EntryPoints, Parameter, -}; - -const CONTRACT_NAME: &str = "faucet"; -const HASH_KEY_NAME: &str = "faucet_package"; -const ACCESS_KEY_NAME: &str = "faucet_package_access"; -const ENTRY_POINT_NAME: &str = "call_faucet"; -const ARG_TARGET: &str = "target"; -const ARG_AMOUNT: &str = "amount"; -const CONTRACT_VERSION: &str = "contract_version"; - -#[no_mangle] -pub extern "C" fn call_faucet() { - faucet::delegate(); -} - -fn store() -> (ContractHash, ContractVersion) { - let entry_points = { - let mut entry_points = EntryPoints::new(); - - let entry_point = EntryPoint::new( - ENTRY_POINT_NAME, - vec![ - Parameter::new(ARG_TARGET, AccountHash::cl_type()), - Parameter::new(ARG_AMOUNT, CLType::U512), - ], - CLType::Unit, - EntryPointAccess::Public, - EntryPointType::Session, - ); - - entry_points.add_entry_point(entry_point); - - entry_points - }; - storage::new_contract( - entry_points, - None, - Some(HASH_KEY_NAME.to_string()), - Some(ACCESS_KEY_NAME.to_string()), - ) -} - -#[no_mangle] -pub extern "C" fn call() { - let (contract_hash, contract_version) = store(); - runtime::put_key(CONTRACT_VERSION, storage::new_uref(contract_version).into()); - runtime::put_key(CONTRACT_NAME, contract_hash.into()); -} diff --git a/smart-contract/faucet/Cargo.toml b/smart-contract/faucet/Cargo.toml deleted file mode 100644 index 67378b7f..00000000 --- a/smart-contract/faucet/Cargo.toml +++ /dev/null @@ -1,19 +0,0 @@ -[package] -name = "faucet" -version = "0.1.0" -authors = ["Mateusz Górski "] -edition = "2018" - -[[bin]] -name = "faucet" -path = "src/bin/main.rs" -doctest = false -test = false -bench = false - -[features] -std = ["casperlabs-contract/std", "casperlabs-types/std"] - -[dependencies] -casperlabs-contract = "0.6.0" -casperlabs-types = "0.6.0" diff --git a/smart-contract/faucet/src/bin/main.rs b/smart-contract/faucet/src/bin/main.rs deleted file mode 100644 index 6d294133..00000000 --- a/smart-contract/faucet/src/bin/main.rs +++ /dev/null @@ -1,7 +0,0 @@ -#![no_std] -#![no_main] - -#[no_mangle] -pub extern "C" fn call() { - faucet::delegate(); -} diff --git a/smart-contract/faucet/src/lib.rs b/smart-contract/faucet/src/lib.rs deleted file mode 100644 index 1f4c3742..00000000 --- a/smart-contract/faucet/src/lib.rs +++ /dev/null @@ -1,38 +0,0 @@ -#![no_std] - -use casperlabs_contract::{ - contract_api::{runtime, storage, system}, - unwrap_or_revert::UnwrapOrRevert, -}; -use casperlabs_types::{account::AccountHash, ApiError, U512}; - -const ARG_TARGET: &str = "target"; -const ARG_AMOUNT: &str = "amount"; - -#[repr(u32)] -enum CustomError { - AlreadyFunded = 1, -} - -/// Executes token transfer to supplied account hash. -/// Revert status codes: -/// 1 - requested transfer to already funded account hash. -#[no_mangle] -pub fn delegate() { - let account_hash: AccountHash = runtime::get_named_arg(ARG_TARGET); - - let amount: U512 = runtime::get_named_arg(ARG_AMOUNT); - - // Maybe we will decide to allow multiple funds up until some maximum value. - let already_funded = storage::read_local::(&account_hash) - .unwrap_or_default() - .is_some(); - - if already_funded { - runtime::revert(ApiError::User(CustomError::AlreadyFunded as u16)); - } else { - system::transfer_to_account(account_hash, amount).unwrap_or_revert(); - // Transfer successful; Store the fact of funding in the local state. - storage::write_local(account_hash, amount); - } -} diff --git a/smart-contract/rust-toolchain b/smart-contract/rust-toolchain deleted file mode 100644 index 10a652fd..00000000 --- a/smart-contract/rust-toolchain +++ /dev/null @@ -1 +0,0 @@ -nightly-2020-03-19 From 120e251ebf5b310e14219933c02a28f0a08abacf Mon Sep 17 00:00:00 2001 From: Abner Zheng Date: Mon, 11 Jan 2021 15:00:58 +0800 Subject: [PATCH 2/2] ECO-781: update outdated documents. Signed-off-by: Abner Zheng --- README.md | 216 +------------------------------------- packages/server/.env | 16 +-- packages/server/README.md | 28 ++--- packages/ui/README.md | 4 +- 4 files changed, 23 insertions(+), 241 deletions(-) diff --git a/README.md b/README.md index b0632d55..330b4389 100644 --- a/README.md +++ b/README.md @@ -2,9 +2,9 @@ The purpose of the explorer is to help users interact with the blockchain: -- Sign up to participate in devnet +- Sign up to participate in TestNet - Create accounts (public/private key pairs) -- Ask the faucet for tokens on devnet +- Ask the faucet for tokens on TestNet - Explore the block DAG - Deploy contracts @@ -14,216 +14,10 @@ You could use `yarn run bootstrap` to install all dependencies ## Build -You can use `yarn` in the `ui`, `server` and `sdk` directories to build and interactively develop the components. +You can use `yarn build` in the root directories to build and use `yarn run dev` to interactively develop the components. -To package the whole thing into a docker image, run `docker-build/clarity` in the project root directory. +To package the whole thing into a docker image, run `make docker-build-all` in the project root directory. ## Test -To test the faucet we need the node running and ready to accept deploys. We also have to fund it with initial tokens and send one deploy in the name of the genesis account which will transfer funds to the faucet. - -We can use the `contracts/transfer` to donate the initial amount of funds to the faucet. Contracts will be built together with the docker image, but you can build them separately by running `make .make/explorer/contracts` in the top level directory. - -Start a local docker network first: - -```sh -cd ../hack/docker -make up node-0/up -cd - -``` - -Start the `server` in one console: - -```sh -cd server -npm run dev -``` - -and the `ui` in another one: - -```sh -cd ui -npm start -``` - -A new browser window will automatically open pointing at the app running at http://localhost:8000 - -The server should serve the config that will route traffic to the `grpcwebproxy` container running in Docker, while we can work on the UI and see it reload after each change. Check out the `server` README for details about how it can be configured to tell the UI to connect to a remote server like devnet. - -### Fund the Faucet - -If we were not using the `faucet-account` that's created in the `hack/docker` setup as the Faucet account, we'd have to establish the an account by transfering some tokens to it that it can later pass on. Here's how to do it: - -Generate the necessary contracts first: - -```console -cd .. ; make build-explorer-contracts ; cd - -``` - -The `server` component has a utility program to do the initial token transfer, let's build that first (not necessary if we already built everything with docker): - -```console -cd server ; npm run build ; cd - -``` - -Run the transfer from the genesis account to our test faucet account. - -```sh -node ./server/dist/transfer.js \ - --host-url http://localhost:8401 \ - --transfer-contract-path contracts/transfer_to_account_u512.wasm \ - --payment-contract-path contracts/standard_payment.wasm \ - --payment-amount 100000 \ - --gas-price 10 \ - --from-private-key-path ../hack/docker/keys/faucet-account/account-private.pem \ - --from-public-key-path ../hack/docker/keys/faucet-account/account-public.pem \ - --to-public-key-path ./server/test.public.key \ - --amount 10000000 -``` - -NOTE: If you are connecting to a HTTPS endpoint which uses a self-signed certificate, which is the case in local testing, you have to relax the SSL certificate checks in Node.js like so: - -```console -$ export NODE_TLS_REJECT_UNAUTHORIZED=0 -$ node ./server/dist/transfer.js \ - --host-url https://localhost:8443 \ - ... -``` - -If successful, it should print something like this: - -```console -Transfering tokens to account 045499d51a013e06c6cbb5734843cf3c7f08d66af312d81238ffeb54244f1800 -Deploying 7401ecbe8b2c4e4de2c1e6422fddcfd1ae9d128058e2e6dba97ba62fc51db734 to http://localhost:8401 -Done. -``` - -You can also confirm it in the node's logs in `hack/docker`: - -```console -$ docker logs --tail 1 node-0 -18:13:45.264 [grpc-default-executor-2] INFO i.c.casper.MultiParentCasperImpl - Received Deploy 7401ecbe8b2c4e4de2c1e6422fddcfd1ae9d128058e2e6dba97ba62fc51db734 (f78786150599b50a1353476f5e2f12cd13c214e512096741c48e7ec63639af56 / 1) -``` - -The auto-propose feature is by default not enabled in the `hack/docker` setup, so you have to manually trigger proposal. - -```console -$ ./client.sh node-0 propose -Response: Success! Block d1d95074f4... created and added. -``` - -Following this we can check the status of our deploy. The result of the processing and the fault tolerance can be found in the output: - -```console -$ ./client.sh node-0 show-deploy 7401ecbe8b2c4e4de2c1e6422fddcfd1ae9d128058e2e6dba97ba62fc51db734 - -deploy { - deploy_hash: "7401ecbe8b2c4e4de2c1e6422fddcfd1ae9d128058e2e6dba97ba62fc51db734" - header { - account_public_key: "f78786150599b50a1353476f5e2f12cd13c214e512096741c48e7ec63639af56" - timestamp: 1562350425051 - gas_price: 0 - body_hash: "1ba3a8335e68cbfd461865876bccc9225c560db9045d862ad16d26e3bbbe0a87" - } - ... -} -processing_results { - block_info { - summary { - block_hash: "d1d95074f4779c8de1ed851f22f3bb6c71089359b443b46f747606b1fbc0c974" - ... - } - status { - fault_tolerance: -1.0 - stats { - block_size_bytes: 957577 - deploy_error_count: 0 - } - } - } - cost: 20803 - is_error: false - error_message: "" -} -``` - -If the deploy was successfully executed we can check the balance of the account we wished to give the tokens to: - -```console -$ ./client.sh node-0 query-state \ - -t address \ - -k 045499d51a013e06c6cbb5734843cf3c7f08d66af312d81238ffeb54244f1800 \ - -p "/" \ - -b d1d95074f4779c8de1ed851f22f3bb6c71089359b443b46f747606b1fbc0c974 - -account { - public_key: "045499d51a013e06c6cbb5734843cf3c7f08d66af312d81238ffeb54244f1800" - main_purse { - uref: "e698d314cd8004ca6cdfc5b5ea94b8c930aa3cd108bb32d6a5e9f53bcc201f75" - access_rights: READ_ADD_WRITE - } - known_urefs { - name: "URef(9251312d6c8a3d702a0b7e7754074dd920e5d2af1cf25e75fd2225ba845326ef, READ_ADD_WRITE)" - key { - uref { - uref: "9251312d6c8a3d702a0b7e7754074dd920e5d2af1cf25e75fd2225ba845326ef" - access_rights: READ_ADD_WRITE - } - } - } - known_urefs { - name: "URef(c1a7e37cadf6c5d11cec42a3ffb8a6c9d4a6b9e1da56a88c50b33ffd130ad043, READ_ADD_WRITE)" - key { - uref { - uref: "c1a7e37cadf6c5d11cec42a3ffb8a6c9d4a6b9e1da56a88c50b33ffd130ad043" - access_rights: READ_ADD_WRITE - } - } - } - known_urefs { - name: "mint" - key { - uref { - uref: "c40e2456dae1fa259a134fe1baba639066125b53515c888fe3086cc46e9e40b2" - access_rights: READ_ADD_WRITE - } - } - } - known_urefs { - name: "pos" - key { - uref { - uref: "5fcf27e682ed6856c575200740238f24247d178f3703539169631c11ba6fcc7c" - access_rights: READ_ADD_WRITE - } - } - } - associated_keys { - public_key: "045499d51a013e06c6cbb5734843cf3c7f08d66af312d81238ffeb54244f1800" - weight: 1 - } - action_thresholds { - deployment_threshold: 1 - key_management_threshold: 1 - } -} -``` - -Based on the `main_purse` we can issue a followup request to check the balance: - -```console -$ ./client.sh node-0 query-state \ - -t uref \ - -k e698d314cd8004ca6cdfc5b5ea94b8c930aa3cd108bb32d6a5e9f53bcc201f75 \ - -p "/" \ - -b d1d95074f4779c8de1ed851f22f3bb6c71089359b443b46f747606b1fbc0c974 - -unit { -} -``` - -Alas, that's not the balance. We'll have to figure out how to get there, apparently there's an indirection from the purse to a local address we can't easily see. - -### Create an account - -You can access the explorer at https://localhost:8443 to create accounts and ask the faucet for tokens. +You can use `yarn test` in the project root directory to run the unit tests. diff --git a/packages/server/.env b/packages/server/.env index f2fe87a1..43a3170a 100644 --- a/packages/server/.env +++ b/packages/server/.env @@ -6,10 +6,6 @@ SERVER_USE_TLS=false #SERVER_TLS_CERT_PATH= #SERVER_TLS_KEY_PATH= -# Location of the faucet contract during development. Produce them with the following command: -# make build-explorer-contracts -FAUCET_CONTRACT_PATH=../../contracts/faucet_stored.wasm - # Got to send some payment for the faucet deploys PAYMENT_AMOUNT=1000000000000 @@ -27,16 +23,24 @@ FAUCET_ACCOUNT_PUBLIC_KEY_PATH=./test.public.key STATIC_ROOT=../../ui/build # In production leave this empty to make the UI connect to the nginx reverse proxy. -# In testing we can point to grpcwebproxy (started in `hack/docker`) which is configured to allow CORS. +# In testing we can point to http://localhost:8000/rpc, to use the reverse proxy provided by Express. UI_GRPC_URL=http://localhost:8000/rpc # Set this when in offline mode so the UI can be used with the mock account. # It's passed via config.js so I don't accidentally leave it on by commiting. AUTH_MOCK_ENABLED=false +# Network name, show in top of Clarity website. NETWORK_NAME=LocalDev + +# The location of casper-node that provides the rpc service. JSON_RPC_URL=http://localhost:40101/rpc -# In production, we would use nginx as proxy + +# In production,set it to false and we would use nginx as proxy JSON_RPC_PROXY=true + +# Which chain the deploy is supposed to be run on. CHAIN_NAME=lrt2net + +# The location of event-store, which serve the `EventStoreService` REACT_APP_EVENT_STORE_URL=http://localhost:3000 diff --git a/packages/server/README.md b/packages/server/README.md index 992c3904..f94f65c5 100644 --- a/packages/server/README.md +++ b/packages/server/README.md @@ -7,23 +7,7 @@ This is the server side component for the CasperLabs Explorer: ## Available scripts -NOTE: When the server is run in development mode it uses environment variables from `.env` and saves the nonce to `nonce.txt`. Whenever the docker network is torn down and brougth back up, the `nonce.txt` should be manually deleted and the server restarted to bring the faucet back to initial state as well. - -### `npm run dev` - -Rebuilds and restarts the server whenever there's a source code change. - -### `npm run start:both` - -Runs the server and the UI in development mode. The UI will proxy to the server on its own port, so you can reach API without CORS issues on port 8000, or directly at port 8001. The client can be started in a separate terminal as well. - -### `npm run build` - -Build artifacts into the `dist` directory. - -### `npm run test` - -Run unit tests, for example to check the contract ABI serialization format. +Since we are using yarn workspace, you don't have to run scripts in separate component directory. ## Environment variables @@ -31,8 +15,12 @@ You can start the server in once console and the UI in another, the UI will prox There are some environment variables that can come handy: -- `UI_GRPC_URL`: By default this is empty, or pointing at the local docker setup, but you can set it to any environment to test the UI locally agains the remote `grpcwebproxy` insteance. +- `UI_GRPC_URL`: By default this is empty, or set it to `http://localhost:8000/rpc` to use reverse proxy provided by Express. - `AUTH_MOCK_ENABLED`: By setting it to `true` you can work offline and be automatically logged in with a test user to create accounts and use the faucet. +- `NETWORK_NAME`: Network name, show in top of Clarity website. +- `JSON_RPC_URL`: The location of casper-node that provides the rpc service. +- `CHAIN_NAME`: Which chain the deploy is supposed to be run on. +- `REACT_APP_EVENT_STORE_URL`: The location of event-store, which serve the `EventStoreService` ## Useful links: @@ -41,7 +29,3 @@ There are some environment variables that can come handy: - https://auth0.com/docs/quickstart/spa/vanillajs/02-calling-an-api - https://developer.okta.com/blog/2018/11/15/node-express-typescript - https://blog.envoyproxy.io/envoy-and-grpc-web-a-fresh-new-alternative-to-rest-6504ce7eb880 - -## Sharing code between UI and server - -I haven't yet figured out how to share the TypeScript code in a way that plays nicely with `dist` (i.e. doesn't create a subdirectory), and gets packaged into the `build` as well. I tried to put everything I wanted to share in `server/src/shared` contents and use a symlink to mirror it in `ui/src/shared`, but the compiler wouldn't have it. I ended up copying just what I needed until I figure out a better way. diff --git a/packages/ui/README.md b/packages/ui/README.md index feb1cda5..5612d8a8 100644 --- a/packages/ui/README.md +++ b/packages/ui/README.md @@ -17,7 +17,7 @@ npx create-react-app ui --typescript In the project directory, you can run: -### `yarn start` +### `yarn run dev` Runs the app in the development mode.
Open [http://localhost:8000](http://localhost:8000) to view it in the browser. You can change the port in the `.env` file or by setting the `PORT` env var. @@ -25,7 +25,7 @@ The page will reload if you make edits.
You will also see any lint errors in If we add `"proxy": "http://localhost:8001/"` to `package.json` we get transparent proxying to the server, but it seems to clash with auto-reloading in Firefox; works in Chrome. -To change the code in the `sdk` and see the effect you can run `npm run ~build` in that directory and reload the page. +To change the code in the `sdk` and see the effect you can run `yarn run build` in that directory and reload the page. ### `yarn test`