Skip to content
Open
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
9 changes: 1 addition & 8 deletions rpc/jsonrpc/server/http_json_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,7 @@ func MakeJSONRPCHandler(funcMap map[string]*RPCFunc, logger log.Logger) http.Han
)
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
//}
logger.Info("R.URL.PATH", "path", r.URL.Path)
rpcFunc, ok := funcMap[request.Method]
if !ok || rpcFunc.ws {
responses = append(responses, types.RPCMethodNotFoundError(request.ID))
Expand Down
33 changes: 21 additions & 12 deletions vm/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ var (

type Block struct {
*types.Block
st choices.Status
vm *VM
status choices.Status
vm *VM
}

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

// ID returns a unique ID for this element.
Expand All @@ -39,8 +39,9 @@ func (block *Block) ID() ids.ID {
//
// 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
block.vm.log.Debug(fmt.Sprintf("Accepting block %s (%s) at height %d", block.ID().Hex(), block.ID(), block.Height()))
block.status = choices.Accepted
// Delete this block from verified blocks as it's accepted
delete(block.vm.verifiedBlocks, block.ID())
return block.vm.applyBlock(block)
}
Expand All @@ -49,9 +50,11 @@ func (block *Block) Accept(context.Context) error {
//
// 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")
block.vm.log.Debug(fmt.Sprintf("Rejecting block %s (%s) at height %d", block.ID().Hex(), block.ID(), block.Height()))
block.status = choices.Rejected
// Delete this block from verified blocks as it's rejected
delete(block.vm.verifiedBlocks, block.ID())
return nil
}

// Status returns this element's current status.
Expand All @@ -64,7 +67,7 @@ func (block *Block) Reject(context.Context) error {
//
// TODO: Consider allowing Status to return an error.
func (block *Block) Status() choices.Status {
return block.st
return block.status
}

// Parent returns the ID of this block's parent.
Expand All @@ -81,7 +84,13 @@ func (block *Block) Parent() ids.ID {
// 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()
err := block.ValidateBasic()
if err != nil {
return err
}
// Put that block to verified blocks in memory
block.vm.verifiedBlocks[block.ID()] = block
return nil
}

// Bytes returns the binary representation of this block.
Expand All @@ -97,7 +106,7 @@ func (block *Block) Bytes() []byte {
if err != nil {
panic(fmt.Sprintf("can't serialize block: %s", err))
}
return append([]byte{uint8(block.st)}, data...)
return data
}

// Height returns the height of this block in the chain.
Expand Down
4 changes: 3 additions & 1 deletion vm/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ func main() {
os.Exit(1)
}

landslideVM := vm.New(vm.LocalAppCreator(counter.NewApplication(true)))

rpcchainvm.Serve(
context.Background(),
vm.New(vm.LocalAppCreator(counter.NewApplication(true))),
landslideVM,
)
}
20 changes: 13 additions & 7 deletions vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ func (vm *VM) ParseBlock(ctx context.Context, blockBytes []byte) (snowman.Block,
vm.log.Debug("parse block")

protoBlock := new(tmproto.Block)
if err := protoBlock.Unmarshal(blockBytes[1:]); err != nil {
if err := protoBlock.Unmarshal(blockBytes); err != nil {
vm.log.Error("can't parse block", "err", err)
return nil, err
}
Expand All @@ -560,7 +560,13 @@ func (vm *VM) ParseBlock(ctx context.Context, blockBytes []byte) (snowman.Block,
return nil, err
}

blk := NewBlock(vm, block, choices.Status(uint32(blockBytes[0])))
blk := NewBlock(vm, block, choices.Processing)
if b, err := vm.GetBlock(ctx, blk.ID()); err == nil {
vm.log.Debug("parsed block", "id", blk.ID(), "status", b.Status().String())
// If we have seen this block before, return it with the most up-to-date
// info
return b, nil
}
vm.log.Debug("parsed block", "id", blk.ID(), "status", blk.Status().String())
if _, ok := vm.verifiedBlocks[blk.ID()]; !ok {
vm.verifiedBlocks[blk.ID()] = blk
Expand Down Expand Up @@ -608,7 +614,10 @@ func (vm *VM) BuildBlock(ctx context.Context) (snowman.Block, error) {
}

blk := NewBlock(vm, block, choices.Processing)
vm.verifiedBlocks[blk.ID()] = blk
// Verifies block
if err := blk.Verify(ctx); err != nil {
return nil, err
}

vm.log.Debug("build block", "id", blk.ID(), "height", blk.Height(), "txs", len(block.Txs))
return blk, nil
Expand All @@ -629,10 +638,7 @@ func (vm *VM) SetPreference(ctx context.Context, blkID ids.ID) error {
// a definitionally accepted block, the Genesis block, that will be
// returned.
func (vm *VM) LastAccepted(context.Context) (ids.ID, error) {
if vm.preferred == ids.Empty {
return ids.ID(vm.state.LastBlockID.Hash), nil
}
return vm.preferred, nil
return ids.ID(vm.state.LastBlockID.Hash), nil
}

func (vm *VM) applyBlock(block *Block) error {
Expand Down