Optimal Flexible Consensus and its Application to Ethereum