Skip to content

Commit

Permalink
fix relayer remote consumers list update
Browse files Browse the repository at this point in the history
  • Loading branch information
vanessaviolet committed Feb 7, 2024
1 parent 398bf4e commit cfb777d
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 13 deletions.
2 changes: 1 addition & 1 deletion kernel/hack.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ package kernel
const (
mainnetConsensusOperationElectionForkAt = uint64(1701388800000000000)
mainnetConsensusNodeRemovalTimeForkAt = uint64(1706400000000000000)
mainnetMintDayGapSkipForkBatch = uint64(1800)
mainnetMintDayGapSkipForkBatch = uint64(1810)
mainnetNodeRemovalHackSnapshotHash = "b5a9ab66e3b5d24328f8f87bc38e90f0c426dc38413200bb8ecf7f5b8607a5f9"
)
4 changes: 2 additions & 2 deletions kernel/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,12 +414,12 @@ func (node *Node) BuildAuthenticationMessage(relayerId crypto.Hash) []byte {
return data
}

func (node *Node) AuthenticateAs(recipientId crypto.Hash, msg []byte) (*network.AuthToken, error) {
func (node *Node) AuthenticateAs(recipientId crypto.Hash, msg []byte, timeoutSec int64) (*network.AuthToken, error) {
if len(msg) != 137 {
return nil, fmt.Errorf("peer authentication message malformatted %d", len(msg))
}
ts := binary.BigEndian.Uint64(msg[:8])
if math.Abs(float64(clock.Now().Unix())-float64(ts)) > 5 {
if timeoutSec > 0 && math.Abs(float64(clock.Now().Unix())-float64(ts)) > float64(timeoutSec) {
return nil, fmt.Errorf("peer authentication message timeout %d %d", ts, clock.Now().Unix())
}

Expand Down
7 changes: 4 additions & 3 deletions network/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ type AuthToken struct {
type SyncHandle interface {
GetCacheStore() *ristretto.Cache
BuildAuthenticationMessage(relayerId crypto.Hash) []byte
AuthenticateAs(recipientId crypto.Hash, msg []byte) (*AuthToken, error)
AuthenticateAs(recipientId crypto.Hash, msg []byte, timeoutSec int64) (*AuthToken, error)
BuildGraph() []*SyncPoint
UpdateSyncPoint(peerId crypto.Hash, points []*SyncPoint)
ReadAllNodesWithoutState() []crypto.Hash
Expand Down Expand Up @@ -410,7 +410,7 @@ func (me *Peer) loopHandlePeerMessage(peerId crypto.Hash, receive chan *PeerMess
}

func (me *Peer) relayOrHandlePeerMessage(relayerId crypto.Hash, msg *PeerMessage) error {
logger.Printf("me.relayOrHandlePeerMessage(%s, %s) => %s %v", me.Address, me.IdForNetwork, relayerId, msg)
logger.Printf("me.relayOrHandlePeerMessage(%s, %s) => %s %v", me.Address, me.IdForNetwork, relayerId, msg.Data)
if len(msg.Data) < 64 {
return nil
}
Expand Down Expand Up @@ -443,6 +443,7 @@ func (me *Peer) relayOrHandlePeerMessage(relayerId crypto.Hash, msg *PeerMessage
if !success {
return fmt.Errorf("peer send high timeout")
}
return nil
}
peer = me.remoteRelayers.Get(to)
if peer == nil || peer.IdForNetwork == relayerId {
Expand Down Expand Up @@ -470,7 +471,7 @@ func (me *Peer) updateRemoteRelayerConsumers(relayerId crypto.Hash, data []byte)
for c := len(data) / pl; c > 0; c-- {
var id crypto.Hash
copy(id[:], data[:32])
token, err := me.handle.AuthenticateAs(relayerId, data[32:pl])
token, err := me.handle.AuthenticateAs(relayerId, data[32:pl], 0)
if err != nil {
return nil
}
Expand Down
5 changes: 3 additions & 2 deletions network/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func (me *Peer) ListenConsumers() error {

for !me.closing {
me.gossipRound.Clear()
neighbors := me.consumers.Slice()
neighbors := me.Neighbors()
for i := range neighbors {
j := int(time.Now().UnixNano() % int64(i+1))
neighbors[i], neighbors[j] = neighbors[j], neighbors[i]
Expand All @@ -204,6 +204,7 @@ func (me *Peer) ListenConsumers() error {
me.gossipRound.Put(p.IdForNetwork, p)
}

neighbors = me.Neighbors()
msg := me.buildConsumersMessage()
for _, p := range neighbors {
if !p.isRemoteRelayer {
Expand Down Expand Up @@ -366,7 +367,7 @@ func (me *Peer) authenticateNeighbor(client Client) (*Peer, error) {
}
me.receivedMetric.handle(PeerMessageTypeAuthentication)

token, err := me.handle.AuthenticateAs(me.IdForNetwork, msg.Data)
token, err := me.handle.AuthenticateAs(me.IdForNetwork, msg.Data, 5)
if err != nil {
auth <- err
return
Expand Down
21 changes: 16 additions & 5 deletions rpc/consensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ var (
)

func TestConsensus(t *testing.T) {
testConsensus(t, false)
testConsensus(t, true)
testConsensus(t, false)
}

func testConsensus(t *testing.T, withRelayers bool) {
Expand Down Expand Up @@ -796,15 +796,18 @@ func setupTestNet(root string, withRelayers bool) ([]common.Address, []common.Ad
id := s.Hash().ForNetwork(gns.NetworkId())
peers[i] = fmt.Sprintf("%[email protected]:170%02d", id.String(), i+1)
}
peersList := `"` + strings.Join(peers[:len(peers)/3], `","`) + `"`
peersListHead := `"` + strings.Join(peers[:len(peers)/3], `","`) + `"`
peersListTail := `"` + strings.Join(peers[len(peers)/2:], `","`) + `"`

if withRelayers {
peers := make([]string, len(relayers))
for i, s := range relayers {
id := s.Hash().ForNetwork(gns.NetworkId())
peers[i] = fmt.Sprintf("%[email protected]:160%02d", id.String(), i+1)
}
peersList = `"` + strings.Join(peers[:len(peers)/3], `","`) + `"`
peersListHead = `"` + strings.Join(peers[:len(peers)/3], `","`) + `"`
peersListTail = `"` + strings.Join(peers[len(peers)/2:], `","`) + `"`
peersList := `"` + strings.Join(peers[:len(peers)/3], `","`) + `"`
for i, a := range relayers {
dir := fmt.Sprintf("%s/mixin-160%02d", root, i+1)
err := os.MkdirAll(dir, 0755)
Expand Down Expand Up @@ -836,7 +839,15 @@ func setupTestNet(root string, withRelayers bool) ([]common.Address, []common.Ad
panic(err)
}

isRelayer := !withRelayers && strings.Contains(peersList, fmt.Sprintf("170%02d", i+1))
peersList := peersListHead
if i > len(signers)/2 {
peersList = peersListTail
}
p2p := fmt.Sprintf("170%02d", i+1)
isRelayer := !withRelayers && (strings.Contains(peersListHead, p2p) || strings.Contains(peersListTail, p2p))
if isRelayer {
peersList = peersListHead
}
configData := []byte(fmt.Sprintf(configDataTmpl, a.PrivateSpendKey.String(), isRelayer, peersList))
err = os.WriteFile(dir+"/config.toml", configData, 0644)
if err != nil {
Expand All @@ -847,7 +858,7 @@ func setupTestNet(root string, withRelayers bool) ([]common.Address, []common.Ad
panic(err)
}
}
return signers, payees, genesisData, peersList
return signers, payees, genesisData, peersListHead
}

func testSignTransaction(node string, account common.Address, rawStr string) (*common.SignedTransaction, error) {
Expand Down

0 comments on commit cfb777d

Please sign in to comment.