Skip to content

Commit

Permalink
#122 Simple go cyberd client
Browse files Browse the repository at this point in the history
  • Loading branch information
hleb-albau committed Dec 19, 2018
1 parent 921a9bc commit 8bbf19d
Show file tree
Hide file tree
Showing 9 changed files with 233 additions and 209 deletions.
8 changes: 4 additions & 4 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,11 @@ func NewCyberdApp(
app.paramsKeeper = params.NewKeeper(app.cdc, dbKeys.params, dbKeys.tParams)
app.feeCollectionKeeper = auth.NewFeeCollectionKeeper(app.cdc, dbKeys.fees)

var stakeKeeper *stake.Keeper
var stakeKeeper stake.Keeper
coinTransferHooks := []cbdbank.CoinsTransferHook{bandwidth.CollectAddressesWithStakeChange()}
app.bankKeeper = cbdbank.NewBankKeeper(app.accountKeeper, stakeKeeper, coinTransferHooks)
app.bankKeeper = cbdbank.NewBankKeeper(app.accountKeeper, &stakeKeeper, coinTransferHooks)
app.accBandwidthKeeper = bandwidth.NewAccBandwidthKeeper(dbKeys.accBandwidth)
*stakeKeeper = stake.NewKeeper(
stakeKeeper = stake.NewKeeper(
app.cdc, dbKeys.stake,
dbKeys.tStake, app.bankKeeper,
app.paramsKeeper.Subspace(stake.DefaultParamspace),
Expand All @@ -179,7 +179,7 @@ func NewCyberdApp(
app.bankKeeper, stakeKeeper, app.feeCollectionKeeper,
distr.DefaultCodespace,
)
app.minter = mint.NewMinter(app.feeCollectionKeeper, *stakeKeeper)
app.minter = mint.NewMinter(app.feeCollectionKeeper, stakeKeeper)

app.memStorage = &InMemoryStorage{}

Expand Down
165 changes: 165 additions & 0 deletions client/http_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
package client

import (
"errors"
"fmt"
cli "github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/keys"
cskeys "github.com/cosmos/cosmos-sdk/crypto/keys"
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/x/link"
tdmClient "github.com/tendermint/tendermint/rpc/client"
"github.com/tendermint/tendermint/rpc/lib/client"
"os"
)

type HttpCyberdClient struct {
// tdm client
tdmClient tdmClient.Client
// transport client
httpClient rpcclient.JSONRPCClient

// general fields
nodeUrl string
chainId string

// fields used by local keys store to sing transactions
passphrase string
fromAddress sdk.AccAddress
cliCtx cli.CLIContext
txBuilder authtxb.TxBuilder
}

func NewHttpCyberdClient(nodeUrl string, passphrase string, singAddr string) CyberdClient {

tdmHttpClient := tdmClient.NewHTTP(nodeUrl, "/websocket")
httpClient := rpcclient.NewJSONRPCClient(nodeUrl)
status, err := tdmHttpClient.Status()
if err != nil {
panic(err)
}

cdc := app.MakeCodec()
app.SetPrefix()
addr, cliAddrName := accountFromAddress(singAddr)
verifier := &NoopVerifier{ChainId: status.NodeInfo.Network}
cliCtx := cli.CLIContext{
Client: tdmHttpClient,
NodeURI: nodeUrl,
AccountStore: "acc",
From: cliAddrName,
TrustNode: true,
Async: false,
PrintResponse: true,
Verifier: verifier,
}.WithCodec(cdc).WithAccountDecoder(cdc)

accountNumber, _ := cliCtx.GetAccountNumber(addr)
txBuilder := authtxb.TxBuilder{
Gas: 1000000,
ChainID: status.NodeInfo.Network,
AccountNumber: accountNumber,
Codec: cdc,
}

return HttpCyberdClient{
tdmClient: tdmHttpClient,
httpClient: *httpClient,

chainId: status.NodeInfo.Network,

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

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

/*func (c HttpCyberdClient) GetCurrentBandwidthCreditPrice() (float64, error) {
}
func (c HttpCyberdClient) GetAccount(address sdk.AccAddress) (auth.Account, error) {
}
func (c HttpCyberdClient) GetAccountBandwidth(address sdk.AccAddress) (bdwth.AcсBandwidth, error) {
}*/

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))
for _, l := range links {
msges = append(msges, link.NewMsg(c.fromAddress, cbd.Cid(l.From), cbd.Cid(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
}
c.txBuilder.Sequence = seq

txBytes, err := c.txBuilder.BuildAndSign(c.cliCtx.From, c.passphrase, msgs)
if err != nil {
panic(err)
}

result, err := c.cliCtx.BroadcastTxSync(txBytes)
if err != nil {
println("Error during broadcasting tx. Rebrodcasting ...")
println(err.Error())

_ = c.BroadcastTx(msgs)
}

if result.Code != 0 {
return errors.New(string(result.Log))
}
return nil
}

func accountFromAddress(from string) (fromAddr sdk.AccAddress, fromName string) {
if from == "" {
return nil, ""
}

keybase, err := keys.GetKeyBase()
if err != nil {
fmt.Println("no keybase found")
os.Exit(1)
}

var info cskeys.Info
if addr, err := sdk.AccAddressFromBech32(from); err == nil {
info, err = keybase.GetByAddress(addr)
if err != nil {
fmt.Printf("could not find key %s\n", from)
os.Exit(1)
}
} else {
info, err = keybase.Get(from)
if err != nil {
fmt.Printf("could not find key %s\n", from)
os.Exit(1)
}
}

fromAddr = info.GetAddress()
fromName = info.GetName()
return
}
32 changes: 14 additions & 18 deletions client/spec.go
Original file line number Diff line number Diff line change
@@ -1,41 +1,37 @@
package client

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
cbd "github.com/cybercongress/cyberd/app/types"
bdwth "github.com/cybercongress/cyberd/x/bandwidth/types"
)

type Link struct {
from cbd.Cid
to cbd.Cid
address sdk.AccAddress
From cbd.Cid
To cbd.Cid
}

type CyberdClient interface {
// Cyberd Client Specification

// returns current connected node chain id
GetChainId() (string, error)
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)
/* // 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 for given address
GetAccount(address sdk.AccAddress) (auth.Account, error)
// returns account for given address
GetAccount(address sdk.AccAddress) (auth.Account, error)
// returns account bandwidth information for given account
GetAccountBandwidth(address sdk.AccAddress) (bdwth.AcсBandwidth, error)
// returns account bandwidth information for given account
GetAccountBandwidth(address sdk.AccAddress) (bdwth.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
// do not wait till tx will be mined, just returns results from tx mempool check
SubmitLinkAsync(link Link) error
SubmitLinkSync(link Link) error

// see `SubmitLinkAsync`. Links will be submitted as single tx with multiple msges.
SubmitLinksAsync(links []Link) error
SubmitLinksSync(links []Link) error
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ require (
github.com/mitchellh/mapstructure v1.0.0 // indirect
github.com/mr-tron/base58 v1.1.0 // indirect
github.com/multiformats/go-multibase v0.3.0 // indirect
github.com/multiformats/go-multihash v1.0.8 // indirect
github.com/multiformats/go-multihash v1.0.8
github.com/onsi/gomega v1.4.2 // indirect
github.com/pelletier/go-toml v1.2.0 // indirect
github.com/pkg/errors v0.8.0
Expand Down
19 changes: 19 additions & 0 deletions wiki/cid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main

import (
cbd "github.com/cybercongress/cyberd/app/types"
"github.com/ipfs/go-cid"
"github.com/multiformats/go-multihash"
)

var pref = cid.Prefix{
Version: 0,
Codec: cid.Raw,
MhType: multihash.SHA2_256,
MhLength: -1, // default length
}

func Cid(data string) cbd.Cid {
result, _ := pref.Sum([]byte(data))
return cbd.Cid(result.String())
}

0 comments on commit 8bbf19d

Please sign in to comment.