forked from attestantio/dirk
/
signbeaconproposal.go
139 lines (122 loc) · 4.08 KB
/
signbeaconproposal.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
// Copyright © 2020 Attestant Limited.
// 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 standard
import (
"bytes"
"context"
"encoding/binary"
"encoding/gob"
"fmt"
"github.com/bliiitz/dirk/rules"
"github.com/opentracing/opentracing-go"
"github.com/pkg/errors"
e2types "github.com/wealdtech/go-eth2-types/v2"
)
type signBeaconProposalState struct {
Slot int64
}
// Encode encodes the proposal state.
func (s *signBeaconProposalState) Encode() []byte {
data := make([]byte, 1+8)
// Version.
data[0] = 0x01
if s != nil {
// Slot.
binary.LittleEndian.PutUint64(data[1:9], uint64(s.Slot))
}
return data
}
// Decode decodes the proposal state.
func (s *signBeaconProposalState) Decode(data []byte) error {
var err error
if len(data) == 0 {
return errors.New("no data supplied")
}
switch data[0] {
case 0x01:
if len(data) != 9 {
return fmt.Errorf("invalid version 1 data size %d", len(data))
}
s.Slot = int64(binary.LittleEndian.Uint64(data[1:9]))
default:
err = gob.NewDecoder(bytes.NewBuffer(data)).Decode(s)
}
return err
}
// OnSignBeaconProposal is called when a request to sign a beacon block proposal needs to be approved.
func (s *Service) OnSignBeaconProposal(ctx context.Context, metadata *rules.ReqMetadata, req *rules.SignBeaconProposalData) rules.Result {
span, _ := opentracing.StartSpanFromContext(ctx, "rules.OnSignBeaconProposal")
defer span.Finish()
log := log.With().Str("client", metadata.Client).Str("account", metadata.Account).Str("rule", "sign beacon proposal").Logger()
// The request must have the appropriate domain.
if !bytes.Equal(req.Domain[0:4], e2types.DomainBeaconProposer[:]) {
log.Warn().Msg("Not approving non-beacon proposal due to incorrect domain")
return rules.DENIED
}
// Fetch state from previous signings.
state, err := s.fetchSignBeaconProposalState(ctx, metadata.PubKey)
if err != nil {
log.Error().Err(err).Msg("Failed to fetch state for beacon proposal")
return rules.FAILED
}
slot := req.Slot
if state.Slot != -1 {
// The request slot must be greater than the previous request slot.
if int64(slot) <= state.Slot {
log.Warn().
Int64("previousSlot", state.Slot).
Uint64("slot", slot).
Msg("Request slot equal to or lower than previous signed slot")
return rules.DENIED
}
}
state.Slot = int64(slot)
if err = s.storeSignBeaconProposalState(ctx, metadata.PubKey, state); err != nil {
log.Error().Err(err).Msg("Failed to store state for beacon proposal")
return rules.FAILED
}
return rules.APPROVED
}
func (s *Service) fetchSignBeaconProposalState(ctx context.Context, pubKey []byte) (*signBeaconProposalState, error) {
state := &signBeaconProposalState{}
key := make([]byte, len(pubKey)+len(actionSignBeaconProposal))
copy(key, pubKey)
copy(key[len(pubKey):], actionSignBeaconProposal)
data, err := s.store.Fetch(ctx, key)
if err != nil {
if err.Error() == "not found" {
// No value; set it to -1.
state.Slot = -1
} else {
return nil, err
}
} else {
err = state.Decode(data)
if err != nil {
return nil, errors.Wrap(err, "failed to decode state")
}
}
log.Trace().Int64("slot", state.Slot).Msg("Returning proposal state from store")
return state, nil
}
func (s *Service) storeSignBeaconProposalState(ctx context.Context, pubKey []byte, state *signBeaconProposalState) error {
key := make([]byte, len(pubKey)+len(actionSignBeaconProposal))
copy(key, pubKey)
copy(key[len(pubKey):], actionSignBeaconProposal)
err := s.store.Store(ctx, key, state.Encode())
if err != nil {
return err
}
log.Trace().Int64("slot", state.Slot).Msg("Stored proposal state to store")
return nil
}