Skip to content

Commit 2f3fbdc

Browse files
author
Asa Oines
authored
Send a commit whenever we can create a PreparedCertificate (ethereum#596)
1 parent ac86758 commit 2f3fbdc

File tree

4 files changed

+34
-38
lines changed

4 files changed

+34
-38
lines changed

.circleci/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ jobs:
6969
command: |
7070
set -euo pipefail
7171
export CELO_MONOREPO_DIR="$PWD"
72-
git clone --depth 1 https://github.com/celo-org/celo-monorepo.git ${CELO_MONOREPO_DIR} -b asaj/parent-signatures
72+
git clone --depth 1 https://github.com/celo-org/celo-monorepo.git ${CELO_MONOREPO_DIR} -b master
7373
yarn install || yarn install
7474
# separate build to avoid ENOMEM in CI :(
7575
yarn build --scope @celo/utils

consensus/istanbul/core/commit.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,13 @@ func (c *core) handleCommit(msg *istanbul.Message) error {
136136
logger.Trace("Got a quorum of commits", "tag", "stateTransition", "commits", c.current.Commits)
137137
c.commit()
138138
} else if c.current.GetPrepareOrCommitSize() >= minQuorumSize && c.state.Cmp(StatePrepared) < 0 {
139-
logger.Trace("Got enough prepares and commits to generate a PreparedCertificate")
140139
if err := c.current.CreateAndSetPreparedCertificate(minQuorumSize); err != nil {
141140
logger.Error("Failed to create and set preprared certificate", "err", err)
142141
return err
143142
}
143+
logger.Trace("Got quorum prepares or commits", "tag", "stateTransition", "commits", c.current.Commits, "prepares", c.current.Prepares)
144+
c.setState(StatePrepared)
145+
c.sendCommit()
144146
}
145147
}
146148

consensus/istanbul/core/prepare.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ func (c *core) handlePrepare(msg *istanbul.Message) error {
146146
// TODO(joshua): Remove state comparisons (or change the cmp function)
147147
if (preparesAndCommits >= minQuorumSize) && c.state.Cmp(StatePrepared) < 0 {
148148
if err := c.current.CreateAndSetPreparedCertificate(minQuorumSize); err != nil {
149+
logger.Error("Failed to create and set preprared certificate", "err", err)
149150
return err
150151
}
151152
logger.Trace("Got quorum prepares or commits", "tag", "stateTransition", "commits", c.current.Commits, "prepares", c.current.Prepares)

consensus/istanbul/core/roundstate.go

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,15 @@ func (s *roundState) GetPrepareOrCommitSize() int {
6363
s.mu.RLock()
6464
defer s.mu.RUnlock()
6565

66-
return s.getPrepareOrCommitSize()
66+
result := s.Prepares.Size() + s.Commits.Size()
67+
68+
// find duplicate one
69+
for _, m := range s.Prepares.Values() {
70+
if s.Commits.Get(m.Address) != nil {
71+
result--
72+
}
73+
}
74+
return result
6775
}
6876

6977
func (s *roundState) Subject() *istanbul.Subject {
@@ -143,51 +151,36 @@ func (s *roundState) Sequence() *big.Int {
143151
return s.sequence
144152
}
145153

146-
func (s *roundState) SetPreparedCertificate(preparedCertificate istanbul.PreparedCertificate) {
147-
s.mu.Lock()
148-
defer s.mu.Unlock()
149-
150-
s.preparedCertificate = preparedCertificate
151-
}
152-
153154
func (s *roundState) CreateAndSetPreparedCertificate(quorumSize int) error {
154155
s.mu.Lock()
155156
defer s.mu.Unlock()
156157

157-
prepareOrCommitSize := s.getPrepareOrCommitSize()
158-
if prepareOrCommitSize >= quorumSize {
159-
messages := make([]istanbul.Message, prepareOrCommitSize)
160-
i := 0
161-
for _, message := range s.Prepares.Values() {
162-
messages[i] = *message
163-
i++
158+
messages := make([]istanbul.Message, quorumSize)
159+
i := 0
160+
for _, message := range s.Prepares.Values() {
161+
if i == quorumSize {
162+
break
164163
}
165-
for _, message := range s.Commits.Values() {
166-
if s.Prepares.Get(message.Address) == nil {
167-
messages[i] = *message
168-
i++
169-
}
164+
messages[i] = *message
165+
i++
166+
}
167+
for _, message := range s.Commits.Values() {
168+
if i == quorumSize {
169+
break
170170
}
171-
s.preparedCertificate = istanbul.PreparedCertificate{
172-
Proposal: s.Preprepare.Proposal,
173-
PrepareOrCommitMessages: messages,
171+
if s.Prepares.Get(message.Address) == nil {
172+
messages[i] = *message
173+
i++
174174
}
175-
return nil
176-
} else {
175+
}
176+
if i != quorumSize {
177177
return errFailedCreatePreparedCertificate
178178
}
179-
}
180-
181-
func (s *roundState) getPrepareOrCommitSize() int {
182-
result := s.Prepares.Size() + s.Commits.Size()
183-
184-
// find duplicate one
185-
for _, m := range s.Prepares.Values() {
186-
if s.Commits.Get(m.Address) != nil {
187-
result--
188-
}
179+
s.preparedCertificate = istanbul.PreparedCertificate{
180+
Proposal: s.Preprepare.Proposal,
181+
PrepareOrCommitMessages: messages,
189182
}
190-
return result
183+
return nil
191184
}
192185

193186
// The DecodeRLP method should read one value from the given

0 commit comments

Comments
 (0)