🚀 feat: Implement TE boost order with async EI data refresh#1914
🚀 feat: Implement TE boost order with async EI data refresh#1914
Conversation
There was a problem hiding this comment.
Pull request overview
This PR implements Truth Egg (TE) count-based boost ordering with asynchronous refresh of user data from the Egg Inc API. The implementation extracts the TE count calculation logic into a separate function and adds logic to refresh TE data for users with zero TE count when the TE boost order is selected.
Key Changes:
- Extracted TE count calculation logic into a new
updateContractFarmerTEfunction with async API calls - Added automatic refresh of TE data when switching to TE boost order for users with
TECount == 0 - Replaced inline TE counting during farmer addition with a call to the new function
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 6 comments.
| File | Description |
|---|---|
| src/boost/boost.go | Extracted TE count logic into updateContractFarmerTE function with async goroutine for API calls, and updated AddFarmerToContract to call the new function |
| src/boost/contract.go | Added loop to refresh TE data for boosters with zero TE count when TE boost order is selected |
| go func(eggIncID, userID string, b *Booster) { | ||
| backup, _ := ei.GetFirstContactFromAPI(s, eggIncID, userID, true) | ||
| virtue := backup.GetVirtue() | ||
|
|
||
| var allEov uint32 | ||
|
|
||
| // virtueEggs := []string{"CURIOSITY", "INTEGRITY", "HUMILITY", "RESILIENCE", "KINDNESS"} | ||
| for i := range 5 { | ||
| eov := virtue.GetEovEarned()[i] | ||
| delivered := virtue.GetEggsDelivered()[i] | ||
|
|
||
| eovEarned := countTETiersPassed(delivered) | ||
| eovPending := pendingTruthEggs(delivered, eov) | ||
|
|
||
| allEov += max(eovEarned-eovPending, 0) | ||
| } | ||
|
|
||
| b.TECount = int(allEov) | ||
| refreshBoostListMessage(s, contract) | ||
|
|
||
| }(eggIncID, userID, b) |
There was a problem hiding this comment.
The goroutine modifies the b.TECount field (line 996) and calls refreshBoostListMessage without any synchronization. This creates a race condition since:
- The
Boosterstruct (b) is accessed concurrently by the goroutine and potentially by other code paths - The
contractstruct is accessed without locking its mutex, even though the Contract type has amutex sync.Mutexfield for thread safety (line 325 in boost.go) - The
refreshBoostListMessagefunction accesses contract fields (Location, etc.) without synchronization
Consider using the contract's mutex to protect the write to b.TECount and the call to refreshBoostListMessage:
go func(eggIncID, userID string, b *Booster, contract *Contract) {
backup, _ := ei.GetFirstContactFromAPI(s, eggIncID, userID, true)
virtue := backup.GetVirtue()
var allEov uint32
for i := range 5 {
eov := virtue.GetEovEarned()[i]
delivered := virtue.GetEggsDelivered()[i]
eovEarned := countTETiersPassed(delivered)
eovPending := pendingTruthEggs(delivered, eov)
allEov += max(eovEarned-eovPending, 0)
}
contract.mutex.Lock()
b.TECount = int(allEov)
contract.mutex.Unlock()
refreshBoostListMessage(s, contract)
}(eggIncID, userID, b, contract)There was a problem hiding this comment.
@copilot open a new pull request to apply changes based on this feedback
| for i := range 5 { | ||
| eov := virtue.GetEovEarned()[i] | ||
| delivered := virtue.GetEggsDelivered()[i] | ||
|
|
||
| eovEarned := countTETiersPassed(delivered) | ||
| eovPending := pendingTruthEggs(delivered, eov) | ||
|
|
||
| allEov += max(eovEarned-eovPending, 0) | ||
| } |
There was a problem hiding this comment.
The code accesses array indices without bounds checking. If virtue.GetEovEarned() or virtue.GetEggsDelivered() returns a slice with fewer than 5 elements, this will cause an index out of bounds panic.
Add length validation:
eovEarned := virtue.GetEovEarned()
eggsDelivered := virtue.GetEggsDelivered()
if len(eovEarned) < 5 || len(eggsDelivered) < 5 {
log.Printf("Insufficient virtue data for user %s", userID)
return
}
for i := range 5 {
eov := eovEarned[i]
delivered := eggsDelivered[i]
// ... rest of the loop
}|
@mkmccarty I've opened a new pull request, #1915, to work on those changes. Once the pull request is ready, I'll request review from you. |
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
784c96b to
d3ab3ec
Compare
No description provided.