Skip to content

Commit 4be61bb

Browse files
authored
Merge branch 'dev' into stakers_model
2 parents 5a57560 + 2aa256b commit 4be61bb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+2153
-747
lines changed

CONTRIBUTING.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# How to Contribute to Avalanche
2+
3+
## Setup
4+
5+
To start developing on AvalancheGo, you'll need a few things installed.
6+
7+
- Golang version >= 1.19.6
8+
- gcc
9+
- g++
10+
11+
## Issues
12+
13+
### Security
14+
15+
- Do not open up a GitHub issue if it relates to a security vulnerability in AvalancheGo, and instead refer to our [security policy](./SECURITY.md).
16+
17+
### Did you fix whitespace, format code, or make a purely cosmetic patch?
18+
19+
- Changes from the community that are cosmetic in nature and do not add anything substantial to the stability, functionality, or testability of `avalanchego` will generally not be accepted.
20+
21+
### Making an Issue
22+
23+
- Check that the issue you're filing doesn't already exist by searching under [issues](https://github.com/ava-labs/avalanchego/issues).
24+
- If you're unable to find an open issue addressing the problem, [open a new one](https://github.com/ava-labs/avalanchego/issues/new/choose). Be sure to include a *title and clear description* with as much relevant information as possible.
25+
26+
## Features
27+
28+
- If you want to start a discussion about the development of a new feature or the modfiication of an existing one, start a thread under GitHub [discussions](https://github.com/ava-labs/avalanchego/discussions/categories/ideas).
29+
- Post a thread about your idea and why it should be added to AvalancheGo.
30+
- Don't start working on a pull request until you've received positive feedback from the maintainers.
31+
32+
## Pull Request Guidelines
33+
34+
- Open a new GitHub pull request containing your changes.
35+
- Ensure the PR description clearly describes the problem and solution. Include the relevant issue number if applicable.
36+
- The PR should be opened against the `dev` branch.
37+
- If your PR isn't ready to be reviewed just yet, you can open it as a draft to collect early feedback on your changes.
38+
- Once the PR is ready for review, mark it as ready-for-review and request review from one of the maintainers.
39+
40+
### Autogenerated code
41+
42+
- Any changes to protobuf message types require that protobuf files are regenerated.
43+
44+
```sh
45+
./scripts/protobuf_codegen.sh
46+
```
47+
48+
- To add or remove an interface that needs a corresponding mock generated, add it to the mock file [here](./scripts/mocks.mockgen.txt). You can regenerate the mocks by running the following script.
49+
50+
```sh
51+
./scripts/mock.gen.sh
52+
```
53+
54+
### Testing
55+
56+
#### Local
57+
58+
- Build the avalanchego binary
59+
60+
```sh
61+
./scripts/build.sh
62+
```
63+
64+
- Run unit tests
65+
66+
```sh
67+
./scripts/build_test.sh
68+
```
69+
70+
- Run the linter
71+
72+
```sh
73+
./scripts/lint.sh
74+
```
75+
76+
### Continuous Integration (CI)
77+
78+
- Pull requests will generally not be approved or merged unless they pass CI.
79+
80+
## Other
81+
82+
### Do you have questions about the source code?
83+
84+
- Ask any question about AvalancheGo under GitHub [discussions](https://github.com/ava-labs/avalanchego/discussions/categories/q-a).
85+
86+
### Do you want to contribute to the Avalanche documentation?
87+
88+
- Please check out the `avalanche-docs` repository [here](https://github.com/ava-labs/avalanche-docs).

database/rpcdb/db_client.go

Lines changed: 114 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ package rpcdb
66
import (
77
"context"
88
"encoding/json"
9+
"sync"
910

1011
"google.golang.org/protobuf/types/known/emptypb"
1112

1213
"github.com/ava-labs/avalanchego/database"
1314
"github.com/ava-labs/avalanchego/utils"
1415
"github.com/ava-labs/avalanchego/utils/set"
15-
"github.com/ava-labs/avalanchego/utils/wrappers"
1616

1717
rpcdbpb "github.com/ava-labs/avalanchego/proto/pb/rpcdb"
1818
)
@@ -108,10 +108,7 @@ func (db *DatabaseClient) NewIteratorWithStartAndPrefix(start, prefix []byte) da
108108
Err: err,
109109
}
110110
}
111-
return &iterator{
112-
db: db,
113-
id: resp.Id,
114-
}
111+
return newIterator(db, resp.Id)
115112
}
116113

117114
// Compact attempts to optimize the space utilization in the provided range
@@ -189,16 +186,85 @@ type iterator struct {
189186
db *DatabaseClient
190187
id uint64
191188

192-
data []*rpcdbpb.PutRequest
193-
errs wrappers.Errs
189+
data []*rpcdbpb.PutRequest
190+
fetchedData chan []*rpcdbpb.PutRequest
191+
192+
errLock sync.RWMutex
193+
err error
194+
195+
reqUpdateError chan chan struct{}
196+
197+
once sync.Once
198+
onClose chan struct{}
199+
onClosed chan struct{}
200+
}
201+
202+
func newIterator(db *DatabaseClient, id uint64) *iterator {
203+
it := &iterator{
204+
db: db,
205+
id: id,
206+
fetchedData: make(chan []*rpcdbpb.PutRequest),
207+
reqUpdateError: make(chan chan struct{}),
208+
onClose: make(chan struct{}),
209+
onClosed: make(chan struct{}),
210+
}
211+
go it.fetch()
212+
return it
213+
}
214+
215+
// Invariant: fetch is the only thread with access to send requests to the
216+
// server's iterator. This is needed because iterators are not thread safe and
217+
// the server expects the client (us) to only ever issue one request at a time
218+
// for a given iterator id.
219+
func (it *iterator) fetch() {
220+
defer func() {
221+
resp, err := it.db.client.IteratorRelease(context.Background(), &rpcdbpb.IteratorReleaseRequest{
222+
Id: it.id,
223+
})
224+
if err != nil {
225+
it.setError(err)
226+
} else {
227+
it.setError(errEnumToError[resp.Err])
228+
}
229+
230+
close(it.fetchedData)
231+
close(it.onClosed)
232+
}()
233+
234+
for {
235+
resp, err := it.db.client.IteratorNext(context.Background(), &rpcdbpb.IteratorNextRequest{
236+
Id: it.id,
237+
})
238+
if err != nil {
239+
it.setError(err)
240+
return
241+
}
242+
243+
if len(resp.Data) == 0 {
244+
return
245+
}
246+
247+
for {
248+
select {
249+
case it.fetchedData <- resp.Data:
250+
case onUpdated := <-it.reqUpdateError:
251+
it.updateError()
252+
close(onUpdated)
253+
continue
254+
case <-it.onClose:
255+
return
256+
}
257+
break
258+
}
259+
}
194260
}
195261

196262
// Next attempts to move the iterator to the next element and returns if this
197263
// succeeded
198264
func (it *iterator) Next() bool {
199265
if it.db.closed.Get() {
200266
it.data = nil
201-
it.errs.Add(database.ErrClosed)
267+
it.setError(database.ErrClosed)
202268
return false
203269
}
204270
if len(it.data) > 1 {
@@ -207,32 +273,24 @@ func (it *iterator) Next() bool {
207273
return true
208274
}
209275

210-
resp, err := it.db.client.IteratorNext(context.Background(), &rpcdbpb.IteratorNextRequest{
211-
Id: it.id,
212-
})
213-
if err != nil {
214-
it.errs.Add(err)
215-
return false
216-
}
217-
it.data = resp.Data
276+
it.data = <-it.fetchedData
218277
return len(it.data) > 0
219278
}
220279

221280
// Error returns any that occurred while iterating
222281
func (it *iterator) Error() error {
223-
if it.errs.Errored() {
224-
return it.errs.Err
282+
if err := it.getError(); err != nil {
283+
return err
225284
}
226285

227-
resp, err := it.db.client.IteratorError(context.Background(), &rpcdbpb.IteratorErrorRequest{
228-
Id: it.id,
229-
})
230-
if err != nil {
231-
it.errs.Add(err)
232-
} else {
233-
it.errs.Add(errEnumToError[resp.Err])
286+
onUpdated := make(chan struct{})
287+
select {
288+
case it.reqUpdateError <- onUpdated:
289+
<-onUpdated
290+
case <-it.onClosed:
234291
}
235-
return it.errs.Err
292+
293+
return it.getError()
236294
}
237295

238296
// Key returns the key of the current element
@@ -253,12 +311,39 @@ func (it *iterator) Value() []byte {
253311

254312
// Release frees any resources held by the iterator
255313
func (it *iterator) Release() {
256-
resp, err := it.db.client.IteratorRelease(context.Background(), &rpcdbpb.IteratorReleaseRequest{
314+
it.once.Do(func() {
315+
close(it.onClose)
316+
<-it.onClosed
317+
})
318+
}
319+
320+
func (it *iterator) updateError() {
321+
resp, err := it.db.client.IteratorError(context.Background(), &rpcdbpb.IteratorErrorRequest{
257322
Id: it.id,
258323
})
259324
if err != nil {
260-
it.errs.Add(err)
325+
it.setError(err)
261326
} else {
262-
it.errs.Add(errEnumToError[resp.Err])
327+
it.setError(errEnumToError[resp.Err])
328+
}
329+
}
330+
331+
func (it *iterator) setError(err error) {
332+
if err == nil {
333+
return
334+
}
335+
336+
it.errLock.Lock()
337+
defer it.errLock.Unlock()
338+
339+
if it.err == nil {
340+
it.err = err
263341
}
264342
}
343+
344+
func (it *iterator) getError() error {
345+
it.errLock.RLock()
346+
defer it.errLock.RUnlock()
347+
348+
return it.err
349+
}

genesis/beacons.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func SampleBeacons(networkID uint32, count int) ([]string, []string) {
139139
sampledIDs := make([]string, 0, count)
140140

141141
s := sampler.NewUniform()
142-
_ = s.Initialize(uint64(len(ips)))
142+
s.Initialize(uint64(len(ips)))
143143
indices, _ := s.Sample(count)
144144
for _, index := range indices {
145145
sampledIPs = append(sampledIPs, ips[int(index)])

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ require (
4444
github.com/stretchr/testify v1.8.1
4545
github.com/supranational/blst v0.3.11-0.20220920110316-f72618070295
4646
github.com/syndtr/goleveldb v1.0.1-0.20220614013038-64ee5596c38a
47+
github.com/thepudds/fzgen v0.4.2
4748
go.opentelemetry.io/otel v1.11.0
4849
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.11.0
4950
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.11.0
@@ -108,6 +109,7 @@ require (
108109
github.com/prometheus/common v0.37.0 // indirect
109110
github.com/prometheus/procfs v0.8.0 // indirect
110111
github.com/rjeczalik/notify v0.9.3 // indirect
112+
github.com/sanity-io/litter v1.5.1 // indirect
111113
github.com/spf13/afero v1.8.2 // indirect
112114
github.com/spf13/cast v1.5.0 // indirect
113115
github.com/spf13/jwalterweatherman v1.1.0 // indirect

go.sum

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ github.com/cncf/xds/go v0.0.0-20210805033703-aa0b78936158/go.mod h1:eXthEFrGJvWH
114114
github.com/cncf/xds/go v0.0.0-20210922020428-25de7278fc84/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
115115
github.com/cncf/xds/go v0.0.0-20211011173535-cb28da3451f1/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
116116
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
117+
github.com/davecgh/go-spew v0.0.0-20161028175848-04cdfd42973b/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
117118
github.com/davecgh/go-spew v0.0.0-20171005155431-ecdeabc65495/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
118119
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
119120
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
@@ -375,6 +376,7 @@ github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
375376
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
376377
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
377378
github.com/pkg/sftp v1.13.1/go.mod h1:3HaPG6Dq1ILlpPZRO0HVMrsydcdLt6HRDccSgb87qRg=
379+
github.com/pmezard/go-difflib v0.0.0-20151028094244-d8ed2627bdf0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
378380
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
379381
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
380382
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
@@ -406,9 +408,11 @@ github.com/rjeczalik/notify v0.9.3 h1:6rJAzHTGKXGj76sbRgDiDcYj/HniypXmSJo1SWakZe
406408
github.com/rjeczalik/notify v0.9.3/go.mod h1:gF3zSOrafR9DQEWSE8TjfI9NkooDxbyT4UgRGKZA0lc=
407409
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
408410
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
409-
github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k=
411+
github.com/rogpeppe/go-internal v1.8.1 h1:geMPLpDpQOgVyCg5z5GoRwLHepNdb71NXb67XFkP+Eg=
410412
github.com/rs/cors v1.7.0 h1:+88SsELBHx5r+hZ8TCkggzSstaWNbDvThkVK8H6f9ik=
411413
github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU=
414+
github.com/sanity-io/litter v1.5.1 h1:dwnrSypP6q56o3lFxTU+t2fwQ9A+U5qrXVO4Qg9KwVU=
415+
github.com/sanity-io/litter v1.5.1/go.mod h1:5Z71SvaYy5kcGtyglXOC9rrUi3c1E8CamFWjQsazTh0=
412416
github.com/shirou/gopsutil v3.21.11+incompatible h1:+1+c1VGhc88SSonWP6foOcLhvnKlUeu/erjjvaPEYiI=
413417
github.com/shirou/gopsutil v3.21.11+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
414418
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
@@ -434,6 +438,7 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
434438
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
435439
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
436440
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
441+
github.com/stretchr/testify v0.0.0-20161117074351-18a02ba4a312/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
437442
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
438443
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
439444
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
@@ -451,6 +456,8 @@ github.com/supranational/blst v0.3.11-0.20220920110316-f72618070295/go.mod h1:jZ
451456
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7/go.mod h1:q4W45IWZaF22tdD+VEXcAWRA037jwmWEB5VWYORlTpc=
452457
github.com/syndtr/goleveldb v1.0.1-0.20220614013038-64ee5596c38a h1:1ur3QoCqvE5fl+nylMaIr9PVV1w343YRDtsy+Rwu7XI=
453458
github.com/syndtr/goleveldb v1.0.1-0.20220614013038-64ee5596c38a/go.mod h1:RRCYJbIwD5jmqPI9XoAFR0OcDxqUctll6zUj/+B4S48=
459+
github.com/thepudds/fzgen v0.4.2 h1:HlEHl5hk2/cqEomf2uK5SA/FeJc12s/vIHmOG+FbACw=
460+
github.com/thepudds/fzgen v0.4.2/go.mod h1:kHCWdsv5tdnt32NIHYDdgq083m6bMtaY0M+ipiO9xWE=
454461
github.com/tklauser/go-sysconf v0.3.5 h1:uu3Xl4nkLzQfXNsWn15rPc/HQCJKObbt1dKJeWp3vU4=
455462
github.com/tklauser/go-sysconf v0.3.5/go.mod h1:MkWzOF4RMCshBAMXuhXJs64Rte09mITnppBXY/rYEFI=
456463
github.com/tklauser/numcpus v0.2.2 h1:oyhllyrScuYI6g+h/zUvNXNp1wy7x8qQy3t/piefldA=

network/network.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -683,9 +683,7 @@ func (n *network) Peers(peerID ids.NodeID) ([]ips.ClaimedIPPort, error) {
683683
// We select a random sample of validators to gossip to avoid starving out a
684684
// validator from being gossiped for an extended period of time.
685685
s := sampler.NewUniform()
686-
if err := s.Initialize(uint64(len(unknownValidators))); err != nil {
687-
return nil, err
688-
}
686+
s.Initialize(uint64(len(unknownValidators)))
689687

690688
// Calculate the unknown information we need to send to this peer.
691689
validatorIPs := make([]ips.ClaimedIPPort, 0, int(n.config.PeerListNumValidatorIPs))

network/peer/set.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,7 @@ func (s *peerSet) Sample(n int, precondition func(Peer) bool) []Peer {
120120
}
121121

122122
sampler := sampler.NewUniform()
123-
// It is impossible for the sampler to report an error here. Since
124-
// [len(s.peersSlice)] <= MaxInt64.
125-
_ = sampler.Initialize(uint64(len(s.peersSlice)))
123+
sampler.Initialize(uint64(len(s.peersSlice)))
126124

127125
peers := make([]Peer, 0, n)
128126
for len(peers) < n {

0 commit comments

Comments
 (0)