-
Notifications
You must be signed in to change notification settings - Fork 21
Feature/avail sync #508
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
Open
Yiakman
wants to merge
18
commits into
PaimaStudios:v-next
Choose a base branch
from
Yiakman:feature/avail-sync
base: v-next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Feature/avail sync #508
Changes from 12 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
17e49d6
fixed configs and scripts to run avail in port 9955
Yiakman 87d9ec9
deploy scripts for registering app_id. Added launch of avail light cl…
Yiakman 39b99f7
fixed launch and deploy for avail
Yiakman b9016f9
updated config
Yiakman 1805d77
read avail app info
Yiakman 0f34d5e
Add AvailClient and types for block handling
Yiakman 039c0e5
added genesisHash to avail network schema
Yiakman b3fb1e0
fetcher and state
Yiakman 0acc3d7
added avail to stateMachine
Yiakman a52c6c7
fixed ts lint errors
Yiakman b904d0d
getLatestBlockHeight uses light client status
Yiakman a180608
primitives and processing
Yiakman 7dcc7bd
Add delay configuration to AvailSyncState
Yiakman 4c08a9c
added block log and removed sleep in the fetcher
Yiakman 4ab0e1f
final fixes
Yiakman 3ca428b
Merge branch 'v-next' of github.com:PaimaStudios/paima-engine into fe…
Yiakman 677685a
clean
Yiakman bda3358
added avail_enabled flag in start.ts
Yiakman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
avail_path | ||
avail_path | ||
avail_app.json |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,19 @@ | ||
{ | ||
"name": "@e2e/avail-contracts", | ||
"version": "0.3.0", | ||
"exports": {}, | ||
"exports": { | ||
".": "./read-app.ts" | ||
}, | ||
"tasks": { | ||
"avail-node:start": "deno run -A --unstable-detect-cjs @paima/npm-avail-node --dev", | ||
"avail-node:wait": "wait-on tcp:9944", | ||
"avail-light-client:start": "deno run -A --unstable-detect-cjs @paima/npm-avail-light-client --network local --config ./config.yml", | ||
"avail-node:start": "deno run -A --unstable-detect-cjs npm:@paimaexample/npm-avail-node --dev --rpc-port 9955 --no-telemetry", | ||
"avail-node:wait": "wait-on tcp:9955", | ||
"avail-light-client:deploy": "deno run -A --unstable-detect-cjs ./deploy.ts", | ||
"avail-light-client:start": "deno run -A --unstable-detect-cjs npm:@paimaexample/npm-avail-light-client --config ./config.yml --app-id $AVAIL_APP_ID", | ||
"avail-light-client:wait": "wait-on tcp:7007" | ||
}, | ||
"imports": {} | ||
"imports": { | ||
"avail-js-sdk": "npm:avail-js-sdk@^0.4.2", | ||
"@paimaexample/npm-avail-node": "npm:@paimaexample/npm-avail-node@0.3.30", | ||
"@paimaexample/npm-avail-light-client": "npm:@paimaexample/npm-avail-light-client@0.3.30" | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { Account, Pallets, SDK } from "avail-js-sdk"; | ||
|
||
const sdk = await SDK.New("ws://localhost:9955/ws"); | ||
const seed: string = Deno.env.get("SEED") ?? "//Alice"; | ||
if (!seed) { | ||
throw new Error("SEED environment variable is not set"); | ||
} | ||
const account = Account.new(seed); | ||
const genesisHash = await sdk.client.api.rpc.chain.getBlockHash(0); | ||
console.log("Account Address: ", account.address); | ||
// Use a fixed key string | ||
const ApplicationKey = "app_key_" + Date.now(); | ||
|
||
export async function createApplicationKey() { | ||
// Create application key transaction | ||
const tx = sdk.tx.dataAvailability.createApplicationKey(ApplicationKey); | ||
console.log("Submitting transaction to create application key..."); | ||
|
||
// Execute and wait for inclusion | ||
const res = await tx.executeWaitForInclusion(account, {}); | ||
|
||
// Check if transaction was successful | ||
const isOk = res.isSuccessful(); | ||
if (isOk === undefined) { | ||
throw new Error("Cannot check if transaction was successful"); | ||
} else if (!isOk) { | ||
console.log("Transaction failed", res); | ||
throw new Error("Transaction failed"); | ||
} | ||
|
||
// Extract event data | ||
if (res.events === undefined) throw new Error("No events found"); | ||
|
||
const event = res.events.findFirst( | ||
Pallets.DataAvailabilityEvents.ApplicationKeyCreated, | ||
); | ||
if (event === undefined) { | ||
throw new Error("ApplicationKeyCreated event not found"); | ||
} | ||
|
||
const appId = event.id; | ||
console.log(`Application created successfully:`); | ||
console.log(`Owner: ${event.owner}`); | ||
console.log(`Key: ${event.keyToString()}`); | ||
console.log(`App Id: ${appId}`); | ||
console.log(`Transaction Hash: ${res.txHash}`); | ||
return { appId, txHash: res.txHash }; | ||
} | ||
|
||
const { appId, txHash } = await createApplicationKey(); | ||
console.log("Transaction Hash: ", txHash.toString()); | ||
const data = JSON.stringify({ appId, txHash, ApplicationKey, genesisHash }); | ||
const fileName = Deno.cwd() + "/avail_app.json"; | ||
console.log("Writing to file: ", fileName); | ||
await Deno.writeTextFile(fileName, data); | ||
|
||
const child = new Deno.Command("deno", { | ||
args: ["task", "-f", "@e2e/avail-contracts", "avail-light-client:start"], | ||
env: { | ||
AVAIL_APP_ID: appId.toString(), | ||
}, | ||
}).spawn(); | ||
|
||
console.log("Light Client Started"); | ||
|
||
await child.status; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
export type AvailApplicationInfo = { | ||
appId: number; | ||
txHash: { // The txHash of the apps creation transaction | ||
value: string; | ||
}; | ||
ApplicationKey: string; | ||
genesisHash: string; | ||
}; | ||
|
||
let cachedAppInfo: AvailApplicationInfo | undefined; | ||
export function readAvailApplication(): AvailApplicationInfo { | ||
if (cachedAppInfo) return cachedAppInfo; | ||
try { | ||
// Get the directory of the current module file using Deno's URL API | ||
const dir = new URL(".", import.meta.url); | ||
// Construct the full path to avail_app.json | ||
const appInfoPath = new URL("avail_app.json", dir); | ||
const appInfoJson = Deno.readTextFileSync(appInfoPath); | ||
const appInfo = JSON.parse(appInfoJson) as AvailApplicationInfo; | ||
cachedAppInfo = appInfo; | ||
return appInfo; | ||
} catch (err) { | ||
if (err instanceof Deno.errors.NotFound) { | ||
throw new Error("avail_app.json not found in the current directory"); | ||
} | ||
throw new Error(`Failed to read avail_app.json: ${String(err)}`); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,27 @@ | ||
# Default configuration for avail-light-client | ||
# For a full list of options, see the official Avail documentation. | ||
# Avail Light Client config (no --network needed) | ||
|
||
log_level="info" | ||
http_server_host="127.0.0.1" | ||
http_server_port=7007 | ||
# Logging | ||
log_level = "info" | ||
|
||
# Secret key for libp2p keypair. Can be either set to 'seed' or to 'key'. | ||
# If set to seed, keypair will be generated from that seed. | ||
# If 'secret_key' is not set, a random seed will be used. | ||
# secret_key={ seed: "avail" } | ||
# API server | ||
http_server_host = "127.0.0.1" | ||
http_server_port = 7007 | ||
|
||
# P2P TCP listener port (default: 37000). | ||
port=37000 | ||
|
||
# WebSocket endpoint of a full node. | ||
# RPC to full node (custom 9955) | ||
full_node_ws = ["ws://127.0.0.1:9944"] | ||
|
||
# Application ID. If not set or set to 0, application client is not started. | ||
app_id=0 | ||
|
||
# Confidence threshold (default: 92.0). | ||
confidence=92.0 | ||
|
||
# File system path where RocksDB used by the light client stores its data. | ||
# This path is relative to the location of this config file. | ||
avail_path="avail_path" | ||
|
||
# Vector of Light Client bootstrap nodes. | ||
# This is for a local setup. Replace with public bootstraps for testnet/mainnet. | ||
bootstraps=["/ip4/127.0.0.1/tcp/39000/p2p/12D3KooWMm1c4pzeLPGkkCJMAgFbsfQ8xmVDusg272icWsaNHWzN"] | ||
# App / LC behavior | ||
app_id = 0 | ||
confidence = 92.0 | ||
avail_path = "avail_path" | ||
network_mode = "both" # both | p2p-only | rpc-only | ||
|
||
# LibP2P | ||
port = 37000 | ||
webrtc_port = 37001 | ||
# secret_key = { seed = "avail" } # optional deterministic key | ||
bootstraps = ["/ip4/127.0.0.1/tcp/39000/p2p/12D3KooWMm1c4pzeLPGkkCJMAgFbsfQ8xmVDusg272icWsaNHWzN"] | ||
|
||
# Telemetry / network identity (equivalent to local --network defaults) | ||
ot_collector_endpoint = "http://127.0.0.1:4317" | ||
genesis_hash = "DEV" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { createScheduledData, insertPrimitiveAccounting } from "@paima/db"; | ||
import { BuiltinTransitions, generateRawStmInput } from "@paima/concise"; | ||
import { | ||
type ConfigPrimitivePayloadType, | ||
ConfigPrimitiveAccountingPayloadType, | ||
ConfigPrimitiveType, | ||
type ConfigSyncProtocolType, | ||
type FlattenSyncProtocolIOFor, | ||
} from "@paima/config"; | ||
import { type StateUpdateStream, World } from "@paima/coroutine"; | ||
|
||
import type { PaimaBlockNumber } from "@paima/utils"; | ||
|
||
export default function* processAvailPaimaL2Datum( | ||
paima_block_height: PaimaBlockNumber, | ||
response: FlattenSyncProtocolIOFor< | ||
ConfigSyncProtocolType.AVAIL_PARALLEL, | ||
ConfigPrimitiveType.AvailPaimaL2, | ||
ConfigPrimitivePayloadType.Event | ||
>, | ||
): StateUpdateStream<void> { | ||
const { scheduledPrefix } = response.input; | ||
const { payload, syncProtocol } = response.output; | ||
const scheduledInputData = generateRawStmInput( | ||
BuiltinTransitions[ConfigPrimitiveType.AvailPaimaL2] | ||
.scheduledPrefix, | ||
scheduledPrefix, | ||
{ payload }, | ||
); | ||
yield* World.resolve(insertPrimitiveAccounting, { | ||
primitive_name: response.output.syncProtocol.payload.primitiveName, | ||
paima_block_height: paima_block_height, | ||
payload_type: ConfigPrimitiveAccountingPayloadType.Event, | ||
payload: JSON.stringify(response.output.payload) as any, | ||
Yiakman marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
}); | ||
if (scheduledPrefix) { | ||
yield* createScheduledData( | ||
JSON.stringify(scheduledInputData), | ||
{ | ||
blockHeight: syncProtocol.payload.ownChain.blockNumber, | ||
}, | ||
{ | ||
primitiveName: response.output.syncProtocol.payload.primitiveName, | ||
txHash: response.output.syncProtocol.internal.transactionHash, | ||
Yiakman marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
caip2: response.output.syncProtocol.payload.caip2, | ||
fromAddress: response.input.contractAddress, | ||
contractAddress: response.input.contractAddress, | ||
} | ||
); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.