Skip to content

Commit

Permalink
Merge pull request #261 from graphprotocol/mde/add-change-permissions…
Browse files Browse the repository at this point in the history
…-message-to-encoder

chore: add ChangePermissions message to encoder
  • Loading branch information
Maikol authored Jan 28, 2025
2 parents dadf415 + dcc5b01 commit 3219a7e
Show file tree
Hide file tree
Showing 13 changed files with 1,681 additions and 1,894 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: '20'
node-version: '22'
registry-url: https://registry.npmjs.org
- name: test
working-directory: ./packages/subgraph
Expand Down
54 changes: 49 additions & 5 deletions crates/encoding/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,6 @@ impl Encoder {
self.networks.clear();
self.compressed.push(CompressedMessage::Reset);
}
Message::ChangeOwnership { new_owner_address } => {
self.compressed.push(CompressedMessage::ChangeOwnership {
new_owner_address: *new_owner_address,
});
}
Message::RegisterNetworksAndAliases { remove, add } => {
for index in remove {
self.remove_network(*index);
Expand All @@ -169,6 +164,20 @@ impl Encoder {
add: add.clone(),
});
}
Message::ChangePermissions {
address,
valid_through,
permissions,
} => {
self.compressed.push(CompressedMessage::ChangePermissions {
address: *address,
valid_through: *valid_through,
permissions: permissions
.iter()
.map(|x| Message::str_to_u64(x.as_str()))
.collect(),
});
}
};
Ok(())
}
Expand Down Expand Up @@ -451,4 +460,39 @@ mod tests {
assert_ne!(networks_before, encoder.networks);
assert_ne!(encoder.networks.last().unwrap().1.block_delta, 0);
}

#[test]
fn change_permissions_message() {
let mut encoder = Encoder::new(CURRENT_ENCODING_VERSION, vec![]).unwrap();

let test_permissions = vec![
"RegisterNetworksAndAliasesMessage".to_string(),
"CorrectEpochsMessage".to_string(),
];

let result_permissions = vec![6, 1];

let compressed = encoder
.compress(&[Message::ChangePermissions {
address: [1u8; 20],
valid_through: 123u64,
permissions: test_permissions.clone(),
}])
.unwrap();

assert_eq!(compressed.len(), 1);

match &compressed[0] {
CompressedMessage::ChangePermissions {
address,
valid_through,
permissions,
} => {
assert_eq!(*address, [1u8; 20]);
assert_eq!(*valid_through, 123u64);
assert_eq!(*permissions, result_permissions);
}
_ => panic!("Expected ChangePermissions message"),
}
}
}
31 changes: 25 additions & 6 deletions crates/encoding/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,33 @@ pub enum Message {
UpdateVersion {
version_number: u64,
},
ChangeOwnership {
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)>,
},
ChangePermissions {
address: [u8; 20],
valid_through: u64,
permissions: Vec<String>,
},
}

impl Message {
pub fn str_to_u64(str: &str) -> u64 {
match str {
"SetBlockNumbersForNextEpochMessage" => 0u64,
"CorrectEpochsMessage" => 1,
"UpdateVersionMessage" => 2,
"RegisterNetworksMessage" => 3,
"ChangePermissionsMessage" => 4,
"ResetStateMessage" => 5,
"RegisterNetworksAndAliasesMessage" => 6,
_ => 7,
}
}
}

#[derive(Clone, Debug, PartialEq, Eq)]
Expand All @@ -66,14 +83,16 @@ pub enum CompressedMessage {
UpdateVersion {
version_number: u64,
},
ChangeOwnership {
new_owner_address: [u8; 20],
},
Reset,
RegisterNetworksAndAliases {
remove: Vec<u64>,
add: Vec<(String, String)>,
},
ChangePermissions {
address: [u8; 20],
valid_through: u64,
permissions: Vec<u64>,
},
}

impl CompressedMessage {
Expand Down
24 changes: 20 additions & 4 deletions crates/encoding/src/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,14 @@ fn serialize_message(message: &CompressedMessage, bytes: &mut Vec<u8>) {
CompressedMessage::CorrectEpochs { .. } => {
todo!()
}
CompressedMessage::ChangeOwnership { new_owner_address } => {
bytes.extend_from_slice(new_owner_address);
}
CompressedMessage::RegisterNetworksAndAliases { add, remove } => {
serialize_register_networks_and_aliases(add, remove, bytes)
}
CompressedMessage::ChangePermissions {
address,
valid_through,
permissions,
} => serialize_change_permissions(address, *valid_through, permissions, bytes),
}
}

Expand Down Expand Up @@ -99,6 +101,20 @@ fn serialize_register_networks_and_aliases(
}
}

fn serialize_change_permissions(
address: &[u8],
valid_through: u64,
permissions: &[u64],
bytes: &mut Vec<u8>,
) {
bytes.extend_from_slice(address);
serialize_u64(valid_through, bytes);
serialize_u64(permissions.len() as u64, bytes);
for permission in permissions {
serialize_u64(*permission, 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 @@ -138,7 +154,7 @@ fn message_tag(m: &CompressedMessage) -> u8 {
CompressedMessage::CorrectEpochs { .. } => 1,
CompressedMessage::UpdateVersion { .. } => 2,
CompressedMessage::RegisterNetworks { .. } => 3,
CompressedMessage::ChangeOwnership { .. } => 4,
CompressedMessage::ChangePermissions { .. } => 4,
CompressedMessage::Reset => 5,
CompressedMessage::RegisterNetworksAndAliases { .. } => 6,
}
Expand Down
34 changes: 21 additions & 13 deletions crates/json-oracle-encoder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,20 @@ fn messages_to_encoded_message_blocks(
Message::RegisterNetworksAndAliases { remove, add } => {
ee::CompressedMessage::RegisterNetworksAndAliases { remove, add }
}
Message::ChangeOwnership { new_owner_address } => {
ee::CompressedMessage::ChangeOwnership {
new_owner_address: new_owner_address
.try_into()
.map_err(|_| anyhow!("Bad owner address length; must be 20 bytes"))?,
}
}
Message::ChangePermissions {
address,
valid_through,
permissions,
} => ee::CompressedMessage::ChangePermissions {
address: address
.try_into()
.map_err(|_| anyhow!("Bad address length; must be 20 bytes"))?,
valid_through,
permissions: permissions
.into_iter()
.map(|x| ee::Message::str_to_u64(x.as_str()))
.collect(),
},
Message::SetBlockNumbersForNextEpoch(SetBlockNumbersForNextEpoch::Empty {
count,
}) => ee::CompressedMessage::SetBlockNumbersForNextEpoch(
Expand Down Expand Up @@ -148,15 +155,16 @@ pub enum Message {
version_number: u64,
},
Reset,
#[serde(rename_all = "camelCase")]
ChangeOwnership {
#[serde(deserialize_with = "deserialize_hex")]
new_owner_address: Vec<u8>,
},
RegisterNetworksAndAliases {
remove: Vec<u64>,
add: Vec<(String, String)>,
},
ChangePermissions {
#[serde(deserialize_with = "deserialize_hex")]
address: Vec<u8>,
valid_through: u64,
permissions: Vec<String>,
},
}

impl Message {
Expand All @@ -167,8 +175,8 @@ impl Message {
Message::RegisterNetworks { .. } => "RegisterNetworks",
Message::UpdateVersion { .. } => "UpdateVersion",
Message::Reset => "Reset",
Message::ChangeOwnership { .. } => "ChangeOwnership",
Message::RegisterNetworksAndAliases { .. } => "RegisterNetworksAndAliases",
Message::ChangePermissions { .. } => "ChangePermissions",
}
}
}
Expand Down

This file was deleted.

3 changes: 3 additions & 0 deletions crates/xtask/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ features = ["derive"]
[dependencies.url]
version = "2"
features = ["serde"]

[lints.rust]
unexpected_cfgs = { level = "allow", check-cfg = ['cfg(trick_rust_analyzer_into_highlighting_interpolated_bits)'] }
14 changes: 7 additions & 7 deletions packages/subgraph/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
"scripts": {
"prepare": "./scripts/prepare.sh",
"codegen": "yarn && graph codegen",
"test": "yarn && yarn prep:test && yarn codegen && graph test",
"test": "yarn && yarn prep:test && yarn codegen && graph test -d",
"build": "yarn && yarn prepare && graph build",
"deploy-mainnet": "yarn && yarn prep:mainnet && yarn codegen && graph build --network mainnet && graph deploy --studio graph-ebo-ethereum",
"deploy-arbitrum": "yarn && yarn prep:arbitrum && yarn codegen && graph build --network arbitrum-one && graph deploy --studio graph-ebo-arbitrum",
"deploy-sepolia": "yarn && yarn prep:sepolia && yarn codegen && graph build --network sepolia && graph deploy --studio graph-ebo-sepolia",
"deploy-arbitrum-sepolia": "yarn && yarn prep:arbitrum-sepolia && yarn codegen && graph build --network arbitrum-sepolia && graph deploy --studio graph-ebo-arbitrum-sepolia",
"deploy-mainnet": "yarn && yarn prep:mainnet && yarn codegen && graph build --network mainnet && graph deploy graph-ebo-ethereum",
"deploy-arbitrum": "yarn && yarn prep:arbitrum && yarn codegen && graph build --network arbitrum-one && graph deploy graph-ebo-arbitrum",
"deploy-sepolia": "yarn && yarn prep:sepolia && yarn codegen && graph build --network sepolia && graph deploy graph-ebo-sepolia",
"deploy-arbitrum-sepolia": "yarn && yarn prep:arbitrum-sepolia && yarn codegen && graph build --network arbitrum-sepolia && graph deploy graph-ebo-arbitrum-sepolia",
"create-local": "graph create --node http://127.0.0.1:8020/ edgeandnode/block-oracle",
"remove-local": "graph remove --node http://127.0.0.1:8020/ edgeandnode/block-oracle",
"deploy-local": "yarn codegen && graph deploy --node http://127.0.0.1:8020/ --ipfs http://localhost:${IPFS_PORT} edgeandnode/block-oracle --version-label 0.1.0",
Expand All @@ -21,8 +21,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.69.1",
"@graphprotocol/graph-ts": "^0.34.0",
"@graphprotocol/graph-cli": "^0.94.0",
"@graphprotocol/graph-ts": "^0.37.0",
"matchstick-as": "^0.5.0",
"mustache": "^4.0.1"
}
Expand Down
1 change: 1 addition & 0 deletions packages/subgraph/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type ChangePermissionsMessage implements Message @entity {
block: MessageBlock!
data: Bytes
address: String!
validThrough: BigInt!
oldPermissions: [String!]!
newPermissions: [String!]!
}
Expand Down
25 changes: 20 additions & 5 deletions packages/subgraph/src/mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -607,34 +607,49 @@ function executeChangePermissionsMessage(
let message = cache.getChangePermissionsMessage(id);
let address = reader.advance(20).toHexString(); // address should always be 20 bytes

// Get valid_through
let validThrough = decodeU64(reader);
if (!reader.ok) {
return;
}

// Get the length of the new premissions list
let permissionsListLength = decodeU64(reader) as i32;
if (!reader.ok) {
return;
}

let permissionEntry = cache.getPermissionListEntry(address);
permissionEntry.validThrough = BigInt.fromU64(validThrough)
let oldPermissionList = permissionEntry.permissions;
let newPermissionList = new Array<String>();

for (let i = 0; i < permissionsListLength; i++) {
let permission = decodeU64(reader) as i32;
newPermissionList.push(MessageTag.toString(permission));
if(MessageTag.isValid(permission)) {
newPermissionList.push(MessageTag.toString(permission));
} else {
reader.fail(`Permission to add is invalid. Permission index: ${permission.toString()}`)
}
}
permissionEntry.permissions = newPermissionList;

message.block = messageBlock.id;
message.address = address;
message.validThrough = BigInt.fromU64(validThrough);
message.oldPermissions = oldPermissionList;
message.newPermissions = newPermissionList;
message.data = reader.diff(snapshot);


let list = globalState.permissionList;
list.push(permissionEntry.id);
if(!list.includes(permissionEntry.id)) {
list.push(permissionEntry.id);
} else if (permissionsListLength == 0) {
// this will remove the now empty permission entry from the list, preventing spam
list.splice(list.indexOf(permissionEntry.id), 1)
}
globalState.permissionList = list;

// might want to remove it from the "allow list" if the new permission list length is 0
// Right now the address won't be able to execute anything on that case, but it can spam
}

function executeResetStateMessage(
Expand Down
10 changes: 6 additions & 4 deletions packages/subgraph/src/store-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,13 @@ export class StoreCache {

getPermissionListEntry(id: String): PermissionListEntry {
if (this.permissionListEntries.safeGet(id) == null) {
let permissionListyEntry = PermissionListEntry.load(id);
if (permissionListyEntry == null) {
permissionListyEntry = new PermissionListEntry(id);
let permissionListEntry = PermissionListEntry.load(id);
if (permissionListEntry == null) {
permissionListEntry = new PermissionListEntry(id);
permissionListEntry.permissions = [];
permissionListEntry.validThrough = BigInt.fromI32(0);
}
this.permissionListEntries.set(id, permissionListyEntry);
this.permissionListEntries.set(id, permissionListEntry);
}
return this.permissionListEntries.safeGet(id)!;
}
Expand Down
Loading

0 comments on commit 3219a7e

Please sign in to comment.