/
beacon.go
145 lines (111 loc) · 3.87 KB
/
beacon.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
145
// Package beacon implements the combined beacon and epochtime
// application.
package beacon
import (
"encoding/binary"
"fmt"
"github.com/tendermint/tendermint/abci/types"
"golang.org/x/crypto/sha3"
beacon "github.com/oasisprotocol/oasis-core/go/beacon/api"
"github.com/oasisprotocol/oasis-core/go/consensus/api/transaction"
"github.com/oasisprotocol/oasis-core/go/consensus/tendermint/api"
beaconState "github.com/oasisprotocol/oasis-core/go/consensus/tendermint/apps/beacon/state"
)
var (
prodEntropyCtx = []byte("EkB-tmnt")
_ api.Application = (*beaconApplication)(nil)
)
type beaconApplication struct {
state api.ApplicationState
backend internalBackend
}
func (app *beaconApplication) Name() string {
return AppName
}
func (app *beaconApplication) ID() uint8 {
return AppID
}
func (app *beaconApplication) Methods() []transaction.MethodName {
return Methods
}
func (app *beaconApplication) Blessed() bool {
return false
}
func (app *beaconApplication) Dependencies() []string {
return nil
}
func (app *beaconApplication) OnRegister(state api.ApplicationState, md api.MessageDispatcher) {
app.state = state
}
func (app *beaconApplication) OnCleanup() {
}
func (app *beaconApplication) BeginBlock(ctx *api.Context, req types.RequestBeginBlock) error {
state := beaconState.NewMutableState(ctx.State())
params, err := state.ConsensusParameters(ctx)
if err != nil {
return fmt.Errorf("beacon: failed to query consensus parameters: %w", err)
}
if err := app.doInitBackend(params); err != nil {
return fmt.Errorf("beacon: failed to (re-)initialize backend: %w", err)
}
return app.backend.OnBeginBlock(ctx, state, params, req)
}
func (app *beaconApplication) ExecuteMessage(ctx *api.Context, kind, msg interface{}) (interface{}, error) {
return nil, fmt.Errorf("beacon: unexpected message")
}
func (app *beaconApplication) ExecuteTx(ctx *api.Context, tx *transaction.Transaction) error {
state := beaconState.NewMutableState(ctx.State())
params, err := state.ConsensusParameters(ctx)
if err != nil {
return fmt.Errorf("beacon: failed to query consensus parameters: %w", err)
}
ctx.SetPriority(AppPriority)
return app.backend.ExecuteTx(ctx, state, params, tx)
}
func (app *beaconApplication) EndBlock(ctx *api.Context, req types.RequestEndBlock) (types.ResponseEndBlock, error) {
return types.ResponseEndBlock{}, nil
}
func (app *beaconApplication) doEmitEpochEvent(ctx *api.Context, epoch beacon.EpochTime) {
ctx.EmitEvent(api.NewEventBuilder(app.Name()).TypedAttribute(&beacon.EpochEvent{Epoch: epoch}))
}
func (app *beaconApplication) scheduleEpochTransitionBlock(
ctx *api.Context,
state *beaconState.MutableState,
nextEpoch beacon.EpochTime,
nextHeight int64,
) error {
ctx.Logger().Info("scheduling epoch transition block",
"epoch", nextEpoch,
"next_height", nextHeight,
"is_check_only", ctx.IsCheckOnly(),
)
if err := state.SetFutureEpoch(ctx, nextEpoch, nextHeight); err != nil {
return fmt.Errorf("beacon: failed to set future epoch from interval: %w", err)
}
return nil
}
func (app *beaconApplication) onNewBeacon(ctx *api.Context, value []byte) error {
state := beaconState.NewMutableState(ctx.State())
if err := state.SetBeacon(ctx, value); err != nil {
ctx.Logger().Error("onNewBeacon: failed to set beacon",
"err", err,
)
return fmt.Errorf("beacon: failed to set beacon: %w", err)
}
ctx.EmitEvent(api.NewEventBuilder(app.Name()).TypedAttribute(&beacon.BeaconEvent{Beacon: value}))
return nil
}
// New constructs a new beacon application instance.
func New() api.Application {
return &beaconApplication{}
}
// GetBeacon derives the actual beacon from the epoch and entropy source.
func GetBeacon(epoch beacon.EpochTime, entropyCtx, entropy []byte) []byte {
var tmp [8]byte
binary.LittleEndian.PutUint64(tmp[:], uint64(epoch))
h := sha3.New256()
_, _ = h.Write(entropyCtx)
_, _ = h.Write(entropy)
_, _ = h.Write(tmp[:])
return h.Sum(nil)
}