Repository navigation
feat: Implement eth2util/deposit - #134
Conversation
varex83
left a comment
There was a problem hiding this comment.
Putting the first part of the review here, will check other part later
| } | ||
|
|
||
| /// Serializes a list of deposit data into a single file. | ||
| pub fn marshal_deposit_data( |
There was a problem hiding this comment.
I think it should be replaced using serde and serde_json
There was a problem hiding this comment.
The DepositDataJson itself use serde as the serialize/deserialize. This method contains the transformation from input to serde-able datastructure. I don't like the name marshal, but decided to keep it to compatible with golang API for the first phase
There was a problem hiding this comment.
I agree with @varex83. Ideally this would be done at the serde level.
There was a problem hiding this comment.
Not sure if I understand correctly, but I'm not sure how can I implement this at serde level.
The function needs to take network as an argument to get fork_version and sign on that. If we want to write a Serialization we need to wrap the DepositData to DepositDataWithNetwork and impl Serialize trait for that. But it's quite over-engineering from my point of view.
There was a problem hiding this comment.
I don't think it will be over-engineering. From my point of view it would be just more rust idiomatic and will work natively with serde (which I would expect as a developer working on serialization/deserialization in rust)
There was a problem hiding this comment.
Now that you mention the extra parameter, your solution seems like the right approach but I agree on the overengineering comment.
Let's leave it as it is for now then.
| } | ||
|
|
||
| /// Serializes a list of deposit data into a single file. | ||
| pub fn marshal_deposit_data( |
There was a problem hiding this comment.
I agree with @varex83. Ideally this would be done at the serde level.
emlautarom1
left a comment
There was a problem hiding this comment.
LGTM
Before merging, I suggest to evaluate if we can replace some of our code (in particular helpers) with methods provided by alloy
| tokio::fs::write(&file_path, bytes).await?; | ||
|
|
||
| let mut perms = tokio::fs::metadata(&file_path).await?.permissions(); | ||
| perms.set_readonly(true); | ||
| tokio::fs::set_permissions(&file_path, perms).await?; |
There was a problem hiding this comment.
File could be modified between write and permission change. I think we can check file hash before / after or use some kind of atomic operations instead.
There was a problem hiding this comment.
But it should be fine right? We read a snapshot of the permissions, and then write a modified version of the snapshot. If something happens to the file (ex. gets deleted) then the operation fails.
There was a problem hiding this comment.
I mean that we set file to read-only, but the contents of it could be changed between write and permissions update
There was a problem hiding this comment.
Hmm, makes sense. Let's follow what the Go implementation does and add a TODO here in case they don't do it atomically.
| let mut deposit_datas_list = Vec::new(); | ||
|
|
||
| for file in files { | ||
| let bytes = tokio::fs::read(&file).await?; |
There was a problem hiding this comment.
Currently files are read sequentially, consider using FuturesUnordered
There was a problem hiding this comment.
Potential optimization, but the original Go code performs this operation sequentially.
| } | ||
|
|
||
| /// Reads all deposit data files from a cluster directory. | ||
| pub async fn read_deposit_data_files(cluster_dir: &Path) -> Result<Vec<Vec<DepositData>>> { |
There was a problem hiding this comment.
| pub async fn read_deposit_data_files(cluster_dir: &Path) -> Result<Vec<Vec<DepositData>>> { | |
| pub async fn read_deposit_data_files(cluster_dir: impl AsRef<Path>) -> Result<Vec<Vec<DepositData>>> { |
There was a problem hiding this comment.
Is there any lint that we can use for this? Seems to be a recurring topic in our reviews
There was a problem hiding this comment.
Seems like adding pedantic = "warn" to [workspace.lints.clippy] adds a lot of good suggestions, I think we can use it
There was a problem hiding this comment.
or we can customize github copilot review: https://docs.github.com/en/copilot/how-tos/use-copilot-agents/request-a-code-review/use-code-review#customizing-copilots-reviews-with-custom-instructions
There was a problem hiding this comment.
It would be nice to have immediate feedback on the IDE rather than waiting for Copilot to review it, but as a fall-back that's OK too.
| async fn read_deposit_data_files_invalid_pubkey_hex() { | ||
| let dir = tempdir().unwrap(); | ||
| let datas = generate_deposit_datas(DEFAULT_DEPOSIT_AMOUNT); | ||
| let bytes = marshal_deposit_data(&datas, "goerli").unwrap(); | ||
| let file = get_deposit_file_path(dir.path(), DEFAULT_DEPOSIT_AMOUNT); | ||
| tokio::fs::write(&file, &bytes).await.unwrap(); | ||
|
|
There was a problem hiding this comment.
Since we have a lot of code duplication here I would suggest using test_case or something similar
There was a problem hiding this comment.
That looks cleaner, will try to adopt it later 👍
Should fix: #63