kaspad/domain/consensus/test_consensus_render_to_dot.go
Ori Newman 5dbb1da84b
Implement pruning point proof (#1832)
* Calculate GHOSTDAG, reachability etc for each level

* Don't preallocate cache for dag stores except level 0 and reduce the number of connections in the integration test to 32

* Reduce the number of connections in the integration test to 16

* Increase page file

* BuildPruningPointProof

* BuildPruningPointProof

* Add PruningProofManager

* Implement ApplyPruningPointProof

* Add prefix and fix blockAtDepth and fill headersByLevel

* Some bug fixes

* Include all relevant blocks for each level in the proof

* Fix syncAndValidatePruningPointProof to return the right block hash

* Fix block window

* Fix isAncestorOfPruningPoint

* Ban for rule errors on pruning proof

* Find common ancestor for blockAtDepthMAtNextLevel

* Use pruning proof in TestValidateAndInsertImportedPruningPoint

* stage status and finality point for proof blocks

* Uncomment golint

* Change test timeouts

* Calculate merge set for ApplyPruningPointProof

* Increase test timeout

* Add better caching for daa window store

* Return to default timeout

* Add ErrPruningProofMissesBlocksBelowPruningPoint

* Add errDAAWindowBlockNotFound

* Force connection loop next iteration on connection manager stop

* Revert to Test64IncomingConnections

* Remove BlockAtDepth from DAGTraversalManager

* numBullies->16

* Set page file size to 8gb

* Increase p2p max message size

* Test64IncomingConnections->Test16IncomingConnections

* Add comment for PruningProofM

* Add comment in `func (c *ConnectionManager) Stop()`

* Rename isAncestorOfPruningPoint->isAncestorOfSelectedTip

* Revert page file to 16gb

* Improve ExpectedHeaderPruningPoint perf

* Fix comment

* Revert "Improve ExpectedHeaderPruningPoint perf"

This reverts commit bca1080e7140c78d510f51bbea858ae280c2f38e.

* Don't test windows
2021-10-26 09:48:27 +03:00

84 lines
2.0 KiB
Go

package consensus
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os/exec"
"strings"
"github.com/kaspanet/kaspad/domain/consensus/model"
)
// RenderDAGToDot is a helper function for debugging tests.
// It requires graphviz installed.
func (tc *testConsensus) RenderDAGToDot(filename string) error {
dotScript, _ := tc.convertToDot()
return renderDotScript(dotScript, filename)
}
func (tc *testConsensus) convertToDot() (string, error) {
var dotScriptBuilder strings.Builder
dotScriptBuilder.WriteString("digraph {\n\trankdir = TB; \n")
edges := []string{}
blocksIterator, err := tc.blockStore.AllBlockHashesIterator(tc.databaseContext)
if err != nil {
return "", err
}
defer blocksIterator.Close()
stagingArea := model.NewStagingArea()
for ok := blocksIterator.First(); ok; ok = blocksIterator.Next() {
hash, err := blocksIterator.Get()
if err != nil {
return "", err
}
dotScriptBuilder.WriteString(fmt.Sprintf("\t\"%s\";\n", hash))
parents, err := tc.dagTopologyManagers[0].Parents(stagingArea, hash)
if err != nil {
return "", err
}
for _, parentHash := range parents {
edges = append(edges, fmt.Sprintf("\t\"%s\" -> \"%s\";", hash, parentHash))
}
}
dotScriptBuilder.WriteString("\n")
dotScriptBuilder.WriteString(strings.Join(edges, "\n"))
dotScriptBuilder.WriteString("\n}")
return dotScriptBuilder.String(), nil
}
func renderDotScript(dotScript string, filename string) error {
command := exec.Command("dot", "-Tsvg")
stdin, err := command.StdinPipe()
if err != nil {
return fmt.Errorf("Error creating stdin pipe: %s", err)
}
spawn("renderDotScript", func() {
defer stdin.Close()
_, err = io.WriteString(stdin, dotScript)
if err != nil {
panic(fmt.Errorf("Error writing dotScript into stdin pipe: %s", err))
}
})
var stderr bytes.Buffer
command.Stderr = &stderr
svg, err := command.Output()
if err != nil {
return fmt.Errorf("Error getting output of dot: %s\nstderr:\n%s", err, stderr.String())
}
return ioutil.WriteFile(filename, svg, 0600)
}