-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
*: Collect height and state data from chain
Closes #95. Signed-off-by: Evgenii Baidakov <[email protected]>
- Loading branch information
Showing
4 changed files
with
207 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package multinodepool | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"sync" | ||
"time" | ||
|
||
"github.com/nspcc-dev/neo-go/pkg/rpcclient" | ||
"github.com/nspcc-dev/neofs-net-monitor/pkg/monitor" | ||
) | ||
|
||
// Pool collects data from each node. | ||
type Pool struct { | ||
endpoints []string | ||
dialTimeout time.Duration | ||
clients []*rpcclient.Client | ||
} | ||
|
||
func NewPool(endpoints []string, dialTimeout time.Duration) *Pool { | ||
return &Pool{ | ||
endpoints: endpoints, | ||
dialTimeout: dialTimeout, | ||
clients: make([]*rpcclient.Client, len(endpoints)), | ||
} | ||
} | ||
|
||
func (c *Pool) Dial(ctx context.Context) error { | ||
opts := rpcclient.Options{DialTimeout: c.dialTimeout} | ||
|
||
for i, ep := range c.endpoints { | ||
neoClient, err := neoGoClient(ctx, ep, opts) | ||
if err != nil { | ||
return fmt.Errorf("neoGoClient: %w", err) | ||
} | ||
|
||
c.clients[i] = neoClient | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c *Pool) FetchHeight() []monitor.HeightData { | ||
wg := sync.WaitGroup{} | ||
wg.Add(len(c.clients)) | ||
|
||
mu := sync.Mutex{} | ||
var heights []monitor.HeightData | ||
|
||
for _, cl := range c.clients { | ||
go func(cl *rpcclient.Client) { | ||
defer wg.Done() | ||
|
||
stHeight, err := cl.GetStateHeight() | ||
if err != nil { | ||
log.Printf("GetStateHeight for %s: %v", cl.Endpoint(), err) | ||
return | ||
} | ||
|
||
mu.Lock() | ||
heights = append(heights, monitor.HeightData{ | ||
Host: cl.Endpoint(), | ||
Value: stHeight.Local, | ||
}) | ||
mu.Unlock() | ||
}(cl) | ||
} | ||
|
||
wg.Wait() | ||
|
||
return heights | ||
} | ||
|
||
func (c *Pool) FetchState(height uint32) []monitor.StateData { | ||
wg := sync.WaitGroup{} | ||
wg.Add(len(c.clients)) | ||
|
||
mu := sync.Mutex{} | ||
var states []monitor.StateData | ||
|
||
for _, cl := range c.clients { | ||
go func(cl *rpcclient.Client) { | ||
defer wg.Done() | ||
|
||
stHeight, err := cl.GetStateRootByHeight(height) | ||
if err != nil { | ||
log.Printf("GetStateRootByHeight for %s: %v", cl.Endpoint(), err) | ||
return | ||
} | ||
|
||
mu.Lock() | ||
states = append(states, monitor.StateData{ | ||
Host: cl.Endpoint(), | ||
Value: stHeight.Hash().String(), | ||
}) | ||
mu.Unlock() | ||
}(cl) | ||
} | ||
|
||
wg.Wait() | ||
|
||
return states | ||
} | ||
|
||
func neoGoClient(ctx context.Context, endpoint string, opts rpcclient.Options) (*rpcclient.Client, error) { | ||
cli, err := rpcclient.New(ctx, endpoint, opts) | ||
if err != nil { | ||
return nil, fmt.Errorf("can't create neo-go client: %w", err) | ||
} | ||
|
||
err = cli.Init() | ||
if err != nil { | ||
return nil, fmt.Errorf("can't init neo-go client: %w", err) | ||
} | ||
|
||
return cli, nil | ||
} |