Skip to content

Commit

Permalink
Merge pull request #6 from gnosis/johanna/gw-1040-fix-ipfs-timeouts
Browse files Browse the repository at this point in the history
feat: gw-1040 fix ipfs timeouts
  • Loading branch information
johannafransn authored Nov 22, 2024
2 parents 74e0d38 + b4bebf4 commit d4cc804
Show file tree
Hide file tree
Showing 6 changed files with 220 additions and 143 deletions.
Binary file modified .cache/cache.db
Binary file not shown.
207 changes: 104 additions & 103 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,112 +6,113 @@ field_selection:
- hash
- transactionIndex
contracts:
- name: Hub
handler: src/event_handlers/hubV1.ts
events:
- event: HubTransfer(address indexed from, address indexed to, uint256 amount)
- event: OrganizationSignup(address indexed organization)
- event: Signup(address indexed user, address token)
- event: Trust(address indexed canSendTo, address indexed user, uint256 limit)
- name: PersonalCRC
abi_file_path: abis/PersonalCRC.json
handler: src/event_handlers/hubV1.ts
events:
- event: Transfer(address indexed from, address indexed to, uint256 amount)
- name: HubV2
handler: src/event_handlers/hubV2.ts
events:
- event: RegisterHuman(address indexed avatar, address indexed inviter)
- event: RegisterOrganization(address indexed organization, string name)
- event: RegisterGroup(address indexed group, address indexed mint, address indexed treasury, string name, string symbol)
- event: Trust(address indexed truster, address indexed trustee, uint256 expiryTime)
- event: Stopped(address indexed avatar)
- event: StreamCompleted(address indexed operator, address indexed from, address indexed to, uint256[] ids, uint256[] amounts)
- event: PersonalMint(address indexed human, uint256 amount, uint256 period, uint256 endPeriod)
- event: TransferSingle(address indexed operator, address indexed from, address indexed to, uint256 id, uint256 value)
- event: TransferBatch(address indexed operator, address indexed from, address indexed to, uint256[] ids, uint256[] values)
- event: DiscountCost(address indexed account, uint256 indexed id, uint256 discountCost)
- name: ERC20Lift
handler: src/event_handlers/hubV2.ts
events:
- event: ProxyCreation(address proxy, address masterCopy)
- event: ERC20WrapperDeployed(address indexed avatar, address indexed erc20Wrapper, uint8 circlesType)
- name: StandardTreasury
handler: src/event_handlers/treasury.ts
events:
- event: CreateVault(address indexed group, address indexed vault)
- event: GroupMintSingle(address indexed group, uint256 indexed id, uint256 value, bytes userData)
- event: GroupMintBatch(address indexed group, uint256[] ids, uint256[] values, bytes userData)
- event: GroupRedeem(address indexed group, uint256 indexed id, uint256 value, bytes data)
- event: GroupRedeemCollateralReturn(address indexed group, address indexed to, uint256[] ids, uint256[] values)
- event: GroupRedeemCollateralBurn(address indexed group, uint256[] ids, uint256[] values)
- name: CustomTreasury
handler: src/event_handlers/treasury.ts
events:
- event: CreateVault(address indexed group, address indexed vault)
- event: GroupMintSingle(address indexed group, uint256 indexed id, uint256 value, bytes userData)
- event: GroupMintBatch(address indexed group, uint256[] ids, uint256[] values, bytes userData)
- event: GroupRedeem(address indexed group, uint256 indexed id, uint256 value, bytes data)
- event: GroupRedeemCollateralReturn(address indexed group, address indexed to, uint256[] ids, uint256[] values)
- event: GroupRedeemCollateralBurn(address indexed group, uint256[] ids, uint256[] values)
- name: NameRegistry
handler: src/event_handlers/hubV2.ts
events:
- event: UpdateMetadataDigest(address indexed avatar, bytes32 metadataDigest)
- name: WrappedERC20
handler: src/event_handlers/wrappedERC20.ts
events:
- event: Transfer(address indexed from, address indexed to, uint256 value)
- event: DepositDemurraged(address indexed account, uint256 amount, uint256 inflationaryAmount)
- event: WithdrawDemurraged(address indexed account, uint256 amount, uint256 inflationaryAmount)
- event: DepositInflationary(address indexed account, uint256 amount, uint256 demurragedAmount)
- event: WithdrawInflationary(address indexed account, uint256 amount, uint256 demurragedAmount)
- name: SafeAccount
handler: src/event_handlers/hubV2.ts
events:
- event: ExecutionSuccess(bytes32 txHash, uint256 payment)

