-
Notifications
You must be signed in to change notification settings - Fork 178
/
Copy pathabci.go
92 lines (73 loc) · 2.32 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
package clock
import (
"time"
"github.com/cometbft/cometbft/libs/log"
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
helpers "github.com/CosmosContracts/juno/v27/app/helpers"
"github.com/CosmosContracts/juno/v27/x/clock/keeper"
"github.com/CosmosContracts/juno/v27/x/clock/types"
)
var endBlockSudoMessage = []byte(types.EndBlockSudoMessage)
// EndBlocker executes on contracts at the end of the block.
func EndBlocker(ctx sdk.Context, k keeper.Keeper) {
defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyEndBlocker)
logger := k.Logger(ctx)
p := k.GetParams(ctx)
// Get all contracts
contracts, err := k.GetAllContracts(ctx)
if err != nil {
logger.Error("Failed to get contracts", "error", err)
return
}
// Track errors
errorExecs := make([]string, len(contracts))
errorExists := false
// Execute all contracts that are not jailed
for idx, contract := range contracts {
// Skip jailed contracts
if contract.IsJailed {
continue
}
// Get sdk.AccAddress from contract address
contractAddr := sdk.MustAccAddressFromBech32(contract.ContractAddress)
if handleError(ctx, k, logger, errorExecs, &errorExists, err, idx, contract.ContractAddress) {
continue
}
// Create context with gas limit
childCtx := ctx.WithGasMeter(sdk.NewGasMeter(p.ContractGasLimit))
// Execute contract
helpers.ExecuteContract(k.GetContractKeeper(), childCtx, contractAddr, endBlockSudoMessage, &err)
if handleError(ctx, k, logger, errorExecs, &errorExists, err, idx, contract.ContractAddress) {
continue
}
}
// Log errors if present
if errorExists {
logger.Error("Failed to execute contracts", "contracts", errorExecs)
}
}
// Function to handle contract execution errors. Returns true if error is present, false otherwise.
func handleError(
ctx sdk.Context,
k keeper.Keeper,
logger log.Logger,
errorExecs []string,
errorExists *bool,
err error,
idx int,
contractAddress string,
) bool {
// Check if error is present
if err != nil {
// Flag error
*errorExists = true
errorExecs[idx] = contractAddress
// Attempt to jail contract, log error if present
err := k.SetJailStatus(ctx, contractAddress, true)
if err != nil {
logger.Error("Failed to jail contract", "contract", contractAddress, "error", err)
}
}
return err != nil
}