kaistshadow / blockchain-sim

Scalable blockchain simulator/emulator running on shadow simulator
MIT License
9 stars 1 forks source link

Release 0.2.0 - Monitor node, generator node, plugin directory #294

Closed tkdlqm2 closed 3 years ago

tkdlqm2 commented 3 years ago

Release 0.2.0 - Monitor node, generator node, plugin directory

Originally posted by @tkdlqm2 in https://github.com/kaistshadow/blockchain-sim/issues/272#issuecomment-816392739

tkdlqm2 commented 3 years ago

[Monitor node issue] 상황 : 현재 TxGenerator(트랜잭션 증폭기)가 miner 노드에게 tx를 전파를 하고, miner 노드들은 전파 받은 tx를 모니터링 노드에게 브로드 캐스트를 함. 하지만 시뮬레이션 결과를 보면, TxGenerator가 전파한 Tx의 개수와 miner 노드들이 수신한 Tx의 개수는 동일하지만 miner 노드들이 브로드 캐스트한 Tx를 수신한 모니터링 노드의 Tx개수의 차이가 상당히 남.

다음 소스코드는 bitcoin의 net_processing.cpp 파일의 sendMsg 함수에서 전파할 Transaction을 Inv에 넣는 로직임. while문 조건을 보면 INVENTORY_BROADCAST_MAX 값이 있는데, 이는 bitcoin의 default 매크로 값으로 써, 브로드 캐스트의 인터벌 값이 명시가 되어 있음. 해당 매크로 값은 default가 35sec 인데, 이를 14sec로 줄이면 비로소 모니터링 노드까지 Tx 개수가 일치하게 됨.

while (!vInvTx.empty() && nRelayedTransactions < INVENTORY_BROADCAST_MAX) {
                        // Fetch the top element from the heap
                        std::pop_heap(vInvTx.begin(), vInvTx.end(), compareInvMempoolOrder);
                        std::set<uint256>::iterator it = vInvTx.back();
                        vInvTx.pop_back();
                        uint256 hash = *it;
                        // Remove it from the to-be-sent set
                        pto->m_tx_relay->setInventoryTxToSend.erase(it);
                        // Check if not in the filter alreadyCheck if not in the filter already
                        if (pto->m_tx_relay->filterInventoryKnown.contains(hash)) {
                            continue;
                        }
                        // Not in the mempool anymore? don't bother sending it.
                        auto txinfo = mempool.info(hash);
                        if (!txinfo.tx) {
                            continue;
                        }
                        if (filterrate && txinfo.feeRate.GetFeePerK() < filterrate) {
                            continue;
                        }
                        if (pto->m_tx_relay->pfilter && !pto->m_tx_relay->pfilter->IsRelevantAndUpdate(*txinfo.tx)) continue;
                        // Send
                        vInv.push_back(CInv(MSG_TX, hash));
                        nRelayedTransactions++;
                        {
                            // Expire old relay messages
                            while (!vRelayExpiration.empty() && vRelayExpiration.front().first < nNow)
                            {
                                mapRelay.erase(vRelayExpiration.front().second);
                                vRelayExpiration.pop_front();
                            }

                            auto ret = mapRelay.insert(std::make_pair(hash, std::move(txinfo.tx)));
                            if (ret.second) {
                                vRelayExpiration.push_back(std::make_pair(nNow + 15 * 60 * 1000000, ret.first));
                            }
                        }
                        if (vInv.size() == MAX_INV_SZ) {
                            connman->PushMessage(pto, msgMaker.Make(NetMsgType::INV, vInv));
                            vInv.clear();
                        }
                        pto->m_tx_relay->filterInventoryKnown.insert(hash);
                    }

결론 : 비트코인의 소스코드를 직접 수정할 필요 없이, TxGenerator가 Monitoring node에게 Tx를 직접 전파를 해주는 식으로 하면 해결할 수 있음.

tkdlqm2 commented 3 years ago

Find packge 구현

모니터 노드 플러그인, TxGenerator 노드 플러그인, 마이너 노드 rpc 플러그인이 regtest 구현 중에 겹쳐서 사용이 됨. 그래서 소스파일이 중복으로 되기에 이를 Find pacakge를 활용하여 최적화 시킴.

Install/plugins

├── libBITCOIND_0.19.1DEV.so
├── libBITCOINTPS_TESTER.so
├── libBITCOIN_MONITOR.so
├── libBLEEPP2P_TARGET.so
├── libBLEEPP2P_TESTER.so
└── minerNode_rpc.so

사용 과정

# copy bitcoind plugin
add_custom_target(TxGenerator_connection-copy-plugin ALL
        COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:Bitcoin_0_19_1dev::Plugin> ${CMAKE_CURRENT_SOURCE_DIR})
add_dependencies(TxGenerator_connection-copy-plugin Bitcoin_0_19_1dev::Plugin)

# copy TPSTESTER plugin
add_custom_target(TPSTESTER-plugin ALL
        COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:TPSTESTER::Plugin> ${CMAKE_CURRENT_SOURCE_DIR})
add_dependencies(TPSTESTER-plugin TPSTESTER::Plugin)

# copy Monitor Node plugin
add_custom_target(MonitorNode-plugin ALL
        COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:MonitorNode::Plugin> ${CMAKE_CURRENT_SOURCE_DIR})
add_dependencies(MonitorNode-plugin MonitorNode::Plugin)

# copy Miner node rpc plugin
add_custom_target(MinerNodeRPC-plugin ALL
        COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:MinerNodeRPC::Plugin> ${CMAKE_CURRENT_SOURCE_DIR})
add_dependencies(MinerNodeRPC-plugin MinerNodeRPC::Plugin)