Skip to content

Commit

Permalink
Merge pull request #1197 from multiversx/add-get-shard-of-address
Browse files Browse the repository at this point in the history
vm: Add get_shard_of_address
  • Loading branch information
andrei-marinica authored Aug 18, 2023
2 parents cb907d2 + 7f8cca5 commit 99c642d
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
"name": "get caller",
"gasSchedule": "v3",
"steps": [
{
"step": "setState",
"accounts": {
"sc:basic-features": {
"nonce": "0",
"balance": "0",
"code": "file:../output/basic-features.wasm"
},
"address:an_account": {
"nonce": "0",
"balance": "0"
}
}
},
{
"step": "scCall",
"id": "get_shard_of_address",
"tx": {
"from": "address:an_account",
"to": "sc:basic-features",
"function": "get_shard_of_address",
"arguments": ["sc:basic-features"],
"gasLimit": "50,000,000",
"gasPrice": "0"
},
"expect": {
"out": [
"2"
],
"status": "",
"logs": "*",
"gas": "*",
"refund": "*"
}
},
{
"step": "scQuery",
"id": "get_shard_of_address_query",
"tx": {
"to": "sc:basic-features",
"function": "get_shard_of_address",
"arguments": ["sc:basic-features"]
},
"expect": {
"out": [
"2"
],
"status": "",
"logs": "*",
"gas": "*",
"refund": "*"
}
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,12 @@ fn get_cumulated_validator_rewards_go() {
world().run("scenarios/get_cumulated_validator_rewards.scen.json");
}

#[test]
#[ignore = "TODO: missing support from scenario-go"]
fn get_shard_of_address_go() {
world().run("scenarios/get_shard_of_address.scen.json");
}

#[test]
fn managed_address_array_go() {
world().run("scenarios/managed_address_array.scen.json");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,11 @@ fn get_cumulated_validator_rewards_rs() {
world().run("scenarios/get_cumulated_validator_rewards.scen.json");
}

#[test]
fn get_shard_of_address_rs() {
world().run("scenarios/get_shard_of_address.scen.json");
}

#[test]
fn managed_address_array_rs() {
world().run("scenarios/managed_address_array.scen.json");
Expand Down
6 changes: 5 additions & 1 deletion vm/src/vm_hooks/vh_dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ impl VMHooks for VMHooksDispatcher {
}

fn get_shard_of_address(&self, address_offset: MemPtr) -> i32 {
panic!("Unavailable: get_shard_of_address")
unsafe {
mem_conv::with_bytes(address_offset, 32, |address_bytes| {
self.handler.get_shard_of_address(address_bytes)
})
}
}

fn is_smart_contract(&self, address_offset: MemPtr) -> i32 {
Expand Down
4 changes: 4 additions & 0 deletions vm/src/vm_hooks/vh_handler/vh_blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ pub trait VMHooksBlockchain: VMHooksHandlerSource {
);
}

fn get_shard_of_address(&self, address_bytes: &[u8]) -> i32 {
(address_bytes[address_bytes.len() - 1] % 3).into()
}

fn is_smart_contract(&self, address_bytes: &[u8]) -> bool {
VMAddress::from_slice(address_bytes).is_smart_contract_address()
}
Expand Down

0 comments on commit 99c642d

Please sign in to comment.