Skip to content

Commit cb46696

Browse files
committed
Fix history panic on config blocks
Getting a panic error when committer receives a config block and attempts to write the tran to history db. Fixed by only writing to history db for endorsement transactions. Change-Id: I32d5e414199d85355eeae84f3e1ae680fe0811f0 Signed-off-by: denyeart <[email protected]>
1 parent 0920c5e commit cb46696

File tree

2 files changed

+53
-22
lines changed

2 files changed

+53
-22
lines changed

core/ledger/kvledger/history/historydb/historyleveldb/historyleveldb.go

+38-22
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
"github.com/hyperledger/fabric/core/ledger/ledgerconfig"
2525
"github.com/hyperledger/fabric/core/ledger/util/leveldbhelper"
2626
"github.com/hyperledger/fabric/protos/common"
27-
"github.com/hyperledger/fabric/protos/utils"
27+
putils "github.com/hyperledger/fabric/protos/utils"
2828
logging "github.com/op/go-logging"
2929
)
3030

@@ -97,41 +97,57 @@ func (historyDB *historyDB) Commit(block *common.Block) error {
9797
//TODO add check for invalid trans in bit array
9898
for _, envBytes := range block.Data.Data {
9999
tranNo++
100-
logger.Debugf("Updating history for tranNo: %v", tranNo)
101100

102-
// extract actions from the envelope message
103-
respPayload, err := utils.GetActionFromEnvelope(envBytes)
101+
env, err := putils.GetEnvelopeFromBlock(envBytes)
104102
if err != nil {
105103
return err
106104
}
107105

108-
//preparation for extracting RWSet from transaction
109-
txRWSet := &rwset.TxReadWriteSet{}
110-
111-
// Get the Result from the Action and then Unmarshal
112-
// it into a TxReadWriteSet using custom unmarshalling
113-
if err = txRWSet.Unmarshal(respPayload.Results); err != nil {
106+
payload, err := putils.GetPayload(env)
107+
if err != nil {
114108
return err
115109
}
116110

117-
// for each transaction, loop through the namespaces and writesets
118-
// and add a history record for each write
119-
for _, nsRWSet := range txRWSet.NsRWs {
120-
ns := nsRWSet.NameSpace
111+
if common.HeaderType(payload.Header.ChainHeader.Type) == common.HeaderType_ENDORSER_TRANSACTION {
121112

122-
for _, kvWrite := range nsRWSet.Writes {
123-
writeKey := kvWrite.Key
113+
logger.Debugf("Updating history for tranNo: %d", tranNo)
114+
// extract actions from the envelope message
115+
respPayload, err := putils.GetActionFromEnvelope(envBytes)
116+
if err != nil {
117+
return err
118+
}
124119

125-
logger.Debugf("Writing history record for: ns=%s, key=%s, blockNo=%d tranNo=%d",
126-
ns, writeKey, blockNo, tranNo)
120+
//preparation for extracting RWSet from transaction
121+
txRWSet := &rwset.TxReadWriteSet{}
122+
123+
// Get the Result from the Action and then Unmarshal
124+
// it into a TxReadWriteSet using custom unmarshalling
125+
if err = txRWSet.Unmarshal(respPayload.Results); err != nil {
126+
return err
127+
}
128+
// for each transaction, loop through the namespaces and writesets
129+
// and add a history record for each write
130+
for _, nsRWSet := range txRWSet.NsRWs {
131+
ns := nsRWSet.NameSpace
127132

128-
//composite key for history records is in the form ns~key~blockNo~tranNo
129-
compositeHistoryKey := historydb.ConstructCompositeHistoryKey(ns, writeKey, blockNo, tranNo)
133+
for _, kvWrite := range nsRWSet.Writes {
134+
writeKey := kvWrite.Key
130135

131-
// No value is required, write an empty byte array (emptyValue) since Put() of nil is not allowed
132-
dbBatch.Put(compositeHistoryKey, emptyValue)
136+
logger.Debugf("Writing history record for: ns=%s, key=%s, blockNo=%d tranNo=%d",
137+
ns, writeKey, blockNo, tranNo)
138+
139+
//composite key for history records is in the form ns~key~blockNo~tranNo
140+
compositeHistoryKey := historydb.ConstructCompositeHistoryKey(ns, writeKey, blockNo, tranNo)
141+
142+
// No value is required, write an empty byte array (emptyValue) since Put() of nil is not allowed
143+
dbBatch.Put(compositeHistoryKey, emptyValue)
144+
}
133145
}
146+
147+
} else {
148+
logger.Debugf("Skipping transaction %d since it is not an endorsement transaction\n", tranNo)
134149
}
150+
135151
}
136152

137153
// add savepoint for recovery purpose

core/ledger/kvledger/history/historydb/historyleveldb/historyleveldb_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"os"
2121
"testing"
2222

23+
configtxtest "github.com/hyperledger/fabric/common/configtx/test"
2324
"github.com/hyperledger/fabric/core/ledger"
2425
"github.com/hyperledger/fabric/core/ledger/testutil"
2526
"github.com/spf13/viper"
@@ -125,3 +126,17 @@ func TestHistoryDisabled(t *testing.T) {
125126
_, err2 := qhistory.GetHistoryForKey("ns1", "key7")
126127
testutil.AssertError(t, err2, "Error should have been returned for GetHistoryForKey() when history disabled")
127128
}
129+
130+
//TestGenesisBlockNoError tests that Genesis blocks are ignored by history processing
131+
// since we only persist history of chaincode key writes
132+
func TestGenesisBlockNoError(t *testing.T) {
133+
134+
env := NewTestHistoryEnv(t)
135+
defer env.cleanup()
136+
137+
block, err := configtxtest.MakeGenesisBlock("test_chainid")
138+
testutil.AssertNoError(t, err, "")
139+
140+
err = env.TestHistoryDB.Commit(block)
141+
testutil.AssertNoError(t, err, "")
142+
}

0 commit comments

Comments
 (0)