Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
801 changes: 801 additions & 0 deletions contracts/Deflate.sol

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions contracts/Zlib.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.23;

library Zlib {
uint8 constant CM_DEFLATE = 8;

function decompress(bytes memory src) external pure {
// bits 0 to 3 CM Compression method
// bits 4 to 7 CINFO Compression info
if ((uint8(src[0]) & 15) != CM_DEFLATE) return;
uint256 window = 1 << (uint256(uint8(src[0]) >> 4) + 8);

// bits 0 to 4 FCHECK (check bits for CMF and FLG)
// bit 5 FDICT (preset dictionary)
// bits 6 to 7 FLEVEL (compression level)
if ((uint8(src[1]) & (1 << 5)) == 0) return; // FDICT unsupported
if ((((uint256(uint8(src[0])) << 8) | (uint8(src[1]))) % 31) > 0)
return; // FCHECK

uint256 level = uint8(src[1]) >> 6; // bits 6 to 7 FLEVEL (compression level)
}
}
Empty file.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@unruggable/gateways",
"version": "1.2.1",
"version": "1.2.2",
"description": "Trustless Ethereum Multichain CCIP-Read Gateway",
"publishConfig": {
"access": "public"
Expand Down
10 changes: 6 additions & 4 deletions scripts/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ if (prefetch) {
Date.now() - t0
);
} catch (err) {
console.log(new Date(), `Prefetch failed: ${flattenErrors(err)}`);
console.error(new Date(), `Prefetch failed: ${flattenErrors(err)}`);
}
setTimeout(fire, gateway.latestCache.cacheMs);
};
Expand Down Expand Up @@ -277,9 +277,11 @@ export default {
);
return Response.json({ data }, { headers });
} catch (err) {
const error = flattenErrors(err);
console.log(new Date(), error);
return Response.json({ error }, { headers, status: 500 });
console.error(new Date(), flattenErrors(err, String));
return Response.json(
{ message: flattenErrors(err) },
{ headers, status: 500 }
);
}
}
default: {
Expand Down
24 changes: 24 additions & 0 deletions src/beacon.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { BigNumberish, HexString, HexString32 } from './types.js';
import { isHexString } from 'ethers/utils';
import { sha256 } from 'ethers/crypto';
import { CachedValue } from './cached.js';

export async function fetchBeaconData(url: string) {
try {
Expand All @@ -14,6 +15,29 @@ export async function fetchBeaconData(url: string) {
}
}

export function beaconConfigCache(beaconAPI: string) {
return Object.assign(
new CachedValue(async () => {
const [genesis, spec] = await Promise.all([
fetchBeaconData(`${beaconAPI}/eth/v1/beacon/genesis`),
fetchBeaconData(`${beaconAPI}/eth/v1/config/spec`),
]);
const temp = {
genesisTime: BigInt(genesis.genesis_time),
secondsPerSlot: BigInt(spec.SECONDS_PER_SLOT),
fetchSidecars(t: bigint) {
return fetchSidecars(
beaconAPI,
(t - this.genesisTime) / this.secondsPerSlot
);
},
};
return temp;
}, Infinity),
{ beaconAPI }
);
}

export type BlobSidecar = {
blob: HexString;
kzg_commitment: HexString;
Expand Down
31 changes: 29 additions & 2 deletions src/eth/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,34 @@ export type RPCEthGetProof = {

export type EthAccountProof = Omit<RPCEthGetProof, 'storageProof'>;

export type RPCEthGetBlock<TransactionT = HexString> = {
export type RPCEthTransaction = {
type: HexString;
chainId: HexString;
nonce: HexString;
gasPrice: HexString;
to: HexAddress;
from: HexAddress;
gas: HexString;
value: HexString;
input: HexString;
r: HexString32;
s: HexString32;
v: HexString;
hash: HexString32;
blockHash: HexString32;
blockNumber: HexString;
transactionIndex: HexString;
};

export function isEIP4844(tx: RPCEthTransaction): tx is RPCEthTransaction4844 {
return tx.type === '0x3';
}

export type RPCEthTransaction4844 = RPCEthTransaction & {
blobVersionedHashes: HexString32[];
};

export type RPCEthGetBlock<tx extends boolean = false> = {
hash: HexString32;
stateRoot: HexString32;
parentHash: HexString32;
Expand All @@ -43,7 +70,7 @@ export type RPCEthGetBlock<TransactionT = HexString> = {
extraData: HexString;
mixHash: HexString32; // prev_randao
nonce: HexString;
transactions: TransactionT[];
transactions: (tx extends true ? RPCEthTransaction : HexString32)[];
timestamp: HexString;
uncles: HexString[];
// optional
Expand Down
Loading
Loading