aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJimmy Hu <jimmy.hu@dexon.org>2018-11-15 14:08:17 +0800
committerGitHub <noreply@github.com>2018-11-15 14:08:17 +0800
commit02669f255e0d131c9e5fce49660103a636dddd51 (patch)
tree2b8e00752bb057e62c3aab8b072b1af58d7deb06
parentce9da6912a16f064160781bbff8a9762e305bae9 (diff)
downloaddexon-consensus-02669f255e0d131c9e5fce49660103a636dddd51.tar
dexon-consensus-02669f255e0d131c9e5fce49660103a636dddd51.tar.gz
dexon-consensus-02669f255e0d131c9e5fce49660103a636dddd51.tar.bz2
dexon-consensus-02669f255e0d131c9e5fce49660103a636dddd51.tar.lz
dexon-consensus-02669f255e0d131c9e5fce49660103a636dddd51.tar.xz
dexon-consensus-02669f255e0d131c9e5fce49660103a636dddd51.tar.zst
dexon-consensus-02669f255e0d131c9e5fce49660103a636dddd51.zip
core: Add timeout to TSig (#330)
-rw-r--r--core/configuration-chain.go13
-rw-r--r--core/configuration-chain_test.go37
2 files changed, 49 insertions, 1 deletions
diff --git a/core/configuration-chain.go b/core/configuration-chain.go
index 03a4602..47a40b1 100644
--- a/core/configuration-chain.go
+++ b/core/configuration-chain.go
@@ -265,13 +265,24 @@ func (cc *configurationChain) runTSig(
}
}
}()
+ timeout := make(chan struct{}, 1)
+ go func() {
+ // TODO(jimmy-dexon): make timeout configurable.
+ time.Sleep(5 * time.Second)
+ timeout <- struct{}{}
+ cc.tsigReady.Broadcast()
+ }()
var signature crypto.Signature
var err error
for func() bool {
signature, err = cc.tsig[hash].signature()
+ select {
+ case <-timeout:
+ return false
+ default:
+ }
return err == ErrNotEnoughtPartialSignatures
}() {
- // TODO(jimmy-dexon): add a timeout here.
cc.tsigReady.Wait()
}
delete(cc.tsig, hash)
diff --git a/core/configuration-chain_test.go b/core/configuration-chain_test.go
index 129d07c..281be06 100644
--- a/core/configuration-chain_test.go
+++ b/core/configuration-chain_test.go
@@ -330,6 +330,43 @@ func (s *ConfigurationChainTestSuite) TestMultipleTSig() {
}
}
+func (s *ConfigurationChainTestSuite) TestTSigTimeout() {
+ k := 2
+ n := 7
+ round := uint64(0)
+ cfgChains := s.runDKG(k, n, round)
+ timeout := 6 * time.Second
+
+ hash := crypto.Keccak256Hash([]byte("🍯🍋"))
+
+ psigs := s.preparePartialSignature(hash, round, cfgChains)
+
+ errs := make(chan error, n)
+ qualify := 0
+ for nID, cc := range cfgChains {
+ if _, exist := cc.gpk[round].qualifyNodeIDs[nID]; !exist {
+ continue
+ }
+ qualify++
+ go func(cc *configurationChain) {
+ _, err := cc.runTSig(round, hash)
+ // Prevent racing by collecting errors and check in main thread.
+ errs <- err
+ }(cc)
+ // Only 1 partial signature is provided.
+ err := cc.processPartialSignature(psigs[0])
+ s.Require().NoError(err)
+ }
+ time.Sleep(timeout)
+ s.Require().Len(errs, qualify)
+ for nID, cc := range cfgChains {
+ if _, exist := cc.gpk[round].qualifyNodeIDs[nID]; !exist {
+ continue
+ }
+ s.Equal(<-errs, ErrNotEnoughtPartialSignatures)
+ }
+}
+
func TestConfigurationChain(t *testing.T) {
suite.Run(t, new(ConfigurationChainTestSuite))
}