Skip to content

Commit

Permalink
Add lock around dummy DA data (#21)
Browse files Browse the repository at this point in the history
* Add lock around dummy DA data

* Add concurrent read/write test

* Increase counter in concurrent test
  • Loading branch information
Manav-Aggarwal committed Nov 17, 2023
1 parent 57bc360 commit ee3b613
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
11 changes: 10 additions & 1 deletion test/dummy.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"crypto/sha256"
"encoding/binary"
"errors"
"sync"

"github.com/rollkit/go-da"
)
Expand All @@ -16,10 +17,11 @@ import (
// Data is stored in a map, where key is a serialized sequence number. This key is returned as ID.
// Commitments are simply hashes, and proofs are ED25519 signatures.
type DummyDA struct {
mu *sync.Mutex // protects data and height
data map[uint64][]kvp
height uint64
privKey ed25519.PrivateKey
pubKey ed25519.PublicKey
height uint64
}

type kvp struct {
Expand All @@ -29,6 +31,7 @@ type kvp struct {
// NewDummyDA create new instance of DummyDA
func NewDummyDA() *DummyDA {
da := &DummyDA{
mu: new(sync.Mutex),
data: make(map[uint64][]kvp),
}
da.pubKey, da.privKey, _ = ed25519.GenerateKey(rand.Reader)
Expand All @@ -39,6 +42,8 @@ var _ da.DA = &DummyDA{}

// Get returns Blobs for given IDs.
func (d *DummyDA) Get(ids []da.ID) ([]da.Blob, error) {
d.mu.Lock()
defer d.mu.Unlock()
blobs := make([]da.Blob, len(ids))
for i, id := range ids {
if len(id) < 8 {
Expand All @@ -61,6 +66,8 @@ func (d *DummyDA) Get(ids []da.ID) ([]da.Blob, error) {

// GetIDs returns IDs of Blobs at given DA height.
func (d *DummyDA) GetIDs(height uint64) ([]da.ID, error) {
d.mu.Lock()
defer d.mu.Unlock()
kvps := d.data[height]
ids := make([]da.ID, len(kvps))
for i, kv := range kvps {
Expand All @@ -80,6 +87,8 @@ func (d *DummyDA) Commit(blobs []da.Blob) ([]da.Commitment, error) {

// Submit stores blobs in DA layer.
func (d *DummyDA) Submit(blobs []da.Blob) ([]da.ID, []da.Proof, error) {
d.mu.Lock()
defer d.mu.Unlock()
ids := make([]da.ID, len(blobs))
proofs := make([]da.Proof, len(blobs))
d.height += 1
Expand Down
28 changes: 28 additions & 0 deletions test/test_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package test

import (
"bytes"
"sync"
"testing"
"time"

Expand All @@ -21,6 +22,9 @@ func RunDATestSuite(t *testing.T, d da.DA) {
t.Run("Check Errors", func(t *testing.T) {
CheckErrors(t, d)
})
t.Run("Concurrent read/write test", func(t *testing.T) {
ConcurrentReadWriteTest(t, d)
})
}

// TODO(tzdybal): how to get rid of those aliases?
Expand Down Expand Up @@ -141,3 +145,27 @@ func GetIDsTest(t *testing.T, da da.DA) {

assert.True(t, found)
}

// ConcurrentReadWriteTest tests the use of mutex lock in DummyDA by calling separate methods that use `d.data` and making sure there's no race conditions
func ConcurrentReadWriteTest(t *testing.T, da da.DA) {
var wg sync.WaitGroup
wg.Add(2)

go func() {
defer wg.Done()
for i := uint64(0); i < 100; i++ {
_, err := da.GetIDs(i)
assert.NoError(t, err)
}
}()

go func() {
defer wg.Done()
for i := uint64(0); i < 100; i++ {
_, _, err := da.Submit([][]byte{[]byte("test")})
assert.NoError(t, err)
}
}()

wg.Wait()
}

0 comments on commit ee3b613

Please sign in to comment.