Skip to content

Commit

Permalink
fix: NetworkInfo model
Browse files Browse the repository at this point in the history
  • Loading branch information
andskur committed Oct 5, 2021
1 parent 996cce2 commit ac6401e
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 20 deletions.
42 changes: 42 additions & 0 deletions examples/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package main

import (
"context"
"fmt"

"github.com/ethereum/go-ethereum/rpc"
api "github.com/gateway-fm/near-api-go"
"github.com/gateway-fm/near-api-go/config"
)

func main() {
rpc, err := rpc.DialContext(context.Background(), "https://rpc.mainnet.near.org")
if err != nil {
panic(err)
}

cfg := &config.Config{
RPCClient: rpc,
NetworkID: "mainnet",
}

apiN, err := api.NewClient(cfg)
if err != nil {
panic(err)
}

status, err := apiN.NodeStatus(context.Background())
if err != nil {
panic(err)
}

fmt.Println(status.ChainID)

netInfo, err := apiN.NetworkInfo(context.Background())
if err != nil {
panic(err)
}

fmt.Println(netInfo.NumActivePeers)
fmt.Println(netInfo.ReceivedBytesPerSec)
}
20 changes: 7 additions & 13 deletions models/network_structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,22 @@ import (

// NetworkInfo holds network information
type NetworkInfo struct {
ActivePeers []FullPeerInfo `json:"active_peers"`
ActivePeers []*PeerInfo `json:"active_peers"`
NumActivePeers uint `json:"num_active_peers"`
PeerMaxCount uint32 `json:"peer_max_count"`
HighestHeightPeers []FullPeerInfo `json:"highest_height_peers"`
HighestHeightPeers []*PeerInfo `json:"highest_height_peers"`
SentBytesPerSec uint64 `json:"sent_bytes_per_sec"`
ReceivedBytesPerSec uint64 `json:"received_bytes_per_sec"`
KnownProducers []KnownProducer `json:"known_producers"`
MetricRecorder MetricRecorder `json:"metric_recorder"`
KnownProducers []*PeerInfo `json:"known_producers"`
MetricRecorder *MetricRecorder `json:"metric_recorder"`
PeerCounter uint `json:"peer_counter"`
}

type FullPeerInfo struct {
PeerInfo PeerInfo `json:"peer_info"`
ChainInfo PeerChainInfo `json:"chain_info"`
EdgeInfo EdgeInfo `json:"edge_info"`
}

// PeerInfo holds peer information
type PeerInfo struct {
ID key.PeerID `json:"id"`
Addr *string `json:"addr"`
AccountID *types.AccountID `json:"account_id"`
ID string `json:"id"`
Addr string `json:"addr"`
AccountID types.AccountID `json:"account_id"`
}

// PeerChainInfo contains peer chain information. This is derived from PeerCHainInfoV2 in nearcore
Expand Down
2 changes: 1 addition & 1 deletion types/balance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

fuzz "github.com/google/gofuzz"

. "github.com/eteu-technologies/near-api-go/pkg/types"
. "github.com/gateway-fm/near-api-go/pkg/types"
)

func TestNEARToYocto(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions types/hash/crypto_hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (c *CryptoHash) UnmarshalJSON(b []byte) error {

data, err := base58.Decode(string(s))
if err != nil {
return err
return fmt.Errorf("decode base58Hash: %w", err)
}

if l := len(data); l != sha256.Size {
Expand Down Expand Up @@ -48,7 +48,7 @@ func NewCryptoHash(data []byte) CryptoHash {
func NewCryptoHashFromBase58(blob string) (ch CryptoHash, err error) {
bytes, err := base58.Decode(blob)
if err != nil {
return
return [sha256.Size]byte{}, fmt.Errorf("decode base58Hash: %w", err)
}

if len(bytes) != sha256.Size {
Expand Down
2 changes: 1 addition & 1 deletion types/key/key_pair.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

"github.com/mr-tron/base58"

"github.com/eteu-technologies/near-api-go/pkg/types/signature"
"github.com/gateway-fm/near-api-go/types/signature"
)

type KeyPair struct {
Expand Down
13 changes: 11 additions & 2 deletions types/key/public_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
"strings"

"github.com/mr-tron/base58"

"github.com/eteu-technologies/near-api-go/pkg/types/signature"
"github.com/gateway-fm/near-api-go/types/signature"
)

// TODO: SECP256K1
Expand Down Expand Up @@ -36,9 +37,17 @@ func (p *PublicKey) UnmarshalJSON(b []byte) error {
return err
}

fmt.Println(s)

keyParts := strings.Split(s, ":")

if len(keyParts) > 1 {
s = keyParts[1]
}

dec, err := base58.Decode(s)
if err != nil {
return err
return fmt.Errorf("failed to decode public key: %w", err)
}

*p = PublicKey{}
Expand Down
2 changes: 1 addition & 1 deletion types/key/public_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package key_test
import (
"testing"

"github.com/eteu-technologies/near-api-go/pkg/types/key"
"github.com/gateway-fm/near-api-go/types/key"
)

func TestED25519Key(t *testing.T) {
Expand Down

0 comments on commit ac6401e

Please sign in to comment.