/
abci.go
110 lines (86 loc) · 3.14 KB
/
abci.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
package swingset
import (
// "os"
"context"
"fmt"
"time"
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/Agoric/agoric-sdk/golang/cosmos/vm"
"github.com/Agoric/agoric-sdk/golang/cosmos/x/swingset/types"
)
type beginBlockAction struct {
*vm.ActionHeader `actionType:"BEGIN_BLOCK"`
ChainID string `json:"chainID"`
Params types.Params `json:"params"`
}
type endBlockAction struct {
*vm.ActionHeader `actionType:"END_BLOCK"`
}
type commitBlockAction struct {
*vm.ActionHeader `actionType:"COMMIT_BLOCK"`
}
type afterCommitBlockAction struct {
*vm.ActionHeader `actionType:"AFTER_COMMIT_BLOCK"`
}
func BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock, keeper Keeper) error {
defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyBeginBlocker)
action := beginBlockAction{
ChainID: ctx.ChainID(),
Params: keeper.GetParams(ctx),
}
_, err := keeper.BlockingSend(ctx, action)
// fmt.Fprintf(os.Stderr, "BEGIN_BLOCK Returned from SwingSet: %s, %v\n", out, err)
if err != nil {
panic(err)
}
err = keeper.UpdateQueueAllowed(ctx)
return err
}
var endBlockHeight int64
var endBlockTime int64
func EndBlock(ctx sdk.Context, req abci.RequestEndBlock, keeper Keeper) ([]abci.ValidatorUpdate, error) {
defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyEndBlocker)
action := endBlockAction{}
_, err := keeper.BlockingSend(ctx, action)
// fmt.Fprintf(os.Stderr, "END_BLOCK Returned from SwingSet: %s, %v\n", out, err)
if err != nil {
// NOTE: A failed END_BLOCK means that the SwingSet state is inconsistent.
// Panic here, in the hopes that a replay from scratch will fix the problem.
panic(err)
}
// Save our EndBlock status.
endBlockHeight = ctx.BlockHeight()
endBlockTime = ctx.BlockTime().Unix()
return []abci.ValidatorUpdate{}, nil
}
func getEndBlockContext() sdk.Context {
return sdk.Context{}.
WithContext(context.Background()).
WithBlockHeight(endBlockHeight).
WithBlockTime(time.Unix(endBlockTime, 0))
}
func CommitBlock(keeper Keeper) error {
defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), "commit_blocker")
action := commitBlockAction{}
_, err := keeper.BlockingSend(getEndBlockContext(), action)
// fmt.Fprintf(os.Stderr, "COMMIT_BLOCK Returned from SwingSet: %s, %v\n", out, err)
if err != nil {
// NOTE: A failed COMMIT_BLOCK means that the SwingSet state is inconsistent.
// Panic here, in the hopes that a replay from scratch will fix the problem.
panic(err)
}
return err
}
func AfterCommitBlock(keeper Keeper) error {
// defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), "commit_blocker")
action := afterCommitBlockAction{}
_, err := keeper.BlockingSend(getEndBlockContext(), action)
// fmt.Fprintf(os.Stderr, "AFTER_COMMIT_BLOCK Returned from SwingSet: %s, %v\n", out, err)
if err != nil {
// Panic here, in the hopes that a replay from scratch will fix the problem.
panic(fmt.Errorf("AFTER_COMMIT_BLOCK failed: %s. Swingset is in an irrecoverable inconsistent state", err))
}
return err
}