This paper proposes a blockchain-based scheme, focusing on anonymous identity authentication and data aggregation, for safer and more reliable bidirectional communication between the utility company and power consumers based on Advanced Metering Infrastructure (AMI). Firstly, to cope with the mutual identity authentication between resource-constrained Smart Meters, a lightweight anonymous authentication strategy is designed using Elliptic Curve Cryptography. Meanwhile, a reputation-based consensus protocol is developed to accomplish data aggregation in AMI by using decentralization and non-tampering features of the blockchain. During the communication, in each time slot, the proposed scheme only needs to select a trusted user randomly to undertake the mining node rather than to introduce a third-party in a centralized manner to summarize and record the user-side data into the blockchain. Such scheme can effectively prevent data tampering and also be effective to decrease the processing complexity. The AVISPA tool is adopted to formally evaluate its security. The simulation results show promising performance. The proposed scheme not only can guarantee secure communication but also effectively decrease the computational cost in AMI.