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.

341 lines
9.1 KiB

  1. package types
  2. import (
  3. "bytes"
  4. "sort"
  5. "testing"
  6. "time"
  7. "github.com/stretchr/testify/assert"
  8. tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
  9. )
  10. var (
  11. valEd25519 = []string{ABCIPubKeyTypeEd25519}
  12. valSecp256k1 = []string{ABCIPubKeyTypeSecp256k1}
  13. valSr25519 = []string{ABCIPubKeyTypeSr25519}
  14. )
  15. func TestConsensusParamsValidation(t *testing.T) {
  16. testCases := []struct {
  17. params ConsensusParams
  18. valid bool
  19. }{
  20. // test block params
  21. {
  22. params: makeParams(makeParamsArgs{
  23. blockBytes: 1,
  24. evidenceAge: 2,
  25. precision: 1,
  26. messageDelay: 1}),
  27. valid: true,
  28. },
  29. {
  30. params: makeParams(makeParamsArgs{
  31. blockBytes: 0,
  32. evidenceAge: 2,
  33. precision: 1,
  34. messageDelay: 1}),
  35. valid: false,
  36. },
  37. {
  38. params: makeParams(makeParamsArgs{
  39. blockBytes: 47 * 1024 * 1024,
  40. evidenceAge: 2,
  41. precision: 1,
  42. messageDelay: 1}),
  43. valid: true,
  44. },
  45. {
  46. params: makeParams(makeParamsArgs{
  47. blockBytes: 10,
  48. evidenceAge: 2,
  49. precision: 1,
  50. messageDelay: 1}),
  51. valid: true,
  52. },
  53. {
  54. params: makeParams(makeParamsArgs{
  55. blockBytes: 100 * 1024 * 1024,
  56. evidenceAge: 2,
  57. precision: 1,
  58. messageDelay: 1}),
  59. valid: true,
  60. },
  61. {
  62. params: makeParams(makeParamsArgs{
  63. blockBytes: 101 * 1024 * 1024,
  64. evidenceAge: 2,
  65. precision: 1,
  66. messageDelay: 1}),
  67. valid: false,
  68. },
  69. {
  70. params: makeParams(makeParamsArgs{
  71. blockBytes: 1024 * 1024 * 1024,
  72. evidenceAge: 2,
  73. precision: 1,
  74. messageDelay: 1}),
  75. valid: false,
  76. },
  77. {
  78. params: makeParams(makeParamsArgs{
  79. blockBytes: 1024 * 1024 * 1024,
  80. evidenceAge: 2,
  81. precision: 1,
  82. messageDelay: 1}),
  83. valid: false,
  84. },
  85. // test evidence params
  86. {
  87. params: makeParams(makeParamsArgs{
  88. blockBytes: 1,
  89. evidenceAge: 0,
  90. maxEvidenceBytes: 0,
  91. precision: 1,
  92. messageDelay: 1}),
  93. valid: false,
  94. },
  95. {
  96. params: makeParams(makeParamsArgs{
  97. blockBytes: 1,
  98. evidenceAge: 2,
  99. maxEvidenceBytes: 2,
  100. precision: 1,
  101. messageDelay: 1}),
  102. valid: false,
  103. },
  104. {
  105. params: makeParams(makeParamsArgs{
  106. blockBytes: 1000,
  107. evidenceAge: 2,
  108. maxEvidenceBytes: 1,
  109. precision: 1,
  110. messageDelay: 1}),
  111. valid: true,
  112. },
  113. {
  114. params: makeParams(makeParamsArgs{
  115. blockBytes: 1,
  116. evidenceAge: -1,
  117. maxEvidenceBytes: 0,
  118. precision: 1,
  119. messageDelay: 1}),
  120. valid: false,
  121. },
  122. // test no pubkey type provided
  123. {
  124. params: makeParams(makeParamsArgs{
  125. evidenceAge: 2,
  126. pubkeyTypes: []string{},
  127. precision: 1,
  128. messageDelay: 1}),
  129. valid: false,
  130. },
  131. // test invalid pubkey type provided
  132. {
  133. params: makeParams(makeParamsArgs{
  134. evidenceAge: 2,
  135. pubkeyTypes: []string{"potatoes make good pubkeys"},
  136. precision: 1,
  137. messageDelay: 1}),
  138. valid: false,
  139. },
  140. // test invalid pubkey type provided
  141. {
  142. params: makeParams(makeParamsArgs{
  143. evidenceAge: 2,
  144. precision: 1,
  145. messageDelay: -1}),
  146. valid: false,
  147. },
  148. {
  149. params: makeParams(makeParamsArgs{
  150. evidenceAge: 2,
  151. precision: -1,
  152. messageDelay: 1}),
  153. valid: false,
  154. },
  155. }
  156. for i, tc := range testCases {
  157. if tc.valid {
  158. assert.NoErrorf(t, tc.params.ValidateConsensusParams(), "expected no error for valid params (#%d)", i)
  159. } else {
  160. assert.Errorf(t, tc.params.ValidateConsensusParams(), "expected error for non valid params (#%d)", i)
  161. }
  162. }
  163. }
  164. type makeParamsArgs struct {
  165. blockBytes int64
  166. blockGas int64
  167. evidenceAge int64
  168. maxEvidenceBytes int64
  169. pubkeyTypes []string
  170. precision time.Duration
  171. messageDelay time.Duration
  172. }
  173. func makeParams(args makeParamsArgs) ConsensusParams {
  174. if args.pubkeyTypes == nil {
  175. args.pubkeyTypes = valEd25519
  176. }
  177. return ConsensusParams{
  178. Block: BlockParams{
  179. MaxBytes: args.blockBytes,
  180. MaxGas: args.blockGas,
  181. },
  182. Evidence: EvidenceParams{
  183. MaxAgeNumBlocks: args.evidenceAge,
  184. MaxAgeDuration: time.Duration(args.evidenceAge),
  185. MaxBytes: args.maxEvidenceBytes,
  186. },
  187. Validator: ValidatorParams{
  188. PubKeyTypes: args.pubkeyTypes,
  189. },
  190. Synchrony: SynchronyParams{
  191. Precision: args.precision,
  192. MessageDelay: args.messageDelay,
  193. },
  194. }
  195. }
  196. func TestConsensusParamsHash(t *testing.T) {
  197. params := []ConsensusParams{
  198. makeParams(makeParamsArgs{blockBytes: 4, blockGas: 2, evidenceAge: 3, maxEvidenceBytes: 1}),
  199. makeParams(makeParamsArgs{blockBytes: 1, blockGas: 4, evidenceAge: 3, maxEvidenceBytes: 1}),
  200. makeParams(makeParamsArgs{blockBytes: 1, blockGas: 2, evidenceAge: 4, maxEvidenceBytes: 1}),
  201. makeParams(makeParamsArgs{blockBytes: 2, blockGas: 5, evidenceAge: 7, maxEvidenceBytes: 1}),
  202. makeParams(makeParamsArgs{blockBytes: 1, blockGas: 7, evidenceAge: 6, maxEvidenceBytes: 1}),
  203. makeParams(makeParamsArgs{blockBytes: 9, blockGas: 5, evidenceAge: 4, maxEvidenceBytes: 1}),
  204. makeParams(makeParamsArgs{blockBytes: 7, blockGas: 8, evidenceAge: 9, maxEvidenceBytes: 1}),
  205. makeParams(makeParamsArgs{blockBytes: 4, blockGas: 6, evidenceAge: 5, maxEvidenceBytes: 1}),
  206. }
  207. hashes := make([][]byte, len(params))
  208. for i := range params {
  209. hashes[i] = params[i].HashConsensusParams()
  210. }
  211. // make sure there are no duplicates...
  212. // sort, then check in order for matches
  213. sort.Slice(hashes, func(i, j int) bool {
  214. return bytes.Compare(hashes[i], hashes[j]) < 0
  215. })
  216. for i := 0; i < len(hashes)-1; i++ {
  217. assert.NotEqual(t, hashes[i], hashes[i+1])
  218. }
  219. }
  220. func TestConsensusParamsUpdate(t *testing.T) {
  221. testCases := []struct {
  222. intialParams ConsensusParams
  223. updates *tmproto.ConsensusParams
  224. updatedParams ConsensusParams
  225. }{
  226. // empty updates
  227. {
  228. intialParams: makeParams(makeParamsArgs{blockBytes: 1, blockGas: 2, evidenceAge: 3}),
  229. updates: &tmproto.ConsensusParams{},
  230. updatedParams: makeParams(makeParamsArgs{blockBytes: 1, blockGas: 2, evidenceAge: 3}),
  231. },
  232. {
  233. // update synchrony params
  234. intialParams: makeParams(makeParamsArgs{evidenceAge: 3, precision: time.Second, messageDelay: 3 * time.Second}),
  235. updates: &tmproto.ConsensusParams{
  236. Synchrony: &tmproto.SynchronyParams{
  237. Precision: time.Second * 2,
  238. MessageDelay: time.Second * 4,
  239. },
  240. },
  241. updatedParams: makeParams(makeParamsArgs{evidenceAge: 3, precision: 2 * time.Second, messageDelay: 4 * time.Second}),
  242. },
  243. // fine updates
  244. {
  245. intialParams: makeParams(makeParamsArgs{blockBytes: 1, blockGas: 2, evidenceAge: 3}),
  246. updates: &tmproto.ConsensusParams{
  247. Block: &tmproto.BlockParams{
  248. MaxBytes: 100,
  249. MaxGas: 200,
  250. },
  251. Evidence: &tmproto.EvidenceParams{
  252. MaxAgeNumBlocks: 300,
  253. MaxAgeDuration: time.Duration(300),
  254. MaxBytes: 50,
  255. },
  256. Validator: &tmproto.ValidatorParams{
  257. PubKeyTypes: valSecp256k1,
  258. },
  259. },
  260. updatedParams: makeParams(makeParamsArgs{
  261. blockBytes: 100, blockGas: 200,
  262. evidenceAge: 300,
  263. maxEvidenceBytes: 50,
  264. pubkeyTypes: valSecp256k1}),
  265. },
  266. {
  267. intialParams: makeParams(makeParamsArgs{blockBytes: 1, blockGas: 2, evidenceAge: 3}),
  268. updates: &tmproto.ConsensusParams{
  269. Block: &tmproto.BlockParams{
  270. MaxBytes: 100,
  271. MaxGas: 200,
  272. },
  273. Evidence: &tmproto.EvidenceParams{
  274. MaxAgeNumBlocks: 300,
  275. MaxAgeDuration: time.Duration(300),
  276. MaxBytes: 50,
  277. },
  278. Validator: &tmproto.ValidatorParams{
  279. PubKeyTypes: valSr25519,
  280. },
  281. },
  282. updatedParams: makeParams(makeParamsArgs{
  283. blockBytes: 100,
  284. blockGas: 200,
  285. evidenceAge: 300,
  286. maxEvidenceBytes: 50,
  287. pubkeyTypes: valSr25519}),
  288. },
  289. }
  290. for _, tc := range testCases {
  291. assert.Equal(t, tc.updatedParams, tc.intialParams.UpdateConsensusParams(tc.updates))
  292. }
  293. }
  294. func TestConsensusParamsUpdate_AppVersion(t *testing.T) {
  295. params := makeParams(makeParamsArgs{blockBytes: 1, blockGas: 2, evidenceAge: 3})
  296. assert.EqualValues(t, 0, params.Version.AppVersion)
  297. updated := params.UpdateConsensusParams(
  298. &tmproto.ConsensusParams{Version: &tmproto.VersionParams{AppVersion: 1}})
  299. assert.EqualValues(t, 1, updated.Version.AppVersion)
  300. }
  301. func TestProto(t *testing.T) {
  302. params := []ConsensusParams{
  303. makeParams(makeParamsArgs{blockBytes: 4, blockGas: 2, evidenceAge: 3, maxEvidenceBytes: 1}),
  304. makeParams(makeParamsArgs{blockBytes: 1, blockGas: 4, evidenceAge: 3, maxEvidenceBytes: 1}),
  305. makeParams(makeParamsArgs{blockBytes: 1, blockGas: 2, evidenceAge: 4, maxEvidenceBytes: 1}),
  306. makeParams(makeParamsArgs{blockBytes: 2, blockGas: 5, evidenceAge: 7, maxEvidenceBytes: 1}),
  307. makeParams(makeParamsArgs{blockBytes: 1, blockGas: 7, evidenceAge: 6, maxEvidenceBytes: 1}),
  308. makeParams(makeParamsArgs{blockBytes: 9, blockGas: 5, evidenceAge: 4, maxEvidenceBytes: 1}),
  309. makeParams(makeParamsArgs{blockBytes: 7, blockGas: 8, evidenceAge: 9, maxEvidenceBytes: 1}),
  310. makeParams(makeParamsArgs{blockBytes: 4, blockGas: 6, evidenceAge: 5, maxEvidenceBytes: 1}),
  311. makeParams(makeParamsArgs{precision: time.Second, messageDelay: time.Minute}),
  312. makeParams(makeParamsArgs{precision: time.Nanosecond, messageDelay: time.Millisecond}),
  313. }
  314. for i := range params {
  315. pbParams := params[i].ToProto()
  316. oriParams := ConsensusParamsFromProto(pbParams)
  317. assert.Equal(t, params[i], oriParams)
  318. }
  319. }