-
Notifications
You must be signed in to change notification settings - Fork 5
/
stm.go
74 lines (64 loc) · 1.4 KB
/
stm.go
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
64
65
66
67
68
69
70
71
72
73
74
package block_stm
import (
"context"
"errors"
"fmt"
"runtime"
"sync"
storetypes "cosmossdk.io/store/types"
)
func ExecuteBlock(
ctx context.Context,
blockSize int,
stores map[storetypes.StoreKey]int,
storage MultiStore,
executors int,
txExecutor TxExecutor,
) error {
return ExecuteBlockWithEstimates(
ctx, blockSize, stores, storage, executors,
nil, txExecutor,
)
}
func ExecuteBlockWithEstimates(
ctx context.Context,
blockSize int,
stores map[storetypes.StoreKey]int,
storage MultiStore,
executors int,
estimates []MultiLocations, // txn -> multi-locations
txExecutor TxExecutor,
) error {
if executors < 0 {
return fmt.Errorf("invalid number of executors: %d", executors)
}
if executors == 0 {
executors = maxParallelism()
}
// Create a new scheduler
scheduler := NewScheduler(blockSize)
mvMemory := NewMVMemoryWithEstimates(blockSize, stores, storage, scheduler, estimates)
var wg sync.WaitGroup
wg.Add(executors)
for i := 0; i < executors; i++ {
e := NewExecutor(ctx, scheduler, txExecutor, mvMemory, i)
go func() {
defer wg.Done()
e.Run()
}()
}
wg.Wait()
if !scheduler.Done() {
if ctx.Err() != nil {
// cancelled
return ctx.Err()
}
return errors.New("scheduler did not complete")
}
// Write the snapshot into the storage
mvMemory.WriteSnapshot(storage)
return nil
}
func maxParallelism() int {
return min(runtime.GOMAXPROCS(0), runtime.NumCPU())
}