Engineering Blockchain-based Software Systems: Foundations, Survey, and Future Directions