Skip to content

Commit

Permalink
Merge pull request #255 from graphprotocol/juanmardefago/chain-aliasing
Browse files Browse the repository at this point in the history
feat: add basic chain aliasing and encoding for it
  • Loading branch information
juanmardefago authored May 6, 2024
2 parents 207ecd3 + aa194b5 commit a3fa849
Show file tree
Hide file tree
Showing 20 changed files with 471 additions and 313 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/subgraph.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: '16'
node-version: '20'
registry-url: https://registry.npmjs.org
- name: test
working-directory: ./packages/subgraph
Expand Down
14 changes: 14 additions & 0 deletions crates/encoding/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,20 @@ impl Encoder {
new_owner_address: *new_owner_address,
});
}
Message::RegisterNetworksAndAliases { remove, add } => {
for index in remove {
self.remove_network(*index);
}
for (id, _) in add {
self.add_network(id);
}

self.compressed
.push(CompressedMessage::RegisterNetworksAndAliases {
remove: remove.clone(),
add: add.clone(),
});
}
};
Ok(())
}
Expand Down
10 changes: 10 additions & 0 deletions crates/encoding/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ pub enum Message {
new_owner_address: [u8; 20],
},
Reset,
RegisterNetworksAndAliases {
// Remove is by index
remove: Vec<NetworkIndex>,
// Add is by CAIP2 id and Alias
add: Vec<(String, String)>,
},
}

#[derive(Clone, Debug, PartialEq, Eq)]
Expand All @@ -64,6 +70,10 @@ pub enum CompressedMessage {
new_owner_address: [u8; 20],
},
Reset,
RegisterNetworksAndAliases {
remove: Vec<u64>,
add: Vec<(String, String)>,
},
}

impl CompressedMessage {
Expand Down
22 changes: 22 additions & 0 deletions crates/encoding/src/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ fn serialize_message(message: &CompressedMessage, bytes: &mut Vec<u8>) {
CompressedMessage::ChangeOwnership { new_owner_address } => {
bytes.extend_from_slice(new_owner_address);
}
CompressedMessage::RegisterNetworksAndAliases { add, remove } => {
serialize_register_networks_and_aliases(add, remove, bytes)
}
}
}

Expand Down Expand Up @@ -78,6 +81,24 @@ fn serialize_register_networks(add: &[String], remove: &[NetworkIndex], bytes: &
}
}

fn serialize_register_networks_and_aliases(
add: &[(String, String)],
remove: &[NetworkIndex],
bytes: &mut Vec<u8>,
) {
serialize_u64(remove.len() as u64, bytes);
for id in remove {
// TODO: Compression - could delta encode series here. Probably not worth it.
serialize_u64(*id, bytes);
}

serialize_u64(add.len() as u64, bytes);
for (add0, add1) in add {
serialize_str(add0, bytes);
serialize_str(add1, bytes);
}
}

fn serialize_str(value: &str, bytes: &mut Vec<u8>) {
serialize_u64(value.len() as u64, bytes);
bytes.extend_from_slice(value.as_bytes());
Expand Down Expand Up @@ -119,6 +140,7 @@ fn message_tag(m: &CompressedMessage) -> u8 {
CompressedMessage::RegisterNetworks { .. } => 3,
CompressedMessage::ChangeOwnership { .. } => 4,
CompressedMessage::Reset => 5,
CompressedMessage::RegisterNetworksAndAliases { .. } => 6,
}
}

