Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change StartWriteBatch #140

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions shim/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -544,17 +544,17 @@ 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) (isBatchEnabled bool) {
if !h.usePeerWriteBatch {
return errors.New("peer does not support write batch")
return false
}

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

h.startWriteBatch[txCtxID] = true
return nil
return true
}

func (h *Handler) handleFinishWriteBatch(channelID string, txID string) error {
Expand Down
3 changes: 2 additions & 1 deletion shim/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,8 +374,9 @@ type ChaincodeStubInterface interface {
// StartWriteBatch enables a mode where all changes are not immediately forwarded to the feast,
// but accumulate in the cache. The cache is sent in large batches either at the end of transaction
// execution or after the FinishWriteBatch call.
// StartWriteBatch returns a bool indication (isBatchEnabled) that the peer supports or does not support writing by batches.
// IMPORTANT: in this mode, the expected order of transaction execution and expected errors can be changed.
StartWriteBatch() error
StartWriteBatch() (isBatchEnabled bool)

// FinishWriteBatch sends accumulated changes in large batches to the peer
// if StartWriteBatch has been called before it.
Expand Down
2 changes: 1 addition & 1 deletion shim/stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ func (s *ChaincodeStub) GetQueryResultWithPagination(query string, pageSize int3
// --------- Batch State functions ----------

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

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
Loading