Skip to content

Commit

Permalink
update skeleton (#5)
Browse files Browse the repository at this point in the history
* add cometbft dependency

* add avalanche dependency

* update deps

* add buf build config to project and add serve function

* add database connection

* add Initialize logic

* add init test and mock database

* refactor pkgs and initialization

* update vm block methods
  • Loading branch information
n0cte authored Apr 3, 2024
1 parent e37055a commit 6a9358f
Show file tree
Hide file tree
Showing 36 changed files with 11,659 additions and 5 deletions.
43 changes: 43 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# This workflow will build a golang project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go

name: Go

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:

build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.22'

- name: Verify dependencies
run: go mod verify

- name: Build
run: go build -v ./...

- name: Run go vet
run: go vet ./...

- name: Install staticcheck
run: go install honnef.co/go/tools/cmd/staticcheck@latest

- name: Run staticcheck
run: staticcheck ./...

- name: Install golint
run: go install golang.org/x/lint/golint@latest

- name: Test
run: go test -race -vet=off -v ./...
9 changes: 5 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/
# OS
.DS_Store

# Go workspace file
go.work
# IDE
.idea
.vscode
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# cometvm
# landslidevm
83 changes: 83 additions & 0 deletions database/batch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package database

import (
"context"
"slices"

dbm "github.com/cometbft/cometbft-db"
"github.com/consideritdone/landslidevm/proto/rpcdb"
)

var (
_ dbm.Batch = (*Batch)(nil)
)

type (
Batch struct {
Ops []BatchOp
size int
db *Database
}

BatchOp struct {
Key []byte
Value []byte
Delete bool
}
)

func (b *Batch) Set(key, value []byte) error {
b.Ops = append(b.Ops, BatchOp{
Key: slices.Clone(key),
Value: slices.Clone(value),
})
b.size += len(key) + len(value)
return nil
}

func (b *Batch) Delete(key []byte) error {
b.Ops = append(b.Ops, BatchOp{
Key: slices.Clone(key),
Delete: true,
})
b.size += len(key)
return nil
}

func (b *Batch) Write() error {
request := &rpcdb.WriteBatchRequest{}
keySet := make(map[string]bool, len(b.Ops))
for i := len(b.Ops) - 1; i >= 0; i-- {
op := b.Ops[i]
key := string(op.Key)
if keySet[key] {
continue
}
keySet[key] = true

if op.Delete {
request.Deletes = append(request.Deletes, &rpcdb.DeleteRequest{
Key: op.Key,
})
} else {
request.Puts = append(request.Puts, &rpcdb.PutRequest{
Key: op.Key,
Value: op.Value,
})
}
}

resp, err := b.db.client.WriteBatch(context.Background(), request)
if err != nil {
return err
}
return ErrEnumToError[resp.Err]
}

func (b *Batch) WriteSync() error {
return b.Write()
}

func (b *Batch) Close() error {
return nil
}
125 changes: 125 additions & 0 deletions database/database.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package database

import (
"context"
"sync/atomic"

dbm "github.com/cometbft/cometbft-db"
"github.com/consideritdone/landslidevm/proto/rpcdb"
)

var (
_ dbm.DB = (*Database)(nil)
)

type (
Database struct {
client rpcdb.DatabaseClient
closed atomic.Bool
}
)

func New(db rpcdb.DatabaseClient) *Database {
return &Database{
client: db,
}
}

func (db *Database) Close() error {
db.closed.Store(true)
resp, err := db.client.Close(context.Background(), &rpcdb.CloseRequest{})
if err != nil {
return err
}
return ErrEnumToError[resp.Err]
}

func (db *Database) Has(key []byte) (bool, error) {
resp, err := db.client.Has(context.Background(), &rpcdb.HasRequest{
Key: key,
})
if err != nil {
return false, err
}
return resp.Has, ErrEnumToError[resp.Err]
}

func (db *Database) Get(key []byte) ([]byte, error) {
resp, err := db.client.Get(context.Background(), &rpcdb.GetRequest{
Key: key,
})
if err != nil {
return nil, err
}
return resp.Value, ErrEnumToError[resp.Err]
}

func (db *Database) Set(key []byte, value []byte) error {
resp, err := db.client.Put(context.Background(), &rpcdb.PutRequest{
Key: key,
Value: value,
})
if err != nil {
return err
}
return ErrEnumToError[resp.Err]
}

func (db *Database) SetSync(key []byte, value []byte) error {
return db.Set(key, value)
}

func (db *Database) Delete(key []byte) error {
resp, err := db.client.Delete(context.Background(), &rpcdb.DeleteRequest{
Key: key,
})
if err != nil {
return err
}
return ErrEnumToError[resp.Err]
}

func (db *Database) DeleteSync(key []byte) error {
return db.Delete(key)
}

func (db *Database) Iterator(start, end []byte) (dbm.Iterator, error) {
resp, err := db.client.NewIteratorWithStartAndPrefix(context.Background(), &rpcdb.NewIteratorWithStartAndPrefixRequest{
Start: start,
Prefix: nil,
})
if err != nil {
return nil, err
}
it := Iterator{
db: db,
id: resp.Id,
start: start,
end: end,

fetchedData: make(chan []*rpcdb.PutRequest),
reqUpdateError: make(chan chan struct{}),
onClose: make(chan struct{}),
onClosed: make(chan struct{}),
}
go it.fetch()
return &it, nil
}

func (db *Database) ReverseIterator(start, end []byte) (dbm.Iterator, error) {
return db.Iterator(start, end)
}

func (db *Database) NewBatch() dbm.Batch {
return &Batch{db: db}
}

func (db *Database) Print() error {
//TODO implement me
return nil
}

func (db *Database) Stats() map[string]string {
//TODO implement me
return nil
}
16 changes: 16 additions & 0 deletions database/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package database

import (
"errors"

"github.com/consideritdone/landslidevm/proto/rpcdb"
)

var (
ErrClosed = errors.New("closed")
ErrNotFound = errors.New("not found")
ErrEnumToError = map[rpcdb.Error]error{
rpcdb.Error_ERROR_CLOSED: ErrClosed,
rpcdb.Error_ERROR_NOT_FOUND: ErrNotFound,
}
)
Loading

0 comments on commit 6a9358f

Please sign in to comment.