Skip to content
This repository was archived by the owner on Oct 28, 2021. It is now read-only.

Commit 9421227

Browse files
committed
Make precompiles return success condition.
1 parent ffcbe7b commit 9421227

File tree

7 files changed

+29
-20
lines changed

7 files changed

+29
-20
lines changed

libethcore/ChainOperationParams.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ using namespace eth;
2929
PrecompiledContract::PrecompiledContract(
3030
unsigned _base,
3131
unsigned _word,
32-
std::function<bytes(bytesConstRef)> const& _exec,
32+
PrecompiledExecutor const& _exec,
3333
u256 const& _startingBlock
3434
):
3535
PrecompiledContract([=](unsigned size) -> bigint

libethcore/ChainOperationParams.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
#include <libdevcore/Common.h>
2525
#include "Common.h"
26+
#include <libethcore/Precompiled.h>
27+
2628
#include <libevmcore/EVMSchedule.h>
2729

2830
namespace dev
@@ -36,8 +38,8 @@ class PrecompiledContract
3638
PrecompiledContract() = default;
3739
PrecompiledContract(
3840
std::function<bigint(size_t)> const& _cost,
39-
std::function<bytes(bytesConstRef)> const& _exec,
40-
u256 const& _startingBlock
41+
PrecompiledExecutor const& _exec,
42+
u256 const& _startingBlock = 0
4143
):
4244
m_cost(_cost),
4345
m_execute(_exec),
@@ -46,18 +48,18 @@ class PrecompiledContract
4648
PrecompiledContract(
4749
unsigned _base,
4850
unsigned _word,
49-
std::function<bytes(bytesConstRef)> const& _exec,
50-
u256 const& _startingBlock
51+
PrecompiledExecutor const& _exec,
52+
u256 const& _startingBlock = 0
5153
);
5254

5355
bigint cost(bytesConstRef _in) const { return m_cost(_in.size()); }
54-
bytes execute(bytesConstRef _in) const { return m_execute(_in); }
56+
std::pair<bool, bytes> execute(bytesConstRef _in) const { return m_execute(_in); }
5557

5658
u256 const& startingBlock() const { return m_startingBlock; }
5759

5860
private:
5961
std::function<bigint(size_t)> m_cost;
60-
std::function<bytes(bytesConstRef)> m_execute;
62+
PrecompiledExecutor m_execute;
6163
u256 m_startingBlock = 0;
6264
};
6365

libethcore/Precompiled.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,28 +66,28 @@ ETH_REGISTER_PRECOMPILED(ecrecover)(bytesConstRef _in)
6666
{
6767
ret = dev::sha3(rec);
6868
memset(ret.data(), 0, 12);
69-
return ret.asBytes();
69+
return {true, ret.asBytes()};
7070
}
7171
}
7272
catch (...) {}
7373
}
7474
}
75-
return {};
75+
return {true, {}};
7676
}
7777

7878
ETH_REGISTER_PRECOMPILED(sha256)(bytesConstRef _in)
7979
{
80-
return dev::sha256(_in).asBytes();
80+
return {true, dev::sha256(_in).asBytes()};
8181
}
8282

8383
ETH_REGISTER_PRECOMPILED(ripemd160)(bytesConstRef _in)
8484
{
85-
return h256(dev::ripemd160(_in), h256::AlignRight).asBytes();
85+
return {true, h256(dev::ripemd160(_in), h256::AlignRight).asBytes()};
8686
}
8787

8888
ETH_REGISTER_PRECOMPILED(identity)(bytesConstRef _in)
8989
{
90-
return _in.toBytes();
90+
return {true, _in.toBytes()};
9191
}
9292

9393
}

libethcore/Precompiled.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ namespace dev
3131
namespace eth
3232
{
3333

34-
using PrecompiledExecutor = std::function<bytes(bytesConstRef _in)>;
34+
using PrecompiledExecutor = std::function<std::pair<bool, bytes>(bytesConstRef _in)>;
3535

3636
DEV_SIMPLE_EXCEPTION(ExecutorNotFound);
3737

@@ -54,7 +54,7 @@ class PrecompiledRegistrar
5454
};
5555

