Power generation systems tend to be distributed and decentralized, and therefore the concept of microgrid has been proposed, which needs to implement decentralized data storage and power dispatching. The traditional power system architecture is no longer suitable in the decentralized microgrid system because there are no trusted third parties such as control centers. Therefore, it is challenging to securely implement data aggregation and power dispatching in microgrids without any trusted third party. In this paper, by leveraging blockchain, we propose secure data aggregation based on homomorphic encryption and the PBFT (Practical Byzantine Fault Tolerance) consensus, and meanwhile we propose automatic power dispatching by utilizing the PSO (Particle Swarm Optimization) algorithm and smart contracts. The security and performance analysis shows the effectiveness and efficiency of our proposed solutions.