Skip to content

Commit

Permalink
Add delegation test, cover 18.3% (#2274)
Browse files Browse the repository at this point in the history
Add unit test for validator.go

fix the format problem

fixing the failing test

some work

adding tests for validator sanitychecks

creating fresh validator wrapper for testing sanity check

minor

adding more tests to transactions
  • Loading branch information
fxfactorial authored Mar 5, 2020
1 parent 1e5d420 commit 71974e1
Show file tree
Hide file tree
Showing 3 changed files with 608 additions and 11 deletions.
82 changes: 82 additions & 0 deletions staking/types/delegation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package types

import (
"math/big"
"testing"

common "github.com/ethereum/go-ethereum/common"
common2 "github.com/harmony-one/harmony/internal/common"
)

var (
testAddr, _ = common2.Bech32ToAddress("one129r9pj3sk0re76f7zs3qz92rggmdgjhtwge62k")
delegatorAddr = common.Address(testAddr)
delegationAmt = big.NewInt(100000)
// create a new delegation:
delegation = NewDelegation(delegatorAddr, delegationAmt)
)

func TestUndelegate(t *testing.T) {
epoch1 := big.NewInt(10)
amount1 := big.NewInt(1000)
delegation.Undelegate(epoch1, amount1)

// check the undelegation's Amount
if delegation.Undelegations[0].Amount.Cmp(amount1) != 0 {
t.Errorf("undelegate failed, amount does not match")
}
// check the undelegation's Epoch
if delegation.Undelegations[0].Epoch.Cmp(epoch1) != 0 {
t.Errorf("undelegate failed, epoch does not match")
}

epoch2 := big.NewInt(12)
amount2 := big.NewInt(2000)
delegation.Undelegate(epoch2, amount2)

// check the number of undelegations
if len(delegation.Undelegations) != 2 {
t.Errorf("total number of undelegations should have been two")
}
}

func TestTotalInUndelegation(t *testing.T) {
var totalAmount *big.Int = delegation.TotalInUndelegation()

// check the total amount of undelegation
if totalAmount.Cmp(big.NewInt(3000)) != 0 {
t.Errorf("total undelegation amount is not correct")
}
}

func TestDeleteEntry(t *testing.T) {
// add the third delegation
// Undelegations[]: 1000, 2000, 3000
epoch3 := big.NewInt(15)
amount3 := big.NewInt(3000)
delegation.Undelegate(epoch3, amount3)

// delete the second undelegation entry
// Undelegations[]: 1000, 3000
deleteEpoch := big.NewInt(12)
delegation.DeleteEntry(deleteEpoch)

// check if the Undelegtaions[1] == 3000
if delegation.Undelegations[1].Amount.Cmp(big.NewInt(3000)) != 0 {
t.Errorf("deleting an undelegation entry fails, amount is not correct")
}
}

func TestRemoveUnlockUndelegations(t *testing.T) {
lastEpochInCommitte := big.NewInt(16)
curEpoch := big.NewInt(24)

epoch4 := big.NewInt(21)
amount4 := big.NewInt(4000)
delegation.Undelegate(epoch4, amount4)

result := delegation.RemoveUnlockedUndelegations(curEpoch, lastEpochInCommitte)
if result.Cmp(big.NewInt(8000)) != 0 {
t.Errorf("removing an unlocked undelegation fails")
}
}
102 changes: 97 additions & 5 deletions staking/types/transaction_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package types

import (
"fmt"
"bytes"
"errors"
"math/big"
"testing"

Expand All @@ -19,6 +20,20 @@ var (
testBLSPubKey2 = "40379eed79ed82bebfb4310894fd33b6a3f8413a78dc4d43b98d0adc9ef69f3285df05eaab9f2ce5f7227f8cb920e809"
)

func createDelegate() (*StakingTransaction, error) {
dAddr, _ := common2.Bech32ToAddress(testAccount)
vAddr, _ := common2.Bech32ToAddress(testAccount)
stakePayloadMaker := func() (Directive, interface{}) {
return DirectiveDelegate, Delegate{
DelegatorAddress: dAddr,
ValidatorAddress: vAddr,
Amount: big.NewInt(100),
}
}
gasPrice := big.NewInt(1)
return NewStakingTransaction(0, 21000, gasPrice, stakePayloadMaker)
}

func CreateTestNewTransaction() (*StakingTransaction, error) {
dAddr, _ := common2.Bech32ToAddress(testAccount)

Expand Down Expand Up @@ -96,9 +111,86 @@ func TestTransactionCopy(t *testing.T) {
if cv1.CommissionRates.Rate.Equal(cv2.CommissionRates.Rate) {
t.Errorf("CommissionRate should not be equal")
}
}

func TestHash(t *testing.T) {
stakingTx, err := CreateTestNewTransaction()
if err != nil {
t.Errorf("cannot create new staking transaction, %v\n", err)
}
hash := stakingTx.Hash()
if hash.String() == "" {
t.Errorf("cannot get hash of staking transaction, %v\n", err)
}
if stakingTx.Hash().String() != hash.String() {
t.Errorf("cannot set hash of staking transaction\n")
}
}

func TestGasCost(t *testing.T) {
stakingTx, err := CreateTestNewTransaction()
if err != nil {
t.Errorf("cannot create validator staking transaction, %v\n", err)
}
if stakingTx.Gas() != 600000 {
t.Errorf("gas set incorrectly \n")
}
if stakingTx.GasPrice().Int64() != big.NewInt(1).Int64() {
t.Errorf("gas price set incorrectly \n")
}
cost, err := stakingTx.Cost()
if err != nil || cost.Int64() != 600100 {
t.Errorf("staking transaction cost is incorrect %v\n", err)
}
delegateTx, err := createDelegate()
if err != nil {
t.Errorf("cannot create delegate staking transaction, %v\n", err)
}
cost, err = delegateTx.Cost()
if err != nil || cost.Int64() != 21100 {
t.Errorf("staking transaction cost is incorrect %v\n", err)
}
dAddr, _ := common2.Bech32ToAddress(testAccount)
vAddr, _ := common2.Bech32ToAddress(testAccount)
delegateTx1, err := NewStakingTransaction(0, 21000, big.NewInt(1), func() (Directive, interface{}) {
return DirectiveCreateValidator, Delegate{
DelegatorAddress: dAddr,
ValidatorAddress: vAddr,
Amount: big.NewInt(100),
}
})
if _, err = delegateTx1.Cost(); err == nil {
t.Error("expected", errStakingTransactionTypeCastErr, "got", nil)
}
}

func TestNonce(t *testing.T) {
stakingTx, err := CreateTestNewTransaction()
if err != nil {
t.Errorf("cannot create validator staking transaction, %v\n", err)
}
if stakingTx.Nonce() != 0 {
t.Error("incorrect nonce \n")
}
}

fmt.Println("cv1", cv1)
fmt.Println("cv2", cv2)
fmt.Println("cv1", cv1.Description)
fmt.Println("cv2", cv2.Description)
func TestData(t *testing.T) {
stakingTx, err := CreateTestNewTransaction()
if err != nil {
t.Errorf("cannot create validator staking transaction, %v\n", err)
}
encoded, err := stakingTx.RLPEncodeStakeMsg()
if err != nil {
t.Errorf("could not rlp encode staking tx %v\n", err)
}
if bytes.Compare(stakingTx.Data(), encoded) != 0 {
t.Error("RLPEncode and Data does not match \n")
}
if _, err = RLPDecodeStakeMsg(encoded, DirectiveCreateValidator); err != nil {
t.Errorf("could not rlp decode staking tx %v\n", err)
}
e := errors.New("rlp: expected input string or byte for common.Address, decoding into (types.Delegate).ValidatorAddress")
if _, err = RLPDecodeStakeMsg(encoded, DirectiveDelegate); err == nil {
t.Error("expected", e, "got", nil)
}
}
Loading

0 comments on commit 71974e1

Please sign in to comment.