-
Notifications
You must be signed in to change notification settings - Fork 0
/
vault.go
95 lines (76 loc) · 1.74 KB
/
vault.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
package ngtype
import (
"errors"
"math/big"
"time"
"github.com/gogo/protobuf/proto"
"golang.org/x/crypto/sha3"
)
var (
ErrNotCheckpoint = errors.New("not proper time for building new vault")
ErrInvalidHookBlock = errors.New("the vault's hook_block is invalid")
ErrMalformedVault = errors.New("the vault structure is malformed")
)
func NewVault(newAccountID uint64, prevVault *Vault, hookBlock *Block, currentSheet *Sheet) *Vault {
if !hookBlock.IsCheckpoint() {
log.Error(ErrNotCheckpoint)
return nil
}
if !hookBlock.IsSealed() {
log.Error(ErrBlockIsUnsealing)
return nil
}
if !hookBlock.VerifyHash() {
log.Error(ErrInvalidHookBlock)
}
prevVaultHash, err := prevVault.CalculateHash()
if err != nil {
log.Error(err)
}
newAccount := NewRewardAccount(newAccountID, hookBlock.Beneficiary, big.NewInt(0))
v := &Vault{
Height: 0,
NewAccount: newAccount,
Timestamp: time.Now().Unix(),
PrevVaultHash: prevVaultHash,
HookBlockHash: hookBlock.Hash,
Sheet: currentSheet,
}
v.Hash, err = v.CalculateHash()
if err != nil {
log.Error(err)
}
return v
}
func (m *Vault) CalculateHash() ([]byte, error) {
v := m.Copy()
v.Hash = nil
b, err := proto.Marshal(v)
hash := sha3.Sum256(b)
m.Hash = hash[:]
return hash[:], err
}
func GetGenesisVault() *Vault {
var hookGenesisBlock = GetGenesisBlock()
blockHash, err := hookGenesisBlock.CalculateHash()
if err != nil {
log.Error(err)
}
v := &Vault{
Height: 0,
Sheet: nil,
NewAccount: GetGenesisAccount(),
Timestamp: genesisTimestamp,
PrevVaultHash: nil,
HookBlockHash: blockHash,
}
v.Hash, err = v.CalculateHash()
if err != nil {
log.Error(err)
}
return v
}
func (m *Vault) Copy() *Vault {
v := *m
return &v
}