2020-08-14 03:55:01 -07:00
|
|
|
package airgapped
|
|
|
|
|
|
|
|
import (
|
2020-08-14 07:38:58 -07:00
|
|
|
"bytes"
|
2020-08-14 03:55:01 -07:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2020-08-20 08:08:11 -07:00
|
|
|
client "github.com/depools/dc4bc/client/types"
|
2020-08-14 03:55:01 -07:00
|
|
|
"github.com/depools/dc4bc/fsm/state_machines/dkg_proposal_fsm"
|
|
|
|
"github.com/depools/dc4bc/fsm/types/requests"
|
|
|
|
"github.com/depools/dc4bc/fsm/types/responses"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2020-08-14 07:38:58 -07:00
|
|
|
const (
|
|
|
|
DKGIdentifier = "dkg_identifier"
|
2020-08-17 03:22:46 -07:00
|
|
|
testDB = "test_level_db"
|
2020-08-14 07:38:58 -07:00
|
|
|
)
|
|
|
|
|
2020-08-14 03:55:01 -07:00
|
|
|
type Node struct {
|
|
|
|
ParticipantID int
|
|
|
|
Participant string
|
|
|
|
Machine *AirgappedMachine
|
|
|
|
commits []requests.DKGProposalCommitConfirmationRequest
|
|
|
|
deals []requests.DKGProposalDealConfirmationRequest
|
|
|
|
responses []requests.DKGProposalResponseConfirmationRequest
|
2020-08-14 07:38:58 -07:00
|
|
|
masterKeys []requests.DKGProposalMasterKeyConfirmationRequest
|
2020-08-14 03:55:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Node) storeOperation(t *testing.T, o client.Operation) {
|
|
|
|
switch o.Event {
|
|
|
|
case dkg_proposal_fsm.EventDKGCommitConfirmationReceived:
|
|
|
|
var req requests.DKGProposalCommitConfirmationRequest
|
|
|
|
if err := json.Unmarshal(o.Result, &req); err != nil {
|
2020-08-14 04:16:18 -07:00
|
|
|
t.Fatalf("failed to unmarshal fsm req: %v", err)
|
2020-08-14 03:55:01 -07:00
|
|
|
}
|
|
|
|
n.commits = append(n.commits, req)
|
|
|
|
case dkg_proposal_fsm.EventDKGDealConfirmationReceived:
|
|
|
|
var req requests.DKGProposalDealConfirmationRequest
|
|
|
|
if err := json.Unmarshal(o.Result, &req); err != nil {
|
2020-08-14 04:16:18 -07:00
|
|
|
t.Fatalf("failed to unmarshal fsm req: %v", err)
|
2020-08-14 03:55:01 -07:00
|
|
|
}
|
|
|
|
n.deals = append(n.deals, req)
|
|
|
|
case dkg_proposal_fsm.EventDKGResponseConfirmationReceived:
|
|
|
|
var req requests.DKGProposalResponseConfirmationRequest
|
|
|
|
if err := json.Unmarshal(o.Result, &req); err != nil {
|
2020-08-14 04:16:18 -07:00
|
|
|
t.Fatalf("failed to unmarshal fsm req: %v", err)
|
2020-08-14 03:55:01 -07:00
|
|
|
}
|
|
|
|
n.responses = append(n.responses, req)
|
|
|
|
case dkg_proposal_fsm.EventDKGMasterKeyConfirmationReceived:
|
|
|
|
var req requests.DKGProposalMasterKeyConfirmationRequest
|
|
|
|
if err := json.Unmarshal(o.Result, &req); err != nil {
|
2020-08-14 04:16:18 -07:00
|
|
|
t.Fatalf("failed to unmarshal fsm req: %v", err)
|
2020-08-14 03:55:01 -07:00
|
|
|
}
|
2020-08-14 07:38:58 -07:00
|
|
|
n.masterKeys = append(n.masterKeys, req)
|
2020-08-14 03:55:01 -07:00
|
|
|
default:
|
2020-08-14 04:16:18 -07:00
|
|
|
t.Fatalf("invalid event: %s", o.Event)
|
2020-08-14 03:55:01 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Transport struct {
|
|
|
|
nodes []*Node
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tr *Transport) BroadcastOperation(t *testing.T, operation client.Operation) {
|
|
|
|
for _, node := range tr.nodes {
|
|
|
|
if operation.To == "" || operation.To == node.Participant {
|
|
|
|
node.storeOperation(t, operation)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func createOperation(t *testing.T, opType string, to string, req interface{}) client.Operation {
|
|
|
|
reqBz, err := json.Marshal(req)
|
|
|
|
if err != nil {
|
2020-08-14 04:16:18 -07:00
|
|
|
t.Fatalf("failed to marshal request: %v", err)
|
2020-08-14 03:55:01 -07:00
|
|
|
}
|
|
|
|
op := client.Operation{
|
|
|
|
ID: uuid.New().String(),
|
|
|
|
Type: client.OperationType(opType),
|
|
|
|
Payload: reqBz,
|
|
|
|
Result: nil,
|
|
|
|
CreatedAt: time.Now(),
|
2020-08-14 07:38:58 -07:00
|
|
|
DKGIdentifier: DKGIdentifier,
|
2020-08-14 03:55:01 -07:00
|
|
|
To: to,
|
|
|
|
}
|
|
|
|
return op
|
|
|
|
}
|
|
|
|
|
2020-08-14 07:38:58 -07:00
|
|
|
func TestAirgappedAllSteps(t *testing.T) {
|
|
|
|
nodesCount := 13
|
2020-08-14 03:55:01 -07:00
|
|
|
participants := make([]string, nodesCount)
|
|
|
|
for i := 0; i < nodesCount; i++ {
|
|
|
|
participants[i] = fmt.Sprintf("Participant#%d", i)
|
|
|
|
}
|
|
|
|
|
|
|
|
tr := &Transport{}
|
|
|
|
for i := 0; i < nodesCount; i++ {
|
2020-08-17 03:22:46 -07:00
|
|
|
am, err := NewAirgappedMachine(fmt.Sprintf(testDB+"%d", i))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to create airgapped machine: %v", err)
|
|
|
|
}
|
2020-08-14 03:55:01 -07:00
|
|
|
node := Node{
|
|
|
|
ParticipantID: i,
|
|
|
|
Participant: participants[i],
|
|
|
|
Machine: am,
|
|
|
|
}
|
|
|
|
tr.nodes = append(tr.nodes, &node)
|
|
|
|
}
|
|
|
|
|
2020-08-19 06:47:37 -07:00
|
|
|
// Remove this block later, after client testing
|
|
|
|
//var initReq responses.SignatureProposalParticipantInvitationsResponse
|
|
|
|
//for _, n := range tr.nodes {
|
|
|
|
// entry := &responses.SignatureProposalParticipantInvitationEntry{
|
|
|
|
// ParticipantId: n.ParticipantID,
|
|
|
|
// Title: n.Participant,
|
|
|
|
// }
|
|
|
|
// initReq = append(initReq, entry)
|
|
|
|
//}
|
|
|
|
//op := createOperation(t, string(signature_proposal_fsm.StateAwaitParticipantsConfirmations), "", initReq)
|
|
|
|
//runStep(tr, func(n *Node, wg *sync.WaitGroup) {
|
|
|
|
// defer wg.Done()
|
|
|
|
//
|
|
|
|
// _, err := n.Machine.HandleOperation(op)
|
|
|
|
// if err != nil {
|
|
|
|
// t.Fatalf("%s: failed to handle operation %s: %v", n.Participant, op.Type, err)
|
|
|
|
// }
|
|
|
|
//})
|
2020-08-14 03:55:01 -07:00
|
|
|
|
|
|
|
// get commits
|
2020-08-21 09:59:47 -07:00
|
|
|
var getCommitsRequest responses.DKGProposalPubKeysParticipantResponse
|
2020-08-14 03:55:01 -07:00
|
|
|
for _, n := range tr.nodes {
|
|
|
|
pubKey, err := n.Machine.pubKey.MarshalBinary()
|
|
|
|
if err != nil {
|
2020-08-14 04:16:18 -07:00
|
|
|
t.Fatalf("%s: failed to marshal pubkey: %v", n.Participant, err)
|
2020-08-14 03:55:01 -07:00
|
|
|
}
|
2020-08-21 09:59:47 -07:00
|
|
|
entry := &responses.DKGProposalPubKeysParticipantEntry{
|
2020-08-14 03:55:01 -07:00
|
|
|
ParticipantId: n.ParticipantID,
|
2020-08-20 08:08:11 -07:00
|
|
|
Addr: n.Participant,
|
2020-08-14 03:55:01 -07:00
|
|
|
DkgPubKey: pubKey,
|
|
|
|
}
|
|
|
|
getCommitsRequest = append(getCommitsRequest, entry)
|
|
|
|
}
|
2020-08-19 06:47:37 -07:00
|
|
|
op := createOperation(t, string(dkg_proposal_fsm.StateDkgCommitsAwaitConfirmations), "", getCommitsRequest)
|
2020-08-14 03:55:01 -07:00
|
|
|
runStep(tr, func(n *Node, wg *sync.WaitGroup) {
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
operations, err := n.Machine.HandleOperation(op)
|
|
|
|
if err != nil {
|
2020-08-14 04:16:18 -07:00
|
|
|
t.Fatalf("%s: failed to handle operation %s: %v", n.Participant, op.Type, err)
|
2020-08-14 03:55:01 -07:00
|
|
|
}
|
|
|
|
for _, op := range operations {
|
|
|
|
tr.BroadcastOperation(t, op)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
//deals
|
|
|
|
runStep(tr, func(n *Node, wg *sync.WaitGroup) {
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
var payload responses.DKGProposalCommitParticipantResponse
|
|
|
|
for _, req := range n.commits {
|
|
|
|
p := responses.DKGProposalCommitParticipantEntry{
|
|
|
|
ParticipantId: req.ParticipantId,
|
2020-08-21 09:59:47 -07:00
|
|
|
Addr: fmt.Sprintf("Participant#%d", req.ParticipantId),
|
|
|
|
DkgCommit: req.Commit,
|
2020-08-14 03:55:01 -07:00
|
|
|
}
|
|
|
|
payload = append(payload, &p)
|
|
|
|
}
|
|
|
|
op := createOperation(t, string(dkg_proposal_fsm.StateDkgDealsAwaitConfirmations), "", payload)
|
|
|
|
|
|
|
|
operations, err := n.Machine.HandleOperation(op)
|
|
|
|
if err != nil {
|
2020-08-14 04:16:18 -07:00
|
|
|
t.Fatalf("%s: failed to handle operation %s: %v", n.Participant, op.Type, err)
|
2020-08-14 03:55:01 -07:00
|
|
|
}
|
|
|
|
for _, op := range operations {
|
|
|
|
tr.BroadcastOperation(t, op)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
//responses
|
|
|
|
runStep(tr, func(n *Node, wg *sync.WaitGroup) {
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
var payload responses.DKGProposalDealParticipantResponse
|
|
|
|
for _, req := range n.deals {
|
|
|
|
p := responses.DKGProposalDealParticipantEntry{
|
|
|
|
ParticipantId: req.ParticipantId,
|
2020-08-21 09:59:47 -07:00
|
|
|
Addr: fmt.Sprintf("Participant#%d", req.ParticipantId),
|
|
|
|
DkgDeal: req.Deal,
|
2020-08-14 03:55:01 -07:00
|
|
|
}
|
|
|
|
payload = append(payload, &p)
|
|
|
|
}
|
|
|
|
op := createOperation(t, string(dkg_proposal_fsm.StateDkgResponsesAwaitConfirmations), "", payload)
|
|
|
|
|
|
|
|
operations, err := n.Machine.HandleOperation(op)
|
|
|
|
if err != nil {
|
2020-08-14 04:16:18 -07:00
|
|
|
t.Fatalf("%s: failed to handle operation %s: %v", n.Participant, op.Type, err)
|
2020-08-14 03:55:01 -07:00
|
|
|
}
|
|
|
|
for _, op := range operations {
|
|
|
|
tr.BroadcastOperation(t, op)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
//master key
|
|
|
|
runStep(tr, func(n *Node, wg *sync.WaitGroup) {
|
|
|
|
defer wg.Done()
|
|
|
|
|
2020-08-21 09:59:47 -07:00
|
|
|
var payload responses.DKGProposalResponseParticipantResponse
|
2020-08-14 03:55:01 -07:00
|
|
|
for _, req := range n.responses {
|
2020-08-21 09:59:47 -07:00
|
|
|
p := responses.DKGProposalResponseParticipantEntry{
|
2020-08-14 03:55:01 -07:00
|
|
|
ParticipantId: req.ParticipantId,
|
2020-08-21 09:59:47 -07:00
|
|
|
Addr: fmt.Sprintf("Participant#%d", req.ParticipantId),
|
|
|
|
DkgResponse: req.Response,
|
2020-08-14 03:55:01 -07:00
|
|
|
}
|
|
|
|
payload = append(payload, &p)
|
|
|
|
}
|
|
|
|
op := createOperation(t, string(dkg_proposal_fsm.StateDkgMasterKeyAwaitConfirmations), "", payload)
|
|
|
|
|
|
|
|
operations, err := n.Machine.HandleOperation(op)
|
|
|
|
if err != nil {
|
2020-08-14 04:16:18 -07:00
|
|
|
t.Fatalf("%s: failed to handle operation %s: %v", n.Participant, op.Type, err)
|
2020-08-14 03:55:01 -07:00
|
|
|
}
|
|
|
|
for _, op := range operations {
|
|
|
|
tr.BroadcastOperation(t, op)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-08-14 07:38:58 -07:00
|
|
|
// check that all master keys are equal
|
|
|
|
for _, n := range tr.nodes {
|
|
|
|
for i := 0; i < len(n.masterKeys); i++ {
|
|
|
|
if !bytes.Equal(n.masterKeys[0].MasterKey, n.masterKeys[i].MasterKey) {
|
|
|
|
t.Fatalf("master keys is not equal!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
msgToSign := []byte("i am a message")
|
|
|
|
sigShares := make([][]byte, 0)
|
|
|
|
for _, n := range tr.nodes {
|
2020-08-18 11:52:04 -07:00
|
|
|
sigShare, err := n.Machine.createPartialSign(msgToSign, DKGIdentifier)
|
2020-08-14 07:38:58 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to create sig share: %v", err.Error())
|
|
|
|
}
|
|
|
|
sigShares = append(sigShares, sigShare)
|
|
|
|
}
|
|
|
|
|
2020-08-18 11:52:04 -07:00
|
|
|
fullSign, err := tr.nodes[0].Machine.recoverFullSign(msgToSign, sigShares, DKGIdentifier)
|
2020-08-14 07:38:58 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to recover full sign: %v", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, n := range tr.nodes {
|
2020-08-18 11:52:04 -07:00
|
|
|
if err = n.Machine.verifySign(msgToSign, fullSign, DKGIdentifier); err != nil {
|
2020-08-14 07:38:58 -07:00
|
|
|
t.Fatalf("failed to verify signature: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2020-08-14 04:16:18 -07:00
|
|
|
|
2020-08-14 07:38:58 -07:00
|
|
|
fmt.Println("DKG succeeded, signature recovered and verified")
|
2020-08-14 03:55:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func runStep(transport *Transport, cb func(n *Node, wg *sync.WaitGroup)) {
|
|
|
|
var wg = &sync.WaitGroup{}
|
|
|
|
for _, node := range transport.nodes {
|
|
|
|
wg.Add(1)
|
|
|
|
n := node
|
|
|
|
go cb(n, wg)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
}
|