Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Geo blocking with 451 #382

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

Conversation

gbarkhatov
Copy link
Contributor

Copy link
Member

@vitsalis vitsalis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to have a separation between the internal representation of the API being available and the fact that we are using the health check endpoint to accomplish that.

src/app/api/apiWrapper.ts Outdated Show resolved Hide resolved
(error.response?.status === 451 || error.request.status === 451)
) {
return {
status: "geoBlocked",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
status: "geoBlocked",
status: "geoblocked",

Weird for a status to have camel case

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

common syntax for such constant usually using upper case letter with _
Is this something we can agree on as a team and adopt for all future changes?
for example, axios use this pattern https://www.npmjs.com/package/axios#error-type

@@ -35,6 +40,26 @@ export const ConnectSmall: React.FC<ConnectSmallProps> = ({

const { coinName, networkName } = getNetworkConfig();

const renderHealthCheckTooltip = () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This tooltip is not about healthcheck, it's about showing whether someone is geo-blocked or not.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is health check indeeed, if we get anything besides the normal response we will render an error text OR geo blocking text

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

<span
className="cursor-pointer text-xs"
data-tooltip-id={`tooltip-connect-${healthCheck.status}`}
data-tooltip-content={healthCheck.message}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we getting the content for a tooltip directly out of the error returned from an API response? The API response should not specify the content, the API response should specify what went wrong, and based on what went wrong we construct the content in the content component.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -0,0 +1,4 @@
export type HealthCheckResult =
| { status: "normal"; message: string }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

normal, geoblocked and error should be constants.

Otherwise, we could also have an enum, but I understand that's difficult in js.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

messages={[
"The Bitcoin Staking functionality is not available at the moment.",
]}
icon={healthCheckFailed}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This icon has nothing to do with health check. Health check is the endpoint that we use to check whether the API is available. The icon name should be something along the lines of apiNotAvailable

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -83,6 +87,7 @@ export const Staking: React.FC<StakingProps> = ({
setDelegationsLocalStorage,
btcWalletBalanceSat,
availableUTXOs,
healthCheck,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
healthCheck,
apiAvailable,

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vitsalis you want it only in Staking? Or do you want me to change the healthCheck completely to apiAvailable?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@gbarkhatov gbarkhatov requested a review from vitsalis July 26, 2024 09:20
Copy link
Member

@vitsalis vitsalis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, I would like an extra layer between healthcheck and the rest of the application, but this might be overengineering, so lgtm.

@@ -17,13 +25,15 @@ interface ConnectSmallProps {
address: string;
btcWalletBalanceSat?: number;
onDisconnect: () => void;
apiAvailable?: HealthCheckResult;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this ??

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is undefined before we fetch and get into one of the normal - geoblocking - error state

@@ -35,6 +45,31 @@ export const ConnectSmall: React.FC<ConnectSmallProps> = ({

const { coinName, networkName } = getNetworkConfig();

const renderApiAvailableTooltip = () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const renderApiAvailableTooltip = () => {
const renderApiNotAvailableTooltip = () => {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's also have a comment describing what this function's purpose is.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

<button
className="btn-primary btn h-[2.5rem] min-h-[2.5rem] rounded-full px-2 text-white md:rounded-lg"
onClick={onConnect}
disabled={!!address || !isApiNormal}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a comment denoting that we disable the button if the API is not healthy.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@gbarkhatov
Copy link
Contributor Author

@jrwbabylonchain @jeremy-babylonchain waiting for the 2nd pair of eyes

@jrwbabylonlab
Copy link
Collaborator

jrwbabylonlab commented Jul 29, 2024

Dumping my thoughts about the PR.
Types:
I generally think it's best to take a hybrid approach on managing the types.
We shall:

  • Put widely shared types under types
  • Put domain specific/package specific under the same location as the actual implementation.

For the health check case, I feel this is quite confusing using the healthcheck result type outside of the api client. why would react component need to have knowledge of "backend healthcheck"?

The more i think about it, the more i think it's due to the fact that our current getHealthCheck is actually mixing up client layer and service layer. Let's breakdown in details what those two layers do:

  1. Client layer: No business logic, it just setup axios and http client configurations (connection pool, timeout, analytics etc) to making the call. The healthcheck types only lives here.
  2. Service layer: All our business logic and custom types sit here. This layer will call the healtcheck client method, then turn it into GeoBlock related terms, objects and types.

To summarise, I think we shall take this healthcheck as a good opportunity to standandarise the layers and avoid involving API specific types across the repo. i.e this shall not appear https://github.com/babylonchain/simple-staking/pull/382/files#diff-8be97df2c2968a8d06ade0ed70c78ddde9ff115505d6335ecff9119d47b47dbfR28 it shall be replaced with more domain specific things such as IsGeoBlocked etc.

Dependency

We are injecting the apiAvailable in many different places. What we actually need is a boolean value to indicate whether it is geoBlocked. Why not consider putting it high put in the react chain (useContext) so that we have less props to manage? This will make code cleaner and easier to write tests.

@gbarkhatov
Copy link
Contributor Author

@jrwbabylonlab

  1. I will check whether or not I will apply it as a more domain specific naming and placement, separation of concerns. But the same approach with "good opportunity to start making better stuff" might be applied to new additions to 700 lines of code Staking.tsx
  2. apiAvailable is used in 2 places - Header and Staking. I would not put any state to global (whether it's context or redux or whatever) unless I have to. Looking at UI side, I believe at some point of time we will have only one place to connect the wallet (not we have UI connect duplication), so as later on when we refactor and improve I think we might cut the amount of context providers. I'll move this PR to labs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants