mirror of
https://github.com/AleoHQ/leo.git
synced 2024-11-24 02:42:21 +03:00
Update examples to use transition keyword
This commit is contained in:
parent
e7e01b23a9
commit
a886357585
@ -19,8 +19,7 @@ record Bid {
|
||||
// Requires that `bidder` matches the function caller.
|
||||
// The owner of the record is set to the entity responsible for running the auction (auction runner).
|
||||
// The address of the auction runner is aleo1fxs9s0w97lmkwlcmgn0z3nuxufdee5yck9wqrs0umevp7qs0sg9q5xxxzh.
|
||||
@program
|
||||
function place_bid(bidder: address, amount: u64) -> Bid {
|
||||
transition place_bid(bidder: address, amount: u64) -> Bid {
|
||||
// Ensure the caller is the auction bidder.
|
||||
console.assert_eq(self.caller, bidder);
|
||||
// Return a new 'Bid' record for the auction bidder.
|
||||
@ -39,8 +38,7 @@ function place_bid(bidder: address, amount: u64) -> Bid {
|
||||
// Requires that the function caller is the auction runner.
|
||||
// Assumes that the function is invoked only after the bidding period has ended.
|
||||
// In the event of a tie, the first bid is selected.
|
||||
@program
|
||||
function resolve(first: Bid, second: Bid) -> Bid {
|
||||
transition resolve(first: Bid, second: Bid) -> Bid {
|
||||
// Ensure the caller is the auctioneer.
|
||||
console.assert_eq(self.caller, aleo1fxs9s0w97lmkwlcmgn0z3nuxufdee5yck9wqrs0umevp7qs0sg9q5xxxzh);
|
||||
// Resolve the winner of the auction.
|
||||
@ -55,8 +53,7 @@ function resolve(first: Bid, second: Bid) -> Bid {
|
||||
// - `bid` : The winning bid.
|
||||
// Requires that the function caller is the auction runner.
|
||||
// Assumes that the function is invoked only after all bids have been resolved.
|
||||
@program
|
||||
function finish(bid: Bid) -> Bid {
|
||||
transition finish(bid: Bid) -> Bid {
|
||||
// Ensure the caller is the auctioneer.
|
||||
console.assert_eq(self.caller, aleo1fxs9s0w97lmkwlcmgn0z3nuxufdee5yck9wqrs0umevp7qs0sg9q5xxxzh);
|
||||
// Return 'is_winner' as 'true' in the winning 'Bid'.
|
||||
|
@ -17,8 +17,7 @@ mapping balances: field => u64;
|
||||
// - `amount`: The amount of tokens to issue.
|
||||
// Requires that the function caller is the bank.
|
||||
// The bank's address is aleo1t0uer3jgtsgmx5tq6x6f9ecu8tr57rzzfnc2dgmcqldceal0ls9qf6st7a.
|
||||
@program
|
||||
function issue(owner: address, amount: u64) -> Token {
|
||||
transition issue(owner: address, amount: u64) -> Token {
|
||||
console.assert_eq(self.caller, aleo1t0uer3jgtsgmx5tq6x6f9ecu8tr57rzzfnc2dgmcqldceal0ls9qf6st7a);
|
||||
return Token {
|
||||
owner: owner,
|
||||
@ -31,8 +30,7 @@ function issue(owner: address, amount: u64) -> Token {
|
||||
// Returns a new Token with the remaining amount of money.
|
||||
// - `token` : A record containing tokens to deposit.
|
||||
// - `amount`: The amount of tokens to deposit.
|
||||
@program
|
||||
function deposit(token: Token, amount: u64) -> Token {
|
||||
transition deposit(token: Token, amount: u64) -> Token {
|
||||
let difference: u64 = token.amount - amount;
|
||||
|
||||
let remaining: Token = Token {
|
||||
@ -62,8 +60,7 @@ finalize deposit(hash: field, amount: u64) {
|
||||
// - `rate` : The compound interest rate.
|
||||
// - `periods` : The number of periods to compound the interest over.
|
||||
// Requires that the function caller is the bank.
|
||||
@program
|
||||
function withdraw(recipient: address, amount: u64, rate: u64, periods: u64) -> Token {
|
||||
transition withdraw(recipient: address, amount: u64, rate: u64, periods: u64) -> Token {
|
||||
console.assert_eq(self.caller, aleo1t0uer3jgtsgmx5tq6x6f9ecu8tr57rzzfnc2dgmcqldceal0ls9qf6st7a);
|
||||
let hash: field = BHP256::hash(recipient);
|
||||
|
||||
|
@ -20,8 +20,7 @@ record board_state {
|
||||
}
|
||||
|
||||
// Returns a new board_state.
|
||||
@program
|
||||
function new_board_state(
|
||||
transition new_board_state(
|
||||
ships: u64,
|
||||
opponent: address,
|
||||
) -> board_state {
|
||||
@ -39,8 +38,7 @@ function new_board_state(
|
||||
|
||||
// Returns a new board state that has been started.
|
||||
// Fails if this board has been started before.
|
||||
@program
|
||||
function start_board(
|
||||
transition start_board(
|
||||
// The record of the board to start. A board can only be started once.
|
||||
board: board_state,
|
||||
) -> board_state {
|
||||
@ -61,8 +59,7 @@ function start_board(
|
||||
|
||||
// Returns a new board state record that includes all the played tiles.
|
||||
// Fails if r1 has been played before.
|
||||
@program
|
||||
function update_played_tiles(
|
||||
transition update_played_tiles(
|
||||
// The record of the board to update.
|
||||
board: board_state,
|
||||
// The u64 equivalent of a bitstring fire coordinate to send to the opponent.
|
||||
@ -94,8 +91,7 @@ function update_played_tiles(
|
||||
}
|
||||
|
||||
// Returns a new board state record that includes all the hits and misses.
|
||||
@program
|
||||
function update_hits_and_misses(
|
||||
transition update_hits_and_misses(
|
||||
// The record of the board to update.
|
||||
board: board_state,
|
||||
// The u64 equivalent of a bitstring of whether this player's previous move was a hit or miss.
|
||||
|
@ -9,8 +9,7 @@ record move {
|
||||
}
|
||||
|
||||
// Returns new move record owned by the opponent.
|
||||
@program
|
||||
function create_move(
|
||||
transition create_move(
|
||||
// The move record created by the opponent.
|
||||
move_record: move,
|
||||
// The u64 representation of incoming_fire_coordinate, the bitstring fire coordinate to send to the opponent.
|
||||
@ -34,8 +33,7 @@ function create_move(
|
||||
|
||||
// Returns the move record owned by the opponent.
|
||||
// Note, this move record contains dummy fire coordinates and previous hit or miss.
|
||||
@program
|
||||
function start_game(player_2: address) -> move {
|
||||
transition start_game(player_2: address) -> move {
|
||||
return move {
|
||||
owner: player_2,
|
||||
gates: 0u64,
|
||||
|
@ -74,8 +74,7 @@ function horizontal_check(
|
||||
}
|
||||
|
||||
// Returns `true` if the ship placement is valid.
|
||||
@program
|
||||
function validate_ship(
|
||||
transition validate_ship(
|
||||
// The u64 representation of a ship's placement in an 8x8 grid.
|
||||
ship: u64,
|
||||
// The length of the placed ship.
|
||||
@ -104,8 +103,7 @@ function validate_ship(
|
||||
|
||||
// Returns the u64 representation of all the ships' placements in an 8x8 grid. This function will fail
|
||||
// if any of the ship placements overlap each other.
|
||||
@program
|
||||
function create_board(
|
||||
transition create_board(
|
||||
// The u64 representation of a carrier's placement in an 8x8 grid. Length = 5.
|
||||
carrier: u64,
|
||||
// The u64 representation of a battleship's placement in an 8x8 grid. Length = 4.
|
||||
|
@ -4,8 +4,7 @@ import move.leo;
|
||||
import verify.leo;
|
||||
|
||||
// Returns a new record representing a new game of battleship.
|
||||
@program
|
||||
function initialize_board(
|
||||
transition initialize_board(
|
||||
// The u64 representation of a carrier's placement in an 8x8 grid. Length = 5.
|
||||
carrier: u64,
|
||||
// The u64 representation of a battleship's placement in an 8x8 grid. Length = 4.
|
||||
@ -42,8 +41,7 @@ function initialize_board(
|
||||
// Returns an updated board state record that has been started. This board cannot be used to start any other games.
|
||||
// Returns a dummy move record owned by the opponent.
|
||||
// This function commits a given board to a game with an opponent and creates the initial dummy move.
|
||||
@program
|
||||
function offer_battleship(
|
||||
transition offer_battleship(
|
||||
// The board record to start a game with.
|
||||
board: board.leo/board_state.record,
|
||||
) -> (board.leo/board_state.record, move.leo/move.record) {
|
||||
@ -55,8 +53,7 @@ function offer_battleship(
|
||||
|
||||
// Returns updated board_state.record that has been started and can no longer be used to join or start new games.
|
||||
// Returns dummy move record owned by the opponent.
|
||||
@program
|
||||
function start_battleship(
|
||||
transition start_battleship(
|
||||
// The board record to play the game with.
|
||||
board: board.leo/board_state.record,
|
||||
// The move record to play to begin the game. This should be the dummy move record created from offer_battleship.
|
||||
@ -74,8 +71,7 @@ function start_battleship(
|
||||
|
||||
// Returns updated board record.
|
||||
// Returns new move record owned by opponent.
|
||||
@program
|
||||
function play(
|
||||
transition play(
|
||||
// The board record to update.
|
||||
board: board.leo/board_state.record,
|
||||
// The incoming move from the opponent.
|
||||
|
@ -16,8 +16,7 @@
|
||||
// Note that the implementation below uses tuples instead of arrays.
|
||||
// The implementation also manually unrolls the loop.
|
||||
|
||||
@program
|
||||
function bubble_sort(
|
||||
transition bubble_sort(
|
||||
arr0: u32,
|
||||
arr1: u32,
|
||||
arr2: u32,
|
||||
|
@ -2,8 +2,7 @@
|
||||
// Core functions are built-in to the Leo language and call handwritten, optimized circuits in the AVM.
|
||||
// To call a core function, use the correct capitalized identifier followed by two colons
|
||||
// and then the function name. Example: `Pedersen64::hash()`.
|
||||
@program
|
||||
function main(a: field) -> field {
|
||||
transition main(a: field) -> field {
|
||||
let b: field = BHP256::hash(a);
|
||||
let c: field = Poseidon2::hash(b);
|
||||
let d: field = BHP256::commit(c, 1scalar);
|
||||
|
@ -1,8 +1,7 @@
|
||||
// This function takes a group coordinate as input `a` and performs several operations which should output the `0group`.
|
||||
// Note that the operations can be called as associated functions on the `a` variable.
|
||||
|
||||
@program
|
||||
function main(a: group) -> group {
|
||||
transition main(a: group) -> group {
|
||||
// unary
|
||||
let e: group = a.double(); // 2a
|
||||
let g: group = e.neg(); // -2a
|
||||
|
@ -1,7 +1,6 @@
|
||||
// The 'ntzdebruijn' main function.
|
||||
// From Hacker's Delight 2nd ed. figure 5-26
|
||||
@program
|
||||
function main(public x: u32) -> u8 {
|
||||
transition main(public x: u32) -> u8 {
|
||||
if x == 0u32 {return 32u8;}
|
||||
// 0x04D7651F = 81224991
|
||||
x = (x & 0u32.sub_wrapped(x)).mul_wrapped(81224991u32);
|
||||
|
@ -1,7 +1,6 @@
|
||||
// The 'ntzgaudet' main function.
|
||||
// From Hacker's Delight 2nd ed. figure 5-24
|
||||
@program
|
||||
function main(public x: u32) -> u8 {
|
||||
transition main(public x: u32) -> u8 {
|
||||
let y: u32 = x & 0u32.sub_wrapped(x); // Isolate rightmost 1-bit
|
||||
let bz: u8 = (y != 0u32) ? 0u8 : 1u8;
|
||||
// 0x0000FFFF = 65535
|
||||
|
@ -1,7 +1,6 @@
|
||||
// The 'ntzloops' main function.
|
||||
// From Hacker's Delight 2nd ed. figure 5-23
|
||||
@program
|
||||
function main(public x: u32) -> u8 {
|
||||
transition main(public x: u32) -> u8 {
|
||||
x = !x & x.sub_wrapped(1u32);
|
||||
let n: u8 = 0u8;
|
||||
for i:u8 in 0u8..32u8 {
|
||||
|
@ -1,7 +1,6 @@
|
||||
// The 'ntzmasks' main function.
|
||||
// From Hacker's Delight 2nd ed. figure 5-20
|
||||
@program
|
||||
function main(public x: u32) -> u8 {
|
||||
transition main(public x: u32) -> u8 {
|
||||
if (x == 0u32) {return 32u8;}
|
||||
let n: u8 = 1u8;
|
||||
// x >>= 16u8 wasn't working, and I don't want to use
|
||||
|
@ -1,7 +1,6 @@
|
||||
// The 'ntzreisers' main function.
|
||||
// From Hacker's Delight 2nd ed. figure 5-27
|
||||
@program
|
||||
function main(public x: u32) -> u8 {
|
||||
transition main(public x: u32) -> u8 {
|
||||
x = (x & 0u32.sub_wrapped(x)).rem_wrapped(37u32);
|
||||
return reisersTableLookup(x);
|
||||
}
|
||||
|
@ -1,7 +1,6 @@
|
||||
// The 'nztseals' main function.
|
||||
// From Hacker's Delight 2nd ed. figure 5-25
|
||||
@program
|
||||
function main(public x: u32) -> u8 {
|
||||
transition main(public x: u32) -> u8 {
|
||||
// 0x0450FBAF = 72416175
|
||||
x = (x & 0u32.sub_wrapped(x)).mul_wrapped(72416175u32);
|
||||
return sealsTableLookup(x >> 26u8);
|
||||
|
@ -1,8 +1,7 @@
|
||||
// The 'ntzsearchtree' main function.
|
||||
// From Hacker's Delight 2nd ed. figure 5-22,
|
||||
// expanded to a 32-bit version.
|
||||
@program
|
||||
function main(public x: u32) -> u8 {
|
||||
transition main(public x: u32) -> u8 {
|
||||
if (x & 65535u32 != 0u32) {
|
||||
if (x & 255u32 != 0u32) {
|
||||
if (x & 15u32 != 0u32) {
|
||||
|
@ -1,7 +1,6 @@
|
||||
// The 'ntzsmallvals' main function.
|
||||
// From Hacker's Delight 2nd ed. figure 5-21
|
||||
@program
|
||||
function main(public x: u32) -> u8 {
|
||||
transition main(public x: u32) -> u8 {
|
||||
if (x == 0u32) {return 32u8;}
|
||||
let n: u8 = 31u8;
|
||||
let y: u32 = x.shl_wrapped(16u8); if (y != 0u32) {n = n - 16u8; x = y;}
|
||||
|
@ -1,5 +1,4 @@
|
||||
// The 'helloworld' main function.
|
||||
@program
|
||||
function main(public a: u32, b: u32) -> u32 {
|
||||
transition main(public a: u32, b: u32) -> u32 {
|
||||
return a + b;
|
||||
}
|
||||
|
@ -1,7 +1,6 @@
|
||||
// This function calculates the interest accrued
|
||||
// over ten iterations for some `capital` and `rate`.
|
||||
@program
|
||||
function fixed_iteration_interest(capital: u32, public rate: u32) -> u32 {
|
||||
transition fixed_iteration_interest(capital: u32, public rate: u32) -> u32 {
|
||||
let amount: u32 = capital;
|
||||
|
||||
// Accrue for exactly 10 iterations.
|
||||
@ -15,8 +14,7 @@ function fixed_iteration_interest(capital: u32, public rate: u32) -> u32 {
|
||||
|
||||
// This function calculates the interest accrued
|
||||
// over a variable number of iterations (max 50) for some `capital` and `rate`.
|
||||
@program
|
||||
function bounded_iteration_interest(capital: u32,
|
||||
transition bounded_iteration_interest(capital: u32,
|
||||
public rate: u32,
|
||||
iterations: u8) -> u32 {
|
||||
console.assert(iterations <= 50u8);
|
||||
|
@ -10,8 +10,7 @@ struct Message {
|
||||
|
||||
// The "main" function of this Leo program takes a "Message" struct type as input.
|
||||
// To see how to input variable "m" is passed in open up `inputs/message.in`.
|
||||
@program
|
||||
function main(m: Message) -> field {
|
||||
transition main(m: Message) -> field {
|
||||
|
||||
// 1. Define the "Message" type.
|
||||
// 2. Use brackets `{ }` to enclose the struct members.
|
||||
|
@ -12,8 +12,7 @@ record Token {
|
||||
|
||||
// The `mint` function initializes a new record with the
|
||||
// specified number of tokens assigned to the specified receiver.
|
||||
@program
|
||||
function mint(owner: address, amount: u64) -> Token {
|
||||
transition mint(owner: address, amount: u64) -> Token {
|
||||
return Token {
|
||||
owner: owner,
|
||||
gates: 0u64,
|
||||
@ -23,8 +22,7 @@ function mint(owner: address, amount: u64) -> Token {
|
||||
|
||||
// The `transfer` function sends the specified number of tokens
|
||||
// to the receiver from the provided token record.
|
||||
@program
|
||||
function transfer(token: Token, to: address, amount: u64) -> (Token, Token) {
|
||||
transition transfer(token: Token, to: address, amount: u64) -> (Token, Token) {
|
||||
|
||||
// Checks the given token record has sufficient balance.
|
||||
// This `sub` operation is safe, and the proof will fail
|
||||
|
@ -21,8 +21,7 @@ struct Board {
|
||||
}
|
||||
|
||||
// Returns an empty board.
|
||||
@program
|
||||
function new() -> Board {
|
||||
transition new() -> Board {
|
||||
return Board {
|
||||
r1: Row { c1: 0u8, c2: 0u8, c3: 0u8 },
|
||||
r2: Row { c1: 0u8, c2: 0u8, c3: 0u8 },
|
||||
@ -54,8 +53,7 @@ function check_for_win(b: Board, p: u8) -> bool {
|
||||
// Assumes that `player` is either 1 or 2.
|
||||
// Assumes that `row` and `col` are valid indices into the board.
|
||||
// If an entry is already occupied, the move is invalid and the board is returned unchanged.
|
||||
@program
|
||||
function make_move(player: u8, row: u8, col: u8, board: Board) -> (Board, u8) {
|
||||
transition make_move(player: u8, row: u8, col: u8, board: Board) -> (Board, u8) {
|
||||
// Check that inputs are valid.
|
||||
console.assert(player == 1u8 || player == 2u8);
|
||||
console.assert(1u8 <= row && row <= 3u8);
|
||||
|
@ -14,8 +14,7 @@ record token {
|
||||
/* Mint */
|
||||
|
||||
// The function `mint_public` issues the specified token amount for the token receiver publicly on the network.
|
||||
@program
|
||||
function mint_public(public receiver: address, public amount: u64) {
|
||||
transition mint_public(public receiver: address, public amount: u64) {
|
||||
// Mint the tokens publicly by invoking the computation on-chain.
|
||||
async finalize(receiver, amount);
|
||||
}
|
||||
@ -28,8 +27,7 @@ finalize mint_public(public receiver: address, public amount: u64) {
|
||||
}
|
||||
|
||||
// The function `mint_private` initializes a new record with the specified amount of tokens for the receiver.
|
||||
@program
|
||||
function mint_private(receiver: address, amount: u64) -> token {
|
||||
transition mint_private(receiver: address, amount: u64) -> token {
|
||||
return token {
|
||||
owner: receiver,
|
||||
gates: 0u64,
|
||||
@ -38,8 +36,7 @@ function mint_private(receiver: address, amount: u64) -> token {
|
||||
}
|
||||
|
||||
/* Transfer */
|
||||
@program
|
||||
function transfer_public(public receiver: address, public amount: u64) {
|
||||
transition transfer_public(public receiver: address, public amount: u64) {
|
||||
// Transfer the tokens publicly, by invoking the computation on-chain.
|
||||
async finalize(self.caller, receiver, amount);
|
||||
}
|
||||
@ -56,8 +53,7 @@ finalize transfer_public(public sender: address, public receiver: address, publi
|
||||
}
|
||||
|
||||
// The function `transfer_private` sends the specified token amount to the token receiver from the specified token record.
|
||||
@program
|
||||
function transfer_private(sender: token, receiver: address, amount: u64) -> (token, token) {
|
||||
transition transfer_private(sender: token, receiver: address, amount: u64) -> (token, token) {
|
||||
// Checks the given token record has sufficient balance.
|
||||
// This `sub` operation is safe, and the proof will fail if an overflow occurs.
|
||||
// `difference` holds the change amount to be returned to sender.
|
||||
@ -83,8 +79,7 @@ function transfer_private(sender: token, receiver: address, amount: u64) -> (tok
|
||||
|
||||
// The function `transfer_private_to_public` turns a specified token amount from a token record into public tokens for the specified receiver.
|
||||
// This function preserves privacy for the sender's record, however it publicly reveals the token receiver and the token amount.
|
||||
@program
|
||||
function transfer_private_to_public(sender: token, public receiver: address, public amount: u64) -> token {
|
||||
transition transfer_private_to_public(sender: token, public receiver: address, public amount: u64) -> token {
|
||||
// Checks the given token record has a sufficient token amount.
|
||||
// This `sub` operation is safe, and the proof will fail if an underflow occurs.
|
||||
// `difference` holds the change amount for the caller.
|
||||
@ -113,8 +108,7 @@ finalize transfer_private_to_public(public receiver: address, public amount: u64
|
||||
|
||||
// The function `transfer_public_to_private` turns a specified token amount from `account` into a token record for the specified receiver.
|
||||
// This function preserves privacy for the receiver's record, however it publicly reveals the caller and the specified token amount.
|
||||
@program
|
||||
function transfer_public_to_private(public receiver: address, public amount: u64) -> token {
|
||||
transition transfer_public_to_private(public receiver: address, public amount: u64) -> token {
|
||||
// Produces a token record for the token receiver.
|
||||
let transferred: token = token {
|
||||
owner: receiver,
|
||||
|
@ -1,7 +1,6 @@
|
||||
// This function calculates the number of powers of two ("twoadicity")
|
||||
// in the prime factorization of the input number `n`.
|
||||
@program
|
||||
function main(public n: field) -> u8 {
|
||||
transition main(public n: field) -> u8 {
|
||||
let remaining_n: field = n;
|
||||
let powers_of_two: u8 = 0u8;
|
||||
// Since field ints are 253 bits or fewer, any number in the field
|
||||
|
@ -33,8 +33,7 @@ mapping agree_votes: field => u64;
|
||||
mapping disagree_votes: field => u64;
|
||||
|
||||
// Propose a new proposal to vote on.
|
||||
@program
|
||||
function propose(public info: ProposalInfo) -> Proposal {
|
||||
transition propose(public info: ProposalInfo) -> Proposal {
|
||||
// Authenticate proposer.
|
||||
console.assert_eq(self.caller, info.proposer);
|
||||
|
||||
@ -58,8 +57,7 @@ finalize propose(public id: field) {
|
||||
}
|
||||
|
||||
// Create a new ticket to vote with.
|
||||
@program
|
||||
function new_ticket(
|
||||
transition new_ticket(
|
||||
public pid: field,
|
||||
public voter: address,
|
||||
) -> Ticket {
|
||||
@ -78,8 +76,7 @@ finalize new_ticket(public pid: field) {
|
||||
}
|
||||
|
||||
// Vote privately to agree with a proposal.
|
||||
@program
|
||||
function agree(ticket: Ticket) {
|
||||
transition agree(ticket: Ticket) {
|
||||
// Finalize this vote.
|
||||
async finalize(ticket.pid);
|
||||
}
|
||||
@ -89,8 +86,7 @@ finalize agree(public pid: field) {
|
||||
}
|
||||
|
||||
// Vote privately to disagree with a proposal.
|
||||
@program
|
||||
function disagree(ticket: Ticket) {
|
||||
transition disagree(ticket: Ticket) {
|
||||
// Finalize this vote.
|
||||
async finalize(ticket.pid);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user