github.com/cranelv/ethereum_mpc@v0.0.0-20191031014521-23aeb1415092/mpcService/mpc_test_ctx_factory.go (about) 1 package mpcService 2 3 import ( 4 "github.com/ethereum/go-ethereum/mpcService/protocol" 5 "github.com/ethereum/go-ethereum/common" 6 "github.com/ethereum/go-ethereum/mpcService/step" 7 "github.com/ethereum/go-ethereum/p2p/discover" 8 ) 9 10 type MpcTestCtxFactory struct { 11 test bool 12 step int 13 result *BaseMpcResult 14 } 15 16 func (mf *MpcTestCtxFactory) CreateContext(ctxType uint, hash common.Hash, peers []protocol.PeerInfo,leader *discover.NodeID, preSetValue ...protocol.MpcValue) (MpcInterface, error) { 17 switch ctxType { 18 case protocol.MpcCreateLockAccountLeader: 19 return mf.CreateRequestAccountMpc(hash, peers,leader, preSetValue...) 20 21 case protocol.MpcCreateLockAccountPeer: 22 return mf.acknowledgeCreateAccountMpc(hash, peers,leader, preSetValue...) 23 24 case protocol.MpcTXSignLeader: 25 return mf.requestTxSignMpc(hash, peers,leader, preSetValue...) 26 27 case protocol.MpcTXSignPeer: 28 return mf.acknowledgeTxSignMpc(hash, peers,leader, preSetValue...) 29 } 30 31 return nil, protocol.ErrContextType 32 } 33 34 //send create LockAccount from leader 35 func (mf *MpcTestCtxFactory)CreateRequestAccountMpc(mpcID common.Hash, peers []protocol.PeerInfo,leader *discover.NodeID, preSetValue ...protocol.MpcValue) (*MpcContext, error) { 36 result := createMpcBaseMpcResult() 37 mf.result = result 38 result.Initialize(preSetValue...) 39 mpc := createMpcContext(mpcID, peers,leader, result) 40 requestMpc := step.CreateRequestMpcStep(result,mpc.NodeInfo, protocol.MpcCreateLockAccountLeader) 41 mpcReady := step.CreateMpcReadyStep(result,mpc.NodeInfo) 42 return mf.generateCreateTestMpc(mpc,result, requestMpc, mpcReady) 43 } 44 45 //get message from leader and create Context 46 func (mf *MpcTestCtxFactory)acknowledgeCreateAccountMpc(mpcID common.Hash, peers []protocol.PeerInfo,leader *discover.NodeID, preSetValue ...protocol.MpcValue) (*MpcContext, error) { 47 findMap := make(map[uint64]bool) 48 for _, item := range peers { 49 _, exist := findMap[item.Seed] 50 if exist { 51 return nil, protocol.ErrMpcSeedDuplicate 52 } 53 findMap[item.Seed] = true 54 } 55 56 result := createMpcBaseMpcResult() 57 mf.result = result 58 result.Initialize(preSetValue...) 59 mpc := createMpcContext(mpcID, peers,leader, result) 60 AcknowledgeMpc := step.CreateAcknowledgeMpcStep(result,mpc.NodeInfo, protocol.MpcCreateLockAccountPeer) 61 mpcReady := step.CreateGetMpcReadyStep(result,mpc.NodeInfo) 62 return mf.generateCreateTestMpc(mpc, result,AcknowledgeMpc, mpcReady) 63 } 64 65 func (mf *MpcTestCtxFactory)generateCreateTestMpc(mpc *MpcContext,result protocol.MpcResultInterface, firstStep protocol.MpcStepFunc, readyStep protocol.MpcStepFunc) (*MpcContext, error) { 66 if mf.test{ 67 test := 1000 68 mpcTest := make([]protocol.MpcStepFunc, test+2) 69 mpcTest[0] = firstStep 70 mpcTest[1] = readyStep 71 for i := 0; i < test; i++ { 72 mpcTest[i+2] = step.CreateMpcJRSS_Step(result,protocol.MPCDegree, mpc.NodeInfo) 73 } 74 75 mpc.setMpcStep(mpcTest[:mf.step]...) 76 return mpc, nil 77 }else { 78 JRSS := step.CreateMpcJRSS_Step(result,protocol.MPCDegree, mpc.NodeInfo) 79 PublicKey := step.CreateMpcAddressStep(result,mpc.NodeInfo) 80 ackAddress := step.CreateAckMpcAccountStep(result,mpc.NodeInfo) 81 mpcTest := []protocol.MpcStepFunc{firstStep, readyStep, JRSS, PublicKey, ackAddress} 82 mpc.setMpcStep(mpcTest[:mf.step]...) 83 return mpc, nil 84 } 85 } 86 func (mf *MpcTestCtxFactory)requestTxSignMpc(hash common.Hash, peers []protocol.PeerInfo,leader *discover.NodeID, preSetValue ...protocol.MpcValue) (*MpcContext, error) { 87 result := createMpcBaseMpcResult() 88 mf.result = result 89 result.Initialize(preSetValue...) 90 mpc := createMpcContext(hash, peers,leader, result) 91 requestMpc := step.CreateRequestMpcStep(result,mpc.NodeInfo, protocol.MpcTXSignLeader) 92 mpcReady := step.CreateMpcReadyStep(result,mpc.NodeInfo) 93 return mf.generateTxSignMpc(mpc,result, requestMpc, mpcReady) 94 } 95 96 //get message from leader and create Context 97 func (mf *MpcTestCtxFactory)acknowledgeTxSignMpc(hash common.Hash, peers []protocol.PeerInfo,leader *discover.NodeID, preSetValue ...protocol.MpcValue) (*MpcContext, error) { 98 result := createMpcBaseMpcResult() 99 mf.result = result 100 result.Initialize(preSetValue...) 101 mpc := createMpcContext(hash, peers,leader, result) 102 AcknowledgeMpc := step.CreateAcknowledgeMpcStep(result,mpc.NodeInfo, protocol.MpcTXSignPeer) 103 mpcReady := step.CreateGetMpcReadyStep(result,mpc.NodeInfo) 104 return mf.generateTxSignMpc(mpc,result, AcknowledgeMpc, mpcReady) 105 } 106 107 func (mf *MpcTestCtxFactory)generateTxSignMpc(mpc *MpcContext,result protocol.MpcResultInterface, firstStep protocol.MpcStepFunc, readyStep protocol.MpcStepFunc) (*MpcContext, error) { 108 109 JRJZ := step.CreateTXSignJR_JZ_Step(result,protocol.MPCDegree, mpc.NodeInfo) 110 111 preKeys := []string{protocol.MpcSignA0} 112 ResultKeys := []string{protocol.MpcSignAPoint} 113 AGPoint := step.CreateMpcPoint_Step(result,mpc.NodeInfo, preKeys, ResultKeys) 114 115 lagStepPreValueKeys := []string{protocol.MpcSignARSeed} 116 lagStepResultKeys := []string{protocol.MpcSignARResult} 117 ARLag := step.CreateTXSign_Lagrange_Step(result,mpc.NodeInfo, lagStepPreValueKeys, lagStepResultKeys) 118 119 TXSignLag := step.CreateTxSign_CalSignStep(result,mpc.NodeInfo, protocol.MpcTxSignResult) 120 mpcTest := []protocol.MpcStepFunc{firstStep, readyStep, JRJZ, AGPoint, ARLag, TXSignLag} 121 mpc.setMpcStep(mpcTest[:mf.step]...) 122 return mpc, nil 123 }