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.

788 lines
28 KiB

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