Expand Down
8 changes: 8 additions & 0 deletions crates/json-oracle-encoder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ fn messages_to_encoded_message_blocks(
Message::RegisterNetworks { remove, add } => {
ee::CompressedMessage::RegisterNetworks { remove, add }
}
Message::RegisterNetworksAndAliases { remove, add } => {
ee::CompressedMessage::RegisterNetworksAndAliases { remove, add }
}
Message::ChangeOwnership { new_owner_address } => {
ee::CompressedMessage::ChangeOwnership {
new_owner_address: new_owner_address
Expand Down Expand Up @@ -150,6 +153,10 @@ pub enum Message {
#[serde(deserialize_with = "deserialize_hex")]
new_owner_address: Vec<u8>,
},
RegisterNetworksAndAliases {
remove: Vec<u64>,
add: Vec<(String, String)>,
},
}

impl Message {
Expand All @@ -161,6 +168,7 @@ impl Message {
Message::UpdateVersion { .. } => "UpdateVersion",
Message::Reset => "Reset",
Message::ChangeOwnership { .. } => "ChangeOwnership",
Message::RegisterNetworksAndAliases { .. } => "RegisterNetworksAndAliases",
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions packages/subgraph/config/arbitrum-sepolia.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
{ "entry": "ResetStateMessage" },
{ "entry": "UpdateVersionsMessage" },
{ "entry": "RegisterNetworksMessage" },
{ "entry": "RegisterNetworksAndAliasesMessage" },
{ "entry": "ChangePermissionsMessage", "lastEntry": true }
],
"validThrough": "0"
Expand All @@ -18,6 +19,7 @@
"permissions": [
{ "entry": "UpdateVersionsMessage" },
{ "entry": "RegisterNetworksMessage" },
{ "entry": "RegisterNetworksAndAliasesMessage" },
{ "entry": "ChangePermissionsMessage", "lastEntry": true }
],
"validThrough": "0"
Expand Down
1 change: 1 addition & 0 deletions packages/subgraph/config/arbitrum.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"permissions": [
{ "entry": "UpdateVersionsMessage" },
{ "entry": "RegisterNetworksMessage" },
{ "entry": "RegisterNetworksAndAliasesMessage" },
{ "entry": "ChangePermissionsMessage", "lastEntry": true }
],
"validThrough": "0"
Expand Down
1 change: 1 addition & 0 deletions packages/subgraph/config/local.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
{ "entry": "CorrectEpochsMessage" },
{ "entry": "UpdateVersionsMessage" },
{ "entry": "RegisterNetworksMessage" },
{ "entry": "RegisterNetworksAndAliasesMessage" },
{ "entry": "ChangePermissionsMessage" },
{ "entry": "ResetStateMessage", "lastEntry": true }
],
Expand Down
1 change: 1 addition & 0 deletions packages/subgraph/config/mainnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
{ "entry": "SetBlockNumbersForEpochMessage" },
{ "entry": "UpdateVersionsMessage" },
{ "entry": "RegisterNetworksMessage" },
{ "entry": "RegisterNetworksAndAliasesMessage" },
{ "entry": "ChangePermissionsMessage", "lastEntry": true }
],
"validThrough": "0"
Expand Down
2 changes: 2 additions & 0 deletions packages/subgraph/config/sepolia.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
{ "entry": "ResetStateMessage" },
{ "entry": "UpdateVersionsMessage" },
{ "entry": "RegisterNetworksMessage" },
{ "entry": "RegisterNetworksAndAliasesMessage" },
{ "entry": "ChangePermissionsMessage", "lastEntry": true }
],
"validThrough": "0"
Expand All @@ -18,6 +19,7 @@
"permissions": [
{ "entry": "UpdateVersionsMessage" },
{ "entry": "RegisterNetworksMessage" },
{ "entry": "RegisterNetworksAndAliasesMessage" },
{ "entry": "ChangePermissionsMessage", "lastEntry": true }
],
"validThrough": "0"
Expand Down
2 changes: 2 additions & 0 deletions packages/subgraph/config/test.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
{ "entry": "CorrectEpochsMessage" },
{ "entry": "UpdateVersionsMessage" },
{ "entry": "RegisterNetworksMessage" },
{ "entry": "RegisterNetworksAndAliasesMessage" },
{ "entry": "ChangePermissionsMessage" },
{ "entry": "ResetStateMessage", "lastEntry": true }
],
Expand All @@ -28,6 +29,7 @@
{ "entry": "CorrectEpochsMessage" },
{ "entry": "UpdateVersionsMessage" },
{ "entry": "RegisterNetworksMessage" },
{ "entry": "RegisterNetworksAndAliasesMessage" },
{ "entry": "ChangePermissionsMessage" },
{ "entry": "ResetStateMessage", "lastEntry": true }
],
Expand Down
4 changes: 2 additions & 2 deletions packages/subgraph/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
"prep:arbitrum-sepolia": "mustache ./config/arbitrum-sepolia.json subgraph.template.yaml > subgraph.yaml && mustache ./config/arbitrum-sepolia.json src/constants.template.ts > src/constants.ts"
},
"devDependencies": {
"@graphprotocol/graph-cli": "^0.52.0",
"@graphprotocol/graph-ts": "^0.31.0",
"@graphprotocol/graph-cli": "^0.69.1",
"@graphprotocol/graph-ts": "^0.34.0",
"matchstick-as": "^0.5.0",
"mustache": "^4.0.1"
}
Expand Down
9 changes: 9 additions & 0 deletions packages/subgraph/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,18 @@ type RegisterNetworksMessage implements Message @entity {
addCount: BigInt!
}

