Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Commit 0057315

Browse files
committed
vm: implement some kv business logic
Signed-off-by: Gyuho Lee <[email protected]>
1 parent 76f5747 commit 0057315

File tree

3 files changed

+67
-4
lines changed

3 files changed

+67
-4
lines changed

vm/info/info.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved.
2+
// See the file LICENSE for licensing terms.
3+
4+
// Package info defines the key owner information.
5+
package info
6+
7+
import (
8+
"github.com/ava-labs/quarkvm/crypto/ed25519"
9+
"github.com/ava-labs/quarkvm/vm/codec"
10+
)
11+
12+
func init() {
13+
codec.RegisterType(&Info{})
14+
}
15+
16+
type Info struct {
17+
Owner ed25519.PublicKey `serialize:"true"`
18+
LastUpdated int64 `serialize:"true"`
19+
Expiry int64 `serialize:"true"`
20+
Keys int64 `serialize:"true"` // decays faster the more keys you have
21+
}

vm/kv.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved.
2+
// See the file LICENSE for licensing terms.
3+
4+
package vm

vm/storage/storage.go

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"github.com/ava-labs/avalanchego/database/prefixdb"
1111
"github.com/ava-labs/avalanchego/database/versiondb"
1212
"github.com/ava-labs/quarkvm/crypto/ed25519"
13+
"github.com/ava-labs/quarkvm/vm/codec"
14+
"github.com/ava-labs/quarkvm/vm/info"
1315
)
1416

1517
var (
@@ -23,6 +25,10 @@ var (
2325
keyPrefix = []byte("key")
2426
)
2527

28+
var (
29+
ErrKeyExists = errors.New("key already exists")
30+
)
31+
2632
type Storage interface {
2733
Block() database.Database
2834
Info() database.Database
@@ -68,17 +74,49 @@ func (s *storage) Key() database.Database {
6874
return s.keyDB
6975
}
7076

77+
// any non-existent or expired (see below) key can be claimed
78+
// by anyone that submits a sufficient PoW.
7179
func (s *storage) Put(k []byte, v []byte, opts ...OpOption) error {
7280
ret := &Op{}
7381
ret.applyOpts(opts)
74-
if ret.overwrite && ret.owner == nil {
82+
if ret.owner == nil {
7583
return errors.New("caller must provide the public key to make modification or overwrite")
7684
}
7785

78-
// TODO: look up key
79-
// if exists, only overwrite when conditions are met
80-
// implement other business logic
86+
if exist, _ := s.keyDB.Has(k); exist && !ret.overwrite {
87+
return ErrKeyExists
88+
}
89+
90+
// "a/b" then pfx is "a" and sub is "b"
91+
pfx, sub, _, err := getPrefix(k)
92+
if err != nil {
93+
return err
94+
}
95+
96+
// find the owner of the prefix
97+
infoBytes, err := s.infoDB.Get(pfx)
98+
if err != nil && err == database.ErrClosed {
99+
// TODO: db returns error if not found?
100+
return err
101+
}
102+
103+
if len(infoBytes) > 0 {
104+
// TODO: validate with public key
105+
// where to get signature
106+
ret.owner.Verify(infoBytes, nil)
107+
108+
// TODO: update the info key
109+
} else {
110+
info := &info.Info{Owner: ret.owner}
111+
infoBytes, err := codec.Marshal(info)
112+
if err != nil {
113+
return err
114+
}
115+
s.infoDB.Put(pfx, infoBytes)
116+
}
81117

118+
// if validated or new key,
119+
// the owner is allowed to write the key
82120
return s.keyDB.Put(k, v)
83121
}
84122

0 commit comments

Comments
 (0)