github.com/ava-labs/avalanchego@v1.11.11/vms/tracedvm/block.go (about) 1 // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. 2 // See the file LICENSE for licensing terms. 3 4 package tracedvm 5 6 import ( 7 "context" 8 "errors" 9 "fmt" 10 11 "go.opentelemetry.io/otel/attribute" 12 13 "github.com/ava-labs/avalanchego/snow/consensus/snowman" 14 "github.com/ava-labs/avalanchego/snow/engine/snowman/block" 15 16 oteltrace "go.opentelemetry.io/otel/trace" 17 ) 18 19 var ( 20 _ snowman.Block = (*tracedBlock)(nil) 21 _ snowman.OracleBlock = (*tracedBlock)(nil) 22 _ block.WithVerifyContext = (*tracedBlock)(nil) 23 24 errExpectedBlockWithVerifyContext = errors.New("expected block.WithVerifyContext") 25 ) 26 27 type tracedBlock struct { 28 snowman.Block 29 30 vm *blockVM 31 } 32 33 func (b *tracedBlock) Verify(ctx context.Context) error { 34 ctx, span := b.vm.tracer.Start(ctx, b.vm.verifyTag, oteltrace.WithAttributes( 35 attribute.Stringer("blkID", b.ID()), 36 attribute.Int64("height", int64(b.Height())), 37 )) 38 defer span.End() 39 40 return b.Block.Verify(ctx) 41 } 42 43 func (b *tracedBlock) Accept(ctx context.Context) error { 44 ctx, span := b.vm.tracer.Start(ctx, b.vm.acceptTag, oteltrace.WithAttributes( 45 attribute.Stringer("blkID", b.ID()), 46 attribute.Int64("height", int64(b.Height())), 47 )) 48 defer span.End() 49 50 return b.Block.Accept(ctx) 51 } 52 53 func (b *tracedBlock) Reject(ctx context.Context) error { 54 ctx, span := b.vm.tracer.Start(ctx, b.vm.rejectTag, oteltrace.WithAttributes( 55 attribute.Stringer("blkID", b.ID()), 56 attribute.Int64("height", int64(b.Height())), 57 )) 58 defer span.End() 59 60 return b.Block.Reject(ctx) 61 } 62 63 func (b *tracedBlock) Options(ctx context.Context) ([2]snowman.Block, error) { 64 oracleBlock, ok := b.Block.(snowman.OracleBlock) 65 if !ok { 66 return [2]snowman.Block{}, snowman.ErrNotOracle 67 } 68 69 ctx, span := b.vm.tracer.Start(ctx, b.vm.optionsTag, oteltrace.WithAttributes( 70 attribute.Stringer("blkID", b.ID()), 71 attribute.Int64("height", int64(b.Height())), 72 )) 73 defer span.End() 74 75 blks, err := oracleBlock.Options(ctx) 76 if err != nil { 77 return [2]snowman.Block{}, err 78 } 79 return [2]snowman.Block{ 80 &tracedBlock{ 81 Block: blks[0], 82 vm: b.vm, 83 }, 84 &tracedBlock{ 85 Block: blks[1], 86 vm: b.vm, 87 }, 88 }, nil 89 } 90 91 func (b *tracedBlock) ShouldVerifyWithContext(ctx context.Context) (bool, error) { 92 blkWithCtx, ok := b.Block.(block.WithVerifyContext) 93 if !ok { 94 return false, nil 95 } 96 97 ctx, span := b.vm.tracer.Start(ctx, b.vm.shouldVerifyWithContextTag, oteltrace.WithAttributes( 98 attribute.Stringer("blkID", b.ID()), 99 attribute.Int64("height", int64(b.Height())), 100 )) 101 defer span.End() 102 103 return blkWithCtx.ShouldVerifyWithContext(ctx) 104 } 105 106 func (b *tracedBlock) VerifyWithContext(ctx context.Context, blockCtx *block.Context) error { 107 blkWithCtx, ok := b.Block.(block.WithVerifyContext) 108 if !ok { 109 return fmt.Errorf("%w but got %T", errExpectedBlockWithVerifyContext, b.Block) 110 } 111 112 ctx, span := b.vm.tracer.Start(ctx, b.vm.verifyWithContextTag, oteltrace.WithAttributes( 113 attribute.Stringer("blkID", b.ID()), 114 attribute.Int64("height", int64(b.Height())), 115 attribute.Int64("pChainHeight", int64(blockCtx.PChainHeight)), 116 )) 117 defer span.End() 118 119 return blkWithCtx.VerifyWithContext(ctx, blockCtx) 120 }