Skip to content

Commit

Permalink
Various bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
hleb-albau committed Dec 20, 2018
1 parent f463f0b commit c42912d
Show file tree
Hide file tree
Showing 16 changed files with 165 additions and 89 deletions.
37 changes: 18 additions & 19 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,19 +123,20 @@ func NewCyberdApp(
cdc := MakeCodec()

dbKeys := CyberdAppDbKeys{
main: sdk.NewKVStoreKey("main"),
acc: sdk.NewKVStoreKey("acc"),
cidNum: sdk.NewKVStoreKey("cid_index"),
links: sdk.NewKVStoreKey("links"),
rank: sdk.NewKVStoreKey("rank"),
stake: sdk.NewKVStoreKey("stake"),
fees: sdk.NewKVStoreKey("fee"),
tStake: sdk.NewTransientStoreKey("transient_stake"),
keyDistr: sdk.NewKVStoreKey("distr"),
slashing: sdk.NewKVStoreKey("slashing"),
params: sdk.NewKVStoreKey("params"),
tParams: sdk.NewTransientStoreKey("transient_params"),
accBandwidth: sdk.NewKVStoreKey("acc_bandwidth"),
main: sdk.NewKVStoreKey("main"),
acc: sdk.NewKVStoreKey("acc"),
cidNum: sdk.NewKVStoreKey("cid_index"),
cidNumReverse: sdk.NewKVStoreKey("cid_index_reverse"),
links: sdk.NewKVStoreKey("links"),
rank: sdk.NewKVStoreKey("rank"),
stake: sdk.NewKVStoreKey("stake"),
fees: sdk.NewKVStoreKey("fee"),
tStake: sdk.NewTransientStoreKey("transient_stake"),
keyDistr: sdk.NewKVStoreKey("distr"),
slashing: sdk.NewKVStoreKey("slashing"),
params: sdk.NewKVStoreKey("params"),
tParams: sdk.NewTransientStoreKey("transient_params"),
accBandwidth: sdk.NewKVStoreKey("acc_bandwidth"),
}

ms := store.NewMainKeeper(dbKeys.main)
Expand Down Expand Up @@ -198,7 +199,7 @@ func NewCyberdApp(
// register message routes
app.Router().
AddRoute("bank", sdkbank.NewHandler(app.bankKeeper)).
AddRoute("link", link.NewLinksHandler(app.cidNumKeeper, app.linkIndexedKeeper, app.accountKeeper)).
AddRoute("link", link.NewLinksHandler(app.cidNumKeeper, &app.linkIndexedKeeper, app.accountKeeper)).
AddRoute("stake", stake.NewHandler(app.stakeKeeper)).
AddRoute("slashing", slashing.NewHandler(app.slashingKeeper))

Expand All @@ -213,7 +214,7 @@ func NewCyberdApp(

// mount the multistore and load the latest state
app.MountStores(
dbKeys.main, dbKeys.acc, dbKeys.cidNum, dbKeys.links, dbKeys.rank, dbKeys.stake,
dbKeys.main, dbKeys.acc, dbKeys.cidNum, dbKeys.cidNumReverse, dbKeys.links, dbKeys.rank, dbKeys.stake,
dbKeys.slashing, dbKeys.params, dbKeys.keyDistr, dbKeys.fees, dbKeys.accBandwidth,
)
app.MountStoresTransient(dbKeys.tParams, dbKeys.tStake)
Expand Down Expand Up @@ -377,10 +378,8 @@ func (app *CyberdApp) DeliverTx(txBytes []byte) (res abci.ResponseDeliverTx) {
} else {

resp := app.BaseApp.DeliverTx(txBytes)
if resp.Code == 0 {
app.bandwidthMeter.ConsumeAccBandwidth(ctx, accBw, txCost)
app.curBlockSpentBandwidth = app.curBlockSpentBandwidth + uint64(txCost)
}
app.bandwidthMeter.ConsumeAccBandwidth(ctx, accBw, txCost)
app.curBlockSpentBandwidth = app.curBlockSpentBandwidth + uint64(txCost)

return abci.ResponseDeliverTx{
Code: uint32(resp.Code),
Expand Down
10 changes: 7 additions & 3 deletions app/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
)

type RankedCid struct {
cid cbdlink.Cid
rank float64
Cid cbdlink.Cid `json:"cid"`
Rank float64 `amino:"unsafe" json:"rank"`
}

func (app *CyberdApp) RpcContext() sdk.Context {
Expand All @@ -35,7 +35,7 @@ func (app *CyberdApp) Search(cid string, page, perPage int) ([]RankedCid, int, e

result := make([]RankedCid, 0, len(rankedCidNumbers))
for _, c := range rankedCidNumbers {
result = append(result, RankedCid{cid: app.cidNumKeeper.GetCid(ctx, c.GetNumber()), rank: c.GetRank()})
result = append(result, RankedCid{Cid: app.cidNumKeeper.GetCid(ctx, c.GetNumber()), Rank: c.GetRank()})
}

return result, size, nil
Expand Down Expand Up @@ -65,3 +65,7 @@ func (app *CyberdApp) IsLinkExist(from cbdlink.Cid, to cbdlink.Cid, address sdk.

return false
}

func (app *CyberdApp) CurrentBandwidthPrice() float64 {
return app.currentCreditPrice
}
27 changes: 27 additions & 0 deletions client/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package client

import (
cbdlink "github.com/cybercongress/cyberd/x/link/types"
)

type CidsFilter map[cbdlink.Cid]map[cbdlink.Cid]struct{}

func (cf CidsFilter) Put(from cbdlink.Cid, to cbdlink.Cid) {

cidLinks := cf[from]
if cidLinks == nil {
cidLinks = make(map[cbdlink.Cid]struct{})
}
cidLinks[to] = struct{}{}
cf[from] = cidLinks
}

func (cf CidsFilter) Contains(from cbdlink.Cid, to cbdlink.Cid) bool {

cidLinks := cf[from]
if cidLinks == nil {
return false
}
_, contains := cidLinks[to]
return contains
}
68 changes: 50 additions & 18 deletions client/http_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
authtxb "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder"
"github.com/cybercongress/cyberd/app"
cbd "github.com/cybercongress/cyberd/app/types"
"github.com/cybercongress/cyberd/daemon/rpc"
bwtps "github.com/cybercongress/cyberd/x/bandwidth/types"
"github.com/cybercongress/cyberd/x/link"
cbdlink "github.com/cybercongress/cyberd/x/link/types"
tdmClient "github.com/tendermint/tendermint/rpc/client"
"github.com/tendermint/tendermint/rpc/lib/client"
"os"
Expand All @@ -29,8 +31,8 @@ type HttpCyberdClient struct {
// fields used by local keys store to sing transactions
passphrase string
fromAddress sdk.AccAddress
cliCtx cli.CLIContext
txBuilder authtxb.TxBuilder
cliCtx *cli.CLIContext
txBuilder *authtxb.TxBuilder
}

func NewHttpCyberdClient(nodeUrl string, passphrase string, singAddr string) CyberdClient {
Expand Down Expand Up @@ -58,11 +60,18 @@ func NewHttpCyberdClient(nodeUrl string, passphrase string, singAddr string) Cyb
}.WithCodec(cdc).WithAccountDecoder(cdc)

accountNumber, _ := cliCtx.GetAccountNumber(addr)
seq, err := cliCtx.GetAccountSequence(addr)

if err != nil {
panic(err)
}

txBuilder := authtxb.TxBuilder{
Gas: 1000000,
ChainID: status.NodeInfo.Network,
AccountNumber: accountNumber,
Codec: cdc,
Sequence: seq,
}

return HttpCyberdClient{
Expand All @@ -73,46 +82,68 @@ func NewHttpCyberdClient(nodeUrl string, passphrase string, singAddr string) Cyb

passphrase: passphrase,
fromAddress: addr,
cliCtx: cliCtx,
txBuilder: txBuilder,
cliCtx: &cliCtx,
txBuilder: &txBuilder,
}
}

func (c HttpCyberdClient) GetChainId() string {
return c.chainId
}

/*func (c HttpCyberdClient) GetCurrentBandwidthCreditPrice() (float64, error) {
func (c HttpCyberdClient) IsLinkExist(from cbdlink.Cid, to cbdlink.Cid, addr sdk.AccAddress) (result bool, err error) {
_, err = c.httpClient.Call("is_link_exist",
map[string]interface{}{"from": from, "to": to, "address": addr.String()},
&result,
)
return
}

func (c HttpCyberdClient) GetAccount(address sdk.AccAddress) (auth.Account, error) {
func (c HttpCyberdClient) GetCurrentBandwidthCreditPrice() (float64, error) {
result := &rpc.ResultBandwidthPrice{}
_, err := c.httpClient.Call("current_bandwidth_price", map[string]interface{}{}, &result)
return result.Price, err
}

func (c HttpCyberdClient) GetAccountBandwidth(address sdk.AccAddress) (bdwth.AcсBandwidth, error) {
}*/
func (c HttpCyberdClient) GetAccountBandwidth() (result bwtps.AcсBandwidth, err error) {
_, err = c.httpClient.Call("account_bandwidth",
map[string]interface{}{"address": c.fromAddress.String()}, &result)
return
}

func (c HttpCyberdClient) SubmitLinkSync(link Link) error {
return c.SubmitLinksSync([]Link{link})
}

func (c HttpCyberdClient) SubmitLinksSync(links []Link) error {
msges := make([]sdk.Msg, 0, len(links))

// used to remove duplicated items
var filter = make(CidsFilter)
msges := make([]sdk.Msg, 0)

for _, l := range links {
msges = append(msges, link.NewMsg(c.fromAddress, cbd.Cid(l.From), cbd.Cid(l.To)))

if filter.Contains(l.From, l.To) {
continue
}

exists, err := c.IsLinkExist(l.From, l.To, c.fromAddress)
if err != nil {
return err
}
if !exists {
msges = append(msges, link.NewMsg(c.fromAddress, l.From, l.To))
}
filter.Put(l.From, l.To)
}
return c.BroadcastTx(msges)
}

func (c HttpCyberdClient) BroadcastTx(msgs []sdk.Msg) error {

seq, err := c.cliCtx.GetAccountSequence(c.fromAddress)
if err != nil {
return err
if len(msgs) == 0 {
return nil
}
c.txBuilder.Sequence = seq

txBytes, err := c.txBuilder.BuildAndSign(c.cliCtx.From, c.passphrase, msgs)
if err != nil {
Expand All @@ -130,6 +161,7 @@ func (c HttpCyberdClient) BroadcastTx(msgs []sdk.Msg) error {
if result.Code != 0 {
return errors.New(string(result.Log))
}
c.txBuilder.Sequence = c.txBuilder.Sequence + 1
return nil
}

Expand Down
26 changes: 14 additions & 12 deletions client/spec.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package client

import (
cbd "github.com/cybercongress/cyberd/app/types"
sdk "github.com/cosmos/cosmos-sdk/types"
bwtps "github.com/cybercongress/cyberd/x/bandwidth/types"
cbdlink "github.com/cybercongress/cyberd/x/link/types"
)

type Link struct {
From cbd.Cid
To cbd.Cid
From cbdlink.Cid
To cbdlink.Cid
}

type CyberdClient interface {
Expand All @@ -15,17 +17,17 @@ type CyberdClient interface {
// returns current connected node chain id
GetChainId() string

/* // get current bandwidth credits price
// price 1 is price for situation, when all users use all their bandwidth (all blocks are filled for 100%)
// if price < 1, that means blocks filled partially, thus allow more active users to do more transactions
// if price > 1, that means network is under high load.
GetCurrentBandwidthCreditPrice() (float64, error)
// returns, if given link already exists
IsLinkExist(from cbdlink.Cid, to cbdlink.Cid, addr sdk.AccAddress) (result bool, err error)

// returns account for given address
GetAccount(address sdk.AccAddress) (auth.Account, error)
// get current bandwidth credits price
// price 1 is price for situation, when all users use all their bandwidth (all blocks are filled for 100%)
// if price < 1, that means blocks filled partially, thus allow more active users to do more transactions
// if price > 1, that means network is under high load.
GetCurrentBandwidthCreditPrice() (float64, error)

// returns account bandwidth information for given account
GetAccountBandwidth(address sdk.AccAddress) (bdwth.AcсBandwidth, error)*/
// returns account bandwidth information for given account
GetAccountBandwidth() (bwtps.AcсBandwidth, error)

// links two cids for given user
// this method also should check, either cids are correct cids and given user is msg signer
Expand Down
9 changes: 9 additions & 0 deletions daemon/rpc/bandwidth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package rpc

type ResultBandwidthPrice struct {
Price float64 `amino:"unsafe" json:"price"`
}

func CurrentBandwidthPrice() (*ResultBandwidthPrice, error) {
return &ResultBandwidthPrice{cyberdApp.CurrentBandwidthPrice()}, nil
}
9 changes: 5 additions & 4 deletions daemon/rpc/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ func SetCyberdApp(cApp *app.CyberdApp) {
}

var Routes = map[string]*rpcserver.RPCFunc{
"search": rpcserver.NewRPCFunc(Search, "cid,page,perPage"),
"account": rpcserver.NewRPCFunc(Account, "address"),
"account_bandwidth": rpcserver.NewRPCFunc(AccountBandwidth, "address"),
"is_link_exist": rpcserver.NewRPCFunc(IsLinkExist, "from, to, address"),
"search": rpcserver.NewRPCFunc(Search, "cid,page,perPage"),
"account": rpcserver.NewRPCFunc(Account, "address"),
"account_bandwidth": rpcserver.NewRPCFunc(AccountBandwidth, "address"),
"is_link_exist": rpcserver.NewRPCFunc(IsLinkExist, "from,to,address"),
"current_bandwidth_price": rpcserver.NewRPCFunc(CurrentBandwidthPrice, ""),
}

func init() {
Expand Down
10 changes: 10 additions & 0 deletions docs/rpc.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ the method name for further details such as parameter and return information.
|2|[account](#account)|Get account nonce, pubkey, number, and coins.|
|3|[account_bandwidth](#account-bandwidth)|Get account bandwidth info for current height.|
|4|[is_link_exist](#link-exist)|Return true, if given link exist.|
|5|[current_bandwidth_price](#current-bandwidth-price)|Returns current bandwidth credit price.|

### Method Details

Expand Down Expand Up @@ -80,6 +81,15 @@ the method name for further details such as parameter and return information.
|Description|Return true, if given link exist.|
|[Return to Overview](#method-overview)<br />

<a name="current-bandwidth-price"/>

| | |
|---|---|
|Method|current_bandwidth_price|
|Parameters||
|Description|Returns current bandwidth credit price.|
|[Return to Overview](#method-overview)<br />

***
<br />
<br />
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ github.com/syndtr/goleveldb v0.0.0-20181128100959-b001fa50d6b2 h1:GnOzE5fEFN3b2z
github.com/syndtr/goleveldb v0.0.0-20181128100959-b001fa50d6b2/go.mod h1:Z4AUp2Km+PwemOoO/VB5AOx9XSsIItzFjoJlOSiYmn0=
github.com/tendermint/btcd v0.0.0-20180816174608-e5840949ff4f h1:R0wLxgASGMoRQTF/dCSk4N+M3j9DLyPDzDff2WtCg/I=
github.com/tendermint/btcd v0.0.0-20180816174608-e5840949ff4f/go.mod h1:DC6/m53jtQzr/NFmMNEu0rxf18/ktVoVtMrnDD5pN+U=
github.com/tendermint/crypto v0.0.0-20180820045704-3764759f34a5 h1:u8i49c+BxloX3XQ55cvzFNXplizZP/q00i+IlttUjAU=
github.com/tendermint/crypto v0.0.0-20180820045704-3764759f34a5/go.mod h1:z4YtwM70uOnk8h0pjJYlj3zdYwi9l03By6iAIF5j/Pk=
github.com/tendermint/go-amino v0.14.1 h1:o2WudxNfdLNBwMyl2dqOJxiro5rfrEaU0Ugs6offJMk=
github.com/tendermint/go-amino v0.14.1/go.mod h1:i/UKE5Uocn+argJJBb12qTZsCDBcAYMbR92AaJVmKso=
Expand Down
7 changes: 4 additions & 3 deletions testnet/checklist.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
2. adjust genesis supply in x/mint
3. reset old state
4. change genesis file chain-id
5. generate new first validator keys
6. upadte genesis gen tx
5. upadte genesis gen tx
6. update readme.md testnet lable
7. Update docs/run_validator.md guide

## Useful commands

Expand All @@ -21,7 +22,7 @@
scp -P 33324 /home/hlb/projects/cyberd/testnet/genesis.json earth@earth.cybernode.ai:/cyberdata/cyberd/config/
scp -P 33324 /home/hlb/projects/cyberd/testnet/config.toml earth@earth.cybernode.ai:/cyberdata/cyberd/config/
# Copy from earth
scp -P 33324 earth@earth.cybernode.ai:/path/file /host/path/file
scp -P 33324 earth@93.125.26.210:/cyberdata/cyberd/config/priv_validator.json ~/.cyberd/config/
```

```bash
Expand Down
2 changes: 1 addition & 1 deletion testnet/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ db_backend = "leveldb"
db_dir = "data"

# Output level for logging, including package level options
log_level = "main:info,state:info,*:error"
log_level = "*:error"

# Output format: 'plain' (colored text) or 'json'
log_format = "plain"
Expand Down
Loading

0 comments on commit c42912d

Please sign in to comment.