Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vesting account data #158

Closed
wants to merge 32 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
92f0215
Get Vesting base on vestingAccountYAML in cosmos
HarleyAppleChoi Jul 8, 2021
9b18b76
Add PerodicVestingAccount type
HarleyAppleChoi Jul 9, 2021
bfd7964
Fix duplicated import dbtypes -> bddbtypes
HarleyAppleChoi Jul 9, 2021
46bb073
Introduce periods types
HarleyAppleChoi Jul 9, 2021
83c0b12
Update periods types
HarleyAppleChoi Jul 9, 2021
41fbebe
Add types
HarleyAppleChoi Jul 9, 2021
9d6dbe8
period types
HarleyAppleChoi Jul 9, 2021
2a409de
Implenment grpc get account
HarleyAppleChoi Jul 12, 2021
f844b73
Update unpack account
HarleyAppleChoi Jul 12, 2021
c172c66
Implement get accounts from grpc
HarleyAppleChoi Jul 13, 2021
2c3cd98
authttypes->authtypes
HarleyAppleChoi Jul 13, 2021
6c2fdc0
add testing
HarleyAppleChoi Jul 14, 2021
4fc3992
Add coin type
HarleyAppleChoi Jul 14, 2021
4f7ed91
Handle error for block and genesis is empty
HarleyAppleChoi Jul 16, 2021
8e75985
Fix bug
HarleyAppleChoi Jul 19, 2021
6e0956d
Merge branch 'apple/fix_bug' into apple/vesting_account_data
HarleyAppleChoi Jul 20, 2021
768a4aa
Implenment test case
HarleyAppleChoi Jul 20, 2021
1131b52
Implenment account string
HarleyAppleChoi Jul 22, 2021
d13bf88
Fix when genesis account number more than 63353 pq will error
HarleyAppleChoi Jul 22, 2021
628422a
Delete comment
HarleyAppleChoi Jul 22, 2021
5eb5a34
implenment test case
HarleyAppleChoi Jul 26, 2021
9c1c653
Turn test case to pure string comparasion
HarleyAppleChoi Jul 26, 2021
cafd94f
fix test case
HarleyAppleChoi Jul 27, 2021
b2cfab4
Merge branch 'cosmos-v0.40.x-ori' into apple/vesting_account_data
HarleyAppleChoi Jul 27, 2021
0e03404
lint
HarleyAppleChoi Jul 27, 2021
c3b6c3c
delete duplicated postgresql patch funtion
HarleyAppleChoi Jul 27, 2021
c128aa5
Better variable naming
HarleyAppleChoi Jul 27, 2021
6fbde0f
Add error handling for not valid address query
HarleyAppleChoi Jul 27, 2021
5fe5d3d
make lint
HarleyAppleChoi Jul 27, 2021
8b22f09
Merge branch 'cosmos-v0.40.x-ori' into apple/vesting_account_data
HarleyAppleChoi Jul 29, 2021
7db338a
delete get all accounts
HarleyAppleChoi Jul 29, 2021
7ffe9c8
Merge branch 'cosmos/v0.40.x' into apple/vesting_account_data
HarleyAppleChoi Oct 7, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 50 additions & 13 deletions database/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package database
import (
"fmt"

codectypes "github.com/cosmos/cosmos-sdk/codec/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
dbtypes "github.com/forbole/bdjuno/database/types"
dbutils "github.com/forbole/bdjuno/database/utils"

"github.com/forbole/bdjuno/types"
)

Expand All @@ -19,7 +21,7 @@ func (db *Db) SaveAccounts(accounts []types.Account) error {
}

// Store up-to-date data
err := db.saveAccounts(paramsNumber, accounts)
err := db.saveAccounts(accounts)
if err != nil {
return fmt.Errorf("error while storing accounts: %s", err)
}
Expand All @@ -28,20 +30,35 @@ func (db *Db) SaveAccounts(accounts []types.Account) error {
return nil
}

