The paper introduces a novel decentralized electricity market framework tailored for network community microgrid systems, leveraging blockchain technology. It presents a comprehensive model that integrates blockchain with a microgrid energy management system (MEMS) to facilitate peer-to-peer (P2P) energy trading, thereby ensuring optimal power flow and mitigating line congestion. The proposed optimization model takes into account crucial factors such as line flow constraints, market clearance price (MCP) using the double auction method, and social welfare optimization for energy transactions among buyers (consumers) and sellers (prosumers). By incorporating the power transfer distribution factor (PTDF) to calculate service charges associated with distribution network usage, the model safeguards the interests of all market participants while minimizing the risk of line overload. A case study is conducted to illustrate the efficacy of the proposed model, demonstrating the tangible benefits of blockchain integration in effectively managing and optimizing decentralized energy trading within microgrid environments. The proposed blockchain model for P2P energy trading offers a compelling alternative to conventional microgrid energy trading systems. By streamlining trade execution and eliminating intermediaries, it significantly reduces transaction times, with average processing times of around 10 s, highlighting its rapid processing capabilities. Furthermore, its decentralized nature and cryptographic security mechanisms provide robust protection against tampering and fraud, ensuring the integrity of transactions. Additionally, the transparent ledger system guarantees complete audibility and fairness for all participants, distinguishing it from opaque processes typical in traditional models.