Skip to content

Commit 16cc1ff

Browse files
bidzyyys0xNeshidependabot[bot]
authored
fix: export-abi feature (#602)
Resolves #439 @0xNeshi the idea is to have a working solution within the current Stylus SDK's limitations. We can work on a "clean" solution after the audit starts. #### PR Checklist <!-- Before merging the pull request all of the following must be completed. Feel free to submit a PR or Draft PR even if some items are pending. Some of the items may not apply. --> - [x] Tests - [x] Documentation - [x] Changelog --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: 0xNeshi <nenad.misic@openzeppelin.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
1 parent 66a0a81 commit 16cc1ff

File tree

46 files changed

+1435
-476
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1435
-476
lines changed

.github/workflows/fuzz.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828

2929
steps:
3030
- name: Checkout repository
31-
uses: actions/checkout@v3
31+
uses: actions/checkout@v4
3232

3333
- name: Install Rust toolchain
3434
uses: actions-rs/toolchain@v1
@@ -47,7 +47,7 @@ jobs:
4747
4848
- name: Restore cached corpus
4949
id: cache-corpus
50-
uses: actions/cache@v3
50+
uses: actions/cache@v4
5151
with:
5252
path: fuzz/corpus/${{ github.event.inputs.fuzz_target }}
5353
key: fuzz-corpus-${{ github.event.inputs.fuzz_target }}-${{ github.sha }}
@@ -100,7 +100,7 @@ jobs:
100100
101101
- name: Upload crashes
102102
if: always()
103-
uses: actions/upload-artifact@v3
103+
uses: actions/upload-artifact@v4
104104
with:
105105
name: fuzz-crashes-${{ github.event.inputs.fuzz_target }}
106106
path: fuzz/artifacts/crashes-${{ github.event.inputs.fuzz_target }}
@@ -127,7 +127,7 @@ jobs:
127127
128128
- name: Upload fuzzing stats
129129
if: always()
130-
uses: actions/upload-artifact@v3
130+
uses: actions/upload-artifact@v4
131131
with:
132132
name: fuzz-stats-${{ github.event.inputs.fuzz_target }}
133133
path: fuzz/fuzz_stats

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2323
### Changed
2424

2525
- Optimize Stylus SDK imports. #598
26+
- Updated the recommended way to inherit errors. #602
2627

2728
### Changed (Breaking)
2829

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

contracts/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ motsu.workspace = true
2828
# Currently, the std feature is only used for testing purposes.
2929
std = []
3030
reentrant = ["stylus-sdk/reentrant"]
31+
export-abi = ["stylus-sdk/export-abi", "std"]
3132

3233
[lib]
3334
crate-type = ["lib"]

contracts/src/access/control.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,10 @@ pub trait IAccessControl {
161161
///
162162
/// * [`Error::UnauthorizedAccount`] - If [`msg::sender`] has not been
163163
/// granted `role`.
164-
fn only_role(&self, role: B256) -> Result<(), Self::Error>;
164+
fn only_role(
165+
&self,
166+
role: B256,
167+
) -> Result<(), <Self as IAccessControl>::Error>;
165168

166169
/// Returns the admin role that controls `role`. See
167170
/// [`IAccessControl::grant_role`] and [`IAccessControl::revoke_role`].
@@ -197,7 +200,7 @@ pub trait IAccessControl {
197200
&mut self,
198201
role: B256,
199202
account: Address,
200-
) -> Result<(), Self::Error>;
203+
) -> Result<(), <Self as IAccessControl>::Error>;
201204

202205
/// Revokes `role` from `account`.
203206
///
@@ -221,7 +224,7 @@ pub trait IAccessControl {
221224
&mut self,
222225
role: B256,
223226
account: Address,
224-
) -> Result<(), Self::Error>;
227+
) -> Result<(), <Self as IAccessControl>::Error>;
225228

226229
/// Revokes `role` from the calling account.
227230
///
@@ -248,7 +251,7 @@ pub trait IAccessControl {
248251
&mut self,
249252
role: B256,
250253
confirmation: Address,
251-
) -> Result<(), Self::Error>;
254+
) -> Result<(), <Self as IAccessControl>::Error>;
252255
}
253256

