forked from FactomProject/factomd
/
ecBlock.go
119 lines (102 loc) · 3.5 KB
/
ecBlock.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 testHelper
//A package for functions used multiple times in tests that aren't useful in production code.
import (
"github.com/FactomProject/factomd/common/entryBlock"
"github.com/FactomProject/factomd/common/entryCreditBlock"
"github.com/FactomProject/factomd/common/interfaces"
"github.com/FactomProject/factomd/common/primitives"
"github.com/FactomProject/factomd/util"
)
func createECEntriesfromBlocks(fBlock interfaces.IFBlock, eBlocks []*entryBlock.EBlock, height int) []interfaces.IECBlockEntry {
ecEntries := []interfaces.IECBlockEntry{}
ecEntries = append(ecEntries, entryCreditBlock.NewServerIndexNumber2(uint8(height%10+1)))
ecEntries = append(ecEntries, entryCreditBlock.NewMinuteNumber(1))
ecEntries = append(ecEntries, entryCreditBlock.NewMinuteNumber(2))
ecEntries = append(ecEntries, entryCreditBlock.NewMinuteNumber(3))
ecEntries = append(ecEntries, entryCreditBlock.NewMinuteNumber(4))
ecEntries = append(ecEntries, entryCreditBlock.NewMinuteNumber(5))
ecEntries = append(ecEntries, entryCreditBlock.NewMinuteNumber(6))
ecEntries = append(ecEntries, entryCreditBlock.NewMinuteNumber(7))
ecEntries = append(ecEntries, entryCreditBlock.NewMinuteNumber(8))
ecEntries = append(ecEntries, entryCreditBlock.NewMinuteNumber(9))
ecEntries = append(ecEntries, entryCreditBlock.NewMinuteNumber(10))
trans := fBlock.GetTransactions()
for _, t := range trans {
ecOut := t.GetECOutputs()
for i, ec := range ecOut {
increase := new(entryCreditBlock.IncreaseBalance)
increase.ECPubKey = primitives.Byte32ToByteSlice32(ec.GetAddress().Fixed())
increase.TXID = t.GetHash()
increase.Index = uint64(i)
increase.NumEC = ec.GetAmount() / fBlock.GetExchRate()
ecEntries = append(ecEntries, increase)
}
}
for _, eBlock := range eBlocks {
if height == 0 {
ecEntries = append(ecEntries, NewCommitChain(eBlock))
} else {
ecEntries = append(ecEntries, NewCommitEntry(eBlock))
}
}
return ecEntries
}
func NewCommitEntry(eBlock *entryBlock.EBlock) *entryCreditBlock.CommitEntry {
commit := entryCreditBlock.NewCommitEntry()
commit.Version = 1
err := commit.MilliTime.UnmarshalBinary([]byte{0, 0, 0, 0, 0, byte(eBlock.GetHeader().GetDBHeight())})
if err != nil {
panic(err)
}
commit.EntryHash = eBlock.Body.EBEntries[0]
bin, err := commit.MarshalBinary()
if err != nil {
panic(err)
}
cost, err := util.EntryCost(bin)
if err != nil {
panic(err)
}
commit.Credits = cost
SignCommit(0, commit)
return commit
}
func NewCommitChain(eBlock *entryBlock.EBlock) *entryCreditBlock.CommitChain {
commit := entryCreditBlock.NewCommitChain()
commit.Version = 1
err := commit.MilliTime.UnmarshalBinary([]byte{0, 0, 0, 0, 0, byte(eBlock.GetHeader().GetDBHeight())})
if err != nil {
panic(err)
}
commit.ChainIDHash = eBlock.GetHashOfChainIDHash()
w := primitives.NewZeroHash()
eh0 := eBlock.GetEntryHashes()[0].Bytes()
cid := eBlock.GetHeader().GetChainID().Bytes()
w.SetBytes(primitives.DoubleSha(append(eh0, cid...)))
commit.Weld = w
commit.EntryHash = eBlock.Body.EBEntries[0]
bin, err := commit.MarshalBinary()
if err != nil {
panic(err)
}
cost, err := util.EntryCost(bin)
if err != nil {
panic(err)
}
commit.Credits = cost
SignCommit(0, commit)
return commit
}
func CreateTestEntryCreditBlock(prev interfaces.IEntryCreditBlock) interfaces.IEntryCreditBlock {
block, err := entryCreditBlock.NextECBlock(prev)
if err != nil {
panic(err)
}
return block
}
func SignCommit(n uint64, tx interfaces.ISignable) {
err := tx.Sign(NewPrivKey(n))
if err != nil {
panic(err)
}
}