Skip to content

Commit

Permalink
Merge pull request #409 from klim0v/v1.2
Browse files Browse the repository at this point in the history
fixes
  • Loading branch information
danil-lashin authored Sep 28, 2020
2 parents 260ce96 + 4529cd2 commit 309fe9e
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 75 deletions.
224 changes: 155 additions & 69 deletions core/state/candidates/candidate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,74 +516,6 @@ func TestCandidates_GetTotalStake_fromModelAndFromDB(t *testing.T) {
}
}

func TestCandidates_GetTotalStake_forCustomCoins(t *testing.T) {

mutableTree, _ := tree.NewMutableTree(0, db.NewMemDB(), 1024)
b := bus.NewBus()
wl, err := waitlist.NewWaitList(b, mutableTree)
if err != nil {
t.Fatal(err)
}
b.SetEvents(eventsdb.NewEventsStore(db.NewMemDB()))
b.SetWaitList(waitlist.NewBus(wl))
accs, err := accounts.NewAccounts(b, mutableTree)
if err != nil {
t.Fatal(err)
}
b.SetAccounts(accounts.NewBus(accs))
b.SetChecker(checker.NewChecker(b))
candidates, err := NewCandidates(b, mutableTree)
if err != nil {
t.Fatal(err)
}

coinsState, err := coins.NewCoins(b, mutableTree)
if err != nil {
t.Fatal(err)
}

candidates.Create([20]byte{1}, [20]byte{2}, [20]byte{3}, [32]byte{4}, 10)
coinsState.Create(1,
types.StrToCoinSymbol("AAA"),
"AAACOIN",
helpers.BipToPip(big.NewInt(10)),
10,
helpers.BipToPip(big.NewInt(10000)),
big.NewInt(0).Exp(big.NewInt(10), big.NewInt(10+18), nil),
nil)

err = coinsState.Commit()
if err != nil {
t.Fatal(err)
}

symbol := coinsState.GetCoinBySymbol(types.StrToCoinSymbol("AAA"), 0)
if symbol == nil {
t.Fatal("coin not found")
}

var stakes []types.Stake
for i := 0; i < 50; i++ {
value := strconv.Itoa(i + 2000)
stakes = append(stakes, types.Stake{
Owner: types.StringToAddress(strconv.Itoa(i)),
Coin: uint64(symbol.ID()),
Value: value,
BipValue: "0",
})
}
candidates.SetStakes([32]byte{4}, stakes, nil)

candidates.RecalculateStakes(0)

candidates.LoadCandidates()
totalStake := candidates.GetTotalStake([32]byte{4})
totalStakeString := totalStake.String()
if totalStakeString != "9802420350703877401368" {
t.Fatalf("total stake %s", totalStakeString)
}
}

func TestCandidates_Export(t *testing.T) {
mutableTree, _ := tree.NewMutableTree(0, db.NewMemDB(), 1024)
candidates, err := NewCandidates(bus.NewBus(), mutableTree)
Expand Down Expand Up @@ -953,7 +885,6 @@ func TestCandidates_IsNewCandidateStakeSufficient(t *testing.T) {
t.Fatal(err)
}

// todo: validators.GetCandidatesCountForBlock(1)
if !candidates.IsNewCandidateStakeSufficient(0, big.NewInt(1000), 1) {
t.Log("is not new candidate stake sufficient")
}
Expand Down Expand Up @@ -1147,3 +1078,158 @@ func TestCandidate_GetFilteredUpdates(t *testing.T) {
t.Fatal("error merge updates")
}
}

func TestCandidates_CalculateBipValue_RecalculateStakes_GetTotalStake(t *testing.T) {
mutableTree, _ := tree.NewMutableTree(0, db.NewMemDB(), 1024)
b := bus.NewBus()
b.SetChecker(checker.NewChecker(b))
busCoins, err := coins.NewCoins(b, mutableTree)
if err != nil {
t.Fatal(err)
}
b.SetCoins(coins.NewBus(busCoins))
candidates, err := NewCandidates(b, mutableTree)
if err != nil {
t.Fatal(err)
}

coinsState, err := coins.NewCoins(b, mutableTree)
if err != nil {
t.Fatal(err)
}

candidates.Create([20]byte{1}, [20]byte{1}, [20]byte{1}, [32]byte{1}, 1)
candidates.SetStakes([32]byte{1}, []types.Stake{
{
Owner: types.Address{1},
Coin: 52,
Value: "27331500301898443574821601",
BipValue: "0",
},
{
Owner: types.Address{1},
Coin: 52,
Value: "26788352158593847436109305",
BipValue: "0",
},
{
Owner: types.Address{1},
Coin: 52,
Value: "23056159980819190092008573",
BipValue: "0",
},
{
Owner: types.Address{1},
Coin: 52,
Value: "11588709101209768903338862",
BipValue: "0",
},
{
Owner: types.Address{1},
Coin: 52,
Value: "10699458018244407488345007",
BipValue: "0",
},
{
Owner: types.Address{1},
Coin: 52,
Value: "10178615801247206484340203",
BipValue: "0",
},
{
Owner: types.Address{1},
Coin: 52,
Value: "9695040709408605598614475",
BipValue: "0",
},
{
Owner: types.Address{1},
Coin: 52,
Value: "9311613733840163086812673",
BipValue: "0",
},
{
Owner: types.Address{1},
Coin: 52,
Value: "8035237015568850680085714",
BipValue: "0",
},
{
Owner: types.Address{1},
Coin: 52,
Value: "7751636678470495902806639",
BipValue: "0",
},
{
Owner: types.Address{1},
Coin: 52,
Value: "7729118857616059555215844",
BipValue: "0",
},
{
Owner: types.Address{1},
Coin: 52,
Value: "7246351659896715230790480",
BipValue: "0",
},
{
Owner: types.Address{1},
Coin: 52,
Value: "5634000000000000000000000",
BipValue: "0",
},
{
Owner: types.Address{1},
Coin: 52,
Value: "5111293424492290525817483",
BipValue: "0",
},
{
Owner: types.Address{1},
Coin: 52,
Value: "4636302767358508700208179",
BipValue: "0",
},
{
Owner: types.Address{1},
Coin: 52,
Value: "4375153667350433703873779",
BipValue: "0",
},
{
Owner: types.Address{1},
Coin: 52,
Value: "6468592759016388938414535",
BipValue: "0",
},
}, nil)
volume, _ := big.NewInt(0).SetString("235304453408778922901904166", 10)
reserve, _ := big.NewInt(0).SetString("3417127836274022127064945", 10)
maxSupply, _ := big.NewInt(0).SetString("1000000000000000000000000000000000", 10)
coinsState.Create(52,
types.StrToCoinSymbol("ONLY1"),
"ONLY1",
volume,
70,
reserve,
maxSupply,
nil)

amount, _ := big.NewInt(0).SetString("407000000000000000000000", 10)
cache := newCoinsCache()

bipValue := candidates.calculateBipValue(52, amount, false, true, cache)
if bipValue.Sign() < 0 {
t.Fatalf("%s", bipValue.String())
}
bipValue = candidates.calculateBipValue(52, amount, false, true, cache)
if bipValue.Sign() < 0 {
t.Fatalf("%s", bipValue.String())
}

candidates.RecalculateStakes(0)
totalStake := candidates.GetTotalStake([32]byte{1})
if totalStake.String() != "2435386873327199834002556" {
t.Fatalf("total stake %s", totalStake.String())
}
}
1 change: 0 additions & 1 deletion core/state/candidates/candidates.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,6 @@ func (c *Candidates) stakeKick(owner types.Address, value *big.Int, coin types.C
Coin: coin,
ValidatorPubKey: pubKey,
})
c.bus.Accounts().AddBalance(owner, coin, value)
c.bus.Checker().AddCoin(coin, big.NewInt(0).Neg(value))
}

