This repository has been archived by the owner on Feb 14, 2023. It is now read-only.
/
data.go
144 lines (121 loc) · 4.18 KB
/
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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright 2016-2018 Stratumn SAS. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package tmpop
import (
"context"
"encoding/json"
"fmt"
"github.com/stratumn/go-chainscript"
"github.com/stratumn/go-core/monitoring"
"github.com/stratumn/go-core/monitoring/errorcode"
"github.com/stratumn/go-core/store"
"github.com/stratumn/go-core/types"
abci "github.com/tendermint/abci/types"
wire "github.com/tendermint/go-wire"
)
// GetCurrentHeader returns the current block header
func (t *TMPop) GetCurrentHeader() *abci.Header {
return t.currentHeader
}
// ReadLastBlock returns the last block committed by TMPop
func ReadLastBlock(ctx context.Context, kv store.KeyValueReader) (*LastBlock, error) {
lBytes, err := kv.GetValue(ctx, tmpopLastBlockKey)
if err != nil {
return nil, err
}
var l LastBlock
if lBytes == nil {
return &l, nil
}
err = wire.ReadBinaryBytes(lBytes, &l)
if err != nil {
return nil, types.WrapError(err, errorcode.InvalidArgument, Name, "could not read last block")
}
return &l, nil
}
// saveLastBlock saves the last block committed by TMPop
func saveLastBlock(ctx context.Context, a store.KeyValueWriter, l LastBlock) {
err := a.SetValue(ctx, tmpopLastBlockKey, wire.BinaryBytes(l))
if err != nil {
monitoring.LogEntry().Warn(err)
}
}
func getValidatorHashKey(height int64) []byte {
key := fmt.Sprintf("tmpop:validator:%d", height)
return []byte(key)
}
// saveValidatorHash saves the hash of the validator used for the current block
func (t *TMPop) saveValidatorHash(ctx context.Context) error {
if t.state.validator != nil {
key := getValidatorHashKey(t.currentHeader.Height)
h, err := t.state.validator.Hash()
if err != nil {
return err
}
if err := t.kvDB.SetValue(ctx, key, h[:]); err != nil {
return err
}
}
return nil
}
// getValidatorHash gets the hash of the validator used for a block at a specific height
func (t *TMPop) getValidatorHash(ctx context.Context, height int64) ([]byte, error) {
key := getValidatorHashKey(height)
value, err := t.kvDB.GetValue(ctx, key)
if err != nil || value == nil {
return nil, err
}
return value, nil
}
func getCommitLinkHashesKey(height int64) []byte {
key := fmt.Sprintf("tmpop:linkhashes:%d", height)
return []byte(key)
}
// saveCommitLinkHashes saves the link hashes of valid links created in the
// current block. Since Tendermint can include invalid transactions and
// doesn't provide yet an easy way to know which transactions are invalid in
// a block, this is useful to generate valid evidence and ignore invalid
// transactions.
func (t *TMPop) saveCommitLinkHashes(ctx context.Context, links []*chainscript.Link) error {
if len(links) > 0 {
key := getCommitLinkHashesKey(t.currentHeader.Height)
var linkHashes []chainscript.LinkHash
for _, link := range links {
linkHash, _ := link.Hash()
linkHashes = append(linkHashes, linkHash)
}
value, err := json.Marshal(linkHashes)
if err != nil {
return types.WrapError(err, errorcode.InvalidArgument, Name, "json.Marshal")
}
if err := t.kvDB.SetValue(ctx, key, value); err != nil {
return err
}
}
return nil
}
// getCommitLinkHashes gets the link hashes included in a block at a specific height.
// This is useful to ignore invalid links included in that block.
func (t *TMPop) getCommitLinkHashes(ctx context.Context, height int64) ([]chainscript.LinkHash, error) {
key := getCommitLinkHashesKey(height)
value, err := t.kvDB.GetValue(ctx, key)
if err != nil || value == nil {
return nil, err
}
var linkHashes []chainscript.LinkHash
if err := json.Unmarshal(value, &linkHashes); err != nil {
return nil, types.WrapError(err, errorcode.InvalidArgument, Name, "json.Unmarshal")
}
return linkHashes, nil
}