diff options
author | Nemil Dalal <nemild@gmail.com> | 2016-01-14 10:17:51 -0500 |
---|---|---|
committer | Nemil Dalal <nemild@gmail.com> | 2016-01-14 10:17:51 -0500 |
commit | e8c0c436c9aad0f3902a4e0826bf72de49bde12b (patch) | |
tree | f4e16f625bb87ed6622161d2fdfe2ff12d05a77b /solidity.html.markdown | |
parent | d97c7be291b6bc3fb1477c30ac96b5b6cc36b9c4 (diff) |
Added observer pattern, per barkthin's feedback
Diffstat (limited to 'solidity.html.markdown')
-rw-r--r-- | solidity.html.markdown | 45 |
1 files changed, 42 insertions, 3 deletions
diff --git a/solidity.html.markdown b/solidity.html.markdown index 71a44b92..3a5e8ff5 100644 --- a/solidity.html.markdown +++ b/solidity.html.markdown @@ -506,7 +506,8 @@ function remove() { // Steps: 1. Commit to something, 2. Reveal commitment sha3("some_bid_amount", "some secret"); // commit -// call contract's reveal function showing bid plus secret that hashes to SHA3 +// call contract's reveal function in the future +// showing bid plus secret that hashes to SHA3 reveal(100, "mySecret"); // B. Storage optimization @@ -531,7 +532,45 @@ reveal(100, "mySecret"); // Contracts must be manually called to handle time-based scheduling; can create external // code to regularly ping, or provide incentives (ether) for others to -// E. State machines +// E. Observer Pattern +// An Observer Pattern lets you register as a subscriber and +// register a function which is called by the oracle (note, the oracle pays +// for this action to be run) +// Some similarities to subscription in Pub/sub + +// This is an abstract contract, both client and server classes import +// the client should implement +contract SomeOracleCallback { + function oracleCallback(int _value, uint _time, bytes32 info) external; +} + +contract SomeOracle { + SomeOracleCallback[] callbacks; // array of all subscribers + + // Register subscriber + function addSubscriber(SomeOracleCallback a) { + callbacks.push(a); + } + + function notify(value, time, info) private { + for(uint i = 0;i < callbacks.length; i++) { + // all called subscribers must implement the oracleCallback + callbacks[i].oracleCallback(value, time, info); + } + } + + function doSomething() public { + // Code to do something + + // Notify all subscribers + notify(_value, _time, _info); + } +} + +// Now, your client contract can addSubscriber by importing SampleOracleCallback +// and registering with Sample Oracle + +// F. State machines // see example below for State enum and inState modifier @@ -748,7 +787,7 @@ someContractAddress.callcode('function_name'); - Libraries ## Style -- Python's [PEP8](https://www.python.org/dev/peps/pep-0008/) is used as the baseline style guide, including its general philosophy) +- Python's [PEP8](https://www.python.org/dev/peps/pep-0008/) is used as the baseline style guide, including its general philosophy ## Future To Dos - New keywords: protected, inheritable |