Skip to content
Snippets Groups Projects
Select Git revision
  • f4e22b7d1fed61b0bbfd20fa7c0de5763107c934
  • release default protected
  • master protected
  • NationalTreasure/NotificationUpgrade
  • XX-4441
  • xx-4417/gw-poll-earliest-client-round
  • tls-websockets
  • hotfix/drain
  • hotfix/matcher
  • projects/crust_RELEASE
  • XX-4055/ChannelIdentityTracking
  • XX-4066/CrustUpgrade_MASTER
  • Ace/Huawei
  • hotfix/accumulate-notifs
  • XX-3564/TlsCipherSuite
  • hotfix/groupNotification
  • Anne/License-Update
  • hotfix/trustoldgatewaysonly
  • hotfix/notifications
  • notls
  • url-repo-rename
  • v0.0.4
  • v0.0.3
  • v0.0.2
  • v0.0.1
25 results

roundData.go

  • roundData.go 2.20 KiB
    ///////////////////////////////////////////////////////////////////////////////
    // Copyright © 2020 xx network SEZC                                          //
    //                                                                           //
    // Use of this source code is governed by a license that can be found in the //
    // LICENSE file                                                              //
    ///////////////////////////////////////////////////////////////////////////////
    
    // Store a list of the most recent rounds, holding the most recent update for each
    
    package dataStructures
    
    import (
    	"github.com/pkg/errors"
    	"gitlab.com/elixxir/comms/mixmessages"
    	"gitlab.com/xx_network/primitives/id"
    	"gitlab.com/xx_network/ring"
    )
    
    const RoundInfoBufLen = 1500
    
    // ID numbers can overwrite
    type Data struct {
    	rounds *ring.Buff
    }
    
    // Initialize a new Data object
    func NewData() *Data {
    	// We want data using the round ID as its primary
    
    	return &Data{
    		rounds: ring.NewBuff(RoundInfoBufLen),
    	}
    }
    
    // Upsert a round into the ring bugger
    func (d *Data) UpsertRound(r *Round) error {
    	//find the round location
    	//check the new state is newer then the current
    	//replace the round info object
    	return d.rounds.UpsertById(int(r.info.ID), r)
    }
    
    // Get a given round id from the ring buffer as a roundInfo
    func (d *Data) GetRound(id int) (*mixmessages.RoundInfo, error) {
    	val, err := d.rounds.GetById(id)
    	if err != nil {
    		return nil, errors.Wrapf(err, "Failed to get round by id with "+
    			"%d", id)
    	}
    
    	if val == nil {
    		return nil, errors.Errorf("Failed to get round by id with %d, "+
    			"got nil round", id)
    	}
    
    	return val.(*Round).Get(), nil
    }
    
    // Get a given round id from the ring buffer as a round object
    func (d *Data) GetWrappedRound(id int) (*Round, error) {
    	val, err := d.rounds.GetById(id)
    	if err != nil {
    		return nil, errors.Wrapf(err, "Failed to get update with id %d", id)
    	}
    	var rtn *Round
    	if val != nil {
    		rtn = val.(*Round)
    	}
    	return rtn, nil
    }
    
    // Get the ID of the newest round in the buffer
    func (d *Data) GetLastRoundID() id.Round {
    	return id.Round(d.rounds.GetNewestId())
    }
    
    // Gets the ID of the oldest roundd in the buffer
    func (d *Data) GetOldestRoundID() id.Round {
    	return id.Round(d.rounds.GetOldestId())
    }