aboutsummaryrefslogblamecommitdiff
path: root/consensus/dummy/consensus.go
blob: 92bd261d96b175ced0e4f91e3491b2e62250b881 (plain) (tree)
1
2
3
4
5
6
7
8
9
10


             






                                  
                                                    






                                                    
                                               

 



                                                                                                                                                                                                   
                                
                                                      
                                                 


                                                               

 
                         
                              


                                                          
                                   


     
                                                                                                                                           


     






                                                                           



                                                                                                                               
                                                                              

                                                                                                                   






                                                                                        

                                         
































                                                                                                                       


                                                                                                                                  












                                                                                             


                                                                               
                                   


                                                                                                           










                                                                    


                                                                                                                                            

















































                                                                                                     


                                                                                              













































                                                                                                                


                                                                                              
                  


                                                                                           

                                         

 








                                                                                    


                                                                                                                                               





                                                                                          
 

                                                                  

 





                                                                                                                                               


                                
              


                                                                            



                                          


















                                           


                                                                                                                  
                            

 





                                                                            


                                        
                  
 
package dummy

import (
	"errors"
	"fmt"
	"golang.org/x/crypto/sha3"
	"math/big"
	"runtime"
	"time"

	myparams "github.com/ava-labs/coreth/params"
	"github.com/ava-labs/go-ethereum/common"
	"github.com/ava-labs/go-ethereum/consensus"
	"github.com/ava-labs/go-ethereum/core/state"
	"github.com/ava-labs/go-ethereum/core/types"
	"github.com/ava-labs/go-ethereum/params"
	"github.com/ava-labs/go-ethereum/rlp"
	"github.com/ava-labs/go-ethereum/rpc"
	mapset "github.com/deckarep/golang-set"
)

type OnFinalizeCallbackType = func(chain consensus.ChainReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header)
type OnFinalizeAndAssembleCallbackType = func(chain consensus.ChainReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header, receipts []*types.Receipt)
type OnAPIsCallbackType = func(consensus.ChainReader) []rpc.API

type ConsensusCallbacks struct {
	OnSeal                func(*types.Block) error
	OnSealHash            func(*types.Header)
	OnAPIs                OnAPIsCallbackType
	OnFinalize            OnFinalizeCallbackType
	OnFinalizeAndAssemble OnFinalizeAndAssembleCallbackType
}

type DummyEngine struct {
	cb *ConsensusCallbacks
}

func NewDummyEngine(cb *ConsensusCallbacks) *DummyEngine {
	return &DummyEngine{cb: cb}
}

var (
	allowedFutureBlockTime = 15 * time.Second // Max time from current time allowed for blocks, before they're considered future blocks
)

var (
	maxUncles = 2 // Maximum number of uncles allowed in a single block

	errTooManyUncles   = errors.New("too many uncles")
	errZeroBlockTime   = errors.New("timestamp equals parent's")
	errDuplicateUncle  = errors.New("duplicate uncle")
	errUncleIsAncestor = errors.New("uncle is ancestor")
	errDanglingUncle   = errors.New("uncle's parent is not ancestor")
)

// modified from consensus.go
func (self *DummyEngine) verifyHeader(chain consensus.ChainReader, header, parent *types.Header, uncle bool, seal bool) error {
	// Ensure that the header's extra-data section is of a reasonable size
	if uint64(len(header.Extra)) > myparams.MaximumExtraDataSize {
		return fmt.Errorf("extra-data too long: %d > %d", len(header.Extra), myparams.MaximumExtraDataSize)
	}
	// Verify the header's timestamp
	if !uncle {
		if header.Time > uint64(time.Now().Add(allowedFutureBlockTime).Unix()) {
			return consensus.ErrFutureBlock
		}
	}
	//if header.Time <= parent.Time {
	if header.Time < parent.Time {
		return errZeroBlockTime
	}
	// Verify that the gas limit is <= 2^63-1
	cap := uint64(0x7fffffffffffffff)
	if header.GasLimit > cap {
		return fmt.Errorf("invalid gasLimit: have %v, max %v", header.GasLimit, cap)
	}
	// Verify that the gasUsed is <= gasLimit
	if header.GasUsed > header.GasLimit {
		return fmt.Errorf("invalid gasUsed: have %d, gasLimit %d", header.GasUsed, header.GasLimit)
	}

	// Verify that the gas limit remains within allowed bounds
	diff := int64(parent.GasLimit) - int64(header.GasLimit)
	if diff < 0 {
		diff *= -1
	}
	limit := parent.GasLimit / params.GasLimitBoundDivisor

	if uint64(diff) >= limit || header.GasLimit < params.MinGasLimit {
		return fmt.Errorf("invalid gas limit: have %d, want %d += %d", header.<