Skip to content
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
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
12 changes: 10 additions & 2 deletions client/api_nodes_responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@

package client

type ApiNode struct {
Url string `json:"url,omitempty"`
Version string `json:"version,omitempty"`
Height int64 `json:"height,omitempty"`
Latency byte `json:"latency,omitempty"`
Status string `json:"status,omitempty"`
}

type ApiNodesResponse struct {
Meta Meta `json:"meta"`
Data []Peer `json:"data"`
Meta Meta `json:"meta"`
Data []ApiNode `json:"data"`
}
20 changes: 8 additions & 12 deletions client/api_nodes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,11 @@ func TestApiNodesService_All(t *testing.T) {
"totalCount": 1
},
"data": [{
"ip": "1.2.3.4",
"port": 4002,
"ports": {
"@arkecosystem/core-wallet-api": 4040
},
"url": "http://1.2.3.4:4003",
"version": "2.0.0",
"latency": 10
"height": 1204291,
"latency": 10,
"status": "success"
}]
}`)
})
Expand All @@ -62,14 +60,12 @@ func TestApiNodesService_All(t *testing.T) {
Self: "/api-nodes?transform=true&limit=100&page=1",
TotalCount: 1,
},
Data: []Peer{{
Ip: "1.2.3.4",
Port: 4002,
Ports: PeerPorts{
"@arkecosystem/core-wallet-api": 4040,
},
Data: []ApiNode{{
Url: "http://1.2.3.4:4003",
Version: "2.0.0",
Height: 1204291,
Latency: 10,
Status: "success",
}},
})
}
47 changes: 47 additions & 0 deletions client/bigint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package client

import (
"fmt"
"math/big"
"strings"
)

// BigInt wraps *big.Int to (un)marshal the decimal-string format the API
// uses for wei-scale values (nonces, balances, fees, rewards), avoiding the
// range ceiling of uint64/uint32.
Comment thread
ItsANameToo marked this conversation as resolved.
Outdated
type BigInt struct {
*big.Int
}

// UnmarshalJSON and MarshalJSON intentionally use different receiver types
// (a lint tool may flag this). UnmarshalJSON must be a pointer receiver since
// it mutates b.Int; a value receiver would only update a local copy and the
// parsed value would be lost. MarshalJSON must stay a value receiver because
// BigInt is used as a map value elsewhere (e.g. NodeFeesResponse), and map
// values are not addressable in Go — a pointer receiver would fail to satisfy
// json.Marshaler there, silently falling back to *big.Int's default (bare,
// unquoted) JSON encoding instead of this type's quoted decimal-string format.
Comment thread
ItsANameToo marked this conversation as resolved.
Outdated
func (b *BigInt) UnmarshalJSON(data []byte) error {
s := strings.Trim(string(data), `"`)
if s == "" || s == "null" {
b.Int = big.NewInt(0)
return nil
}

i, ok := new(big.Int).SetString(s, 10)
if !ok {
return fmt.Errorf("client: invalid big integer %q", s)
}

b.Int = i

return nil
}

