-
Notifications
You must be signed in to change notification settings - Fork 92
/
ChainedTransactions_test.go
117 lines (97 loc) · 3.15 KB
/
ChainedTransactions_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
package longtest
import (
"fmt"
"testing"
"time"
"github.com/FactomProject/factom"
. "github.com/FactomProject/factomd/engine"
. "github.com/FactomProject/factomd/testHelper"
)
// FIXME: test runs > 40 min try to tune down to 10 min
// TODO: refactor to use testAccount helpers
func TestChainedTransactions(t *testing.T) {
// a genesis block address w/ funding
bankSecret := "Fs3E9gV6DXsYzf7Fqx1fVBQPQXV695eP3k5XbmHEZVRLkMdD9qCK"
bankAddress := "FA2jK2HcLnRdS94dEcU27rF3meoJfpUcZPSinpb7AwQvPRY6RL1Q"
var depositSecrets []string
var depositAddresses []string
for i := 0; i < 120; i++ {
priv, addr := RandomFctAddressPair()
depositSecrets = append(depositSecrets, priv)
depositAddresses = append(depositAddresses, addr)
}
var maxBlocks = 500
state0 := SetupSim("LAF", map[string]string{"--debuglog": "."}, maxBlocks+1, 0, 0, t)
var ecPrice uint64 = state0.GetFactoshisPerEC() //10000
var oneFct uint64 = factom.FactoidToFactoshi("1")
waitForDeposit := func(i int, amt uint64) uint64 {
balance := GetBalance(state0, depositAddresses[i])
TimeNow(state0)
fmt.Printf("%v waitForDeposit %v %v - %v = diff: %v \n", i, depositAddresses[i], balance, amt, balance-int64(amt))
var waited bool
for balance != int64(amt) {
waited = true
balance = GetBalance(state0, depositAddresses[i])
time.Sleep(time.Millisecond * 100)
}
if waited {
fmt.Printf("%v waitForDeposit %v %v - %v = diff: %v \n", i, depositAddresses[i], balance, amt, balance-int64(amt))
TimeNow(state0)
}
return uint64(balance)
}
_ = waitForDeposit
initialBalance := 10 * oneFct
fee := 12 * ecPrice
prepareTransactions := func(bal uint64) ([]func(), uint64, int) {
var transactions []func()
var i int
for i = 0; i < len(depositAddresses)-1; i += 1 {
bal -= fee
in := i
out := i + 1
send := bal
txn := func() {
SendTxn(state0, send, depositSecrets[in], depositAddresses[out], ecPrice)
}
transactions = append(transactions, txn)
}
return transactions, bal, i
}
// offset to send initial blocking transaction
offset := 1
mkTransactions := func() { // txnGenerator
// fund the start address
SendTxn(state0, initialBalance, bankSecret, depositAddresses[0], ecPrice)
WaitMinutes(state0, 1)
waitForDeposit(0, initialBalance)
transactions, finalBalance, finalAddress := prepareTransactions(initialBalance)
var sent []int
var unblocked bool = false
for i := 1; i < len(transactions); i++ {
sent = append(sent, i)
//fmt.Printf("offset: %v <=> i:%v", offset, i)
if i == offset {
fmt.Printf("\n==>TXN offset%v\n", offset)
transactions[0]() // unblock the transactions
unblocked = true
}
transactions[i]()
}
if !unblocked {
transactions[0]() // unblock the transactions
}
offset++ // next time start further in the future
fmt.Printf("send chained transations")
waitForDeposit(finalAddress, finalBalance)
// empty final address returning remaining funds to bank
SendTxn(state0, finalBalance-fee, depositSecrets[finalAddress], bankAddress, ecPrice)
waitForDeposit(finalAddress, 0)
}
for x := 1; x <= 120; x++ {
mkTransactions()
WaitBlocks(state0, 1)
}
WaitForAllNodes(state0)
ShutDownEverything(t)
}