Skip to content

Commit e092c7c

Browse files
authored
Fix payload attributes not updating after missed slot (#601)
1 parent 59549f6 commit e092c7c

File tree

4 files changed

+115
-7
lines changed

4 files changed

+115
-7
lines changed

services/api/optimistic_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ func TestBuilderApiSubmitNewBlockOptimistic(t *testing.T) {
470470
require.NoError(t, err)
471471
withRoot, err := ComputeWithdrawalsRoot([]*capella.Withdrawal{})
472472
require.NoError(t, err)
473-
backend.relay.payloadAttributes[emptyHash] = payloadAttributesHelper{
473+
backend.relay.payloadAttributes[getPayloadAttributesKey(emptyHash, tc.slot)] = payloadAttributesHelper{
474474
slot: tc.slot,
475475
withdrawalsRoot: withRoot,
476476
payloadAttributes: beaconclient.PayloadAttributes{

services/api/service.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,7 @@ func (api *RelayAPI) processPayloadAttributes(payloadAttributes beaconclient.Pay
679679

680680
// discard payload attributes if already known
681681
api.payloadAttributesLock.RLock()
682-
_, ok := api.payloadAttributes[payloadAttributes.Data.ParentBlockHash]
682+
_, ok := api.payloadAttributes[getPayloadAttributesKey(payloadAttributes.Data.ParentBlockHash, payloadAttrSlot)]
683683
api.payloadAttributesLock.RUnlock()
684684

685685
if ok {
@@ -719,12 +719,12 @@ func (api *RelayAPI) processPayloadAttributes(payloadAttributes beaconclient.Pay
719719
// Step 1: clean up old ones
720720
for parentBlockHash, attr := range api.payloadAttributes {
721721
if attr.slot < apiHeadSlot {
722-
delete(api.payloadAttributes, parentBlockHash)
722+
delete(api.payloadAttributes, getPayloadAttributesKey(parentBlockHash, attr.slot))
723723
}
724724
}
725725

726726
// Step 2: save new one
727-
api.payloadAttributes[payloadAttributes.Data.ParentBlockHash] = payloadAttributesHelper{
727+
api.payloadAttributes[getPayloadAttributesKey(payloadAttributes.Data.ParentBlockHash, payloadAttrSlot)] = payloadAttributesHelper{
728728
slot: payloadAttrSlot,
729729
parentHash: payloadAttributes.Data.ParentBlockHash,
730730
withdrawalsRoot: withdrawalsRoot,
@@ -1617,7 +1617,7 @@ func (api *RelayAPI) checkSubmissionFeeRecipient(w http.ResponseWriter, log *log
16171617

16181618
func (api *RelayAPI) checkSubmissionPayloadAttrs(w http.ResponseWriter, log *logrus.Entry, submission *common.BlockSubmissionInfo) (payloadAttributesHelper, bool) {
16191619
api.payloadAttributesLock.RLock()
1620-
attrs, ok := api.payloadAttributes[submission.BidTrace.ParentHash.String()]
1620+
attrs, ok := api.payloadAttributes[getPayloadAttributesKey(submission.BidTrace.ParentHash.String(), submission.BidTrace.Slot)]
16211621
api.payloadAttributesLock.RUnlock()
16221622
if !ok || submission.BidTrace.Slot != attrs.slot {
16231623
log.WithFields(logrus.Fields{

services/api/service_test.go

Lines changed: 106 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ func TestBuilderSubmitBlock(t *testing.T) {
456456
},
457457
}
458458
backend.relay.payloadAttributes = make(map[string]payloadAttributesHelper)
459-
backend.relay.payloadAttributes[parentHash] = payloadAttributesHelper{
459+
backend.relay.payloadAttributes[getPayloadAttributesKey(parentHash, submissionSlot)] = payloadAttributesHelper{
460460
slot: submissionSlot,
461461
parentHash: parentHash,
462462
payloadAttributes: beaconclient.PayloadAttributes{
@@ -627,6 +627,110 @@ func TestCheckSubmissionFeeRecipient(t *testing.T) {
627627
}
628628
}
629629

630+
func TestProcessPayloadAttrs(t *testing.T) {
631+
withdrawalsRoot, err := utils.HexToHash(testWithdrawalsRoot)
632+
require.NoError(t, err)
633+
parentHash, err := utils.HexToHash(testParentHash)
634+
require.NoError(t, err)
635+
636+
t.Run("Stores updated payload attribute with parent hash is the same", func(t *testing.T) {
637+
_, _, backend := startTestBackend(t)
638+
attrsEvent := beaconclient.PayloadAttributesEvent{
639+
Version: common.ForkVersionStringDeneb,
640+
Data: beaconclient.PayloadAttributesEventData{
641+
ProposalSlot: testSlot,
642+
ParentBlockHash: testParentHash,
643+
PayloadAttributes: beaconclient.PayloadAttributes{
644+
PrevRandao: testPrevRandao,
645+
Withdrawals: []*capella.Withdrawal{
646+
{
647+
Index: 989694,
648+
},
649+
},
650+
ParentBeaconBlockRoot: testParentHash,
651+
},
652+
},
653+
}
654+
testParentBeaconRoot := phase0.Root(parentHash)
655+
expectedAttrs := payloadAttributesHelper{
656+
slot: testSlot,
657+
parentHash: testParentHash,
658+
withdrawalsRoot: phase0.Root(withdrawalsRoot),
659+
parentBeaconRoot: &testParentBeaconRoot,
660+
payloadAttributes: beaconclient.PayloadAttributes{
661+
PrevRandao: testPrevRandao,
662+
Withdrawals: []*capella.Withdrawal{
663+
{
664+
Index: 989694,
665+
},
666+
},
667+
ParentBeaconBlockRoot: testParentHash,
668+
},
669+
}
670+
backend.relay.processPayloadAttributes(attrsEvent)
671+
backend.relay.payloadAttributesLock.RLock()
672+
attrs := backend.relay.payloadAttributes[getPayloadAttributesKey(testParentHash, testSlot)]
673+
backend.relay.payloadAttributesLock.RUnlock()
674+
require.Equal(t, expectedAttrs, attrs)
675+
676+
attrsEvent.Data.ProposalSlot = testSlot + 1
677+
backend.relay.processPayloadAttributes(attrsEvent)
678+
backend.relay.payloadAttributesLock.RLock()
679+
attrs = backend.relay.payloadAttributes[getPayloadAttributesKey(testParentHash, testSlot+1)]
680+
backend.relay.payloadAttributesLock.RUnlock()
681+
expectedAttrs.slot = testSlot + 1
682+
require.Equal(t, expectedAttrs, attrs)
683+
})
684+
685+
t.Run("Skips payload attribute update with same parent hash and slot", func(t *testing.T) {
686+
_, _, backend := startTestBackend(t)
687+
attrsEvent := beaconclient.PayloadAttributesEvent{
688+
Version: common.ForkVersionStringDeneb,
689+
Data: beaconclient.PayloadAttributesEventData{
690+
ProposalSlot: testSlot,
691+
ParentBlockHash: testParentHash,
692+
PayloadAttributes: beaconclient.PayloadAttributes{
693+
PrevRandao: testPrevRandao,
694+
Withdrawals: []*capella.Withdrawal{
695+
{
696+
Index: 989694,
697+
},
698+
},
699+
ParentBeaconBlockRoot: testParentHash,
700+
},
701+
},
702+
}
703+
testParentBeaconRoot := phase0.Root(parentHash)
704+
expectedAttrs := payloadAttributesHelper{
705+
slot: testSlot,
706+
parentHash: testParentHash,
707+
withdrawalsRoot: phase0.Root(withdrawalsRoot),
708+
parentBeaconRoot: &testParentBeaconRoot,
709+
payloadAttributes: beaconclient.PayloadAttributes{
710+
PrevRandao: testPrevRandao,
711+
Withdrawals: []*capella.Withdrawal{
712+
{
713+
Index: 989694,
714+
},
715+
},
716+
ParentBeaconBlockRoot: testParentHash,
717+
},
718+
}
719+
backend.relay.processPayloadAttributes(attrsEvent)
720+
backend.relay.payloadAttributesLock.RLock()
721+
attrs := backend.relay.payloadAttributes[getPayloadAttributesKey(testParentHash, testSlot)]
722+
backend.relay.payloadAttributesLock.RUnlock()
723+
require.Equal(t, expectedAttrs, attrs)
724+
725+
attrsEvent.Data.PayloadAttributes.ParentBeaconBlockRoot = testWithdrawalsRoot
726+
backend.relay.processPayloadAttributes(attrsEvent)
727+
backend.relay.payloadAttributesLock.RLock()
728+
attrs = backend.relay.payloadAttributes[getPayloadAttributesKey(testParentHash, testSlot)]
729+
backend.relay.payloadAttributesLock.RUnlock()
730+
require.Equal(t, expectedAttrs, attrs)
731+
})
732+
}
733+
630734
func TestCheckSubmissionPayloadAttrs(t *testing.T) {
631735
withdrawalsRoot, err := utils.HexToHash(testWithdrawalsRoot)
632736
require.NoError(t, err)
@@ -775,7 +879,7 @@ func TestCheckSubmissionPayloadAttrs(t *testing.T) {
775879
t.Run(tc.description, func(t *testing.T) {
776880
_, _, backend := startTestBackend(t)
777881
backend.relay.payloadAttributesLock.RLock()
778-
backend.relay.payloadAttributes[testParentHash] = tc.attrs
882+
backend.relay.payloadAttributes[getPayloadAttributesKey(testParentHash, testSlot)] = tc.attrs
779883
backend.relay.payloadAttributesLock.RUnlock()
780884

781885
w := httptest.NewRecorder()

services/api/utils.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,7 @@ func verifyBlockSignature(block *common.VersionedSignedBlindedBeaconBlock, domai
146146

147147
return bls.VerifySignatureBytes(msg[:], sig[:], pubKey[:])
148148
}
149+
150+
func getPayloadAttributesKey(parentHash string, slot uint64) string {
151+
return fmt.Sprintf("%s-%d", parentHash, slot)
152+
}

0 commit comments

Comments
 (0)