Expand Down
6 changes: 3 additions & 3 deletions core/state/candidates/coins_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (c *coinsCache) Exists(id types.CoinID) bool {
}

func (c *coinsCache) Get(id types.CoinID) (saleReturn *big.Int, totalAmount *big.Int) {
return big.NewInt(0).Set(c.list[id].totalAmount), big.NewInt(0).Set(c.list[id].totalAmount)
return big.NewInt(0).Set(c.list[id].saleReturn), big.NewInt(0).Set(c.list[id].totalAmount)
}

func (c *coinsCache) Set(id types.CoinID, saleReturn *big.Int, totalAmount *big.Int) {
Expand All @@ -41,6 +41,6 @@ func (c *coinsCache) Set(id types.CoinID, saleReturn *big.Int, totalAmount *big.
c.list[id] = &coinsCacheItem{}
}

c.list[id].totalAmount = totalAmount
c.list[id].saleReturn = saleReturn
c.list[id].totalAmount = big.NewInt(0).Set(totalAmount)
c.list[id].saleReturn = big.NewInt(0).Set(saleReturn)
}
3 changes: 2 additions & 1 deletion core/state/waitlist/waitlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ func (wl *WaitList) AddWaitList(address types.Address, pubkey types.Pubkey, coin
w.AddToList(candidate.ID, coin, value)
wl.setToMap(address, w)
w.markDirty(address)
wl.bus.Checker().AddCoin(coin, value)
}

func (wl *WaitList) Delete(address types.Address, pubkey types.Pubkey, coin types.CoinID) {
Expand All @@ -173,7 +174,7 @@ func (wl *WaitList) Delete(address types.Address, pubkey types.Pubkey, coin type
w.List = items
wl.markDirty(address)
wl.setToMap(address, w)
wl.bus.Checker().AddCoinVolume(coin, value)
wl.bus.Checker().AddCoin(coin, big.NewInt(0).Neg(value))
}

func (wl *WaitList) getOrNew(address types.Address) *Model {
Expand Down
2 changes: 2 additions & 0 deletions core/state/waitlist/waitlist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package waitlist
import (
"github.com/MinterTeam/minter-go-node/core/state/bus"
"github.com/MinterTeam/minter-go-node/core/state/candidates"
"github.com/MinterTeam/minter-go-node/core/state/checker"
"github.com/MinterTeam/minter-go-node/core/types"
"github.com/MinterTeam/minter-go-node/tree"
db "github.com/tendermint/tm-db"
Expand All @@ -12,6 +13,7 @@ import (

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

wl, err := NewWaitList(b, mutableTree)
Expand Down
3 changes: 2 additions & 1 deletion core/transaction/delegate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -501,8 +501,9 @@ func TestDelegateData_addFromWaitlist(t *testing.T) {
privateKey, _ := crypto.GenerateKey()
addr := crypto.PubkeyToAddress(privateKey.PublicKey)
cState.Waitlist.AddWaitList(addr, pubkey, 0, big.NewInt(100))
cState.Checker.AddCoin(0, big.NewInt(0).Neg(big.NewInt(100)))
cState.Accounts.AddBalance(addr, 0, helpers.BipToPip(big.NewInt(1000000)))
cState.Checker.AddCoinVolume(0, helpers.BipToPip(big.NewInt(1000000)))
cState.Checker.AddCoin(0, helpers.BipToPip(big.NewInt(0).Neg(big.NewInt(1000000))))

value := big.NewInt(10000000000)
data := DelegateData{
Expand Down

0 comments on commit 309fe9e

Please sign in to comment.