Skip to content
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
steps:
- uses: actions/setup-go@v4
with:
go-version: "1.19"
go-version: "1.20"
- uses: actions/checkout@v3
- run: go mod download
shell: bash
Expand All @@ -27,7 +27,7 @@ jobs:
steps:
- uses: actions/setup-go@v4
with:
go-version: "1.19"
go-version: "1.20"
- uses: actions/checkout@v3
- name: test VM
run: go test -v ./vm/...
17 changes: 9 additions & 8 deletions rpc/jsonrpc/server/http_json_handler.go
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I[2024-01-12|08:45:44.758] R.URL.PATH module=rpc path=/ext/bc/2QAdREospcgYUQwtsV1DCy4rr353hSUsYWpFKnTozovdahnpLS/rpc
So, I think that we should delete this block of code

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've mentioned the block of code on lines 70-77

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
// HTTP + JSON handler

// jsonrpc calls grab the given method's function info and runs reflect.Call
func makeJSONRPCHandler(funcMap map[string]*RPCFunc, logger log.Logger) http.HandlerFunc {
func MakeJSONRPCHandler(funcMap map[string]*RPCFunc, logger log.Logger) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
b, err := ioutil.ReadAll(r.Body)
if err != nil {
Expand Down Expand Up @@ -67,13 +67,14 @@ func makeJSONRPCHandler(funcMap map[string]*RPCFunc, logger log.Logger) http.Han
)
continue
}
if len(r.URL.Path) > 1 {
responses = append(
responses,
types.RPCInvalidRequestError(request.ID, fmt.Errorf("path %s is invalid", r.URL.Path)),
)
continue
}
// TODO: need to rever this change
//if len(r.URL.Path) > 1 {
// responses = append(
// responses,
// types.RPCInvalidRequestError(request.ID, fmt.Errorf("path %s is invalid", r.URL.Path)),
// )
// continue
//}
rpcFunc, ok := funcMap[request.Method]
if !ok || rpcFunc.ws {
responses = append(responses, types.RPCMethodNotFoundError(request.ID))
Expand Down
2 changes: 1 addition & 1 deletion rpc/jsonrpc/server/rpc_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func RegisterRPCFuncs(mux *http.ServeMux, funcMap map[string]*RPCFunc, logger lo
}

// JSONRPC endpoints
mux.HandleFunc("/", handleInvalidJSONRPCPaths(makeJSONRPCHandler(funcMap, logger)))
mux.HandleFunc("/", handleInvalidJSONRPCPaths(MakeJSONRPCHandler(funcMap, logger)))
}

// Function introspection
Expand Down
127 changes: 73 additions & 54 deletions vm/block.go
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please: change field name from 'st' to 'status',
get rid of panic on line 54,
explain trick on line 100

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package vm

import (
"context"
"fmt"
"time"

"github.com/ava-labs/avalanchego/ids"
Expand All @@ -11,85 +12,103 @@ import (
)

var (
_ snowman.Block = &Block{}
_ choices.Decidable = (*Block)(nil)
_ snowman.Block = (*Block)(nil)
)

// Block implements the snowman.Block interface
type Block struct {
id ids.ID
tmBlock *types.Block
vm *VM
status choices.Status
*types.Block
st choices.Status
vm *VM
}

// newBlock returns a new Block wrapping the Tendermint Block type and implementing the snowman.Block interface
func (vm *VM) newBlock(tmBlock *types.Block) (*Block, error) {
var id ids.ID
copy(id[:], tmBlock.Hash())

return &Block{
id: id,
tmBlock: tmBlock,
vm: vm,
}, nil
}

func (b *Block) ID() ids.ID {
return b.id
func NewBlock(vm *VM, block *types.Block, st choices.Status) *Block {
return &Block{Block: block, vm: vm, st: st}
}

func (b *Block) Accept(ctx context.Context) error {
b.SetStatus(choices.Accepted)
return b.vm.applyBlock(b)
// ID returns a unique ID for this element.
//
// Typically, this is implemented by using a cryptographic hash of a
// binary representation of this element. An element should return the same
// IDs upon repeated calls.
func (block *Block) ID() ids.ID {
return ids.ID(block.Hash())
}

func (b *Block) Reject(ctx context.Context) error {
b.SetStatus(choices.Rejected)

return nil
// Accept this element.
//
// This element will be accepted by every correct node in the network.
func (block *Block) Accept(context.Context) error {
block.vm.log.Debug("try to accept block", "block", block.ID())
block.st = choices.Accepted
delete(block.vm.verifiedBlocks, block.ID())
return block.vm.applyBlock(block)
}

func (b *Block) SetStatus(status choices.Status) {
b.status = status
// Reject this element.
//
// This element will not be accepted by any correct node in the network.
func (block *Block) Reject(context.Context) error {
block.vm.log.Debug("try to reject block", "block", block.ID())
block.st = choices.Rejected
panic("implement me")
}

func (b *Block) Status() choices.Status {
return b.status
// Status returns this element's current status.
//
// If Accept has been called on an element with this ID, Accepted should be
// returned. Similarly, if Reject has been called on an element with this
// ID, Rejected should be returned. If the contents of this element are
// unknown, then Unknown should be returned. Otherwise, Processing should be
// returned.
//
// TODO: Consider allowing Status to return an error.
func (block *Block) Status() choices.Status {
return block.st
}

func (b *Block) Parent() ids.ID {
var id ids.ID
parentHash := b.tmBlock.Header.LastBlockID.Hash
copy(id[:], parentHash)

return id
// Parent returns the ID of this block's parent.
func (block *Block) Parent() ids.ID {
return ids.ID(block.LastBlockID.Hash)
}

func (b *Block) Verify(context.Context) error {
if b == nil || b.tmBlock == nil {
return errInvalidBlock
}

return b.tmBlock.ValidateBasic()
// Verify that the state transition this block would make if accepted is
// valid. If the state transition is invalid, a non-nil error should be
// returned.
//
// It is guaranteed that the Parent has been successfully verified.
//
// If nil is returned, it is guaranteed that either Accept or Reject will be
// called on this block, unless the VM is shut down.
func (block *Block) Verify(context.Context) error {
return block.ValidateBasic()
}

func (b *Block) Bytes() []byte {
block, err := b.tmBlock.ToProto()
// Bytes returns the binary representation of this block.
//
// This is used for sending blocks to peers. The bytes should be able to be
// parsed into the same block on another node.
func (block *Block) Bytes() []byte {
b, err := block.ToProto()
if err != nil {
panic(err)
panic(fmt.Sprintf("can't convert block to proto obj: %s", err))
}
data, err := block.Marshal()
data, err := b.Marshal()
if err != nil {
panic(err)
panic(fmt.Sprintf("can't serialize block: %s", err))
}

return data
return append([]byte{uint8(block.st)}, data...)
}

func (b *Block) Height() uint64 {
return uint64(b.tmBlock.Height)
// Height returns the height of this block in the chain.
func (block *Block) Height() uint64 {
return uint64(block.Block.Height)
}

func (b *Block) Timestamp() time.Time {
return b.tmBlock.Time
// Time this block was proposed at. This value should be consistent across
// all nodes. If this block hasn't been successfully verified, any value can
// be returned. If this block is the last accepted block, the timestamp must
// be returned correctly. Otherwise, accepted blocks can return any value.
func (block *Block) Timestamp() time.Time {
return block.Block.Time
}
13 changes: 7 additions & 6 deletions vm/cmd/main.go
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is better to build variable of virtual machine for debug purposes, at the same time it doesn't matter.

Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,24 @@ package main
import (
"context"
"fmt"
"github.com/consideritdone/landslidecore/abci/example/counter"
landslideCoreVM "github.com/consideritdone/landslidecore/vm"
"os"

"github.com/consideritdone/landslidecore/abci/example/counter"
"github.com/consideritdone/landslidecore/vm"

"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/utils/ulimit"
"github.com/ava-labs/avalanchego/vms/rpcchainvm"
)

func main() {

if err := ulimit.Set(ulimit.DefaultFDLimit, logging.NoLog{}); err != nil {
fmt.Printf("failed to set fd limit correctly due to: %s", err)
os.Exit(1)
}

vm := landslideCoreVM.NewVM(counter.NewApplication(true))

rpcchainvm.Serve(context.Background(), vm)
rpcchainvm.Serve(
context.Background(),
vm.New(vm.LocalAppCreator(counter.NewApplication(true))),
)
}
20 changes: 19 additions & 1 deletion vm/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

var (
_ dbm.DB = &Database{}
_ dbm.DB = (*Database)(nil)
)

type (
Expand All @@ -24,6 +24,20 @@ type (
}
)

func NewDB(db database.Database) *Database {
return &Database{
Database: db,
}
}

func (db Database) Close() error {
return db.Database.Close()
}

func (db Database) Has(key []byte) (bool, error) {
return db.Database.Has(key)
}

func (db Database) Get(key []byte) ([]byte, error) {
res, err := db.Database.Get(key)
if err != nil {
Expand All @@ -43,6 +57,10 @@ func (db Database) SetSync(key []byte, value []byte) error {
return db.Database.Put(key, value)
}

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

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