-
Notifications
You must be signed in to change notification settings - Fork 5
refactor(interpreter): Simplify fundsQueue & improve naming #100
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
Open
Azorlogh
wants to merge
2
commits into
main
Choose a base branch
from
refactor/interpreter-send-readability
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| package interpreter | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "math/big" | ||
| "slices" | ||
| ) | ||
|
|
||
| type Sender struct { | ||
| Name string | ||
| Amount *big.Int | ||
| Color string | ||
| } | ||
|
|
||
| type fundsQueue struct { | ||
| asset string | ||
| senders []Sender | ||
| } | ||
|
|
||
| // Create a fundsQueue from a slice of senders. | ||
| func newFundsQueue(senders []Sender) fundsQueue { | ||
| queue := fundsQueue{ | ||
| senders: []Sender{}, | ||
| } | ||
| queue.Push(senders...) | ||
| return queue | ||
| } | ||
|
|
||
| // Push senders to this fundsQueue | ||
| func (s *fundsQueue) Push(senders ...Sender) { | ||
| for _, sender := range senders { | ||
| s.PushOne(sender) | ||
| } | ||
| } | ||
|
|
||
| // Push a single sender to this queue | ||
| func (s *fundsQueue) PushOne(sender Sender) { | ||
| if sender.Amount.Cmp(big.NewInt(0)) == 0 { | ||
| return | ||
| } | ||
| if len(s.senders) == 0 { | ||
| s.senders = []Sender{sender} | ||
| return | ||
| } | ||
| last := s.senders[len(s.senders)-1] | ||
| if last.Name == sender.Name && last.Color == sender.Color { | ||
| last.Amount.Add(last.Amount, sender.Amount) | ||
| } else { | ||
| s.senders = append(s.senders, sender) | ||
| } | ||
| } | ||
|
|
||
| // Pull everything from this queue | ||
| func (s *fundsQueue) PullAll() []Sender { | ||
| senders := s.senders | ||
| s.senders = []Sender{} | ||
| return senders | ||
| } | ||
|
|
||
| // Pull at most maxAmount from this queue, with any color | ||
| func (s *fundsQueue) PullAnything(maxAmount *big.Int) []Sender { | ||
| return s.Pull(maxAmount, nil) | ||
| } | ||
|
|
||
| func (s *fundsQueue) PullColored(maxAmount *big.Int, color string) []Sender { | ||
| return s.Pull(maxAmount, &color) | ||
| } | ||
| func (s *fundsQueue) PullUncolored(maxAmount *big.Int) []Sender { | ||
| return s.PullColored(maxAmount, "") | ||
| } | ||
|
|
||
| // Pull at most maxAmount from this queue, with the given color | ||
| func (s *fundsQueue) Pull(maxAmount *big.Int, color *string) []Sender { | ||
| // clone so that we can manipulate this arg | ||
| maxAmount = new(big.Int).Set(maxAmount) | ||
|
|
||
| // TODO preallocate for perfs | ||
| out := newFundsQueue([]Sender{}) | ||
| offset := 0 | ||
|
|
||
| for maxAmount.Sign() > 0 && len(s.senders) > offset { | ||
|
|
||
| frontSender := s.senders[offset] | ||
|
|
||
| if color != nil && frontSender.Color != *color { | ||
| offset += 1 | ||
| continue | ||
| } | ||
|
|
||
| switch frontSender.Amount.Cmp(maxAmount) { | ||
| case -1: // not enough | ||
| maxAmount.Sub(maxAmount, frontSender.Amount) | ||
| out.Push(frontSender) | ||
| if offset == 0 { | ||
| s.senders = s.senders[1:] | ||
| } else { | ||
| s.senders = slices.Delete(s.senders, offset, offset+1) | ||
| } | ||
| case 1: // more than enough | ||
| out.Push(Sender{ | ||
| Name: frontSender.Name, | ||
| Amount: maxAmount, | ||
| Color: frontSender.Color, | ||
| }) | ||
| s.senders[offset].Amount.Sub(s.senders[offset].Amount, maxAmount) | ||
| return out.senders | ||
| case 0: // exactly enough | ||
| out.Push(s.senders[offset]) | ||
| if offset == 0 { | ||
| s.senders = s.senders[1:] | ||
| } else { | ||
| s.senders = slices.Delete(s.senders, offset, offset+1) | ||
| } | ||
| return out.senders | ||
| } | ||
| } | ||
|
|
||
| return out.senders | ||
| } | ||
|
|
||
| // Clone the queue so that you can safely mutate one without mutating the other | ||
| func (s fundsQueue) Clone() fundsQueue { | ||
| return fundsQueue{ | ||
| senders: slices.Clone(s.senders), | ||
| asset: s.asset, | ||
| } | ||
| } | ||
|
|
||
| func (s fundsQueue) String() string { | ||
| out := ">" | ||
| for i, sender := range s.senders { | ||
| if sender.Color == "" { | ||
| out += fmt.Sprintf("%v from %v", sender.Amount, sender.Name) | ||
| } else { | ||
| out += fmt.Sprintf("%v from %v\\%v", sender.Amount, sender.Name, sender.Color) | ||
| } | ||
| if i != len(s.senders)-1 { | ||
| out += ", " | ||
| } | ||
| } | ||
| out += ">" | ||
| return out | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Deep-clone sender amounts in
Clone()Clone()only copies the slice, so every clonedSender.Amountstill points to the same*big.Int. WhencloneState()(interpreter.go Line 604) restoresfundsQueue, subsequent mutations done after the clone leak back into the “restored” queue, breaking backtracking (e.g.oneofsources will see already-depleted amounts). Please deep-copy each amount.func (s fundsQueue) Clone() fundsQueue { - return fundsQueue{ - senders: slices.Clone(s.senders), - asset: s.asset, - } + cloned := make([]Sender, len(s.senders)) + for i, sender := range s.senders { + cloned[i] = Sender{ + Name: sender.Name, + Color: sender.Color, + Amount: new(big.Int).Set(sender.Amount), + } + } + return fundsQueue{ + senders: cloned, + asset: s.asset, + } }📝 Committable suggestion
🤖 Prompt for AI Agents