Skip to content

Commit

Permalink
v5.0.0 update (#23)
Browse files Browse the repository at this point in the history
* v5.0.0 update

* v5 for go modules

* Add wallet_foreign_api client

* NodeOwnerAPI Client

* NodeOwnerAPI done

* Full NodeForeignAPI client

* Update gomod temp

* Fix v4 v5
  • Loading branch information
quentinlesceller authored Dec 17, 2020
1 parent eb169fb commit 15e865d
Show file tree
Hide file tree
Showing 55 changed files with 2,122 additions and 1,473 deletions.
3 changes: 2 additions & 1 deletion README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ Any contribution is more than welcome. Currently libgrin has:

- Minimal API types.
- Minimal Core package which includes PoW verification.
- Client package containing wrappers around Node API and wallet owner API.
- Client package containing wrappers around Node API (Foreign and Owner) and wallet API (Foreign and Owner).
- Minimal keychain types
- Minimal libwallet
- Minimal p2p types

Missing parts:

Expand Down
113 changes: 100 additions & 13 deletions api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@

package api

import (
"bytes"
"encoding/json"

"github.com/blockcypher/libgrin/v5/core"
)

// BlockPrintable is the result of the Grin block API
type BlockPrintable struct {
Header BlockHeaderPrintable `json:"header"`
Expand Down Expand Up @@ -59,17 +66,22 @@ type BlockHeaderPrintable struct {
// OutputPrintable represents the output of a block
type OutputPrintable struct {
// The type of output Coinbase|Transaction
OutputType string `json:"output_type"`
OutputType outputType `json:"output_type"`
// The homomorphic commitment representing the output's amount
// (as hex string)
Commit string `json:"commit"`
// Whether the output has been spent
Spent bool `json:"spent"`
// Rangeproof (as hex string)
Proof string `json:"proof"`
Proof *string `json:"proof"`
// Rangeproof hash (as hex string)
ProofHash string `json:"proof_hash"`
MerkleProof string `json:"merkle_proof"`
ProofHash string `json:"proof_hash"`
// BlockHeight at which the output is found
BlockHeight *uint64 `json:"block_height"`
// Merkle Proof
MerkleProof *string `json:"merkle_proof"`
// MMR Position
MMRIndex uint64 `json:"mmr_index"`
}

// TxKernelsPrintables is the tx kernel
Expand All @@ -83,13 +95,88 @@ type TxKernelsPrintables struct {

// The Status represents various statistics about the network
type Status struct {
ProtocolVersion int `json:"protocol_version"`
UserAgent string `json:"user_agent"`
Connections int `json:"connections"`
Tip struct {
Height int `json:"height"`
LastBlockPushed string `json:"last_block_pushed"`
PrevBlockToLast string `json:"prev_block_to_last"`
TotalDifficulty int `json:"total_difficulty"`
} `json:"tip"`
ProtocolVersion uint32 `json:"protocol_version"`
UserAgent string `json:"user_agent"`
Connections uint32 `json:"connections"`
Tip Tip `json:"tip"`
SyncStatus string `json:"sync_status"`
SyncInfo *string `json:"sync_info"`
}

// LocatedTxKernel is a located TxKernel
type LocatedTxKernel struct {
TxKernel core.TxKernel `json:"tx_kernel"`
Height uint64 `json:"height"`
MMRIndex uint64 `json:"mmr_index"`
}

// outputType is the type of output
type outputType int

const (
// CoinbaseOutputType is a coinbase output type
CoinbaseOutputType outputType = iota
// TransactionOutputType is a transaction output type
TransactionOutputType
)

var toStringOutputType = map[outputType]string{
CoinbaseOutputType: "Coinbase",
TransactionOutputType: "Transaction",
}

var toIDOutputType = map[string]outputType{
"Coinbase": CoinbaseOutputType,
"Transaction": TransactionOutputType,
}

// MarshalJSON marshals the enum as a quoted json string
func (s outputType) MarshalJSON() ([]byte, error) {
buffer := bytes.NewBufferString(`"`)
buffer.WriteString(toStringOutputType[s])
buffer.WriteString(`"`)
return buffer.Bytes(), nil
}

// UnmarshalJSON unmarshals a quoted json string to the enum value
func (s *outputType) UnmarshalJSON(b []byte) error {
var j string
err := json.Unmarshal(b, &j)
if err != nil {
return err
}
// Note that if the string cannot be found then it will be set to the zero value, 'CoinbaseOutputType' in this case.
*s = toIDOutputType[j]
return nil
}

// OutputListing is for traversing all outputs in the UTXO set with the
// transactions in the block
type OutputListing struct {
// The last available output index
HighestIndex uint64 `json:"highest_index"`
// The last insertion index retrieved
LastRetrievedIndex uint64 `json:"last_retrieved_index"`
// A printable version of the outputs
Outputs []OutputPrintable `json:"outputs"`
}

// Tip is the state of the current fork tip
type Tip struct {
/// Height of the tip (max height of the fork)
Height uint64 `json:"height"`
// Last block pushed to the fork
LastBlockPushed string `json:"last_block_pushed"`
// Block previous to last
PrevBlockToLast string `json:"prev_block_to_last"`
// Total difficulty accumulated on that fork
TotalDifficulty uint64 `json:"total_difficulty"`
}

// Version is the API Version Information
type Version struct {
// Current node API Version (api crate version)
NodeVersion string `json:"node_version"`
// Block header version
BlockHeaderVersion uint16 `json:"block_header_version"`
}
48 changes: 48 additions & 0 deletions api/types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2020 BlockCypher
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package api

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
)

func TestUnmarshalOutputType(t *testing.T) {
coinbaseb := []byte(`"Coinbase"`)
var inbound outputType
if err := json.Unmarshal(coinbaseb, &inbound); err != nil {
assert.Error(t, err)
}
assert.Equal(t, inbound, CoinbaseOutputType)

transactionb := []byte(`"Transaction"`)
var outbound outputType
if err := json.Unmarshal(transactionb, &outbound); err != nil {
assert.Error(t, err)
}
assert.Equal(t, outbound, TransactionOutputType)
}

func TestMarshalOutputType(t *testing.T) {
coinbaseb, err := json.Marshal(CoinbaseOutputType)
assert.Nil(t, err)
assert.Equal(t, string(coinbaseb), "\"Coinbase\"")

transactionb, err := json.Marshal(TransactionOutputType)
assert.Nil(t, err)
assert.Equal(t, string(transactionb), "\"Transaction\"")
}
85 changes: 0 additions & 85 deletions client/node_api.go

This file was deleted.

Loading

0 comments on commit 15e865d

Please sign in to comment.