mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 10:22:23 +00:00
add comments where appropriate. fix flakey test which now fails due to mutex contention.
This commit is contained in:
parent
6aba51311a
commit
9cc2c4770f
2 changed files with 10 additions and 6 deletions
|
|
@ -43,8 +43,8 @@ const maxWithdrawalCount = 10
|
||||||
// pending inclusion.
|
// pending inclusion.
|
||||||
type withdrawalQueue struct {
|
type withdrawalQueue struct {
|
||||||
queue types.Withdrawals
|
queue types.Withdrawals
|
||||||
pending chan struct{}
|
pending chan struct{} // channel to notify when there are pending withdrawals in the queue
|
||||||
mu sync.Mutex
|
mu sync.Mutex // mutex to gate access to the queue
|
||||||
}
|
}
|
||||||
|
|
||||||
// add queues a withdrawal for future inclusion.
|
// add queues a withdrawal for future inclusion.
|
||||||
|
|
@ -52,10 +52,11 @@ func (w *withdrawalQueue) add(withdrawal *types.Withdrawal) error {
|
||||||
w.mu.Lock()
|
w.mu.Lock()
|
||||||
defer w.mu.Unlock()
|
defer w.mu.Unlock()
|
||||||
|
|
||||||
if len(w.queue)+1 > maxWithdrawalCount {
|
if len(w.queue) == maxWithdrawalCount {
|
||||||
return errors.New("withdrawal queue full")
|
return errors.New("withdrawal queue full")
|
||||||
}
|
}
|
||||||
w.queue = append(w.queue, withdrawal)
|
w.queue = append(w.queue, withdrawal)
|
||||||
|
// send a notification to the pending channel without blocking
|
||||||
select {
|
select {
|
||||||
case w.pending <- struct{}{}:
|
case w.pending <- struct{}{}:
|
||||||
default:
|
default:
|
||||||
|
|
@ -74,11 +75,14 @@ func (w *withdrawalQueue) gatherPending(gatherCount int) []*types.Withdrawal {
|
||||||
return w.queue[:gatherCount]
|
return w.queue[:gatherCount]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// popFront removes a number of withdrawals from the front of the queue
|
||||||
|
// up to a maximum count.
|
||||||
func (w *withdrawalQueue) popFront(count int) {
|
func (w *withdrawalQueue) popFront(count int) {
|
||||||
w.mu.Lock()
|
w.mu.Lock()
|
||||||
defer w.mu.Unlock()
|
defer w.mu.Unlock()
|
||||||
|
|
||||||
w.queue = w.queue[count:]
|
w.queue = w.queue[count:]
|
||||||
|
// if the queue is still non-empty, notify so they will be picked up
|
||||||
if len(w.queue) > 0 {
|
if len(w.queue) > 0 {
|
||||||
select {
|
select {
|
||||||
case w.pending <- struct{}{}:
|
case w.pending <- struct{}{}:
|
||||||
|
|
@ -158,7 +162,7 @@ func (c *SimulatedBeacon) Stop() error {
|
||||||
|
|
||||||
// sealBlock initiates payload building for a new block and creates a new block
|
// sealBlock initiates payload building for a new block and creates a new block
|
||||||
// with the completed payload.
|
// with the completed payload.
|
||||||
func (c *SimulatedBeacon) sealBlock(sealEmpty bool, timestamp uint64) (bool, error) {
|
func (c *SimulatedBeacon) sealBlock(allowEmpty bool, timestamp uint64) (committed bool, err error) {
|
||||||
if timestamp <= c.lastBlockTime {
|
if timestamp <= c.lastBlockTime {
|
||||||
timestamp = c.lastBlockTime + 1
|
timestamp = c.lastBlockTime + 1
|
||||||
}
|
}
|
||||||
|
|
@ -193,7 +197,7 @@ func (c *SimulatedBeacon) sealBlock(sealEmpty bool, timestamp uint64) (bool, err
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
payload := envelope.ExecutionPayload
|
payload := envelope.ExecutionPayload
|
||||||
if !sealEmpty && len(payload.Transactions) == 0 && len(payload.Withdrawals) == 0 {
|
if !allowEmpty && len(payload.Transactions) == 0 && len(payload.Withdrawals) == 0 {
|
||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -96,7 +96,7 @@ func TestSimulatedBeaconSendWithdrawals(t *testing.T) {
|
||||||
defer subscription.Unsubscribe()
|
defer subscription.Unsubscribe()
|
||||||
|
|
||||||
// generate some withdrawals
|
// generate some withdrawals
|
||||||
for i := 0; i < 20; i++ {
|
for i := 0; i < 10; i++ {
|
||||||
withdrawals = append(withdrawals, types.Withdrawal{Index: uint64(i)})
|
withdrawals = append(withdrawals, types.Withdrawal{Index: uint64(i)})
|
||||||
if err := mock.withdrawals.add(&withdrawals[i]); err != nil {
|
if err := mock.withdrawals.add(&withdrawals[i]); err != nil {
|
||||||
t.Fatal("addWithdrawal failed", err)
|
t.Fatal("addWithdrawal failed", err)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue