Transactive energy plays a key role in the operation and energy management of future power systems. However, the conventional operational mechanism, which follows a centralized design, is often less secure, vulnerable to malicious behaviors, and suffers from privacy leakage. In this work, we introduce blockchain technology in transactive energy to address these challenges. Specifically, we develop a novel blockchain-based transactive energy framework for prosumers and design a decentralized energy trading algorithm that matches the operation of the underlying blockchain system. We prove that the trading algorithm guarantees the socially optimal performance as well as the individual benefit, and thus incentivizes prosumers to join the transactive energy platform. Moreover, we evaluate the feasibility of the transactive energy platform throughout the implementation of a small-scale network of Internet of Things (IoT) devices and extensive simulations using real-world data. Our results show that this blockchain-based transactive energy platform is feasible in practice, and the decentralized trading algorithm reduces the user's individual cost by up to 77% and lowers the overall cost by 24%.