Skip to content

Commit

Permalink
Merge pull request #440 from GrKamil/dev
Browse files Browse the repository at this point in the history
fix waitlist
  • Loading branch information
danil-lashin authored Oct 13, 2020
2 parents 4502014 + 0b86bd1 commit d4bb603
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
2 changes: 1 addition & 1 deletion core/state/waitlist/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ func (m *Model) AddToList(candidateId uint32, coin types.CoinID, value *big.Int)
m.List = append(m.List, Item{
CandidateId: candidateId,
Coin: coin,
Value: value,
Value: new(big.Int).Set(value),
})
}
2 changes: 1 addition & 1 deletion core/state/waitlist/waitlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func (wl *WaitList) Delete(address types.Address, pubkey types.Pubkey, coin type
value := big.NewInt(0)
items := make([]Item, 0, len(w.List)-1)
for _, item := range w.List {
if item.CandidateId != candidate.ID && item.Coin != coin {
if item.CandidateId != candidate.ID || item.Coin != coin {
items = append(items, item)
} else {
value.Add(value, item.Value)
Expand Down
52 changes: 52 additions & 0 deletions core/state/waitlist/waitlist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,55 @@ func TestWaitListToGetByAddressAndPubKey(t *testing.T) {
t.Fatal("Incorrect amount of items in waitlist")
}
}

func TestWaitListToPartialDelete(t *testing.T) {
b := bus.NewBus()
b.SetChecker(checker.NewChecker(b))
mutableTree, _ := tree.NewMutableTree(0, db.NewMemDB(), 1024)

wl, err := NewWaitList(b, mutableTree)
if err != nil {
t.Fatal(err)
}

candidatesState, err := candidates.NewCandidates(b, mutableTree)
if err != nil {
t.Fatal(err)
}

addr, pubkey, coin, val := types.Address{0}, types.Pubkey{0}, types.GetBaseCoinID(), big.NewInt(1e18)
candidatesState.Create(addr, addr, addr, pubkey, 10)

wl.AddWaitList(addr, pubkey, coin, val)
wl.AddWaitList(addr, pubkey, 1, val)
wl.AddWaitList(addr, pubkey, 2, val)
if err := wl.Commit(); err != nil {
t.Fatal(err)
}

_, _, err = mutableTree.SaveVersion()
if err != nil {
t.Fatal(err)
}

wl.Delete(addr, pubkey, 0)
wl.Delete(addr, pubkey, 1)
wl.AddWaitList(addr, pubkey, 1, big.NewInt(1e17))
_, _, err = mutableTree.SaveVersion()
if err != nil {
t.Fatal(err)
}

items := wl.GetByAddressAndPubKey(addr, pubkey)
if len(items) != 2 {
t.Fatal("Incorrect amount of items in waitlist")
}

if items[1].Value.Cmp(big.NewInt(1e17)) != 0 || items[1].Coin != 1 {
t.Fatal("Invalid waitlist data")
}

if items[0].Value.Cmp(val) != 0 || items[0].Coin != 2 {
t.Fatal("Invalid waitlist data")
}
}

0 comments on commit d4bb603

Please sign in to comment.