map source ids to paths, skip libraries if opt is set
Get artifact data/ id + bytecodes
Analyze sources and asts using the SourceAnalyzer, this walks the ast and creates CoverageItems which are elements that need to be analyzed in coverage paired with a SourceLocation, which indicates where in the source code they are.
4 Look at source maps and create anchors which map bytecode instructions to CoverageItems
collect:
Create a contract runner with some options and coverage set to "true"
Run tests
Collect hit data (in TestResult.coverage, collected by the CoverageCollector), with artifact id and wether it comes from creation code or runtime code
Do the corresponding analysis depending on the options
How hit map is built:
There's a CoverageCollector inspector which collects hit maps
This is a mapping of bytecode hash -> HitMap
pub struct HitMap {
pub bytecode: Bytes,
pub hits: BTreeMap<usize, u64>, // usize is program counter, u64 is number of hits
}
On each step it will add a hit to the program counter of the instruction
What we would need:
Build a hitmap. This can be done with a DynTracer, looking at non system contracts. Pc is offset from bytecode in number of instructions (not bytes), each instruction is 16bits and it will have the u16 as the raw_opcode_unmasked. We could do the same that on evm, map pc for a given hash/bytecode with hits.
Once zksolc adds a source map, use it to create the anchors.
Currently Blocked by: https://github.com/matter-labs/era-compiler-solidity/issues/160
How it works
2 steps, prepare and collect
prepare:
SourceAnalyzer
, this walks the ast and createsCoverageItems
which are elements that need to be analyzed in coverage paired with aSourceLocation
, which indicates where in the source code they are. 4 Look at source maps and create anchors which map bytecode instructions to CoverageItemscollect:
How hit map is built:
There's a CoverageCollector inspector which collects hit maps This is a mapping of bytecode hash -> HitMap
On each
step
it will add a hit to the program counter of the instructionWhat we would need:
🔗 Useful Links
https://book.getfoundry.sh/reference/forge/forge-coverage?highlight=cforge coverage#examples