-
Notifications
You must be signed in to change notification settings - Fork 1
/
confirmerlib.go
143 lines (126 loc) · 4.37 KB
/
confirmerlib.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
package confirmer
import (
"errors"
"fmt"
. "github.com/iotaledger/iota.go/api"
. "github.com/iotaledger/iota.go/bundle"
. "github.com/iotaledger/iota.go/transaction"
. "github.com/iotaledger/iota.go/trinary"
"github.com/unioproject/tanglebeat/lib/multiapi"
"github.com/unioproject/tanglebeat/lib/utils"
"strings"
"time"
)
func (conf *Confirmer) attachToTangle(trunkHash, branchHash Hash, trytes []Trytes) ([]Trytes, uint64, error) {
var apiret multiapi.MultiCallRet
//fmt.Printf("conf call ATT ----------------------- %v\n", conf.IotaMultiAPIaTT)
ret, err := conf.IotaMultiAPIaTT.AttachToTangle(trunkHash, branchHash, 14, trytes, &apiret)
//fmt.Printf("conf end ATT ----------------------- %v\n", conf.IotaMultiAPIaTT)
conf.AEC.CheckError(apiret.Endpoint, err)
return ret, uint64(apiret.Duration / time.Millisecond), err
}
var all9 = Trytes(strings.Repeat("9", 81))
func (conf *Confirmer) promote() (error, Hash) {
var err error
if conf.Log != nil {
var m string
if conf.PromoteChain {
m = "chain"
} else {
m = "blowball"
}
conf.Log.Debugf("CONFIRMER-PROMO: promoting '%v'. Tag = '%v'. Address: %v Tail = %v Bundle = %v",
m, conf.TxTagPromote, conf.AddressPromote, conf.nextTailHashToPromote, conf.bundleHash)
}
transfers := Transfers{{
Address: conf.AddressPromote,
Value: 0,
Tag: conf.TxTagPromote,
}}
ts := utils.UnixSec(time.Now()) // corrected, must be seconds, not millis
prepTransferOptions := PrepareTransfersOptions{
Timestamp: &ts,
}
// TODO multi api
bundleTrytesPrep, err := conf.IotaMultiAPI.GetAPI().PrepareTransfers(all9, transfers, prepTransferOptions)
if conf.AEC.CheckError(conf.IotaMultiAPI.GetAPIEndpoint(), err) {
return fmt.Errorf("from PrepareTransfers: '%v'", err), ""
}
var apiret multiapi.MultiCallRet
st := utils.UnixMs(time.Now())
gttaResp, err := conf.IotaMultiAPIgTTA.GetTransactionsToApprove(3, &apiret)
if conf.AEC.CheckError(apiret.Endpoint, err) {
return fmt.Errorf("from GetTransactionsToApprove: '%v'", err), ""
}
conf.totalDurationGTTAMsec += utils.UnixMs(time.Now()) - st
trunkTxh := conf.nextTailHashToPromote
branchTxh := gttaResp.BranchTransaction
btrytes, duration, err := conf.attachToTangle(trunkTxh, branchTxh, bundleTrytesPrep)
if err != nil {
return fmt.Errorf("from conf.attachToTangle: '%v'", err), ""
}
conf.totalDurationATTMsec += duration
// no multi args!!!
_, err = conf.IotaMultiAPI.StoreAndBroadcast(btrytes, &apiret)
if conf.AEC.CheckError(apiret.Endpoint, err) {
return err, ""
}
nowis := time.Now()
conf.numPromote += 1
tail, err := utils.TailFromBundleTrytes(btrytes)
if err != nil {
return err, ""
}
if conf.PromoteChain {
conf.nextTailHashToPromote = tail.Hash
}
conf.Log.Debugf("CONFIRMER-PROMO: finished promoting bundle hash %v. Next tail to promote = %v",
conf.bundleHash, conf.nextTailHashToPromote)
conf.nextPromoTime = nowis.Add(time.Duration(conf.PromoteEverySec) * time.Second)
return nil, tail.Hash
}
func (conf *Confirmer) reattach() error {
var err error
if curTail, err := utils.TailFromBundleTrytes(conf.lastBundleTrytes); err != nil {
return err
} else {
if curTail.Bundle != conf.bundleHash {
// assert
return errors.New("CONFIRMER-REATT:reattach: inconsistency curTail.Bundle != conf.bundleHash")
}
}
var apiret multiapi.MultiCallRet
gttaResp, err := conf.IotaMultiAPIgTTA.GetTransactionsToApprove(3, &apiret)
if conf.AEC.CheckError(apiret.Endpoint, err) {
return err
}
conf.totalDurationGTTAMsec += uint64(apiret.Duration / time.Millisecond)
var btrytes []Trytes
var duration uint64
btrytes, duration, err = conf.attachToTangle(
gttaResp.TrunkTransaction,
gttaResp.BranchTransaction,
conf.lastBundleTrytes)
if err != nil {
return err
}
conf.totalDurationATTMsec += duration
// no multi args!!!
_, err = conf.IotaMultiAPI.StoreAndBroadcast(btrytes, &apiret)
if conf.AEC.CheckError(apiret.Endpoint, err) {
return err
}
var newTail *Transaction
if newTail, err = utils.TailFromBundleTrytes(btrytes); err != nil {
return err
}
conf.debugf("CONFIRMER-REATT: finished reattaching. New tail hash %v", newTail.Hash)
nowis := time.Now()
conf.numAttach += 1
conf.lastBundleTrytes = btrytes
conf.nextForceReattachTime = nowis.Add(time.Duration(conf.ForceReattachAfterMin) * time.Minute)
conf.nextTailHashToPromote = newTail.Hash
conf.nextPromoTime = nowis // start promoting immediately
conf.isNotPromotable = false
return nil
}