254257
#[public]
@@ -259,7 +262,10 @@ impl IAccessControl for AccessControl {
259262
self.roles.getter(role).has_role.get(account)
260263
}
261264

262-
fn only_role(&self, role: B256) -> Result<(), Self::Error> {
265+
fn only_role(
266+
&self,
267+
role: B256,
268+
) -> Result<(), <Self as IAccessControl>::Error> {
263269
self._check_role(role, msg::sender())
264270
}
265271

@@ -271,7 +277,7 @@ impl IAccessControl for AccessControl {
271277
&mut self,
272278
role: B256,
273279
account: Address,
274-
) -> Result<(), Self::Error> {
280+
) -> Result<(), <Self as IAccessControl>::Error> {
275281
let admin_role = self.get_role_admin(role);
276282
self.only_role(admin_role)?;
277283
self._grant_role(role, account);
@@ -282,7 +288,7 @@ impl IAccessControl for AccessControl {
282288
&mut self,
283289
role: B256,
284290
account: Address,
285-
) -> Result<(), Self::Error> {
291+
) -> Result<(), <Self as IAccessControl>::Error> {
286292
let admin_role = self.get_role_admin(role);
287293
self.only_role(admin_role)?;
288294
self._revoke_role(role, account);
@@ -293,7 +299,7 @@ impl IAccessControl for AccessControl {
293299
&mut self,
294300
role: B256,
295301
confirmation: Address,
296-
) -> Result<(), Self::Error> {
302+
) -> Result<(), <Self as IAccessControl>::Error> {
297303
if msg::sender() != confirmation {
298304
return Err(Error::BadConfirmation(
299305
AccessControlBadConfirmation {},

contracts/src/access/ownable.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ pub trait IOwnable {
102102
fn transfer_ownership(
103103
&mut self,
104104
new_owner: Address,
105-
) -> Result<(), Self::Error>;
105+
) -> Result<(), <Self as IOwnable>::Error>;
106106

107107
/// Leaves the contract without owner. It will not be possible to call
108108
/// functions that require `only_owner`. Can only be called by the current
@@ -122,7 +122,7 @@ pub trait IOwnable {
122122
/// # Events
123123
///
124124
/// * [`OwnershipTransferred`].
125-
fn renounce_ownership(&mut self) -> Result<(), Self::Error>;
125+
fn renounce_ownership(&mut self) -> Result<(), <Self as IOwnable>::Error>;
126126
}
127127

128128
#[public]
@@ -136,7 +136,7 @@ impl IOwnable for Ownable {
136136
fn transfer_ownership(
137137
&mut self,
138138
new_owner: Address,
139-
) -> Result<(), Self::Error> {
139+
) -> Result<(), <Self as IOwnable>::Error> {
140140
self.only_owner()?;
141141

142142
if new_owner.is_zero() {
@@ -150,7 +150,7 @@ impl IOwnable for Ownable {
150150
Ok(())
151151
}
152152

153-
fn renounce_ownership(&mut self) -> Result<(), Self::Error> {
153+
fn renounce_ownership(&mut self) -> Result<(), <Self as IOwnable>::Error> {
154154
self.only_owner()?;
155155
self._transfer_ownership(Address::ZERO);
156156
Ok(())

contracts/src/access/ownable_two_step.rs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub use sol::*;
2525
use stylus_sdk::{evm, msg, prelude::*, storage::StorageAddress};
2626

2727
use crate::{
28-
access::ownable::{self, IOwnable, Ownable, OwnableUnauthorizedAccount},
28+
access::ownable::{self, IOwnable, Ownable},
2929
utils::introspection::erc165::{Erc165, IErc165},
3030
};
3131

@@ -117,7 +117,7 @@ pub trait IOwnable2Step {
117117
fn transfer_ownership(
118118
&mut self,
119119
new_owner: Address,
120-
) -> Result<(), Self::Error>;
120+
) -> Result<(), <Self as IOwnable2Step>::Error>;
121121

122122
/// Accepts the ownership of the contract.
123123
/// Can only be called by the pending owner.
@@ -134,7 +134,9 @@ pub trait IOwnable2Step {
134134
/// # Events
135135
///
136136
/// * [`crate::access::ownable::OwnershipTransferred`].
137-
fn accept_ownership(&mut self) -> Result<(), Self::Error>;
137+
fn accept_ownership(
138+
&mut self,
139+
) -> Result<(), <Self as IOwnable2Step>::Error>;
138140

139141
/// Leaves the contract without owner. It will not be possible to call
140142
/// [`Ownable::only_owner`] functions. Can only be called by the current
@@ -154,7 +156,9 @@ pub trait IOwnable2Step {
154156
/// # Events
155157
///
156158
/// * [`crate::access::ownable::OwnershipTransferred`].
157-
fn renounce_ownership(&mut self) -> Result<(), Self::Error>;
159+
fn renounce_ownership(
160+
&mut self,
161+
) -> Result<(), <Self as IOwnable2Step>::Error>;
158162
}
159163

160164
#[public]
@@ -172,7 +176,7 @@ impl IOwnable2Step for Ownable2Step {
172176
fn transfer_ownership(
173177
&mut self,
174178
new_owner: Address,
175-
) -> Result<(), Self::Error> {
179+
) -> Result<(), <Self as IOwnable2Step>::Error> {
176180
self.ownable.only_owner()?;
177181
self.pending_owner.set(new_owner);
178182

@@ -184,19 +188,23 @@ impl IOwnable2Step for Ownable2Step {
184188
Ok(())
185189
}
186190

187-
fn accept_ownership(&mut self) -> Result<(), Self::Error> {
191+
fn accept_ownership(
192+
&mut self,
193+
) -> Result<(), <Self as IOwnable2Step>::Error> {
188194
let sender = msg::sender();
189195
let pending_owner = self.pending_owner();
190196
if sender != pending_owner {
191197
return Err(ownable::Error::UnauthorizedAccount(
192-
OwnableUnauthorizedAccount { account: sender },
198+
ownable::OwnableUnauthorizedAccount { account: sender },
193199
));
194200
}
195201
self._transfer_ownership(sender);
196202
Ok(())
197203
}
198204

199-
fn renounce_ownership(&mut self) -> Result<(), Self::Error> {
205+
fn renounce_ownership(
206+
&mut self,
207+
) -> Result<(), <Self as IOwnable2Step>::Error> {
200208
self.ownable.only_owner()?;
201209
self._transfer_ownership(Address::ZERO);
202210
Ok(())
@@ -241,11 +249,7 @@ mod tests {
241249
use motsu::prelude::Contract;
242250
use stylus_sdk::prelude::TopLevelStorage;
243251

244-
use super::{
245-
ownable::Error, IOwnable, IOwnable2Step, Ownable, Ownable2Step,
246-
OwnableUnauthorizedAccount,
247-
};
248-
use crate::utils::introspection::erc165::IErc165;
252+
use super::*;
249253

250254
unsafe impl TopLevelStorage for Ownable2Step {}
251255

@@ -304,7 +308,7 @@ mod tests {
304308
let err = contract.sender(alice).transfer_ownership(dave).unwrap_err();
305309
assert!(matches!(
306310
err,
307-
Error::UnauthorizedAccount(OwnableUnauthorizedAccount {
311+
ownable::Error::UnauthorizedAccount(ownable::OwnableUnauthorizedAccount {
308312
account
309313
}) if account == alice
310314
));
@@ -344,7 +348,7 @@ mod tests {
344348
let err = contract.sender(alice).accept_ownership().unwrap_err();
345349
assert!(matches!(
346350
err,
347-
Error::UnauthorizedAccount(OwnableUnauthorizedAccount {
351+
ownable::Error::UnauthorizedAccount(ownable::OwnableUnauthorizedAccount {
348352
account
349353
}) if account == alice
350354
));
@@ -401,7 +405,7 @@ mod tests {
401405
let err = contract.sender(alice).renounce_ownership().unwrap_err();
402406
assert!(matches!(
403407
err,
404-
Error::UnauthorizedAccount(OwnableUnauthorizedAccount {
408+
ownable::Error::UnauthorizedAccount(ownable::OwnableUnauthorizedAccount {
405409
account
406410
}) if account == alice
407411
));

0 commit comments

Comments
 (0)