-
Notifications
You must be signed in to change notification settings - Fork 2
ROLL-16 Tickets #34
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
Draft
matsakiv
wants to merge
8
commits into
master
Choose a base branch
from
feat/tickets
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
ROLL-16 Tickets #34
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
09a0644
TicketItem for Stack and instruction skeleton
matsakiv 4b3d94a
Implement all instructions, add DUP check
matsakiv a3451e0
Update ticket balance interface
matsakiv 9ac3375
OperationItem
matsakiv 9dceae9
Save tickets updates to context
matsakiv aea7a2c
Move ticket balance updates to tmp state
matsakiv b6ad80e
Implement tickets methods for MockContext
matsakiv 4c63c0f
Implement TicketBalanceDiff
matsakiv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,4 +6,6 @@ bin | |
| target | ||
| tezos_kernel/target | ||
| boot_kernel/target | ||
| .env | ||
| .env | ||
| .bin | ||
| local.env | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,3 +12,4 @@ mod lambda; | |
| mod math; | ||
| mod scope; | ||
| mod stack; | ||
| mod tickets; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,7 +7,11 @@ use std::borrow::Borrow; | |
| use tezos_michelson::michelson::data::instructions::{Dig, Drop, Dug, Dup, Push, Swap}; | ||
|
|
||
| use crate::{ | ||
| err_unsupported, interpreter::PureInterpreter, stack::Stack, types::StackItem, Result, | ||
| err_unsupported, | ||
| interpreter::{Interpreter, PureInterpreter, TicketStorage}, | ||
| stack::Stack, | ||
| types::StackItem, | ||
| Error, InterpreterContext, OperationScope, Result, | ||
| }; | ||
|
|
||
| impl PureInterpreter for Push { | ||
|
|
@@ -18,14 +22,20 @@ impl PureInterpreter for Push { | |
| } | ||
| } | ||
|
|
||
| impl PureInterpreter for Drop { | ||
| fn execute(&self, stack: &mut Stack) -> Result<()> { | ||
| impl Interpreter for Drop { | ||
| fn execute( | ||
| &self, | ||
| stack: &mut Stack, | ||
| scope: &OperationScope, | ||
| context: &mut impl InterpreterContext, | ||
| ) -> Result<()> { | ||
| let count: usize = match &self.n { | ||
| Some(n) => n.try_into()?, | ||
| None => 1, | ||
| }; | ||
| for _ in 0..count { | ||
| stack.pop()?; | ||
| let item = stack.pop()?; | ||
| item.drop_tickets(&scope.self_address, context)?; | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
@@ -42,6 +52,11 @@ impl PureInterpreter for Dup { | |
| } | ||
| // TODO: check if copyable | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Invite you to implement this as well :) |
||
| let res = stack.dup_at(n - 1)?; | ||
|
|
||
| if res.has_tickets() { | ||
| return Err(Error::NonDupableType); // proto.alpha.michelson_v1.non_dupable_type | ||
| } | ||
|
|
||
| stack.push(res) | ||
| } | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| // SPDX-FileCopyrightText: 2023 Baking Bad <hello@bakingbad.dev> | ||
| // | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| use tezos_michelson::michelson::{ | ||
| data::instructions::{JoinTickets, ReadTicket, SplitTicket, Ticket}, | ||
| types, | ||
| }; | ||
|
|
||
| use crate::{ | ||
| err_mismatch, | ||
| interpreter::{Interpreter, InterpreterContext, PureInterpreter}, | ||
| pop_cast, | ||
| stack::Stack, | ||
| typechecker::check_type_comparable, | ||
| types::{AddressItem, OptionItem, PairItem, StackItem, TicketItem}, | ||
| OperationScope, Result, | ||
| }; | ||
|
|
||
| impl Interpreter for Ticket { | ||
| fn execute( | ||
| &self, | ||
| stack: &mut Stack, | ||
| scope: &OperationScope, | ||
| context: &mut impl InterpreterContext, | ||
| ) -> Result<()> { | ||
| let identifier = stack.pop()?; | ||
| let identifier_ty = identifier.get_type()?; | ||
| check_type_comparable(&identifier_ty)?; | ||
|
|
||
| let amount = pop_cast!(stack, Nat); | ||
|
|
||
| if amount.is_zero() { | ||
| let ty = types::ticket(identifier_ty); | ||
| return stack.push(StackItem::Option(OptionItem::None(ty))); | ||
| } | ||
|
|
||
| let ticket = TicketItem { | ||
| source: AddressItem::new(scope.self_address.clone().into()), | ||
| identifier: Box::new(identifier), | ||
| amount: amount.clone(), | ||
| }; | ||
|
|
||
| context.update_ticket_balance( | ||
| &scope.self_address.clone().into(), | ||
| &ticket.identifier.clone().into_micheline(&identifier_ty)?, | ||
| &identifier_ty, | ||
| &scope.self_address.clone().into(), | ||
| amount.value().into(), | ||
| )?; | ||
|
|
||
| stack.push(StackItem::Option(OptionItem::Some(Box::new(ticket.into())))) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is OptionItem::some() |
||
| } | ||
| } | ||
|
|
||
| impl PureInterpreter for ReadTicket { | ||
| fn execute(&self, stack: &mut Stack) -> Result<()> { | ||
| let ticket = pop_cast!(stack, Ticket); | ||
|
|
||
| let pair = PairItem::from_items(vec![ | ||
| ticket.source.clone().into(), | ||
| *ticket.identifier.clone(), | ||
| ticket.amount.clone().into(), | ||
| ])?; | ||
|
|
||
| stack.push(ticket.into())?; // return ticket back to stack | ||
| stack.push(pair.into()) | ||
| } | ||
| } | ||
|
|
||
| impl PureInterpreter for SplitTicket { | ||
| fn execute(&self, stack: &mut Stack) -> Result<()> { | ||
| let ticket = pop_cast!(stack, Ticket); // ticket | ||
| let pair_n1_n2 = pop_cast!(stack, Pair); // pair nat nat | ||
|
|
||
| let (n1, n2) = match pair_n1_n2.unpair() { | ||
| (StackItem::Nat(n1), StackItem::Nat(n2)) => (n1, n2), | ||
| (s1, s2) => { | ||
| return err_mismatch!("Pair Nat Nat", StackItem::Pair(PairItem::new(s1, s2))) | ||
| } | ||
| }; | ||
|
|
||
| if n1.is_zero() || n2.is_zero() || n1.clone() + n2.clone() != ticket.amount { | ||
| let ty = types::pair(vec![types::nat(), types::nat()]); | ||
| return stack.push(StackItem::Option(OptionItem::None(ty))); | ||
| } | ||
|
|
||
| let ticket_1 = TicketItem { | ||
| source: ticket.source.clone(), | ||
| identifier: ticket.identifier.clone(), | ||
| amount: n1, | ||
| }; | ||
| let ticket_2 = TicketItem { | ||
| source: ticket.source, | ||
| identifier: ticket.identifier, | ||
| amount: n2, | ||
| }; | ||
| let pair = PairItem::new(ticket_1.into(), ticket_2.into()); | ||
|
|
||
| stack.push(StackItem::Option(OptionItem::Some(Box::new(pair.into())))) | ||
| } | ||
| } | ||
|
|
||
| impl PureInterpreter for JoinTickets { | ||
| fn execute(&self, stack: &mut Stack) -> Result<()> { | ||
| let tickets = pop_cast!(stack, Pair); // tickets pair | ||
| let (ticket_1, ticket_2) = match tickets.unpair() { | ||
| (StackItem::Ticket(ticket_1), StackItem::Ticket(ticket_2)) => (ticket_1, ticket_2), | ||
| (s1, s2) => { | ||
| return err_mismatch!("Pair Ticket Ticket", StackItem::Pair(PairItem::new(s1, s2))) | ||
| } | ||
| }; | ||
|
|
||
| if ticket_1.source != ticket_2.source || *ticket_1.identifier != *ticket_2.identifier { | ||
| let ty = types::ticket(ticket_1.identifier.get_type()?); | ||
| return stack.push(StackItem::Option(OptionItem::None(ty))); | ||
| } | ||
|
|
||
| let ticket = TicketItem { | ||
| source: ticket_1.source, | ||
| identifier: ticket_1.identifier, | ||
| amount: ticket_1.amount + ticket_2.amount, | ||
| }; | ||
|
|
||
| stack.push(StackItem::Option(OptionItem::Some(Box::new(ticket.into())))) | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd just reuse
pending_state, the only difference is that for tmp values you do not updatemodified_keys.You can also implement that with existing
get/setmethods by adding flag