Local energy trading has attracted the attention of many researchers as a result of its promising benefits. These benefits include minimizing gas emission, reducing power shortage, and establishing a competitive energy market. However, the energy trading between several prosumers causes trust, security, and privacy challenges in energy systems. On the other hand, a single point of failure and an increase in overall system cost occur when the energy system is managed using a centralized model. Therefore, to tackle the mentioned issues, this work proposes a two-layered secure Peer-to-Peer (P2P) energy trading model based on blockchain. The proposed model has two layers: authentication, and secure energy trading. In the authentication layer, in order to protect the proposed model from impersonation attacks, a mutual authentication process is implemented. In the energy trading layer, a new consensus mechanism is proposed to minimize the number of malicious validators. Afterwards, an incentive-punishment algorithm is introduced to motivate energy prosumers to contribute more energy in the model. Next, a dynamic contract theory based on supply-demand ratio pricing scheme is proposed. The purpose of the proposed pricing scheme is to solve the issues associated with the existing pricing schemes. It also preserves the privacy of the actual energy consumption behavior of the trading participants. Furthermore, a consensus mechanism validators’ selection model is proposed. The aim of the proposed work is to have an efficient and secure P2P energy trading platform. Simulations are executed to show the performance of the proposed model in terms of communication and computational costs, reputation, energy contributed, reward, and prices. The results for the authentication process show 7.45 ms computational cost and 1152 bits communication cost, which are better than the existing works. In the consensus process, 66.67% of the validators are selected to conduct the consensus for every transaction. This selection efficiently improves the consensus process and minimizes the number of malicious validators. In the proposed model, the increase in reward is observed for increased energy contribution, decreased non-malicious transactions and adjustment of energy consumption. The proposed model shows a satisfactory performance in terms of trust, security, and privacy.