Skip to content

Commit

Permalink
Improvements
Browse files Browse the repository at this point in the history
See PR #72
  • Loading branch information
RiccardoM authored Jan 8, 2021
1 parent 60c6ff2 commit 9abe79b
Show file tree
Hide file tree
Showing 11 changed files with 170 additions and 102 deletions.
14 changes: 10 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Tests / Code Coverage
name: Tests
# Tests / Code Coverage workflow runs unit tests and uploads a code coverage report.
# This workflow is run on pushes to master & every Pull Requests where a .go, .mod, .sum have been changed.
on:
Expand All @@ -20,9 +20,15 @@ jobs:
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- uses: actions/checkout@v2
- name: Test & Coverage report creation
- name: Checkout
uses: actions/checkout@v2
- name: Setup Go
uses: actions/setup-go@v2
with:
go-version: 1.15
- name: Test & Create coverage report
run: make install test-unit stop-docker-test
- uses: codecov/codecov-action@v1.0.7
- name: Upload cove coverage
uses: codecov/codecov-action@v1.0.7
with:
file: ./coverage.txt
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# BDJuno
[![Build Status](https://travis-ci.com/forbole/bdjuno.svg?branch=master)](https://travis-ci.com/forbole/bdjuno)
[![Go Report Card](https://goreportcard.com/badge/github.com/forbole/bdjuno)](https://goreportcard.com/reporl-debugt/github.com/forbole/bdjuno)
[![Codecov](https://img.shields.io/codecov/c/github/forbole/bdjuno)](https://codecov.io/gh/forbole/bdjuno/branch/master)
[![GitHub Workflow Status](https://img.shields.io/github/workflow/status/forbole/bdjuno/Tests)](https://github.com/forbole/bdjuno/actions?query=workflow%3ATests)
[![Go Report Card](https://goreportcard.com/badge/github.com/forbole/bdjuno)](https://goreportcard.com/report/github.com/forbole/bdjuno)
[![Codecov](https://img.shields.io/codecov/c/github/forbole/bdjuno)](https://codecov.io/gh/forbole/bdjuno/branch/cosmos-v0.39.x)

BDJuno (shorthand for BigDipper Juno) is the [Juno](https://github.com/desmos-labs/juno) implementation for [BigDipper](https://github.com/forbole/big-dipper).

Expand Down
17 changes: 17 additions & 0 deletions database/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,23 @@ func (db *BigDipperDb) SaveConsensus(event constypes.ConsensusEvent) error {
return err
}

// GetLastBlockHeight returns the last block height stored inside the database
func (db *BigDipperDb) GetLastBlockHeight() (int64, error) {
stmt := `SELECT height FROM block ORDER BY timestamp DESC LIMIT 1`

var blocks []dbtypes.BlockRow
if err := db.Sqlx.Select(&blocks, stmt); err != nil {
return 0, err
}

if len(blocks) == 0 {
return 0, fmt.Errorf("cannot get block, no blocks saved")
}

return blocks[0].Height, nil
}

// getBlockHeightTime retrieves the block at the specific time
func (db *BigDipperDb) getBlockHeightTime(pastTime time.Time) (dbtypes.BlockRow, error) {
stmt := `SELECT block.timestamp, block.height
FROM block
Expand Down
87 changes: 85 additions & 2 deletions database/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ import (
"regexp"
"strings"
"testing"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
tmctypes "github.com/tendermint/tendermint/rpc/core/types"
tmtypes "github.com/tendermint/tendermint/types"
"github.com/tendermint/tendermint/version"

stakingtypes "github.com/forbole/bdjuno/x/staking/types"

"github.com/cosmos/cosmos-sdk/simapp"
jconfig "github.com/desmos-labs/juno/config"
Expand All @@ -16,6 +24,10 @@ import (
_ "github.com/proullon/ramsql/driver"
)

func TestDatabaseTestSuite(t *testing.T) {
suite.Run(t, new(DbTestSuite))
}

type DbTestSuite struct {
suite.Suite

Expand Down Expand Up @@ -71,6 +83,77 @@ func (suite *DbTestSuite) SetupTest() {
suite.database = bigDipperDb
}

func TestDatabaseTestSuite(t *testing.T) {
suite.Run(t, new(DbTestSuite))
// getBlock builds, stores and returns a block for the provided height
func (suite *DbTestSuite) getBlock(height int64) *tmctypes.ResultBlock {
validator := suite.getValidator(
"cosmosvalcons1qqqqrezrl53hujmpdch6d805ac75n220ku09rl",
"cosmosvaloper1rcp29q3hpd246n6qak7jluqep4v006cdsc2kkl",
"cosmosvalconspub1zcjduepq7mft6gfls57a0a42d7uhx656cckhfvtrlmw744jv4q0mvlv0dypskehfk8",
)

addr, err := sdk.ConsAddressFromBech32(validator.GetConsAddr())
suite.Require().NoError(err)

block := &tmctypes.ResultBlock{
BlockID: tmtypes.BlockID{},
Block: &tmtypes.Block{
Header: tmtypes.Header{
Version: version.Consensus{},
ChainID: "",
Height: height,
Time: time.Now(),
LastBlockID: tmtypes.BlockID{},
LastCommitHash: nil,
DataHash: nil,
ValidatorsHash: []byte("hash"),
NextValidatorsHash: nil,
ConsensusHash: nil,
AppHash: nil,
LastResultsHash: nil,
EvidenceHash: nil,
ProposerAddress: tmtypes.Address(addr.Bytes()),
},
Data: tmtypes.Data{},
Evidence: tmtypes.EvidenceData{},
LastCommit: &tmtypes.Commit{
Height: height - 1,
Round: 1,
BlockID: tmtypes.BlockID{},
Signatures: nil,
},
},
}

err = suite.database.SaveBlock(block, 10000, 100)
suite.Require().NoError(err)
return block
}

// getValidator stores inside the database a validator having the given
// consensus address, validator address and validator public key
func (suite *DbTestSuite) getValidator(consAddr, valAddr, pubkey string) stakingtypes.Validator {
selfDelegation := suite.getAccount("cosmos1z4hfrxvlgl4s8u4n5ngjcw8kdqrcv43599amxs")

pubKey, err := sdk.GetPubKeyFromBech32(sdk.Bech32PubKeyTypeConsPub, pubkey)
suite.Require().NoError(err)

maxRate := sdk.NewDec(10)
maxChangeRate := sdk.NewDec(20)

validator := stakingtypes.NewValidator(consAddr, valAddr, pubKey, selfDelegation.String(), &maxChangeRate, &maxRate)
err = suite.database.SaveValidatorData(validator)
suite.Require().NoError(err)

return validator
}

// getAccount saves inside the database an account having the given address
func (suite *DbTestSuite) getAccount(addr string) sdk.AccAddress {
delegator, err := sdk.AccAddressFromBech32(addr)
suite.Require().NoError(err)

_, err = suite.database.Sql.Exec(`INSERT INTO account (address) VALUES ($1) ON CONFLICT DO NOTHING`, delegator.String())
suite.Require().NoError(err)

return delegator
}
91 changes: 39 additions & 52 deletions database/staking_validators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,35 +22,6 @@ func newIntPtr(value int64) *sdk.Int {
return &val
}

// getValidator stores inside the database a validator having the given
// consensus address, validator address and validator public key
func (suite *DbTestSuite) getValidator(consAddr, valAddr, pubkey string) types.Validator {
selfDelegation := suite.getAccount("cosmos1z4hfrxvlgl4s8u4n5ngjcw8kdqrcv43599amxs")

pubKey, err := sdk.GetPubKeyFromBech32(sdk.Bech32PubKeyTypeConsPub, pubkey)
suite.Require().NoError(err)

maxRate := sdk.NewDec(10)
maxChangeRate := sdk.NewDec(20)

validator := types.NewValidator(consAddr, valAddr, pubKey, selfDelegation.String(), &maxChangeRate, &maxRate)
err = suite.database.SaveValidatorData(validator)
suite.Require().NoError(err)

return validator
}

// getAccount saves inside the database an account having the given address
func (suite *DbTestSuite) getAccount(addr string) sdk.AccAddress {
delegator, err := sdk.AccAddressFromBech32(addr)
suite.Require().NoError(err)

_, err = suite.database.Sql.Exec(`INSERT INTO account (address) VALUES ($1) ON CONFLICT DO NOTHING`, delegator.String())
suite.Require().NoError(err)

return delegator
}

// _________________________________________________________

func (suite *DbTestSuite) TestSaveValidator() {
Expand Down Expand Up @@ -357,6 +328,8 @@ VALUES ('cosmosvalcons1qqqqrezrl53hujmpdch6d805ac75n220ku09rl',
// _________________________________________________________

func (suite *DbTestSuite) TestSaveValidatorsVotingPowers() {
block := suite.getBlock(100)

validator1 := suite.getValidator(
"cosmosvalcons1qqqqrezrl53hujmpdch6d805ac75n220ku09rl",
"cosmosvaloper1rcp29q3hpd246n6qak7jluqep4v006cdsc2kkl",
Expand All @@ -369,8 +342,8 @@ func (suite *DbTestSuite) TestSaveValidatorsVotingPowers() {
)

votingPowers := []types.ValidatorVotingPower{
types.NewValidatorVotingPower(validator1.GetConsAddr(), 1000, 100),
types.NewValidatorVotingPower(validator2.GetConsAddr(), 2000, 100),
types.NewValidatorVotingPower(validator1.GetConsAddr(), 1000, block.Block.Height),
types.NewValidatorVotingPower(validator2.GetConsAddr(), 2000, block.Block.Height),
}

for _, power := range votingPowers {
Expand Down Expand Up @@ -402,59 +375,73 @@ func (suite *DbTestSuite) TestSaveValidatorsVotingPowers() {
//-----------------------------------------------------------

func (suite *DbTestSuite) TestSaveValidatorStatus() {
validator1 := suite.getValidator(
block1 := suite.getBlock(10)
block2 := suite.getBlock(20)

validator := suite.getValidator(
"cosmosvalcons1qqqqrezrl53hujmpdch6d805ac75n220ku09rl",
"cosmosvaloper1rcp29q3hpd246n6qak7jluqep4v006cdsc2kkl",
"cosmosvalconspub1zcjduepq7mft6gfls57a0a42d7uhx656cckhfvtrlmw744jv4q0mvlv0dypskehfk8",
)

history1 := dbtypes.NewValidatorStatusHistoryRow(
err := suite.database.SaveValidatorStatus(types.NewValidatorStatus(
validator.GetConsAddr(),
1,
false,
10,
validator1.GetConsAddr(),
)

history2 := []dbtypes.ValidatorStatusHistoryRow{
history1,
dbtypes.NewValidatorStatusHistoryRow(
2,
true,
20,
validator1.GetConsAddr(),
),
}
err := suite.database.SaveValidatorStatus(types.NewValidatorStatus(validator1.GetConsAddr(), 1, false, 10))
block1.Block.Height,
))
suite.Require().NoError(err)

var result []dbtypes.ValidatorStatusRow
err = suite.database.Sqlx.Select(&result, "SELECT * FROM validator_status")
suite.Require().NoError(err)
suite.Require().Len(result, 1)
suite.Require().True(result[0].Equal(dbtypes.NewValidatorStatusRow(1, false, validator1.GetConsAddr())))
suite.Require().True(result[0].Equal(dbtypes.NewValidatorStatusRow(1, false, validator.GetConsAddr())))

firstRow := dbtypes.NewValidatorStatusHistoryRow(
1,
false,
block1.Block.Height,
validator.GetConsAddr(),
)

var result2 []dbtypes.ValidatorStatusHistoryRow
err = suite.database.Sqlx.Select(&result2, "SELECT * FROM validator_status_history")
suite.Require().NoError(err)
suite.Require().Len(result2, 1)
suite.Require().True(result2[0].Equal(history1))
suite.Require().True(result2[0].Equal(firstRow))

// Second insert
err = suite.database.SaveValidatorStatus(types.NewValidatorStatus(validator1.GetConsAddr(), 2, true, 20))
err = suite.database.SaveValidatorStatus(types.NewValidatorStatus(
validator.GetConsAddr(),
2,
true,
block2.Block.Height,
))
suite.Require().NoError(err)

var result3 []dbtypes.ValidatorStatusRow
err = suite.database.Sqlx.Select(&result3, "SELECT * FROM validator_status")
suite.Require().NoError(err)
suite.Require().Len(result3, 1)
suite.Require().True(result3[0].Equal(dbtypes.NewValidatorStatusRow(2, true, validator1.GetConsAddr())))
suite.Require().True(result3[0].Equal(dbtypes.NewValidatorStatusRow(2, true, validator.GetConsAddr())))

expected := []dbtypes.ValidatorStatusHistoryRow{
firstRow,
dbtypes.NewValidatorStatusHistoryRow(
2,
true,
block2.Block.Height,
validator.GetConsAddr(),
),
}

var result4 []dbtypes.ValidatorStatusHistoryRow
err = suite.database.Sqlx.Select(&result4, "SELECT * FROM validator_status_history")
suite.Require().NoError(err)
suite.Require().Len(result4, 2)
for index, row := range result4 {
suite.Require().True(row.Equal(history2[index]))
suite.Require().True(row.Equal(expected[index]))
}

}
Expand Down
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.15

require (
github.com/cosmos/cosmos-sdk v0.39.2
github.com/desmos-labs/juno v0.0.0-20210104142039-0e91c396632f
github.com/desmos-labs/juno v0.0.0-20210108090929-23b21a6cf04e
github.com/go-co-op/gocron v0.3.3
github.com/jmoiron/sqlx v1.2.1-0.20200324155115-ee514944af4b
github.com/lib/pq v1.3.0
Expand All @@ -13,8 +13,7 @@ require (
github.com/stretchr/testify v1.6.1
github.com/tendermint/tendermint v0.33.9
github.com/ziutek/mymysql v1.5.4 // indirect
golang.org/x/mod v0.4.0 // indirect
golang.org/x/tools v0.0.0-20201215192005-fa10ef0b8743 // indirect
golang.org/x/net v0.0.0-20201021035429-f5854403a974 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 // indirect
)
Loading

0 comments on commit 9abe79b

Please sign in to comment.