Skip to content
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

feat: DNS example #1059

Merged
merged 13 commits into from
Aug 19, 2024
Merged

feat: DNS example #1059

merged 13 commits into from
Aug 19, 2024

Conversation

vadorovsky
Copy link
Contributor

Add an example program which stores simple DNS records as compressed
accounts. It consists of the following endpoints:

  • create
  • update
  • delete

Currently it has only Rust SBF tests.

@vadorovsky vadorovsky force-pushed the vadorovsky/dns branch 4 times, most recently from cfb859a to 5e94897 Compare August 7, 2024 15:37
@vadorovsky vadorovsky changed the base branch from main to vadorovsky/discriminator-macro August 7, 2024 15:38
@vadorovsky vadorovsky force-pushed the vadorovsky/dns branch 4 times, most recently from ae4465b to f715bd8 Compare August 8, 2024 04:33
@vadorovsky vadorovsky force-pushed the vadorovsky/discriminator-macro branch from d430382 to e62d138 Compare August 8, 2024 04:35
@vadorovsky vadorovsky changed the base branch from vadorovsky/discriminator-macro to vadorovsky/macro-light-accounts August 8, 2024 04:40
@vadorovsky vadorovsky force-pushed the vadorovsky/macro-light-accounts branch from d2e0e46 to de5d56d Compare August 8, 2024 05:05
@vadorovsky vadorovsky marked this pull request as ready for review August 8, 2024 05:09
@vadorovsky vadorovsky force-pushed the vadorovsky/macro-light-accounts branch from de5d56d to 1d7e40f Compare August 8, 2024 12:37
@vadorovsky vadorovsky changed the base branch from vadorovsky/macro-light-accounts to vadorovsky/macro-trait-accounts August 8, 2024 12:40
@ananas-block
Copy link
Contributor

ananas-block commented Aug 9, 2024

Thanks for adding the changes!
The way you hash values onchain should be secure now.

However, the instructions sent a lot of redundant data because the complete CompressedAccount is sent as instruction data. Some data is generated onchain, constant or already sent as account. For example, the data hash is created onchain, the lamports amount is always zero and the owner should always be the signer. (One can make a case to send the owner in the instruction data as well and compare it onchain with the signer to be able to throw meaningful error, if we just use the signer as owner the error for an invalid owner will be ProofVerificationFailed.)

The pattern we implement for example in the token program is to create an additional input data struct and just send the most necessary data for the operation instead of the complete compressed account including its data_hash.
For example in the token program we just send the subset of TokenData (input token data) then hash it, serialize it and build the compressed account onchain (actual order is build compressed account and token data, then serialize, hash and add the data and data hash to the compressed account.

Another more complex example where I use a different input and data struct.

Since this is a minimal example optimizing instruction data might be overkill up to you to what degree you think it makes sense to optimize. cc @SwenSchaeferjohann

@vadorovsky
Copy link
Contributor Author

I'm actually fine with optimizing instruction data now - it will give us an idea how a generic instruction is going to look like and whether there is any room for improvement or having some convenience structs.

One I can already think of is packing all address_* parameters into convenience struct. Would be somehow similar to NewAddressParamsPacked, but without seed.

@ananas-block
Copy link
Contributor

The address convenience struct makes sense. From a security standpoint this could help to incentivize secure patterns since seeds need to be checked onchain.

@vadorovsky vadorovsky changed the base branch from vadorovsky/macro-trait-accounts to main August 14, 2024 09:42
@vadorovsky vadorovsky force-pushed the vadorovsky/dns branch 2 times, most recently from 2216f3d to cc35240 Compare August 16, 2024 09:43
@vadorovsky vadorovsky changed the base branch from main to vadorovsky/macro-light-hasher August 16, 2024 09:43
- `AsByteVec`, providing `as_byte_vec()` method, guarantees consistent way
  of serializing types to 2D byte vectors before hashing. We provide default
  implementations for primitives. More complex types need to implement
  `IntoBytes` themselves.
  - By using 2D vectors, we make it possible to represent structs with
    multiple fields. This way, we can handle struct fields (nested structs)
    the same way as primitive fields and deal with all types by using
    one trait.
  - The reason behing using vectors instead of slices is that in some
    cases, we cannot just cast the type without defining custom bytes.
    Vectors, although they might introduce copies, make sure we always
    own the bytes we are creating.
- `#[derive(LightHasher)]` implements `AsByteVec` and `DataHasher` traits.
  The `DataHasher` implementation takes bytes returned by `AsByteVec` as
  an input.
- empty string
- `Option` with `Some` and `None` and assertion that `None != Some(0)`
- array (including an empty one)
@vadorovsky vadorovsky force-pushed the vadorovsky/macro-light-hasher branch from 14c9f46 to 84c55be Compare August 19, 2024 08:06
vadorovsky and others added 11 commits August 19, 2024 10:44
Add an example program which stores simple DNS records as compressed
accounts. It consists of the following endpoints:

- create
- update
- delete

Currently it has only Rust SBF tests.

Co-authored-by: Michal Rostecki <vadorovsky@protonmail.com>
This gives a security guarantee for the program. Allowing the caller to
pass a custom seed was not secure.
- Avoid sending a full `CompressedAccount`.
- SDK changes which make it easier:
  - Add `MerkleOutputContext` which stores only the Merkle tree pubkey.
  - Add `PackedAddressContext` for address Merkle tree and queue.
  - Add `pack_*` methods for above.
    - Add separate methods for single Merkle contexts (e.g.
      `pack_merkle_context`) and multiple ones
      (e.g. `pack_merkle_contexts`).
    - In contrast to the equivalents existing in system program SDK, do
      everything in one `iter().collect()` chain, not in 3 loops.
  - Add `RemainingAccounts` structure which takes out the burden of
    iteracting with `HashMap` directly.
Base automatically changed from vadorovsky/macro-light-hasher to main August 19, 2024 11:01
@SwenSchaeferjohann SwenSchaeferjohann merged commit 08b666a into main Aug 19, 2024
3 checks passed
SwenSchaeferjohann added a commit that referenced this pull request Aug 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants