From 9f0e66ee3f3f2d2f45260e5558cb70b43d598fcf Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Sat, 1 Jul 2017 01:37:24 -0500 Subject: [PATCH] blockchain: Correct invalid assertion. This corrects the assertion in the decodeSpentTxOut function so it does not improperly cause a panic when unwinding transactions during a reorg under certain circumstances. In particular, the provided transaction version that is passed when a stxo entry does not exist is now -1 in order to properly distinguish it from the zero value. It also updates the tests accordingly. This was discovered by the reorg on testnet from block 00000000000018c58c2d2816f03dac327d975a18af6edf1a369df67ecddaf816 to 0000000000001c1161a367156465cc6226e9f862d9c585f94db5779fdf5455ff. --- blockchain/chainio.go | 4 ++-- blockchain/chainio_test.go | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/blockchain/chainio.go b/blockchain/chainio.go index b34492de..dccedad2 100644 --- a/blockchain/chainio.go +++ b/blockchain/chainio.go @@ -298,7 +298,7 @@ func decodeSpentTxOut(serialized []byte, stxo *spentTxOut, txVersion int32) (int // it. This should never happen unless there is database // corruption or this function is being called without the // proper state. - if txVersion == 0 { + if txVersion == -1 { return offset, AssertError("decodeSpentTxOut called " + "without a containing tx version when the " + "serialized stxo that does not encode the " + @@ -382,7 +382,7 @@ func deserializeSpendJournalEntry(serialized []byte, txns []*wire.MsgTx, view *U // to detect this case and pull the tx version from the // entry that contains the version information as just // described. - var txVersion int32 + txVersion := int32(-1) originHash := &txIn.PreviousOutPoint.Hash entry := view.LookupEntry(originHash) if entry != nil { diff --git a/blockchain/chainio_test.go b/blockchain/chainio_test.go index 7020923d..a3fcb5af 100644 --- a/blockchain/chainio_test.go +++ b/blockchain/chainio_test.go @@ -194,8 +194,9 @@ func TestStxoDecodeErrors(t *testing.T) { bytesRead: 2, }, { - name: "no serialized tx version and passed 0", + name: "no serialized tx version and passed -1", stxo: spentTxOut{}, + txVersion: -1, serialized: hexToBytes("003205"), errType: AssertError(""), bytesRead: 1,