Skip to content

Instantly share code, notes, and snippets.

@anshuiisc
Created February 20, 2019 11:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anshuiisc/25da0dc214078290f1dbaba21dc5448e to your computer and use it in GitHub Desktop.
Save anshuiisc/25da0dc214078290f1dbaba21dc5448e to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.4.24-nightly.2018.5.3+commit.72c3b3a2.js&optimize=false&gist=
pragma solidity ^0.4.0;
import "github.com/20Scoops-CNX/solidity-utils/lib/ListInteger.sol";
import "github.com/20Scoops-CNX/solidity-utils/lib/Strings.sol";
import "github.com/20Scoops-CNX/solidity-utils/lib/Integers.sol";
contract ExampleContract {
using ListInteger for *;
using Strings for string;
using Integers for uint;
uint[] public items;
function exist(uint[] items, uint value) public returns (bool) {
uint i = 0;
while (i<items.getSize() && items[i] != value) {
i++;
}
if(i>=items.getSize())
return false;
else if( items[i] == value)
return true;
}
function example(uint c) public returns (uint, uint) {
uint size1 = items.getSize();
if(exist(items,c)){
items.removeByValue(c);
}
uint size2 = items.getSize();
return (size1,size2);
}
function example1() public {
items.add(1);
items.add(2);
items.add(4);
items.add(3);
// uint index = items.find(9911);
// return index;
}
}
pragma solidity ^0.4.0;
contract Ballot {
struct Voter {
uint weight;
bool voted;
uint8 vote;
address delegate;
}
struct Proposal {
uint voteCount;
}
address chairperson;
mapping(address => Voter) voters;
Proposal[] proposals;
/// Create a new ballot with $(_numProposals) different proposals.
function Ballot(uint8 _numProposals) public {
chairperson = msg.sender;
voters[chairperson].weight = 1;
proposals.length = _numProposals;
}
/// Give $(toVoter) the right to vote on this ballot.
/// May only be called by $(chairperson).
function giveRightToVote(address toVoter) public {
if (msg.sender != chairperson || voters[toVoter].voted) return;
voters[toVoter].weight = 1;
}
/// Delegate your vote to the voter $(to).
function delegate(address to) public {
Voter storage sender = voters[msg.sender]; // assigns reference
if (sender.voted) return;
while (voters[to].delegate != address(0) && voters[to].delegate != msg.sender)
to = voters[to].delegate;
if (to == msg.sender) return;
sender.voted = true;
sender.delegate = to;
Voter storage delegateTo = voters[to];
if (delegateTo.voted)
proposals[delegateTo.vote].voteCount += sender.weight;
else
delegateTo.weight += sender.weight;
}
/// Give a single vote to proposal $(toProposal).
function vote(uint8 toProposal) public {
Voter storage sender = voters[msg.sender];
if (sender.voted || toProposal >= proposals.length) return;
sender.voted = true;
sender.vote = toProposal;
proposals[toProposal].voteCount += sender.weight;
}
function winningProposal() public constant returns (uint8 _winningProposal) {
uint256 winningVoteCount = 0;
for (uint8 prop = 0; prop < proposals.length; prop++)
if (proposals[prop].voteCount > winningVoteCount) {
winningVoteCount = proposals[prop].voteCount;
_winningProposal = prop;
}
}
}
pragma solidity ^0.4.7;
import "remix_tests.sol"; // this import is automatically injected by Remix.
import "./ballot.sol";
contract test3 {
Ballot ballotToTest;
function beforeAll () {
ballotToTest = new Ballot(2);
}
function checkWinningProposal () public {
ballotToTest.vote(1);
Assert.equal(ballotToTest.winningProposal(), uint(1), "1 should be the winning proposal");
}
function checkWinninProposalWithReturnValue () public constant returns (bool) {
return ballotToTest.winningProposal() == 1;
}
}
pragma solidity ^0.4.0;
contract BytesOrStrings {
string constant _string = "cryptopus.co Medium";
bytes32 constant _bytes = "cryptopus.co Medium";
function getAsString() public returns(string) {
return _string;
}
function getAsBytes() public returns(bytes32) {
return _bytes;
}
}
pragma solidity ^0.4.21;
contract ExampleContract {
string public Name;
function ExampleContract (string name) public {
Name = name;
}
}
contract Factory {
address[] public contracts;
function createContract(string name) public returns(address) {
address newContract = new ExampleContract(name);
contracts.push(newContract);
return newContract;
}
}
pragma solidity ^0.4.0;
import "browser/hipc_register.sol";
import "github.com/20Scoops-CNX/solidity-utils/lib/ListInteger.sol";
// import "github.com/20Scoops-CNX/solidity-utils/lib/Strings.sol";
// import "github.com/20Scoops-CNX/solidity-utils/lib/Integers.sol";
contract Plan_SC{
event loglist(uint256[] x,string s);
event logint(uint x,string s);
event logexist(uint256[] x,uint s,bool b);
using ListInteger for *;
// using Strings for string;
// using Integers for uint;
uint action_count;
uint256[][6] dag;
function Plan_SC(){
action_count=3;
dag[0]=[1];
dag[1]=[2];
dag[2]=[3,1];
// dag[3]=[4,2];
// dag[4]=[5,2,3];
// dag[5]=[6,5];
logint(0,"dag created");
// Register_SC Reg_SC_addr=new Register_SC(); // retuns address of new contract
// for(uint256 x = 0; x < agent_actList.length; x++) {
// Reg_SC_addr.setAct(agent_actList[x]);// use same logic as bove
// }
}
function exist(uint[] items, uint value) public returns (bool) {
uint i = 0;bool b;
while (i<items.getSize() && items[i] != value) {
i++;
}
if(i>=items.getSize()){
b=false;
// logexist(items,value,b);
return b;
}
else if( items[i] == value){
b=true;
// logexist(items,value,b);
return b;
}
}
uint256 CompletedListLength;
function runPlan(Register_SC register_addr){
// logint(dag.length,"size of dag length");
while(CompletedListLength!=action_count)
for(uint256 x = 0; x < dag.length; x++) {
uint current;
bool _success;
if(dag[x].getSize()==1){
current =dag[x][0];
// logint(dag[x].getSize(),"size after condition check");
_success=dispatchNext(register_addr,current);
// logint(dag[x].getSize(),"size after condition check after remove");
}
if(_success){
for(uint256 y = 0; y < dag.length; y++) {
if(exist(dag[y],current)){
// loglist(dag[y],"CHECK1:printing full list before deletion");
dag[y].removeByValue(current);
// loglist(dag[y],"printing full list after deletion");
}
}
}
}
}
function dispatchNext(Register_SC register_addr, uint256 Action_id) returns(bool _success){
Act_SC act_addr=register_addr.getAct(Action_id);
bool success=act_addr.execute(Action_id);
uint256[] memory res=act_addr.getCompletedList();
CompletedListLength=res.length;
loglist(res,"completed_list_updated");
return success;
}
}
pragma solidity ^0.4.0;
contract Register_SC {
event logDeployedContract(address indexed tenant,string s);
mapping(uint256 => Act_SC) action_scAddrMap;
function setAct(uint[] memory action_names) public {
Act_SC a1_addr=new Act_SC(action_names); // retuns address of new contract
for(uint256 x = 0; x < action_names.length; x++) {
action_scAddrMap[action_names[x]]=a1_addr;
}
logDeployedContract(a1_addr,"new_contract_address");
}
function getAct(uint256 action_name) public returns(Act_SC act_scAddr) {
return action_scAddrMap[action_name];
}
}
contract Act_SC{
event display(string x);
event display(string x,uint[] s);
event abort(string s,uint256 actionID);
event completed(string s,uint256 actionID);
event displayActionName(uint256 x);
struct Action{
uint256 Action_id;
string Action_URI;
bytes32 [] precond;
bytes32[] effects;
}
Action private action_1;
Action private action_2;
Action private action_3;
Action private action_4;
Action private action_5;
Action private action_6;
mapping(uint256 => Action) actionid_ActionMap;
// uint256 [] action_ids;
uint256[] public completedList;
function Act_SC(uint[] memory action_names){
for(uint256 x = 0; x < action_names.length; x++) {
if(action_names[x]==1){
action_1.Action_id=action_names[x];
action_1.Action_URI="http://a1.com";
action_1.precond.push("precond1_0");
action_1.precond.push("precond1_1");
action_1.effects.push("effects1_0");
action_1.effects.push("effects1_1");
actionid_ActionMap[1]=action_1;
}
else if(action_names[x]==2){
action_2.Action_id=action_names[x];
action_2.Action_URI="http://a2.com";
action_2.precond.push("precond2_0");
action_2.precond.push("precond2_1");
action_2.effects.push("effects2_0");
action_2.effects.push("effects2_1");
actionid_ActionMap[2]=action_2;
}
else if(action_names[x]==3){
action_2.Action_id=3;
action_2.Action_URI="http://a2.com";
action_2.precond.push("precond3_0");
action_2.precond.push("precond3_1");
action_2.effects.push("effects3_0");
action_2.effects.push("effects3_1");
actionid_ActionMap[3]=action_3;
}
else if(action_names[x]==4){
action_2.Action_id=4;
action_2.Action_URI="http://a2.com";
action_2.precond.push("precond2_0");
action_2.precond.push("precond2_1");
action_2.effects.push("effects2_0");
action_2.effects.push("effects2_1");
actionid_ActionMap[4]=action_4;
}
else if(action_names[x]==5){
action_2.Action_id=action_names[x];
action_2.Action_URI="http://a2.com";
action_2.precond.push("precond2_0");
action_2.precond.push("precond2_1");
action_2.effects.push("effects2_0");
action_2.effects.push("effects2_1");
actionid_ActionMap[5]=action_5;
}
else if(action_names[x]==6){
action_2.Action_id=action_names[x];
action_2.Action_URI="http://a2.com";
action_2.precond.push("precond2_0");
action_2.precond.push("precond2_1");
action_2.effects.push("effects2_0");
action_2.effects.push("effects2_1");
actionid_ActionMap[6]=action_6;
}
}
display("Act_SC_created for actions",action_names);
}
function getCompletedList() external returns(uint256[] list) {
return completedList;
}
function printAction() {
displayActionName(action_1.Action_id);
// display(a1.Action_name,a1.precond[0],a1.effects[0]);
}
function getPreCondition(uint256 Action_id) public returns (bytes32[] preCond){
return actionid_ActionMap[Action_id].precond;
}
function getEffect(uint256 Action_id) public returns(bytes32[] effect) {
return actionid_ActionMap[Action_id].effects;
}
function checkPreCondition(bytes32[] preCond) returns(bool cond) {
return true;
}
function checkEffect(bytes32[] effect) public returns(bool cond) {
return true;
}
//uses oracle return predList
function getPredicatesValues(bytes32[] input)returns(string str){
return "get_callback_from_oraclize";
}
function execute(uint256 Action_id) external returns(bool success){
bytes32[] memory preCond=getPreCondition(Action_id);
getPredicatesValues(preCond); //?? why we need this
if(checkPreCondition(preCond)){
//oracalize post .. using URI
display("callAPI(action_api_map[actionID])");//actual call for action
// check return of oracalise post
}
else{
abort("Transaction_Rollback",Action_id);
return false;
}
bytes32[] memory eff=getEffect(Action_id);
getPredicatesValues(eff); //?? why we need this
if(checkEffect(eff)){
completedList.push(Action_id);
completed("Action_Completed", Action_id);
return true;
}
else
abort("Transaction_Rollback",Action_id);
return false;
}
}
pragma solidity ^0.4.0;
import "github.com/oraclize/ethereum-api/oraclizeAPI.sol";
contract Register_SC_Orac {
event logDeployedContract(address indexed tenant,string s);
mapping(uint256 => Act_SC) action_scAddrMap;
function setAct(uint[] memory action_names) public {
Act_SC a1_addr=new Act_SC(action_names); // retuns address of new contract
for(uint256 x = 0; x < action_names.length; x++) {
action_scAddrMap[action_names[x]]=a1_addr;
}
logDeployedContract(a1_addr,"new_contract_address");
}
function getAct(uint256 action_name) public returns(Act_SC act_scAddr) {
return action_scAddrMap[action_name];
}
}
contract Act_SC is usingOraclize{
event display(string x);
event display(string x,uint[] s);
event abort(string s,uint256 actionID);
event completed(string s,uint256 actionID);
event displayActionName(uint256 x);
struct Action{
uint256 Action_id;
string Action_URI;
bytes32 [] precond;
bytes32[] effects;
}
Action private action_1;
Action private action_2;
Action private action_3;
Action private action_4;
Action private action_5;
Action private action_6;
mapping(uint256 => Action) actionid_ActionMap;
// uint256 [] action_ids;
uint256[] public completedList;
function Act_SC(uint[] memory action_names){
for(uint256 x = 0; x < action_names.length; x++) {
if(action_names[x]==1){
action_1.Action_id=action_names[x];
action_1.Action_URI="http://a1.com";
action_1.precond.push("precond1_0");
action_1.precond.push("precond1_1");
action_1.effects.push("effects1_0");
action_1.effects.push("effects1_1");
actionid_ActionMap[1]=action_1;
}
else if(action_names[x]==2){
action_2.Action_id=action_names[x];
action_2.Action_URI="http://a2.com";
action_2.precond.push("precond2_0");
action_2.precond.push("precond2_1");
action_2.effects.push("effects2_0");
action_2.effects.push("effects2_1");
actionid_ActionMap[2]=action_2;
}
else if(action_names[x]==3){
action_2.Action_id=3;
action_2.Action_URI="http://a2.com";
action_2.precond.push("precond3_0");
action_2.precond.push("precond3_1");
action_2.effects.push("effects3_0");
action_2.effects.push("effects3_1");
actionid_ActionMap[3]=action_3;
}
else if(action_names[x]==4){
action_2.Action_id=4;
action_2.Action_URI="http://a2.com";
action_2.precond.push("precond2_0");
action_2.precond.push("precond2_1");
action_2.effects.push("effects2_0");
action_2.effects.push("effects2_1");
actionid_ActionMap[4]=action_4;
}
else if(action_names[x]==5){
action_2.Action_id=action_names[x];
action_2.Action_URI="http://a2.com";
action_2.precond.push("precond2_0");
action_2.precond.push("precond2_1");
action_2.effects.push("effects2_0");
action_2.effects.push("effects2_1");
actionid_ActionMap[5]=action_5;
}
else if(action_names[x]==6){
action_2.Action_id=action_names[x];
action_2.Action_URI="http://a2.com";
action_2.precond.push("precond2_0");
action_2.precond.push("precond2_1");
action_2.effects.push("effects2_0");
action_2.effects.push("effects2_1");
actionid_ActionMap[6]=action_6;
}
}
display("Act_SC_created for actions",action_names);
}
function getCompletedList() external returns(uint256[] list) {
return completedList;
}
function printAction() {
displayActionName(action_1.Action_id);
// display(a1.Action_name,a1.precond[0],a1.effects[0]);
}
function getPreCondition(uint256 Action_id) public returns (bytes32[] preCond){
return actionid_ActionMap[Action_id].precond;
}
function getEffect(uint256 Action_id) public returns(bytes32[] effect) {
return actionid_ActionMap[Action_id].effects;
}
function checkPreCondition(bytes32[] preCond,bytes32[] preCond_orac) returns(bool cond) {
return true;
}
function checkEffect(bytes32[] eff,bytes32[] eff_orac) public returns(bool cond) {
return true;
}
//oraclize start from here...
bytes32 [] orac_res;// for storing preCond and effects
function __callback(bytes32 myid, bytes32 [] result) {
if (msg.sender != oraclize_cbAddress()) throw;
orac_res = result;
}
function getPredicatesValues(uint256 Action_id,string s)returns(bytes32[] effect){
string action_url=actionid_ActionMap[Action_id].Action_URI;
oraclize_query("URL", action_url);// will call __callback
return orac_res;
}
function execute(uint256 Action_id) external{
bytes32[] memory preCond=getPreCondition(Action_id);
bytes32[] memory preCond_orac=getPredicatesValues(Action_id,"preCond");
if(checkPreCondition(preCond,preCond_orac)){
display("callAPI(action_api_map[actionID])");
}
else
abort("Transaction_Rollback",Action_id);
bytes32[] memory eff=getEffect(Action_id);
bytes32[] memory eff_orac=getPredicatesValues(Action_id,"eff");
if(checkEffect(eff,eff_orac)){
completedList.push(Action_id);
completed("Action_Completed", Action_id);
}
else
abort("Transaction_Rollback",Action_id);
}
}
pragma solidity ^0.4.0;
import "github.com/oraclize/ethereum-api/oraclizeAPI.sol";
contract WolframAlpha is usingOraclize {
string public temperature;
event newOraclizeQuery(string description);
event newTemperatureMeasure(string temperature);
function WolframAlpha() {
update();
}
function __callback(bytes32 myid, string result) {
if (msg.sender != oraclize_cbAddress()) throw;
temperature = result;
newTemperatureMeasure(temperature);
// do something with the temperature measure..
}
function update() payable {
newOraclizeQuery("Oraclize query was sent, standing by for the answer..");
oraclize_query("WolframAlpha", "temperature in London");
}
}
pragma solidity ^0.4.6;
contract LocalSC{
// list of all completed actions
uint[] public completedActions;
//stores localPlan as actions in array
localAction[] allActions;
//mapping from actionID to actual logic
mapping (string => Logic()) actionLogic;
struct localAction {
string[] actionsIn;
string[] actionsOut;
string[] preCond;
string[] effect;
string actionID;
}
function LocalSC() public{
string[] memory temp_IN = new string[](10);
string[] memory temp_Out = new string[](10);
string[] memory temp_precond = new string[](10);
string[] memory temp_effect = new string[](10);
temp_IN={};temp_precond={“at1-p1"};temp_effect={“at1-p2"};temp_Out={};unit actionID="a";
temp_IN={};temp_precond={“at1-p2"};temp_effect={“at1-p3"};temp_Out={"e"};unit actionID="d";
temp_IN={"e"};temp_precond={“at1-p3"};temp_effect={“at1-p4"};temp_Out={};unit actionID="f";
allActions.push(localAction(temp_IN,temp_Out,temp_precond,temp_effect,actionID));
}
fucntion execPlan(){
for (uint action = 0; action < allActions.length; action++) {
if(isSubset(allActions[action].actionsIn,completedActions) && getPrecond(allActions[action].preCond)){
// isSubset(a,b)- returns true if a is subset of b, to check if dependent action is completed
// getPrecond- Checks the sensor values over the given preconditions
executeAction(actionLogic(allActions[action].actionID));
// wait for Ack event from Actuation module
waitForEffectAck(Event e);
//add action id to the completedActions list
sendCompletion(allActions[action].actionsOut);
}
}
}
}
pragma solidity ^0.4.23;
contract ParentContract{
ConfigContract two;
constructor(ConfigContract _conf) public{
two = _conf;
}
function getNameFromOtherContract() public view returns(string) {
return two.getName();
}
}
contract ConfigContract{
constructor() public{
}
function getName() external pure returns (string) {
return "Amel";
}
}
@anshuiisc
Copy link
Author

HiPC- AIMB paper logic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment