There are a series of challenges in microgrid transactions, and blockchain technology holds the promise of addressing these challenges. However, with the increasing number of users in microgrid transactions, existing blockchain systems may struggle to meet the growing demands for transactions. Therefore, this paper proposes an efficient and secure blockchain consensus algorithm designed to meet the demands of large-scale microgrid electricity transactions. The algorithm begins by utilizing a Spectral clustering algorithm to partition the blockchain network into different lower-level consensus set based on the transaction characteristics of nodes. Subsequently, a dual-layer consensus process is employed to enhance the efficiency of consensus. Additionally, we have designed a secure consensus set leader election strategy to promptly identify leaders with excellent performance. Finally, we have introduced an authentication method that combines zero-knowledge proofs and key sharing to further mitigate the risk of malicious nodes participating in the consensus. Theoretical analysis indicates that our proposed consensus algorithm, incorporating multiple layers of security measures, effectively withstands blockchain attacks such as denial of service. Simulation experiment results demonstrate that our algorithm outperforms similar blockchain algorithms significantly in terms of communication overhead, consensus latency, and throughput.