forked from hyperledger-solang/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: Support errors according to
solc
(hyperledger-solang#1516)
Implement `Panic` and custom errors for the Polkadot target: - Allow catching `Panic` in sema - Allow custom errors in sema - Implement selector calculation for custom errors - Refactor try-catch in codegen to allow catch clauses on `Panic` errors - Add any custom errors in the metadata
- Loading branch information
Showing
28 changed files
with
711 additions
and
234 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
contract TryCatchCaller { | ||
constructor() payable {} | ||
|
||
function test(uint128 div) public payable returns (uint128) { | ||
TryCatchCallee instance = new TryCatchCallee(); | ||
|
||
try instance.test(div) returns (uint128) { | ||
return 4; | ||
} catch Error(string reason) { | ||
assert(reason == "foo"); | ||
return 1; | ||
} catch Panic(uint reason) { | ||
assert(reason == 0x12); | ||
return 0; | ||
} catch (bytes raw) { | ||
if (raw.length == 0) { | ||
return 3; | ||
} | ||
if (raw == hex"bfb4ebcf") { | ||
return 2; | ||
} | ||
} | ||
|
||
assert(false); | ||
} | ||
} | ||
|
||
contract TryCatchCallee { | ||
error Foo(); | ||
|
||
// div = 0: Reverts with Panic error | ||
// div = 1: Reverts with Error error | ||
// div = 2: Reverts with Foo error | ||
// div = 3: Reverts with empty error | ||
// div > 3: Doesn't revert | ||
function test(uint128 div) public pure returns (uint128) { | ||
if (div == 1) { | ||
revert("foo"); | ||
} | ||
if (div == 2) { | ||
revert Foo(); | ||
} | ||
if (div == 3) { | ||
revert(); | ||
} | ||
return 123 / div; | ||
} | ||
} |
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,35 @@ | ||
import expect from 'expect'; | ||
import { createConnection, deploy, aliceKeypair, query, } from './index'; | ||
import { ContractPromise } from '@polkadot/api-contract'; | ||
import { ApiPromise } from '@polkadot/api'; | ||
import { KeyringPair } from '@polkadot/keyring/types'; | ||
|
||
|
||
describe('Deploy and test the try_catch contract', () => { | ||
let conn: ApiPromise; | ||
let caller: ContractPromise; | ||
let alice: KeyringPair; | ||
|
||
before(async function () { | ||
alice = aliceKeypair(); | ||
|
||
conn = await createConnection(); | ||
await deploy(conn, alice, 'TryCatchCallee.contract', 0n); | ||
const caller_contract = await deploy(conn, alice, 'TryCatchCaller.contract', 1000000000n); | ||
caller = new ContractPromise(conn, caller_contract.abi, caller_contract.address); | ||
}); | ||
|
||
after(async function () { | ||
await conn.disconnect(); | ||
}); | ||
|
||
it('Tests all catch clauses', async function () { | ||
this.timeout(20000); | ||
|
||
for (let in_out = 0; in_out < 5; in_out++) { | ||
console.log("Testing case: " + in_out); | ||
const answer = await query(conn, alice, caller, "test", [in_out]); | ||
expect(answer.output?.toJSON()).toStrictEqual(in_out); | ||
} | ||
}); | ||
}); |
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
Oops, something went wrong.