-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #68 from proto-kit/feature/state-service-refactor
Refactored State Services into more clear structure
- Loading branch information
Showing
22 changed files
with
220 additions
and
102 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
packages/protocol/src/utils/merkletree/InMemoryMerkleTreeStorage.ts
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
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
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
File renamed without changes.
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,32 @@ | ||
## State Service Architecture | ||
|
||
The state services are built using a abstraction stack, where every service that is below the root is based on the parent. | ||
All of this services have the ability to base off one another to allow for diff-based changes. | ||
You can think of it a bit like git, where every commit is only the diff between the parents state and the next state. | ||
|
||
*But*, in this architecture there is no such thing as branching, it is strictly linear and behaves almost like a traditional stack. | ||
That means, for example, that you shouldn't change state in a service that has one or more children, as these changes might not get reflected instantly in the children's perception of state. | ||
|
||
Now I will go over all the different kinds of stateservices | ||
|
||
Btw all of this also applies to to merkle tree stores | ||
|
||
### AsyncStateService | ||
|
||
This is always the base for the whole stack and is meant to be implemented by the actual persistence layer. | ||
That means primarily the DB (but is also implemented by the in-memory state service). | ||
It's functions are async-based in order to enable external DB APIs | ||
|
||
### CachedStateService | ||
|
||
It receives a AsyncStateService as a parent and can build on top of it. | ||
It "caches" in the sense that it can preload state entries from the parent (asynchronously) and then lets circuits among others perform synchronous operations on them. | ||
It additionally caches write operations that can then later be merged back into the parent (or thrown away). | ||
|
||
### SyncCachedStateService | ||
|
||
These are the same as CachedStateService, with the difference that it accepts a CachedStateService and requires no preloading. | ||
|
||
### PreFilledStateService | ||
|
||
Pre-filled with only a part of the data needed. This is mostly used in Task implementation where all state needed is passed as args. |
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 @@ | ||
export interface AsyncMerkleTreeStore { | ||
openTransaction: () => void; | ||
|
||
commit: () => void; | ||
|
||
setNodeAsync: (key: bigint, level: number, value: bigint) => Promise<void>; | ||
|
||
getNodeAsync: (key: bigint, level: number) => Promise<bigint | undefined>; | ||
} |
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
35 changes: 35 additions & 0 deletions
35
packages/sequencer/src/state/merkle/SyncCachedMerkleTreeStore.ts
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,35 @@ | ||
import { | ||
InMemoryMerkleTreeStorage, MerkleTreeStore, | ||
RollupMerkleTree | ||
} from "@proto-kit/protocol"; | ||
|
||
export class SyncCachedMerkleTreeStore extends InMemoryMerkleTreeStorage { | ||
public constructor(private readonly parent: MerkleTreeStore) { | ||
super(); | ||
} | ||
|
||
public getNode(key: bigint, level: number): bigint | undefined { | ||
return super.getNode(key, level) ?? this.parent.getNode(key, level); | ||
} | ||
|
||
public setNode(key: bigint, level: number, value: bigint) { | ||
super.setNode(key, level, value); | ||
} | ||
|
||
public mergeIntoParent() { | ||
if (Object.keys(this.nodes).length === 0) { | ||
return; | ||
} | ||
|
||
const { height } = RollupMerkleTree; | ||
const { nodes } = this; | ||
|
||
Array.from({ length: height }).forEach((ignored, level) => | ||
Object.entries(nodes[level]).forEach((entry) => { | ||
this.parent.setNode(BigInt(entry[0]), level, entry[1]); | ||
}) | ||
); | ||
|
||
this.nodes = {} | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
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
File renamed without changes.
Oops, something went wrong.