snapshot_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. // Copyright 2017 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package clique
  17. import (
  18. "bytes"
  19. "crypto/ecdsa"
  20. "math/big"
  21. "testing"
  22. "github.com/ethereum/go-ethereum/common"
  23. "github.com/ethereum/go-ethereum/core"
  24. "github.com/ethereum/go-ethereum/core/rawdb"
  25. "github.com/ethereum/go-ethereum/core/types"
  26. "github.com/ethereum/go-ethereum/crypto"
  27. "github.com/ethereum/go-ethereum/ethdb"
  28. "github.com/ethereum/go-ethereum/params"
  29. )
  30. type testerVote struct {
  31. signer string
  32. voted string
  33. auth bool
  34. }
  35. // testerAccountPool is a pool to maintain currently active tester accounts,
  36. // mapped from textual names used in the tests below to actual Ethereum private
  37. // keys capable of signing transactions.
  38. type testerAccountPool struct {
  39. accounts map[string]*ecdsa.PrivateKey
  40. }
  41. func newTesterAccountPool() *testerAccountPool {
  42. return &testerAccountPool{
  43. accounts: make(map[string]*ecdsa.PrivateKey),
  44. }
  45. }
  46. func (ap *testerAccountPool) sign(header *types.Header, signer string) {
  47. // Ensure we have a persistent key for the signer
  48. if ap.accounts[signer] == nil {
  49. ap.accounts[signer], _ = crypto.GenerateKey()
  50. }
  51. // Sign the header and embed the signature in extra data
  52. sig, _ := crypto.Sign(sigHash(header).Bytes(), ap.accounts[signer])
  53. copy(header.Extra[len(header.Extra)-65:], sig)
  54. }
  55. func (ap *testerAccountPool) address(account string) common.Address {
  56. // Ensure we have a persistent key for the account
  57. if ap.accounts[account] == nil {
  58. ap.accounts[account], _ = crypto.GenerateKey()
  59. }
  60. // Resolve and return the Ethereum address
  61. return crypto.PubkeyToAddress(ap.accounts[account].PublicKey)
  62. }
  63. // testerChainReader implements consensus.ChainReader to access the genesis
  64. // block. All other methods and requests will panic.
  65. type testerChainReader struct {
  66. db ethdb.Database
  67. }
  68. func (r *testerChainReader) Config() *params.ChainConfig { return params.AllCliqueProtocolChanges }
  69. func (r *testerChainReader) CurrentHeader() *types.Header { panic("not supported") }
  70. func (r *testerChainReader) GetHeader(common.Hash, uint64) *types.Header { panic("not supported") }
  71. func (r *testerChainReader) GetBlock(common.Hash, uint64) *types.Block { panic("not supported") }
  72. func (r *testerChainReader) GetHeaderByHash(common.Hash) *types.Header { panic("not supported") }
  73. func (r *testerChainReader) GetHeaderByNumber(number uint64) *types.Header {
  74. if number == 0 {
  75. return rawdb.ReadHeader(r.db, rawdb.ReadCanonicalHash(r.db, 0), 0)
  76. }
  77. panic("not supported")
  78. }
  79. // Tests that voting is evaluated correctly for various simple and complex scenarios.
  80. func TestVoting(t *testing.T) {
  81. // Define the various voting scenarios to test
  82. tests := []struct {
  83. epoch uint64
  84. signers []string
  85. votes []testerVote
  86. results []string
  87. }{
  88. {
  89. // Single signer, no votes cast
  90. signers: []string{"A"},
  91. votes: []testerVote{{signer: "A"}},
  92. results: []string{"A"},
  93. }, {
  94. // Single signer, voting to add two others (only accept first, second needs 2 votes)
  95. signers: []string{"A"},
  96. votes: []testerVote{
  97. {signer: "A", voted: "B", auth: true},
  98. {signer: "B"},
  99. {signer: "A", voted: "C", auth: true},
  100. },
  101. results: []string{"A", "B"},
  102. }, {
  103. // Two signers, voting to add three others (only accept first two, third needs 3 votes already)
  104. signers: []string{"A", "B"},
  105. votes: []testerVote{
  106. {signer: "A", voted: "C", auth: true},
  107. {signer: "B", voted: "C", auth: true},
  108. {signer: "A", voted: "D", auth: true},
  109. {signer: "B", voted: "D", auth: true},
  110. {signer: "C"},
  111. {signer: "A", voted: "E", auth: true},
  112. {signer: "B", voted: "E", auth: true},
  113. },
  114. results: []string{"A", "B", "C", "D"},
  115. }, {
  116. // Single signer, dropping itself (weird, but one less cornercase by explicitly allowing this)
  117. signers: []string{"A"},
  118. votes: []testerVote{
  119. {signer: "A", voted: "A", auth: false},
  120. },
  121. results: []string{},
  122. }, {
  123. // Two signers, actually needing mutual consent to drop either of them (not fulfilled)
  124. signers: []string{"A", "B"},
  125. votes: []testerVote{
  126. {signer: "A", voted: "B", auth: false},
  127. },
  128. results: []string{"A", "B"},
  129. }, {
  130. // Two signers, actually needing mutual consent to drop either of them (fulfilled)
  131. signers: []string{"A", "B"},
  132. votes: []testerVote{
  133. {signer: "A", voted: "B", auth: false},
  134. {signer: "B", voted: "B", auth: false},
  135. },
  136. results: []string{"A"},
  137. }, {
  138. // Three signers, two of them deciding to drop the third
  139. signers: []string{"A", "B", "C"},
  140. votes: []testerVote{
  141. {signer: "A", voted: "C", auth: false},
  142. {signer: "B", voted: "C", auth: false},
  143. },
  144. results: []string{"A", "B"},
  145. }, {
  146. // Four signers, consensus of two not being enough to drop anyone
  147. signers: []string{"A", "B", "C", "D"},
  148. votes: []testerVote{
  149. {signer: "A", voted: "C", auth: false},
  150. {signer: "B", voted: "C", auth: false},
  151. },
  152. results: []string{"A", "B", "C", "D"},
  153. }, {
  154. // Four signers, consensus of three already being enough to drop someone
  155. signers: []string{"A", "B", "C", "D"},
  156. votes: []testerVote{
  157. {signer: "A", voted: "D", auth: false},
  158. {signer: "B", voted: "D", auth: false},
  159. {signer: "C", voted: "D", auth: false},
  160. },
  161. results: []string{"A", "B", "C"},
  162. }, {
  163. // Authorizations are counted once per signer per target
  164. signers: []string{"A", "B"},
  165. votes: []testerVote{
  166. {signer: "A", voted: "C", auth: true},
  167. {signer: "B"},
  168. {signer: "A", voted: "C", auth: true},
  169. {signer: "B"},
  170. {signer: "A", voted: "C", auth: true},
  171. },
  172. results: []string{"A", "B"},
  173. }, {
  174. // Authorizing multiple accounts concurrently is permitted
  175. signers: []string{"A", "B"},
  176. votes: []testerVote{
  177. {signer: "A", voted: "C", auth: true},
  178. {signer: "B"},
  179. {signer: "A", voted: "D", auth: true},
  180. {signer: "B"},
  181. {signer: "A"},
  182. {signer: "B", voted: "D", auth: true},
  183. {signer: "A"},
  184. {signer: "B", voted: "C", auth: true},
  185. },
  186. results: []string{"A", "B", "C", "D"},
  187. }, {
  188. // Deauthorizations are counted once per signer per target
  189. signers: []string{"A", "B"},
  190. votes: []testerVote{
  191. {signer: "A", voted: "B", auth: false},
  192. {signer: "B"},
  193. {signer: "A", voted: "B", auth: false},
  194. {signer: "B"},
  195. {signer: "A", voted: "B", auth: false},
  196. },
  197. results: []string{"A", "B"},
  198. }, {
  199. // Deauthorizing multiple accounts concurrently is permitted
  200. signers: []string{"A", "B", "C", "D"},
  201. votes: []testerVote{
  202. {signer: "A", voted: "C", auth: false},
  203. {signer: "B"},
  204. {signer: "C"},
  205. {signer: "A", voted: "D", auth: false},
  206. {signer: "B"},
  207. {signer: "C"},
  208. {signer: "A"},
  209. {signer: "B", voted: "D", auth: false},
  210. {signer: "C", voted: "D", auth: false},
  211. {signer: "A"},
  212. {signer: "B", voted: "C", auth: false},
  213. },
  214. results: []string{"A", "B"},
  215. }, {
  216. // Votes from deauthorized signers are discarded immediately (deauth votes)
  217. signers: []string{"A", "B", "C"},
  218. votes: []testerVote{
  219. {signer: "C", voted: "B", auth: false},
  220. {signer: "A", voted: "C", auth: false},
  221. {signer: "B", voted: "C", auth: false},
  222. {signer: "A", voted: "B", auth: false},
  223. },
  224. results: []string{"A", "B"},
  225. }, {
  226. // Votes from deauthorized signers are discarded immediately (auth votes)
  227. signers: []string{"A", "B", "C"},
  228. votes: []testerVote{
  229. {signer: "C", voted: "B", auth: false},
  230. {signer: "A", voted: "C", auth: false},
  231. {signer: "B", voted: "C", auth: false},
  232. {signer: "A", voted: "B", auth: false},
  233. },
  234. results: []string{"A", "B"},
  235. }, {
  236. // Cascading changes are not allowed, only the account being voted on may change
  237. signers: []string{"A", "B", "C", "D"},
  238. votes: []testerVote{
  239. {signer: "A", voted: "C", auth: false},
  240. {signer: "B"},
  241. {signer: "C"},
  242. {signer: "A", voted: "D", auth: false},
  243. {signer: "B", voted: "C", auth: false},
  244. {signer: "C"},
  245. {signer: "A"},
  246. {signer: "B", voted: "D", auth: false},
  247. {signer: "C", voted: "D", auth: false},
  248. },
  249. results: []string{"A", "B", "C"},
  250. }, {
  251. // Changes reaching consensus out of bounds (via a deauth) execute on touch
  252. signers: []string{"A", "B", "C", "D"},
  253. votes: []testerVote{
  254. {signer: "A", voted: "C", auth: false},
  255. {signer: "B"},
  256. {signer: "C"},
  257. {signer: "A", voted: "D", auth: false},
  258. {signer: "B", voted: "C", auth: false},
  259. {signer: "C"},
  260. {signer: "A"},
  261. {signer: "B", voted: "D", auth: false},
  262. {signer: "C", voted: "D", auth: false},
  263. {signer: "A"},
  264. {signer: "C", voted: "C", auth: true},
  265. },
  266. results: []string{"A", "B"},
  267. }, {
  268. // Changes reaching consensus out of bounds (via a deauth) may go out of consensus on first touch
  269. signers: []string{"A", "B", "C", "D"},
  270. votes: []testerVote{
  271. {signer: "A", voted: "C", auth: false},
  272. {signer: "B"},
  273. {signer: "C"},
  274. {signer: "A", voted: "D", auth: false},
  275. {signer: "B", voted: "C", auth: false},
  276. {signer: "C"},
  277. {signer: "A"},
  278. {signer: "B", voted: "D", auth: false},
  279. {signer: "C", voted: "D", auth: false},
  280. {signer: "A"},
  281. {signer: "B", voted: "C", auth: true},
  282. },
  283. results: []string{"A", "B", "C"},
  284. }, {
  285. // Ensure that pending votes don't survive authorization status changes. This
  286. // corner case can only appear if a signer is quickly added, removed and then
  287. // readded (or the inverse), while one of the original voters dropped. If a
  288. // past vote is left cached in the system somewhere, this will interfere with
  289. // the final signer outcome.
  290. signers: []string{"A", "B", "C", "D", "E"},
  291. votes: []testerVote{
  292. {signer: "A", voted: "F", auth: true}, // Authorize F, 3 votes needed
  293. {signer: "B", voted: "F", auth: true},
  294. {signer: "C", voted: "F", auth: true},
  295. {signer: "D", voted: "F", auth: false}, // Deauthorize F, 4 votes needed (leave A's previous vote "unchanged")
  296. {signer: "E", voted: "F", auth: false},
  297. {signer: "B", voted: "F", auth: false},
  298. {signer: "C", voted: "F", auth: false},
  299. {signer: "D", voted: "F", auth: true}, // Almost authorize F, 2/3 votes needed
  300. {signer: "E", voted: "F", auth: true},
  301. {signer: "B", voted: "A", auth: false}, // Deauthorize A, 3 votes needed
  302. {signer: "C", voted: "A", auth: false},
  303. {signer: "D", voted: "A", auth: false},
  304. {signer: "B", voted: "F", auth: true}, // Finish authorizing F, 3/3 votes needed
  305. },
  306. results: []string{"B", "C", "D", "E", "F"},
  307. }, {
  308. // Epoch transitions reset all votes to allow chain checkpointing
  309. epoch: 3,
  310. signers: []string{"A", "B"},
  311. votes: []testerVote{
  312. {signer: "A", voted: "C", auth: true},
  313. {signer: "B"},
  314. {signer: "A"}, // Checkpoint block, (don't vote here, it's validated outside of snapshots)
  315. {signer: "B", voted: "C", auth: true},
  316. },
  317. results: []string{"A", "B"},
  318. },
  319. }
  320. // Run through the scenarios and test them
  321. for i, tt := range tests {
  322. // Create the account pool and generate the initial set of signers
  323. accounts := newTesterAccountPool()
  324. signers := make([]common.Address, len(tt.signers))
  325. for j, signer := range tt.signers {
  326. signers[j] = accounts.address(signer)
  327. }
  328. for j := 0; j < len(signers); j++ {
  329. for k := j + 1; k < len(signers); k++ {
  330. if bytes.Compare(signers[j][:], signers[k][:]) > 0 {
  331. signers[j], signers[k] = signers[k], signers[j]
  332. }
  333. }
  334. }
  335. // Create the genesis block with the initial set of signers
  336. genesis := &core.Genesis{
  337. ExtraData: make([]byte, extraVanity+common.AddressLength*len(signers)+extraSeal),
  338. }
  339. for j, signer := range signers {
  340. copy(genesis.ExtraData[extraVanity+j*common.AddressLength:], signer[:])
  341. }
  342. // Create a pristine blockchain with the genesis injected
  343. db := ethdb.NewMemDatabase()
  344. genesis.Commit(db)
  345. // Assemble a chain of headers from the cast votes
  346. headers := make([]*types.Header, len(tt.votes))
  347. for j, vote := range tt.votes {
  348. headers[j] = &types.Header{
  349. Number: big.NewInt(int64(j) + 1),
  350. Time: big.NewInt(int64(j) * int64(blockPeriod)),
  351. Coinbase: accounts.address(vote.voted),
  352. Extra: make([]byte, extraVanity+extraSeal),
  353. }
  354. if j > 0 {
  355. headers[j].ParentHash = headers[j-1].Hash()
  356. }
  357. if vote.auth {
  358. copy(headers[j].Nonce[:], nonceAuthVote)
  359. }
  360. accounts.sign(headers[j], vote.signer)
  361. }
  362. // Pass all the headers through clique and ensure tallying succeeds
  363. head := headers[len(headers)-1]
  364. snap, err := New(&params.CliqueConfig{Epoch: tt.epoch}, db).snapshot(&testerChainReader{db: db}, head.Number.Uint64(), head.Hash(), headers)
  365. if err != nil {
  366. t.Errorf("test %d: failed to create voting snapshot: %v", i, err)
  367. continue
  368. }
  369. // Verify the final list of signers against the expected ones
  370. signers = make([]common.Address, len(tt.results))
  371. for j, signer := range tt.results {
  372. signers[j] = accounts.address(signer)
  373. }
  374. for j := 0; j < len(signers); j++ {
  375. for k := j + 1; k < len(signers); k++ {
  376. if bytes.Compare(signers[j][:], signers[k][:]) > 0 {
  377. signers[j], signers[k] = signers[k], signers[j]
  378. }
  379. }
  380. }
  381. result := snap.signers()
  382. if len(result) != len(signers) {
  383. t.Errorf("test %d: signers mismatch: have %x, want %x", i, result, signers)
  384. continue
  385. }
  386. for j := 0; j < len(result); j++ {
  387. if !bytes.Equal(result[j][:], signers[j][:]) {
  388. t.Errorf("test %d, signer %d: signer mismatch: have %x, want %x", i, j, result[j], signers[j])
  389. }
  390. }
  391. }
  392. }