dc4bc/airgapped/airgapped_test.go

323 lines
9.6 KiB
Go
Raw Normal View History

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"
"os"
2020-09-02 09:02:44 -07:00
"sync"
"testing"
"time"
client "github.com/depools/dc4bc/client/types"
2020-08-25 05:56:27 -07:00
"github.com/depools/dc4bc/fsm/fsm"
2020-08-14 03:55:01 -07:00
"github.com/depools/dc4bc/fsm/state_machines/dkg_proposal_fsm"
2020-08-28 06:31:31 -07:00
"github.com/depools/dc4bc/fsm/state_machines/signature_proposal_fsm"
2020-08-26 07:12:24 -07:00
"github.com/depools/dc4bc/fsm/state_machines/signing_proposal_fsm"
2020-08-14 03:55:01 -07:00
"github.com/depools/dc4bc/fsm/types/requests"
"github.com/depools/dc4bc/fsm/types/responses"
2020-08-25 05:56:27 -07:00
"github.com/depools/dc4bc/storage"
2020-08-14 03:55:01 -07:00
"github.com/google/uuid"
)
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-09-02 09:02:44 -07:00
partialSigns []requests.SigningProposalPartialSignRequest
2020-08-14 03:55:01 -07:00
}
2020-08-25 05:56:27 -07:00
func (n *Node) storeOperation(t *testing.T, msg storage.Message) {
switch fsm.Event(msg.Event) {
2020-08-14 03:55:01 -07:00
case dkg_proposal_fsm.EventDKGCommitConfirmationReceived:
var req requests.DKGProposalCommitConfirmationRequest
2020-08-25 05:56:27 -07:00
if err := json.Unmarshal(msg.Data, &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
2020-08-25 05:56:27 -07:00
if err := json.Unmarshal(msg.Data, &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
2020-08-25 05:56:27 -07:00
if err := json.Unmarshal(msg.Data, &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
2020-08-25 05:56:27 -07:00
if err := json.Unmarshal(msg.Data, &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-09-02 09:02:44 -07:00
case signing_proposal_fsm.EventSigningPartialSignReceived:
var req requests.SigningProposalPartialSignRequest
2020-08-26 07:12:24 -07:00
if err := json.Unmarshal(msg.Data, &req); err != nil {
t.Fatalf("failed to unmarshal fsm req: %v", err)
}
n.partialSigns = append(n.partialSigns, req)
2020-08-14 03:55:01 -07:00
default:
2020-08-25 05:56:27 -07:00
t.Fatalf("invalid event: %s", msg.Event)
2020-08-14 03:55:01 -07:00
}
}
type Transport struct {
nodes []*Node
}
2020-08-25 05:56:27 -07:00
func (tr *Transport) BroadcastMessage(t *testing.T, msg storage.Message) {
2020-08-14 03:55:01 -07:00
for _, node := range tr.nodes {
2020-08-25 05:56:27 -07:00
if msg.RecipientAddr == "" || msg.RecipientAddr == node.Participant {
node.storeOperation(t, msg)
2020-08-14 03:55:01 -07:00
}
}
}
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,
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) {
testDir := "/tmp/airgapped_test"
2020-08-26 07:12:24 -07:00
nodesCount := 10
2020-08-28 06:31:31 -07:00
threshold := 3
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++ {
am, err := NewAirgappedMachine(fmt.Sprintf("%s/%s-%d", testDir, testDB, i))
2020-08-17 03:22:46 -07:00
if err != nil {
t.Fatalf("failed to create airgapped machine: %v", err)
}
2020-08-28 06:31:31 -07:00
am.SetAddress(participants[i])
2020-09-18 06:57:51 -07:00
am.SetEncryptionKey([]byte(fmt.Sprintf(testDB+"%d", i)))
if err = am.InitKeys(); err != nil {
t.Fatalf(err.Error())
}
2020-08-14 03:55:01 -07:00
node := Node{
ParticipantID: i,
Participant: participants[i],
Machine: am,
}
tr.nodes = append(tr.nodes, &node)
}
defer os.RemoveAll(testDir)
2020-08-14 03:55:01 -07:00
2020-08-28 06:31:31 -07:00
var initReq responses.SignatureProposalParticipantInvitationsResponse
for _, n := range tr.nodes {
pubKey, err := n.Machine.pubKey.MarshalBinary()
if err != nil {
t.Fatalf("failed to marshal dkg pubkey: %v", err)
}
2020-08-28 06:31:31 -07:00
entry := &responses.SignatureProposalParticipantInvitationEntry{
ParticipantId: n.ParticipantID,
Addr: n.Participant,
Threshold: threshold,
DkgPubKey: pubKey,
2020-08-28 06:31:31 -07:00
}
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,
Addr: n.Participant,
2020-08-14 03:55:01 -07:00
DkgPubKey: pubKey,
}
getCommitsRequest = append(getCommitsRequest, entry)
}
2020-08-28 06:31:31 -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()
2020-08-25 05:56:27 -07:00
operation, err := n.Machine.HandleOperation(op)
2020-08-14 03:55:01 -07:00
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
}
2020-08-25 05:56:27 -07:00
for _, msg := range operation.ResultMsgs {
tr.BroadcastMessage(t, msg)
2020-08-14 03:55:01 -07:00
}
})
//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)
2020-08-25 05:56:27 -07:00
operation, err := n.Machine.HandleOperation(op)
2020-08-14 03:55:01 -07:00
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
}
2020-08-25 05:56:27 -07:00
for _, msg := range operation.ResultMsgs {
tr.BroadcastMessage(t, msg)
2020-08-14 03:55:01 -07:00
}
})
//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)
2020-08-25 05:56:27 -07:00
operation, err := n.Machine.HandleOperation(op)
2020-08-14 03:55:01 -07:00
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
}
2020-08-25 05:56:27 -07:00
for _, msg := range operation.ResultMsgs {
tr.BroadcastMessage(t, msg)
2020-08-14 03:55:01 -07:00
}
})
//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)
2020-08-25 05:56:27 -07:00
operation, err := n.Machine.HandleOperation(op)
2020-08-14 03:55:01 -07:00
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
}
2020-08-25 05:56:27 -07:00
for _, msg := range operation.ResultMsgs {
tr.BroadcastMessage(t, msg)
2020-08-14 03:55:01 -07:00
}
})
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")
2020-08-26 07:12:24 -07:00
//partialSigns
runStep(tr, func(n *Node, wg *sync.WaitGroup) {
defer wg.Done()
2020-08-26 08:15:38 -07:00
payload := responses.SigningPartialSignsParticipantInvitationsResponse{
2020-08-26 07:12:24 -07:00
SrcPayload: msgToSign,
}
2020-09-02 09:02:44 -07:00
op := createOperation(t, string(signing_proposal_fsm.StateSigningAwaitPartialSigns), "", payload)
2020-08-26 07:12:24 -07:00
operation, err := n.Machine.HandleOperation(op)
2020-08-14 07:38:58 -07:00
if err != nil {
2020-08-26 07:12:24 -07:00
t.Fatalf("%s: failed to handle operation %s: %v", n.Participant, op.Type, err)
2020-08-14 07:38:58 -07:00
}
2020-08-26 07:12:24 -07:00
for _, msg := range operation.ResultMsgs {
tr.BroadcastMessage(t, msg)
}
})
2020-08-14 07:38:58 -07:00
2020-08-26 07:12:24 -07:00
//recover full signature
runStep(tr, func(n *Node, wg *sync.WaitGroup) {
defer wg.Done()
2020-08-14 07:38:58 -07:00
2020-08-26 07:12:24 -07:00
var payload responses.SigningProcessParticipantResponse
for _, req := range n.partialSigns {
p := responses.SigningProcessParticipantEntry{
ParticipantId: req.ParticipantId,
Addr: fmt.Sprintf("Participant#%d", req.ParticipantId),
PartialSign: req.PartialSign,
}
payload.Participants = append(payload.Participants, &p)
2020-08-14 07:38:58 -07:00
}
2020-08-26 07:12:24 -07:00
payload.SrcPayload = msgToSign
2020-09-02 09:02:44 -07:00
op := createOperation(t, string(signing_proposal_fsm.StateSigningPartialSignsCollected), "", payload)
2020-08-26 07:12:24 -07:00
operation, err := n.Machine.HandleOperation(op)
if err != nil {
t.Fatalf("%s: failed to handle operation %s: %v", n.Participant, op.Type, err)
}
for _, msg := range operation.ResultMsgs {
tr.BroadcastMessage(t, msg)
}
})
2020-08-14 04:16:18 -07:00
2020-08-26 07:12:24 -07:00
fmt.Println("DKG succeeded, signature recovered")
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()
}