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.

790 lines
28 KiB

  1. <!-- markdown-link-check-disable -->
  2. # ***This an unfinished draft. Comments are welcome!***
  3. **TODO:** We will need to do small adaptations to the verification
  4. spec to reflect the semantics in the LightStore (verified, trusted,
  5. untrusted, etc. not needed anymore). In more detail:
  6. - The state of the Lightstore needs to go. Functions like `LatestVerified` can
  7. keep the name but will ignore state as it will not exist anymore.
  8. - verification spec should be adapted to the second parameter of
  9. `VerifyToTarget`
  10. being a lightblock; new version number of function tag;
  11. - We should clarify what is the expectation of VerifyToTarget
  12. so if it returns TimeoutError it can be assumed faulty. I guess that
  13. VerifyToTarget with correct full node should never terminate with
  14. TimeoutError.
  15. - We need to introduce a new version number for the new
  16. specification. So we should decide how
  17. to handle that.
  18. # Light Client Attack Detector
  19. In this specification, we strengthen the light client to be resistant
  20. against so-called light client attacks. In a light client attack, all
  21. the correct Tendermint full nodes agree on the sequence of generated
  22. blocks (no fork), but a set of faulty full nodes attack a light client
  23. by generating (signing) a block that deviates from the block of the
  24. same height on the blockchain. In order to do so, some of these faulty
  25. full nodes must have been validators before and violate
  26. [[TMBC-FM-2THIRDS]](TMBC-FM-2THIRDS-link), as otherwise, if
  27. [[TMBC-FM-2THIRDS]](TMBC-FM-2THIRDS-link) would hold,
  28. [verification](verification) would satisfy
  29. [[LCV-SEQ-SAFE.1]](LCV-SEQ-SAFE-link).
  30. An attack detector (or detector for short) is a mechanism that is used
  31. by the light client [supervisor](supervisor) after
  32. [verification](verification) of a new light block
  33. with the primary, to cross-check the newly learned light block with
  34. other peers (secondaries). It expects as input a light block with some
  35. height *root* (that serves as a root of trust), and a verification
  36. trace (a sequence of lightblocks) that the primary provided.
  37. In case the detector observes a light client attack, it computes
  38. evidence data that can be used by Tendermint full nodes to isolate a
  39. set of faulty full nodes that are still within the unbonding period
  40. (more than 1/3 of the voting power of the validator set at some block of the chain),
  41. and report them via ABCI to the application of a Tendermint blockchain
  42. in order to punish faulty nodes.
  43. ## Context of this document
  44. The light client [verification](verification) specification is
  45. designed for the Tendermint failure model (1/3 assumption)
  46. [[TMBC-FM-2THIRDS]](TMBC-FM-2THIRDS-link). It is safe under this
  47. assumption, and live if it can reliably (that is, no message loss, no
  48. duplication, and eventually delivered) and timely communicate with a
  49. correct full node. If [[TMBC-FM-2THIRDS]](TMBC-FM-2THIRDS-link) assumption is violated, the light client
  50. can be fooled to trust a light block that was not generated by
  51. Tendermint consensus.
  52. This specification, the attack detector, is a "second line of
  53. defense", in case the 1/3 assumption is violated. Its goal is to
  54. detect a light client attack (conflicting light blocks) and collect
  55. evidence. However, it is impractical to probe all full nodes. At this
  56. time we consider a simple scheme of maintaining an address book of
  57. known full nodes from which a small subset (e.g., 4) are chosen
  58. initially to communicate with. More involved book keeping with
  59. probabilistic guarantees can be considered at later stages of the
  60. project.
  61. The light client maintains a simple address book containing addresses
  62. of full nodes that it can pick as primary and secondaries. To obtain
  63. a new light block, the light client first does
  64. [verification](verification) with the primary, and then cross-checks
  65. the light block (and the trace of light blocks that led to it) with
  66. the secondaries using this specification.
  67. ## Tendermint Consensus and Light Client Attacks
  68. In this section we will give some mathematical definitions of what we
  69. mean by light client attacks (that are considered in this
  70. specification) and how they differ from main-chain forks. To this end
  71. we start by defining some properties of the sequence of blocks that is
  72. decided upon by Tendermint consensus in normal operation (if the
  73. Tendermint failure model holds
  74. [[TMBC-FM-2THIRDS]](TMBC-FM-2THIRDS-link)),
  75. and then define different
  76. deviations that correspond to attack scenarios.
  77. #### **[TMBC-GENESIS.1]**
  78. Let *Genesis* be the agreed-upon initial block (file).
  79. #### **[TMBC-FUNC-SIGN.1]**
  80. Let *b* and *c* be two light blocks with *b.Header.Height + 1 =
  81. c.Header.Height*. We define the predicate **signs(b,c)** to hold
  82. iff *c.Header.LastCommit* is in *PossibleCommit(b)*.
  83. [[TMBC-SOUND-DISTR-POSS-COMMIT.1]](TMBC-SOUND-DISTR-POSS-COMMIT-link).
  84. > The above encodes sequential verification, that is, intuitively,
  85. > b.Header.NextValidators = c.Header.Validators and 2/3 of
  86. > these Validators signed c?
  87. #### **[TMBC-FUNC-SUPPORT.1]**
  88. Let *b* and *c* be two light blocks. We define the predicate
  89. **supports(b,c,t)** to hold iff
  90. - *t - trustingPeriod < b.Header.Time < t*
  91. - the voting power in *b.NextValidators* of nodes in *c.Commit*
  92. is more than 1/3 of *TotalVotingPower(b.Header.NextValidators)*
  93. > That is, if the [Tendermint failure model](TMBC-FM-2THIRDS-link)
  94. > holds, then *c* has been signed by at least one correct full node, cf.
  95. > [[TMBC-VAL-CONTAINS-CORR.1]](TMBC-VAL-CONTAINS-CORR-link).
  96. > The following formalizes that *b* was properly generated by
  97. > Tendermint; *b* can be traced back to genesis
  98. #### **[TMBC-SEQ-ROOTED.1]**
  99. Let *b* be a light block.
  100. We define *sequ-rooted(b)* iff for all *i*, *1 <= i < h = b.Header.Height*,
  101. there exist light blocks *a(i)* s.t.
  102. - *a(1) = Genesis* and
  103. - *a(h) = b* and
  104. - *signs( a(i) , a(i+1) )*.
  105. > The following formalizes that *c* is trusted based on *b* in
  106. > skipping verification. Observe that we do not require here (yet)
  107. > that *b* was properly generated.
  108. #### **[TMBC-SKIP-TRACE.1]**
  109. Let *b* and *c* be light blocks. We define *skip-trace(b,c,t)* if at
  110. time t there exists an *h* and a sequence *a(1)*, ... *a(h)* s.t.
  111. - *a(1) = b* and
  112. - *a(h) = c* and
  113. - *supports( a(i), a(i+1), t)*, for all i, *1 <= i < h*.
  114. We call such a sequence *a(1)*, ... *a(h)* a **verification trace**.
  115. > The following formalizes that two light blocks of the same height
  116. > should agree on the content of the header. Observe that *b* and *c*
  117. > may disagree on the Commit. This is a special case if the canonical
  118. > commit has not been decided on, that is, if b.Header.Height is the
  119. > maximum height of all blocks decided upon by Tendermint at this
  120. > moment.
  121. #### **[TMBC-SIGN-SKIP-MATCH.1]**
  122. Let *a*, *b*, *c*, be light blocks and *t* a time, we define
  123. *sign-skip-match(a,b,c,t) = true* iff the following implication
  124. evaluates to true:
  125. - *sequ-rooted(a)* and
  126. - *b.Header.Height = c.Header.Height* and
  127. - *skip-trace(a,b,t)*
  128. - *skip-trace(a,c,t)*
  129. implies *b.Header = c.Header*.
  130. > Observe that *sign-skip-match* is defined via an implication. If it
  131. > evaluates to false this means that the left-hand-side of the
  132. > implication evaluates to true, and the right-hand-side evaluates to
  133. > false. In particular, there are two **different** headers *b* and
  134. > *c* that both can be verified from a common block *a* from the
  135. > chain. Thus, the following describes an attack.
  136. #### **[TMBC-ATTACK.1]**
  137. If there exists three light blocks a, b, and c, with
  138. *sign-skip-match(a,b,c,t) = false* then we have an *attack*. We say
  139. we have **an attack at height** *b.Header.Height* and write
  140. *attack(a,b,c,t)*.
  141. > The lightblock *a* need not be unique, that is, there may be
  142. > several blocks that satisfy the above requirement for the same
  143. > blocks *b* and *c*.
  144. [[TMBC-ATTACK.1]](#TMBC-ATTACK1) is a formalization of the violation
  145. of the agreement property based on the result of consensus, that is,
  146. the generated blocks.
  147. **Remark.**
  148. Violation of agreement is only possible if more than 1/3 of the validators (or
  149. next validators) of some previous block deviated from the protocol. The
  150. upcoming "accountability" specification will describe how to compute
  151. a set of at least 1/3 faulty nodes from two conflicting blocks. []
  152. There are different ways to characterize forks
  153. and attack scenarios. This specification uses the "node-based
  154. characterization of attacks" which focuses on what kinds of nodes are
  155. affected (light nodes vs. full nodes). For future reference and
  156. discussion we also provide a
  157. "block-based characterization of attacks" below.
  158. ### Node-based characterization of attacks
  159. #### **[TMBC-MC-FORK.1]**
  160. We say there is a (main chain) fork at time *t* if
  161. - there are two correct full nodes *i* and *j* and
  162. - *i* is different from *j* and
  163. - *i* has decided on *b* and
  164. - *j* has decided on *c* and
  165. - there exist *a* such that *attack(a,b,c,t)*.
  166. #### **[TMBC-LC-ATTACK.1]**
  167. We say there is a light client attack at time *t*, if
  168. - there is **no** (main chain) fork [[TMBC-MC-FORK.1]](#TMBC-MC-FORK1), and
  169. - there exist nodes that have computed light blocks *b* and *c* and
  170. - there exist *a* such that *attack(a,b,c,t)*.
  171. We say the attack is at height *a.Header.Height*.
  172. > In this specification we consider detection of light client
  173. > attacks. Intuitively, the case we consider is that
  174. > light block *b* is the one from the
  175. > blockchain, and some attacker has computed *c* and tries to wrongly
  176. > convince
  177. > the light client that *c* is the block from the chain.
  178. #### **[TMBC-LC-ATTACK-EVIDENCE.1]**
  179. We consider the following case of a light client attack
  180. [[TMBC-LC-ATTACK.1]](#TMBC-LC-ATTACK1):
  181. - *attack(a,b,c,t)*
  182. - there is a peer p1 that has a sequence *chain* of blocks from *a* to *b*
  183. - *skip-trace(a,c,t)*: by [[TMBC-SKIP-TRACE.1]](#TMBC-SKIP-TRACE1) there is a
  184. verification trace *v* of the form *a = v(1)*, ... *v(h) = c*
  185. Evidence for p1 (that proves an attack) consists for index i
  186. of v(i) and v(i+1) such that
  187. - E1(i). v(i) is equal to the block of *chain* at height v(i).Height, and
  188. - E2(i). v(i+1) that is different from the block of *chain* at
  189. height v(i+1).height
  190. > Observe p1 can
  191. >
  192. > - check that v(i+1) differs from its block at that height, and
  193. > - verify v(i+1) in one step from v(i) as v is a verification trace.
  194. **Proposition.** In the case of attack, evidence exists.
  195. *Proof.* First observe that
  196. - (A). (NOT E2(i)) implies E1(i+1)
  197. Now by contradiction assume there is no evidence. Thus
  198. - for all i, we have NOT E1(i) or NOT E2(i)
  199. - for i = 1 we have E1(1) and thus NOT E2(1)
  200. thus by induction on i, by (A) we have for all i that **E1(i)**
  201. - from attack we have E2(h-1), and as there is no evidence for
  202. i = h - 1 we get **NOT E1(h-1)**. Contradiction.
  203. QED.
  204. #### **[TMBC-LC-EVIDENCE-DATA.1]**
  205. To prove the attack to p1, because of Point E1, it is sufficient to
  206. submit
  207. - v(i).Height (rather than v(i)).
  208. - v(i+1)
  209. This information is *evidence for height v(i).Height*.
  210. ### Block-based characterization of attacks
  211. In this section we provide a different characterization of attacks. It
  212. is not defined on the nodes that are affected but purely on the
  213. content of the blocks. In that sense these definitions are less
  214. operational.
  215. > They might be relevant for a closer analysis of fork scenarios on the
  216. > chain, which is out of the scope of this specification.
  217. #### **[TMBC-SIGN-UNIQUE.1]**
  218. Let *b* and *c* be light blocks, we define the predicate
  219. *sign-unique(b,c)* to evaluate to true iff the following implication
  220. evaluates to true:
  221. - *b.Header.Height = c.Header.Height* and
  222. - *sequ-rooted(b)* and
  223. - *sequ-rooted(c)*
  224. implies *b = c*.
  225. #### **[TMBC-BLOCKS-MCFORK.1]**
  226. If there exists two light blocks b and c, with *sign-unique(b,c) =
  227. false* then we have a *fork*.
  228. > The difference of the above definition to
  229. > [[TMBC-MC-FORK.1]](#TMBC-MC-FORK1) is subtle. The latter requires a
  230. > full node being affected by a bad block while
  231. > [[TMBC-BLOCKS-MCFORK.1]](#TMBC-BLOCKS-MCFORK1) just requires that a
  232. > bad block exists, possibly in memory of an attacker.
  233. > The following captures a light client fork. There is no fork up to
  234. > the height of block b. However, c is of that height, is different,
  235. > and passes skipping verification. It is a stricter property than
  236. > [[TMBC-LC-ATTACK.1]](#TMBC-LC-ATTACK1), as
  237. > [[TMBC-LC-ATTACK.1]](#TMBC-LC-ATTACK1) requires that no correct full
  238. > node is affected.
  239. #### **[TMBC-BLOCKS-LCFORK.1]**
  240. Let *a*, *b*, *c*, be light blocks and *t* a time. We define
  241. *light-client-fork(a,b,c,t)* iff
  242. - *sign-skip-match(a,b,c,t) = false* and
  243. - *sequ-rooted(b)* and
  244. - *b* is "unique", that is, for all *d*, *sequ-rooted(d)* and
  245. *d.Header.Height = b.Header.Height* implies *d = b*
  246. > Finally, let us also define bogus blocks that have no support.
  247. > Observe that bogus is even defined if there is a fork.
  248. > Also, for the definition it would be sufficient to restrict *a* to
  249. > *a.height < b.height* (which is implied by the definitions which
  250. > unfold until *supports()*).
  251. #### **[TMBC-BOGUS.1]**
  252. Let *b* be a light block and *t* a time. We define *bogus(b,t)* iff
  253. - *sequ-rooted(b) = false* and
  254. - for all *a*, *sequ-rooted(a)* implies *skip-trace(a,b,t) = false*
  255. ### Informal Problem statement
  256. There is no sequential specification: the detector only makes sense
  257. in a distributed systems where some nodes misbehave.
  258. We work under the assumption that full nodes and validators are
  259. responsible for detecting attacks on the main chain, and the evidence
  260. reactor takes care of broadcasting evidence to communicate
  261. misbehaving nodes via ABCI to the application, and halt the chain in
  262. case of a fork. The point of this specification is to shield a light
  263. clients against attacks that cannot be detected by full nodes, and
  264. are fully addressed at light clients (and consequently IBC relayers,
  265. which use the light client protocols to observe the state of a
  266. blockchain). In order to provide full nodes the incentive to follow
  267. the protocols when communicating with the light client, this
  268. specification also considers the generation of evidence that will
  269. also be processed by the Tendermint blockchain.
  270. #### **[LCD-IP-MODEL.1]**
  271. The detector is designed under the assumption that
  272. - [[TMBC-FM-2THIRDS]](TMBC-FM-2THIRDS-link) may be violated
  273. - there is no fork on the main chain.
  274. > As a result some faulty full nodes may launch an attack on a light
  275. > client.
  276. The following requirements are operational in that they describe how
  277. things should be done, rather than what should be done. However, they
  278. do not constitute temporal logic verification conditions. For those,
  279. see [LCD-DIST-*] below.
  280. The detector is called in the [supervisor](supervisor) as follows
  281. ```go
  282. Evidences := AttackDetector(root_of_trust, verifiedLS);`
  283. ```
  284. where
  285. - `root-of-trust` is a light block that is trusted (that is,
  286. except upon initialization, the primary and the secondaries
  287. agreed on in the past), and
  288. - `verifiedLS` is a lightstore that contains a verification trace that
  289. starts from a lightblock that can be verified with the
  290. `root-of-trust` in one step and ends with a lightblock of the height
  291. requested by the user
  292. - `Evidences` is a list of evidences for misbehavior
  293. #### **[LCD-IP-STATEMENT.1]**
  294. Whenever AttackDetector is called, the detector should for each
  295. secondary try to replay the verification trace `verifiedLS` with the
  296. secondary
  297. - in case replaying leads to detection of a light client attack
  298. (one of the lightblocks differ from the one in verifiedLS with
  299. the same height), we should return evidence
  300. - if the secondary cannot provide a verification trace, we have no
  301. proof for an attack. Block *b* may be bogus. In this case the
  302. secondary is faulty and it should be replaced.
  303. ## Assumptions/Incentives/Environment
  304. It is not in the interest of faulty full nodes to talk to the
  305. detector as long as the detector is connected to at least one
  306. correct full node. This would only increase the likelihood of
  307. misbehavior being detected. Also we cannot punish them easily
  308. (cheaply). The absence of a response need not be the fault of the full
  309. node.
  310. Correct full nodes have the incentive to respond, because the
  311. detector may help them to understand whether their header is a good
  312. one. We can thus base liveness arguments of the detector on
  313. the assumptions that correct full nodes reliably talk to the
  314. detector.
  315. ### Assumptions
  316. #### **[LCD-A-CorrFull.1]**
  317. At all times there is at least one correct full
  318. node among the primary and the secondaries.
  319. > For this version of the detection we take this assumption. It
  320. > allows us to establish the invariant that the lightblock
  321. > `root-of-trust` is always the one from the blockchain, and we can
  322. > use it as starting point for the evidence computation. Moreover, it
  323. > allows us to establish the invariant at the supervisor that any
  324. > lightblock in the (top-level) lightstore is from the blockchain.
  325. > In the future we might design a lightclient based on the assumption
  326. > that at least in regular intervals the lightclient is connected to a
  327. > correct full node. This will require the detector to reconsider
  328. > `root-of-trust`, and remove lightblocks from the top-level
  329. > lightstore.
  330. #### **[LCD-A-RelComm.1]**
  331. Communication between the detector and a correct full node is
  332. reliable and bounded in time. Reliable communication means that
  333. messages are not lost, not duplicated, and eventually delivered. There
  334. is a (known) end-to-end delay *Delta*, such that if a message is sent
  335. at time *t* then it is received and processed by time *t + Delta*.
  336. This implies that we need a timeout of at least *2 Delta* for remote
  337. procedure calls to ensure that the response of a correct peer arrives
  338. before the timeout expires.
  339. ## Definitions
  340. ### Evidence
  341. Following the definition of
  342. [[TMBC-LC-ATTACK-EVIDENCE.1]](#TMBC-LC-ATTACK-EVIDENCE1), by evidence
  343. we refer to a variable of the following type
  344. #### **[LC-DATA-EVIDENCE.1]**
  345. ```go
  346. type LightClientAttackEvidence struct {
  347. ConflictingBlock LightBlock
  348. CommonHeight int64
  349. }
  350. ```
  351. As the above data is computed for a specific peer, the following
  352. data structure wraps the evidence and adds the peerID.
  353. #### **[LC-DATA-EVIDENCE-INT.1]**
  354. ```go
  355. type InternalEvidence struct {
  356. Evidence LightClientAttackEvidence
  357. Peer PeerID
  358. }
  359. ```
  360. #### **[LC-SUMBIT-EVIDENCE.1]**
  361. ```go
  362. func submitEvidence(Evidences []InternalEvidence)
  363. ```
  364. - Expected postcondition
  365. - for each `ev` in `Evidences`: submit `ev.Evidence` to `ev.Peer`
  366. ---
  367. ### LightStore
  368. Lightblocks and LightStores are defined in the verification
  369. specification [LCV-DATA-LIGHTBLOCK.1] and [LCV-DATA-LIGHTSTORE.1]. See
  370. the [verification specification][verification] for details.
  371. ## (Distributed) Problem statement
  372. > As the attack detector is there to reduce the impact of faulty
  373. > nodes, and faulty nodes imply that there is a distributed system,
  374. > there is no sequential specification to which this distributed
  375. > problem statement may refer to.
  376. The detector gets as input a trusted lightblock called *root* and an
  377. auxiliary lightstore called *primary_trace* with lightblocks that have
  378. been verified before, and that were provided by the primary.
  379. #### **[LCD-DIST-INV-ATTACK.1]**
  380. If the detector returns evidence for height *h*
  381. [[TMBC-LC-EVIDENCE-DATA.1]](#TMBC-LC-EVIDENCE-DATA1), then there is an
  382. attack at height *h*. [[TMBC-LC-ATTACK.1]](#TMBC-LC-ATTACK1)
  383. #### **[LCD-DIST-INV-STORE.1]**
  384. If the detector does not return evidence, then *primary_trace*
  385. contains only blocks from the blockchain.
  386. #### **[LCD-DIST-LIVE.1]**
  387. The detector eventually terminates.
  388. #### **[LCD-DIST-TERM-NORMAL.1]**
  389. If
  390. - the *primary_trace* contains only blocks from the blockchain, and
  391. - there is no attack, and
  392. - *Secondaries* is always non-empty, and
  393. - the age of *root* is always less than the trusting period,
  394. then the detector does not return evidence.
  395. #### **[LCD-DIST-TERM-ATTACK.1]**
  396. If
  397. - there is an attack, and
  398. - a secondary reports a block that conflicts
  399. with one of the blocks in *primary_trace*, and
  400. - *Secondaries* is always non-empty, and
  401. - the age of *root* is always less than the trusting period,
  402. then the detector returns evidence.
  403. > Observe that above we require that "a secondary reports a block that
  404. > conflicts". If there is an attack, but no secondary tries to launch
  405. > it against the detector (or the message from the secondary is lost
  406. > by the network), then there is nothing to detect for us.
  407. #### **[LCD-DIST-SAFE-SECONDARY.1]**
  408. No correct secondary is ever replaced.
  409. #### **[LCD-DIST-SAFE-BOGUS.1]**
  410. If
  411. - a secondary reports a bogus lightblock,
  412. - the age of *root* is always less than the trusting period,
  413. then the secondary is replaced before the detector terminates.
  414. > The above property is quite operational ("reports"), but it captures
  415. > quite closely the requirement. As the
  416. > detector only makes sense in a distributed setting, and does
  417. > not have a sequential specification, less "pure"
  418. > specification are acceptable.
  419. # Protocol
  420. ## Functions and Data defined in other Specifications
  421. ### From the supervisor
  422. ```go
  423. Replace_Secondary(addr Address, root-of-trust LightBlock)
  424. ```
  425. ### From the verifier
  426. ```go
  427. func VerifyToTarget(primary PeerID, root LightBlock,
  428. targetHeight Height) (LightStore, Result)
  429. ```
  430. > Note: the above differs from the current version in the second
  431. > parameter. verification will be revised.
  432. Observe that `VerifyToTarget` does communication with the secondaries
  433. via the function [FetchLightBlock](fetch).
  434. ### Shared data of the light client
  435. - a pool of full nodes *FullNodes* that have not been contacted before
  436. - peer set called *Secondaries*
  437. - primary
  438. > Note that the lightStore is not needed to be shared.
  439. ## Outline
  440. The problem laid out is solved by calling the function `AttackDetector`
  441. with a lightstore that contains a light block that has just been
  442. verified by the verifier.
  443. Then `AttackDetector` downloads headers from the secondaries. In case
  444. a conflicting header is downloaded from a secondary,
  445. `CreateEvidenceForPeer` which computes evidence in the case that
  446. indeed an attack is confirmed. It could be that the secondary reports
  447. a bogus block, which means that there need not be an attack, and the
  448. secondary is replaced.
  449. ## Details of the functions
  450. #### **[LCD-FUNC-DETECTOR.1]:**
  451. ```go
  452. func AttackDetector(root LightBlock, primary_trace []LightBlock)
  453. ([]InternalEvidence) {
  454. Evidences := new []InternalEvidence;
  455. for each secondary in Secondaries {
  456. // we replay the primary trace with the secondary, in
  457. // order to generate evidence that we can submit to the
  458. // secodary. We return the evidence + the trace the
  459. // secondary told us that spans the evidence at its local store
  460. EvidenceForSecondary, newroot, secondary_trace, result :=
  461. CreateEvidenceForPeer(secondary,
  462. root,
  463. primary_trace);
  464. if result == FaultyPeer {
  465. Replace_Secondary(root);
  466. }
  467. else if result == FoundEvidence {
  468. // the conflict is not bogus
  469. Evidences.Add(EvidenceForSecondary);
  470. // we replay the secondary trace with the primary, ...
  471. EvidenceForPrimary, _, result :=
  472. CreateEvidenceForPeer(primary,
  473. newroot,
  474. secondary_trace);
  475. if result == FoundEvidence {
  476. Evidences.Add(EvidenceForPrimary);
  477. }
  478. // At this point we do not care about the other error
  479. // codes. We already have generated evidence for an
  480. // attack and need to stop the lightclient. It does not
  481. // help to call replace_primary. Also we will use the
  482. // same primary to check with other secondaries in
  483. // later iterations of the loop
  484. }
  485. // In the case where the secondary reports NoEvidence
  486. // we do nothing
  487. }
  488. return Evidences;
  489. }
  490. ```
  491. - Expected precondition
  492. - root and primary trace are a verification trace
  493. - Expected postcondition
  494. - solves the problem statement (if attack found, then evidence is reported)
  495. - Error condition
  496. - `ErrorTrustExpired`: fails if root expires (outside trusting
  497. period) [[LCV-INV-TP.1]](LCV-INV-TP1-link)
  498. - `ErrorNoPeers`: if no peers are left to replace secondaries, and
  499. no evidence was found before that happened
  500. ---
  501. ```go
  502. func CreateEvidenceForPeer(peer PeerID, root LightBlock, trace LightStore)
  503. (Evidence, LightBlock, LightStore, result) {
  504. common := root;
  505. for i in 1 .. len(trace) {
  506. auxLS, result := VerifyToTarget(peer, common, trace[i].Header.Height)
  507. if result != ResultSuccess {
  508. // something went wrong; peer did not provide a verifyable block
  509. return (nil, nil, nil, FaultyPeer)
  510. }
  511. else {
  512. if auxLS.LatestVerified().Header != trace[i].Header {
  513. // the header reported by the peer differs from the
  514. // reference header in trace but both could be
  515. // verified from common in one step.
  516. // we can create evidence for submission to the secondary
  517. ev := new InternalEvidence;
  518. ev.Evidence.ConflictingBlock := trace[i];
  519. ev.Evidence.CommonHeight := common.Height;
  520. ev.Peer := peer
  521. return (ev, common, auxLS, FoundEvidence)
  522. }
  523. else {
  524. // the peer agrees with the trace, we move common forward
  525. // we could delete auxLS as it will be overwritten in
  526. // the next iteration
  527. common := trace[i]
  528. }
  529. }
  530. }
  531. return (nil, nil, nil, NoEvidence)
  532. }
  533. ```
  534. - Expected precondition
  535. - root and trace are a verification trace
  536. - Expected postcondition
  537. - finds evidence where trace and peer diverge
  538. - Error condition
  539. - `ErrorTrustExpired`: fails if root expires (outside trusting
  540. period) [[LCV-INV-TP.1]](LCV-INV-TP1-link)
  541. - If `VerifyToTarget` returns error but root is not expired then return
  542. `FaultyPeer`
  543. ---
  544. ## Correctness arguments
  545. #### Argument for [[LCD-DIST-INV-ATTACK.1]](#LCD-DIST-INV-ATTACK1)
  546. Under the assumption that root and trace are a verification trace,
  547. when in `CreateEvidenceForPeer` the detector the detector creates
  548. evidence, then the lightclient has seen two different headers (one via
  549. `trace` and one via `VerifyToTarget` for the same height that can both
  550. be verified in one step.
  551. #### Argument for [[LCD-DIST-INV-STORE.1]](#LCD-DIST-INV-STORE1)
  552. We assume that there is at least one correct peer, and there is no
  553. fork. As a result the correct peer has the correct sequence of
  554. blocks. Since the primary_trace is checked block-by-block also against
  555. each secondary, and at no point evidence was generated that means at
  556. no point there were conflicting blocks.
  557. #### Argument for [[LCD-DIST-LIVE.1]](#LCD-DIST-LIVE1)
  558. At the latest when [[LCV-INV-TP.1]](LCV-INV-TP1-link) is violated,
  559. `AttackDetector` terminates.
  560. #### Argument for [[LCD-DIST-TERM-NORMAL.1]](#LCD-DIST-TERM-NORMAL1)
  561. As there are finitely many peers, eventually the main loop
  562. terminates. As there is no attack no evidence can be generated.
  563. #### Argument for [[LCD-DIST-TERM-ATTACK.1]](#LCD-DIST-TERM-ATTACK1)
  564. Argument similar to [[LCD-DIST-TERM-NORMAL.1]](#LCD-DIST-TERM-NORMAL1)
  565. #### Argument for [[LCD-DIST-SAFE-SECONDARY.1]](#LCD-DIST-SAFE-SECONDARY1)
  566. Secondaries are only replaced if they time-out or if they report bogus
  567. blocks. The former is ruled out by the timing assumption, the latter
  568. by correct peers only reporting blocks from the chain.
  569. #### Argument for [[LCD-DIST-SAFE-BOGUS.1]](#LCD-DIST-SAFE-BOGUS1)
  570. Once a bogus block is recognized as such the secondary is removed.
  571. # References
  572. > links to other specifications/ADRs this document refers to
  573. [[verification]] The specification of the light client verification.
  574. [[supervisor]] The specification of the light client supervisor.
  575. [verification]: https://github.com/tendermint/spec/blob/master/rust-spec/lightclient/verification/verification.md
  576. [supervisor]: https://github.com/tendermint/spec/blob/master/rust-spec/lightclient/supervisor/supervisor.md
  577. [block]: https://github.com/tendermint/spec/blob/d46cd7f573a2c6a2399fcab2cde981330aa63f37/spec/core/data_structures.md
  578. [TMBC-FM-2THIRDS-link]: https://github.com/tendermint/spec/blob/master/rust-spec/lightclient/verification/verification.md#tmbc-fm-2thirds1
  579. [TMBC-SOUND-DISTR-POSS-COMMIT-link]: https://github.com/tendermint/spec/blob/master/rust-spec/lightclient/verification/verification.md#tmbc-sound-distr-poss-commit1
  580. [LCV-SEQ-SAFE-link]:https://github.com/tendermint/spec/blob/master/rust-spec/lightclient/verification/verification.md#lcv-seq-safe1
  581. [TMBC-VAL-CONTAINS-CORR-link]:
  582. https://github.com/tendermint/spec/blob/master/rust-spec/lightclient/verification/verification.md#tmbc-val-contains-corr1
  583. [fetch]:
  584. https://github.com/tendermint/spec/blob/master/rust-spec/lightclient/verification/verification.md#lcv-func-fetch1
  585. [LCV-INV-TP1-link]:
  586. https://github.com/tendermint/spec/blob/master/rust-spec/lightclient/verification/verification.md#lcv-inv-tp1