Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

Load account from file #21812

Merged
merged 3 commits into from
Dec 15, 2021
Merged

Conversation

losman0s
Copy link
Contributor

Problem

Programs can be dumped to file, and seamlessly loaded from file at genesis into the test validator. There is no similar feature for accounts (only dumping data of an account is supported, and no loading). This would ease testing on localnet when several accounts are necessary to run tests (manual and automated both), and for repeatability purposes (cloning leads to changing states per test run).

Summary of Changes

  • Add complete account dump to file (not only data)
  • Add solana-test-validator option to load accounts from file

Notes

  • Serialization/deserialization is done using the formats available in the --output option of solana-test-validator, i.e. json and json-compact
  • Behavior is unchanged when dumping account to file without the --output option (only data gets written)

Fixes #20279

@mergify mergify bot added the community Community contribution label Dec 11, 2021
@mergify mergify bot requested a review from a team December 11, 2021 22:14
@losman0s losman0s force-pushed the load-account-from-file branch from d39f462 to a5e0443 Compare December 11, 2021 22:28
@codecov
Copy link

codecov bot commented Dec 12, 2021

Codecov Report

Merging #21812 (3b0212f) into master (02fa135) will increase coverage by 0.0%.
The diff coverage is 0.0%.

❗ Current head 3b0212f differs from pull request most recent head 4fb7ae7. Consider uploading reports for the commit 4fb7ae7 to get more accurate results

@@           Coverage Diff            @@
##           master   #21812    +/-   ##
========================================
  Coverage    81.3%    81.3%            
========================================
  Files         516      516            
  Lines      144307   144064   -243     
========================================
- Hits       117323   117150   -173     
+ Misses      26984    26914    -70     

@losman0s losman0s force-pushed the load-account-from-file branch from a5e0443 to 3b0212f Compare December 12, 2021 12:41
@losman0s losman0s marked this pull request as ready for review December 12, 2021 14:52
Copy link
Contributor

@mvines mvines left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this PR. We've wanted this feature for a while!

cli/src/wallet.rs Outdated Show resolved Hide resolved
@@ -203,6 +206,37 @@ impl TestValidatorGenesis {
self
}

