forked from urnetwork/connect
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransfer_contract_manager_test.go
More file actions
137 lines (108 loc) · 3.43 KB
/
transfer_contract_manager_test.go
File metadata and controls
137 lines (108 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package connect
import (
"context"
"testing"
"time"
// mathrand "math/rand"
"crypto/hmac"
"crypto/sha256"
"github.com/go-playground/assert/v2"
// "google.golang.org/protobuf/proto"
"github.com/urnetwork/connect/protocol"
)
func TestTakeContract(t *testing.T) {
// in parallel, add contracts, take contracts, and optionally return contract
// make sure all created contracts get eventually taken
k := 4
n := 64
// contractReturnP := float32(0.5)
timeout := 30 * time.Second
ctx := context.Background()
clientId := NewId()
client := NewClientWithDefaults(ctx, clientId, NewNoContractClientOob())
defer client.Cancel()
contractManager := client.ContractManager()
destinationId := NewId()
contractManager.SetProvideModesWithReturnTraffic(map[protocol.ProvideMode]bool{
protocol.ProvideMode_Network: true,
protocol.ProvideMode_Public: true,
})
contracts := make(chan *protocol.Contract)
go func() {
for i := 0; i < k*n; i += 1 {
contractId := NewId()
contractByteCount := gib(1)
relationship := protocol.ProvideMode_Public
provideSecretKey, ok := contractManager.GetProvideSecretKey(relationship)
assert.Equal(t, true, ok)
storedContract := &protocol.StoredContract{
ContractId: contractId.Bytes(),
TransferByteCount: uint64(contractByteCount),
SourceId: clientId.Bytes(),
DestinationId: destinationId.Bytes(),
}
storedContractBytes, err := ProtoMarshal(storedContract)
assert.Equal(t, nil, err)
defer MessagePoolReturn(storedContractBytes)
mac := hmac.New(sha256.New, provideSecretKey)
storedContractHmac := mac.Sum(storedContractBytes)
verified := contractManager.Verify(storedContractHmac, storedContractBytes, relationship)
assert.Equal(t, true, verified)
result := &protocol.CreateContractResult{
Contract: &protocol.Contract{
StoredContractBytes: storedContractBytes,
StoredContractHmac: storedContractHmac,
ProvideMode: relationship,
},
}
frame, err := ToFrame(result, DefaultProtocolVersion)
assert.Equal(t, nil, err)
contractManager.Receive(SourceId(ControlId), []*protocol.Frame{frame}, protocol.ProvideMode_Network)
}
}()
for j := 0; j < k; j += 1 {
go func() {
for i := 0; i < n; {
contractKey := ContractKey{
Destination: DestinationId(destinationId),
}
if contract := contractManager.TakeContract(ctx, contractKey, timeout); contract != nil {
// if mathrand.Float32() < contractReturnP {
// // put back
// contractManager.ReturnContract(ctx, destinationId, contract)
// } else {
select {
case contracts <- contract:
case <-time.After(timeout):
t.FailNow()
}
i += 1
// }
}
}
}()
}
contractIds := map[Id]bool{}
for i := 0; i < k*n; i += 1 {
select {
case contract := <-contracts:
var storedContract protocol.StoredContract
err := ProtoUnmarshal(contract.StoredContractBytes, &storedContract)
assert.Equal(t, nil, err)
contractId, err := IdFromBytes(storedContract.ContractId)
assert.Equal(t, nil, err)
assert.Equal(t, false, contractIds[contractId])
contractIds[contractId] = true
case <-time.After(timeout):
t.FailNow()
}
}
assert.Equal(t, k*n, len(contractIds))
// no more
contractKey := ContractKey{
Destination: DestinationId(destinationId),
}
contract := contractManager.TakeContract(ctx, contractKey, 0)
assert.Equal(t, nil, contract)
// all the contracts are accounted for
}