@@ -14,26 +14,19 @@ import (
14
14
// its maximum capacity is reached.
15
15
// It is NOT THREAD SAFE to use.
16
16
type commitsTracker struct {
17
- // map of commit block hash to data
18
- // data = message + tracking linked list element pointer
19
- mapping map [common.Hash ]commitMessageMapData
20
- // double linked list of block hash
17
+ // map of commit block hash to linked list commit message.
18
+ mapping map [common.Hash ]* list.Element
19
+ // double linked list of commit messages
21
20
// to track the order commit messages were added in.
22
21
linkedList * list.List
23
22
capacity int
24
23
}
25
24
26
- type commitMessageMapData struct {
27
- message * CommitMessage
28
- // element contains a block hash value.
29
- element * list.Element
30
- }
31
-
32
25
// newCommitsTracker creates a new commit messages tracker
33
26
// with the capacity specified.
34
27
func newCommitsTracker (capacity int ) commitsTracker {
35
28
return commitsTracker {
36
- mapping : make (map [common.Hash ]commitMessageMapData , capacity ),
29
+ mapping : make (map [common.Hash ]* list. Element , capacity ),
37
30
linkedList : list .New (),
38
31
capacity : capacity ,
39
32
}
@@ -45,26 +38,21 @@ func newCommitsTracker(capacity int) commitsTracker {
45
38
func (ct * commitsTracker ) add (commitMessage * CommitMessage ) {
46
39
blockHash := commitMessage .Vote .Hash
47
40
48
- data , has := ct .mapping [blockHash ]
41
+ listElement , has := ct .mapping [blockHash ]
49
42
if has {
50
- // commit already exists so override the commit for the block hash ;
43
+ // commit already exists so override the commit message in the linked list ;
51
44
// do not move the list element in the linked list to avoid
52
45
// someone re-sending the same commit message and going at the
53
46
// front of the list, hence erasing other possible valid commit messages
54
47
// in the tracker.
55
- data .message = commitMessage
56
- ct .mapping [blockHash ] = data
48
+ listElement .Value = commitMessage
57
49
return
58
50
}
59
51
60
52
// add new block hash in tracker
61
53
ct .cleanup ()
62
- element := ct .linkedList .PushFront (blockHash )
63
- data = commitMessageMapData {
64
- message : commitMessage ,
65
- element : element ,
66
- }
67
- ct .mapping [blockHash ] = data
54
+ listElement = ct .linkedList .PushFront (commitMessage )
55
+ ct .mapping [blockHash ] = listElement
68
56
}
69
57
70
58
// cleanup removes the oldest commit message from the tracker
@@ -79,40 +67,42 @@ func (ct *commitsTracker) cleanup() {
79
67
oldestElement := ct .linkedList .Back ()
80
68
ct .linkedList .Remove (oldestElement )
81
69
82
- oldestBlockHash := oldestElement .Value .(common.Hash )
70
+ oldestCommitMessage := oldestElement .Value .(* CommitMessage )
71
+ oldestBlockHash := oldestCommitMessage .Vote .Hash
83
72
delete (ct .mapping , oldestBlockHash )
84
73
}
85
74
86
75
// delete deletes all the vote messages for a particular
87
76
// block hash from the vote messages tracker.
88
77
func (ct * commitsTracker ) delete (blockHash common.Hash ) {
89
- data , has := ct .mapping [blockHash ]
78
+ listElement , has := ct .mapping [blockHash ]
90
79
if ! has {
91
80
return
92
81
}
93
82
94
- ct .linkedList .Remove (data . element )
83
+ ct .linkedList .Remove (listElement )
95
84
delete (ct .mapping , blockHash )
96
85
}
97
86
98
87
// message returns a pointer to the
99
88
// commit message for a particular block hash from
100
89
// the tracker. It returns nil if the block hash
101
90
// does not exist in the tracker
102
- func (ct * commitsTracker ) message (
103
- blockHash common. Hash ) ( message * CommitMessage ) {
104
- data , ok := ct .mapping [blockHash ]
91
+ func (ct * commitsTracker ) message (blockHash common. Hash ) (
92
+ message * CommitMessage ) {
93
+ listElement , ok := ct .mapping [blockHash ]
105
94
if ! ok {
106
95
return nil
107
96
}
108
97
109
- return data . message
98
+ return listElement . Value .( * CommitMessage )
110
99
}
111
100
112
101
// forEach runs the function `f` on each
113
102
// commit message stored in the tracker.
114
103
func (ct * commitsTracker ) forEach (f func (message * CommitMessage )) {
115
104
for _ , data := range ct .mapping {
116
- f (data .message )
105
+ message := data .Value .(* CommitMessage )
106
+ f (message )
117
107
}
118
108
}
0 commit comments