From d58302872111196e9c1b943fc4de6d982702d18d Mon Sep 17 00:00:00 2001 From: Martin Boehm Date: Fri, 14 Feb 2020 10:04:12 +0100 Subject: [PATCH] Check the order of utxos --- db/rocksdb.go | 12 ++++++++++-- db/rocksdb_test.go | 22 +++++++++++----------- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/db/rocksdb.go b/db/rocksdb.go index 6b38dd5e..defe0c29 100644 --- a/db/rocksdb.go +++ b/db/rocksdb.go @@ -1477,7 +1477,7 @@ func (d *RocksDB) disconnectBlock(height uint32, blockTxs []blockTxs) error { sBtxID := string(btxID) a, exist := blockAddressesTxs[sAddrDesc] if !exist { - blockAddressesTxs[sAddrDesc] = map[string]struct{}{sBtxID: struct{}{}} + blockAddressesTxs[sAddrDesc] = map[string]struct{}{sBtxID: {}} } else { _, exist = a[sBtxID] if !exist { @@ -1851,8 +1851,16 @@ func (d *RocksDB) ComputeInternalStateColumnStats(stopCompute chan os.Signal) er func (d *RocksDB) fixUtxo(addrDesc bchain.AddressDescriptor, ba *AddrBalance) (bool, error) { var checksum big.Int + var prevUtxo *Utxo for i := range ba.Utxos { - checksum.Add(&checksum, &ba.Utxos[i].ValueSat) + utxo := &ba.Utxos[i] + checksum.Add(&checksum, &utxo.ValueSat) + if prevUtxo != nil { + if prevUtxo.Vout > utxo.Vout && *(*int)(unsafe.Pointer(&utxo.BtxID[0])) == *(*int)(unsafe.Pointer(&prevUtxo.BtxID[0])) && bytes.Equal(utxo.BtxID, prevUtxo.BtxID) { + glog.Error("FixUtxo: addrDesc ", addrDesc, ", needs reorder") + } + } + prevUtxo = utxo } if checksum.Cmp(&ba.BalanceSat) != 0 { var checksumFromTxs big.Int diff --git a/db/rocksdb_test.go b/db/rocksdb_test.go index 2f932120..75ae1c52 100644 --- a/db/rocksdb_test.go +++ b/db/rocksdb_test.go @@ -1148,19 +1148,19 @@ func TestAddrBalance_utxo_methods(t *testing.T) { SentSat: *big.NewInt(10000), BalanceSat: *big.NewInt(1000), Utxos: []Utxo{ - Utxo{ + { BtxID: hexToBytes(dbtestdata.TxidB1T1), Vout: 1, Height: 5000, ValueSat: *big.NewInt(100), }, - Utxo{ + { BtxID: hexToBytes(dbtestdata.TxidB1T1), Vout: 4, Height: 5000, ValueSat: *big.NewInt(100), }, - Utxo{ + { BtxID: hexToBytes(dbtestdata.TxidB1T2), Vout: 0, Height: 5001, @@ -1208,49 +1208,49 @@ func TestAddrBalance_utxo_methods(t *testing.T) { SentSat: *big.NewInt(10000), BalanceSat: *big.NewInt(1000), Utxos: []Utxo{ - Utxo{ + { BtxID: hexToBytes(dbtestdata.TxidB1T1), Vout: 0, Height: 5000, ValueSat: *big.NewInt(100), }, - Utxo{ + { BtxID: hexToBytes(dbtestdata.TxidB1T1), Vout: 1, Height: 5000, ValueSat: *big.NewInt(100), }, - Utxo{ + { BtxID: hexToBytes(dbtestdata.TxidB1T1), Vout: 2, Height: 5000, ValueSat: *big.NewInt(100), }, - Utxo{ + { BtxID: hexToBytes(dbtestdata.TxidB1T1), Vout: 4, Height: 5000, ValueSat: *big.NewInt(100), }, - Utxo{ + { BtxID: hexToBytes(dbtestdata.TxidB1T1), Vout: 10, Height: 5000, ValueSat: *big.NewInt(100), }, - Utxo{ + { BtxID: hexToBytes(dbtestdata.TxidB1T2), Vout: 0, Height: 5001, ValueSat: *big.NewInt(800), }, - Utxo{ + { BtxID: hexToBytes(dbtestdata.TxidB2T1), Vout: 0, Height: 5003, ValueSat: *big.NewInt(800), }, - Utxo{ + { BtxID: hexToBytes(dbtestdata.TxidB2T1), Vout: 1, Height: 5003,