Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
dhrubabasu committed Aug 15, 2024
1 parent d3944b3 commit 8cb82af
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 6 deletions.
27 changes: 21 additions & 6 deletions vms/platformvm/block/executor/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,40 @@ func (*Block) ShouldVerifyWithContext(context.Context) (bool, error) {
return true, nil
}

func (b *Block) VerifyWithContext(_ context.Context, ctx *smblock.Context) error {
func (b *Block) VerifyWithContext(ctx context.Context, smCtx *smblock.Context) error {
pChainHeight := uint64(0)
if ctx != nil {
pChainHeight = ctx.PChainHeight
if smCtx != nil {
pChainHeight = smCtx.PChainHeight
}

blkID := b.ID()
if blkState, ok := b.manager.blkIDToState[blkID]; ok {
if !blkState.verifiedHeights.Contains(pChainHeight) {
// PlatformVM blocks are currently valid regardless of the ProposerVM's
// PChainHeight. If this changes, those validity checks should be done prior
// to adding [pChainHeight] to [verifiedHeights].
if err := b.Visit(&warpBlockVerifier{
ctx: ctx,
chainCtx: b.manager.ctx,
state: b.manager.state,
pChainHeight: pChainHeight,
}); err != nil {
return err
}

blkState.verifiedHeights.Add(pChainHeight)
}

// This block has already been verified.
return nil
}

if err := b.Visit(&warpBlockVerifier{
ctx: ctx,
chainCtx: b.manager.ctx,
state: b.manager.state,
pChainHeight: pChainHeight,
}); err != nil {
return err
}

return b.Visit(&verifier{
backend: b.manager.backend,
txExecutorBackend: b.manager.txExecutorBackend,
Expand Down
128 changes: 128 additions & 0 deletions vms/platformvm/block/executor/warp_verifier.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package executor

import (
"bytes"
"context"
"errors"
"fmt"

"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/vms/platformvm/block"
"github.com/ava-labs/avalanchego/vms/platformvm/state"
"github.com/ava-labs/avalanchego/vms/platformvm/txs"
)

var (
_ block.Visitor = (*warpBlockVerifier)(nil)
_ txs.Visitor = (*warpTxVerifier)(nil)
)

// warpBlockVerifier handles the logic for verifying all the warp message
// signatures in a block's transactions.
type warpBlockVerifier struct {
ctx context.Context
chainCtx *snow.Context
state state.State
pChainHeight uint64
}

// Pre-Banff did not contain any transactions with warp messages.
func (*warpBlockVerifier) ApricotAbortBlock(*block.ApricotAbortBlock) error { return nil }
func (*warpBlockVerifier) ApricotCommitBlock(*block.ApricotCommitBlock) error { return nil }
func (*warpBlockVerifier) ApricotProposalBlock(*block.ApricotProposalBlock) error { return nil }
func (*warpBlockVerifier) ApricotStandardBlock(*block.ApricotStandardBlock) error { return nil }
func (*warpBlockVerifier) ApricotAtomicBlock(*block.ApricotAtomicBlock) error { return nil }

// No transactions in these blocks.
func (*warpBlockVerifier) BanffAbortBlock(*block.BanffAbortBlock) error { return nil }
func (*warpBlockVerifier) BanffCommitBlock(*block.BanffCommitBlock) error { return nil }

func (v *warpBlockVerifier) BanffProposalBlock(b *block.BanffProposalBlock) error {
return v.verifyStandardTxs(b.Transactions)
}

func (v *warpBlockVerifier) BanffStandardBlock(b *block.BanffStandardBlock) error {
return v.verifyStandardTxs(b.Transactions)
}

func (v *warpBlockVerifier) verifyStandardTxs(txs []*txs.Tx) error {
for _, tx := range txs {
if err := tx.Unsigned.Visit(&warpTxVerifier{
ctx: v.ctx,
chainCtx: v.chainCtx,
state: v.state,
pChainHeight: v.pChainHeight,
}); err != nil {
return err
}
}
return nil
}

// warpTxVerifier handles the logic for verifying the warp message
// signature in a transaction.
type warpTxVerifier struct {
ctx context.Context
chainCtx *snow.Context
state state.State
pChainHeight uint64
}

func (*warpTxVerifier) AddDelegatorTx(*txs.AddDelegatorTx) error { return nil }
func (*warpTxVerifier) AddSubnetValidatorTx(*txs.AddSubnetValidatorTx) error { return nil }
func (*warpTxVerifier) AddValidatorTx(*txs.AddValidatorTx) error { return nil }
func (*warpTxVerifier) AdvanceTimeTx(*txs.AdvanceTimeTx) error { return nil }
func (*warpTxVerifier) BaseTx(*txs.BaseTx) error { return nil }
func (*warpTxVerifier) CreateChainTx(*txs.CreateChainTx) error { return nil }
func (*warpTxVerifier) CreateSubnetTx(*txs.CreateSubnetTx) error { return nil }
func (*warpTxVerifier) ExportTx(*txs.ExportTx) error { return nil }
func (*warpTxVerifier) ImportTx(*txs.ImportTx) error { return nil }
func (*warpTxVerifier) RemoveSubnetValidatorTx(*txs.RemoveSubnetValidatorTx) error { return nil }
func (*warpTxVerifier) RewardValidatorTx(*txs.RewardValidatorTx) error { return nil }
func (*warpTxVerifier) TransferSubnetOwnershipTx(*txs.TransferSubnetOwnershipTx) error { return nil }
func (*warpTxVerifier) TransformSubnetTx(*txs.TransformSubnetTx) error { return nil }
func (*warpTxVerifier) ConvertSubnetTx(*txs.ConvertSubnetTx) error { return nil }
func (*warpTxVerifier) AddPermissionlessDelegatorTx(*txs.AddPermissionlessDelegatorTx) error {
return nil
}

func (*warpTxVerifier) AddPermissionlessValidatorTx(*txs.AddPermissionlessValidatorTx) error {
return nil
}

func (v *warpTxVerifier) RegisterSubnetValidatorTx(tx *txs.RegisterSubnetValidatorTx) error {
if v.pChainHeight == 0 {
return errors.New("pChainHeight must be > 0")
}

chainID, addr, err := v.state.GetSubnetManager(tx.ParsedMessage.SubnetID)
if err != nil {
return fmt.Errorf("failed to lookup subnet manager for %s: %w", tx.ParsedMessage.SubnetID, err)
}

if tx.Message.SourceChainID != chainID {
return fmt.Errorf("mismatched chainIDs: expected %s, got %s", chainID, tx.Message.SourceChainID)
}

if !bytes.Equal(tx.SourceAddress, addr) {
return fmt.Errorf("mismatched addresses: expected %v, got %v", tx.SourceAddress, addr)
}

err = tx.Message.Signature.Verify(
v.ctx,
&tx.Message.UnsignedMessage,
v.chainCtx.NetworkID,
v.chainCtx.ValidatorState,
v.pChainHeight,
1,
2,
)
if err != nil {
return fmt.Errorf("failed to verify warp signature: %w", err)
}

return nil
}
7 changes: 7 additions & 0 deletions vms/platformvm/metrics/tx_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,13 @@ func (m *txMetrics) ConvertSubnetTx(*txs.ConvertSubnetTx) error {
return nil
}

func (m *txMetrics) RegisterSubnetValidatorTx(*txs.RegisterSubnetValidatorTx) error {
m.numTxs.With(prometheus.Labels{
txLabel: "register_subnet_validator",
}).Inc()
return nil
}

func (m *txMetrics) BaseTx(*txs.BaseTx) error {
m.numTxs.With(prometheus.Labels{
txLabel: "base",
Expand Down
2 changes: 2 additions & 0 deletions vms/platformvm/txs/register_subnet_validator_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type RegisterSubnetValidatorTx struct {
SyntacticallyVerified bool `json:"-"`

// Populated during syntactic verification
SourceAddress []byte `json:"-"`
ParsedMessage *message.RegisterSubnetValidator `json:"-"`
}

Expand Down Expand Up @@ -99,6 +100,7 @@ func (tx *RegisterSubnetValidatorTx) SyntacticVerify(ctx *snow.Context) error {

// cache that this is valid
tx.SyntacticallyVerified = true
tx.SourceAddress = addressedCall.SourceAddress
tx.ParsedMessage = msg
return nil
}
Expand Down

0 comments on commit 8cb82af

Please sign in to comment.