You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
2.7 KiB

  1. package privval_test
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/require"
  8. "github.com/tendermint/tendermint/privval"
  9. )
  10. const oldPrivvalContent = `{
  11. "address": "1D8089FAFDFAE4A637F3D616E17B92905FA2D91D",
  12. "pub_key": {
  13. "type": "tendermint/PubKeyEd25519",
  14. "value": "r3Yg2AhDZ745CNTpavsGU+mRZ8WpRXqoJuyqjN8mJq0="
  15. },
  16. "last_height": "5",
  17. "last_round": "0",
  18. "last_step": 3,
  19. "last_signature": "CTr7b9ZQlrJJf+12rPl5t/YSCUc/KqV7jQogCfFJA24e7hof69X6OMT7eFLVQHyodPjD/QTA298XHV5ejxInDQ==",
  20. "last_signbytes": "750802110500000000000000220B08B398F3E00510F48DA6402A480A20FC258973076512999C3E6839A22E9FBDB1B77CF993E8A9955412A41A59D4CAD312240A20C971B286ACB8AAA6FCA0365EB0A660B189EDC08B46B5AF2995DEFA51A28D215B10013211746573742D636861696E2D533245415533",
  21. "priv_key": {
  22. "type": "tendermint/PrivKeyEd25519",
  23. "value": "7MwvTGEWWjsYwjn2IpRb+GYsWi9nnFsw8jPLLY1UtP6vdiDYCENnvjkI1Olq+wZT6ZFnxalFeqgm7KqM3yYmrQ=="
  24. }
  25. }`
  26. func TestLoadAndUpgrade(t *testing.T) {
  27. oldFilePath := initTmpOldFile(t)
  28. defer os.Remove(oldFilePath)
  29. newStateFile, err := ioutil.TempFile("", "priv_validator_state*.json")
  30. defer os.Remove(newStateFile.Name())
  31. require.NoError(t, err)
  32. newKeyFile, err := ioutil.TempFile("", "priv_validator_key*.json")
  33. defer os.Remove(newKeyFile.Name())
  34. require.NoError(t, err)
  35. oldPV, err := privval.LoadOldFilePV(oldFilePath)
  36. assert.NoError(t, err)
  37. newPV := oldPV.Upgrade(newKeyFile.Name(), newStateFile.Name())
  38. assertEqualPV(t, oldPV, newPV)
  39. assert.NoError(t, err)
  40. upgradedPV := privval.LoadFilePV(newKeyFile.Name(), newStateFile.Name())
  41. assertEqualPV(t, oldPV, upgradedPV)
  42. oldPV, err = privval.LoadOldFilePV(oldFilePath + ".bak")
  43. require.NoError(t, err)
  44. assertEqualPV(t, oldPV, upgradedPV)
  45. }
  46. func assertEqualPV(t *testing.T, oldPV *privval.OldFilePV, newPV *privval.FilePV) {
  47. assert.Equal(t, oldPV.Address, newPV.Key.Address)
  48. assert.Equal(t, oldPV.Address, newPV.GetAddress())
  49. assert.Equal(t, oldPV.PubKey, newPV.Key.PubKey)
  50. assert.Equal(t, oldPV.PubKey, newPV.GetPubKey())
  51. assert.Equal(t, oldPV.PrivKey, newPV.Key.PrivKey)
  52. assert.Equal(t, oldPV.LastHeight, newPV.LastSignState.Height)
  53. assert.Equal(t, oldPV.LastRound, newPV.LastSignState.Round)
  54. assert.Equal(t, oldPV.LastSignature, newPV.LastSignState.Signature)
  55. assert.Equal(t, oldPV.LastSignBytes, newPV.LastSignState.SignBytes)
  56. assert.Equal(t, oldPV.LastStep, newPV.LastSignState.Step)
  57. }
  58. func initTmpOldFile(t *testing.T) string {
  59. tmpfile, err := ioutil.TempFile("", "priv_validator_*.json")
  60. require.NoError(t, err)
  61. t.Logf("created test file %s", tmpfile.Name())
  62. _, err = tmpfile.WriteString(oldPrivvalContent)
  63. require.NoError(t, err)
  64. return tmpfile.Name()
  65. }