-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
921a9bc
commit 8bbf19d
Showing
9 changed files
with
233 additions
and
209 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
Oops, something went wrong.