aboutsummaryrefslogtreecommitdiff
path: root/plugin/evm/block.go
diff options
context:
space:
mode:
authorStephen Buttolph <[email protected]>2020-04-16 11:38:38 -0400
committerGitHub <[email protected]>2020-04-16 11:38:38 -0400
commit6a76dcccc5c5d9686b35595d3f9ab3a6cefd0446 (patch)
tree24ef681a6c846ba58ce2b8db2fa436c10404172b /plugin/evm/block.go
parent756aac82152ba5265f62d234d94a1926cf572b38 (diff)
parent7981c59502a99b6f7938a18b6ae5c3803cf0a794 (diff)
Merge pull request #5 from ava-labs/revert-4-revert-2-plugin
Added the EVM as a plugin
Diffstat (limited to 'plugin/evm/block.go')
-rw-r--r--plugin/evm/block.go75
1 files changed, 75 insertions, 0 deletions
diff --git a/plugin/evm/block.go b/plugin/evm/block.go
new file mode 100644
index 0000000..ec47490
--- /dev/null
+++ b/plugin/evm/block.go
@@ -0,0 +1,75 @@
+// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
+// See the file LICENSE for licensing terms.
+
+package evm
+
+import (
+ "fmt"
+
+ "github.com/ava-labs/go-ethereum/core/types"
+ "github.com/ava-labs/go-ethereum/rlp"
+
+ "github.com/ava-labs/gecko/ids"
+ "github.com/ava-labs/gecko/snow/choices"
+ "github.com/ava-labs/gecko/snow/consensus/snowman"
+)
+
+// Block implements the snowman.Block interface
+type Block struct {
+ id ids.ID
+ ethBlock *types.Block
+ vm *VM
+}
+
+// ID implements the snowman.Block interface
+func (b *Block) ID() ids.ID { return b.id }
+
+// Accept implements the snowman.Block interface
+func (b *Block) Accept() {
+ b.vm.ctx.Log.Verbo("Block %s is accepted", b.ID())
+ b.vm.updateStatus(b.ID(), choices.Accepted)
+}
+
+// Reject implements the snowman.Block interface
+func (b *Block) Reject() {
+ b.vm.ctx.Log.Verbo("Block %s is rejected", b.ID())
+ b.vm.updateStatus(b.ID(), choices.Rejected)
+}
+
+// Status implements the snowman.Block interface
+func (b *Block) Status() choices.Status {
+ status := b.vm.getCachedStatus(b.ID())
+ if status == choices.Unknown && b.ethBlock != nil {
+ return choices.Processing
+ }
+ return status
+}
+
+// Parent implements the snowman.Block interface
+func (b *Block) Parent() snowman.Block {
+ parentID := ids.NewID(b.ethBlock.ParentHash())
+ block := &Block{
+ id: parentID,
+ ethBlock: b.vm.getCachedBlock(parentID),
+ vm: b.vm,
+ }
+ b.vm.ctx.Log.Verbo("Parent(%s) has status: %s", block.ID(), block.Status())
+ return block
+}
+
+// Verify implements the snowman.Block interface
+func (b *Block) Verify() error {
+ _, err := b.vm.chain.InsertChain([]*types.Block{b.ethBlock})
+ return err
+}
+
+// Bytes implements the snowman.Block interface
+func (b *Block) Bytes() []byte {
+ res, err := rlp.EncodeToBytes(b.ethBlock)
+ if err != nil {
+ panic(err)
+ }
+ return res
+}
+
+func (b *Block) String() string { return fmt.Sprintf("EVM block, ID = %s", b.ID()) }