Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.
Merged
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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ KVVM defines a blockchain that is a key-value storage server. Each block in the

KVVM is served over RPC with [go-plugin](https://github.com/hashicorp/go-plugin).

# Features
TODO: Extend on
* PoW Transactions (no tokens)
* Prefixes (address prefixes reserved)
* Hashed Value Keys
* Prefix Expiry (based on weight of all key-values)

# Quick start

At its core, the Avalanche protocol still maintains the immutable ordered sequence of states in a fully permissionless settings. And KVVM defines the rules and data structures to store key-value pairs.
Expand Down
1 change: 1 addition & 0 deletions chain/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var (
ErrValueTooBig = errors.New("value too big")
ErrPrefixExpired = errors.New("prefix expired")
ErrKeyMissing = errors.New("key missing")
ErrInvalidKey = errors.New("key is invalid")
ErrPublicKeyMismatch = errors.New("public key does not match decoded prefix")
ErrPrefixNotExpired = errors.New("prefix not expired")
ErrPrefixMissing = errors.New("prefix missing")
Expand Down
17 changes: 17 additions & 0 deletions chain/set_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@ package chain

import (
"bytes"
"fmt"

"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/quarkvm/parser"
"golang.org/x/crypto/sha3"
)

const IDLen = 32

var _ UnsignedTransaction = &SetTx{}

type SetTx struct {
Expand Down Expand Up @@ -48,6 +53,18 @@ func (s *SetTx) Execute(db database.Database, blockTime uint64) error {
if i.Expiry < blockTime {
return ErrPrefixExpired
}
// If Key is equal to hash length, ensure it is equal to the hash of the
// value
if len(s.Key) == IDLen && len(s.Value) > 0 {
h := sha3.Sum256(s.Value)
id, err := ids.ToID(h[:])
if err != nil {
return err
}
if !bytes.Equal(s.Key, id[:]) {
return fmt.Errorf("%w: expected %x got %x", ErrInvalidKey, id[:], s.Key)
}
}
return s.updatePrefix(db, blockTime, i)
}

Expand Down
48 changes: 48 additions & 0 deletions chain/set_tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/ava-labs/avalanchego/database/memdb"
"github.com/ava-labs/avalanchego/ids"
"golang.org/x/crypto/sha3"

"github.com/ava-labs/quarkvm/parser"
)
Expand Down Expand Up @@ -91,6 +92,53 @@ func TestSetTx(t *testing.T) {
blockTime: 1,
err: nil,
},
{ // write hashed value
utx: &SetTx{
BaseTx: &BaseTx{
Sender: sender,
Prefix: []byte("foo"),
BlockID: ids.GenerateTestID(),
},
Key: func() []byte {
h := sha3.Sum256([]byte("value"))
return h[:]
}(),
Value: []byte("value"),
},
blockTime: 1,
err: nil,
},
{ // write incorrect hashed value
utx: &SetTx{
BaseTx: &BaseTx{
Sender: sender,
Prefix: []byte("foo"),
BlockID: ids.GenerateTestID(),
},
Key: func() []byte {
h := sha3.Sum256([]byte("not value"))
return h[:]
}(),
Value: []byte("value"),
},
blockTime: 1,
err: ErrInvalidKey,
},
{ // delete hashed value
utx: &SetTx{
BaseTx: &BaseTx{
Sender: sender,
Prefix: []byte("foo"),
BlockID: ids.GenerateTestID(),
},
Key: func() []byte {
h := sha3.Sum256([]byte("value"))
return h[:]
}(),
},
blockTime: 1,
err: nil,
},
{
utx: &SetTx{
BaseTx: &BaseTx{
Expand Down