forked from ethereum/go-ethereum
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request ethereum#137 from rauljordan/shard-storage
Use Persistent Key-Val DB for Shard Node Collation Storage
- Loading branch information
Showing
10 changed files
with
218 additions
and
82 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,23 @@ | ||
package database | ||
|
||
import ( | ||
"path/filepath" | ||
|
||
"github.com/ethereum/go-ethereum/ethdb" | ||
) | ||
|
||
// ShardBackend defines an interface for a shardDB's necessary method | ||
// signatures. | ||
type ShardBackend interface { | ||
Get(k []byte) ([]byte, error) | ||
Has(k []byte) (bool, error) | ||
Put(k []byte, val []byte) error | ||
Delete(k []byte) error | ||
} | ||
|
||
// NewShardDB initializes a shardDB that writes to local disk. | ||
func NewShardDB(dataDir string, name string) (ShardBackend, error) { | ||
// Uses default cache and handles values. | ||
// TODO: allow these arguments to be set based on cli context. | ||
return ethdb.NewLDBDatabase(filepath.Join(dataDir, name), 16, 16) | ||
} |
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,92 @@ | ||
package database | ||
|
||
import ( | ||
"strconv" | ||
"testing" | ||
) | ||
|
||
var db ShardBackend | ||
|
||
func init() { | ||
shardDB, err := NewShardDB("/tmp/datadir", "shardchaindata") | ||
if err != nil { | ||
panic(err) | ||
} | ||
db = shardDB | ||
} | ||
|
||
// Testing the concurrency of the shardDB with multiple processes attempting to write. | ||
func Test_DBConcurrent(t *testing.T) { | ||
for i := 0; i < 100; i++ { | ||
go func(val string) { | ||
if err := db.Put([]byte("ralph merkle"), []byte(val)); err != nil { | ||
t.Errorf("could not save value in db: %v", err) | ||
} | ||
}(strconv.Itoa(i)) | ||
} | ||
} | ||
|
||
func Test_DBPut(t *testing.T) { | ||
if err := db.Put([]byte("ralph merkle"), []byte{1, 2, 3}); err != nil { | ||
t.Errorf("could not save value in db: %v", err) | ||
} | ||
} | ||
|
||
func Test_DBHas(t *testing.T) { | ||
key := []byte("ralph merkle") | ||
|
||
if err := db.Put(key, []byte{1, 2, 3}); err != nil { | ||
t.Fatalf("could not save value in db: %v", err) | ||
} | ||
|
||
has, err := db.Has(key) | ||
if err != nil { | ||
t.Errorf("could not check if db has key: %v", err) | ||
} | ||
if !has { | ||
t.Errorf("db should have key: %v", key) | ||
} | ||
|
||
key2 := []byte{} | ||
has2, err := db.Has(key2) | ||
if err != nil { | ||
t.Errorf("could not check if db has key: %v", err) | ||
} | ||
if has2 { | ||
t.Errorf("db should not have non-existent key: %v", key2) | ||
} | ||
} | ||
|
||
func Test_DBGet(t *testing.T) { | ||
key := []byte("ralph merkle") | ||
|
||
if err := db.Put(key, []byte{1, 2, 3}); err != nil { | ||
t.Fatalf("could not save value in db: %v", err) | ||
} | ||
|
||
val, err := db.Get(key) | ||
if err != nil { | ||
t.Errorf("get failed: %v", err) | ||
} | ||
if len(val) == 0 { | ||
t.Errorf("no value stored for key") | ||
} | ||
|
||
key2 := []byte{} | ||
val2, err := db.Get(key2) | ||
if len(val2) != 0 { | ||
t.Errorf("non-existent key should not have a value. key=%v, value=%v", key2, val2) | ||
} | ||
} | ||
|
||
func Test_DBDelete(t *testing.T) { | ||
key := []byte("ralph merkle") | ||
|
||
if err := db.Put(key, []byte{1, 2, 3}); err != nil { | ||
t.Fatalf("could not save value in db: %v", err) | ||
} | ||
|
||
if err := db.Delete(key); err != nil { | ||
t.Errorf("could not delete key: %v", key) | ||
} | ||
} |
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
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
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
Oops, something went wrong.