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

Deposit contract suggestions #95

Merged
merged 22 commits into from
May 15, 2024

Conversation

marijanp
Copy link
Collaborator

@marijanp marijanp commented May 8, 2024

Incorporate refactorings after discussions with @Avi-D-coder.

Merge this branch into feature/deposit-contract then merge feature/deposit-contract into master.

Or directly merge this.

Copy link

github-actions bot commented May 8, 2024

File Coverage
All files 57%
kairos-crypto/src/implementations/casper.rs 6%
kairos-test-utils/src/cctl/parsers.rs 66%
kairos-tx/src/asn.rs 51%
kairos-tx/src/error.rs 0%
kairos-server/src/routes/deposit.rs 88%
kairos-server/src/routes/transfer.rs 90%
kairos-test-utils/src/cctl.rs 90%
kairos-server/src/state/transactions.rs 66%
kairos-server/src/state/trie.rs 35%
demo-contract-tests/tests/test_fixture/mod.rs 97%
kairos-server/tests/transactions.rs 85%
kairos-server/src/state/transactions/batch_state.rs 36%
kairos-server/src/config.rs 0%
kairos-server/src/errors.rs 12%
kairos-server/src/lib.rs 95%
kairos-server/src/state.rs 92%
kairos-server/src/utils.rs 22%

Minimum allowed coverage is 60%

Generated by 🐒 cobertura-action against b663b89

@jonas089
Copy link
Contributor

jonas089 commented May 9, 2024

What's the idea behind this PR? Is it supposed to replace the other deposit contract PR?

.gitignore Outdated Show resolved Hide resolved

