Skip to content

Commit 3faecfa

Browse files
committed
assert
1 parent 58d15d6 commit 3faecfa

File tree

12 files changed

+853
-18
lines changed

12 files changed

+853
-18
lines changed

eosio.assert/abi/eosio.assert.abi

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@
148148
{
149149
"name": "actions",
150150
"type": "contract_action[]"
151+
},
152+
{
153+
"name": "abi_hashes",
154+
"type": "checksum256[]"
151155
}
152156
]
153157
}

eosio.assert/include/eosio.assert/eosio.assert.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,13 @@ using manifests =
8888
indexed_by<"id"_n, eosio::const_mem_fun<stored_manifest, key256, &stored_manifest::id_key>>>;
8989
using manifests_id_index = decltype(std::declval<manifests>().get_index<"id"_n>());
9090

91+
struct abi_hash {
92+
name owner;
93+
checksum256 hash;
94+
95+
uint64_t primary_key() const { return owner; }
96+
};
97+
98+
using abi_hash_table = eosio::multi_index<"abihash"_n, abi_hash>;
99+
91100
} // namespace assert_contract

eosio.assert/src/eosio.assert.cpp

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,20 +69,46 @@ struct asserter {
6969
actions.find({name{0}, name{0}}) != actions.end();
7070
}
7171

72+
static std::string hash_to_str(const checksum256& hash) {
73+
static const char table[] = "0123456789abcdef";
74+
std::string result;
75+
for (uint8_t byte : hash.hash) {
76+
result += table[byte >> 4];
77+
result += table[byte & 0xf];
78+
}
79+
return result;
80+
}
81+
7282
void require(const checksum256& chain_params_hash, const checksum256& manifest_id,
73-
const vector<contract_action>& actions) {
74-
eosio_assert(chain_params_hash == chain.hash, "Incorrect chain");
83+
const vector<contract_action>& actions, const vector<checksum256>& abi_hashes) {
84+
if (!(chain_params_hash == chain.hash))
85+
eosio_assert(false, ("chain hash is " + hash_to_str(chain.hash) + " but user expected " +
86+
hash_to_str(chain_params_hash))
87+
.c_str());
7588
auto it = manifest_id_idx.find(to_key256(manifest_id));
7689
eosio_assert(it != manifest_id_idx.end(), "manifest not found");
90+
flat_set<name> contracts;
7791
for (auto& action : actions) {
92+
contracts.insert(action.contract);
7893
if (!in(action, it->whitelist))
7994
eosio_assert(
8095
false,
8196
(action.action.to_string() + "@" + action.contract.to_string() + " is not in whitelist").c_str());
8297
if (in(action, it->blacklist))
83-
eosio_assert(
84-
false,
85-
(action.action.to_string() + "@" + action.contract.to_string() + " is in blacklist").c_str());
98+
eosio_assert(false,
99+
(action.action.to_string() + "@" + action.contract.to_string() + " is in blacklist").c_str());
100+
}
101+
abi_hash_table table{"eosio"_n, "eosio"_n};
102+
eosio_assert(abi_hashes.size() == contracts.size(), "incorrect number of abi hashes");
103+
for (size_t i = 0; i < abi_hashes.size(); ++i) {
104+
auto it = table.find(*contracts.nth(i));
105+
checksum256 hash{};
106+
if (it != table.end())
107+
hash = it->hash;
108+
if (!(abi_hashes[i] == hash))
109+
eosio_assert(false, (contracts.nth(i)->to_string() + " abi hash is " + hash_to_str(hash) +
110+
" but user expected " + hash_to_str(abi_hashes[i]))
111+
.c_str());
86112
}
87113
}
88114

