snapshot_test.go 14 KB

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