-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Shared state with light store and in memory store implementation.
- Loading branch information
Showing
7 changed files
with
171 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
...client/src/main/scala/ch/epfl/ognjanovic/stevan/tendermint/light/LightBlockStatuses.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package ch.epfl.ognjanovic.stevan.tendermint.light | ||
|
||
object LightBlockStatuses { | ||
sealed trait LightBlockStatus | ||
|
||
object Verified extends LightBlockStatus | ||
|
||
object Trusted extends LightBlockStatus | ||
} |
44 changes: 44 additions & 0 deletions
44
.../src/main/scala/ch/epfl/ognjanovic/stevan/tendermint/light/store/InMemoryLightStore.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package ch.epfl.ognjanovic.stevan.tendermint.light.store | ||
|
||
import ch.epfl.ognjanovic.stevan.tendermint.light.LightBlockStatuses.{LightBlockStatus, Trusted, Verified} | ||
import ch.epfl.ognjanovic.stevan.tendermint.verified.types.{Height, LightBlock} | ||
|
||
import scala.collection.mutable | ||
|
||
sealed class InMemoryLightStore( | ||
private val verified: mutable.SortedMap[Height, LightBlock], | ||
private val trusted: mutable.SortedMap[Height, LightBlock], | ||
private val inverse: mutable.Map[Height, LightBlockStatus] | ||
) extends LightStore { | ||
|
||
override def get(height: Height, status: LightBlockStatus): Option[LightBlock] = { | ||
statusNamespace(status).get(height) | ||
} | ||
|
||
override def update(lightBlock: LightBlock, status: LightBlockStatus): Unit = { | ||
inverse | ||
.get(lightBlock.header.height) | ||
.foreach(previousStatus ⇒ statusNamespace(previousStatus).remove(lightBlock.header.height)) | ||
statusNamespace(status).put(lightBlock.header.height, lightBlock) | ||
inverse.put(lightBlock.header.height, status) | ||
} | ||
|
||
override def remove(height: Height): Unit = { | ||
inverse.get(height).foreach(status ⇒ statusNamespace(status).remove(height)) | ||
} | ||
|
||
override def latest(status: LightBlockStatus): Option[LightBlock] = { | ||
val map = statusNamespace(status) | ||
map.get(map.lastKey) | ||
} | ||
|
||
override def all(status: LightBlockStatus): Iterable[LightBlock] = statusNamespace(status).values | ||
|
||
private def statusNamespace(status: LightBlockStatus): mutable.SortedMap[Height, LightBlock] = { | ||
status match { | ||
case Verified ⇒ verified | ||
case Trusted ⇒ trusted | ||
} | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
...t-client/src/main/scala/ch/epfl/ognjanovic/stevan/tendermint/light/store/LightStore.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package ch.epfl.ognjanovic.stevan.tendermint.light.store | ||
|
||
import ch.epfl.ognjanovic.stevan.tendermint.light.LightBlockStatuses.LightBlockStatus | ||
import ch.epfl.ognjanovic.stevan.tendermint.verified.types.{Height, LightBlock} | ||
|
||
// ported from tendermint-rs | ||
/** | ||
* Implementations need to be stateful. | ||
*/ | ||
trait LightStore { | ||
def get(height: Height, status: LightBlockStatus): Option[LightBlock] | ||
|
||
def update(lightBlock: LightBlock, status: LightBlockStatus) | ||
|
||
def remove(height: Height) | ||
|
||
def latest(status: LightBlockStatus): Option[LightBlock] | ||
|
||
def all(status: LightBlockStatus): Iterable[LightBlock] | ||
} |
41 changes: 41 additions & 0 deletions
41
...scala/ch/epfl/ognjanovic/stevan/tendermint/light/store/LightStoreBackedTrustedState.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package ch.epfl.ognjanovic.stevan.tendermint.light.store | ||
|
||
import ch.epfl.ognjanovic.stevan.tendermint.light.LightBlockStatuses.{Trusted, Verified} | ||
import ch.epfl.ognjanovic.stevan.tendermint.verified.light.TrustedStates.TrustedState | ||
import ch.epfl.ognjanovic.stevan.tendermint.verified.light.VotingPowerVerifiers.VotingPowerVerifier | ||
import ch.epfl.ognjanovic.stevan.tendermint.verified.types.LightBlock | ||
|
||
class LightStoreBackedTrustedState(private val lightStore: LightStore, private val trustVerifier: VotingPowerVerifier) | ||
extends TrustedState { | ||
|
||
override def trusted(lightBlock: LightBlock): Boolean = { | ||
if (!(lightBlock.header.height > currentHeight())) | ||
throw new IllegalArgumentException( | ||
"It is not possible to call this method with a block lower that the latest verified one") | ||
|
||
if (isAdjacent(lightBlock)) | ||
trustedLightBlock.header.nextValidators == lightBlock.header.validators | ||
else | ||
trustVerifier.trustedCommit(trustedLightBlock.nextValidatorSet, lightBlock.commit) | ||
} | ||
|
||
override def trustedLightBlock: LightBlock = { | ||
val possibleTrusted = lightStore.latest(Trusted) | ||
val possibleVerified = lightStore.latest(Verified) | ||
|
||
(possibleTrusted, possibleVerified) match { | ||
case (Some(trusted), Some(verified)) if trusted.header.height < verified.header.height ⇒ | ||
verified | ||
case (Some(trusted), None) ⇒ trusted | ||
case _ ⇒ throw new IllegalStateException("Trusted state should never end up in this situation") | ||
} | ||
} | ||
|
||
override def increaseTrust(lightBlock: LightBlock): TrustedState = { | ||
if (!(lightBlock.header.height > currentHeight() && trusted(lightBlock))) | ||
throw new IllegalArgumentException("Illegal light block:" + lightBlock) | ||
lightStore.update(lightBlock, Verified) | ||
new LightStoreBackedTrustedState(lightStore, trustVerifier) | ||
} | ||
|
||
} |