-
-
Notifications
You must be signed in to change notification settings - Fork 291
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Use Buffer.concat for bytes utils * Simplify Buffer.concat() * Remove need for byteArrayConcat Co-authored-by: dapplion <[email protected]>
- Loading branch information
Showing
6 changed files
with
49 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,23 @@ | ||
import {allForks} from "@lodestar/types"; | ||
import {allForks, RootHex} from "@lodestar/types"; | ||
import {IChainForkConfig} from "@lodestar/config"; | ||
import {byteArrayConcat} from "../../../util/bytes.js"; | ||
import {toHex} from "@lodestar/utils"; | ||
|
||
/** | ||
* Hash SignedBeaconBlock in a byte form easy to compare only | ||
* @param blocks | ||
* @param config | ||
* String to uniquely identify block segments. Used for peer scoring and to compare if batches are equivalent. | ||
*/ | ||
export function hashBlocks(blocks: allForks.SignedBeaconBlock[], config: IChainForkConfig): Uint8Array { | ||
return byteArrayConcat( | ||
blocks.map((block) => config.getForkTypes(block.message.slot).SignedBeaconBlock.hashTreeRoot(block)) | ||
); | ||
export function hashBlocks(blocks: allForks.SignedBeaconBlock[], config: IChainForkConfig): RootHex { | ||
switch (blocks.length) { | ||
case 0: | ||
return "0x"; | ||
case 1: | ||
return toHex(config.getForkTypes(blocks[0].message.slot).SignedBeaconBlock.hashTreeRoot(blocks[0])); | ||
default: { | ||
const block0 = blocks[0]; | ||
const blockN = blocks[blocks.length - 1]; | ||
return ( | ||
toHex(config.getForkTypes(block0.message.slot).SignedBeaconBlock.hashTreeRoot(block0)) + | ||
toHex(config.getForkTypes(blockN.message.slot).SignedBeaconBlock.hashTreeRoot(blockN)) | ||
); | ||
} | ||
} | ||
} |
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,22 @@ | ||
import {itBench} from "@dapplion/benchmark"; | ||
|
||
describe("bytes utils", function () { | ||
const roots: Uint8Array[] = []; | ||
let buffers: Buffer[] = []; | ||
const count = 32; | ||
before(function () { | ||
this.timeout(60 * 1000); | ||
for (let i = 0; i < count; i++) { | ||
roots.push(new Uint8Array(Array.from({length: 32}, () => i))); | ||
} | ||
buffers = roots.map((root) => Buffer.from(root.buffer)); | ||
}); | ||
|
||
itBench({ | ||
id: `Buffer.concat ${count} items`, | ||
fn: () => { | ||
Buffer.concat(buffers); | ||
}, | ||
runsFactor: 1000, | ||
}); | ||
}); |
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
f0a5f41
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Possible performance regression was detected for some benchmarks.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold.
Full benchmark results