-
Notifications
You must be signed in to change notification settings - Fork 0
/
history.go
67 lines (52 loc) · 1.36 KB
/
history.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
package main
import (
"encoding/binary"
"github.com/hyperledger/fabric/core/chaincode/shim"
b64 "encoding/base64"
"errors"
)
func (t *LoyaltyChaincode) getHistory(stub shim.ChaincodeStubInterface, key string, valueType ValueType) ([]HistoryEntry, error) {
historyIer, err := stub.GetHistoryForKey(key)
if err != nil {
return nil, err
}
var history []HistoryEntry = []HistoryEntry{}
for i := 0; historyIer.HasNext(); i++ {
modification, err := historyIer.Next()
if err != nil {
return nil, err
}
var value string;
switch valueType {
case UInt64:
uintValue := binary.LittleEndian.Uint64(modification.Value)
value = uintToString(uintValue)
case String:
value = string(modification.Value)
default:
value = b64.StdEncoding.EncodeToString(modification.Value)
}
historyEntry := &HistoryEntry{
TxId: modification.TxId,
Timestamp: modification.Timestamp.Seconds,
Value: value,
}
history = append(history, *historyEntry)
}
return history, nil
}
func (t *LoyaltyChaincode) getEntryInfo(stub shim.ChaincodeStubInterface, key string) (*InfoEntry, error) {
history, err := t.getHistory(stub, key, String)
if err != nil {
return nil, err
}
n := len(history) - 1
if n < 0 {
return nil, errors.New("No entry history found")
}
return &InfoEntry{
TxId: history[n].TxId,
Timestamp: history[n].Timestamp,
},
nil
}