networks:
# - id: 10200
# start_block: 2211707
# contracts:
# - name: Hub
# address:
# - 0xdbF22D4e8962Db3b2F1d9Ff55be728A887e47710
# - name: PersonalCRC
# - name: HubV2
# address:
# - 0xb80feeDfEce647dDc709777D5094fACD157BA001
# - name: ERC20Lift
# address:
# - 0xBD2D6Fbb6A702B04B750Bc9942fBaAE81187355E
# - name: StandardTreasury
# address:
# - 0xC06ADED7950429FdF2023e139B076f6BaFf9Fe1C
# - name: NameRegistry
# address:
# - 0x24b3fDCdD9fef844fB3094ef43c0A6Ac23a6dF9E
# - name: WrappedERC20
- id: 100
start_block: 12529458
# start_block: 36486013
contracts:
- name: Hub
handler: src/event_handlers/hubV1.ts
events:
- event: HubTransfer(address indexed from, address indexed to, uint256 amount)
- event: OrganizationSignup(address indexed organization)
- event: Signup(address indexed user, address token)
- event: Trust(address indexed canSendTo, address indexed user, uint256 limit)
- name: PersonalCRC
abi_file_path: abis/PersonalCRC.json
handler: src/event_handlers/hubV1.ts
events:
- event: Transfer(address indexed from, address indexed to, uint256 amount)
- name: HubV2
address:
- 0xc12C1E50ABB450d6205Ea2C3Fa861b3B834d13e8
handler: src/event_handlers/hubV2.ts
events:
- event: RegisterHuman(address indexed avatar, address indexed inviter)
- event: RegisterOrganization(address indexed organization, string name)
- event: RegisterGroup(address indexed group, address indexed mint, address indexed treasury, string name, string symbol)
- event: Trust(address indexed truster, address indexed trustee, uint256 expiryTime)
- event: Stopped(address indexed avatar)
- event: StreamCompleted(address indexed operator, address indexed from, address indexed to, uint256[] ids, uint256[] amounts)
- event: PersonalMint(address indexed human, uint256 amount, uint256 period, uint256 endPeriod)
- event: TransferSingle(address indexed operator, address indexed from, address indexed to, uint256 id, uint256 value)
- event: TransferBatch(address indexed operator, address indexed from, address indexed to, uint256[] ids, uint256[] values)
- event: DiscountCost(address indexed account, uint256 indexed id, uint256 discountCost)
- name: ERC20Lift
address:
- 0x5F99a795dD2743C36D63511f0D4bc667e6d3cDB5
handler: src/event_handlers/hubV2.ts
events:
- event: ProxyCreation(address proxy, address masterCopy)
- event: ERC20WrapperDeployed(address indexed avatar, address indexed erc20Wrapper, uint8 circlesType)
- name: StandardTreasury
address:
- 0x08F90aB73A515308f03A718257ff9887ED330C6e
handler: src/event_handlers/treasury.ts
events:
- event: CreateVault(address indexed group, address indexed vault)
- event: GroupMintSingle(address indexed group, uint256 indexed id, uint256 value, bytes userData)
- event: GroupMintBatch(address indexed group, uint256[] ids, uint256[] values, bytes userData)
- event: GroupRedeem(address indexed group, uint256 indexed id, uint256 value, bytes data)
- event: GroupRedeemCollateralReturn(address indexed group, address indexed to, uint256[] ids, uint256[] values)
- event: GroupRedeemCollateralBurn(address indexed group, uint256[] ids, uint256[] values)
- name: CustomTreasury
handler: src/event_handlers/treasury.ts
events:
- event: CreateVault(address indexed group, address indexed vault)
- event: GroupMintSingle(address indexed group, uint256 indexed id, uint256 value, bytes userData)
- event: GroupMintBatch(address indexed group, uint256[] ids, uint256[] values, bytes userData)
- event: GroupRedeem(address indexed group, uint256 indexed id, uint256 value, bytes data)
- event: GroupRedeemCollateralReturn(address indexed group, address indexed to, uint256[] ids, uint256[] values)
- event: GroupRedeemCollateralBurn(address indexed group, uint256[] ids, uint256[] values)
- name: NameRegistry
address:
- 0xA27566fD89162cC3D40Cb59c87AAaA49B85F3474
handler: src/event_handlers/hubV2.ts
events:
- event: UpdateMetadataDigest(address indexed avatar, bytes32 metadataDigest)
- name: WrappedERC20
handler: src/event_handlers/wrappedERC20.ts
events:
- event: Transfer(address indexed from, address indexed to, uint256 value)
- event: DepositDemurraged(address indexed account, uint256 amount, uint256 inflationaryAmount)
- event: WithdrawDemurraged(address indexed account, uint256 amount, uint256 inflationaryAmount)
- event: DepositInflationary(address indexed account, uint256 amount, uint256 demurragedAmount)
- event: WithdrawInflationary(address indexed account, uint256 amount, uint256 demurragedAmount)
- name: SafeAccount
- name: CustomTreasury
- name: Hub
address:
- 0x29b9a7fBb8995b2423a71cC17cf9810798F6C543
- name: PersonalCRC
handler: src/event_handlers/hubV2.ts
events:
- event: ExecutionSuccess(bytes32 txHash, uint256 payment)

