-
-
Notifications
You must be signed in to change notification settings - Fork 317
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'unstable' into slot-intervals
- Loading branch information
Showing
38 changed files
with
614 additions
and
216 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import {CachedBeaconStateAllForks} from "@lodestar/state-transition"; | ||
import {AllocSource, BufferPool} from "../util/bufferPool.js"; | ||
|
||
type ProcessStateBytesFn<T> = (stateBytes: Uint8Array) => Promise<T>; | ||
|
||
/* | ||
* Serialize state using the BufferPool if provided. | ||
*/ | ||
export async function serializeState<T>( | ||
state: CachedBeaconStateAllForks, | ||
source: AllocSource, | ||
processFn: ProcessStateBytesFn<T>, | ||
bufferPool?: BufferPool | null | ||
): Promise<T> { | ||
const size = state.type.tree_serializedSize(state.node); | ||
let stateBytes: Uint8Array | null = null; | ||
if (bufferPool) { | ||
const bufferWithKey = bufferPool.alloc(size, source); | ||
if (bufferWithKey) { | ||
stateBytes = bufferWithKey.buffer; | ||
const dataView = new DataView(stateBytes.buffer, stateBytes.byteOffset, stateBytes.byteLength); | ||
state.serializeToBytes({uint8Array: stateBytes, dataView}, 0); | ||
} | ||
} | ||
|
||
if (!stateBytes) { | ||
// we already have metrics in BufferPool so no need to do it here | ||
stateBytes = state.serialize(); | ||
} | ||
|
||
return processFn(stateBytes); | ||
// release the buffer back to the pool automatically | ||
} |
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
Oops, something went wrong.