Sovereign Debt: Optimal Contract, Underinvestment, and Forgiveness