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

Commit 94880a2

Browse files
committed
Add "starting block" option for precompiled contracts.
1 parent ccb8af1 commit 94880a2

File tree

9 files changed

+55
-23
lines changed

9 files changed

+55
-23
lines changed

deps

Submodule deps updated 1 file

libethashseal/genesis/mainNetwork.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ R"E(
5656
"0000000000000000000000000000000000000002": { "precompiled": { "name": "sha256", "linear": { "base": 60, "word": 12 } } },
5757
"0000000000000000000000000000000000000003": { "precompiled": { "name": "ripemd160", "linear": { "base": 600, "word": 120 } } },
5858
"0000000000000000000000000000000000000004": { "precompiled": { "name": "identity", "linear": { "base": 15, "word": 3 } } },
59+
"0000000000000000000000000000000000000006": { "precompiled": { "name": "alt_bn128_G1_add", "startingBlock": "0xffffffffffffffffff", "linear": { "base": 15, "word": 3 } } },
60+
"0000000000000000000000000000000000000007": { "precompiled": { "name": "alt_bn128_G1_mul", "startingBlock": "0xffffffffffffffffff", "linear": { "base": 15, "word": 3 } } },
61+
"0000000000000000000000000000000000000008": { "precompiled": { "name": "alt_bn128_pairing_product", "startingBlock": "0xffffffffffffffffff", "linear": { "base": 15, "word": 3 } } },
5962
"3282791d6fd713f1e94f4bfd565eaa78b3a0599d": {
6063
"balance": "1337000000000000000000"
6164
},

libethashseal/genesis/metropolisTest.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ R"E(
5454
"0000000000000000000000000000000000000001": { "wei": "1", "precompiled": { "name": "ecrecover", "linear": { "base": 3000, "word": 0 } } },
5555
"0000000000000000000000000000000000000002": { "wei": "1", "precompiled": { "name": "sha256", "linear": { "base": 60, "word": 12 } } },
5656
"0000000000000000000000000000000000000003": { "wei": "1", "precompiled": { "name": "ripemd160", "linear": { "base": 600, "word": 120 } } },
57-
"0000000000000000000000000000000000000004": { "wei": "1", "precompiled": { "name": "identity", "linear": { "base": 15, "word": 3 } } }
58-
}
57+
"0000000000000000000000000000000000000004": { "wei": "1", "precompiled": { "name": "identity", "linear": { "base": 15, "word": 3 } } },
58+
"0000000000000000000000000000000000000006": { "wei": "1", "precompiled": { "name": "alt_bn128_G1_add", "linear": { "base": 15, "word": 3 } } },
59+
"0000000000000000000000000000000000000007": { "wei": "1", "precompiled": { "name": "alt_bn128_G1_mul", "linear": { "base": 15, "word": 3 } } },
60+
"0000000000000000000000000000000000000008": { "wei": "1", "precompiled": { "name": "alt_bn128_pairing_product", "linear": { "base": 15, "word": 3 } } },
61+
}
5962
}
6063
)E";

libethcore/ChainOperationParams.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,19 @@ using namespace std;
2626
using namespace dev;
2727
using namespace eth;
2828

29-
PrecompiledContract::PrecompiledContract(unsigned _base, unsigned _word, std::function<bytes(bytesConstRef)> const& _exec):
29+
PrecompiledContract::PrecompiledContract(
30+
unsigned _base,
31+
unsigned _word,
32+
std::function<bytes(bytesConstRef)> const& _exec,
33+
u256 const& _startingBlock
34+
):
3035
PrecompiledContract([=](unsigned size) -> bigint
3136
{
3237
bigint s = size;
3338
bigint b = _base;
3439
bigint w = _word;
3540
return b + (s + 31) / 32 * w;
36-
}, _exec)
41+
}, _exec, _startingBlock)
3742
{}
3843

3944
ChainOperationParams::ChainOperationParams()

libethcore/ChainOperationParams.h

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,31 @@ class PrecompiledContract
3434
{
3535
public:
3636
PrecompiledContract() = default;
37-
PrecompiledContract(std::function<bigint(size_t)> const& _cost, std::function<bytes(bytesConstRef)> const& _exec):
37+
PrecompiledContract(
38+
std::function<bigint(size_t)> const& _cost,
39+
std::function<bytes(bytesConstRef)> const& _exec,
40+
u256 const& _startingBlock
41+
):
3842
m_cost(_cost),
39-
m_execute(_exec)
43+
m_execute(_exec),
44+
m_startingBlock(_startingBlock)
4045
{}
41-
PrecompiledContract(unsigned _base, unsigned _word, std::function<bytes(bytesConstRef)> const& _exec);
46+
PrecompiledContract(
47+
unsigned _base,
48+
unsigned _word,
49+
std::function<bytes(bytesConstRef)> const& _exec,
50+
u256 const& _startingBlock
51+
);
4252

4353
bigint cost(bytesConstRef _in) const { return m_cost(_in.size()); }
4454
bytes execute(bytesConstRef _in) const { return m_execute(_in); }
4555

56+
u256 const& startingBlock() const { return m_startingBlock; }
57+
4658
private:
4759
std::function<bigint(size_t)> m_cost;
4860
std::function<bytes(bytesConstRef)> m_execute;
61+
u256 m_startingBlock = 0;
4962
};
5063

5164
struct ChainOperationParams

