github.com/MetalBlockchain/metalgo@v1.11.9/snow/engine/snowman/bootstrap/acceptor.go (about) 1 // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. 2 // See the file LICENSE for licensing terms. 3 4 package bootstrap 5 6 import ( 7 "context" 8 9 "github.com/prometheus/client_golang/prometheus" 10 11 "github.com/MetalBlockchain/metalgo/snow" 12 "github.com/MetalBlockchain/metalgo/snow/consensus/snowman" 13 "github.com/MetalBlockchain/metalgo/snow/engine/snowman/block" 14 ) 15 16 var ( 17 _ block.Parser = (*parseAcceptor)(nil) 18 _ snowman.Block = (*blockAcceptor)(nil) 19 ) 20 21 type parseAcceptor struct { 22 parser block.Parser 23 ctx *snow.ConsensusContext 24 numAccepted prometheus.Counter 25 } 26 27 func (p *parseAcceptor) ParseBlock(ctx context.Context, bytes []byte) (snowman.Block, error) { 28 blk, err := p.parser.ParseBlock(ctx, bytes) 29 if err != nil { 30 return nil, err 31 } 32 return &blockAcceptor{ 33 Block: blk, 34 ctx: p.ctx, 35 numAccepted: p.numAccepted, 36 }, nil 37 } 38 39 type blockAcceptor struct { 40 snowman.Block 41 42 ctx *snow.ConsensusContext 43 numAccepted prometheus.Counter 44 } 45 46 func (b *blockAcceptor) Accept(ctx context.Context) error { 47 if err := b.ctx.BlockAcceptor.Accept(b.ctx, b.ID(), b.Bytes()); err != nil { 48 return err 49 } 50 err := b.Block.Accept(ctx) 51 b.numAccepted.Inc() 52 return err 53 }