Lefteris Karapetsas
10 years ago
15 changed files with 406 additions and 57 deletions
@ -0,0 +1,103 @@ |
|||
/*
|
|||
This file is part of cpp-ethereum. |
|||
|
|||
cpp-ethereum is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
cpp-ethereum is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU General Public License |
|||
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
|
|||
/** @file NatspecHandler.cpp
|
|||
* @author Lefteris Karapetsas <lefteris@ethdev.com> |
|||
* @date 2015 |
|||
*/ |
|||
#include "NatspecHandler.h" |
|||
#include <string> |
|||
#include <boost/filesystem.hpp> |
|||
|
|||
#include <libdevcore/Common.h> |
|||
#include <libdevcore/CommonData.h> |
|||
#include <libdevcore/Exceptions.h> |
|||
#include <libdevcore/Log.h> |
|||
#include <libdevcrypto/SHA3.h> |
|||
#include <libethereum/Defaults.h> |
|||
|
|||
using namespace dev; |
|||
using namespace dev::eth; |
|||
using namespace std; |
|||
|
|||
NatspecHandler::NatspecHandler() |
|||
{ |
|||
string path = Defaults::dbPath(); |
|||
boost::filesystem::create_directories(path); |
|||
ldb::Options o; |
|||
o.create_if_missing = true; |
|||
ldb::DB::Open(o, path + "/natspec", &m_db); |
|||
} |
|||
|
|||
NatspecHandler::~NatspecHandler() |
|||
{ |
|||
delete m_db; |
|||
} |
|||
|
|||
void NatspecHandler::add(dev::h256 const& _contractHash, string const& _doc) |
|||
{ |
|||
bytes k = _contractHash.asBytes(); |
|||
string v = _doc; |
|||
m_db->Put(m_writeOptions, ldb::Slice((char const*)k.data(), k.size()), ldb::Slice((char const*)v.data(), v.size())); |
|||
} |
|||
|
|||
string NatspecHandler::retrieve(dev::h256 const& _contractHash) const |
|||
{ |
|||
bytes k = _contractHash.asBytes(); |
|||
string ret; |
|||
m_db->Get(m_readOptions, ldb::Slice((char const*)k.data(), k.size()), &ret); |
|||
return ret; |
|||
} |
|||
|
|||
string NatspecHandler::getUserNotice(string const& json, dev::bytes const& _transactionData) |
|||
{ |
|||
Json::Value natspec; |
|||
Json::Value userNotice; |
|||
string retStr; |
|||
m_reader.parse(json, natspec); |
|||
bytes transactionFunctionPart(_transactionData.begin(), _transactionData.begin() + 4); |
|||
FixedHash<4> transactionFunctionHash(transactionFunctionPart); |
|||
|
|||
Json::Value methods = natspec["methods"]; |
|||
for (Json::ValueIterator it = methods.begin(); it != methods.end(); ++it) |
|||
{ |
|||
Json::Value keyValue = it.key(); |
|||
if (!keyValue.isString()) |
|||
BOOST_THROW_EXCEPTION(Exception() << errinfo_comment("Illegal Natspec JSON detected")); |
|||
|
|||
string functionSig = keyValue.asString(); |
|||
FixedHash<4> functionHash(dev::sha3(functionSig)); |
|||
|
|||
if (functionHash == transactionFunctionHash) |
|||
{ |
|||
Json::Value val = (*it)["notice"]; |
|||
if (!val.isString()) |
|||
BOOST_THROW_EXCEPTION(Exception() << errinfo_comment("Illegal Natspec JSON detected")); |
|||
return val.asString(); |
|||
} |
|||
} |
|||
|
|||
// not found
|
|||
return string(); |
|||
} |
|||
|
|||
string NatspecHandler::getUserNotice(dev::h256 const& _contractHash, dev::bytes const& _transactionData) |
|||
{ |
|||
return getUserNotice(retrieve(_contractHash), _transactionData); |
|||
} |
|||
|
|||
|
@ -0,0 +1,58 @@ |
|||
/*
|
|||
This file is part of cpp-ethereum. |
|||
|
|||
cpp-ethereum is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
cpp-ethereum is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU General Public License |
|||
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
|
|||
/** @file NatspecHandler.h
|
|||
* @author Lefteris Karapetsas <lefteris@ethdev.com> |
|||
* @date 2015 |
|||
*/ |
|||
|
|||
#pragma once |
|||
|
|||
#pragma warning(push) |
|||
#pragma warning(disable: 4100 4267) |
|||
#include <leveldb/db.h> |
|||
#pragma warning(pop) |
|||
#include <jsoncpp/json/json.h> |
|||
#include <libdevcore/FixedHash.h> |
|||
|
|||
namespace ldb = leveldb; |
|||
|
|||
class NatspecHandler |
|||
{ |
|||
public: |
|||
NatspecHandler(); |
|||
~NatspecHandler(); |
|||
|
|||
/// Stores locally in a levelDB a key value pair of contract code hash to natspec documentation
|
|||
void add(dev::h256 const& _contractHash, std::string const& _doc); |
|||
/// Retrieves the natspec documentation as a string given a contract code hash
|
|||
std::string retrieve(dev::h256 const& _contractHash) const; |
|||
|
|||
/// Given a json natspec string and the transaction data return the user notice
|
|||
std::string getUserNotice(std::string const& json, const dev::bytes& _transactionData); |
|||
/// Given a contract code hash and the transaction's data retrieve the natspec documention's
|
|||
/// user notice for that transaction.
|
|||
/// @returns The user notice or an empty string if no natspec for the contract exists
|
|||
/// or if the existing natspec does not document the @c _methodName
|
|||
std::string getUserNotice(dev::h256 const& _contractHash, dev::bytes const& _transactionDacta); |
|||
|
|||
private: |
|||
ldb::ReadOptions m_readOptions; |
|||
ldb::WriteOptions m_writeOptions; |
|||
ldb::DB* m_db; |
|||
Json::Reader m_reader; |
|||
}; |
@ -0,0 +1,77 @@ |
|||
<!doctype> |
|||
<html> |
|||
|
|||
<head> |
|||
<script type="text/javascript" src="js/es6-promise/promise.min.js"></script> |
|||
<script type="text/javascript" src="../dist/ethereum.js"></script> |
|||
<script type="text/javascript"> |
|||
|
|||
var web3 = require('web3'); |
|||
web3.setProvider(new web3.providers.AutoProvider()); |
|||
|
|||
// solidity source code |
|||
var source = "" + |
|||
"contract test {\n" + |
|||
" /// @notice Will multiplty `a` by 7. \n" + |
|||
" function multiply(uint a) returns(uint d) {\n" + |
|||
" return a * 7;\n" + |
|||
" }\n" + |
|||
"}\n"; |
|||
|
|||
// contract description, this will be autogenerated somehow |
|||
var desc = [{ |
|||
"name": "multiply", |
|||
"inputs": [ |
|||
{ |
|||
"name": "a", |
|||
"type": "uint256" |
|||
} |
|||
], |
|||
"outputs": [ |
|||
{ |
|||
"name": "d", |
|||
"type": "uint256" |
|||
} |
|||
] |
|||
}]; |
|||
|
|||
var contract; |
|||
|
|||
function createExampleContract() { |
|||
// hide create button |
|||
document.getElementById('create').style.visibility = 'hidden'; |
|||
document.getElementById('source').innerText = source; |
|||
|
|||
// create contract |
|||
web3.eth.transact({code: web3.eth.solidity(source)}).then(function (address) { |
|||
contract = web3.contract(address, desc); |
|||
document.getElementById('call').style.visibility = 'visible'; |
|||
}); |
|||
} |
|||
|
|||
function callExampleContract() { |
|||
// this should be generated by ethereum |
|||
var param = parseInt(document.getElementById('value').value); |
|||
|
|||
// call the contract |
|||
contract.multiply(param).transact().then(function(res) { |
|||
document.getElementById('result').innerText = res[0]; |
|||
}); |
|||
} |
|||
|
|||
</script> |
|||
</head> |
|||
<body> |
|||
<h1>contract</h1> |
|||
<div id="source"></div> |
|||
<div id='create'> |
|||
<button type="button" onClick="createExampleContract();">create example contract</button> |
|||
</div> |
|||
<div id='call' style='visibility: hidden;'> |
|||
<input type="number" id="value"></input> |
|||
<button type="button" onClick="callExampleContract()">Call Contract</button> |
|||
</div> |
|||
<div id="result"></div> |
|||
</body> |
|||
</html> |
|||
|
Loading…
Reference in new issue