Skip to content

Commit

Permalink
Merge branch 'main' into release
Browse files Browse the repository at this point in the history
  • Loading branch information
xermicus authored Aug 24, 2023
2 parents 840c304 + 8d416ad commit 76c83c1
Show file tree
Hide file tree
Showing 22 changed files with 329 additions and 210 deletions.
2 changes: 1 addition & 1 deletion docs/targets/solana.rst
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ then the seeds and bump have to be provided. There can be multiple seeds, and an
single bump. If the bump is not provided, then the seeds must not create an
account that falls on the curve. When placed above the constructor, the ``@seed`` can be a string literal,
or a hex string with the format ``hex"4142"``. If before an argument, the seed annotation must refer to an argument
of type ``bytes``. The ``@bump`` must a single byte of type ``bytes1``.
of type ``bytes``, ``address``, or fixed length byte array of ``bytesN``. The ``@bump`` must a single byte of type ``bytes1``.

.. _value_transfer:

Expand Down
2 changes: 1 addition & 1 deletion integration/solana/UserStats.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ contract UserStats {
@payer(wallet)
@seed("user-stats")
@space(250)
constructor(@seed bytes user_key, @bump uint8 _bump, string _name, uint16 _level) {
constructor(@seed address user_key, @bump uint8 _bump, string _name, uint16 _level) {
name = _name;
level = _level;
bump = _bump;
Expand Down
14 changes: 7 additions & 7 deletions integration/solana/user_stats.spec.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
// SPDX-License-Identifier: Apache-2.0

import {loadContract} from "./setup";
import {Keypair, PublicKey} from "@solana/web3.js";
import { loadContract } from "./setup";
import { Keypair, PublicKey } from "@solana/web3.js";
import { utils } from '@coral-xyz/anchor';
import expect from "expect";

describe('PDA hash table', function() {
describe('PDA hash table', function () {
// A PDA (Program derived address) hash table is a way to store values for a provided key
// on a unique account on chain, resembling a hash table. This is an example for achieving
// so with Solidity.

it('Table functions', async function test_table() {
const {program, payer} = await loadContract("UserStats");
const { program, payer } = await loadContract("UserStats");
// A user's public key will be the key for the hash table in this example.
const myUser = Keypair.generate();

Expand All @@ -29,10 +29,10 @@ describe('PDA hash table', function() {
// We create the account to hold the user's related information. The generated PDA becomes the
// data account for our contract.
// If a contract for `userStatsPDA` already exists, this function will fail.
await program.methods.new(myUser.publicKey.toBuffer(), bump, "user-one", 25)
await program.methods.new(myUser.publicKey, bump, "user-one", 25)
.accounts({
dataAccount: userStatsPDA,
wallet: payer.publicKey,
dataAccount: userStatsPDA,
wallet: payer.publicKey,
})
.signers([payer])
.rpc();
Expand Down
2 changes: 1 addition & 1 deletion src/codegen/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -933,7 +933,7 @@ impl ControlFlowGraph {
.collect::<Vec<String>>()
.join(", ")
),
Expression::InternalFunctionCfg { cfg_no } => {
Expression::InternalFunctionCfg { cfg_no, .. } => {
format!("function {}", contract.cfg[*cfg_no].name)
}
Expression::ReturnData { .. } => "(external call return data)".to_string(),
Expand Down
4 changes: 2 additions & 2 deletions src/codegen/encoding/scale_encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ fn encode_compact(
Instr::WriteBuffer {
buf: buffer.clone(),
offset: offset.clone(),
value: Expression::Cast {
value: Expression::Trunc {
loc: Codegen,
ty: Uint(8),
expr: mul.clone().into(),
Expand Down Expand Up @@ -371,7 +371,7 @@ fn encode_compact(
Instr::WriteBuffer {
buf: buffer.clone(),
offset: offset.clone(),
value: Expression::Cast {
value: Expression::Trunc {
loc: Codegen,
ty: Uint(16),
expr: mul.into(),
Expand Down
2 changes: 2 additions & 0 deletions src/codegen/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,7 @@ pub fn expression(
ast::Expression::InternalFunction {
function_no,
signature,
ty,
..
} => {
let function_no = if let Some(signature) = signature {
Expand All @@ -495,6 +496,7 @@ pub fn expression(
};

Expression::InternalFunctionCfg {
ty: ty.clone(),
cfg_no: ns.contracts[contract_no].all_functions[function_no],
}
}
Expand Down
16 changes: 9 additions & 7 deletions src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ use solang_parser::{pt, pt::CodeLocation};
// The sizeof(struct account_data_header)
pub const SOLANA_FIRST_OFFSET: u64 = 16;

/// Name of the storage initializer function
pub const STORAGE_INITIALIZER: &str = "storage_initializer";

#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum OptimizationLevel {
None = 0,
Expand Down Expand Up @@ -241,7 +244,8 @@ fn contract(contract_no: usize, ns: &mut Namespace, opt: &Options) {
ns.contracts[contract_no].default_constructor = Some((func, cfg_no));
}

for dispatch_cfg in function_dispatch(contract_no, &all_cfg, ns, opt) {
for mut dispatch_cfg in function_dispatch(contract_no, &all_cfg, ns, opt) {
optimize_and_check_cfg(&mut dispatch_cfg, ns, ASTFunction::None, opt);
all_cfg.push(dispatch_cfg);
}

Expand All @@ -252,10 +256,7 @@ fn contract(contract_no: usize, ns: &mut Namespace, opt: &Options) {
/// This function will set all contract storage initializers and should be called from the constructor
fn storage_initializer(contract_no: usize, ns: &mut Namespace, opt: &Options) -> ControlFlowGraph {
// note the single `:` to prevent a name clash with user-declared functions
let mut cfg = ControlFlowGraph::new(
format!("{}:storage_initializer", ns.contracts[contract_no].name),
ASTFunction::None,
);
let mut cfg = ControlFlowGraph::new(STORAGE_INITIALIZER.to_string(), ASTFunction::None);
let mut vartab = Vartable::new(ns.next_id);

for layout in &ns.contracts[contract_no].layout {
Expand Down Expand Up @@ -464,6 +465,7 @@ pub enum Expression {
expr: Box<Expression>,
},
InternalFunctionCfg {
ty: Type,
cfg_no: usize,
},
Keccak256 {
Expand Down Expand Up @@ -838,7 +840,8 @@ impl RetrieveType for Expression {
| Expression::AllocDynamicBytes { ty, .. }
| Expression::BytesCast { ty, .. }
| Expression::RationalNumberLiteral { ty, .. }
| Expression::Subscript { ty, .. } => ty.clone(),
| Expression::Subscript { ty, .. }
| Expression::InternalFunctionCfg { ty, .. } => ty.clone(),

Expression::BoolLiteral { .. }
| Expression::MoreEqual { .. }
Expand All @@ -858,7 +861,6 @@ impl RetrieveType for Expression {

Expression::AdvancePointer { .. } => Type::BufferPointer,
Expression::FormatString { .. } => Type::String,
Expression::InternalFunctionCfg { .. } => Type::Unreachable,
Expression::Poison => unreachable!("Expression does not have a type"),
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/codegen/strength_reduce/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,10 @@ fn expression_reduce(expr: &Expression, vars: &Variables, ns: &mut Namespace) ->
/// Other types (e.g. bytes) is not relevant for strength reduce. Bools are only
/// tracked so we can following branching after integer compare.
fn track(ty: &Type) -> bool {
matches!(ty, Type::Uint(_) | Type::Int(_) | Type::Bool | Type::Value)
matches!(
ty,
Type::Uint(_) | Type::Int(_) | Type::Bool | Type::Value | Type::UserType(_)
)
}

// A variable can
Expand Down
Loading

0 comments on commit 76c83c1

Please sign in to comment.