github.com/cranelv/ethereum_mpc@v0.0.0-20191031014521-23aeb1415092/mpcService/mpc_result.go (about)

     1  package mpcService
     2  
     3  import (
     4  	"github.com/ethereum/go-ethereum/log"
     5  	"sync"
     6  	"github.com/ethereum/go-ethereum/mpcService/protocol"
     7  )
     8  
     9  type BaseMpcResult struct {
    10  	mu sync.RWMutex
    11  	Result     map[string]interface{}
    12  }
    13  func createMpcBaseMpcResult() *BaseMpcResult {
    14  	return &BaseMpcResult{Result:make(map[string]interface{})}
    15  }
    16  
    17  func (result *BaseMpcResult) Initialize(preSetValue ... protocol.MpcValue) {
    18  	log.Info("BaseMpcResult.InitializeValue begin")
    19  	for i := 0; i < len(preSetValue); i++ {
    20  		result.Result[preSetValue[i].Key] = preSetValue[i].Value
    21  	}
    22  }
    23  
    24  
    25  func (mpc *BaseMpcResult) SetValue(key string, value interface{}) error {
    26  	mpc.mu.Lock()
    27  	defer mpc.mu.Unlock()
    28  	mpc.Result[key] = value
    29  	return nil
    30  }
    31  
    32  func (mpc *BaseMpcResult) GetValue(key string) (interface{}, error) {
    33  	mpc.mu.RLock()
    34  	defer mpc.mu.RUnlock()
    35  	value, exist := mpc.Result[key]
    36  	if exist {
    37  		return value, nil
    38  	}
    39  
    40  	log.Error("BaseMpcResult GetValue fail." ,"key", key)
    41  	return value, protocol.ErrMpcResultExist
    42  }
    43