type RegisterNetworksAndAliasesMessage implements Message @entity {
id: ID!
block: MessageBlock!
data: Bytes
removeCount: BigInt!
addCount: BigInt!
}

type Network @entity {
id: ID! # chainID now
#chainID: String!
alias: String!
addedAt: Message!
lastUpdatedAt: Message!
removedAt: Message
Expand Down
47 changes: 47 additions & 0 deletions packages/subgraph/src/constants.template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,50 @@ INITIAL_PERMISSION_SET.set("{{address}}", [["{{validThrough}}"],[{{#permissions}
{{/permissionList}}
export let BIGINT_ZERO = BigInt.fromI32(0);
export let BIGINT_ONE = BigInt.fromI32(1);
export let PRELOADED_ALIASES = new Map<String, String>();
PRELOADED_ALIASES.set("bip122:000000000019d6689c085ae165831e93", "btc")
PRELOADED_ALIASES.set("eip155:1", "mainnet")
PRELOADED_ALIASES.set("eip155:5", "goerli")
PRELOADED_ALIASES.set("eip155:10", "optimism")
PRELOADED_ALIASES.set("eip155:56", "bsc")
PRELOADED_ALIASES.set("eip155:97", "chapel")
PRELOADED_ALIASES.set("eip155:99", "poa-core")
PRELOADED_ALIASES.set("eip155:100", "gnosis")
PRELOADED_ALIASES.set("eip155:122", "fuse")
PRELOADED_ALIASES.set("eip155:137", "matic")
PRELOADED_ALIASES.set("eip155:250", "fantom")
PRELOADED_ALIASES.set("eip155:280", "zksync-era-testnet")
PRELOADED_ALIASES.set("eip155:288", "boba")
PRELOADED_ALIASES.set("eip155:1023", "clover")
PRELOADED_ALIASES.set("eip155:1284", "moonbeam")
PRELOADED_ALIASES.set("eip155:1285", "moonriver")
PRELOADED_ALIASES.set("eip155:1287", "mbase")
PRELOADED_ALIASES.set("eip155:4002", "fantom-testnet")
PRELOADED_ALIASES.set("eip155:42161", "arbitrum-one")
PRELOADED_ALIASES.set("eip155:42220", "celo")
PRELOADED_ALIASES.set("eip155:43113", "fuji")
PRELOADED_ALIASES.set("eip155:43114", "avalanche")
PRELOADED_ALIASES.set("eip155:44787", "celo-alfajores")
PRELOADED_ALIASES.set("eip155:80001", "mumbai")
PRELOADED_ALIASES.set("eip155:17000", "holesky")
PRELOADED_ALIASES.set("eip155:1313161554", "aurora")
PRELOADED_ALIASES.set("eip155:1313161555", "aurora-testnet")
PRELOADED_ALIASES.set("eip155:1666600000", "harmony")
PRELOADED_ALIASES.set("eip155:84532", "base-sepolia")
PRELOADED_ALIASES.set("eip155:300", "zksync-era-sepolia")
PRELOADED_ALIASES.set("eip155:1101", "polygon-zkevm")
PRELOADED_ALIASES.set("eip155:324", "zksync-era")
PRELOADED_ALIASES.set("eip155:11155111", "sepolia")
PRELOADED_ALIASES.set("eip155:421613", "arbitrum-goerli")
PRELOADED_ALIASES.set("eip155:421614", "arbitrum-sepolia")
PRELOADED_ALIASES.set("eip155:1442", "polygon-zkevm-testnet")
PRELOADED_ALIASES.set("eip155:59144", "linea")
PRELOADED_ALIASES.set("eip155:59140", "linea-goerli")
PRELOADED_ALIASES.set("eip155:8453", "base")
PRELOADED_ALIASES.set("eip155:534351", "scroll-sepolia")
PRELOADED_ALIASES.set("eip155:534352", "scroll")
PRELOADED_ALIASES.set("eip155:12611", "astar-zkevm-sepolia")
PRELOADED_ALIASES.set("eip155:81457", "blast-mainnet")
PRELOADED_ALIASES.set("eip155:3776", "astar-zkevm-mainnet")
PRELOADED_ALIASES.set("eip155:713715", "sei-testnet")
PRELOADED_ALIASES.set("eip155:168587773", "blast-testnet")
57 changes: 53 additions & 4 deletions packages/subgraph/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,59 @@ export const PREAMBLE_BIT_LENGTH = 8;
export const PREAMBLE_BYTE_LENGTH = PREAMBLE_BIT_LENGTH / 8;
export const TAG_BIT_LENGTH = 4;
//export const OWNER_ADDRESS_STRING = "";
export const EPOCH_MANAGER_ADDRESS = "0x64f990bf16552a693dcb043bb7bf3866c5e05ddb";
export const EPOCH_MANAGER_ADDRESS = "0x5a843145c43d328b9bb7a4401d94918f131bb281";

export let INITIAL_PERMISSION_SET = new Map<String,Array<String>>();
INITIAL_PERMISSION_SET.set("0xeb4ad97a099defc85c900a60adfd2405c455b2c0", ["SetBlockNumbersForEpochMessage","CorrectEpochsMessage","ResetStateMessage"])
INITIAL_PERMISSION_SET.set("0x48301fe520f72994d32ead72e2b6a8447873cf50", ["SetBlockNumbersForEpochMessage","UpdateVersionsMessage","RegisterNetworksMessage","ChangePermissionsMessage"])
export let INITIAL_PERMISSION_SET = new Map<String,Array<Array<String>>>();
INITIAL_PERMISSION_SET.set("0x881355f34b1c4c8e02ed6b20d16d4bf2c35f5d07", [["67713000"],["SetBlockNumbersForEpochMessage","CorrectEpochsMessage","ResetStateMessage"]])
INITIAL_PERMISSION_SET.set("0x3e4470ac38a67a81b5d855aaef8936d45afafa88", [["97000000"],["SetBlockNumbersForEpochMessage","CorrectEpochsMessage","ResetStateMessage"]])
INITIAL_PERMISSION_SET.set("0x5f49491e965895ded343af13389ee45ef60ed793", [["0"],["SetBlockNumbersForEpochMessage","CorrectEpochsMessage","ResetStateMessage"]])
INITIAL_PERMISSION_SET.set("0x8c6de8f8d562f3382417340a6994601ee08d3809", [["0"],["UpdateVersionsMessage","RegisterNetworksMessage","RegisterNetworksAndAliasesMessage","ChangePermissionsMessage"]])
export let BIGINT_ZERO = BigInt.fromI32(0);
export let BIGINT_ONE = BigInt.fromI32(1);
export let PRELOADED_ALIASES = new Map<String, String>();
PRELOADED_ALIASES.set("bip122:000000000019d6689c085ae165831e93", "btc")
PRELOADED_ALIASES.set("eip155:1", "mainnet")
PRELOADED_ALIASES.set("eip155:5", "goerli")
PRELOADED_ALIASES.set("eip155:10", "optimism")
PRELOADED_ALIASES.set("eip155:56", "bsc")
PRELOADED_ALIASES.set("eip155:97", "chapel")
PRELOADED_ALIASES.set("eip155:99", "poa-core")
PRELOADED_ALIASES.set("eip155:100", "gnosis")
PRELOADED_ALIASES.set("eip155:122", "fuse")
PRELOADED_ALIASES.set("eip155:137", "matic")
PRELOADED_ALIASES.set("eip155:250", "fantom")
PRELOADED_ALIASES.set("eip155:280", "zksync-era-testnet")
PRELOADED_ALIASES.set("eip155:288", "boba")
PRELOADED_ALIASES.set("eip155:1023", "clover")
PRELOADED_ALIASES.set("eip155:1284", "moonbeam")
PRELOADED_ALIASES.set("eip155:1285", "moonriver")
PRELOADED_ALIASES.set("eip155:1287", "mbase")
PRELOADED_ALIASES.set("eip155:4002", "fantom-testnet")
PRELOADED_ALIASES.set("eip155:42161", "arbitrum-one")
PRELOADED_ALIASES.set("eip155:42220", "celo")
PRELOADED_ALIASES.set("eip155:43113", "fuji")
PRELOADED_ALIASES.set("eip155:43114", "avalanche")
PRELOADED_ALIASES.set("eip155:44787", "celo-alfajores")
PRELOADED_ALIASES.set("eip155:80001", "mumbai")
PRELOADED_ALIASES.set("eip155:17000", "holesky")
PRELOADED_ALIASES.set("eip155:1313161554", "aurora")
PRELOADED_ALIASES.set("eip155:1313161555", "aurora-testnet")
PRELOADED_ALIASES.set("eip155:1666600000", "harmony")
PRELOADED_ALIASES.set("eip155:84532", "base-sepolia")
PRELOADED_ALIASES.set("eip155:300", "zksync-era-sepolia")
PRELOADED_ALIASES.set("eip155:1101", "polygon-zkevm")
PRELOADED_ALIASES.set("eip155:324", "zksync-era")
PRELOADED_ALIASES.set("eip155:11155111", "sepolia")
PRELOADED_ALIASES.set("eip155:421613", "arbitrum-goerli")
PRELOADED_ALIASES.set("eip155:421614", "arbitrum-sepolia")
PRELOADED_ALIASES.set("eip155:1442", "polygon-zkevm-testnet")
PRELOADED_ALIASES.set("eip155:59144", "linea")
PRELOADED_ALIASES.set("eip155:59140", "linea-goerli")
PRELOADED_ALIASES.set("eip155:8453", "base")
PRELOADED_ALIASES.set("eip155:534351", "scroll-sepolia")
PRELOADED_ALIASES.set("eip155:534352", "scroll")
PRELOADED_ALIASES.set("eip155:12611", "astar-zkevm-sepolia")
PRELOADED_ALIASES.set("eip155:81457", "blast-mainnet")
PRELOADED_ALIASES.set("eip155:3776", "astar-zkevm-mainnet")
PRELOADED_ALIASES.set("eip155:713715", "sei-testnet")
PRELOADED_ALIASES.set("eip155:168587773", "blast-testnet")
6 changes: 4 additions & 2 deletions packages/subgraph/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ export enum MessageTag {
UpdateVersionsMessage,
RegisterNetworksMessage,
ChangePermissionsMessage,
ResetStateMessage
ResetStateMessage,
RegisterNetworksAndAliasesMessage
}

export namespace MessageTag {
Expand All @@ -36,7 +37,8 @@ export namespace MessageTag {
"UpdateVersionsMessage",
"RegisterNetworksMessage",
"ChangePermissionsMessage",
"ResetStateMessage"
"ResetStateMessage",
"RegisterNetworksAndAliasesMessage"
];
export function toString(tag: MessageTag): string {
return tags[tag];
Expand Down
Loading

0 comments on commit a3fa849

Please sign in to comment.