networks:
# - id: 10200
# start_block: 2211707
# contracts:
# - name: Hub
# address:
# - 0xdbF22D4e8962Db3b2F1d9Ff55be728A887e47710
# - name: PersonalCRC
# - name: HubV2
# address:
# - 0xb80feeDfEce647dDc709777D5094fACD157BA001
# - name: ERC20Lift
# address:
# - 0xBD2D6Fbb6A702B04B750Bc9942fBaAE81187355E
# - name: StandardTreasury
# address:
# - 0xC06ADED7950429FdF2023e139B076f6BaFf9Fe1C
# - name: NameRegistry
# address:
# - 0x24b3fDCdD9fef844fB3094ef43c0A6Ac23a6dF9E
# - name: WrappedERC20
- id: 100
#start_block: 12529458
# start_block: 36486013
start_block: 37148256
contracts:
- name: HubV2
address:
- 0xc12C1E50ABB450d6205Ea2C3Fa861b3B834d13e8
- name: ERC20Lift
address:
- 0x5F99a795dD2743C36D63511f0D4bc667e6d3cDB5
- name: StandardTreasury
address:
- 0x08F90aB73A515308f03A718257ff9887ED330C6e
- name: NameRegistry
address:
- 0xA27566fD89162cC3D40Cb59c87AAaA49B85F3474
- name: WrappedERC20
- name: SafeAccount
- name: CustomTreasury
- name: Hub
address:
- 0x29b9a7fBb8995b2423a71cC17cf9810798F6C543
- name: PersonalCRC
4 changes: 2 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 5 additions & 6 deletions src/event_handlers/hubV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@ import {
} from "generated";
import { toBytes, bytesToBigInt, parseEther, zeroAddress } from "viem";
import { incrementStats } from "../incrementStats";
import {
defaultAvatarProps,
getProfileMetadataFromIpfs,
makeAvatarBalanceEntityId,
} from "../utils";
import { defaultAvatarProps, makeAvatarBalanceEntityId } from "../utils";
import { Profile } from "../types";
import { handleTransfer } from "../common/handleTransfer";
import { getProfileMetadataFromIpfs } from "../ipfs";

