aboutsummaryrefslogtreecommitdiffstats
path: root/integration_test/utils.go
blob: 6c665ade4e39c108dd36a3ce6a70c14fe5bd0a75 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package integration

import (
    "time"

    "github.com/dexon-foundation/dexon-consensus-core/core/blockdb"
    "github.com/dexon-foundation/dexon-consensus-core/core/test"
    "github.com/dexon-foundation/dexon-consensus-core/core/types"
)

// PrepareNodes setups nodes for testing.
func PrepareNodes(
    nodeCount int,
    networkLatency, proposingLatency test.LatencyModel) (
    apps map[types.NodeID]*test.App,
    dbs map[types.NodeID]blockdb.BlockDatabase,
    nodes map[types.NodeID]*Node,
    err error) {

    apps = make(map[types.NodeID]*test.App)
    dbs = make(map[types.NodeID]blockdb.BlockDatabase)
    nodes = make(map[types.NodeID]*Node)

    gov, err := test.NewGovernance(nodeCount, 700*time.Millisecond)
    if err != nil {
        return
    }
    for _, prvKey := range gov.PrivateKeys() {
        nID := types.NewNodeID(prvKey.PublicKey())
        apps[nID] = test.NewApp()
        dbs[nID], err = blockdb.NewMemBackedBlockDB()
        if err != nil {
            return
        }
        nodes[nID] = NewNode(
            apps[nID],
            gov,
            dbs[nID],
            prvKey,
            networkLatency,
            proposingLatency,
        )
    }
    return
}

// VerifyApps is a helper to check delivery between test.Apps
func VerifyApps(apps map[types.NodeID]*test.App) (err error) {
    for vFrom, fromApp := range apps {
        if err = fromApp.Verify(); err != nil {
            return
        }
        for vTo, toApp := range apps {
            if vFrom == vTo {
                continue
            }
            if err = fromApp.Compare(toApp); err != nil {
                return
            }
        }
    }
    return
}