libethcore/SealEngine.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,12 @@ class SealEngineFace
7777
SealEngineFace* withChainParams(ChainOperationParams const& _params) { setChainParams(_params); return this; }
7878
virtual EVMSchedule const& evmSchedule(EnvInfo const&) const = 0;
7979

80-
virtual bool isPrecompiled(Address const& _a) const { return m_params.precompiled.count(_a) != 0; }
81-
virtual bigint costOfPrecompiled(Address const& _a, bytesConstRef _in) const { return m_params.precompiled.at(_a).cost(_in); }
82-
virtual bytes executePrecompiled(Address const& _a, bytesConstRef _in) const { return m_params.precompiled.at(_a).execute(_in); }
80+
virtual bool isPrecompiled(Address const& _a, u256 const& _blockNumber) const
81+
{
82+
return m_params.precompiled.count(_a) != 0 && _blockNumber >= m_params.precompiled.at(_a).startingBlock();
83+
}
84+
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); }
8386

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

libethereum/Account.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,20 @@ 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"))
120+
{
121+
cwarn << "No gas cost given for precompiled contract " << n << endl;
122+
throw;
123+
}
119124
try
120125
{
121-
if (p.count("linear"))
122-
{
123-
auto l = p["linear"].get_obj();
124-
unsigned base = toUnsigned(l["base"]);
125-
unsigned word = toUnsigned(l["word"]);
126-
o_precompiled->insert(make_pair(a, PrecompiledContract(base, word, PrecompiledRegistrar::executor(n))));
127-
}
126+
auto l = p["linear"].get_obj();
127+
u256 startingBlock = 0;
128+
if (p.count("startingBlock"))
129+
startingBlock = u256(p["startingBlock"]);
130+
unsigned base = toUnsigned(l["base"]);
131+
unsigned word = toUnsigned(l["word"]);
132+
o_precompiled->insert(make_pair(a, PrecompiledContract(base, word, PrecompiledRegistrar::executor(n), startingBlock)));
128133
}
129134
catch (ExecutorNotFound)
130135
{

libethereum/Executive.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,9 @@ bool Executive::call(CallParameters const& _p, u256 const& _gasPrice, Address co
262262

263263
m_savepoint = m_s.savepoint();
264264

265-
if (m_sealEngine.isPrecompiled(_p.codeAddress))
265+
if (m_sealEngine.isPrecompiled(_p.codeAddress, m_envInfo.number()))
266266
{
267-
bigint g = m_sealEngine.costOfPrecompiled(_p.codeAddress, _p.data);
267+
bigint g = m_sealEngine.costOfPrecompiled(_p.codeAddress, _p.data, m_envInfo.number());
268268
if (_p.gas < g)
269269
{
270270
m_excepted = TransactionException::OutOfGasBase;
@@ -274,7 +274,7 @@ 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);
277+
bytes output = m_sealEngine.executePrecompiled(_p.codeAddress, _p.data, m_envInfo.number());
278278
size_t outputSize = output.size();
279279
m_output = owning_bytes_ref{std::move(output), 0, outputSize};
280280
}
@@ -473,4 +473,4 @@ void Executive::revert()
473473
// Set result address to the null one.
474474
m_newAddress = {};
475475
m_s.rollback(m_savepoint);
476-
}
476+
}

libweb3jsonrpc/DebugFace.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace dev {
1616
{
1717
this->bindAndAddMethod(jsonrpc::Procedure("debug_traceTransaction", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_OBJECT, "param1",jsonrpc::JSON_STRING,"param2",jsonrpc::JSON_OBJECT, NULL), &dev::rpc::DebugFace::debug_traceTransactionI);
1818
this->bindAndAddMethod(jsonrpc::Procedure("debug_storageRangeAt", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_OBJECT, "param1",jsonrpc::JSON_STRING,"param2",jsonrpc::JSON_INTEGER,"param3",jsonrpc::JSON_STRING,"param4",jsonrpc::JSON_STRING,"param5",jsonrpc::JSON_INTEGER, NULL), &dev::rpc::DebugFace::debug_storageRangeAtI);
19-
this->bindAndAddMethod(jsonrpc::Procedure("debug_preimage", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_OBJECT, "param1",jsonrpc::JSON_STRING, NULL), &dev::rpc::DebugFace::debug_preimageI);
19+
this->bindAndAddMethod(jsonrpc::Procedure("debug_preimage", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_STRING, "param1",jsonrpc::JSON_STRING, NULL), &dev::rpc::DebugFace::debug_preimageI);
2020
this->bindAndAddMethod(jsonrpc::Procedure("debug_traceBlockByNumber", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_OBJECT, "param1",jsonrpc::JSON_INTEGER,"param2",jsonrpc::JSON_OBJECT, NULL), &dev::rpc::DebugFace::debug_traceBlockByNumberI);
2121
this->bindAndAddMethod(jsonrpc::Procedure("debug_traceBlockByHash", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_OBJECT, "param1",jsonrpc::JSON_STRING,"param2",jsonrpc::JSON_OBJECT, NULL), &dev::rpc::DebugFace::debug_traceBlockByHashI);
2222
this->bindAndAddMethod(jsonrpc::Procedure("debug_traceCall", jsonrpc::PARAMS_BY_POSITION, jsonrpc::JSON_OBJECT, "param1",jsonrpc::JSON_OBJECT,"param2",jsonrpc::JSON_STRING,"param3",jsonrpc::JSON_OBJECT, NULL), &dev::rpc::DebugFace::debug_traceCallI);

0 commit comments

Comments
 (0)