Skip to content

Commit

Permalink
Generalize expiry based de-duplication (#1782)
Browse files Browse the repository at this point in the history
  • Loading branch information
tsachiherman authored Nov 21, 2024
1 parent 7ab827d commit 07c1df9
Show file tree
Hide file tree
Showing 16 changed files with 316 additions and 269 deletions.
6 changes: 3 additions & 3 deletions chain/accepter.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import (

type Accepter struct {
tracer trace.Tracer
validityWindow *TimeValidityWindow
validityWindow ValidityWindow
metrics *chainMetrics
}

func NewAccepter(
tracer trace.Tracer,
validityWindow *TimeValidityWindow,
validityWindow ValidityWindow,
metrics *chainMetrics,
) *Accepter {
return &Accepter{
Expand All @@ -31,7 +31,7 @@ func (a *Accepter) AcceptBlock(ctx context.Context, blk *ExecutionBlock) error {
_, span := a.tracer.Start(ctx, "Chain.AcceptBlock")
defer span.End()

a.metrics.txsAccepted.Add(float64(len(blk.Txs)))
a.metrics.txsAccepted.Add(float64(len(blk.StatelessBlock.Txs)))
a.validityWindow.Accept(blk)

return nil
Expand Down
5 changes: 4 additions & 1 deletion chain/assembler.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ func (a *Assembler) AssembleBlock(
if err != nil {
return nil, nil, err
}
executionBlock := NewExecutionBlock(sb)
executionBlock, err := NewExecutionBlock(sb)
if err != nil {
return nil, nil, err
}
return a.processor.Execute(ctx, parentView, executionBlock)
}
10 changes: 7 additions & 3 deletions chain/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ type Builder struct {
metadataManager MetadataManager
balanceHandler BalanceHandler
mempool Mempool
validityWindow *TimeValidityWindow
validityWindow ValidityWindow
metrics *chainMetrics
config Config
}
Expand All @@ -79,7 +79,7 @@ func NewBuilder(
metadataManager MetadataManager,
balanceHandler BalanceHandler,
mempool Mempool,
validityWindow *TimeValidityWindow,
validityWindow ValidityWindow,
metrics *chainMetrics,
config Config,
) *Builder {
Expand Down Expand Up @@ -476,7 +476,11 @@ func (c *Builder) BuildBlock(ctx context.Context, parentView state.View, parent
zap.Int64("parent (t)", parent.Tmstmp),
zap.Int64("block (t)", timestamp),
)
return NewExecutionBlock(blk), &ExecutedBlock{
execBlock, err := NewExecutionBlock(blk)
if err != nil {
return nil, nil, nil, err
}
return execBlock, &ExecutedBlock{
Block: blk,
Results: results,
UnitPrices: feeManager.UnitPrices(),
Expand Down
6 changes: 5 additions & 1 deletion chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@ import (
"github.com/ava-labs/avalanchego/x/merkledb"
"github.com/prometheus/client_golang/prometheus"

"github.com/ava-labs/hypersdk/internal/validitywindow"
"github.com/ava-labs/hypersdk/internal/workers"
"github.com/ava-labs/hypersdk/state"
)

// create a type alias for the concrete TimeWindowWindow type.
type ValidityWindow = *validitywindow.TimeValidityWindow[*Transaction]

type Chain struct {
builder *Builder
processor *Processor
Expand All @@ -34,7 +38,7 @@ func NewChain(
balanceHandler BalanceHandler,
authVerifiers workers.Workers,
authVM AuthVM,
validityWindow *TimeValidityWindow,
validityWindow ValidityWindow,
config Config,
) (*Chain, error) {
metrics, err := newMetrics(registerer)
Expand Down
2 changes: 1 addition & 1 deletion chain/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ func (b *BlockParser) ParseBlock(ctx context.Context, bytes []byte) (*ExecutionB
if err != nil {
return nil, err
}
return NewExecutionBlock(blk), nil
return NewExecutionBlock(blk)
}
4 changes: 2 additions & 2 deletions chain/pre_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ import (

type PreExecutor struct {
ruleFactory RuleFactory
validityWindow *TimeValidityWindow
validityWindow ValidityWindow
metadataManager MetadataManager
balanceHandler BalanceHandler
}

func NewPreExecutor(
ruleFactory RuleFactory,
validityWindow *TimeValidityWindow,
validityWindow ValidityWindow,
metadataManager MetadataManager,
balanceHandler BalanceHandler,
) *PreExecutor {
Expand Down
66 changes: 39 additions & 27 deletions chain/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,42 @@ type ExecutionBlock struct {
sigJob workers.Job
}

func NewExecutionBlock(block *StatelessBlock) *ExecutionBlock {
func NewExecutionBlock(block *StatelessBlock) (*ExecutionBlock, error) {
authCounts := make(map[uint8]int)
txsSet := set.NewSet[ids.ID](len(block.Txs))
for _, tx := range block.Txs {
if txsSet.Contains(tx.ID()) {
return nil, ErrDuplicateTx
}
txsSet.Add(tx.ID())
authCounts[tx.Auth.GetTypeID()]++
}

return &ExecutionBlock{
authCounts: authCounts,
txsSet: txsSet,
StatelessBlock: block,
}
}, nil
}

func (b *ExecutionBlock) initTxs() error {
if b.txsSet.Len() == len(b.Txs) {
return nil
}
b.authCounts = make(map[uint8]int)
b.txsSet = set.NewSet[ids.ID](len(b.Txs))
for _, tx := range b.Txs {
if b.txsSet.Contains(tx.ID()) {
return ErrDuplicateTx
}
b.txsSet.Add(tx.ID())
b.authCounts[tx.Auth.GetTypeID()]++
}
func (b *ExecutionBlock) ContainsTx(id ids.ID) bool {
return b.txsSet.Contains(id)
}

return nil
func (b *ExecutionBlock) Height() uint64 {
return b.Hght
}

func (b *ExecutionBlock) Parent() ids.ID {
return b.Prnt
}

func (b *ExecutionBlock) Timestamp() int64 {
return b.Tmstmp
}

func (b *ExecutionBlock) Txs() []*Transaction {
return b.StatelessBlock.Txs
}

type Processor struct {
Expand All @@ -66,7 +81,7 @@ type Processor struct {
authVM AuthVM
metadataManager MetadataManager
balanceHandler BalanceHandler
validityWindow *TimeValidityWindow
validityWindow ValidityWindow
metrics *chainMetrics
config Config
}
Expand All @@ -79,7 +94,7 @@ func NewProcessor(
authVM AuthVM,
metadataManager MetadataManager,
balanceHandler BalanceHandler,
validityWindow *TimeValidityWindow,
validityWindow ValidityWindow,
metrics *chainMetrics,
config Config,
) *Processor {
Expand Down Expand Up @@ -150,7 +165,7 @@ func (p *Processor) Execute(
if b.Tmstmp < parentTimestamp+r.GetMinBlockGap() {
return nil, nil, ErrTimestampTooEarly
}
if len(b.Txs) == 0 && b.Tmstmp < parentTimestamp+r.GetMinEmptyBlockGap() {
if len(b.StatelessBlock.Txs) == 0 && b.Tmstmp < parentTimestamp+r.GetMinEmptyBlockGap() {
return nil, nil, ErrTimestampTooEarly
}

Expand Down Expand Up @@ -286,7 +301,7 @@ func (p *Processor) executeTxs(
defer span.End()

var (
numTxs = len(b.Txs)
numTxs = len(b.StatelessBlock.Txs)
t = b.Tmstmp

f = fetcher.New(im, numTxs, p.config.StateFetchConcurrency)
Expand All @@ -296,7 +311,7 @@ func (p *Processor) executeTxs(
)

// Fetch required keys and execute transactions
for li, ltx := range b.Txs {
for li, ltx := range b.StatelessBlock.Txs {
i := li
tx := ltx

Expand Down Expand Up @@ -361,7 +376,7 @@ func (p *Processor) executeTxs(
return nil, nil, err
}

p.metrics.txsVerified.Add(float64(len(b.Txs)))
p.metrics.txsVerified.Add(float64(len(b.StatelessBlock.Txs)))

// Return tstate that can be used to add block-level keys to state
return results, ts, nil
Expand All @@ -372,13 +387,10 @@ func (p *Processor) AsyncVerify(ctx context.Context, block *ExecutionBlock) erro
ctx, span := p.tracer.Start(ctx, "Chain.AsyncVerify")
defer span.End()

if err := block.initTxs(); err != nil {
return err
}
if block.sigJob != nil {
return nil
}
sigJob, err := p.authVerificationWorkers.NewJob(len(block.Txs))
sigJob, err := p.authVerificationWorkers.NewJob(len(block.StatelessBlock.Txs))
if err != nil {
return err
}
Expand All @@ -395,7 +407,7 @@ func (p *Processor) AsyncVerify(ctx context.Context, block *ExecutionBlock) erro
go batchVerifier.Done(func() { sigVerifySpan.End() })
}()

for _, tx := range block.Txs {
for _, tx := range block.StatelessBlock.Txs {
unsignedTxBytes, err := tx.UnsignedBytes()
if err != nil {
return err //nolint:spancheck
Expand Down
2 changes: 1 addition & 1 deletion chain/stateless_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,5 +147,5 @@ func NewGenesisBlock(root ids.ID) (*ExecutionBlock, error) {
if err != nil {
return nil, err
}
return NewExecutionBlock(sb), nil
return NewExecutionBlock(sb)
}
68 changes: 0 additions & 68 deletions chain/syncer.go

This file was deleted.

Loading

0 comments on commit 07c1df9

Please sign in to comment.