Skip to content

Commit

Permalink
Check for account aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
DOBEN committed Jan 4, 2024
1 parent 9cc24ad commit b55c8db
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 29 deletions.
13 changes: 6 additions & 7 deletions sponsoredTransactionsAuction/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
ARG node_base_image=node:18-slim
ARG rust_base_image=rust:1.65
ARG rust_base_image=rust:1.67

FROM ${node_base_image} AS frontend
FROM ${node_base_image} AS front_end

WORKDIR /app
COPY ./sponsoredTransactionsAuction/frontend/package.json ./package.json
COPY ./sponsoredTransactionsAuction/frontend/tsconfig.node.json ./tsconfig.node.json
COPY ./sponsoredTransactionsAuction/frontend/tsconfig.json ./tsconfig.json
COPY ./sponsoredTransactionsAuction/frontend/vite.config.ts ./vite.config.ts
COPY ./sponsoredTransactionsAuction/frontend/public ./public
COPY ./sponsoredTransactionsAuction/frontend/src ./src
COPY ./sponsoredTransactionsAuction/frontend/index.html ./index.html
COPY ./sponsoredTransactionsAuction/frontend/generated ./generated

RUN yarn
RUN yarn build

FROM ${rust_base_image} AS backend
FROM ${rust_base_image} AS back_end

WORKDIR /backend/app
COPY ./deps/concordium-rust-sdk /deps/concordium-rust-sdk
Expand All @@ -27,7 +26,7 @@ RUN cargo build --release
FROM ubuntu:22.04
WORKDIR /build

COPY --from=backend ./backend/app/target/release/sponsored-transaction-backend ./sponsored-transaction-backend
COPY --from=frontend ./app/dist ./public
COPY --from=back_end ./backend/app/target/release/sponsored-transaction-backend ./sponsored-transaction-backend
COPY --from=front_end ./app/dist ./public

CMD ./sponsored-transaction-backend --public-folder public
CMD ./sponsored-transaction-backend --frontend public
2 changes: 1 addition & 1 deletion sponsoredTransactionsAuction/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ docker run -p 8080:8080 --mount type=bind,source="$(pwd)"/<ACCOUNT_KEY_FILE>,tar
e.g.

```shell
docker run -p 8080:8080 --mount type=bind,source="$(pwd)"/4SizPU2ipqQQza9Xa6fUkQBCDjyd1vTNUNDGbBeiRGpaJQc6qX.export,target=/KEY,readonly -e ACCOUNT_KEY_FILE=/KEY -e PORT=8080 -e NODE=http://node.testnet.concordium.com:20000 -e LOG_LEVEL=info -e CIS2_TOKEN_CONTRACT_INDEX=7370 -e AUCTION_CONTRACT_INDEX=7415 sponsored_transactions
docker run -p 8080:8080 --mount type=bind,source="$(pwd)"/4SizPU2ipqQQza9Xa6fUkQBCDjyd1vTNUNDGbBeiRGpaJQc6qX.export,target=/KEY,readonly -e ACCOUNT_KEY_FILE=/KEY -e PORT=8080 -e NODE=http://node.testnet.concordium.com:20000 -e LOG_LEVEL=debug -e CIS2_TOKEN_CONTRACT_INDEX=7370 -e AUCTION_CONTRACT_INDEX=7415 sponsored_transactions
```

Note: To get your `ACCOUNT_KEY_FILE` (the `4SizPU2ipqQQza9Xa6fUkQBCDjyd1vTNUNDGbBeiRGpaJQc6qX.export` file), export it from the Concordium Browser Wallet for Web.
Expand Down
15 changes: 8 additions & 7 deletions sponsoredTransactionsAuction/backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,28 @@ This page describes the sponsored transactions backend for this dapp example.

The following parameters are supported
- `node` the URL of the node's GRPC V2 interface, e.g., `http://node.testnet.concordium.com:20000`
- `port` the port on which the server will listen for incoming requests
- `log-level` maximum log level (defaults to `debug` if not given)
- `public-folder` the path to the folder, which should be served, defaults to the `public` folder in the current directory.
- `port` the port on which the server will listen for incoming requests, e.g., 127.0.0.1:8080.
- `log-level` maximum log level (defaults to `debug` if not given).
- `frontend` the path to the folder, which should be served, defaults to the `public` folder in the current directory.
- `account-key-file` the path to a file which contains the key credentials.
- `cis2-token-smart-contract-index` the smart contract index which the sponsored transaction is submitted to.
- `auction-smart-contract-index` the smart contract index of the auction smart contract.
- `request-timeout` the request timeout (both of request to the node and server requests) in milliseconds. The node timeout is 500 ms less than the request-timeout to make sure we can fail properly in the server in case of connection timeout due to node connectivity problems.

All of the above is available by using `--help` to get usage information.

An example to run the backend with basic settings and testnet node would be:
```shell
cargo run -- --node http://node.testnet.concordium.com:20000 --account-key-file <YourAccountPathToYourKeys> --cis2-token-smart-contract-index 7370 --auction-smart-contract-index 7415
cargo run -- --node http://node.testnet.concordium.com:20000 --account-key-file <YourAccountPathToYourKeys> --cis2-token-smart-contract-index 7370 --auction-smart-contract-index 7415 --log-level debug
```

An example to run the backend with some filled in example settings would be:

