forked from hyperledger/solang
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Polkadot: Implement
caller_is_root
runtime API (hyperledger#1620)
Recent versions of the contracts pallet expose a runtime API for contracts to check whether their caller is of root origin. The PR exposes this API as a builtin.
- Loading branch information
Showing
13 changed files
with
156 additions
and
15 deletions.
There are no files selected for viewing
This file contains 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 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,14 @@ | ||
import "polkadot"; | ||
|
||
contract CallerIsRoot { | ||
uint public balance; | ||
|
||
function covert() public payable { | ||
if (caller_is_root()) { | ||
balance = 0xdeadbeef; | ||
} else { | ||
print("burn more gas"); | ||
balance = 1; | ||
} | ||
} | ||
} |
This file contains 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,42 @@ | ||
import expect from 'expect'; | ||
import { createConnection, deploy, aliceKeypair, query, weight, transaction } from './index'; | ||
import { ContractPromise } from '@polkadot/api-contract'; | ||
import { ApiPromise } from '@polkadot/api'; | ||
import { KeyringPair } from '@polkadot/keyring/types'; | ||
|
||
describe('Deploy the caller_is_root contract and test it', () => { | ||
let conn: ApiPromise; | ||
let contract: ContractPromise; | ||
let alice: KeyringPair; | ||
|
||
before(async function () { | ||
conn = await createConnection(); | ||
alice = aliceKeypair(); | ||
const instance = await deploy(conn, alice, 'CallerIsRoot.contract', 0n); | ||
contract = new ContractPromise(conn, instance.abi, instance.address); | ||
}); | ||
|
||
after(async function () { | ||
await conn.disconnect(); | ||
}); | ||
|
||
it('is correct on a non-root caller', async function () { | ||
// Without sudo the caller should not be root | ||
const gasLimit = await weight(conn, contract, "covert"); | ||
await transaction(contract.tx.covert({ gasLimit }), alice); | ||
|
||
// Calling `covert` as non-root sets the balance to 1 | ||
const balance = await query(conn, alice, contract, "balance", []); | ||
expect(BigInt(balance.output?.toString() ?? "")).toStrictEqual(1n); | ||
}); | ||
|
||
it('is correct on a root caller', async function () { | ||
// Alice has sudo rights on --dev nodes | ||
const gasLimit = await weight(conn, contract, "covert"); | ||
await transaction(conn.tx.sudo.sudo(contract.tx.covert({ gasLimit })), alice); | ||
|
||
// Calling `covert` as root sets the balance to 0xdeadbeef | ||
const balance = await query(conn, alice, contract, "balance", []); | ||
expect(BigInt(balance.output?.toString() ?? "")).toStrictEqual(0xdeadbeefn); | ||
}); | ||
}); |
This file contains 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 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 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 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 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 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 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 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 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 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