Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(mixin-ownable): add Ownable mixin and tests #1305

Merged
merged 1 commit into from Apr 23, 2019

Conversation

davemneo
Copy link
Contributor

@davemneo davemneo commented Apr 10, 2019

Overview

This class provides a means to assign an address as the owner of a contract. Extending this class and adding this.ownerOnly(); to the beginning of all public functions will throw an error anytime an address other than the primary makes requests.

API

Public

owner() - returns the current owner address or undefined.

Manage contract Ownership with...

renounceOwnership() - Allows only the owner to contract to become ownerless, this will cause all ownerOnly() calls to fail, this is not reversible. Note: the initialOwner will maintain still contain the initial address.
transferOwnership() - Only the owner may transfer ownership to a new

Internal

ownerOrThrow() - returns the current owner address or throws an error.
onlyOwner() - if the owner address did not call this contract an error is thrown.

Published Events:

tokensPurchased with purchaser, beneficiary and amount of tokens bought.

Configuration:

Be sure that any contract extending this Ownable mix-in sets the initialOwner thusly:

export class MyContract extends Ownable(SmartContract) {
  public constructor(protected readonly initialOwner = Deploy.senderAddress) {
    super();
  }
  // .... awesome smart contract code ... 
}

Tests

yarn jest TestOwnable

@davemneo davemneo changed the title feat(mixin-ownable): add Ownable mixin and tests suite feat(mixin-ownable): add Ownable mixin and test suite Apr 10, 2019
@davemneo davemneo changed the title feat(mixin-ownable): add Ownable mixin and test suite feat(mixin-ownable): add Ownable mixin and tests Apr 10, 2019
@codecov
Copy link

codecov bot commented Apr 10, 2019

Codecov Report

Merging #1305 into master will increase coverage by 0.05%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff            @@
##           master   #1305      +/-   ##
=========================================
+ Coverage   64.74%   64.8%   +0.05%     
=========================================
  Files        1529    1529              
  Lines       40396   40396              
  Branches     3941    3941              
=========================================
+ Hits        26156   26179      +23     
+ Misses      14240   14217      -23
Flag Coverage Δ
#e2e 60.41% <ø> (-0.08%) ⬇️
#unit 62.38% <ø> (+0.13%) ⬆️
Impacted Files Coverage Δ
...eo-one-node-protocol/src/payload/HeadersPayload.ts 21.42% <0%> (-35.72%) ⬇️
...-one-node-protocol/src/payload/GetBlocksPayload.ts 64.7% <0%> (-35.3%) ⬇️
.../payload/message/PrepareRequestConsensusMessage.ts 34.37% <0%> (-34.38%) ⬇️
...core/src/transaction/attribute/UInt256Attribute.ts 57.57% <0%> (-6.07%) ⬇️
...neo-one-node-http-rpc/src/middleware/checkReady.ts 91.17% <0%> (-2.95%) ⬇️
...erver-plugin-network/src/node/NEOONENodeAdapter.ts 80.59% <0%> (-1.5%) ⬇️
packages/neo-one-ts-utils/src/type_.ts 71.29% <0%> (-0.31%) ⬇️
...eo-one-node-blockchain/src/WriteBatchBlockchain.ts 86.85% <0%> (+0.3%) ⬆️
.../neo-one-client-core/src/sc/createSmartContract.ts 96.58% <0%> (+0.85%) ⬆️
.../neo-one-node-blockchain/src/wrapExecuteScripts.ts 75% <0%> (+12.5%) ⬆️
... and 4 more

@codecov
Copy link

codecov bot commented Apr 10, 2019

Codecov Report

Merging #1305 into master will increase coverage by 2.56%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1305      +/-   ##
==========================================
+ Coverage   62.23%   64.79%   +2.56%     
==========================================
  Files        1529     1529              
  Lines       40393    40407      +14     
  Branches     3940     3943       +3     
==========================================
+ Hits        25137    26183    +1046     
+ Misses      15256    14224    -1032
Flag Coverage Δ
#e2e 60.29% <ø> (-39.71%) ⬇️
#unit 62.38% <ø> (+0.15%) ⬆️
Impacted Files Coverage Δ
packages/neo-one-website/src/polyfill.ts 0% <0%> (ø) ⬆️
...eo-one-node-blockchain/src/WriteBatchBlockchain.ts 86.85% <0%> (+0.3%) ⬆️
packages/neo-one-node-vm/src/opcodes.ts 84.34% <0%> (+0.5%) ⬆️
packages/neo-one-node-vm/src/errors.ts 64.1% <0%> (+0.85%) ⬆️
packages/neo-one-node-vm/src/execute.ts 82.47% <0%> (+1.03%) ⬆️
...ne-client-core/src/provider/JSONRPCHTTPProvider.ts 93.1% <0%> (+1.14%) ⬆️
...o-one-node-core/src/transaction/TransactionBase.ts 86.98% <0%> (+1.77%) ⬆️
.../neo-one-client-core/src/sc/createSmartContract.ts 96.58% <0%> (+2.56%) ⬆️
...ient-switch/src/common/createConsoleLogMessages.ts 90.9% <0%> (+2.59%) ⬆️
...ckages/neo-one-node-core/src/utils/BinaryReader.ts 83.01% <0%> (+5.66%) ⬆️
... and 82 more

