-
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.
* 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
Showing
36 changed files
with
11,659 additions
and
5 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,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 ./... |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
# cometvm | ||
# landslidevm |
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,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 | ||
} |
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,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 | ||
} |
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,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, | ||
} | ||
) |
Oops, something went wrong.