Refactor chain API into package

pull/3/head
Oliver Gugger 4 years ago
parent 759d9f60e0
commit cf119ed4e5
No known key found for this signature in database
GPG Key ID: 8E4256593F177720

@ -1,4 +1,4 @@
package chantools
package chain
import (
"bytes"
@ -13,66 +13,66 @@ var (
ErrTxNotFound = errors.New("transaction not found")
)
type chainApi struct {
baseUrl string
type Api struct {
BaseUrl string
}
type transaction struct {
Vin []*vin `json:"vin"`
Vout []*vout `json:"vout"`
type TX struct {
Vin []*Vin `json:"vin"`
Vout []*Vout `json:"vout"`
}
type vin struct {
type Vin struct {
Tixid string `json:"txid"`
Vout int `json:"vout"`
Prevout *vout `json:"prevout"`
Prevout *Vout `json:"prevout"`
Sequence uint32 `json:"sequence"`
}
type vout struct {
type Vout struct {
ScriptPubkey string `json:"scriptpubkey"`
ScriptPubkeyAsm string `json:"scriptpubkey_asm"`
ScriptPubkeyType string `json:"scriptpubkey_type"`
ScriptPubkeyAddr string `json:"scriptpubkey_address"`
Value uint64 `json:"value"`
outspend *outspend
Outspend *Outspend
}
type outspend struct {
type Outspend struct {
Spent bool `json:"spent"`
Txid string `json:"txid"`
Vin int `json:"vin"`
Status *status `json:"status"`
Status *Status `json:"status"`
}
type status struct {
type Status struct {
Confirmed bool `json:"confirmed"`
BlockHeight int `json:"block_height"`
BlockHash string `json:"block_hash"`
}
func (a *chainApi) Transaction(txid string) (*transaction, error) {
tx := &transaction{}
err := Fetch(fmt.Sprintf("%s/tx/%s", a.baseUrl, txid), tx)
func (a *Api) Transaction(txid string) (*TX, error) {
tx := &TX{}
err := Fetch(fmt.Sprintf("%s/tx/%s", a.BaseUrl, txid), tx)
if err != nil {
return nil, err
}
for idx, vout := range tx.Vout {
url := fmt.Sprintf(
"%s/tx/%s/outspend/%d", a.baseUrl, txid, idx,
"%s/tx/%s/outspend/%d", a.BaseUrl, txid, idx,
)
outspend := outspend{}
outspend := Outspend{}
err := Fetch(url, &outspend)
if err != nil {
return nil, err
}
vout.outspend = &outspend
vout.Outspend = &outspend
}
return tx, nil
}
func (a *chainApi) PublishTx(rawTxHex string) (string, error) {
url := fmt.Sprintf("%s/tx", a.baseUrl)
func (a *Api) PublishTx(rawTxHex string) (string, error) {
url := fmt.Sprintf("%s/tx", a.BaseUrl)
resp, err := http.Post(url, "text/plain", strings.NewReader(rawTxHex))
if err != nil {
return "", err

@ -12,6 +12,7 @@ import (
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil/hdkeychain"
"github.com/guggero/chantools/chain"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/input"
)
@ -23,7 +24,7 @@ func forceCloseChannels(extendedKey *hdkeychain.ExtendedKey,
if err != nil {
return err
}
chainApi := &chainApi{baseUrl: cfg.ApiUrl}
chainApi := &chain.Api{BaseUrl: cfg.ApiUrl}
signer := &signer{extendedKey: extendedKey}
// Go through all channels in the DB, find the still open ones and

@ -17,8 +17,8 @@ import (
)
var (
cacheSize = 2000
cache []*cacheEntry
cacheSize = 2000
cache []*cacheEntry
errAddrNotFound = errors.New("addr not found")
)

@ -5,17 +5,19 @@ import (
"fmt"
"io/ioutil"
"time"
"github.com/guggero/chantools/chain"
)
func summarizeChannels(apiUrl string, channels []*SummaryEntry) error {
summaryFile := &SummaryEntryFile{
Channels: channels,
}
chainApi := &chainApi{baseUrl: apiUrl}
chainApi := &chain.Api{BaseUrl: apiUrl}
for idx, channel := range channels {
tx, err := chainApi.Transaction(channel.FundingTXID)
if err == ErrTxNotFound {
if err == chain.ErrTxNotFound {
log.Errorf("Funding TX %s not found. Ignoring.",
channel.FundingTXID)
channel.ChanExists = false
@ -27,7 +29,7 @@ func summarizeChannels(apiUrl string, channels []*SummaryEntry) error {
return err
}
channel.ChanExists = true
outspend := tx.Vout[channel.FundingTXIndex].outspend
outspend := tx.Vout[channel.FundingTXIndex].Outspend
if outspend.Spent {
summaryFile.ClosedChannels++
channel.ClosingTX = &ClosingTX{
@ -88,8 +90,8 @@ func summarizeChannels(apiUrl string, channels []*SummaryEntry) error {
return ioutil.WriteFile(fileName, summaryBytes, 0644)
}
func reportOutspend(api *chainApi, summaryFile *SummaryEntryFile,
entry *SummaryEntry, os *outspend) error {
func reportOutspend(api *chain.Api, summaryFile *SummaryEntryFile,
entry *SummaryEntry, os *chain.Outspend) error {
spendTx, err := api.Transaction(os.Txid)
if err != nil {
@ -97,9 +99,9 @@ func reportOutspend(api *chainApi, summaryFile *SummaryEntryFile,
}
summaryFile.FundsClosedChannels += entry.LocalBalance
var utxo []*vout
var utxo []*chain.Vout
for _, vout := range spendTx.Vout {
if !vout.outspend.Spent {
if !vout.Outspend.Spent {
utxo = append(utxo, vout)
}
}
@ -133,7 +135,7 @@ func reportOutspend(api *chainApi, summaryFile *SummaryEntryFile,
// Could maybe be brute forced.
if len(utxo) == 1 &&
utxo[0].ScriptPubkeyType == "v0_p2wpkh" &&
utxo[0].outspend.Spent == false {
utxo[0].Outspend.Spent == false {
entry.ClosingTX.OurAddr = utxo[0].ScriptPubkeyAddr
}
@ -148,7 +150,7 @@ func reportOutspend(api *chainApi, summaryFile *SummaryEntryFile,
// We don't know what this output is, logging for debug.
for idx, vout := range spendTx.Vout {
if !vout.outspend.Spent {
if !vout.Outspend.Spent {
log.Debugf("UTXO %d of type %s with "+
"value %d", idx,
vout.ScriptPubkeyType,
@ -169,7 +171,7 @@ func reportOutspend(api *chainApi, summaryFile *SummaryEntryFile,
return nil
}
func couldBeOurs(entry *SummaryEntry, utxo []*vout) bool {
func couldBeOurs(entry *SummaryEntry, utxo []*chain.Vout) bool {
if len(utxo) == 1 && utxo[0].Value == entry.RemoteBalance {
return false
}
@ -177,6 +179,6 @@ func couldBeOurs(entry *SummaryEntry, utxo []*vout) bool {
return entry.LocalBalance != 0
}
func isCoopClose(tx *transaction) bool {
func isCoopClose(tx *chain.TX) bool {
return tx.Vin[0].Sequence == 0xffffffff
}

@ -10,6 +10,7 @@ import (
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil/hdkeychain"
"github.com/guggero/chantools/chain"
"github.com/lightningnetwork/lnd/input"
)
@ -23,7 +24,7 @@ func sweepTimeLock(extendedKey *hdkeychain.ExtendedKey, apiUrl string,
// Create signer and transaction template.
signer := &signer{extendedKey: extendedKey}
chainApi := &chainApi{baseUrl: apiUrl}
chainApi := &chain.Api{BaseUrl: apiUrl}
sweepTx := wire.NewMsgTx(2)
totalOutputValue := int64(0)

Loading…
Cancel
Save