aboutsummaryrefslogblamecommitdiff
path: root/coreth.go
blob: ec60ce609234f01f12251c906c82345287301d9e (plain) (tree)
1
2
3
4
5
6
7
8







              



                              








                                                



                                            







                           
                         
                        





                         
                                                                                                                                           


     
                                                                  



                                                                                                                               











































                                                                                                               


                                                                                                                                  












                                                                                      







                                                                                                           










                                                                


                                                                                                                                            

















































                                                                                     










                                                                                              

                                     


                                                                                                                                    


                                                                                


                                                                                                                                               


                                                                                
 

                                                              


                                                                                                                                         


                                                          



                                                                            


















                                       


                                                                                                                  
                        










                                                                      
                

 
                                                                                                            
                      
                                   





                                               
                                       

                        
                         
                                                                                                        
     






                                         





                               











                                                                      




















                                                                      
package coreth

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

    "github.com/ethereum/go-ethereum/core/types"
    "github.com/ethereum/go-ethereum/common"
    //"github.com/ethereum/go-ethereum/eth"
    "github.com/ethereum/go-ethereum/event"
    "github.com/ethereum/go-ethereum/params"
    "github.com/ethereum/go-ethereum/consensus"
    "github.com/ethereum/go-ethereum/core/state"
    "github.com/Determinant/coreth/miner"
    "github.com/Determinant/coreth/eth"
    "github.com/ethereum/go-ethereum/rlp"
    "github.com/ethereum/go-ethereum/rpc"
    "github.com/Determinant/coreth/node"
    "github.com/ethereum/go-ethereum/crypto"
)

type Tx = types.Transaction
type Block = types.Block
type Hash = common.Hash

type ETHChain struct {
    mux *event.TypeMux
    backend *eth.Ethereum
    worker *miner.Worker
}

type DummyEngine struct {
}

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

var (
    errZeroBlockTime     = errors.New("timestamp equals parent's")
)

// 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)) > params.MaximumExtraDataSize {
        return fmt.Errorf("extra-data too long: %d > %d", len(header.Extra), params.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 {
        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.GasLimit, parent.GasLimit, limit)
    }
    // Verify that the block number is parent's +1
    if diff := new(big.Int).Sub(header.Number, parent.Number); diff.Cmp(big.NewInt(1)) != 0 {
        return consensus.ErrInvalidNumber
    }
    // Verify the engine specific seal securing the block
    if seal {
        if err := self.VerifySeal(chain, header); err != nil {
            return err
        }
    }
    return nil
}

func (self *DummyEngine) verifyHeaderWorker(chain consensus.ChainReader, headers []*types.Header, seals []bool, index int) error {
    var parent *types.Header
    if index == 0 {
        parent = chain.GetHeader(headers[0].ParentHash, headers[0].Number.Uint64()-1)
    } else if headers[index-1].Hash() == headers[index].ParentHash {
        parent = headers[index-1]
    }
    if parent == nil {
        return consensus.ErrUnknownAncestor
    }
    if chain.GetHeader(headers[index].Hash(), headers[index].Number.Uint64()) != nil {
        return nil // known block
    }
    return self.verifyHeader(chain, headers[index], parent, false, seals[index])
}

func (self *DummyEngine) Author(header *types.Header) (common.Address, error) {
    addr := common.Address{}
    return addr, nil
}

func (self *DummyEngine) VerifyHeader(chain consensus.ChainReader, header *types.Header, seal bool) error {
    // Short circuit if the header is known, or it's parent not
    number := header.Number.Uint64()
    if chain.GetHeader(header.Hash(), number) != nil {
        return nil
    }
    parent := chain.GetHeader(header.ParentHash, number-1)
    if