tests/contracts.hpp.in

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,12 @@ namespace eosio { namespace testing {
66
struct contracts {
77
static std::vector<uint8_t> eosio_assert_wasm() { return read_wasm("${CMAKE_BINARY_DIR}/../eosio.assert/eosio.assert.wasm"); }
88
static std::vector<char> eosio_assert_abi() { return read_abi("${CMAKE_BINARY_DIR}/../eosio.assert/eosio.assert.abi"); }
9+
10+
static std::vector<uint8_t> test_bios_wasm() { return read_wasm("${CMAKE_SOURCE_DIR}/test_contracts/eosio.bios.wasm"); }
11+
static std::vector<char> test_bios_abi() { return read_abi("${CMAKE_SOURCE_DIR}/test_contracts/eosio.bios.abi"); }
12+
13+
static std::vector<char> test_foo_abi() { return read_abi("${CMAKE_SOURCE_DIR}/test_contracts/foo.abi"); }
14+
static std::vector<char> test_bar_abi() { return read_abi("${CMAKE_SOURCE_DIR}/test_contracts/bar.abi"); }
15+
static std::vector<char> test_baz_abi() { return read_abi("${CMAKE_SOURCE_DIR}/test_contracts/baz.abi"); }
916
};
1017
}} //ns eosio::testing

tests/data/require.expected

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,13 @@ push_transaction {
167167
"data": {
168168
"chain_params_hash": "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF",
169169
"manifest_id": "BEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEF",
170-
"actions": []
170+
"actions": [],
171+
"abi_hashes": []
171172
}
172173
}
173174
]
174175
}
175-
Exception: assertion failure with message: Incorrect chain
176+
Exception: assertion failure with message: chain hash is a5e2578a54c35885716a63d70d4b51b227d8aa47ad9a3163c733b79160bb513c but user expected 0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef
176177

177178
require: unknown manifest
178179
=========================
@@ -188,7 +189,8 @@ push_transaction {
188189
"data": {
189190
"chain_params_hash": "a5e2578a54c35885716a63d70d4b51b227d8aa47ad9a3163c733b79160bb513c",
190191
"manifest_id": "BEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEFBEEF",
191-
"actions": []
192+
"actions": [],
193+
"abi_hashes": []
192194
}
193195
}
194196
]
@@ -209,7 +211,8 @@ push_transaction {
209211
"data": {
210212
"chain_params_hash": "a5e2578a54c35885716a63d70d4b51b227d8aa47ad9a3163c733b79160bb513c",
211213
"manifest_id": "bc9423a3524430a0bf83f9bdaad133b2c874b916fb7129df73d66080713bd49f",
212-
"actions": []
214+
"actions": [],
215+
"abi_hashes": []
213216
}
214217
}
215218
]
@@ -234,6 +237,9 @@ push_transaction {
234237
"contract": "contract.1",
235238
"action": "just.this"
236239
}
240+
],
241+
"abi_hashes": [
242+
"0000000000000000000000000000000000000000000000000000000000000000"
237243
]
238244
}
239245
}
@@ -259,6 +265,9 @@ push_transaction {
259265
"contract": "contract.2",
260266
"action": "foo"
261267
}
268+
],
269+
"abi_hashes": [
270+
"0000000000000000000000000000000000000000000000000000000000000000"
262271
]
263272
}
264273
}
@@ -284,6 +293,9 @@ push_transaction {
284293
"contract": "unknown",
285294
"action": "transfer"
286295
}
296+
],
297+
"abi_hashes": [
298+
"0000000000000000000000000000000000000000000000000000000000000000"
287299
]
288300
}
289301
}
@@ -309,6 +321,9 @@ push_transaction {
309321
"contract": "unk.account",
310322
"action": "unk.action"
311323
}
324+
],
325+
"abi_hashes": [
326+
"0000000000000000000000000000000000000000000000000000000000000000"
312327
]
313328
}
314329
}
@@ -334,6 +349,9 @@ push_transaction {
334349
"contract": "unk.account",
335350
"action": "unk.action"
336351
}
352+
],
353+
"abi_hashes": [
354+
"0000000000000000000000000000000000000000000000000000000000000000"
337355
]
338356
}
339357
}
@@ -359,6 +377,9 @@ push_transaction {
359377
"contract": "bad.token",
360378
"action": "transfer"
361379
}
380+
],
381+
"abi_hashes": [
382+
"0000000000000000000000000000000000000000000000000000000000000000"
362383
]
363384
}
364385
}

0 commit comments

Comments
 (0)