-
Notifications
You must be signed in to change notification settings - Fork 43
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
Showing
17 changed files
with
1,175 additions
and
1,595 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# EditorConfig is awesome: https://EditorConfig.org | ||
|
||
# Top-most EditorConfig file. | ||
root = true | ||
|
||
# Unix-style newlines with a newline ending every file. | ||
[*.md] | ||
end_of_line = lf | ||
insert_final_newline = true | ||
max_line_length = 80 | ||
|
||
# 8 space indentation for Golang code. | ||
[*.go] | ||
indent_style = tab | ||
indent_size = 8 | ||
max_line_length = 80 |
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,123 @@ | ||
package lndclient | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"sync" | ||
"time" | ||
|
||
"github.com/btcsuite/btcd/chaincfg/chainhash" | ||
"github.com/btcsuite/btcd/wire" | ||
"github.com/lightningnetwork/lnd/lnrpc/chainrpc" | ||
"google.golang.org/grpc" | ||
) | ||
|
||
// ChainKitClient exposes chain functionality. | ||
type ChainKitClient interface { | ||
// GetBlock returns a block given the corresponding block hash. | ||
GetBlock(ctx context.Context, hash chainhash.Hash) (*wire.MsgBlock, | ||
error) | ||
|
||
// GetBestBlock returns the latest block hash and current height of the | ||
// valid most-work chain. | ||
GetBestBlock(ctx context.Context) (chainhash.Hash, int32, error) | ||
|
||
// GetBlockHash returns the hash of the block in the best blockchain | ||
// at the given height. | ||
GetBlockHash(ctx context.Context, blockHeight int64) (chainhash.Hash, | ||
error) | ||
} | ||
|
||
type chainKitClient struct { | ||
client chainrpc.ChainKitClient | ||
chainMac serializedMacaroon | ||
timeout time.Duration | ||
|
||
wg sync.WaitGroup | ||
} | ||
|
||
func newChainKitClient(conn grpc.ClientConnInterface, | ||
chainMac serializedMacaroon, timeout time.Duration) *chainKitClient { | ||
|
||
return &chainKitClient{ | ||
client: chainrpc.NewChainKitClient(conn), | ||
chainMac: chainMac, | ||
timeout: timeout, | ||
} | ||
} | ||
|
||
func (s *chainKitClient) WaitForFinished() { | ||
s.wg.Wait() | ||
} | ||
|
||
// GetBlock returns a block given the corresponding block hash. | ||
func (s *chainKitClient) GetBlock(ctxParent context.Context, | ||
hash chainhash.Hash) (*wire.MsgBlock, error) { | ||
|
||
ctx, cancel := context.WithTimeout(ctxParent, s.timeout) | ||
defer cancel() | ||
|
||
macaroonAuth := s.chainMac.WithMacaroonAuth(ctx) | ||
req := &chainrpc.GetBlockRequest{ | ||
BlockHash: hash[:], | ||
} | ||
resp, err := s.client.GetBlock(macaroonAuth, req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Convert raw block bytes into wire.MsgBlock. | ||
msgBlock := &wire.MsgBlock{} | ||
blockReader := bytes.NewReader(resp.RawBlock) | ||
err = msgBlock.Deserialize(blockReader) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return msgBlock, nil | ||
} | ||
|
||
// GetBestBlock returns the block hash and current height from the valid | ||
// most-work chain. | ||
func (s *chainKitClient) GetBestBlock(ctxParent context.Context) (chainhash.Hash, | ||
int32, error) { | ||
|
||
ctx, cancel := context.WithTimeout(ctxParent, s.timeout) | ||
defer cancel() | ||
|
||
macaroonAuth := s.chainMac.WithMacaroonAuth(ctx) | ||
resp, err := s.client.GetBestBlock( | ||
macaroonAuth, &chainrpc.GetBestBlockRequest{}, | ||
) | ||
if err != nil { | ||
return chainhash.Hash{}, 0, err | ||
} | ||
|
||
// Cast gRPC block hash bytes as chain hash type. | ||
var blockHash chainhash.Hash | ||
copy(blockHash[:], resp.BlockHash) | ||
|
||
return blockHash, resp.BlockHeight, nil | ||
} | ||
|
||
// GetBlockHash returns the hash of the block in the best blockchain at the | ||
// given height. | ||
func (s *chainKitClient) GetBlockHash(ctxParent context.Context, | ||
blockHeight int64) (chainhash.Hash, error) { | ||
|
||
ctx, cancel := context.WithTimeout(ctxParent, s.timeout) | ||
defer cancel() | ||
|
||
macaroonAuth := s.chainMac.WithMacaroonAuth(ctx) | ||
req := &chainrpc.GetBlockHashRequest{BlockHeight: blockHeight} | ||
resp, err := s.client.GetBlockHash(macaroonAuth, req) | ||
if err != nil { | ||
return chainhash.Hash{}, err | ||
} | ||
|
||
// Cast gRPC block hash bytes as chain hash type. | ||
var blockHash chainhash.Hash | ||
copy(blockHash[:], resp.BlockHash) | ||
|
||
return blockHash, nil | ||
} |
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
Oops, something went wrong.