```shell
cargo run -- --node http://node.testnet.concordium.com:20000 --cis2-token-smart-contract-index 7370 --auction-smart-contract-index 7415 --account-key-file ./4SizPU2ipqQQza9Xa6fUkQBCDjyd1vTNUNDGbBeiRGpaJQc6qX.export --log-level debug
cargo run -- --node http://node.testnet.concordium.com:20000 --account-key-file ./4SizPU2ipqQQza9Xa6fUkQBCDjyd1vTNUNDGbBeiRGpaJQc6qX.export --cis2-token-smart-contract-index 7370 --auction-smart-contract-index 7415 --log-level debug
```

To get your account file (the `3PXwJYYPf6fyVb4GJquxSZU8puxrHfzc4XogdMVot8MUQK53tW.export` file in the above example), export it from the Concordium Browser wallet for web.
To get your account file (the `4SizPU2ipqQQza9Xa6fUkQBCDjyd1vTNUNDGbBeiRGpaJQc6qX.export` file in the above example), export it from the Concordium Browser wallet for web.

<img src="./pic/pic1.png" width="200" />
<img src="./pic/pic2.png" width="200" />
Expand All @@ -46,7 +47,7 @@ The smart contract code at {index: CIS2_TOKEN_CONTRACT_INDEX, subindex: 0} can b
The smart contract code at {index: AUCTION_CONTRACT_INDEX, subindex: 0} can be found [here](https://github.com/Concordium/concordium-rust-smart-contracts/tree/main/examples/sponsored-tx-enabled-auction).

See [src/main.rs](./src/main.rs) for the formats of requests and responses. Both
requests and responses are JSON encoded. The requests are handled by handlers in [src/handlers.rs](./src/handlers.rs).
requests and responses are JSON encoded.

# Contributing

Expand Down
12 changes: 9 additions & 3 deletions sponsoredTransactionsAuction/backend/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ struct App {
#[clap(
long = "request-timeout",
help = "Request timeout (both of request to the node and server requests) in milliseconds.",
default_value = "5000",
default_value = "10000",
env = "REQUEST_TIMEOUT"
)]
request_timeout: u64,
Expand Down Expand Up @@ -183,7 +183,6 @@ async fn main() -> anyhow::Result<()> {
// Render index.html
let index_template = fs::read_to_string(app.frontend_assets.join("index.html"))
.context("Frontend was not built or wrong path to the frontend files.")?;

tracing::info!("Starting server...");
let serve_dir_service = ServeDir::new(app.frontend_assets.join("assets"));
let router = Router::new()
Expand Down Expand Up @@ -365,7 +364,14 @@ async fn handle_signature_bid(

let mut rate_limits = state.rate_limits.lock().await;

let limit = rate_limits.entry(request.signer).or_insert(0u8);
// Account addresses on Concordium have account aliases. We track the
// rate-limits by using the alias 0 for every account. https://developer.concordium.software/en/mainnet/net/references/transactions.html#account-aliases
let alias_account_0 = request
.signer
.get_alias(0)
.ok_or_else(|| ServerError::NoAliasAccount)?;

let limit = rate_limits.entry(alias_account_0).or_insert(0u8);

if *limit >= RATE_LIMIT_PER_ACCOUNT {
tracing::warn!("Rate limit for account {} reached.", request.signer);
Expand Down
2 changes: 2 additions & 0 deletions sponsoredTransactionsAuction/backend/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ pub enum ServerError {
RateLimitError,
#[error("Unable to submit transaction on chain successfully: {0}")]
SubmitSponsoredTransactionError(#[from] RPCError),
#[error("Unable to derive alias account of signer.")]
NoAliasAccount,
}

impl axum::response::IntoResponse for ServerError {
Expand Down
6 changes: 5 additions & 1 deletion sponsoredTransactionsAuction/frontend/src/components/Bid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ async function generateTransferMessage(
itemIndexAuction: string,
) {
try {
const viewItemStateParam: AuctionContract.ViewItemStateParameter = itemIndexAuction as unknown as number;
const viewItemStateParam: AuctionContract.ViewItemStateParameter = Number(itemIndexAuction);

if (Number.isNaN(viewItemStateParam)) {
throw new Error(`ItemIndex is NaN.`);
}

const itemState: AuctionContract.ReturnValueViewItemState = await viewItemState(viewItemStateParam);

Expand Down
24 changes: 14 additions & 10 deletions sponsoredTransactionsAuction/frontend/src/components/ViewItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,20 @@ export default function ViewItem(props: ConnectionProps) {

const viewItemStateParam: AuctionContract.ViewItemStateParameter = Number(data.itemIndex);

if (grpcClient) {
viewItemState(viewItemStateParam)
.then((returnValue) => {
if (returnValue !== undefined) {
setItemState(JSONbig.stringify(returnValue));
}
})
.catch((e) => {
setItemStateError((e as Error).message);
});
if (Number.isNaN(viewItemStateParam)) {
setItemStateError('ItemIndex is NaN.');
} else {
if (grpcClient) {
viewItemState(viewItemStateParam)
.then((returnValue) => {
if (returnValue !== undefined) {
setItemState(JSONbig.stringify(returnValue));
}
})
.catch((e) => {
setItemStateError((e as Error).message);
});
}
}
}

Expand Down

0 comments on commit b55c8db

Please sign in to comment.