Skip to content

Commit 16cf255

Browse files
Integrate stake weighted gossip selection (#511)
* Integrate stake weighted gossip selection * nit * merged * add validity check
1 parent 4e5dd87 commit 16cf255

File tree

11 files changed

+48
-21
lines changed

11 files changed

+48
-21
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.21
44

55
require (
66
github.com/VictoriaMetrics/fastcache v1.10.0
7-
github.com/ava-labs/avalanchego v1.11.3-codec-cleanup
7+
github.com/ava-labs/avalanchego v1.11.3-stake-weighted-gossip.2
88
github.com/cespare/cp v0.1.0
99
github.com/davecgh/go-spew v1.1.1
1010
github.com/deckarep/golang-set/v2 v2.1.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ github.com/ajg/form v1.5.1/go.mod h1:uL1WgH+h2mgNtvBq0339dVnzXdBETtL2LeUXaIv25UY
5454
github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156 h1:eMwmnE/GDgah4HI848JfFxHt+iPb26b4zyfspmqY0/8=
5555
github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156/go.mod h1:Cb/ax3seSYIx7SuZdm2G2xzfwmv3TPSk2ucNfQESPXM=
5656
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
57-
github.com/ava-labs/avalanchego v1.11.3-codec-cleanup h1:vDaeub6mMehtmoYKDNrRf1V2DqGQmbHG2nnJgfvQv0s=
58-
github.com/ava-labs/avalanchego v1.11.3-codec-cleanup/go.mod h1:oTVnF9idL57J4LM/6RByTmKhI4QvV6OCnF99ysyBljE=
57+
github.com/ava-labs/avalanchego v1.11.3-stake-weighted-gossip.2 h1:27eTGL+BfWj/QyeWA56rZazRFgO3Ydhq2sOuUjV3geY=
58+
github.com/ava-labs/avalanchego v1.11.3-stake-weighted-gossip.2/go.mod h1:LZlP3XIzML5IPwxs0jJdMDhTvTkNCakLVsPRHQYm0hI=
5959
github.com/aymerick/raymond v2.0.3-0.20180322193309-b565731e1464+incompatible/go.mod h1:osfaiScAUVup+UC9Nfq76eWqDhXlp+4UYaA8uhTBO6g=
6060
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
6161
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=

peer/network_test.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -871,7 +871,7 @@ type testAppSender struct {
871871
sendCrossChainAppResponseFn func(ids.ID, uint32, []byte) error
872872
sendAppRequestFn func(context.Context, set.Set[ids.NodeID], uint32, []byte) error
873873
sendAppResponseFn func(ids.NodeID, uint32, []byte) error
874-
sendAppGossipFn func([]byte, int, int, int) error
874+
sendAppGossipFn func(common.SendConfig, []byte) error
875875
}
876876

877877
func (t testAppSender) SendCrossChainAppRequest(_ context.Context, chainID ids.ID, requestID uint32, appRequestBytes []byte) error {
@@ -882,10 +882,6 @@ func (t testAppSender) SendCrossChainAppResponse(_ context.Context, chainID ids.
882882
return t.sendCrossChainAppResponseFn(chainID, requestID, appResponseBytes)
883883
}
884884

885-
func (t testAppSender) SendAppGossipSpecific(context.Context, set.Set[ids.NodeID], []byte) error {
886-
panic("not implemented")
887-
}
888-
889885
func (t testAppSender) SendAppRequest(ctx context.Context, nodeIDs set.Set[ids.NodeID], requestID uint32, message []byte) error {
890886
return t.sendAppRequestFn(ctx, nodeIDs, requestID, message)
891887
}
@@ -894,8 +890,8 @@ func (t testAppSender) SendAppResponse(_ context.Context, nodeID ids.NodeID, req
894890
return t.sendAppResponseFn(nodeID, requestID, message)
895891
}
896892

897-
func (t testAppSender) SendAppGossip(_ context.Context, message []byte, numValidators int, numNonValidators int, numPeers int) error {
898-
return t.sendAppGossipFn(message, numValidators, numNonValidators, numPeers)
893+
func (t testAppSender) SendAppGossip(_ context.Context, config common.SendConfig, message []byte) error {
894+
return t.sendAppGossipFn(config, message)
899895
}
900896

901897
func (t testAppSender) SendAppError(ctx context.Context, nodeID ids.NodeID, requestID uint32, errorCode int32, errorMessage string) error {

plugin/evm/config.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const (
3535
defaultMaxBlocksPerRequest = 0 // Default to no maximum on the number of blocks per getLogs request
3636
defaultContinuousProfilerFrequency = 15 * time.Minute
3737
defaultContinuousProfilerMaxFiles = 5
38+
defaultPushGossipPercentStake = .9
3839
defaultPushGossipNumValidators = 100
3940
defaultPushGossipNumPeers = 0
4041
defaultPushRegossipNumValidators = 10
@@ -155,6 +156,7 @@ type Config struct {
155156
KeystoreInsecureUnlockAllowed bool `json:"keystore-insecure-unlock-allowed"`
156157

157158
// Gossip Settings
159+
PushGossipPercentStake float64 `json:"push-gossip-percent-stake"`
158160
PushGossipNumValidators int `json:"push-gossip-num-validators"`
159161
PushGossipNumPeers int `json:"push-gossip-num-peers"`
160162
PushRegossipNumValidators int `json:"push-regossip-num-validators"`
@@ -259,6 +261,7 @@ func (c *Config) SetDefaults() {
259261
c.CommitInterval = defaultCommitInterval
260262
c.SnapshotWait = defaultSnapshotWait
261263
c.RegossipFrequency.Duration = defaultTxRegossipFrequency
264+
c.PushGossipPercentStake = defaultPushGossipPercentStake
262265
c.PushGossipNumValidators = defaultPushGossipNumValidators
263266
c.PushGossipNumPeers = defaultPushGossipNumPeers
264267
c.PushRegossipNumValidators = defaultPushRegossipNumValidators
@@ -315,6 +318,9 @@ func (c *Config) Validate() error {
315318
return fmt.Errorf("cannot use commit interval of 0 with pruning enabled")
316319
}
317320

321+
if c.PushGossipPercentStake < 0 || c.PushGossipPercentStake > 1 {
322+
return fmt.Errorf("push-gossip-percent-stake is %f but must be in the range [0, 1]", c.PushGossipPercentStake)
323+
}
318324
return nil
319325
}
320326

plugin/evm/gossiper_atomic_gossiping_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import (
1414
"github.com/ava-labs/avalanchego/utils/set"
1515
"github.com/stretchr/testify/assert"
1616

17+
commonEng "github.com/ava-labs/avalanchego/snow/engine/common"
18+
1719
"github.com/ava-labs/coreth/plugin/evm/message"
1820
)
1921

@@ -35,7 +37,7 @@ func TestMempoolAtmTxsAppGossipHandling(t *testing.T) {
3537
txRequested bool
3638
)
3739
sender.CantSendAppGossip = false
38-
sender.SendAppGossipF = func(context.Context, []byte, int, int, int) error {
40+
sender.SendAppGossipF = func(context.Context, commonEng.SendConfig, []byte) error {
3941
txGossipedLock.Lock()
4042
defer txGossipedLock.Unlock()
4143

@@ -122,7 +124,7 @@ func TestMempoolAtmTxsAppGossipHandlingDiscardedTx(t *testing.T) {
122124
txRequested bool
123125
)
124126
sender.CantSendAppGossip = false
125-
sender.SendAppGossipF = func(context.Context, []byte, int, int, int) error {
127+
sender.SendAppGossipF = func(context.Context, commonEng.SendConfig, []byte) error {
126128
txGossipedLock.Lock()
127129
defer txGossipedLock.Unlock()
128130

plugin/evm/gossiper_eth_gossiping_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import (
1717
"github.com/ava-labs/avalanchego/ids"
1818
"github.com/ava-labs/avalanchego/utils/set"
1919

20+
commonEng "github.com/ava-labs/avalanchego/snow/engine/common"
21+
2022
"github.com/ethereum/go-ethereum/common"
2123
"github.com/ethereum/go-ethereum/crypto"
2224
"github.com/ethereum/go-ethereum/rlp"
@@ -105,7 +107,7 @@ func TestMempoolEthTxsAppGossipHandling(t *testing.T) {
105107
return nil
106108
}
107109
wg.Add(1)
108-
sender.SendAppGossipF = func(context.Context, []byte, int, int, int) error {
110+
sender.SendAppGossipF = func(context.Context, commonEng.SendConfig, []byte) error {
109111
wg.Done()
110112
return nil
111113
}

plugin/evm/syncervm_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func TestStateSyncToggleEnabledToDisabled(t *testing.T) {
116116

117117
syncDisabledVM := &VM{}
118118
appSender := &commonEng.SenderTest{T: t}
119-
appSender.SendAppGossipF = func(context.Context, []byte, int, int, int) error { return nil }
119+
appSender.SendAppGossipF = func(context.Context, commonEng.SendConfig, []byte) error { return nil }
120120
appSender.SendAppRequestF = func(ctx context.Context, nodeSet set.Set[ids.NodeID], requestID uint32, request []byte) error {
121121
nodeID, hasItem := nodeSet.Pop()
122122
if !hasItem {

plugin/evm/tx_gossip_test.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,12 @@ func TestEthTxGossip(t *testing.T) {
9393
return 0, nil
9494
}
9595
validatorState.GetValidatorSetF = func(context.Context, uint64, ids.ID) (map[ids.NodeID]*validators.GetValidatorOutput, error) {
96-
return map[ids.NodeID]*validators.GetValidatorOutput{requestingNodeID: nil}, nil
96+
return map[ids.NodeID]*validators.GetValidatorOutput{
97+
requestingNodeID: {
98+
NodeID: requestingNodeID,
99+
Weight: 1,
100+
},
101+
}, nil
97102
}
98103

99104
// Ask the VM for any new transactions. We should get nothing at first.
@@ -220,7 +225,12 @@ func TestAtomicTxGossip(t *testing.T) {
220225
return 0, nil
221226
}
222227
validatorState.GetValidatorSetF = func(context.Context, uint64, ids.ID) (map[ids.NodeID]*validators.GetValidatorOutput, error) {
223-
return map[ids.NodeID]*validators.GetValidatorOutput{requestingNodeID: nil}, nil
228+
return map[ids.NodeID]*validators.GetValidatorOutput{
229+
requestingNodeID: {
230+
NodeID: requestingNodeID,
231+
Weight: 1,
232+
},
233+
}, nil
224234
}
225235

226236
// Ask the VM for any new transactions. We should get nothing at first.
@@ -296,6 +306,14 @@ func TestEthTxPushGossipOutbound(t *testing.T) {
296306
require := require.New(t)
297307
ctx := context.Background()
298308
snowCtx := utils.TestSnowContext()
309+
snowCtx.ValidatorState = &validators.TestState{
310+
GetCurrentHeightF: func(context.Context) (uint64, error) {
311+
return 0, nil
312+
},
313+
GetValidatorSetF: func(context.Context, uint64, ids.ID) (map[ids.NodeID]*validators.GetValidatorOutput, error) {
314+
return nil, nil
315+
},
316+
}
299317
sender := &common.FakeSender{
300318
SentAppGossip: make(chan []byte, 1),
301319
}

plugin/evm/vm.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,8 +1099,9 @@ func (vm *VM) initBlockBuilding() error {
10991099
}
11001100

11011101
pushGossipParams := gossip.BranchingFactor{
1102-
Validators: vm.config.PushGossipNumValidators,
1103-
Peers: vm.config.PushGossipNumPeers,
1102+
StakePercentage: vm.config.PushGossipPercentStake,
1103+
Validators: vm.config.PushGossipNumValidators,
1104+
Peers: vm.config.PushGossipNumPeers,
11041105
}
11051106
pushRegossipParams := gossip.BranchingFactor{
11061107
Validators: vm.config.PushRegossipNumValidators,
@@ -1112,6 +1113,7 @@ func (vm *VM) initBlockBuilding() error {
11121113
ethTxPushGossiper, err = gossip.NewPushGossiper[*GossipEthTx](
11131114
ethTxGossipMarshaller,
11141115
ethTxPool,
1116+
vm.validators,
11151117
ethTxGossipClient,
11161118
ethTxGossipMetrics,
11171119
pushGossipParams,
@@ -1130,6 +1132,7 @@ func (vm *VM) initBlockBuilding() error {
11301132
vm.atomicTxPushGossiper, err = gossip.NewPushGossiper[*GossipAtomicTx](
11311133
atomicTxGossipMarshaller,
11321134
vm.mempool,
1135+
vm.validators,
11331136
atomicTxGossipClient,
11341137
atomicTxGossipMetrics,
11351138
pushGossipParams,

plugin/evm/vm_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ func GenesisVM(t *testing.T,
253253
ctx, dbManager, genesisBytes, issuer, m := setupGenesis(t, genesisJSON)
254254
appSender := &commonEng.SenderTest{T: t}
255255
appSender.CantSendAppGossip = true
256-
appSender.SendAppGossipF = func(context.Context, []byte, int, int, int) error { return nil }
256+
appSender.SendAppGossipF = func(context.Context, commonEng.SendConfig, []byte) error { return nil }
257257
err := vm.Initialize(
258258
context.Background(),
259259
ctx,
@@ -3409,7 +3409,7 @@ func TestConfigureLogLevel(t *testing.T) {
34093409
ctx, dbManager, genesisBytes, issuer, _ := setupGenesis(t, test.genesisJSON)
34103410
appSender := &commonEng.SenderTest{T: t}
34113411
appSender.CantSendAppGossip = true
3412-
appSender.SendAppGossipF = func(context.Context, []byte, int, int, int) error { return nil }
3412+
appSender.SendAppGossipF = func(context.Context, commonEng.SendConfig, []byte) error { return nil }
34133413
err := vm.Initialize(
34143414
context.Background(),
34153415
ctx,

0 commit comments

Comments
 (0)