-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #870 from keep-network/relay-request-callback
Relay request callback Update both Solidity and Go code to provide optional relay request callback.
- Loading branch information
Showing
15 changed files
with
242 additions
and
522 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
29 changes: 29 additions & 0 deletions
29
contracts/solidity/contracts/examples/CallbackContract.sol
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,29 @@ | ||
pragma solidity ^0.5.4; | ||
|
||
|
||
/** | ||
* @title CallbackContract | ||
* @dev Example callback contract for Random Beacon. | ||
*/ | ||
contract CallbackContract { | ||
|
||
uint256 internal _lastEntry; | ||
|
||
/** | ||
* @dev Example of a callback method. Method signature can be | ||
* calculated as bytes4(keccak256("callback(uint256)") | ||
*/ | ||
function callback(uint256 requestResponse) | ||
public | ||
{ | ||
_lastEntry = requestResponse; | ||
} | ||
|
||
/** | ||
* @dev Returns previous entry. | ||
*/ | ||
function lastEntry() public view returns (uint256) | ||
{ | ||
return _lastEntry; | ||
} | ||
} |
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,32 @@ | ||
const KeepRandomBeaconProxy = artifacts.require('KeepRandomBeacon.sol') | ||
const KeepRandomBeacon = artifacts.require("KeepRandomBeaconImplV1") | ||
|
||
// The data below should match genesis relay request data defined on contract | ||
// initialization i.e. in 2_deploy_contracts.js. Successful genesis entry will | ||
// trigger creation of the first group that will be chosen to respond on the | ||
// next relay request, resulting another relay entry with creation of another | ||
// group and so on. | ||
|
||
// https://www.wolframalpha.com/input/?i=pi+to+78+digits | ||
const previousEntry = web3.utils.toBN('31415926535897932384626433832795028841971693993751058209749445923078164062862') | ||
|
||
// https://www.wolframalpha.com/input/?i=e+to+78+digits | ||
const seed = web3.utils.toBN('27182818284590452353602874713526624977572470936999595749669676277240766303535') | ||
|
||
// Data generated using client keep-core/pkg/bls package signing previous entry using master secret key '123' | ||
const groupPubKey = "0x1f1954b33144db2b5c90da089e8bde287ec7089d5d6433f3b6becaefdb678b1b2a9de38d14bef2cf9afc3c698a4211fa7ada7b4f036a2dfef0dc122b423259d0" | ||
const groupSignature = web3.utils.toBN('10920102476789591414949377782104707130412218726336356788412941355500907533021') | ||
|
||
module.exports = async function () { | ||
|
||
const keepRandomBeaconProxy = await KeepRandomBeaconProxy.deployed() | ||
let contract = await KeepRandomBeacon.at(keepRandomBeaconProxy.address) | ||
try { | ||
await contract.relayEntry(1, groupSignature, groupPubKey, previousEntry, seed) | ||
console.log('Genesis entry successfully submitted.') | ||
} catch(error) { | ||
console.error('Genesis entry submission failed with', error) | ||
} | ||
|
||
process.exit() | ||
} |
31 changes: 31 additions & 0 deletions
31
contracts/solidity/scripts/request-relay-entry-with-callback.js
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,31 @@ | ||
const crypto = require("crypto") | ||
const KeepRandomBeacon = artifacts.require("KeepRandomBeaconImplV1") | ||
const KeepRandomBeaconProxy = artifacts.require('KeepRandomBeacon.sol') | ||
|
||
// Example usage: | ||
// truffle exec ./scripts/request-relay-entry-with-callback.js yourContractAddress "callbackMethodName" payment | ||
// truffle exec ./scripts/request-relay-entry-with-callback.js 0x9F57C01059057d821c6b4B04A4598322661C934F "callback(uint256)" 100 | ||
|
||
module.exports = async function() { | ||
|
||
const keepRandomBeaconProxy = await KeepRandomBeaconProxy.deployed() | ||
const contractInstance = await KeepRandomBeacon.at(keepRandomBeaconProxy.address) | ||
|
||
try { | ||
let tx = await contractInstance.methods['requestRelayEntry(uint256,address,string)'](crypto.randomBytes(32), process.argv[4], process.argv[5], {value: process.argv[6]}) | ||
console.log('Successfully requested relay entry with a callback. RequestId =', tx.logs[0].args.requestID.toString()) | ||
console.log( | ||
'\n---Transaction Summary---' + '\n' + | ||
'From:' + tx.receipt.from + '\n' + | ||
'To:' + tx.receipt.to + '\n' + | ||
'BlockNumber:' + tx.receipt.blockNumber + '\n' + | ||
'TotalGas:' + tx.receipt.cumulativeGasUsed + '\n' + | ||
'TransactionHash:' + tx.receipt.transactionHash + '\n' + | ||
'--------------------------' | ||
) | ||
} catch(error) { | ||
console.error('Request failed with', error) | ||
} | ||
|
||
process.exit() | ||
} |
Oops, something went wrong.