-
Notifications
You must be signed in to change notification settings - Fork 0
/
anchorData.go
125 lines (100 loc) · 3.09 KB
/
anchorData.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
package database
import (
"bytes"
"encoding/gob"
"fmt"
"github.com/FactomProject/factomd/common/interfaces"
"github.com/FactomProject/factomd/common/primitives"
)
var AnchorDataStr []byte = []byte("AnchorData")
type AnchorDataBase struct {
MerkleRoot string // Merkle Root of a 1000 block window of Directory Blocks
DBlockHeight uint32 // Maximum height that is in the 1000 block window
EthereumRecordHeight uint32 // Directory Block height for this Ethereum anchor's record within Factom
EthereumRecordEntryHash string // Entry Hash for this Ethereum anchor's record within Factom
Ethereum struct {
ContractAddress string // Contract Address that the anchor was put into
TxID string //0x50ea0effc383542811a58704a6d6842ed6d76439a2d942d941896ad097c06a78
BlockHeight int64 //293003
BlockHash string //0x3b504616495fc9cf7be9b5b776692a9abbfb95491fa62abf62dcdf4d53ff5979
TxIndex int64 // Transaction index within its block
}
}
type AnchorData struct {
AnchorDataBase
}
func (e *AnchorData) JSONByte() ([]byte, error) {
return primitives.EncodeJSON(e)
}
func (e *AnchorData) JSONString() (string, error) {
return primitives.EncodeJSONString(e)
}
func (e *AnchorData) JSONBuffer(b *bytes.Buffer) error {
return primitives.EncodeJSONToBuffer(e, b)
}
func (e *AnchorData) String() string {
str, _ := e.JSONString()
return str
}
var _ interfaces.DatabaseBatchable = (*AnchorData)(nil)
// IsSubmitted returns whether or not an Ethereum transaction has been submitted for this anchor
func (c *AnchorData) IsSubmitted() bool {
return c.Ethereum.TxID != ""
}
// IsComplete returns whether or not a given anchor has been recorded back into Factom
func (c *AnchorData) IsComplete() bool {
return c.EthereumRecordHeight > 0
}
func (c *AnchorData) New() interfaces.BinaryMarshallableAndCopyable {
return new(AnchorData)
}
func (e *AnchorData) GetDatabaseHeight() uint32 {
return e.DBlockHeight
}
func (e *AnchorData) DatabasePrimaryIndex() interfaces.IHash {
return UintToHash(e.DBlockHeight)
}
func (e *AnchorData) DatabaseSecondaryIndex() interfaces.IHash {
h, err := primitives.NewShaHashFromStr(e.MerkleRoot)
if err != nil {
panic(err)
}
return h
}
func UintToHash(i uint32) interfaces.IHash {
h, err := primitives.NewShaHashFromStr(fmt.Sprintf("%032x", i))
if err != nil {
panic(err)
}
return h
}
func (e *AnchorData) GetChainID() interfaces.IHash {
h, err := primitives.NewShaHashFromStr(fmt.Sprintf("%032x", AnchorDataStr))
if err != nil {
panic(err)
}
return h
}
func (e *AnchorData) MarshalBinary() ([]byte, error) {
var data primitives.Buffer
enc := gob.NewEncoder(&data)
err := enc.Encode(e.AnchorDataBase)
if err != nil {
return nil, err
}
return data.DeepCopyBytes(), nil
}
func (e *AnchorData) UnmarshalBinaryData(data []byte) (newData []byte, err error) {
dec := gob.NewDecoder(primitives.NewBuffer(data))
adb := AnchorDataBase{}
err = dec.Decode(&adb)
if err != nil {
return nil, err
}
e.AnchorDataBase = adb
return nil, nil
}
func (e *AnchorData) UnmarshalBinary(data []byte) (err error) {
_, err = e.UnmarshalBinaryData(data)
return
}