Skip to content

Commit

Permalink
Merge pull request #260 from dedis/value_contract_prefix
Browse files Browse the repository at this point in the history
Added prefix for value contract storage
  • Loading branch information
jbsv authored Aug 7, 2023
2 parents d0c8112 + 99f2c25 commit 445cdea
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 14 deletions.
25 changes: 20 additions & 5 deletions contracts/value/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ const (
// credentialAllCommand defines the credential command that is allowed to
// perform all commands.
credentialAllCommand = "all"

// contractKeyPrefix is used to prefix keys in the K/V store.
contractKeyPrefix = "VALU" // intentionally 4 bytes only, not a typo!
)

// Command defines a type of command for the value contract
Expand Down Expand Up @@ -172,14 +175,17 @@ func (c valueCommand) write(snap store.Snapshot, step execution.Step) error {
return xerrors.Errorf("'%s' not found in tx arg", ValueArg)
}

err := snap.Set(key, value)
snapKey := prefix(key)
err := snap.Set(snapKey, value)
if err != nil {
return xerrors.Errorf("failed to set value: %v", err)
}

c.index[string(key)] = struct{}{}

dela.Logger.Info().Str("contract", ContractName).Msgf("setting %x=%s", key, value)
dela.Logger.Info().
Str("contract", ContractName).
Msgf("setting value %x=%s", snapKey, value)

return nil
}
Expand All @@ -191,7 +197,7 @@ func (c valueCommand) read(snap store.Snapshot, step execution.Step) error {
return xerrors.Errorf("'%s' not found in tx arg", KeyArg)
}

val, err := snap.Get(key)
val, err := snap.Get(prefix(key))
if err != nil {
return xerrors.Errorf("failed to get key '%s': %v", key, err)
}
Expand All @@ -208,13 +214,18 @@ func (c valueCommand) delete(snap store.Snapshot, step execution.Step) error {
return xerrors.Errorf("'%s' not found in tx arg", KeyArg)
}

err := snap.Delete(key)
snapKey := prefix(key)
err := snap.Delete(snapKey)
if err != nil {
return xerrors.Errorf("failed to delete key '%x': %v", key, err)
}

delete(c.index, string(key))

dela.Logger.Info().
Str("contract", ContractName).
Msgf("deleting value %x", snapKey)

return nil
}

Expand All @@ -223,7 +234,7 @@ func (c valueCommand) list(snap store.Snapshot) error {
res := []string{}

for k := range c.index {
v, err := snap.Get([]byte(k))
v, err := snap.Get(prefix([]byte(k)))
if err != nil {
return xerrors.Errorf("failed to get key '%s': %v", k, err)
}
Expand All @@ -247,3 +258,7 @@ func (h infoLog) Write(p []byte) (int, error) {

return len(p), nil
}

func prefix(key []byte) []byte {
return append([]byte(contractKeyPrefix), key...)
}
12 changes: 6 additions & 6 deletions contracts/value/value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func TestCommand_Write(t *testing.T) {
_, found = contract.index["dummy"]
require.True(t, found)

res, err := snap.Get([]byte("dummy"))
res, err := snap.Get(prefix([]byte("dummy")))
require.NoError(t, err)
require.Equal(t, "value", string(res))
}
Expand All @@ -98,7 +98,7 @@ func TestCommand_Read(t *testing.T) {
require.EqualError(t, err, fake.Err("failed to get key 'dummy'"))

snap := fake.NewSnapshot()
snap.Set(key, []byte("value"))
snap.Set(prefix(key), []byte("value"))

buf := &bytes.Buffer{}
cmd.Contract.printer = buf
Expand Down Expand Up @@ -127,13 +127,13 @@ func TestCommand_Delete(t *testing.T) {
require.EqualError(t, err, fake.Err("failed to delete key '"+keyHex+"'"))

snap := fake.NewSnapshot()
snap.Set(key, []byte("value"))
snap.Set(prefix(key), []byte("value"))
contract.index[keyStr] = struct{}{}

err = cmd.delete(snap, makeStep(t, KeyArg, keyStr))
require.NoError(t, err)

res, err := snap.Get(key)
res, err := snap.Get(prefix(key))
require.Nil(t, err)
require.Nil(t, res)

Expand All @@ -158,8 +158,8 @@ func TestCommand_List(t *testing.T) {
}

snap := fake.NewSnapshot()
snap.Set([]byte(key1), []byte("value1"))
snap.Set([]byte(key2), []byte("value2"))
snap.Set(prefix([]byte(key1)), []byte("value1"))
snap.Set(prefix([]byte(key2)), []byte("value2"))

err := cmd.list(snap)
require.NoError(t, err)
Expand Down
12 changes: 9 additions & 3 deletions test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func getTest[T require.TestingT](numNode, numTx int) func(t T) {
require.NoError(t, err)

for i := 0; i < numTx; i++ {
key := make([]byte, 32)
key := make([]byte, 28) // only 224 bits are available

_, err = rand.Read(key)
require.NoError(t, err)
Expand All @@ -102,7 +102,7 @@ func getTest[T require.TestingT](numNode, numTx int) func(t T) {
err = addAndWait(t, timeout, manager, nodes[0].(cosiDelaNode), args...)
require.NoError(t, err)

proof, err := nodes[0].GetOrdering().GetProof(key)
proof, err := nodes[0].GetOrdering().GetProof(append([]byte("VALU"), key...))
require.NoError(t, err)
require.Equal(t, []byte("value1"), proof.GetValue())
}
Expand All @@ -112,7 +112,13 @@ func getTest[T require.TestingT](numNode, numTx int) func(t T) {
// -----------------------------------------------------------------------------
// Utility functions

func addAndWait(t require.TestingT, to time.Duration, manager txn.Manager, node cosiDelaNode, args ...txn.Arg) error {
func addAndWait(
t require.TestingT,
to time.Duration,
manager txn.Manager,
node cosiDelaNode,
args ...txn.Arg,
) error {
manager.Sync()

tx, err := manager.Make(args...)
Expand Down

0 comments on commit 445cdea

Please sign in to comment.