Skip to content

Commit

Permalink
Working zone to zone
Browse files Browse the repository at this point in the history
  • Loading branch information
alanorwick committed Oct 26, 2021
1 parent 3896177 commit 2cf6ea8
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 21 deletions.
6 changes: 3 additions & 3 deletions consensus/clique/clique.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"sync"
"time"

lru "github.com/hashicorp/golang-lru"
"github.com/spruce-solutions/go-quai/accounts"
"github.com/spruce-solutions/go-quai/common"
"github.com/spruce-solutions/go-quai/common/hexutil"
Expand All @@ -41,7 +42,6 @@ import (
"github.com/spruce-solutions/go-quai/rlp"
"github.com/spruce-solutions/go-quai/rpc"
"github.com/spruce-solutions/go-quai/trie"
lru "github.com/hashicorp/golang-lru"
"golang.org/x/crypto/sha3"
)

Expand Down Expand Up @@ -592,7 +592,7 @@ func (c *Clique) FinalizeAndAssemble(chain consensus.ChainHeaderReader, header *
}

// GetExternalBlocks traces all available branches to find external blocks
func (c *Clique) GetExternalBlocks(chain consensus.ChainHeaderReader, header *types.Header) []*types.ExternalBlock {
func (c *Clique) GetExternalBlocks(chain consensus.ChainHeaderReader, header *types.Header, logging bool) []*types.ExternalBlock {
return make([]*types.ExternalBlock, 0)
}

Expand All @@ -607,7 +607,7 @@ func (c *Clique) GetStopHash(chain consensus.ChainHeaderReader, difficultyContex
}

// TraceBranch recursively traces branches to find
func (c *Clique) TraceBranch(chain consensus.ChainHeaderReader, header *types.Header, context int, stopHash common.Hash, originalContext int, originalLocation []byte) []*types.ExternalBlock {
func (c *Clique) TraceBranch(chain consensus.ChainHeaderReader, header *types.Header, context int, stopHash common.Hash, originalContext int, originalLocation []byte, logging bool) []*types.ExternalBlock {
return make([]*types.ExternalBlock, 0)
}

Expand Down
4 changes: 2 additions & 2 deletions consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ type Engine interface {
uncles []*types.Header, receipts []*types.Receipt) (*types.Block, error)

// GetExternalBlocks retrieves all valid external blocks from external chains
GetExternalBlocks(chain ChainHeaderReader, header *types.Header) []*types.ExternalBlock
GetExternalBlocks(chain ChainHeaderReader, header *types.Header, logging bool) []*types.ExternalBlock

// GetCoincidentHeader retrieves the furthest coincident header back
GetCoincidentHeader(chain ChainHeaderReader, context int, header *types.Header) (*types.Header, int)
Expand All @@ -113,7 +113,7 @@ type Engine interface {
GetStopHash(chain ChainHeaderReader, difficultyContext int, originalContext int, startingHeader *types.Header) common.Hash

// TraceBranch recursively traces branches to find
TraceBranch(chain ChainHeaderReader, header *types.Header, context int, stopHash common.Hash, originalContext int, originalLocation []byte) []*types.ExternalBlock
TraceBranch(chain ChainHeaderReader, header *types.Header, context int, stopHash common.Hash, originalContext int, originalLocation []byte, logging bool) []*types.ExternalBlock

// Seal generates a new sealing request for the given input block and pushes
// the result into the given channel.
Expand Down
25 changes: 17 additions & 8 deletions consensus/ethash/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,7 @@ func (ethash *Ethash) GetStopHash(chain consensus.ChainHeaderReader, difficultyC
return stopHash
}

func (ethash *Ethash) TraceBranch(chain consensus.ChainHeaderReader, header *types.Header, context int, stopHash common.Hash, originalContext int, originalLocation []byte) []*types.ExternalBlock {
func (ethash *Ethash) TraceBranch(chain consensus.ChainHeaderReader, header *types.Header, context int, stopHash common.Hash, originalContext int, originalLocation []byte, logging bool) []*types.ExternalBlock {
extBlocks := make([]*types.ExternalBlock, 0)
steppedBack := false
startingHeader := header
Expand All @@ -764,7 +764,9 @@ func (ethash *Ethash) TraceBranch(chain consensus.ChainHeaderReader, header *typ

// If we have reached a coincident block or we're in Region and found the prev region
if difficultyContext < context && steppedBack {
log.Info("TraceBranch: Found coincident block", "number", header.Number, "context", context, "location", header.Location)
if logging {
log.Info("TraceBranch: Found coincident block", "number", header.Number, "context", context, "location", header.Location)
}
break
}

Expand All @@ -782,17 +784,21 @@ func (ethash *Ethash) TraceBranch(chain consensus.ChainHeaderReader, header *typ
log.Warn("TraceBranch: External Block not found for header", "number", header.Number[context], "context", context)
break
}
log.Info("GetExternalBlocks: Block being added: ", "number", extBlock.Header().Number, "context", extBlock.Context(), "location", extBlock.Header().Location, "txs", len(extBlock.Transactions()))
if logging {
log.Info("GetExternalBlocks: Block being added: ", "number", extBlock.Header().Number, "context", extBlock.Context(), "location", extBlock.Header().Location, "txs", len(extBlock.Transactions()))
}
extBlocks = append(extBlocks, extBlock)
}

if context < types.ContextDepth-1 {
extBlocks = append(extBlocks, ethash.TraceBranch(chain, header, context+1, stopHash, originalContext, originalLocation)...)
extBlocks = append(extBlocks, ethash.TraceBranch(chain, header, context+1, stopHash, originalContext, originalLocation, logging)...)
}

// Stop at the stop hash before iterating downward
if header.ParentHash[context] == stopHash || header.Number[context].Cmp(big.NewInt(1)) <= 0 {
log.Info("TraceBranch: Stopping on stop hash", "number", header.Number, "context", context, "location", header.Location)
if logging {
log.Info("TraceBranch: Stopping on stop hash", "number", header.Number, "context", context, "location", header.Location)
}
break
}

Expand All @@ -808,7 +814,10 @@ func (ethash *Ethash) TraceBranch(chain consensus.ChainHeaderReader, header *typ
}
prevHeader = extBlock.Header()
// In Regions the starting header needs to be broken off for N-1
if context < types.ContextDepth-1 && bytes.Equal(startingHeader.Location, prevHeader.Location) && originalContext != 0 {
if context < types.ContextDepth-1 && bytes.Equal(startingHeader.Location, prevHeader.Location) && originalContext != 0 && sameLocation {
if logging {
log.Info("TraceBranch: Stopping with Region N-1", "number", header.Number, "context", context, "location", header.Location)
}
break
}
}
Expand All @@ -819,7 +828,7 @@ func (ethash *Ethash) TraceBranch(chain consensus.ChainHeaderReader, header *typ
}

// GetExternalBlocks traces all available branches to find external blocks
func (ethash *Ethash) GetExternalBlocks(chain consensus.ChainHeaderReader, header *types.Header) []*types.ExternalBlock {
func (ethash *Ethash) GetExternalBlocks(chain consensus.ChainHeaderReader, header *types.Header, logging bool) []*types.ExternalBlock {
context := chain.Config().Context // Index that node is currently at
externalBlocks := make([]*types.ExternalBlock, 0)

Expand All @@ -833,7 +842,7 @@ func (ethash *Ethash) GetExternalBlocks(chain consensus.ChainHeaderReader, heade
return externalBlocks
}
stopHash := ethash.GetStopHash(chain, difficultyContext, context, coincidentHeader)
externalBlocks = append(externalBlocks, ethash.TraceBranch(chain, coincidentHeader, difficultyContext, stopHash, context, header.Location)...)
externalBlocks = append(externalBlocks, ethash.TraceBranch(chain, coincidentHeader, difficultyContext, stopHash, context, header.Location, logging)...)
}

// Swap order of external blocks
Expand Down
4 changes: 2 additions & 2 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"time"

"github.com/VictoriaMetrics/fastcache"
lru "github.com/hashicorp/golang-lru"
"github.com/spruce-solutions/go-quai/common"
"github.com/spruce-solutions/go-quai/common/mclock"
"github.com/spruce-solutions/go-quai/common/prque"
Expand All @@ -46,7 +47,6 @@ import (
"github.com/spruce-solutions/go-quai/params"
"github.com/spruce-solutions/go-quai/rlp"
"github.com/spruce-solutions/go-quai/trie"
lru "github.com/hashicorp/golang-lru"
)

var (
Expand Down Expand Up @@ -2505,7 +2505,7 @@ func (bc *BlockChain) GetExternalBlocks(header *types.Header) ([]*types.External
return externalBlocks, nil
}
stopHash := bc.engine.GetStopHash(bc, difficultyContext, context, coincidentHeader)
externalBlocks = append(externalBlocks, bc.engine.TraceBranch(bc, coincidentHeader, difficultyContext, stopHash, context, header.Location)...)
externalBlocks = append(externalBlocks, bc.engine.TraceBranch(bc, coincidentHeader, difficultyContext, stopHash, context, header.Location, false)...)
}

return externalBlocks, nil
Expand Down
5 changes: 2 additions & 3 deletions core/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,14 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg

// Gather external blocks and apply transactions, need to trace own local external block cache based on cache to validate
i := 0
externalBlocks := p.engine.GetExternalBlocks(p.bc, header)
externalBlocks := p.engine.GetExternalBlocks(p.bc, header, true)
etxs := 0
for _, externalBlock := range externalBlocks {
externalBlock.Receipts().DeriveFields(p.config, externalBlock.Hash(), externalBlock.Header().Number[externalBlock.Context().Int64()].Uint64(), externalBlock.Transactions())
etxs += len(externalBlock.Transactions())
for _, tx := range externalBlock.Transactions() {
msg, err := tx.AsMessage(types.MakeSigner(p.config, header.Number[types.QuaiNetworkContext]), header.BaseFee[types.QuaiNetworkContext])
// Quick check to make sure we're adding an external transaction, currently saves us from not passing merkel path in external block
if !msg.FromExternal() {
log.Info("Process: Message is not from an external address", "from", msg.From())
continue
}
if err != nil {
Expand All @@ -99,6 +97,7 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg
}
receipts = append(receipts, receipt)
allLogs = append(allLogs, receipt.Logs...)
etxs += 1
i++
}
}
Expand Down
4 changes: 2 additions & 2 deletions ethclient/ethclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"fmt"
"math/big"

"github.com/spruce-solutions/go-quai"
ethereum "github.com/spruce-solutions/go-quai"
"github.com/spruce-solutions/go-quai/common"
"github.com/spruce-solutions/go-quai/common/hexutil"
"github.com/spruce-solutions/go-quai/core/types"
Expand Down Expand Up @@ -163,7 +163,7 @@ func (ec *Client) getBlockWithReceipts(ctx context.Context, method string, args
return nil, reqs[i].Error
}
if uncles[i] == nil {
return nil, fmt.Errorf("getBlockWithReceipts: got null header for uncle %d of block %x", i, body.Hash[:])
return nil, fmt.Errorf("getBlockWithReceipts: got null header for uncle %d with hash %x", i, body.UncleHashes[i])
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion miner/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -993,7 +993,7 @@ func (w *worker) commitNewWork(interrupt *int32, noempty bool, timestamp int64)
}

// Gather external blocks and apply transactions
externalBlocks := w.engine.GetExternalBlocks(w.chain, header)
externalBlocks := w.engine.GetExternalBlocks(w.chain, header, false)
etxs := make(types.Transactions, 0)
for _, externalBlock := range externalBlocks {
externalBlock.Receipts().DeriveFields(w.chainConfig, externalBlock.Hash(), externalBlock.Header().Number[externalBlock.Context().Int64()].Uint64(), externalBlock.Transactions())
Expand Down

0 comments on commit 2cf6ea8

Please sign in to comment.