-
Notifications
You must be signed in to change notification settings - Fork 74
/
randomize_test.go
166 lines (154 loc) · 5.26 KB
/
randomize_test.go
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Copyright (c) 2018 Tomochain
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package randomize
import (
"context"
"math/big"
"testing"
"time"
"github.com/tomochain/tomochain/accounts/abi/bind"
"github.com/tomochain/tomochain/accounts/abi/bind/backends"
"github.com/tomochain/tomochain/common"
"github.com/tomochain/tomochain/contracts"
"github.com/tomochain/tomochain/core"
"github.com/tomochain/tomochain/core/types"
"github.com/tomochain/tomochain/crypto"
"github.com/tomochain/tomochain/params"
)
var (
epocNumber = int64(12)
key, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
addr = crypto.PubkeyToAddress(key.PublicKey)
byte0 = make([][32]byte, epocNumber)
acc1Key, _ = crypto.HexToECDSA("8a1f9a8f95be41cd7ccb6168179afb4504aefe388d1e14474d32c45c72ce7b7a")
acc1Addr = crypto.PubkeyToAddress(acc1Key.PublicKey)
)
func TestRandomize(t *testing.T) {
contractBackend := backends.NewSimulatedBackend(core.GenesisAlloc{
addr: {
Balance: big.NewInt(10_000_000_000_000_000),
},
})
chainID, err := contractBackend.ChainID(context.Background())
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
transactOpts, err := bind.NewKeyedTransactorWithChainID(key, chainID)
if err != nil {
t.Fatalf("can't create TransactOpts: %v", err)
}
transactOpts.GasLimit = 1000000
randomizeAddress, randomize, err := DeployRandomize(transactOpts, contractBackend)
if err != nil {
t.Fatalf("can't deploy root registry: %v", err)
}
contractBackend.Commit()
d := time.Now().Add(1000 * time.Millisecond)
ctx, cancel := context.WithDeadline(context.Background(), d)
defer cancel()
contractBackend.CodeAt(ctx, randomizeAddress, nil)
f := func(key, val common.Hash) bool {
return true
}
contractBackend.ForEachStorageAt(ctx, randomizeAddress, nil, f)
_, err = randomize.SetSecret(byte0)
if err != nil {
t.Fatalf("can't set secret: %v", err)
}
contractBackend.Commit()
}
func TestSendTxRandomizeSecretAndOpening(t *testing.T) {
genesis := core.GenesisAlloc{acc1Addr: {Balance: big.NewInt(100_000_000_000_000_000)}}
backend := backends.NewSimulatedBackend(genesis)
backend.Commit()
chainID, err := backend.ChainID(context.Background())
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
signer := types.LatestSignerForChainID(chainID)
ctx := context.Background()
transactOpts, err := bind.NewKeyedTransactorWithChainID(acc1Key, chainID)
if err != nil {
t.Fatalf("can't create TransactOpts: %v", err)
}
transactOpts.GasLimit = 4200000
epocNumber := uint64(900)
randomizeAddr, randomizeContract, err := DeployRandomize(transactOpts, backend)
if err != nil {
t.Fatalf("Can't deploy randomize SC: %v", err)
}
backend.Commit()
randomizeKeyValue := contracts.RandStringByte(32)
for i := 1; i <= 900; i++ {
nonce := uint64(i)
switch i {
case 800:
tx, err := contracts.BuildTxSecretRandomize(nonce, randomizeAddr, epocNumber, randomizeKeyValue)
if err != nil {
t.Fatalf("Can't create tx randomize secret: %v", err)
}
tx, err = types.SignTx(tx, signer, acc1Key)
if err != nil {
t.Fatalf("Can't sign tx randomize secret: %v", err)
}
err = backend.SendTransaction(ctx, tx)
if err != nil {
t.Fatalf("Can't send tx for create randomize secret: %v", err)
}
case 850:
// Set opening.
tx, err := contracts.BuildTxOpeningRandomize(nonce, randomizeAddr, randomizeKeyValue)
if err != nil {
t.Fatalf("Can't create tx randomize opening: %v", err)
}
tx, err = types.SignTx(tx, signer, acc1Key)
if err != nil {
t.Fatalf("Can't sign tx randomize opening: %v", err)
}
err = backend.SendTransaction(ctx, tx)
if err != nil {
t.Fatalf("Can't send tx for create randomize opening: %v", err)
}
case 900:
// Get randomize secret from SC.
secrets, err := randomizeContract.GetSecret(acc1Addr)
if err != nil {
t.Error("Fail get secrets from randomize", err)
}
if len(secrets) <= 0 {
t.Error("Empty get secrets from SC", err)
}
// Decrypt randomize from SC.
opening, err := randomizeContract.GetOpening(acc1Addr)
if err != nil {
t.Fatalf("Can't get secret from SC: %v", err)
}
_, err = contracts.DecryptRandomizeFromSecretsAndOpening(secrets, opening)
if err != nil {
t.Error("Can't decrypt secret and opening", err)
}
default:
tx, err := types.SignTx(types.NewTransaction(nonce, common.Address{}, new(big.Int), 21000, big.NewInt(params.InitialBaseFee), nil), signer, acc1Key)
if err != nil {
t.Fatalf("Can't sign tx randomize: %v", err)
}
err = backend.SendTransaction(ctx, tx)
if err != nil {
t.Fatalf("Can't send tx for create randomize: %v", err)
}
}
backend.Commit()
}
}