forked from FactomProject/factomd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
factoidMapper.go
119 lines (106 loc) · 4.07 KB
/
factoidMapper.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
package eventservices
import (
"github.com/FactomProject/factomd/common/interfaces"
"github.com/FactomProject/factomd/events/eventmessages/generated/eventmessages"
)
func mapFactoidBlock(block interfaces.IFBlock) *eventmessages.FactoidBlock {
result := &eventmessages.FactoidBlock{
BodyMerkleRoot: block.GetBodyMR().Bytes(),
KeyMerkleRoot: block.GetKeyMR().Bytes(),
PreviousKeyMerkleRoot: block.GetPrevKeyMR().Bytes(),
PreviousLedgerKeyMerkleRoot: block.GetLedgerKeyMR().Bytes(),
ExchangeRate: block.GetExchRate(),
BlockHeight: block.GetDBHeight(),
TransactionCount: uint32(len(block.GetTransactions())),
Transactions: mapTransactions(block.GetTransactions(), block.GetEndOfPeriod()),
}
return result
}
func mapTransactions(transactions []interfaces.ITransaction, endOfPeriod [10]int) []*eventmessages.Transaction {
result := make([]*eventmessages.Transaction, 0)
for i, transaction := range transactions {
err := transaction.ValidateSignatures()
if err == nil {
// The endOfPeriod array contains the transaction count at the new minute transition time
// The minuteMark will contain the value of during which minute the transaction took place which is one higher than the position within the array
var minuteMark = 0
for ; minuteMark < len(endOfPeriod) && endOfPeriod[minuteMark] > 0; minuteMark++ {
if endOfPeriod[minuteMark] >= i {
minuteMark++
break
}
}
result = append(result, mapTransaction(transaction, minuteMark))
}
}
return result
}
func mapTransaction(transaction interfaces.ITransaction, minuteNumber int) *eventmessages.Transaction {
result := &eventmessages.Transaction{
TransactionID: transaction.GetSigHash().Bytes(),
BlockHeight: transaction.GetBlockHeight(),
MinuteNumber: uint32(minuteNumber),
Timestamp: ConvertTimeToTimestamp(transaction.GetTimestamp().GetTime()),
FactoidInputs: mapTransactionAddresses(transaction.GetInputs()),
FactoidOutputs: mapTransactionAddresses(transaction.GetOutputs()),
EntryCreditOutputs: mapTransactionAddresses(transaction.GetECOutputs()),
RedeemConditionDataStructures: mapRCDs(transaction.GetRCDs()),
SignatureBlocks: mapSignatureBlocks(transaction.GetSignatureBlocks()),
}
return result
}
func mapTransactionAddresses(inputs []interfaces.ITransAddress) []*eventmessages.TransactionAddress {
result := make([]*eventmessages.TransactionAddress, len(inputs))
for i, input := range inputs {
result[i] = mapTransactionAddress(input)
}
return result
}
func mapTransactionAddress(address interfaces.ITransAddress) *eventmessages.TransactionAddress {
result := &eventmessages.TransactionAddress{
Amount: address.GetAmount(),
Address: address.GetAddress().Bytes(),
}
return result
}
func mapRCDs(rcds []interfaces.IRCD) []*eventmessages.RCD {
result := make([]*eventmessages.RCD, len(rcds))
for i, rcd := range rcds {
result[i] = mapRCD(rcd)
}
return result
}
type RCD interface {
GetPublicKey() []byte
}
func mapRCD(rcd interfaces.IRCD) *eventmessages.RCD {
result := &eventmessages.RCD{}
if rcd1, ok := rcd.(RCD); ok {
result.Rcd = &eventmessages.RCD_Rcd1{
Rcd1: &eventmessages.RCD1{
PublicKey: rcd1.GetPublicKey(),
},
}
}
return result
}
func mapSignatureBlocks(blocks []interfaces.ISignatureBlock) []*eventmessages.FactoidSignatureBlock {
result := make([]*eventmessages.FactoidSignatureBlock, len(blocks))
for i, block := range blocks {
result[i] = mapSignatureBlock(block)
}
return result
}
func mapSignatureBlock(block interfaces.ISignatureBlock) *eventmessages.FactoidSignatureBlock {
result := &eventmessages.FactoidSignatureBlock{
Signature: mapFactoidSignatureBlockSignatures(block.GetSignatures()),
}
return result
}
func mapFactoidSignatureBlockSignatures(signatures []interfaces.ISignature) [][]byte {
result := make([][]byte, len(signatures))
for i, signature := range signatures {
result[i] = signature.Bytes()
}
return result
}