func (b BigInt) MarshalJSON() ([]byte, error) {
if b.Int == nil {
return []byte(`"0"`), nil
}

return []byte(`"` + b.Int.String() + `"`), nil
}
6 changes: 3 additions & 3 deletions client/blockchain_responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ type BlockchainInfo struct {
// BlockchainData represents the data field in the BlockchainInfo response.
type BlockchainData struct {
Block BlockchainBlock `json:"block"`
Supply string `json:"supply"`
Supply BigInt `json:"supply"`
}

// BlockchainBlock represents a block in the blockchain.
type BlockchainBlock struct {
Height int `json:"height"`
Id string `json:"id"`
Hash string `json:"hash"`
Number int64 `json:"number"`
}
10 changes: 5 additions & 5 deletions client/blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ func TestBlockchainService_Info(t *testing.T) {
`{
"data": {
"block": {
"height": 1213625,
"id": "e7fd8d878860f4f41c1f53eef8ea75b585b22016b8d3aaf2d6742c1b59894016"
"number": 1213625,
"hash": "e7fd8d878860f4f41c1f53eef8ea75b585b22016b8d3aaf2d6742c1b59894016"
},
"supply": "12727605199999969"
}
Expand All @@ -39,10 +39,10 @@ func TestBlockchainService_Info(t *testing.T) {
testResponseStruct(t, "Blockchain.Info", responseStruct, &BlockchainInfo{
Data: BlockchainData{
Block: BlockchainBlock{
Height: 1213625,
Id: "e7fd8d878860f4f41c1f53eef8ea75b585b22016b8d3aaf2d6742c1b59894016",
Number: 1213625,
Hash: "e7fd8d878860f4f41c1f53eef8ea75b585b22016b8d3aaf2d6742c1b59894016",
},
Supply: "12727605199999969",
Supply: newBigInt(12727605199999969),
},
})
}
10 changes: 5 additions & 5 deletions client/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ func (s *BlocksService) List(ctx context.Context, query *Pagination) (*Blocks, *
return responseStruct, resp, err
}

// Get a block by the given id.
func (s *BlocksService) Get(ctx context.Context, id int64) (*GetBlock, *http.Response, error) {
uri := fmt.Sprintf("blocks/%v", id)
// Get a block by the given hash.
func (s *BlocksService) Get(ctx context.Context, hash string) (*GetBlock, *http.Response, error) {
uri := fmt.Sprintf("blocks/%v", hash)

var responseStruct *GetBlock
resp, err := s.client.SendRequest(ctx, "GET", uri, nil, nil, &responseStruct, "api")
Expand Down Expand Up @@ -68,8 +68,8 @@ func (s *BlocksService) Last(ctx context.Context) (*GetBlock, *http.Response, er
}

// Get all transactions by the given block.
func (s *BlocksService) Transactions(ctx context.Context, id int64, query *Pagination) (*GetBlockTransactions, *http.Response, error) {
uri := fmt.Sprintf("blocks/%v/transactions", id)
func (s *BlocksService) Transactions(ctx context.Context, hash string, query *Pagination) (*GetBlockTransactions, *http.Response, error) {
uri := fmt.Sprintf("blocks/%v/transactions", hash)

var responseStruct *GetBlockTransactions
resp, err := s.client.SendRequest(ctx, "GET", uri, query, nil, &responseStruct, "api")
Expand Down
49 changes: 20 additions & 29 deletions client/blocks_responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,27 @@

package client

type BlockForged struct {
Reward uint64 `json:"reward,omitempty,string"`
Fee uint64 `json:"fee,omitempty,string"`
Total uint64 `json:"total,omitempty,string"`
Amount uint64 `json:"amount,omitempty,string"`
}

type BlockPayload struct {
Hash string `json:"hash,omitempty"`
Length uint32 `json:"length,omitempty"`
}

type BlockGenerator struct {
Username string `json:"username,omitempty"`
Address string `json:"address,omitempty"`
PublicKey string `json:"publicKey,omitempty"`
}

type Block struct {
Id string `json:"id,omitempty"`
Version byte `json:"version,omitempty"`
Height int64 `json:"height,omitempty"`
Previous string `json:"previous,omitempty"`
Forged BlockForged `json:"forged,omitempty"`
Payload BlockPayload `json:"payload,omitempty"`
Generator BlockGenerator `json:"generator,omitempty"`
Signature string `json:"signature,omitempty"`
Confirmations uint32 `json:"confirmations,omitempty"`
Transactions byte `json:"transactions,omitempty"`
Timestamp int64 `json:"timestamp,omitempty"`
Hash string `json:"hash,omitempty"`
Number int64 `json:"number,omitempty"`
Round int64 `json:"round,omitempty"`
Confirmations uint32 `json:"confirmations,omitempty"`
Amount BigInt `json:"amount,omitempty"`
Fee BigInt `json:"fee,omitempty"`
GasUsed int64 `json:"gasUsed,omitempty"`
Reward BigInt `json:"reward,omitempty"`
Total BigInt `json:"total,omitempty"`
Proposer string `json:"proposer,omitempty"`
PublicKey string `json:"publicKey,omitempty"`
Username string `json:"username,omitempty"`
ValidatorSet string `json:"validatorSet,omitempty"`
TransactionsRoot string `json:"transactionsRoot,omitempty"`
PayloadSize int64 `json:"payloadSize,omitempty"`
ParentHash string `json:"parentHash,omitempty"`
Signature string `json:"signature,omitempty"`
Timestamp string `json:"timestamp,omitempty"`
TransactionsCount int64 `json:"transactionsCount,omitempty"`
Version byte `json:"version,omitempty"`
}

type Blocks struct {
Expand Down
Loading
Loading