Skip to content
Snippets Groups Projects
Commit 5b8f1c5c authored by capossele's avatar capossele
Browse files

:white_check_mark: adds dispatcher test

parent 3bc0b4c6
No related branches found
No related tags found
No related merge requests found
package drng
import (
"errors"
"time"
"github.com/iotaledger/goshimmer/packages/binary/drng/payload"
......@@ -12,14 +13,14 @@ import (
"github.com/iotaledger/goshimmer/packages/binary/signature/ed25119"
)
func (drng *Instance) Dispatch(issuer ed25119.PublicKey, timestamp time.Time, payload *payload.Payload) {
func (drng *Instance) Dispatch(issuer ed25119.PublicKey, timestamp time.Time, payload *payload.Payload) error {
switch payload.SubType() {
case header.CollectiveBeaconType():
// parse as CollectiveBeaconType
marshalUtil := marshalutil.New(payload.Bytes())
parsedPayload, err := cb.Parse(marshalUtil)
if err != nil {
return
return err
}
// trigger CollectiveBeaconEvent
cbEvent := &events.CollectiveBeaconEvent{
......@@ -35,12 +36,16 @@ func (drng *Instance) Dispatch(issuer ed25119.PublicKey, timestamp time.Time, pa
// process collectiveBeacon
if err := collectiveBeacon.ProcessBeacon(drng.State, cbEvent); err != nil {
return
return err
}
// trigger RandomnessEvent
drng.Events.Randomness.Trigger(drng.State.Randomness())
return nil
default:
//do other stuff
return errors.New("subtype not implemented")
}
}
package drng
import (
"encoding/hex"
"testing"
"time"
"github.com/iotaledger/goshimmer/packages/binary/drng/payload"
"github.com/iotaledger/goshimmer/packages/binary/drng/payload/header"
"github.com/iotaledger/goshimmer/packages/binary/drng/state"
"github.com/iotaledger/goshimmer/packages/binary/drng/subtypes/collectiveBeacon"
"github.com/iotaledger/goshimmer/packages/binary/drng/subtypes/collectiveBeacon/events"
cbPayload "github.com/iotaledger/goshimmer/packages/binary/drng/subtypes/collectiveBeacon/payload"
"github.com/iotaledger/goshimmer/packages/binary/marshalutil"
"github.com/iotaledger/goshimmer/packages/binary/signature/ed25119"
"github.com/stretchr/testify/require"
)
var (
eventTest *events.CollectiveBeaconEvent
prevSignatureTest []byte
signatureTest []byte
dpkTest []byte
issuerPK ed25119.PublicKey
committeeTest *state.Committee
timestampTest time.Time
randomnessTest *state.Randomness
)
func init() {
prevSignatureTest, _ = hex.DecodeString("ae9ba6d1445bffea8e66cb7d28fe5924e0a8d31b11b62a8710204e56e1ba84bc3694a3033e5793fcee6e75e956e5da3016cd0e22aa46fa419cd06343a7ff9d1e9c5c08f660f0bdec099e97ef99f470bb8c607ce9667a165e9caa474710f62ffd")
signatureTest, _ = hex.DecodeString("8dee56fae60dcad960f7176d0813d5415b930cf6e20c299ec2c2dfc5f2ad4903916fd462ba1abf5c32a5bfd94dcc8eba062d011a548d99df7fa1e3bbbc9a0455663d60f6ccc736c1d5b6de727dbe4427e21fb660925518be386265913f447c94")
dpkTest, _ = hex.DecodeString("a02fcd15edd52c8e134027491a43b597505b466d1679e88f70f927e57c45a93ae0765ff02fc2d015e3a02fd8748e2103")
timestampTest = time.Now()
rand, _ := collectiveBeacon.GetRandomness(signatureTest)
randomnessTest = &state.Randomness{
Round: 1,
Randomness: rand,
Timestamp: timestampTest,
}
kp := ed25119.GenerateKeyPair()
issuerPK = kp.PublicKey
committeeTest = &state.Committee{
InstanceID: 1,
Threshold: 3,
Identities: []ed25119.PublicKey{issuerPK},
DistributedPK: dpkTest,
}
}
func dummyPayload() *cbPayload.Payload {
header := header.New(header.CollectiveBeaconType(), 0)
return cbPayload.New(header.Instance(),
1,
prevSignatureTest,
signatureTest,
dpkTest)
}
func TestDispatcher(t *testing.T) {
marshalUtil := marshalutil.New(dummyPayload().Bytes())
parsedPayload, err := payload.Parse(marshalUtil)
require.NoError(t, err)
drng := New(state.SetCommittee(committeeTest))
err = drng.Dispatch(issuerPK, timestampTest, parsedPayload)
require.NoError(t, err)
require.Equal(t, *randomnessTest, drng.State.Randomness())
}
......@@ -81,6 +81,9 @@ func FromBytes(bytes []byte, optionalTargetObject ...*Payload) (result *Payload,
return
}
// return the number of bytes we processed
consumedBytes = marshalUtil.ReadOffset()
// store bytes, so we don't have to marshal manually
result.bytes = bytes[:consumedBytes]
......
......@@ -19,7 +19,6 @@ type Committee struct {
Identities []ed25119.PublicKey
DistributedPK []byte
}
type State struct {
randomness *Randomness
committe *Committee
......@@ -48,6 +47,9 @@ func (s *State) SetRandomness(r *Randomness) {
func (s *State) Randomness() Randomness {
s.mutex.RLock()
defer s.mutex.RUnlock()
if s.randomness == nil {
return Randomness{}
}
return *s.randomness
}
......@@ -60,5 +62,8 @@ func (s *State) SetCommittee(c *Committee) {
func (s *State) Committee() Committee {
s.mutex.RLock()
defer s.mutex.RUnlock()
if s.committe == nil {
return Committee{}
}
return *s.committe
}
......@@ -33,7 +33,9 @@ func ProcessBeacon(drng *state.State, cb *events.CollectiveBeaconEvent) error {
Randomness: randomness,
Timestamp: cb.Timestamp,
}
drng.SetRandomness(newRandomness)
return nil
}
......
......@@ -56,7 +56,7 @@ func TestGetRandomness(t *testing.T) {
require.NoError(t, err)
}
func TestProcessTransaction(t *testing.T) {
func TestProcessBeacon(t *testing.T) {
err := ProcessBeacon(stateTest, eventTest)
require.NoError(t, err)
}
......
......@@ -32,9 +32,12 @@ func configureEvents() {
marshalUtil := marshalutil.New(transaction.GetPayload().Bytes())
parsedPayload, err := payload.Parse(marshalUtil)
if err != nil {
//TODO: handle error
return
}
Instance.Dispatch(transaction.IssuerPublicKey(), transaction.IssuingTime(), parsedPayload)
if err := Instance.Dispatch(transaction.IssuerPublicKey(), transaction.IssuingTime(), parsedPayload); err != nil {
//TODO: handle error
}
})
}))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment