Skip to content

Commit

Permalink
Merge branch 'sync/dev' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
grkamil committed Oct 14, 2020
2 parents 98a029e + 4b70a57 commit 851dfcd
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 11 deletions.
50 changes: 46 additions & 4 deletions core/minter/minter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,13 @@ func TestBlockchain_Height(t *testing.T) {
if block.Data.(types2.EventDataNewBlock).Block.Height != int64(blockchain.Height()) {
t.Fatal("invalid blockchain height")
}

blockchain.lock.RLock()
defer blockchain.lock.RUnlock()
exportedState := blockchain.CurrentState().Export(blockchain.Height() - 1)
if err := exportedState.Verify(); err != nil {
t.Fatal(err)
}
}

func TestBlockchain_SetStatisticData(t *testing.T) {
Expand All @@ -248,6 +255,13 @@ func TestBlockchain_SetStatisticData(t *testing.T) {
if block.Data.(types2.EventDataNewBlock).Block.Header.Time.Nanosecond() != blockchain.StatisticData().BlockEnd.LastBlockInfo.HeaderTimestamp.Nanosecond() {
t.Fatal("statistic last block and event event last block header time not equal")
}

blockchain.lock.RLock()
defer blockchain.lock.RUnlock()
exportedState := blockchain.CurrentState().Export(blockchain.Height() - 1)
if err := exportedState.Verify(); err != nil {
t.Fatal(err)
}
}

func TestBlockchain_IsApplicationHalted(t *testing.T) {
Expand Down Expand Up @@ -303,6 +317,12 @@ func TestBlockchain_IsApplicationHalted(t *testing.T) {
t.Fatalf("don't stop on block %d", height)
return
case <-time.After(2 * time.Second):
blockchain.lock.RLock()
defer blockchain.lock.RUnlock()
exportedState := blockchain.CurrentState().Export(blockchain.Height() - 1)
if err := exportedState.Verify(); err != nil {
t.Fatal(err)
}
return
}
}
Expand Down Expand Up @@ -378,6 +398,13 @@ func TestBlockchain_GetStateForHeightAndDeleteStateVersions(t *testing.T) {
if err == nil {
t.Fatalf("Failed: %s", "state not deleted")
}

blockchain.lock.RLock()
defer blockchain.lock.RUnlock()
exportedState := blockchain.CurrentState().Export(blockchain.Height() - 1)
if err := exportedState.Verify(); err != nil {
t.Fatal(err)
}
}

func TestBlockchain_SendTx(t *testing.T) {
Expand Down Expand Up @@ -436,6 +463,13 @@ func TestBlockchain_SendTx(t *testing.T) {
case <-time.After(10 * time.Second):
t.Fatalf("Timeout waiting for the tx to be committed")
}

blockchain.lock.RLock()
defer blockchain.lock.RUnlock()
exportedState := blockchain.CurrentState().Export(blockchain.Height() - 1)
if err := exportedState.Verify(); err != nil {
t.Fatal(err)
}
}

func TestBlockchain_FrozenFunds(t *testing.T) {
Expand Down Expand Up @@ -465,6 +499,11 @@ func TestBlockchain_FrozenFunds(t *testing.T) {
blockchain.lock.RLock()
defer blockchain.lock.RLock()

exportedState := blockchain.CurrentState().Export(blockchain.Height() - 1)
if err := exportedState.Verify(); err != nil {
t.Fatal(err)
}

events := blockchain.GetEventsDB().LoadEvents(uint32(targetHeight))

if len(events) == 0 {
Expand All @@ -480,10 +519,6 @@ func TestBlockchain_FrozenFunds(t *testing.T) {
t.Error("event validator pubkey invalid")
}

balance := blockchain.CurrentState().Accounts().GetBalance(developers.Address, 0)
if balance.String() != value.String() {
t.Fatal("balance is empty")
}
}

func TestBlockchain_RecalculateStakes_andRemoveValidator(t *testing.T) {
Expand Down Expand Up @@ -719,6 +754,13 @@ func TestBlockchain_RecalculateStakes_andRemoveValidator(t *testing.T) {
if candidate.Status == candidates.CandidateStatusOnline {
t.Fatal("candidate not Offline")
}

blockchain.lock.RLock()
defer blockchain.lock.RUnlock()
exportedState := blockchain.CurrentState().Export(blockchain.Height() - 1)
if err := exportedState.Verify(); err != nil {
t.Fatal(err)
}
}

func TestStopNetworkByHaltBlocks(t *testing.T) {
Expand Down
84 changes: 81 additions & 3 deletions core/state/candidates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"testing"
)

const height = 500000
const height = 1

func TestSimpleDelegate(t *testing.T) {
st := getState()
Expand All @@ -38,6 +38,11 @@ func TestSimpleDelegate(t *testing.T) {
if stake.BipValue.Cmp(amount) != 0 {
t.Errorf("Bip value of stake of address %s should be %s, got %s", address.String(), amount.String(), stake.BipValue.String())
}

err := checkState(st)
if err != nil {
t.Error(err)
}
}

func TestDelegate(t *testing.T) {
Expand Down Expand Up @@ -68,6 +73,11 @@ func TestDelegate(t *testing.T) {
if stake.BipValue.Cmp(totalAmount) != 0 {
t.Errorf("Bip value of stake of address %s should be %s, got %s", address.String(), amount.String(), stake.BipValue.String())
}

err := checkState(st)
if err != nil {
t.Error(err)
}
}

func TestCustomDelegate(t *testing.T) {
Expand All @@ -78,6 +88,7 @@ func TestCustomDelegate(t *testing.T) {

coinID := st.App.GetNextCoinID()
st.Coins.Create(coinID, types.StrToCoinSymbol("TEST"), "TEST COIN", volume, 10, reserve, volume, nil)
st.Accounts.AddBalance([20]byte{1}, 1, helpers.BipToPip(big.NewInt(1000000-500000)))
st.App.SetCoinsCount(coinID.Uint32())

address := types.Address{}
Expand All @@ -100,6 +111,11 @@ func TestCustomDelegate(t *testing.T) {
if stake.BipValue.Cmp(bipValue) != 0 {
t.Errorf("Bip value of stake of address %s should be %s, got %s", address.String(), bipValue.String(), stake.BipValue.String())
}

err := checkState(st)
if err != nil {
t.Error(err)
}
}

func TestComplexDelegate(t *testing.T) {
Expand Down Expand Up @@ -200,6 +216,11 @@ func TestComplexDelegate(t *testing.T) {
t.Fatalf("Stake of address %s found, but should not be", addr.String())
}
}

err := checkState(st)
if err != nil {
t.Error(err)
}
}

func TestStakeSufficiency(t *testing.T) {
Expand Down Expand Up @@ -249,6 +270,11 @@ func TestStakeSufficiency(t *testing.T) {
t.Fatalf("Stake of %s %s of address %s shold be sufficient", stake.String(), coin.String(), addr.String())
}
}

err := checkState(st)
if err != nil {
t.Error(err)
}
}

func TestDoubleSignPenalty(t *testing.T) {
Expand Down Expand Up @@ -300,6 +326,11 @@ func TestDoubleSignPenalty(t *testing.T) {
if !exists {
t.Fatalf("Frozen fund not found")
}

err := checkState(st)
if err != nil {
t.Error(err)
}
}

func TestAbsentPenalty(t *testing.T) {
Expand Down Expand Up @@ -330,6 +361,11 @@ func TestAbsentPenalty(t *testing.T) {
if stake.Cmp(newValue) != 0 {
t.Fatalf("Stake is not correct. Expected %s, got %s", newValue, stake.String())
}

err := checkState(st)
if err != nil {
t.Error(err)
}
}

func TestDoubleAbsentPenalty(t *testing.T) {
Expand All @@ -338,10 +374,10 @@ func TestDoubleAbsentPenalty(t *testing.T) {
pubkey := createTestCandidate(st)

coin := types.GetBaseCoinID()
amount := helpers.BipToPip(big.NewInt(1000))
amount := helpers.BipToPip(big.NewInt(10000))
var addr types.Address
binary.BigEndian.PutUint64(addr[:], 1)
st.Candidates.Delegate(addr, pubkey, coin, amount, big.NewInt(0))
st.Candidates.Delegate(addr, pubkey, coin, amount, amount)
st.Candidates.SetOnline(pubkey)

st.Candidates.RecalculateStakes(height)
Expand All @@ -366,6 +402,13 @@ func TestDoubleAbsentPenalty(t *testing.T) {
if stake.Cmp(newValue) != 0 {
t.Fatalf("Stake is not correct. Expected %s, got %s", newValue, stake.String())
}

st.Candidates.SetOnline(pubkey)
st.Validators.SetNewValidators(st.Candidates.GetNewCandidates(1))
err := checkState(st)
if err != nil {
t.Error(err)
}
}

func TestZeroStakePenalty(t *testing.T) {
Expand Down Expand Up @@ -398,6 +441,11 @@ func TestZeroStakePenalty(t *testing.T) {
if stake.Cmp(newValue) != 0 {
t.Fatalf("Stake is not correct. Expected %s, got %s", newValue, stake.String())
}

err := checkState(st)
if err != nil {
t.Error(err)
}
}

func TestDelegationAfterUnbond(t *testing.T) {
Expand Down Expand Up @@ -457,6 +505,11 @@ func TestDelegationAfterUnbond(t *testing.T) {
}
}

err := checkState(st)
if err != nil {
t.Error(err)
}

}

func TestStakeKick(t *testing.T) {
Expand Down Expand Up @@ -494,6 +547,11 @@ func TestStakeKick(t *testing.T) {
if wl.Value.Cmp(big.NewInt(1)) != 0 {
t.Fatalf("Waitlist is not correct")
}

err := checkState(st)
if err != nil {
t.Error(err)
}
}

func TestRecalculateStakes(t *testing.T) {
Expand All @@ -502,6 +560,7 @@ func TestRecalculateStakes(t *testing.T) {
st.Coins.Create(1, [10]byte{1}, "TestCoin", helpers.BipToPip(big.NewInt(100000)), 70, helpers.BipToPip(big.NewInt(10000)), nil, nil)
pubkey := createTestCandidate(st)

st.Accounts.AddBalance([20]byte{1}, 1, helpers.BipToPip(big.NewInt(100000-1000)))
amount := helpers.BipToPip(big.NewInt(1000))
st.Candidates.Delegate([20]byte{1}, pubkey, 1, amount, big.NewInt(0))

Expand All @@ -518,6 +577,11 @@ func TestRecalculateStakes(t *testing.T) {
if stake.BipValue.String() != "13894954943731374342" {
t.Errorf("stake bip value is %s", stake.BipValue.String())
}

err = checkState(st)
if err != nil {
t.Error(err)
}
}

func getState() *State {
Expand All @@ -530,11 +594,25 @@ func getState() *State {
return s
}

func checkState(cState *State) error {
if _, err := cState.Commit(); err != nil {
return err
}

exportedState := cState.Export(height)
if err := exportedState.Verify(); err != nil {
return err
}

return nil
}

func createTestCandidate(stateDB *State) types.Pubkey {
address := types.Address{}
pubkey := types.Pubkey{}
_, _ = rand.Read(pubkey[:])

stateDB.Validators.Create(pubkey, helpers.BipToPip(big.NewInt(1000)))
stateDB.Candidates.Create(address, address, address, pubkey, 10)

return pubkey
Expand Down
12 changes: 8 additions & 4 deletions core/state/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestStateExport(t *testing.T) {
coinTestID,
coinTest,
"TEST",
helpers.BipToPip(big.NewInt(1)),
helpers.BipToPip(big.NewInt(601)),
10,
helpers.BipToPip(big.NewInt(100)),
helpers.BipToPip(big.NewInt(100)),
Expand All @@ -41,7 +41,7 @@ func TestStateExport(t *testing.T) {
coinTest2ID,
coinTest2,
"TEST2",
helpers.BipToPip(big.NewInt(2)),
helpers.BipToPip(big.NewInt(1002)),
50,
helpers.BipToPip(big.NewInt(200)),
helpers.BipToPip(big.NewInt(200)),
Expand Down Expand Up @@ -107,6 +107,10 @@ func TestStateExport(t *testing.T) {

newState := state.Export(height)

if err := newState.Verify(); err != nil {
t.Error(err)
}

if newState.StartHeight != height {
t.Fatalf("Wrong new state start height. Expected %d, got %d", height, newState.StartHeight)
}
Expand All @@ -128,7 +132,7 @@ func TestStateExport(t *testing.T) {

if newStateCoin.Name != "TEST" ||
newStateCoin.Symbol != coinTest ||
newStateCoin.Volume != helpers.BipToPip(big.NewInt(1)).String() ||
newStateCoin.Volume != helpers.BipToPip(big.NewInt(601)).String() ||
newStateCoin.Reserve != helpers.BipToPip(big.NewInt(100)).String() ||
newStateCoin.MaxSupply != helpers.BipToPip(big.NewInt(100)).String() ||
newStateCoin.Crr != 10 {
Expand All @@ -137,7 +141,7 @@ func TestStateExport(t *testing.T) {

if newStateCoin1.Name != "TEST2" ||
newStateCoin1.Symbol != coinTest2 ||
newStateCoin1.Volume != helpers.BipToPip(big.NewInt(2)).String() ||
newStateCoin1.Volume != helpers.BipToPip(big.NewInt(1002)).String() ||
newStateCoin1.Reserve != helpers.BipToPip(big.NewInt(200)).String() ||
newStateCoin1.MaxSupply != helpers.BipToPip(big.NewInt(200)).String() ||
newStateCoin1.Crr != 50 {
Expand Down

0 comments on commit 851dfcd

Please sign in to comment.