Skip to content
Snippets Groups Projects
Unverified Commit 19cb84a8 authored by Sydney Anne Erickson's avatar Sydney Anne Erickson :chipmunk:
Browse files

Update comments for ERS and move a test

parent 28415e35
No related branches found
No related tags found
1 merge request!58Revert "Modify waiting lock"
...@@ -5,6 +5,9 @@ import ( ...@@ -5,6 +5,9 @@ import (
"gitlab.com/elixxir/primitives/id" "gitlab.com/elixxir/primitives/id"
) )
// The ExtendedRoundStorage (ERS) interface allows storing rounds inside of an external database for clients to pull
// from, because the ring buffer only contains a limited number of them while clients might need to go further back
// into history.
type ExternalRoundStorage interface { type ExternalRoundStorage interface {
// Store: stores the round info inside the underlying storage medium, which generally is a database. Store will // Store: stores the round info inside the underlying storage medium, which generally is a database. Store will
// add the round info to the database if it doesn't exist and will only overwrite the data if it does exist in the // add the round info to the database if it doesn't exist and will only overwrite the data if it does exist in the
......
...@@ -6,6 +6,7 @@ import ( ...@@ -6,6 +6,7 @@ import (
"gitlab.com/elixxir/primitives/id" "gitlab.com/elixxir/primitives/id"
) )
// Calls the underlying interface's function to get a specific round from history
func (i *Instance) GetHistoricalRound(id id.Round) (*pb.RoundInfo, error) { func (i *Instance) GetHistoricalRound(id id.Round) (*pb.RoundInfo, error) {
if i.ers != nil { if i.ers != nil {
return i.ers.Retrieve(id) return i.ers.Retrieve(id)
...@@ -13,6 +14,7 @@ func (i *Instance) GetHistoricalRound(id id.Round) (*pb.RoundInfo, error) { ...@@ -13,6 +14,7 @@ func (i *Instance) GetHistoricalRound(id id.Round) (*pb.RoundInfo, error) {
return nil, errors.New("no ExternalRoundStorage object was defined on instance creation") return nil, errors.New("no ExternalRoundStorage object was defined on instance creation")
} }
// Calls the underlying interface's function to get specific rounds from history
func (i *Instance) GetHistoricalRounds(rounds []id.Round) ([]*pb.RoundInfo, error) { func (i *Instance) GetHistoricalRounds(rounds []id.Round) ([]*pb.RoundInfo, error) {
if i.ers != nil { if i.ers != nil {
return i.ers.RetrieveMany(rounds) return i.ers.RetrieveMany(rounds)
...@@ -20,6 +22,7 @@ func (i *Instance) GetHistoricalRounds(rounds []id.Round) ([]*pb.RoundInfo, erro ...@@ -20,6 +22,7 @@ func (i *Instance) GetHistoricalRounds(rounds []id.Round) ([]*pb.RoundInfo, erro
return nil, errors.New("no ExternalRoundStorage object was defined on instance creation") return nil, errors.New("no ExternalRoundStorage object was defined on instance creation")
} }
// Calls the underlying interface's function to get a range of rounds from history
func (i *Instance) GetHistoricalRoundRange(first, last id.Round) ([]*pb.RoundInfo, error) { func (i *Instance) GetHistoricalRoundRange(first, last id.Round) ([]*pb.RoundInfo, error) {
if i.ers != nil { if i.ers != nil {
return i.ers.RetrieveRange(first, last) return i.ers.RetrieveRange(first, last)
......
...@@ -4,12 +4,7 @@ import ( ...@@ -4,12 +4,7 @@ import (
jww "github.com/spf13/jwalterweatherman" jww "github.com/spf13/jwalterweatherman"
pb "gitlab.com/elixxir/comms/mixmessages" pb "gitlab.com/elixxir/comms/mixmessages"
ds "gitlab.com/elixxir/comms/network/dataStructures" ds "gitlab.com/elixxir/comms/network/dataStructures"
"gitlab.com/elixxir/comms/testkeys"
"gitlab.com/elixxir/comms/testutils"
"gitlab.com/elixxir/crypto/signature"
"gitlab.com/elixxir/crypto/signature/rsa"
"gitlab.com/elixxir/primitives/id" "gitlab.com/elixxir/primitives/id"
"gitlab.com/xx_network/comms/connect"
"testing" "testing"
) )
...@@ -322,68 +317,3 @@ func TestInstance_GetHistoricalRounds(t *testing.T) { ...@@ -322,68 +317,3 @@ func TestInstance_GetHistoricalRounds(t *testing.T) {
} }
} }
} }
// Test that a new round update is inputted into the ERS map
func TestInstance_RoundUpdateAddsToERS(t *testing.T) {
// Get signing certificates
priv := testkeys.LoadFromPath(testkeys.GetNodeKeyPath())
privKey, err := rsa.LoadPrivateKeyFromPem(priv)
pub := testkeys.LoadFromPath(testkeys.GetNodeCertPath())
if err != nil {
t.Errorf("Could not generate rsa key: %s", err)
}
// Create a basic testing NDF and sign it
f := &pb.NDF{}
f.Ndf = []byte(testutils.ExampleJSON)
baseNDF := testutils.NDF
if err != nil {
t.Errorf("Could not generate serialized ndf: %s", err)
}
err = signature.Sign(f, privKey)
if err != nil {
t.Errorf("Could not generate serialized ndf: %s", err)
}
// Build the Instance object with an ERS memory map
pc := &connect.ProtoComms{}
var ers ds.ExternalRoundStorage = ersMemMap{rounds: make(map[id.Round]*pb.RoundInfo)}
i, err := NewInstance(pc, baseNDF, baseNDF, ers)
if err != nil {
t.Error(nil)
}
// Add a permissioning host
_, err = i.comm.AddHost(&id.Permissioning, "0.0.0.0:4200", pub, false, true)
if err != nil {
t.Errorf("Failed to add permissioning host: %+v", err)
}
// Build a basic RoundInfo object and sign it
r := &pb.RoundInfo{
ID: 2,
UpdateID: 4,
}
err = signature.Sign(r, privKey)
if err != nil {
t.Errorf(err.Error())
}
// Cause a RoundUpdate
err = i.RoundUpdate(r)
if err != nil {
t.Errorf(err.Error())
}
// Check that the round info was stored correctly
rr, err := ers.Retrieve(id.Round(r.ID))
if err != nil {
t.Errorf(err.Error())
}
if rr == nil {
t.Fatalf("returned round info was nil")
}
if rr.ID != r.ID || rr.UpdateID != r.UpdateID {
t.Errorf("Second returned round and original mismatched IDs")
}
}
...@@ -37,7 +37,8 @@ type Instance struct { ...@@ -37,7 +37,8 @@ type Instance struct {
ipOverride *ds.IpOverrideList ipOverride *ds.IpOverrideList
} }
// Initializer for instance structs from base comms and NDF // Initializer for instance structs from base comms and NDF, you can put in nil for
// ERS if you don't want to use it
func NewInstance(c *connect.ProtoComms, partial, full *ndf.NetworkDefinition, func NewInstance(c *connect.ProtoComms, partial, full *ndf.NetworkDefinition,
ers ds.ExternalRoundStorage) (*Instance, error) { ers ds.ExternalRoundStorage) (*Instance, error) {
var partialNdf *SecuredNdf var partialNdf *SecuredNdf
...@@ -102,9 +103,8 @@ func NewInstance(c *connect.ProtoComms, partial, full *ndf.NetworkDefinition, ...@@ -102,9 +103,8 @@ func NewInstance(c *connect.ProtoComms, partial, full *ndf.NetworkDefinition,
} }
} }
if ers != nil { // Set our ERS to the passed in ERS object (or nil)
i.ers = ers i.ers = ers
}
return i, nil return i, nil
} }
......
...@@ -666,3 +666,68 @@ func createBadNdf(t *testing.T) *mixmessages.NDF { ...@@ -666,3 +666,68 @@ func createBadNdf(t *testing.T) *mixmessages.NDF {
return f return f
} }
// Test that a new round update is inputted into the ERS map
func TestInstance_RoundUpdateAddsToERS(t *testing.T) {
// Get signing certificates
priv := testkeys.LoadFromPath(testkeys.GetNodeKeyPath())
privKey, err := rsa.LoadPrivateKeyFromPem(priv)
pub := testkeys.LoadFromPath(testkeys.GetNodeCertPath())
if err != nil {
t.Errorf("Could not generate rsa key: %s", err)
}
// Create a basic testing NDF and sign it
f := &pb.NDF{}
f.Ndf = []byte(testutils.ExampleJSON)
baseNDF := testutils.NDF
if err != nil {
t.Errorf("Could not generate serialized ndf: %s", err)
}
err = signature.Sign(f, privKey)
if err != nil {
t.Errorf("Could not generate serialized ndf: %s", err)
}
// Build the Instance object with an ERS memory map
pc := &connect.ProtoComms{}
var ers ds.ExternalRoundStorage = ersMemMap{rounds: make(map[id.Round]*pb.RoundInfo)}
i, err := NewInstance(pc, baseNDF, baseNDF, ers)
if err != nil {
t.Error(nil)
}
// Add a permissioning host
_, err = i.comm.AddHost(&id.Permissioning, "0.0.0.0:4200", pub, false, true)
if err != nil {
t.Errorf("Failed to add permissioning host: %+v", err)
}
// Build a basic RoundInfo object and sign it
r := &pb.RoundInfo{
ID: 2,
UpdateID: 4,
}
err = signature.Sign(r, privKey)
if err != nil {
t.Errorf(err.Error())
}
// Cause a RoundUpdate
err = i.RoundUpdate(r)
if err != nil {
t.Errorf(err.Error())
}
// Check that the round info was stored correctly
rr, err := ers.Retrieve(id.Round(r.ID))
if err != nil {
t.Errorf(err.Error())
}
if rr == nil {
t.Fatalf("returned round info was nil")
}
if rr.ID != r.ID || rr.UpdateID != r.UpdateID {
t.Errorf("Second returned round and original mismatched IDs")
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment