From a3ea170751ce03b988c3c6e4f77206980c56c748 Mon Sep 17 00:00:00 2001 From: subtly Date: Tue, 4 Nov 2014 10:56:22 +0100 Subject: [PATCH 1/4] json rpc only runs once, and only when json rpc test suite is invoked --- test/jsonrpc.cpp | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/test/jsonrpc.cpp b/test/jsonrpc.cpp index 033339ec2..53f51ecad 100644 --- a/test/jsonrpc.cpp +++ b/test/jsonrpc.cpp @@ -43,9 +43,6 @@ using namespace dev; using namespace dev::eth; namespace js = json_spirit; -namespace jsonrpc_tests -{ - string name = "Ethereum(++) tests"; string dbPath; auto s = set{"eth", "shh"}; @@ -74,7 +71,7 @@ struct JsonrpcFixture { } }; -BOOST_GLOBAL_FIXTURE(JsonrpcFixture) +const JsonrpcFixture testJsonRpcServer; BOOST_AUTO_TEST_CASE(jsonrpc_defaultBlock) { @@ -243,8 +240,6 @@ BOOST_AUTO_TEST_CASE(jsonrpc_transact) BOOST_CHECK_EQUAL(txAmount, balance2); } -} - BOOST_AUTO_TEST_SUITE_END() #endif From ae8d6e0a3a783f09925370588c021b65f0674ae0 Mon Sep 17 00:00:00 2001 From: subtly Date: Tue, 4 Nov 2014 15:24:02 +0100 Subject: [PATCH 2/4] coding standards. suite setup struct. --- test/jsonrpc.cpp | 91 ++++++++++++++++++++++++------------------------ 1 file changed, 45 insertions(+), 46 deletions(-) diff --git a/test/jsonrpc.cpp b/test/jsonrpc.cpp index 53f51ecad..68c30734a 100644 --- a/test/jsonrpc.cpp +++ b/test/jsonrpc.cpp @@ -43,41 +43,39 @@ using namespace dev; using namespace dev::eth; namespace js = json_spirit; -string name = "Ethereum(++) tests"; -string dbPath; -auto s = set{"eth", "shh"}; -dev::p2p::NetworkPreferences np(30303, std::string(), false); -dev::WebThreeDirect web3(name, dbPath, true, s, np); - +WebThreeDirect *web3; unique_ptr jsonrpcServer; unique_ptr jsonrpcClient; -struct JsonrpcFixture { - JsonrpcFixture() +struct Setup +{ + Setup() { - cnote << "setup jsonrpc"; + static bool setup = false; + if (setup) + return; + setup = true; - web3.setIdealPeerCount(5); - web3.ethereum()->setForceMining(true); - jsonrpcServer = unique_ptr(new WebThreeStubServer(new jsonrpc::CorsHttpServer(8080), web3, {})); + dev::p2p::NetworkPreferences nprefs(30303, std::string(), false); + web3 = new WebThreeDirect("Ethereum(++) tests", "", true, {"eth", "shh"}, nprefs); + + web3->setIdealPeerCount(5); + web3->ethereum()->setForceMining(true); + jsonrpcServer = unique_ptr(new WebThreeStubServer(new jsonrpc::CorsHttpServer(8080), *web3, {})); jsonrpcServer->setIdentities({}); jsonrpcServer->StartListening(); jsonrpcClient = unique_ptr(new WebThreeStubClient(new jsonrpc::HttpClient("http://localhost:8080"))); } - ~JsonrpcFixture() - { - cnote << "teardown jsonrpc"; - } }; -const JsonrpcFixture testJsonRpcServer; +BOOST_FIXTURE_TEST_SUITE(environment, Setup) BOOST_AUTO_TEST_CASE(jsonrpc_defaultBlock) { cnote << "Testing jsonrpc defaultBlock..."; int defaultBlock = jsonrpcClient->defaultBlock(); - BOOST_CHECK_EQUAL(defaultBlock, web3.ethereum()->getDefault()); + BOOST_CHECK_EQUAL(defaultBlock, web3->ethereum()->getDefault()); } BOOST_AUTO_TEST_CASE(jsonrpc_gasPrice) @@ -91,26 +89,26 @@ BOOST_AUTO_TEST_CASE(jsonrpc_isListening) { cnote << "Testing jsonrpc isListening..."; - web3.startNetwork(); + web3->startNetwork(); bool listeningOn = jsonrpcClient->listening(); - BOOST_CHECK_EQUAL(listeningOn, web3.isNetworkStarted()); + BOOST_CHECK_EQUAL(listeningOn, web3->isNetworkStarted()); - web3.stopNetwork(); + web3->stopNetwork(); bool listeningOff = jsonrpcClient->listening(); - BOOST_CHECK_EQUAL(listeningOff, web3.isNetworkStarted()); + BOOST_CHECK_EQUAL(listeningOff, web3->isNetworkStarted()); } BOOST_AUTO_TEST_CASE(jsonrpc_isMining) { cnote << "Testing jsonrpc isMining..."; - web3.ethereum()->startMining(); + web3->ethereum()->startMining(); bool miningOn = jsonrpcClient->mining(); - BOOST_CHECK_EQUAL(miningOn, web3.ethereum()->isMining()); + BOOST_CHECK_EQUAL(miningOn, web3->ethereum()->isMining()); - web3.ethereum()->stopMining(); + web3->ethereum()->stopMining(); bool miningOff = jsonrpcClient->mining(); - BOOST_CHECK_EQUAL(miningOff, web3.ethereum()->isMining()); + BOOST_CHECK_EQUAL(miningOff, web3->ethereum()->isMining()); } BOOST_AUTO_TEST_CASE(jsonrpc_accounts) @@ -136,18 +134,18 @@ BOOST_AUTO_TEST_CASE(jsonrpc_number) { cnote << "Testing jsonrpc number2..."; int number = jsonrpcClient->number(); - BOOST_CHECK_EQUAL(number, web3.ethereum()->number() + 1); - dev::eth::mine(*(web3.ethereum()), 1); + BOOST_CHECK_EQUAL(number, web3->ethereum()->number() + 1); + dev::eth::mine(*(web3->ethereum()), 1); int numberAfter = jsonrpcClient->number(); BOOST_CHECK_EQUAL(number + 1, numberAfter); - BOOST_CHECK_EQUAL(numberAfter, web3.ethereum()->number() + 1); + BOOST_CHECK_EQUAL(numberAfter, web3->ethereum()->number() + 1); } BOOST_AUTO_TEST_CASE(jsonrpc_peerCount) { cnote << "Testing jsonrpc peerCount..."; int peerCount = jsonrpcClient->peerCount(); - BOOST_CHECK_EQUAL(web3.peerCount(), peerCount); + BOOST_CHECK_EQUAL(web3->peerCount(), peerCount); } BOOST_AUTO_TEST_CASE(jsonrpc_setListening) @@ -155,10 +153,10 @@ BOOST_AUTO_TEST_CASE(jsonrpc_setListening) cnote << "Testing jsonrpc setListening..."; jsonrpcClient->setListening(true); - BOOST_CHECK_EQUAL(web3.isNetworkStarted(), true); + BOOST_CHECK_EQUAL(web3->isNetworkStarted(), true); jsonrpcClient->setListening(false); - BOOST_CHECK_EQUAL(web3.isNetworkStarted(), false); + BOOST_CHECK_EQUAL(web3->isNetworkStarted(), false); } BOOST_AUTO_TEST_CASE(jsonrpc_setMining) @@ -166,10 +164,10 @@ BOOST_AUTO_TEST_CASE(jsonrpc_setMining) cnote << "Testing jsonrpc setMining..."; jsonrpcClient->setMining(true); - BOOST_CHECK_EQUAL(web3.ethereum()->isMining(), true); + BOOST_CHECK_EQUAL(web3->ethereum()->isMining(), true); jsonrpcClient->setMining(false); - BOOST_CHECK_EQUAL(web3.ethereum()->isMining(), false); + BOOST_CHECK_EQUAL(web3->ethereum()->isMining(), false); } BOOST_AUTO_TEST_CASE(jsonrpc_stateAt) @@ -178,36 +176,36 @@ BOOST_AUTO_TEST_CASE(jsonrpc_stateAt) dev::KeyPair key = KeyPair::create(); auto address = key.address(); string stateAt = jsonrpcClient->stateAt(toJS(address), "0"); - BOOST_CHECK_EQUAL(toJS(web3.ethereum()->stateAt(address, jsToU256("0"), 0)), stateAt); + BOOST_CHECK_EQUAL(toJS(web3->ethereum()->stateAt(address, jsToU256("0"), 0)), stateAt); } BOOST_AUTO_TEST_CASE(jsonrpc_transact) { cnote << "Testing jsonrpc transact..."; string coinbase = jsonrpcClient->coinbase(); - BOOST_CHECK_EQUAL(jsToAddress(coinbase), web3.ethereum()->address()); + BOOST_CHECK_EQUAL(jsToAddress(coinbase), web3->ethereum()->address()); dev::KeyPair key = KeyPair::create(); auto address = key.address(); auto receiver = KeyPair::create(); - web3.ethereum()->setAddress(address); + web3->ethereum()->setAddress(address); coinbase = jsonrpcClient->coinbase(); - BOOST_CHECK_EQUAL(jsToAddress(coinbase), web3.ethereum()->address()); + BOOST_CHECK_EQUAL(jsToAddress(coinbase), web3->ethereum()->address()); BOOST_CHECK_EQUAL(jsToAddress(coinbase), address); jsonrpcServer->setAccounts({key}); - auto balance = web3.ethereum()->balanceAt(address, 0); + auto balance = web3->ethereum()->balanceAt(address, 0); string balanceString = jsonrpcClient->balanceAt(toJS(address)); double countAt = jsonrpcClient->countAt(toJS(address)); - BOOST_CHECK_EQUAL(countAt, (double)(uint64_t)web3.ethereum()->countAt(address)); + BOOST_CHECK_EQUAL(countAt, (double)(uint64_t)web3->ethereum()->countAt(address)); BOOST_CHECK_EQUAL(countAt, 0); BOOST_CHECK_EQUAL(toJS(balance), balanceString); BOOST_CHECK_EQUAL(jsToDecimal(balanceString), "0"); - dev::eth::mine(*(web3.ethereum()), 1); - balance = web3.ethereum()->balanceAt(address, 0); + dev::eth::mine(*(web3->ethereum()), 1); + balance = web3->ethereum()->balanceAt(address, 0); balanceString = jsonrpcClient->balanceAt(toJS(address)); BOOST_CHECK_EQUAL(toJS(balance), balanceString); @@ -227,19 +225,20 @@ BOOST_AUTO_TEST_CASE(jsonrpc_transact) jsonrpcClient->transact(t); jsonrpcServer->setAccounts({}); - dev::eth::mine(*(web3.ethereum()), 1); + dev::eth::mine(*(web3->ethereum()), 1); countAt = jsonrpcClient->countAt(toJS(address)); - auto balance2 = web3.ethereum()->balanceAt(receiver.address()); + auto balance2 = web3->ethereum()->balanceAt(receiver.address()); string balanceString2 = jsonrpcClient->balanceAt(toJS(receiver.address())); - BOOST_CHECK_EQUAL(countAt, (double)(uint64_t)web3.ethereum()->countAt(address)); + BOOST_CHECK_EQUAL(countAt, (double)(uint64_t)web3->ethereum()->countAt(address)); BOOST_CHECK_EQUAL(countAt, 1); BOOST_CHECK_EQUAL(toJS(balance2), balanceString2); BOOST_CHECK_EQUAL(jsToDecimal(balanceString2), "750000000000000000"); BOOST_CHECK_EQUAL(txAmount, balance2); } - + +BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE_END() #endif From 2ff0317fb693851be6073f086ffd4aef10500968 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Tue, 4 Nov 2014 14:29:00 +0000 Subject: [PATCH 3/4] Get logblooms/receipts into the database. --- libdevcore/FixedHash.h | 1 + libethereum/BlockChain.cpp | 16 +++++++- libethereum/BlockChain.h | 12 ++++++ libethereum/BlockDetails.h | 22 ++++++++++ libethereum/ExtVM.h | 2 +- libethereum/State.cpp | 2 - libethereum/State.h | 32 +-------------- libethereum/TransactionReceipt.h | 70 ++++++++++++++++++++++++++++++++ 8 files changed, 122 insertions(+), 35 deletions(-) create mode 100644 libethereum/TransactionReceipt.h diff --git a/libdevcore/FixedHash.h b/libdevcore/FixedHash.h index 41d140cad..6aeb0a660 100644 --- a/libdevcore/FixedHash.h +++ b/libdevcore/FixedHash.h @@ -231,6 +231,7 @@ using h520 = FixedHash<65>; using h512 = FixedHash<64>; using h256 = FixedHash<32>; using h160 = FixedHash<20>; +using h512s = std::vector; using h256s = std::vector; using h160s = std::vector; using h256Set = std::set; diff --git a/libethereum/BlockChain.cpp b/libethereum/BlockChain.cpp index 3ba1bb801..765b54627 100644 --- a/libethereum/BlockChain.cpp +++ b/libethereum/BlockChain.cpp @@ -309,10 +309,14 @@ h256s BlockChain::import(bytes const& _block, OverlayDB const& _db) auto b = s.oldBloom(); BlockBlooms bb; BlockTraces bt; + BlockLogBlooms blb; + BlockReceipts br; for (unsigned i = 0; i < s.pending().size(); ++i) { - bt.traces.push_back(s.changesFromPending(i)); bb.blooms.push_back(s.changesFromPending(i).bloom()); + bt.traces.push_back(s.changesFromPending(i)); + blb.blooms.push_back(s.receipt(i).bloom()); + br.receipts.push_back(s.receipt(i)); } s.cleanup(true); td = pd.totalDifficulty + tdIncrease; @@ -334,11 +338,21 @@ h256s BlockChain::import(bytes const& _block, OverlayDB const& _db) WriteGuard l(x_traces); m_traces[newHash] = bt; } + { + WriteGuard l(x_logBlooms); + m_logBlooms[newHash] = blb; + } + { + WriteGuard l(x_receipts); + m_receipts[newHash] = br; + } m_extrasDB->Put(m_writeOptions, toSlice(newHash), (ldb::Slice)dev::ref(m_details[newHash].rlp())); m_extrasDB->Put(m_writeOptions, toSlice(bi.parentHash), (ldb::Slice)dev::ref(m_details[bi.parentHash].rlp())); m_extrasDB->Put(m_writeOptions, toSlice(newHash, 1), (ldb::Slice)dev::ref(m_blooms[newHash].rlp())); m_extrasDB->Put(m_writeOptions, toSlice(newHash, 2), (ldb::Slice)dev::ref(m_traces[newHash].rlp())); + m_extrasDB->Put(m_writeOptions, toSlice(newHash, 3), (ldb::Slice)dev::ref(m_logBlooms[newHash].rlp())); + m_extrasDB->Put(m_writeOptions, toSlice(newHash, 4), (ldb::Slice)dev::ref(m_receipts[newHash].rlp())); m_db->Put(m_writeOptions, toSlice(newHash), (ldb::Slice)ref(_block)); #if ETH_PARANOIA diff --git a/libethereum/BlockChain.h b/libethereum/BlockChain.h index 30f5633c1..49f7b149e 100644 --- a/libethereum/BlockChain.h +++ b/libethereum/BlockChain.h @@ -105,6 +105,14 @@ public: BlockTraces traces(h256 _hash) const { return queryExtras(_hash, m_traces, x_traces, NullBlockTraces); } BlockTraces traces() const { return traces(currentHash()); } + /// Get the transactions' log blooms of a block (or the most recent mined if none given). Thread-safe. + BlockLogBlooms logBlooms(h256 _hash) const { return queryExtras(_hash, m_logBlooms, x_logBlooms, NullBlockLogBlooms); } + BlockLogBlooms logBlooms() const { return logBlooms(currentHash()); } + + /// Get the transactions' receipts of a block (or the most recent mined if none given). Thread-safe. + BlockReceipts receipts(h256 _hash) const { return queryExtras(_hash, m_receipts, x_receipts, NullBlockReceipts); } + BlockReceipts receipts() const { return receipts(currentHash()); } + /// Get a block (RLP format) for the given hash (or the most recent mined if none given). Thread-safe. bytes block(h256 _hash) const; bytes block() const { return block(currentHash()); } @@ -185,6 +193,10 @@ private: mutable BlockBloomsHash m_blooms; mutable boost::shared_mutex x_traces; mutable BlockTracesHash m_traces; + mutable boost::shared_mutex x_logBlooms; + mutable BlockLogBloomsHash m_logBlooms; + mutable boost::shared_mutex x_receipts; + mutable BlockReceiptsHash m_receipts; mutable boost::shared_mutex x_cache; mutable std::map m_cache; diff --git a/libethereum/BlockDetails.h b/libethereum/BlockDetails.h index 90bf65bdd..973e93070 100644 --- a/libethereum/BlockDetails.h +++ b/libethereum/BlockDetails.h @@ -29,6 +29,7 @@ #include #include #include "Manifest.h" +#include "TransactionReceipt.h" namespace ldb = leveldb; namespace dev @@ -71,14 +72,35 @@ struct BlockTraces Manifests traces; }; +struct BlockLogBlooms +{ + BlockLogBlooms() {} + BlockLogBlooms(RLP const& _r) { blooms = _r.toVector(); } + bytes rlp() const { RLPStream s; s << blooms; return s.out(); } + + h512s blooms; +}; + +struct BlockReceipts +{ + BlockReceipts() {} + BlockReceipts(RLP const& _r) { for (auto const& i: _r) receipts.emplace_back(i.data()); } + bytes rlp() const { RLPStream s(receipts.size()); for (TransactionReceipt const& i: receipts) i.streamRLP(s); return s.out(); } + + TransactionReceipts receipts; +}; typedef std::map BlockDetailsHash; typedef std::map BlockBloomsHash; typedef std::map BlockTracesHash; +typedef std::map BlockLogBloomsHash; +typedef std::map BlockReceiptsHash; static const BlockDetails NullBlockDetails; static const BlockBlooms NullBlockBlooms; static const BlockTraces NullBlockTraces; +static const BlockLogBlooms NullBlockLogBlooms; +static const BlockReceipts NullBlockReceipts; } } diff --git a/libethereum/ExtVM.h b/libethereum/ExtVM.h index cf80b2352..758672e49 100644 --- a/libethereum/ExtVM.h +++ b/libethereum/ExtVM.h @@ -102,7 +102,7 @@ public: private: State& m_s; ///< A reference to the base state. - std::map m_origCache; ///< The cache of the address states (i.e. the externalities) as-was prior to the execution. + std::map m_origCache; ///< The cache of the address states (i.e. the externalities) as-was prior to the execution. Manifest* m_ms; }; diff --git a/libethereum/State.cpp b/libethereum/State.cpp index cb8906dc3..51f5901c5 100644 --- a/libethereum/State.cpp +++ b/libethereum/State.cpp @@ -723,9 +723,7 @@ void State::cleanup(bool _fullCommit) m_previousBlock = m_currentBlock; } else - { m_db.rollback(); - } resetCurrent(); } diff --git a/libethereum/State.h b/libethereum/State.h index d966fb385..77b37f389 100644 --- a/libethereum/State.h +++ b/libethereum/State.h @@ -35,6 +35,7 @@ #include "TransactionQueue.h" #include "Account.h" #include "Transaction.h" +#include "TransactionReceipt.h" #include "Executive.h" #include "AccountDiff.h" @@ -52,37 +53,6 @@ struct StateChat: public LogChannel { static const char* name() { return "-S-"; struct StateTrace: public LogChannel { static const char* name() { return "=S="; } static const int verbosity = 7; }; struct StateDetail: public LogChannel { static const char* name() { return "/S/"; } static const int verbosity = 14; }; -class TransactionReceipt -{ -public: - TransactionReceipt(h256 _root, u256 _gasUsed, LogEntries const& _log, Manifest const& _ms): m_stateRoot(_root), m_gasUsed(_gasUsed), m_bloom(eth::bloom(_log)), m_log(_log), m_changes(_ms) {} - - Manifest const& changes() const { return m_changes; } - - h256 const& stateRoot() const { return m_stateRoot; } - u256 const& gasUsed() const { return m_gasUsed; } - LogBloom const& bloom() const { return m_bloom; } - LogEntries const& log() const { return m_log; } - - void streamRLP(RLPStream& _s) const - { - _s.appendList(4) << m_stateRoot << m_gasUsed << m_bloom; - _s.appendList(m_log.size()); - for (LogEntry const& l: m_log) - l.streamRLP(_s); - } - -private: - h256 m_stateRoot; - u256 m_gasUsed; - LogBloom m_bloom; - LogEntries m_log; - - Manifest m_changes; ///< TODO: PoC-7: KILL -}; - -using TransactionReceipts = std::vector; - struct PrecompiledAddress { unsigned gas; diff --git a/libethereum/TransactionReceipt.h b/libethereum/TransactionReceipt.h new file mode 100644 index 000000000..26539c4a9 --- /dev/null +++ b/libethereum/TransactionReceipt.h @@ -0,0 +1,70 @@ +/* + 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 . +*/ +/** @file TransactionReceipt.h + * @author Gav Wood + * @date 2014 + */ + +#pragma once + +#include +#include +#include +#include +#include +#include "Manifest.h" + +namespace dev +{ + +namespace eth +{ + +class TransactionReceipt +{ +public: + TransactionReceipt(bytesConstRef _rlp) { RLP r(_rlp); m_stateRoot = (h256)r[0]; m_gasUsed = (u256)r[1]; m_bloom = (LogBloom)r[2]; for (auto const& i: r[3]) m_log.emplace_back(i); } + TransactionReceipt(h256 _root, u256 _gasUsed, LogEntries const& _log, Manifest const& _ms): m_stateRoot(_root), m_gasUsed(_gasUsed), m_bloom(eth::bloom(_log)), m_log(_log), m_changes(_ms) {} + + Manifest const& changes() const { return m_changes; } + + h256 const& stateRoot() const { return m_stateRoot; } + u256 const& gasUsed() const { return m_gasUsed; } + LogBloom const& bloom() const { return m_bloom; } + LogEntries const& log() const { return m_log; } + + void streamRLP(RLPStream& _s) const + { + _s.appendList(4) << m_stateRoot << m_gasUsed << m_bloom; + _s.appendList(m_log.size()); + for (LogEntry const& l: m_log) + l.streamRLP(_s); + } + +private: + h256 m_stateRoot; + u256 m_gasUsed; + LogBloom m_bloom; + LogEntries m_log; + + Manifest m_changes; ///< TODO: PoC-7: KILL +}; + +using TransactionReceipts = std::vector; + +} +} From 3b786cf004cdfce99db90a676ed91ac029acaa48 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Tue, 4 Nov 2014 16:06:54 +0000 Subject: [PATCH 4/4] Fix for log blooms. --- libdevcore/FixedHash.h | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/libdevcore/FixedHash.h b/libdevcore/FixedHash.h index 6aeb0a660..02d199f62 100644 --- a/libdevcore/FixedHash.h +++ b/libdevcore/FixedHash.h @@ -158,21 +158,25 @@ public: return ret; } - template inline FixedHash& shiftBloom(FixedHash const& _h) { return (*this |= _h.template nbloom()); } + template inline FixedHash& shiftBloom(FixedHash const& _h) + { + return (*this |= _h.template nbloom()); + } template inline FixedHash nbloom() const { - static const unsigned c_bloomBytes = (M + 7) / 8; - unsigned mask = (1 << c_bloomBytes) - 1; + static const unsigned c_bloomBits = M * 8; + unsigned mask = c_bloomBits - 1; + unsigned bloomBytes = (dev::toLog2(c_bloomBits) + 7) / 8; FixedHash ret; byte const* p = data(); for (unsigned i = 0; i < P; ++i) { unsigned index = 0; - for (unsigned j = 0; j < c_bloomBytes; ++j, ++p) + for (unsigned j = 0; j < bloomBytes; ++j, ++p) index = (index << 8) | *p; index &= mask; - ret[N - 1 - index / 8] |= (1 << (index % 8)); + ret[M - 1 - index / 8] |= (1 << (index % 8)); } return ret; }