forked from xuperchain/xuperchain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathversioned_data.go
87 lines (76 loc) · 2.25 KB
/
versioned_data.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
package xmodel
import (
"fmt"
"github.com/xuperchain/xuperunion/pb"
xmodel_pb "github.com/xuperchain/xuperunion/xmodel/pb"
)
func parseVersion(version string) ([]byte, int, error) {
txid := []byte{}
offset := 0
okNum, err := fmt.Sscanf(version, "%x_%d", &txid, &offset)
if okNum != 2 && err != nil {
return nil, 0, fmt.Errorf("parseVersion failed, invalid version: %s", version)
}
return txid, offset, nil
}
//GetTxidFromVersion parse version and fetch txid from version string
func GetTxidFromVersion(version string) []byte {
txid, _, err := parseVersion(version)
if err != nil {
return []byte("")
}
return txid
}
// MakeVersion generate a version by txid and offset, version = txid_offset
func MakeVersion(txid []byte, offset int32) string {
return fmt.Sprintf("%x_%d", txid, offset)
}
// GetVersion get VersionedData's version, if refTxid is nil, return ""
func GetVersion(vd *xmodel_pb.VersionedData) string {
if vd.RefTxid == nil {
return ""
}
return MakeVersion(vd.RefTxid, vd.RefOffset)
}
// GetVersionOfTxInput get version of TxInput
func GetVersionOfTxInput(txIn *pb.TxInputExt) string {
if txIn.RefTxid == nil {
return ""
}
return MakeVersion(txIn.RefTxid, txIn.RefOffset)
}
// GetTxOutputs get transaction outputs
func GetTxOutputs(pds []*xmodel_pb.PureData) []*pb.TxOutputExt {
outputs := []*pb.TxOutputExt{}
for _, pd := range pds {
outputs = append(outputs, &pb.TxOutputExt{
Bucket: pd.Bucket,
Key: pd.Key,
Value: pd.Value,
})
}
return outputs
}
// GetTxInputs get transaction inputs
func GetTxInputs(vds []*xmodel_pb.VersionedData) []*pb.TxInputExt {
inputs := []*pb.TxInputExt{}
for _, vd := range vds {
inputs = append(inputs, &pb.TxInputExt{
Bucket: vd.GetPureData().GetBucket(),
Key: vd.GetPureData().GetKey(),
RefTxid: vd.RefTxid,
RefOffset: vd.RefOffset,
})
}
return inputs
}
// IsEmptyVersionedData check if VersionedData is empty
func IsEmptyVersionedData(vd *xmodel_pb.VersionedData) bool {
return vd.RefTxid == nil && vd.RefOffset == 0
}
func (s *XModel) makeEmptyVersionedData(bucket string, key []byte) *xmodel_pb.VersionedData {
verData := &xmodel_pb.VersionedData{PureData: &xmodel_pb.PureData{}}
verData.PureData.Bucket = bucket
verData.PureData.Key = key
return verData
}