@davemneo davemneo force-pushed the contract-mixins-ownable branch 7 times, most recently from 80a5837 to 6c69293 Compare April 12, 2019 04:16
Copy link
Contributor

@dicarlo2 dicarlo2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

overall lgtm

@davemneo davemneo force-pushed the contract-mixins-ownable branch 4 times, most recently from 202cf86 to 9f7469d Compare April 17, 2019 05:02
Copy link
Contributor

@dicarlo2 dicarlo2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LG, just two last comments.

@davemneo davemneo force-pushed the contract-mixins-ownable branch 3 times, most recently from d809969 to 7c47d5b Compare April 18, 2019 17:29
Copy link
Contributor

@dicarlo2 dicarlo2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One last comment!

@@ -0,0 +1,57 @@
import { Address, SmartContract, createEventNotifier } from '@neo-one/smart-contract';

const notifyTransferOwnership = createEventNotifier<Address | undefined, Address>('transfer_ownership', 'from', 'to');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the from Address can never be undefined, no?

Copy link
Contributor Author

@davemneo davemneo Apr 22, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dicarlo2 True, now, after modifying transferOwnership to use ownerOrThrow otherwise, the mutableOwner as passed is possibly undefined.

Which path should these functions go? extra cpu cycles, or fewer with that optional undefined?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Answer discussed @ standup, going with "Easy to read".

}

protected onlyOwner() {
if (this.mutableOwner !== undefined && !Address.isCaller(this.mutableOwner)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be using this.ownerOrThrow no? Seems like a bug - if there's no owner then this should throw I think.

Copy link
Contributor Author

@davemneo davemneo Apr 20, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dicarlo2 "bug" might be a little harsh, I'd say mine is between 4 and 12 operations more efficient and achieves same goal. I can switch it to ownerOrThrow() if you still like.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think he is suggesting something like:

protected onlyOwner() {
  if (!Address.isCaller(this.ownerOrThrow())) {
    throw new Error('not owner');
  }
}

(edit because assignment)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(because if this.mutableOwner is undefined then this will pass always)

Copy link
Contributor Author

@davemneo davemneo Apr 22, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@danwbyrne yes, that was my glitch the !==undefined. But: re discussion during standup, we are favoring code readability over scrutinizing nominal amounts of gas for extra stack operations. Change will be made, thank you!

Copy link
Contributor

@danwbyrne danwbyrne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmmm, what is the order that these PRs need to go in? I'm starting to get lost by them :P

@davemneo
Copy link
Contributor Author

davemneo commented Apr 20, 2019

@danwbyrne

what is the order that these PRs need to go in? I'm starting to get lost by them :P

This one first.

Copy link
Contributor

@danwbyrne danwbyrne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see now this is the first one that needs to go in :)

}

protected onlyOwner() {
if (this.mutableOwner !== undefined && !Address.isCaller(this.mutableOwner)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think he is suggesting something like:

protected onlyOwner() {
  if (!Address.isCaller(this.ownerOrThrow())) {
    throw new Error('not owner');
  }
}

(edit because assignment)

}

protected onlyOwner() {
if (this.mutableOwner !== undefined && !Address.isCaller(this.mutableOwner)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(because if this.mutableOwner is undefined then this will pass always)

Copy link
Contributor

@danwbyrne danwbyrne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just fix the deepscan

@davemneo
Copy link
Contributor Author

just fix the deepscan

@danwbyrne Unrelated

Copy link
Contributor

@dicarlo2 dicarlo2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm.

The deepscan issues are odd, I wonder why they showed up on this PR.

@dicarlo2
Copy link
Contributor

bors r+

bors bot added a commit that referenced this pull request Apr 23, 2019
1305: feat(mixin-ownable): add Ownable mixin and tests r=dicarlo2 a=davemneo

# Overview 
This class provides a means to assign an address as the owner of a contract. Extending this class and adding ` this.ownerOnly(); ` to the beginning of all public functions will throw an error anytime an address other than the primary makes requests.  

## API
### Public
`owner()` - returns the current owner address or undefined.
##### Manage contract Ownership with...
 `renounceOwnership()` - Allows only the owner to contract to become ownerless, this will cause all `ownerOnly()` calls to fail, this is not reversible.  Note: the `initialOwner` will maintain still contain the initial address.  
 `transferOwnership()` - Only the owner may transfer ownership to a new 

### Internal
`ownerOrThrow()` - returns the current owner address or throws an error.
`onlyOwner()` - if the owner address did not call this contract an error is thrown.

### Published Events:
`tokensPurchased` with purchaser, beneficiary and amount of tokens bought.

## Configuration:
Be sure that any contract extending this `Ownable` mix-in sets the `initialOwner` thusly: 
``` typescript:
export class MyContract extends Ownable(SmartContract) {
  public constructor(protected readonly initialOwner = Deploy.senderAddress) {
    super();
  }
  // .... awesome smart contract code ... 
}
```

## Tests

    yarn jest TestOwnable


Co-authored-by: Dave M <david.mathiesen@neotracker.io>
@bors bors bot merged commit 6f55ea7 into neo-one-suite:master Apr 23, 2019
@davemneo davemneo self-assigned this Apr 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants