Skip to content

Commit

Permalink
pchain sdk integration
Browse files Browse the repository at this point in the history
Signed-off-by: Joshua Kim <[email protected]>
  • Loading branch information
joshua-kim committed Dec 14, 2023
1 parent a277f31 commit 82b8655
Show file tree
Hide file tree
Showing 9 changed files with 527 additions and 83 deletions.
18 changes: 15 additions & 3 deletions vms/platformvm/block/builder/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ type environment struct {
Builder
blkManager blockexecutor.Manager
mempool mempool.Mempool
network network.Network
network *network.Network // TODO remove
sender *common.SenderTest

isBootstrapped *utils.Atomic[bool]
Expand Down Expand Up @@ -164,7 +164,12 @@ func newEnvironment(t *testing.T) *environment {
}

registerer := prometheus.NewRegistry()
res.sender = &common.SenderTest{T: t}
res.sender = &common.SenderTest{
T: t,
SendAppGossipF: func(context.Context, []byte) error {
return nil
},
}

metrics, err := metrics.New("", registerer)
require.NoError(err)
Expand All @@ -180,13 +185,20 @@ func newEnvironment(t *testing.T) *environment {
pvalidators.TestManager,
)

res.network = network.New(
res.network, err = network.New(
res.backend.Ctx,
res.blkManager,
res.mempool,
res.backend.Config.PartialSyncPrimaryNetwork,
res.sender,
time.Second,
100,
10,
0.1,
0.5,
registerer,
)
require.NoError(err)

res.Builder = New(
res.mempool,
Expand Down
109 changes: 109 additions & 0 deletions vms/platformvm/network/gossip.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package network

import (
"context"
"fmt"
"time"

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/network/p2p"
"github.com/ava-labs/avalanchego/network/p2p/gossip"
blockexecutor "github.com/ava-labs/avalanchego/vms/platformvm/block/executor"
"github.com/ava-labs/avalanchego/vms/platformvm/txs"
"github.com/ava-labs/avalanchego/vms/platformvm/txs/mempool"
)

var (
_ p2p.Handler = (*txGossipHandler)(nil)
_ gossip.Set[*txs.Tx] = (*verifierMempool)(nil)
)

// txGossipHandler is the handler called when serving gossip messages
type txGossipHandler struct {
p2p.NoOpHandler
appGossipHandler p2p.Handler
appRequestHandler p2p.Handler
}

func (t txGossipHandler) AppGossip(
ctx context.Context,
nodeID ids.NodeID,
gossipBytes []byte,
) {
t.appGossipHandler.AppGossip(ctx, nodeID, gossipBytes)
}

func (t txGossipHandler) AppRequest(
ctx context.Context,
nodeID ids.NodeID,
deadline time.Time,
requestBytes []byte,
) ([]byte, error) {
return t.appRequestHandler.AppRequest(ctx, nodeID, deadline, requestBytes)
}

func newVerifierMempool(
mempool mempool.Mempool,
verifier blockexecutor.Manager,
bloomMaxItems uint64,
bloomFalsePositiveRate float64,
bloomMaxFalsePositiveRate float64,
) (*verifierMempool, error) {
bloomFilter, err := gossip.NewBloomFilter(
bloomMaxItems,
bloomFalsePositiveRate,
)
if err != nil {
return nil, err
}

return &verifierMempool{
mempool: mempool,
verifier: verifier,
bloomFilter: bloomFilter,
bloomMaxFalsePositiveRate: bloomMaxFalsePositiveRate,
}, nil
}

type Verifier interface {
Verify(tx *txs.Tx) error
}

// verifierMempool performs verification before adding something to the mempool
type verifierMempool struct {
mempool mempool.Mempool
verifier blockexecutor.Manager
bloomFilter *gossip.BloomFilter
bloomMaxFalsePositiveRate float64
}

func (v *verifierMempool) Add(tx *txs.Tx) error {
if err := v.verifier.VerifyTx(tx); err != nil {
return fmt.Errorf("tx %s failed verification: %w", tx.ID(), err)
}

if err := v.mempool.Add(tx); err != nil {
return fmt.Errorf("could not add tx %s to mempool: %w", tx.ID(), err)
}

v.bloomFilter.Add(tx)

ok, err := gossip.ResetBloomFilterIfNeeded(v.bloomFilter, v.bloomMaxFalsePositiveRate)
if ok {
v.Iterate(func(tx *txs.Tx) bool {
v.bloomFilter.Add(tx)
return true
})
}

return err
}

func (v *verifierMempool) Iterate(f func(tx *txs.Tx) bool) {
v.mempool.Iterate(f)
}

func (v *verifierMempool) GetFilter() (bloom []byte, salt []byte, err error) {
bloomBytes, err := v.bloomFilter.Bloom.MarshalBinary()
return bloomBytes, v.bloomFilter.Salt[:], err
}
Loading

0 comments on commit 82b8655

Please sign in to comment.