5656
// TODO: unregister on unload with a static object.
57-
#define ETH_REGISTER_PRECOMPILED(Name) static bytes __eth_registerPrecompiledFunction ## Name(bytesConstRef _in); static PrecompiledExecutor __eth_registerPrecompiledFactory ## Name = ::dev::eth::PrecompiledRegistrar::registerPrecompiled(#Name, &__eth_registerPrecompiledFunction ## Name); static bytes __eth_registerPrecompiledFunction ## Name
57+
#define ETH_REGISTER_PRECOMPILED(Name) static std::pair<bool, bytes> __eth_registerPrecompiledFunction ## Name(bytesConstRef _in); static PrecompiledExecutor __eth_registerPrecompiledFactory ## Name = ::dev::eth::PrecompiledRegistrar::registerPrecompiled(#Name, &__eth_registerPrecompiledFunction ## Name); static std::pair<bool, bytes> __eth_registerPrecompiledFunction ## Name
5858

5959
}
6060
}

libethcore/SealEngine.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class SealEngineFace
8282
return m_params.precompiled.count(_a) != 0 && _blockNumber >= m_params.precompiled.at(_a).startingBlock();
8383
}
8484
virtual bigint costOfPrecompiled(Address const& _a, bytesConstRef _in, u256 const&) const { return m_params.precompiled.at(_a).cost(_in); }
85-
virtual bytes executePrecompiled(Address const& _a, bytesConstRef _in, u256 const&) const { return m_params.precompiled.at(_a).execute(_in); }
85+
virtual std::pair<bool, bytes> executePrecompiled(Address const& _a, bytesConstRef _in, u256 const&) const { return m_params.precompiled.at(_a).execute(_in); }
8686

8787
protected:
8888
virtual bool onOptionChanging(std::string const&, bytes const&) { return true; }

libethereum/Account.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,17 +116,17 @@ AccountMap dev::eth::jsonToAccountMap(std::string const& _json, u256 const& _def
116116
{
117117
js::mObject p = o["precompiled"].get_obj();
118118
auto n = p["name"].get_str();
119-
if (!o.count("linear"))
119+
if (!p.count("linear"))
120120
{
121-
cwarn << "No gas cost given for precompiled contract " << n << endl;
121+
cwarn << "No gas cost given for precompiled contract " << n;
122122
throw;
123123
}
124124
try
125125
{
126126
auto l = p["linear"].get_obj();
127127
u256 startingBlock = 0;
128128
if (p.count("startingBlock"))
129-
startingBlock = u256(p["startingBlock"]);
129+
startingBlock = u256(p["startingBlock"].get_str());
130130
unsigned base = toUnsigned(l["base"]);
131131
unsigned word = toUnsigned(l["word"]);
132132
o_precompiled->insert(make_pair(a, PrecompiledContract(base, word, PrecompiledRegistrar::executor(n), startingBlock)));

libethereum/Executive.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,14 @@ bool Executive::call(CallParameters const& _p, u256 const& _gasPrice, Address co
274274
else
275275
{
276276
m_gas = (u256)(_p.gas - g);
277-
bytes output = m_sealEngine.executePrecompiled(_p.codeAddress, _p.data, m_envInfo.number());
277+
bytes output;
278+
bool success;
279+
tie(success, output) = m_sealEngine.executePrecompiled(_p.codeAddress, _p.data, m_envInfo.number());
280+
if (!success)
281+
{
282+
m_gas = 0;
283+
m_excepted = TransactionException::OutOfGas;
284+
}
278285
size_t outputSize = output.size();
279286
m_output = owning_bytes_ref{std::move(output), 0, outputSize};
280287
}
@@ -473,4 +480,4 @@ void Executive::revert()
473480
// Set result address to the null one.
474481
m_newAddress = {};
475482
m_s.rollback(m_savepoint);
476-
}
483+
}

0 commit comments

Comments
 (0)