Refactor names in interface BlockChainParser, added method IsUTXOChain
parent
f0552ec4ad
commit
dda80bd074
|
@ -33,19 +33,19 @@ func GetChainParams(chain string) *chaincfg.Params {
|
|||
return &chaincfg.MainNetParams
|
||||
}
|
||||
|
||||
func (p *BitcoinBlockParser) GetUIDFromVout(output *bchain.Vout) string {
|
||||
func (p *BitcoinBlockParser) GetAddrIDFromVout(output *bchain.Vout) string {
|
||||
return output.ScriptPubKey.Hex
|
||||
}
|
||||
|
||||
func (p *BitcoinBlockParser) GetUIDFromAddress(address string) ([]byte, error) {
|
||||
func (p *BitcoinBlockParser) GetAddrIDFromAddress(address string) ([]byte, error) {
|
||||
return p.AddressToOutputScript(address)
|
||||
}
|
||||
|
||||
func (p *BitcoinBlockParser) PackUID(str string) ([]byte, error) {
|
||||
func (p *BitcoinBlockParser) PackAddrID(str string) ([]byte, error) {
|
||||
return hex.DecodeString(str)
|
||||
}
|
||||
|
||||
func (p *BitcoinBlockParser) UnpackUID(buf []byte) string {
|
||||
func (p *BitcoinBlockParser) UnpackAddrID(buf []byte) string {
|
||||
return hex.EncodeToString(buf)
|
||||
}
|
||||
|
||||
|
@ -177,3 +177,7 @@ func (p *BitcoinBlockParser) UnpackTx(buf []byte) (*bchain.Tx, uint32, error) {
|
|||
tx.Blocktime = bt
|
||||
return tx, height, nil
|
||||
}
|
||||
|
||||
func (p *BitcoinBlockParser) IsUTXOChain() bool {
|
||||
return true
|
||||
}
|
||||
|
|
|
@ -26,21 +26,21 @@ func GetChainParams(chain string) *chaincfg.Params {
|
|||
return &chaincfg.MainNetParams
|
||||
}
|
||||
|
||||
func (p *ZCashBlockParser) GetUIDFromVout(output *bchain.Vout) string {
|
||||
func (p *ZCashBlockParser) GetAddrIDFromVout(output *bchain.Vout) string {
|
||||
if len(output.ScriptPubKey.Addresses) != 1 {
|
||||
return ""
|
||||
}
|
||||
return output.ScriptPubKey.Addresses[0]
|
||||
}
|
||||
|
||||
func (p *ZCashBlockParser) GetUIDFromAddress(address string) ([]byte, error) {
|
||||
return p.PackUID(address)
|
||||
func (p *ZCashBlockParser) GetAddrIDFromAddress(address string) ([]byte, error) {
|
||||
return p.PackAddrID(address)
|
||||
}
|
||||
|
||||
func (p *ZCashBlockParser) PackUID(str string) ([]byte, error) {
|
||||
func (p *ZCashBlockParser) PackAddrID(str string) ([]byte, error) {
|
||||
return []byte(str), nil
|
||||
}
|
||||
|
||||
func (p *ZCashBlockParser) UnpackUID(buf []byte) string {
|
||||
func (p *ZCashBlockParser) UnpackAddrID(buf []byte) string {
|
||||
return string(buf)
|
||||
}
|
||||
|
|
|
@ -28,7 +28,7 @@ type Mempool struct {
|
|||
chain BlockChain
|
||||
mux sync.Mutex
|
||||
txToInputOutput map[string]inputOutput
|
||||
scriptToTx map[string][]outpoint // TODO rename all occurences
|
||||
addrIDToTx map[string][]outpoint
|
||||
inputs map[outpoint]string
|
||||
}
|
||||
|
||||
|
@ -42,12 +42,12 @@ func (m *Mempool) GetTransactions(address string) ([]string, error) {
|
|||
m.mux.Lock()
|
||||
defer m.mux.Unlock()
|
||||
parser := m.chain.GetChainParser()
|
||||
buf, err := parser.GetUIDFromAddress(address)
|
||||
buf, err := parser.GetAddrIDFromAddress(address)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
outid := parser.UnpackUID(buf)
|
||||
outpoints := m.scriptToTx[outid]
|
||||
addrID := parser.UnpackAddrID(buf)
|
||||
outpoints := m.addrIDToTx[addrID]
|
||||
txs := make([]string, 0, len(outpoints)+len(outpoints)/2)
|
||||
for _, o := range outpoints {
|
||||
txs = append(txs, o.txid)
|
||||
|
@ -69,7 +69,7 @@ func (m *Mempool) updateMappings(newTxToInputOutput map[string]inputOutput, newS
|
|||
m.mux.Lock()
|
||||
defer m.mux.Unlock()
|
||||
m.txToInputOutput = newTxToInputOutput
|
||||
m.scriptToTx = newScriptToTx
|
||||
m.addrIDToTx = newScriptToTx
|
||||
m.inputs = newInputs
|
||||
}
|
||||
|
||||
|
@ -85,7 +85,7 @@ func (m *Mempool) Resync(onNewTxAddr func(txid string, addr string)) error {
|
|||
}
|
||||
parser := m.chain.GetChainParser()
|
||||
newTxToInputOutput := make(map[string]inputOutput, len(m.txToInputOutput)+1)
|
||||
newScriptToTx := make(map[string][]outpoint, len(m.scriptToTx)+1)
|
||||
newAddrIDToTx := make(map[string][]outpoint, len(m.addrIDToTx)+1)
|
||||
newInputs := make(map[outpoint]string, len(m.inputs)+1)
|
||||
for _, txid := range txs {
|
||||
io, exists := m.txToInputOutput[txid]
|
||||
|
@ -97,9 +97,9 @@ func (m *Mempool) Resync(onNewTxAddr func(txid string, addr string)) error {
|
|||
}
|
||||
io.outputs = make([]scriptIndex, 0, len(tx.Vout))
|
||||
for _, output := range tx.Vout {
|
||||
outid := parser.GetUIDFromVout(&output)
|
||||
if outid != "" {
|
||||
io.outputs = append(io.outputs, scriptIndex{outid, output.N})
|
||||
addrID := parser.GetAddrIDFromVout(&output)
|
||||
if addrID != "" {
|
||||
io.outputs = append(io.outputs, scriptIndex{addrID, output.N})
|
||||
}
|
||||
if onNewTxAddr != nil && len(output.ScriptPubKey.Addresses) == 1 {
|
||||
onNewTxAddr(tx.Txid, output.ScriptPubKey.Addresses[0])
|
||||
|
@ -115,13 +115,13 @@ func (m *Mempool) Resync(onNewTxAddr func(txid string, addr string)) error {
|
|||
}
|
||||
newTxToInputOutput[txid] = io
|
||||
for _, si := range io.outputs {
|
||||
newScriptToTx[si.script] = append(newScriptToTx[si.script], outpoint{txid, si.n})
|
||||
newAddrIDToTx[si.script] = append(newAddrIDToTx[si.script], outpoint{txid, si.n})
|
||||
}
|
||||
for _, i := range io.inputs {
|
||||
newInputs[i] = txid
|
||||
}
|
||||
}
|
||||
m.updateMappings(newTxToInputOutput, newScriptToTx, newInputs)
|
||||
m.updateMappings(newTxToInputOutput, newAddrIDToTx, newInputs)
|
||||
glog.Info("Mempool: resync finished in ", time.Since(start), ", ", len(m.txToInputOutput), " transactions in mempool")
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -112,14 +112,22 @@ type BlockChain interface {
|
|||
}
|
||||
|
||||
type BlockChainParser interface {
|
||||
GetUIDFromVout(output *Vout) string
|
||||
GetUIDFromAddress(address string) ([]byte, error)
|
||||
PackUID(script string) ([]byte, error)
|
||||
UnpackUID(buf []byte) string
|
||||
// self description
|
||||
// UTXO chains need "inputs" column in db, that map transactions to transactions that spend them
|
||||
// non UTXO chains have mapping of address to input and output transactions directly in "outputs" column in db
|
||||
IsUTXOChain() bool
|
||||
// address id conversions
|
||||
GetAddrIDFromVout(output *Vout) string
|
||||
GetAddrIDFromAddress(address string) ([]byte, error)
|
||||
PackAddrID(addrID string) ([]byte, error)
|
||||
UnpackAddrID(buf []byte) string
|
||||
// address to output script conversions
|
||||
AddressToOutputScript(address string) ([]byte, error)
|
||||
OutputScriptToAddresses(script []byte) ([]string, error)
|
||||
// transactions
|
||||
ParseTx(b []byte) (*Tx, error)
|
||||
ParseBlock(b []byte) (*Block, error)
|
||||
PackTx(tx *Tx, height uint32, blockTime int64) ([]byte, error)
|
||||
UnpackTx(buf []byte) (*Tx, uint32, error)
|
||||
// blocks
|
||||
ParseBlock(b []byte) (*Block, error)
|
||||
}
|
||||
|
|
|
@ -142,13 +142,13 @@ func (d *RocksDB) GetTransactions(address string, lower uint32, higher uint32, f
|
|||
if glog.V(1) {
|
||||
glog.Infof("rocksdb: address get %s %d-%d ", address, lower, higher)
|
||||
}
|
||||
outid, err := d.chainParser.GetUIDFromAddress(address)
|
||||
addrID, err := d.chainParser.GetAddrIDFromAddress(address)
|
||||
|
||||
kstart, err := packOutputKey(outid, lower)
|
||||
kstart, err := packOutputKey(addrID, lower)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
kstop, err := packOutputKey(outid, higher)
|
||||
kstop, err := packOutputKey(addrID, higher)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -241,12 +241,12 @@ func (d *RocksDB) writeOutputs(wb *gorocksdb.WriteBatch, block *bchain.Block, op
|
|||
|
||||
for _, tx := range block.Txs {
|
||||
for _, output := range tx.Vout {
|
||||
outid := d.chainParser.GetUIDFromVout(&output)
|
||||
if outid != "" {
|
||||
if len(outid) > 1024 {
|
||||
glog.Infof("block %d, skipping outid of length %d", block.Height, len(outid)/2)
|
||||
addrID := d.chainParser.GetAddrIDFromVout(&output)
|
||||
if addrID != "" {
|
||||
if len(addrID) > 1024 {
|
||||
glog.Infof("block %d, skipping addrID of length %d", block.Height, len(addrID)/2)
|
||||
} else {
|
||||
records[outid] = append(records[outid], outpoint{
|
||||
records[addrID] = append(records[addrID], outpoint{
|
||||
txid: tx.Txid,
|
||||
vout: output.N,
|
||||
})
|
||||
|
@ -263,25 +263,25 @@ func (d *RocksDB) writeOutputs(wb *gorocksdb.WriteBatch, block *bchain.Block, op
|
|||
}
|
||||
}
|
||||
|
||||
for outid, outpoints := range records {
|
||||
bOutid, err := d.chainParser.PackUID(outid)
|
||||
for addrID, outpoints := range records {
|
||||
bOutid, err := d.chainParser.PackAddrID(addrID)
|
||||
if err != nil {
|
||||
glog.Warningf("rocksdb: packUID: %v - %d %s", err, block.Height, outid)
|
||||
glog.Warningf("rocksdb: addrID: %v - %d %s", err, block.Height, addrID)
|
||||
continue
|
||||
}
|
||||
key, err := packOutputKey(bOutid, block.Height)
|
||||
if err != nil {
|
||||
glog.Warningf("rocksdb: packOutputKey: %v - %d %s", err, block.Height, outid)
|
||||
continue
|
||||
}
|
||||
val, err := packOutputValue(outpoints)
|
||||
if err != nil {
|
||||
glog.Warningf("rocksdb: packOutputValue: %v", err)
|
||||
glog.Warningf("rocksdb: packOutputKey: %v - %d %s", err, block.Height, addrID)
|
||||
continue
|
||||
}
|
||||
|
||||
switch op {
|
||||
case opInsert:
|
||||
val, err := packOutputValue(outpoints)
|
||||
if err != nil {
|
||||
glog.Warningf("rocksdb: packOutputValue: %v", err)
|
||||
continue
|
||||
}
|
||||
wb.PutCF(d.cfh[cfOutputs], key, val)
|
||||
case opDelete:
|
||||
wb.DeleteCF(d.cfh[cfOutputs], key)
|
||||
|
|
Loading…
Reference in New Issue