#[derive(Event)]
pub struct Deposit {
pub account: Key,
Copy link
Contributor

Choose a reason for hiding this comment

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

What is this key type?
Can we use a more specific type?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The problem is that this could be an AccountHash or a ContractPackageHash.

If you follow how the code that creates the Deposit event you can see that get_immediate_caller is called to figure out who the depositor is. That function in turn is calling call_stack_element_to_key which returns an Key which is derived either from an AccountHash or ContractPackageHash.

Notice that the underlying representation of these two types is not identical.

The question is whether it makes sense to constrain it to AccountHashes, that would imply that Contracts could not have a kairos/L2 account.

We could probably wrap it in an Either type

Copy link
Contributor

@jonas089 jonas089 May 9, 2024

Choose a reason for hiding this comment

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

I did this on purpose to support both Account Hashs and Contract Hashs, the Key wrapper in my implementation will work well for anything Kairos.

I know we don't like Casper Types but in this case it makes sense to use Key.

Copy link
Contributor

Choose a reason for hiding this comment

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

@jonas089 If a contract is the depositor how do they make l2 transactions.

The Key is way to broad, it does not work.

To address an l2 account we cannot use casper account hash.
We need the user's public key.
It would be best if we got the public key from the l1 in the Deposit event.

If we cannot get the public key on the l1 in Deposit We could require the user to send it to the deposit endpoint with the deploy we forward.
But that would require going through the l2 to make a deposit which we might not want.

Making the l2 accounts Casper account hashes would require a lot of work and extra in zk compute.

#[repr(C)]
#[derive(PartialEq, Eq, Clone, Copy, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "datasize", derive(DataSize))]
pub enum Key {
    /// A `Key` under which a user account is stored.
    Account(AccountHash),
    /// A `Key` under which a smart contract is stored and which is the pseudo-hash of the
    /// contract.
    Hash(HashAddr),
    /// A `Key` which is a [`URef`], under which most types of data can be stored.
    URef(URef),
    /// A `Key` under which we store a transfer.
    Transfer(TransferAddr),
    /// A `Key` under which we store a deploy info.
    DeployInfo(DeployHash),
    /// A `Key` under which we store an era info.
    EraInfo(EraId),
    /// A `Key` under which we store a purse balance.
    Balance(URefAddr),
    /// A `Key` under which we store bid information
    Bid(AccountHash),
    /// A `Key` under which we store withdraw information.
    Withdraw(AccountHash),
    /// A `Key` variant whose value is derived by hashing [`URef`]s address and arbitrary data.
    Dictionary(DictionaryAddr),
    /// A `Key` variant under which system contract hashes are stored.
    SystemContractRegistry,
    /// A `Key` under which we store current era info.
    EraSummary,
    /// A `Key` under which we store unbond information.
    Unbond(AccountHash),
    /// A `Key` variant under which chainspec and other hashes are stored.
    ChainspecRegistry,
    /// A `Key` variant under which we store a registry of checksums.
    ChecksumRegistry,
}

Copy link
Contributor

@jonas089 jonas089 May 9, 2024

Choose a reason for hiding this comment

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

There is no way to derive the Public Key from an account hash.

I think your statement that "this doesn't work" is extreme. It definitely works and it might actually be the only solution, it just implies that we will have to hash Public Keys in some places and construct Account Hashs from those Public Keys when parsing the Events.

We can for simplicity restrict it to an account hash, but through call stack we will likely not be able to retrieve a Public Key. I see that this causes computational overhead, but it may not be avoidable unless we find a way to retrieve the Public Key directly from the callstack when the caller is an Account ( and ideally also for another Contract ). I am not aware of such functionality and if none of us find an alternative then I think that we have no choice.

It looks like we will have to use Account Hashs instead of Public Keys for accounting, or at least compute and match the Account Hash when applying Deposits to the L2 state.

Copy link
Contributor

@jonas089 jonas089 May 10, 2024

Choose a reason for hiding this comment

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

But this again is a Casper public key.

Or do we also use Casper public key in the L2?

Copy link
Contributor

Choose a reason for hiding this comment

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

@koxu1996 I don't think we need to send a full signed tx deposit.
The signing of the tx deposit adds no additional guarantees over what the l1 session provides.
I was thinking we would just use tx for withdraw and transfer, and treat deposit separately since it's l1 initiated and simpler.

Copy link
Contributor

@jonas089 jonas089 May 10, 2024

Choose a reason for hiding this comment

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

We should make this decision asap @koxu1996 @Avi-D-coder so that we don't get blocked here. I'd like to merge this PR into the other contract PR because I think it's getting harder to work with when there are 2 PRs for the same issue.

Also we need the kairos-l1-utils crate which is not included on this branch for some reason. I am also confused over the issue that Andrzej pointed out where it looks like this PR is set to merge into Main.

Screenshot 2024-05-10 at 22 50 34

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I agree with Avi I think the easiest would be using the named arg.

@jonas089 @koxu1996 Setting this PR to merge into master serves the purpose of seeing what exact changes would make it in master obviously and to review how the bigger picture would look like.

It moreover triggers the whole pipeline to run since the GHA only run on PRs against master.

Copy link
Contributor

Choose a reason for hiding this comment

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

@Avi-D-coder Okay, I will stick to non-tx deposit.

@marijanp PR is "deposit contract suggestions", so as the name suggests, it should include suggestions/improvements over contract, not the whole contract.

u64::try_from(amount).unwrap_or_else(|_| runtime::revert(ApiError::InvalidArgument));

let new_deposit_record: Deposit = Deposit {
account: get_immediate_caller().unwrap_or_revert(),
Copy link
Contributor

Choose a reason for hiding this comment

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

We only use get_immediate_caller once.
Can we inline it and make it more robust?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It is calling call_stack_element_to_key which is a function that is quite deeper. I would say it is fine to keep it factored out.

@marijanp
Copy link
Collaborator Author

marijanp commented May 9, 2024

What's the idea behind this PR? Is it supposed to replace the other deposit contract PR?

The idea is to get the requested changes incorporated and finallize the deposit contract. After we reach a state that addresses all the problems, the idea is to merge this into the feature/deposit-contract branch.

It would be great if you could answer some of the review comments here.

@marijanp marijanp marked this pull request as ready for review May 9, 2024 18:47
Copy link
Contributor

@jonas089 jonas089 left a comment

Choose a reason for hiding this comment

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

lgtm

@koxu1996
Copy link
Contributor

koxu1996 commented May 10, 2024

What's the idea behind this PR? Is it supposed to replace the other deposit contract PR?

The idea is to get the requested changes incorporated and finallize the deposit contract. After we reach a state that addresses all the problems, the idea is to merge this into the feature/deposit-contract branch.

It would be great if you could answer some of the review comments here.

Sounds good, however I am confused by current target branch:

image

@marijanp Could you update base into feature/deposit-contract?

Right now reviewing PR is more difficult (it includes many more commits), also there is a risk that someone will press "merge" button and merge it directly into main.

@marijanp marijanp changed the base branch from main to feature/deposit-contract May 11, 2024 14:26
@marijanp
Copy link
Collaborator Author

@koxu1996 You are able to change the base yourself. An accidental merge was not possible now until I actually changed the base because there was another approver missing. A merge of this would also include all the changes from @jonas089 branch.

@koxu1996
Copy link
Contributor

@koxu1996 You are able to change the base yourself. An accidental merge was not possible now until I actually changed the base because there was another approver missing. A merge of this would also include all the changes from @jonas089 branch.

@marijanp Well... I think we should take responsibility for our own work - I am glad you changed the base. The approver was missing, because I didn't want to let you merge it directly to main 😉. Even though it includes @jonas089's work, I think Jonas’s PR should be approved and merged - not closed and superseded by this "refactoring" PR.

Copy link
Contributor

@koxu1996 koxu1996 left a comment

Choose a reason for hiding this comment

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

Looks good!

Copy link
Contributor

@Avi-D-coder Avi-D-coder left a comment

Choose a reason for hiding this comment

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

Let's get this merged

@jonas089
Copy link
Contributor

jonas089 commented May 13, 2024

you can merge this, lgtm

edit: I'm done for today and will be back tomorrow morning.

@jonas089
Copy link
Contributor

I resolved the conflicts and the PR is now ready to be merged.

@jonas089
Copy link
Contributor

Is this PR ready to be merged?

@marijanp
Copy link
Collaborator Author

@jonas089 not yet, I'm investigating why the GHA fails

@marijanp marijanp merged commit da68b21 into feature/deposit-contract May 15, 2024
10 checks passed
@marijanp marijanp deleted the deposit-contract-marijan branch May 15, 2024 16:09
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.

4 participants