Skip to content

Commit

Permalink
remove ResetWriteBatch
Browse files Browse the repository at this point in the history
Signed-off-by: Fedor Partanskiy <[email protected]>
  • Loading branch information
pfi79 committed Jan 17, 2025
1 parent 2fad4d7 commit 838571a
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 33 deletions.
17 changes: 2 additions & 15 deletions shim/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -544,35 +544,22 @@ func (h *Handler) sendBatch(channelID string, txid string) error {
return nil
}

func (h *Handler) handleStartWriteBatch(channelID string, txID string) error {
func (h *Handler) handleStartWriteBatch(channelID string, txID string) {
if !h.usePeerWriteBatch {
return errors.New("peer does not support write batch")
return
}

txCtxID := transactionContextID(channelID, txID)
h.startWriteBatchMutex.Lock()
defer h.startWriteBatchMutex.Unlock()

h.startWriteBatch[txCtxID] = true
return nil
}

func (h *Handler) handleFinishWriteBatch(channelID string, txID string) error {
return h.sendBatch(channelID, txID)
}

func (h *Handler) handleResetWriteBatch(channelID string, txID string) {
if !h.usePeerWriteBatch || !h.isStartWriteBatch(channelID, txID) {
return
}

txCtxID := transactionContextID(channelID, txID)

h.batchMutex.Lock()
delete(h.batch, txCtxID)
h.batchMutex.Unlock()
}

func (h *Handler) isStartWriteBatch(channelID string, txID string) bool {
txCtxID := transactionContextID(channelID, txID)
h.startWriteBatchMutex.RLock()
Expand Down
8 changes: 2 additions & 6 deletions shim/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,19 +371,15 @@ type ChaincodeStubInterface interface {
// The marshaled ChaincodeEvent will be available in the transaction's ChaincodeAction.events field.
SetEvent(name string, payload []byte) error

// StartWriteBatch enables a mode where all changes are not immediately forwarded to the feast,
// StartWriteBatch enables a mode where all changes are not immediately forwarded to the peer,
// but accumulate in the cache. The cache is sent in large batches either at the end of transaction
// execution or after the FinishWriteBatch call.
// IMPORTANT: in this mode, the expected order of transaction execution and expected errors can be changed.
StartWriteBatch() error
StartWriteBatch()

// FinishWriteBatch sends accumulated changes in large batches to the peer
// if StartWriteBatch has been called before it.
FinishWriteBatch() error

// ResetWriteBatch clears the accumulated segment of changes, but does not cancel the batches writing mode.
// After ResetWriteBatch, the changes will be written to the batches
ResetWriteBatch()
}

// CommonIteratorInterface allows a chaincode to check whether any more result
Expand Down
9 changes: 2 additions & 7 deletions shim/stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -582,20 +582,15 @@ func (s *ChaincodeStub) GetQueryResultWithPagination(query string, pageSize int3
// --------- Batch State functions ----------

// StartWriteBatch documentation can be found in interfaces.go
func (s *ChaincodeStub) StartWriteBatch() error {
return s.handler.handleStartWriteBatch(s.ChannelID, s.TxID)
func (s *ChaincodeStub) StartWriteBatch() {
s.handler.handleStartWriteBatch(s.ChannelID, s.TxID)
}

// FinishWriteBatch documentation can be found in interfaces.go
func (s *ChaincodeStub) FinishWriteBatch() error {
return s.handler.handleFinishWriteBatch(s.ChannelID, s.TxID)
}

// ResetWriteBatch documentation can be found in interfaces.go
func (s *ChaincodeStub) ResetWriteBatch() {
s.handler.handleResetWriteBatch(s.ChannelID, s.TxID)
}

// Next ...
func (iter *StateQueryIterator) Next() (*queryresult.KV, error) {
result, err := iter.nextResult(StateQueryResult)
Expand Down
10 changes: 5 additions & 5 deletions shim/stub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ func TestChaincodeStubHandlers(t *testing.T) {
resType: peer.ChaincodeMessage_RESPONSE,
payload: []byte("myvalue"),
testFunc: func(s *ChaincodeStub, h *Handler, t *testing.T, payload []byte) {
s.StartWriteBatch() //nolint:errcheck
s.StartWriteBatch()
err := s.PutState("key", payload)
assert.NoError(t, err)
err = s.PutPrivateData("col", "key", payload)
Expand All @@ -339,8 +339,8 @@ func TestChaincodeStubHandlers(t *testing.T) {
err = s.FinishWriteBatch()
assert.NoError(t, err)

s.StartWriteBatch() //nolint:errcheck
s.StartWriteBatch() //nolint:errcheck
s.StartWriteBatch()
s.StartWriteBatch()
err = s.PutState("key", payload)
assert.NoError(t, err)
err = s.PutPrivateData("col", "key", payload)
Expand Down Expand Up @@ -609,8 +609,8 @@ func TestChaincodeStubHandlers(t *testing.T) {
resp := s.InvokeChaincode("cc", [][]byte{}, "channel")
assert.Equal(t, payload, resp.GetPayload())

s.StartWriteBatch() //nolint:errcheck
s.StartWriteBatch() //nolint:errcheck
s.StartWriteBatch()
s.StartWriteBatch()
err = s.PutState("key", payload)
assert.NoError(t, err)
err = s.FinishWriteBatch()
Expand Down

0 comments on commit 838571a

Please sign in to comment.