Skip to content

Commit

Permalink
Bump should be hashed by last (hyperledger-solang#1440)
Browse files Browse the repository at this point in the history
Presently, we hash include the bump in the `seeds` array passed to a
Solana runtime call in the same order specified by developers when they
are writing the contract's constructor. This is not the intended
behavior, though. The bump must always be the last element in the array.
This PR ensures that.

Signed-off-by: Lucas Steuernagel <lucas.tnagel@gmail.com>
  • Loading branch information
LucasSte authored Jul 14, 2023
1 parent 15cc63e commit 08128df
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/codegen/solana_deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,7 @@ pub(super) fn solana_deploy(

// seeds
let mut seeds = Vec::new();
let mut declared_bump = None;

for note in &func.annotations {
match note {
Expand All @@ -486,13 +487,16 @@ pub(super) fn solana_deploy(
}
.into(),
};

seeds.push(expression(&expr, cfg, contract_no, None, ns, vartab, opt));
declared_bump = Some(expr);
}
_ => (),
}
}

if let Some(bump) = declared_bump {
seeds.push(expression(&bump, cfg, contract_no, None, ns, vartab, opt));
}

let seeds = if !seeds.is_empty() {
let ty = Type::Array(
Box::new(Type::Slice(Box::new(Type::Bytes(1)))),
Expand Down
40 changes: 40 additions & 0 deletions tests/codegen_testcases/solidity/solana_bump.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// RUN: --target solana --emit cfg

contract C1 {
@payer(payer)
@space(57)
@bump(25)
constructor(@seed bytes my_seed) {
print("In C1");
}
// BEGIN-CHECK: solang_dispatch
// 25 must be the last seed in the call.
// CHECK: external call::regular address:address 0x0 payload:%instruction.temp.14 value:uint64 0 gas:uint64 0 accounts:%metas.temp.11 seeds:[1] [ [2] [ bytes(%my_seed), bytes(bytes from:bytes1 (bytes1 25)) ] ] contract|function:_ flags:
}

contract C2 {
@payer(payer)
@space(57)
@seed("apple")
@bump(12)
@seed("pine_tree")
constructor(@seed bytes my_seed) {
print("In C2");
}
// BEGIN-CHECK: solang_dispatch
// 12 must be the last seed in the call.
// CHECK: external call::regular address:address 0x0 payload:%instruction.temp.25 value:uint64 0 gas:uint64 0 accounts:%metas.temp.22 seeds:[1] [ [4] [ (alloc slice bytes1 uint32 5 "apple"), (alloc slice bytes1 uint32 9 "pine_tree"), bytes(%my_seed), bytes(bytes from:bytes1 (bytes1 12)) ] ] contract|function:_ flags:
}

contract C3 {
@payer(payer)
@space(57)
@seed("pineapple")
@seed("avocado")
constructor(@bump uint8 bp, @seed bytes my_seed) {
print("In C3");
}
// BEGIN-CHECK: solang_dispatch
// bp must be the last seed in the call
// CHECK: external call::regular address:address 0x0 payload:%instruction.temp.37 value:uint64 0 gas:uint64 0 accounts:%metas.temp.34 seeds:[1] [ [4] [ (alloc slice bytes1 uint32 9 "pineapple"), (alloc slice bytes1 uint32 7 "avocado"), bytes(%my_seed), bytes(bytes from:bytes1 (%bp)) ] ] contract|function:_ flags:
}

0 comments on commit 08128df

Please sign in to comment.