// ###############
// #### TOKEN ####
Expand Down Expand Up @@ -204,7 +201,9 @@ NameRegistry.UpdateMetadataDigest.handler(async ({ event, context }) => {
profileMetadata = await getProfileMetadataFromIpfs(
event.params.metadataDigest
);
} catch (_) {}
} catch (_) {
console.log("Error in nameReg fetching Ipfs", _);
}

const avatar = await context.Avatar.get(event.params.avatar);

Expand Down
107 changes: 107 additions & 0 deletions src/ipfs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import multihash from "multihashes";
import { Profile } from "./types";
import { ProfileCache } from "./cache";
import { Avatar, eventLog } from "generated";
import { uint8ArrayToCidV0 } from "./utils";

// Simple config with only needed values
const config = {
maxImageSizeKB: 150,
imageDimension: 256,
};

const IPFS_ENDPOINTS = [
"https://rpc.aboutcircles.com/profiles/get",

/* "https://cloudflare-ipfs.com/ipfs",
"https://ipfs.io/ipfs",
"https://gateway.pinata.cloud/ipfs", */
];

async function validateImage(imageUrl: string): Promise<boolean> {
try {
const response = await fetch(imageUrl);
const buffer = await response.arrayBuffer();

// Check file size
if (buffer.byteLength > config.maxImageSizeKB * 1024) {
console.error("Image size exceeds limit");
return false;
} else return true;
} catch (error) {
console.error("Failed to validate image:", error);
return false;
}
}

async function fetchFromEndpoint(
endpoint: string,
cidV0: string
): Promise<{ data: Profile | null; timeTaken: number }> {
const startTime = performance.now();
try {
const url = endpoint.includes("aboutcircles.com")
? `${endpoint}?cid=${cidV0}`
: `${endpoint}/${cidV0}`;

const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = (await response.json()) as Profile | undefined;
const timeTaken = performance.now() - startTime;

// If there's an avatar URL, validate it

if (data?.previewImageUrl) {
const isValidImage = await validateImage(data.previewImageUrl);
if (!isValidImage) {
console.warn(`Invalid avatar image in profile from ${endpoint}`);
data.previewImageUrl = undefined;
}
}

return { data: data as Profile, timeTaken };
} catch (e) {
const timeTaken = performance.now() - startTime;
console.warn(
`Failed to fetch from ${endpoint} (${timeTaken.toFixed(2)}ms):`,
e
);
return { data: null, timeTaken };
}
}

export async function getProfileMetadataFromIpfs(
metadataDigest: string
): Promise<{ cidV0: string; data: Profile | null } | null> {
if (!metadataDigest) {
return null;
}
const slicedDigest = metadataDigest.slice(2, metadataDigest.length);

const cache = await ProfileCache.init();
const cacheResult = await cache.read(slicedDigest);

if (cacheResult) {
return cacheResult;
}

const cidV0 = uint8ArrayToCidV0(
Uint8Array.from(Buffer.from(slicedDigest, "hex"))
);

// Try each endpoint until we get a successful response
for (const endpoint of IPFS_ENDPOINTS) {
const { data, timeTaken } = await fetchFromEndpoint(endpoint, cidV0);
console.log(`IPFS fetch from ${endpoint} took ${timeTaken.toFixed(2)}ms`);
if (data) {
console.log("Adding IPFS data to cache", data);
await cache.add(slicedDigest, cidV0, data);
return { cidV0, data };
}
}

console.error("Failed to fetch from all IPFS endpoints");
return { cidV0, data: null };
}
Loading

0 comments on commit d4cc804

Please sign in to comment.