-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore(builder): store decoded constraints in the cache #68
Changes from 6 commits
ce1416f
432f65a
f7419af
bf17351
ed5a5ed
6ebead8
dd6c2fb
a1f6b75
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -97,8 +97,8 @@ type Builder struct { | |
builderResubmitInterval time.Duration | ||
discardRevertibleTxOnErr bool | ||
|
||
// constraintsCache is a map from slot to the constraints made by proposers | ||
constraintsCache *shardmap.FIFOMap[uint64, common.SignedConstraintsList] | ||
// constraintsCache is a map from slot to the decoded constraints made by proposers | ||
constraintsCache *shardmap.FIFOMap[uint64, types.HashToConstraintDecoded] | ||
|
||
limiter *rate.Limiter | ||
submissionOffsetFromEndOfSlot time.Duration | ||
|
@@ -196,7 +196,7 @@ func NewBuilder(args BuilderArgs) (*Builder, error) { | |
discardRevertibleTxOnErr: args.discardRevertibleTxOnErr, | ||
submissionOffsetFromEndOfSlot: args.submissionOffsetFromEndOfSlot, | ||
|
||
constraintsCache: shardmap.NewFIFOMap[uint64, common.SignedConstraintsList](64, 16, shardmap.HashUint64), | ||
constraintsCache: shardmap.NewFIFOMap[uint64, types.HashToConstraintDecoded](64, 16, shardmap.HashUint64), | ||
|
||
limiter: args.limiter, | ||
slotCtx: slotCtx, | ||
|
@@ -343,9 +343,11 @@ func (b *Builder) subscribeToRelayForConstraints(relayBaseEndpoint, authHeader s | |
} | ||
log.Error("Error reading from response body: %v", err) | ||
} | ||
|
||
if !strings.HasPrefix(line, "data: ") { | ||
continue | ||
} | ||
|
||
data := strings.TrimPrefix(line, "data: ") | ||
|
||
// We assume the data is the JSON representation of the constraints | ||
|
@@ -355,49 +357,50 @@ func (b *Builder) subscribeToRelayForConstraints(relayBaseEndpoint, authHeader s | |
log.Warn(fmt.Sprintf("Failed to unmarshal constraints: %v", err)) | ||
continue | ||
} | ||
|
||
if len(constraintsSigned) == 0 { | ||
log.Warn("Received 0 length list of constraints") | ||
continue | ||
} | ||
|
||
OUTER: | ||
for _, constraint := range constraintsSigned { | ||
decodedConstraints, err := DecodeConstraint(constraint) | ||
if err != nil { | ||
log.Error("Failed to decode transaction RLP: ", err) | ||
continue | ||
} | ||
Comment on lines
+367
to
+371
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Decode the constraints from the function in utils |
||
|
||
// For every constraint, we need to check if it has already been seen for the associated slot | ||
slotConstraints, _ := b.constraintsCache.Get(constraint.Message.Slot) | ||
if len(slotConstraints) == 0 { | ||
// New constraint for this slot, add it in the map and continue with the next constraint | ||
b.constraintsCache.Put(constraint.Message.Slot, common.SignedConstraintsList{constraint}) | ||
b.constraintsCache.Put(constraint.Message.Slot, decodedConstraints) | ||
continue | ||
} | ||
for _, slotConstraint := range slotConstraints { | ||
if slotConstraint.Signature == constraint.Signature { | ||
// The constraint has already been seen, we can continue with the next one | ||
continue OUTER | ||
|
||
// Temporary map to keep track of the constraints that are already in the slot | ||
seenTx := make(map[common.Hash]bool) | ||
for hash := range slotConstraints { | ||
seenTx[hash] = true | ||
} | ||
Comment on lines
+381
to
+385
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think you need this additional hashmap, you can just check if |
||
|
||
for hash := range decodedConstraints { | ||
if !seenTx[hash] { | ||
// The constraint is new, we will add this to the slot constraints | ||
slotConstraints[hash] = decodedConstraints[hash] | ||
Comment on lines
+387
to
+390
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here we iterate over the decoded constraints and see if any of the constraints present in it is different and if so we add it to the |
||
} | ||
} | ||
// The constraint is new, we need to append it to the current list | ||
b.constraintsCache.Put(constraint.Message.Slot, append(slotConstraints, constraint)) | ||
|
||
// Update the slot constraints | ||
b.constraintsCache.Put(constraint.Message.Slot, slotConstraints) | ||
Comment on lines
+394
to
+395
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We put the updated slot constraints in the cache. If there are no changes to it, we put the same back |
||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (b *Builder) GetConstraintsForSlot(slot uint64) types.HashToConstraintDecoded { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can just remove this function now There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, removed it! |
||
constraintsDecoded := make(types.HashToConstraintDecoded) | ||
constraintsSigned, _ := b.constraintsCache.Get(slot) | ||
|
||
for _, constraintSigned := range constraintsSigned { | ||
constraints := constraintSigned.Message.Constraints | ||
for _, constraint := range constraints { | ||
decoded := new(types.Transaction) | ||
if err := decoded.UnmarshalBinary(constraint.Tx); err != nil { | ||
log.Error("Failed to decode preconfirmation transaction RLP: ", err) | ||
continue | ||
} | ||
constraintsDecoded[decoded.Hash()] = &types.ConstraintDecoded{Index: constraint.Index, Tx: decoded} | ||
} | ||
} | ||
constraintsDecoded, _ := b.constraintsCache.Get(slot) | ||
return constraintsDecoded | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -9,10 +9,25 @@ import ( | |||||
"fmt" | ||||||
"io" | ||||||
"net/http" | ||||||
|
||||||
"github.com/ethereum/go-ethereum/common" | ||||||
"github.com/ethereum/go-ethereum/core/types" | ||||||
) | ||||||
|
||||||
var errHTTPErrorResponse = errors.New("HTTP error response") | ||||||
|
||||||
func DecodeConstraint(constraint *common.SignedConstraints) (types.HashToConstraintDecoded, error) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
decodedConstraints := make(types.HashToConstraintDecoded) | ||||||
for _, tx := range constraint.Message.Constraints { | ||||||
decoded := new(types.Transaction) | ||||||
if err := decoded.UnmarshalBinary(tx.Tx); err != nil { | ||||||
return nil, err | ||||||
} | ||||||
decodedConstraints[decoded.Hash()] = &types.ConstraintDecoded{Index: tx.Index, Tx: decoded} | ||||||
} | ||||||
return decodedConstraints, nil | ||||||
} | ||||||
|
||||||
// SendSSZRequest is a request to send SSZ data to a remote relay. | ||||||
func SendSSZRequest(ctx context.Context, client http.Client, method, url string, payload []byte, useGzip bool) (code int, err error) { | ||||||
var req *http.Request | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.