/
accountwgs.go
85 lines (71 loc) · 1.52 KB
/
accountwgs.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
75
76
77
78
79
80
81
82
83
84
85
package baseapp
import (
"sync"
sdk "github.com/Finschia/finschia-sdk/types"
)
type AccountWGs struct {
mtx sync.Mutex
wgs map[string]*sync.WaitGroup
}
func NewAccountWGs() *AccountWGs {
return &AccountWGs{
wgs: make(map[string]*sync.WaitGroup),
}
}
func (aw *AccountWGs) Register(tx sdk.Tx) (waits []*sync.WaitGroup, signals []*AccountWG) {
signers := getUniqSigners(tx)
aw.mtx.Lock()
defer aw.mtx.Unlock()
for _, signer := range signers {
if wg := aw.wgs[signer]; wg != nil {
waits = append(waits, wg)
}
sig := waitGroup1()
aw.wgs[signer] = sig
signals = append(signals, NewAccountWG(signer, sig))
}
return waits, signals
}
func (aw *AccountWGs) Wait(waits []*sync.WaitGroup) {
for _, wait := range waits {
wait.Wait()
}
}
func (aw *AccountWGs) Done(signals []*AccountWG) {
aw.mtx.Lock()
defer aw.mtx.Unlock()
for _, signal := range signals {
signal.wg.Done()
if aw.wgs[signal.acc] == signal.wg {
delete(aw.wgs, signal.acc)
}
}
}
func getUniqSigners(tx sdk.Tx) []string {
seen := map[string]bool{}
var signers []string
for _, msg := range tx.GetMsgs() {
for _, addr := range msg.GetSigners() {
if !seen[addr.String()] {
signers = append(signers, string(addr))
seen[addr.String()] = true
}
}
}
return signers
}
type AccountWG struct {
acc string
wg *sync.WaitGroup
}
func NewAccountWG(acc string, wg *sync.WaitGroup) *AccountWG {
return &AccountWG{
acc: acc,
wg: wg,
}
}
func waitGroup1() (wg *sync.WaitGroup) {
wg = &sync.WaitGroup{}
wg.Add(1)
return wg
}