forked from FactomProject/factom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ack.go
138 lines (114 loc) · 3.76 KB
/
ack.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
// Copyright 2016 Factom Foundation
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package factom
import (
"encoding/json"
"fmt"
)
type FactoidTxStatus struct {
TxID string `json:"txid"`
GeneralTransactionData
}
func (f *FactoidTxStatus) String() string {
var s string
s += fmt.Sprintln("TxID:", f.TxID)
s += fmt.Sprintln("Status:", f.Status)
s += fmt.Sprintln("Date:", f.TransactionDateString)
return s
}
type EntryStatus struct {
CommitTxID string `json:"committxid"`
EntryHash string `json:"entryhash"`
CommitData GeneralTransactionData `json:"commitdata"`
EntryData GeneralTransactionData `json:"entrydata"`
ReserveTransactions []ReserveInfo `json:"reserveinfo,omitempty"`
ConflictingRevealEntryHashes []string `json:"conflictingrevealentryhashes,omitempty"`
}
func (e *EntryStatus) String() string {
var s string
if e.EntryHash != "" {
s += fmt.Sprintln("EntryHash:", e.EntryHash)
s += fmt.Sprintln("Status:", e.EntryData.Status)
s += fmt.Sprintln("Date:", e.EntryData.TransactionDateString)
}
s += fmt.Sprintln("TxID:", e.CommitTxID)
s += fmt.Sprintln("Status:", e.CommitData.Status)
s += fmt.Sprintln("Date:", e.CommitData.TransactionDateString)
return s
}
type ReserveInfo struct {
TxID string `json:"txid"`
Timeout int64 `json:"timeout"` //Unix time
}
type GeneralTransactionData struct {
// TransactionDate in Unix time
TransactionDate int64 `json:"transactiondate,omitempty"`
//TransactionDateString ISO8601 time
TransactionDateString string `json:"transactiondatestring,omitempty"`
//Unix time
BlockDate int64 `json:"blockdate,omitempty"`
//ISO8601 time
BlockDateString string `json:"blockdatestring,omitempty"`
Malleated *Malleated `json:"malleated,omitempty"`
Status string `json:"status"`
}
type Malleated struct {
MalleatedTxIDs []string `json:"malleatedtxids"`
}
// EntryCommitACK takes the txid of the commit and searches for the entry/chain commit
func EntryCommitACK(txID, fullTransaction string) (*EntryStatus, error) {
params := ackRequest{Hash: txID, ChainID: "c", FullTransaction: fullTransaction}
req := NewJSON2Request("ack", APICounter(), params)
resp, err := factomdRequest(req)
if err != nil {
return nil, err
}
if resp.Error != nil {
return nil, resp.Error
}
eb := new(EntryStatus)
if err := json.Unmarshal(resp.JSONResult(), eb); err != nil {
return nil, err
}
return eb, nil
}
func FactoidACK(txID, fullTransaction string) (*FactoidTxStatus, error) {
params := ackRequest{Hash: txID, ChainID: "f", FullTransaction: fullTransaction}
req := NewJSON2Request("ack", APICounter(), params)
resp, err := factomdRequest(req)
if err != nil {
return nil, err
}
if resp.Error != nil {
return nil, resp.Error
}
eb := new(FactoidTxStatus)
if err := json.Unmarshal(resp.JSONResult(), eb); err != nil {
return nil, err
}
return eb, nil
}
// EntryRevealACK will take the entryhash and search for the entry and the commit
func EntryRevealACK(entryhash, fullTransaction, chainiID string) (*EntryStatus, error) {
params := ackRequest{Hash: entryhash, ChainID: chainiID, FullTransaction: fullTransaction}
req := NewJSON2Request("ack", APICounter(), params)
resp, err := factomdRequest(req)
if err != nil {
return nil, err
}
if resp.Error != nil {
return nil, resp.Error
}
eb := new(EntryStatus)
if err := json.Unmarshal(resp.JSONResult(), eb); err != nil {
return nil, err
}
return eb, nil
}
// EntryACK is a deprecated call and SHOULD NOT BE USED.
// Use either EntryCommitAck or EntryRevealAck depending on the
// type of hash you are sending.
func EntryACK(entryhash, fullTransaction string) (*EntryStatus, error) {
return EntryRevealACK(entryhash, fullTransaction, "0000000000000000000000000000000000000000000000000000000000000000")
}