-
Notifications
You must be signed in to change notification settings - Fork 1
/
send.go
executable file
·51 lines (44 loc) · 1.55 KB
/
send.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package main
import (
"github.com/shatll-s/nexelliad/app/appmessage"
"github.com/shatll-s/nexelliad/app/protocol/common"
"github.com/shatll-s/nexelliad/domain/consensus/model/externalapi"
"github.com/shatll-s/nexelliad/domain/consensus/utils/consensushashing"
"github.com/shatll-s/nexelliad/infrastructure/network/netadapter/standalone"
"github.com/pkg/errors"
)
func sendBlocks(address string, minimalNetAdapter *standalone.MinimalNetAdapter, blocksChan <-chan *externalapi.DomainBlock) error {
for block := range blocksChan {
routes, err := minimalNetAdapter.Connect(address)
if err != nil {
return err
}
blockHash := consensushashing.BlockHash(block)
log.Infof("Sending block %s", blockHash)
err = routes.OutgoingRoute.Enqueue(&appmessage.MsgInvRelayBlock{
Hash: blockHash,
})
if err != nil {
return err
}
message, err := routes.WaitForMessageOfType(appmessage.CmdRequestRelayBlocks, common.DefaultTimeout)
if err != nil {
return err
}
requestRelayBlockMessage := message.(*appmessage.MsgRequestRelayBlocks)
if len(requestRelayBlockMessage.Hashes) != 1 || *requestRelayBlockMessage.Hashes[0] != *blockHash {
return errors.Errorf("Expecting requested hashes to be [%s], but got %v",
blockHash, requestRelayBlockMessage.Hashes)
}
err = routes.OutgoingRoute.Enqueue(appmessage.DomainBlockToMsgBlock(block))
if err != nil {
return err
}
// TODO(libp2p): Wait for reject message once it has been implemented
err = routes.WaitForDisconnect(common.DefaultTimeout)
if err != nil {
return err
}
}
return nil
}