pub fn add_accounts_with_path(&mut self, filenames: Vec<&str>) -> &mut Self {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about:

Suggested change
pub fn add_accounts_with_path(&mut self, filenames: Vec<&str>) -> &mut Self {
pub fn add_accounts_from_json_files(&mut self, filename: &[Pubkey]) -> &mut Self {

Pubkey as the type for filename, and we don't need to assume a Vec when any slice will do

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Slice instead of Vec makes sense, will do.
However can you elaborate about the suggestion to enforce that the file name be the account address string? I fear it might be unnecessarily constraining, particularly when the pubkey is naturally part of the file content (coming from solana account), and that the deserialization operation would already validate that this address string makes for a valid Pubkey.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It just feels like better typing to require the filename to be a Pubkey here to me, we don't actually accept any other value right? So we can push the validation out to the --account-from-json-file clap.rs argument handling, and know that internally the filename will always be a valid pubkey

Copy link
Contributor Author

@losman0s losman0s Dec 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd argue that we don't really care about the name of the file, actually. A standard flow would look like:

  1. dump mainnet account to file
solana account -u m \
  --output json-compact \
  --output-file SRM_token.json \
  SRMuApVNdxXokk5GT7XD5cUUgXMBCoAz2LHeuAoKWRt
  1. start test validator with this account, and another one dumped beforehand for instance:
solana-test-validator --clone-from-file SRM_token USD_token

Am I missing something?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I was thinking we'd just use the filename as the address:

$ solana-test-validator --account-from-json-file SRMuApVNdxXokk5GT7XD5cUUgXMBCoAz2LHeuAoKWRt.json

but yeah maybe that's not so great, and it doesn't match what --bpf-program already does.

So I take it all back, something like this seems great (also looking to drop the "clone" naming from this argument):

$ solana-test-validator --account SRMuApVNdxXokk5GT7XD5cUUgXMBCoAz2LHeuAoKWRt SRM_token.json

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it!

As a minor tweak, we could change the address to be an optional second argument to --account will simplify the arg parsing code. Slightly inconsistent with the --bpf-program argument though, but feels more natural if the address is also now embedded in SRM_token.json.

$ solana-test-validator --account SRM_token.json
        or
$ solana-test-validator --account SRM_token.json SRMuApVNdxXokk5GT7XD5cUUgXMBCoAz2LHeuAoKWRt 

Copy link
Contributor Author

@losman0s losman0s Dec 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. My bad, I was not clear enough. I was actually proposing to implement one, or the other. I just looked into implementing an optional pubkey arg like you suggest, and unless I am missing something it makes the parsing quite tricky, because option values are collected "flat", even across separate occurrences of a particular option.

Example:

solana-test-validator --account serum SRMuApVNdxXokk5GT7XD5cUUgXMBCoAz2LHeuAoKWRt uscd --account raydium

would be collected by clap as the following String array: [serum, SRMuApVNdxXokk5GT7XD5cUUgXMBCoAz2LHeuAoKWRt, usdc, raydium], not [[serum, SRMuApVNdxXokk5GT7XD5cUUgXMBCoAz2LHeuAoKWRt], [usdc], [raydium]].
The grouping would then need to be inferred from a is_pubkey check succeeding or not, or something similar.
Do you think this is worth it? Or would 1 of the options above sound good enough?

  1. A detail I'd rather clarify, since I mislead you a bit in the examples above: the file argument is currently without the file extension, which is automatically appended to the argument provided. I went that way because only a json file is valid, but I'm open to challenge if you think that's weird.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. This seems best then

$ solana-test-validator --account SRMuApVNdxXokk5GT7XD5cUUgXMBCoAz2LHeuAoKWRt SRM_token.json

Consistent with --bpf-program and allows the same SRM_token.json account data to be reused at different addresses without editing the file contents

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All comments addressed. CI under way. I actually changed the parsing so that the whole filename (including extension) is required. Didn't make much sense to not allow the user to add it (if they're using autocompletion for instance).

The only thing a bit annoying right now is that if you specify a file that does not exist in CWD or tests/fixtures (the paths explored by solana_program_test::find_file), the error log is in test-ledger/validator.log, and not in the CLI output like it is for a wrong pubkey.
I left it that way as it is a tradeoff to be able to get the files automatically found in tests/fixtures without having to supply the whole path. Not sure if it is worth it though, or if I should just require a straight valid path which could be validated before starting the validator, and would display the error in the command output.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, thanks. I pushed a tiny commit to update the arg name

validator/src/bin/solana-test-validator.rs Outdated Show resolved Hide resolved
validator/src/bin/solana-test-validator.rs Outdated Show resolved Hide resolved
validator/src/bin/solana-test-validator.rs Outdated Show resolved Hide resolved
validator/src/bin/solana-test-validator.rs Outdated Show resolved Hide resolved
@losman0s losman0s force-pushed the load-account-from-file branch 6 times, most recently from 281838a to 15903e8 Compare December 15, 2021 12:42
This commit introduces the ability to dump the complete content of an
account to a JSON file (compact or not depending on the provided format
option).

Example:

```sh
solana account -u m \
  --output json-compact \
  --output-file SRM_token.json \
  SRMuApVNdxXokk5GT7XD5cUUgXMBCoAz2LHeuAoKWRt
```

Note: Behavior remains untouched if format option `--output` is not
provided (only account data gets written to file).
This introduces the `--clone-from-file` option for
solana-test-validator. It allows specifying any number of files
(without extension) containing account info and data, which will be
loaded at genesis. This is similar to `--bpf-program` for programs
loading.

The files will be searched for in the CWD or in `tests/fixtures`.

Example: `solana-test-validator --clone-from-file SRM_token USD_token`
@losman0s losman0s force-pushed the load-account-from-file branch from 15903e8 to 4b77dcc Compare December 15, 2021 15:25
mvines
mvines previously approved these changes Dec 15, 2021
validator/src/bin/solana-test-validator.rs Outdated Show resolved Hide resolved
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
community Community contribution
Projects
None yet
Development

Successfully merging this pull request may close these issues.

solana-test-validator could expose add_account_with_file_data
2 participants