-
Notifications
You must be signed in to change notification settings - Fork 8.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FAB-9591] Make active transactions an object
Change-Id: I5e8dd612ae2744da843479ec4d5b7c59e7a1780f Signed-off-by: Matthew Sykes <[email protected]>
- Loading branch information
Showing
4 changed files
with
107 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
Copyright IBM Corp. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package chaincode | ||
|
||
import "sync" | ||
|
||
func NewTxKey(channelID, txID string) string { return channelID + txID } | ||
|
||
type ActiveTransactions struct { | ||
mutex sync.Mutex | ||
ids map[string]struct{} | ||
} | ||
|
||
func NewActiveTransactions() *ActiveTransactions { | ||
return &ActiveTransactions{ | ||
ids: map[string]struct{}{}, | ||
} | ||
} | ||
|
||
func (a *ActiveTransactions) Add(channelID, txID string) bool { | ||
key := NewTxKey(channelID, txID) | ||
a.mutex.Lock() | ||
defer a.mutex.Unlock() | ||
if _, ok := a.ids[key]; ok { | ||
return false | ||
} | ||
|
||
a.ids[key] = struct{}{} | ||
return true | ||
} | ||
|
||
func (a *ActiveTransactions) Remove(channelID, txID string) { | ||
key := NewTxKey(channelID, txID) | ||
a.mutex.Lock() | ||
delete(a.ids, key) | ||
a.mutex.Unlock() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
Copyright IBM Corp. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package chaincode_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hyperledger/fabric/core/chaincode" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestActiveTransactions(t *testing.T) { | ||
activeTx := chaincode.NewActiveTransactions() | ||
assert.NotNil(t, activeTx) | ||
|
||
// Add unique transactions | ||
ok := activeTx.Add("channel-id", "tx-id") | ||
assert.True(t, ok, "a new transaction should return true") | ||
ok = activeTx.Add("channel-id", "tx-id-2") | ||
assert.True(t, ok, "adding a different transaction id should return true") | ||
ok = activeTx.Add("channel-id-2", "tx-id") | ||
assert.True(t, ok, "adding a different channel-id should return true") | ||
|
||
// Attempt to add a transaction that already exists | ||
ok = activeTx.Add("channel-id", "tx-id") | ||
assert.False(t, ok, "attempting to an existing transaction should return false") | ||
|
||
// Remove existing and make sure the ID can be reused | ||
activeTx.Remove("channel-id", "tx-id") | ||
ok = activeTx.Add("channel-id", "tx-id") | ||
assert.True(t, ok, "using a an id that has been removed should return true") | ||
} | ||
|
||
func TestNewTxKey(t *testing.T) { | ||
tests := []struct { | ||
channelID string | ||
txID string | ||
result string | ||
}{ | ||
{"", "", ""}, | ||
{"", "tx-1", "tx-1"}, | ||
{"chan-1", "", "chan-1"}, | ||
{"chan-1", "tx-1", "chan-1tx-1"}, | ||
} | ||
for _, tc := range tests { | ||
result := chaincode.NewTxKey(tc.channelID, tc.txID) | ||
assert.Equal(t, tc.result, result) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters