aboutsummaryrefslogblamecommitdiff
path: root/plugin/evm/block.go
blob: a0a903ba47774e58fc2317ff12933e5ef4386cf5 (plain) (tree)
1
2
3
4
5
6
7
8
9





                                                     
                

             
                                               



                                                












                                                
                                





                                                        


                          

                                                   
                                                    


                                      


                                                
                                

                                                          
                  











                                                           





                                                 
                                                      

                                                                                         
                                 
         
                                                                                  
                                                                  



                                                






                                                                             


                 
                  
                                        




                                                                
                         



                                                                                                      


                                              
                                                                                                          





                                                                                          




                                                                                                              
                         








                                                                                                    
                         







                                                                    
                 
 


                                                                                   
         













                                                                                    
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package evm

import (
	"errors"
	"fmt"

	"github.com/ava-labs/coreth/core/types"
	"github.com/ava-labs/go-ethereum/rlp"

	"github.com/ava-labs/gecko/ids"
	"github.com/ava-labs/gecko/snow/choices"
)

// 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() error {
	vm := b.vm

	vm.ctx.Log.Verbo("Block %s is accepted", b.ID())
	vm.updateStatus(b.ID(), choices.Accepted)

	tx := vm.getAtomicTx(b.ethBlock)
	if tx == nil {
		return nil
	}
	utx, ok := tx.UnsignedTx.(UnsignedAtomicTx)
	if !ok {
		return errors.New("unknown tx type")
	}

	return utx.Accept(vm.ctx, nil)
}

// Reject implements the snowman.Block interface
func (b *Block) Reject() error {
	b.vm.ctx.Log.Verbo("Block %s is rejected", b.ID())
	b.vm.updateStatus(b.ID(), choices.Rejected)
	return nil
}

// 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() ids.ID {
	return ids.NewID(b.ethBlock.ParentHash())
}

// ParentBlock returns [b]'s parent
func (b *Block) parentBlock() (*Block, error) {
	parentID := ids.NewID(b.ethBlock.ParentHash())
	if block := b.vm.getBlock(parentID); block != nil {
		b.vm.ctx.Log.Verbo("Parent(%s) has status: %s", parentID, block.Status())
		return block, nil
	}
	b.vm.ctx.Log.Verbo("Parent(%s) has status: %s", parentID, choices.Unknown)
	return nil, fmt.Errorf("couldn't find block %s", parentID)
}

// Verify implements the snowman.Block interface
func (b *Block) Verify() error {
	// Only enforce a minimum fee when bootstrapping has finished
	if b.vm.ctx.IsBootstrapped() {
		// Ensure the minimum gas price is paid for every transaction
		for _, tx := range b.ethBlock.Transactions() {
			if tx.GasPrice().Cmp(minGasPrice) < 0 {
				return errInvalidBlock
			}
		}
	}

	vm := b.vm
	tx := vm.getAtomicTx(b.ethBlock)
	if tx != nil {
		switch atx := tx.UnsignedTx.(type) {
		case *UnsignedImportTx:
			if b.ethBlock.Hash() == vm.genesisHash {
				return nil
			}
			p, err := b.parentBlock()
			if err != nil {
				return fmt.Errorf("couldn't get %s, parent of %s", b.Parent(), p.ID())
			}
			path := []*Block{}
			inputs := new(ids.Set)
			for {
				if p.Status() == choices.Accepted || p.ethBlock.Hash() == vm.genesisHash {
					break
				}
				if ret, hit := vm.blockAtomicInputCache.Get(p.ID()); hit {
					inputs = ret.(*ids.Set)
					break
				}
				path = append(path, p)
				p, err = p.parentBlock()
				if err != nil {
					return fmt.Errorf("couldn't get %s, parent of %s", p.Parent(), p.ID())
				}
			}
			for i := len(path) - 1; i >= 0; i-- {
				inputsCopy := new(ids.Set)
				p := path[i]
				atx := vm.getAtomicTx(p.ethBlock)
				if atx != nil {
					inputs.Union(atx.UnsignedTx.(UnsignedAtomicTx).InputUTXOs())
					inputsCopy.Union(*inputs)
				}
				vm.blockAtomicInputCache.Put(p.ID(), inputsCopy)
			}
			for _, in := range atx.InputUTXOs().List() {
				if inputs.Contains(in) {
					return errInvalidBlock
				}
			}
		case *UnsignedExportTx:
		default:
			return errors.New("unknown atomic tx type")
		}

		if tx.UnsignedTx.(UnsignedAtomicTx).SemanticVerify(vm, tx) != nil {
			return errInvalidBlock
		}
	}
	_, 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()) }