Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Final StakeContract UTXO API Extension #13

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all 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
33 changes: 30 additions & 3 deletions api/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package api
import (
"blockbook/bchain"
"blockbook/bchain/coins/eth"
"blockbook/bchain/coins/pivx"
"blockbook/common"
"blockbook/db"
"bytes"
Expand Down Expand Up @@ -54,6 +53,17 @@ func (w *Worker) getAddressesFromVout(vout *bchain.Vout) (bchain.AddressDescript
return addrDesc, a, s, err
}

// returns true if scriptPubKey is P2CS
func isP2CS(addrs []string) bool {
if len(addrs) != 2 {
return false
}
// dirty hack (to remove multisig false positives)
// !TODO: implement flag in Vin and Vout objects
return (len(addrs[0]) > 0 &&
(addrs[0][0:1] == "S" || addrs[0][0:1] == "W"))
}

// setSpendingTxToVout is helper function, that finds transaction that spent given output and sets it to the output
// there is no direct index for the operation, it must be found using addresses -> txaddresses -> tx
func (w *Worker) setSpendingTxToVout(vout *Vout, txid string, height uint32) error {
Expand Down Expand Up @@ -878,9 +888,13 @@ func (w *Worker) getAddrDescUtxo(addrDesc bchain.AddressDescriptor, ba *db.AddrB
vad, err := w.chainParser.GetAddrDescFromVout(vout)
if err == nil && bytes.Equal(addrDesc, vad) {
// report only outpoints that are not spent in mempool
stakeContract := false
_, e := spentInMempool[bchainTx.Txid+strconv.Itoa(i)]
addr := vout.ScriptPubKey.Addresses
if !e {
stakeContract := pivx.IsP2CSScript(addrDesc)
if isP2CS(addr) {
stakeContract = true
}
r = append(r, Utxo{
Txid: bchainTx.Txid,
Vout: int32(i),
Expand Down Expand Up @@ -919,7 +933,20 @@ func (w *Worker) getAddrDescUtxo(addrDesc bchain.AddressDescriptor, ba *db.AddrB
if err != nil {
return nil, err
}
stakeContract := pivx.IsP2CSScript(addrDesc)
stakeContract := false
ta, err := w.db.GetTxAddresses(txid)
if err != nil {
return nil, err
}
addr, _, err := ta.Outputs[utxo.Vout].Addresses(w.chainParser)
if err != nil {
return nil, err
}
if len(addr) > 1 {
if isP2CS(addr) {
stakeContract = true
}
}
_, e := spentInMempool[txid+strconv.Itoa(int(utxo.Vout))]
if !e {
r = append(r, Utxo{
Expand Down