func (db *Db) saveAccounts(paramsNumber int, accounts []types.Account) error {
func (db *Db) saveAccounts(accounts []types.Account) error {
if len(accounts) == 0 {
return nil
}

stmt := `INSERT INTO account (address) VALUES `
stmt := `INSERT INTO account (address,details) VALUES `
var params []interface{}

for i, account := range accounts {
ai := i * paramsNumber
stmt += fmt.Sprintf("($%d),", ai+1)
params = append(params, account.Address)
}
ai := i
stmt += fmt.Sprintf("($%d,$%d),", ai+1, ai+2)
protoContent, ok := account.Details.(authtypes.AccountI)
if !ok {
return fmt.Errorf("invalid proposal content types: %T", account.Details)
}

anyContent, err := codectypes.NewAnyWithValue(protoContent)
if err != nil {
return err
}

contentBz, err := db.EncodingConfig.Marshaler.MarshalJSON(anyContent)
if err != nil {
return err
}

contentBzstring := string(contentBz)


params = append(params, account.Address, contentBzstring)
stmt = stmt[:len(stmt)-1]
stmt += " ON CONFLICT DO NOTHING"
_, err := db.Sql.Exec(stmt, params...)
Expand All @@ -53,8 +70,28 @@ func (db *Db) saveAccounts(paramsNumber int, accounts []types.Account) error {
}

// GetAccounts returns all the accounts that are currently stored inside the database.
func (db *Db) GetAccounts() ([]string, error) {
var rows []string
err := db.Sqlx.Select(&rows, `SELECT address FROM account`)
return rows, err
func (db *Db) GetAccounts() ([]types.Account, error) {
var rows []dbtypes.AccountRow
err := db.Sqlx.Select(&rows, `SELECT address,details FROM account`)
if err != nil {
return nil, err
}

returnRows := make([]types.Account, len(rows))
for i, row := range rows {
b := []byte(row.Details)

if len(b) == 0 {
returnRows[i] = types.NewAccount(row.Address, nil)
} else {
//var inter interface{}
var account authtypes.AccountI
err = db.EncodingConfig.Marshaler.UnmarshalInterfaceJSON(b, &account)
if err != nil {
return nil, err
}
returnRows[i] = types.NewAccount(row.Address, account)
}
}
return returnRows, nil
}
82 changes: 66 additions & 16 deletions database/auth_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package database_test

import (
"fmt"

codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
authttypes "github.com/cosmos/cosmos-sdk/x/auth/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
authvestingtypes "github.com/cosmos/cosmos-sdk/x/auth/vesting/types"

"github.com/forbole/bdjuno/types"

Expand All @@ -13,16 +17,34 @@ func (suite *DbTestSuite) TestSaveAccount() {
address, err := sdk.AccAddressFromBech32("cosmos140xsjjg6pwkjp0xjz8zru7ytha60l5aee9nlf7")
suite.Require().NoError(err)

account := authttypes.NewBaseAccountWithAddress(address)
coin := sdk.Coin{
Denom: "daric",
Amount: sdk.NewInt(10),
}
account := authtypes.NewBaseAccountWithAddress(address)
baseVestingAccount := authvestingtypes.BaseVestingAccount{
BaseAccount: account,
OriginalVesting: sdk.NewCoins(coin),
DelegatedFree: sdk.NewCoins(coin),
DelegatedVesting: sdk.NewCoins(coin),
}
continuousVestingAccount := authvestingtypes.ContinuousVestingAccount{
BaseVestingAccount: &baseVestingAccount,
StartTime: 10,
}
saveAccount := []types.Account{
types.NewAccount(
continuousVestingAccount.Address,
&continuousVestingAccount)}

// ------------------------------
// --- Save the data
// ------------------------------

err = suite.database.SaveAccounts([]types.Account{types.NewAccount(account.Address)})
err = suite.database.SaveAccounts(saveAccount)
suite.Require().NoError(err)

err = suite.database.SaveAccounts([]types.Account{types.NewAccount(account.Address)})
err = suite.database.SaveAccounts(saveAccount)
suite.Require().NoError(err, "double account insertion should not insert and returns no error")

// ------------------------------
Expand All @@ -35,18 +57,46 @@ func (suite *DbTestSuite) TestSaveAccount() {
suite.Require().NoError(err)
suite.Require().Len(accountRows, 1, "account table should contain only one row")

expectedAccountRow := dbtypes.NewAccountRow("cosmos140xsjjg6pwkjp0xjz8zru7ytha60l5aee9nlf7")
protoContent, ok := saveAccount[0].Details.(authtypes.AccountI)
suite.Require().True(ok)
anyContent, err := codectypes.NewAnyWithValue(protoContent)
suite.Require().NoError(err)
js, err := suite.database.EncodingConfig.Marshaler.MarshalJSON(anyContent)
suite.Require().NoError(err)
expectedAccountRow := dbtypes.NewAccountRow("cosmos140xsjjg6pwkjp0xjz8zru7ytha60l5aee9nlf7", string(js))

suite.Require().True(expectedAccountRow.Equal(accountRows[0]))
}

func (suite *DbTestSuite) TestBigDipperDb_GetAccounts() {
address, err := sdk.AccAddressFromBech32("cosmos140xsjjg6pwkjp0xjz8zru7ytha60l5aee9nlf7")
suite.Require().NoError(err)

coin := sdk.Coin{
Denom: "daric",
Amount: sdk.NewInt(10),
}
baseAccount := authtypes.NewBaseAccountWithAddress(address)
baseVestingAccount := authvestingtypes.BaseVestingAccount{
BaseAccount: baseAccount,
OriginalVesting: sdk.NewCoins(coin),
DelegatedFree: sdk.NewCoins(coin),
DelegatedVesting: sdk.NewCoins(coin),
}
continuousVestingAccount := authvestingtypes.ContinuousVestingAccount{
BaseVestingAccount: &baseVestingAccount,
StartTime: 10,
}

account := `{"@type":"/cosmos.vesting.v1beta1.ContinuousVestingAccount","base_vesting_account":{"base_account":{"address":"cosmos140xsjjg6pwkjp0xjz8zru7ytha60l5aee9nlf7","pub_key":null,"account_number":"0","sequence":"0"},"original_vesting":[{"denom":"daric","amount":"10"}],"delegated_free":[{"denom":"daric","amount":"10"}],"delegated_vesting":[{"denom":"daric","amount":"10"}],"end_time":"0"},"start_time":"10"}`

// Insert the data
queries := []string{
`INSERT INTO account (address) VALUES ('cosmos1ltzt0z992ke6qgmtjxtygwzn36km4cy6cqdknt')`,
`INSERT INTO account (address) VALUES ('cosmos1re6zjpyczs0w7flrl6uacl0r4teqtyg62crjsn')`,
`INSERT INTO account (address) VALUES ('cosmos1eg47ue0l85lzkfgc4leske6hcah8cz3qajpjy2')`,
`INSERT INTO account (address) VALUES ('cosmos1495ghynrns8sxfnw8mj887pgh0c9z6c4lqkzme')`,
`INSERT INTO account (address) VALUES ('cosmos18fzr6adp3gjw43xu62vfhg248lepfwpf0pj2dm')`,
fmt.Sprintf("INSERT INTO account (address,details) VALUES ('cosmos1ltzt0z992ke6qgmtjxtygwzn36km4cy6cqdknt',%s)", "'"+account+"'"),
fmt.Sprintf("INSERT INTO account (address,details) VALUES ('cosmos1re6zjpyczs0w7flrl6uacl0r4teqtyg62crjsn',%s)", "'"+account+"'"),
fmt.Sprintf("INSERT INTO account (address,details) VALUES ('cosmos1eg47ue0l85lzkfgc4leske6hcah8cz3qajpjy2',%s)", "'"+account+"'"),
fmt.Sprintf("INSERT INTO account (address,details) VALUES ('cosmos1495ghynrns8sxfnw8mj887pgh0c9z6c4lqkzme',%s)", "'"+account+"'"),
fmt.Sprintf("INSERT INTO account (address,details) VALUES ('cosmos18fzr6adp3gjw43xu62vfhg248lepfwpf0pj2dm',%s)", "'"+account+"'"),
}

for _, query := range queries {
Expand All @@ -59,12 +109,12 @@ func (suite *DbTestSuite) TestBigDipperDb_GetAccounts() {
suite.Require().NoError(err)

// Verify the get
expectedAccs := []string{
"cosmos1ltzt0z992ke6qgmtjxtygwzn36km4cy6cqdknt",
"cosmos1re6zjpyczs0w7flrl6uacl0r4teqtyg62crjsn",
"cosmos1eg47ue0l85lzkfgc4leske6hcah8cz3qajpjy2",
"cosmos1495ghynrns8sxfnw8mj887pgh0c9z6c4lqkzme",
"cosmos18fzr6adp3gjw43xu62vfhg248lepfwpf0pj2dm",
expectedAccs := []types.Account{
types.NewAccount("cosmos1ltzt0z992ke6qgmtjxtygwzn36km4cy6cqdknt", &continuousVestingAccount),
types.NewAccount("cosmos1re6zjpyczs0w7flrl6uacl0r4teqtyg62crjsn", &continuousVestingAccount),
types.NewAccount("cosmos1eg47ue0l85lzkfgc4leske6hcah8cz3qajpjy2", &continuousVestingAccount),
types.NewAccount("cosmos1495ghynrns8sxfnw8mj887pgh0c9z6c4lqkzme", &continuousVestingAccount),
types.NewAccount("cosmos18fzr6adp3gjw43xu62vfhg248lepfwpf0pj2dm", &continuousVestingAccount),
}

for index, acc := range expectedAccs {
Expand Down
5 changes: 3 additions & 2 deletions database/schema/01-auth.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
CREATE TABLE account
(
address TEXT NOT NULL PRIMARY KEY
);
address TEXT NOT NULL PRIMARY KEY,
details TEXT
);
Loading