-
Notifications
You must be signed in to change notification settings - Fork 111
/
chain_util.go
94 lines (76 loc) · 2.99 KB
/
chain_util.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
package simibc
import (
"time"
channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types"
ibctmtypes "github.com/cosmos/ibc-go/v7/modules/light-clients/07-tendermint"
ibctesting "github.com/cosmos/ibc-go/v7/testing"
sdk "github.com/cosmos/cosmos-sdk/types"
abci "github.com/cometbft/cometbft/abci/types"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
)
// BeginBlock updates the current header and calls the app.BeginBlock method.
// The new block height is the previous block height + 1.
// The new block time is the previous block time + dt.
//
// NOTE: this method may be used independently of the rest of simibc.
func BeginBlock(c *ibctesting.TestChain, dt time.Duration) {
c.CurrentHeader = tmproto.Header{
ChainID: c.ChainID,
Height: c.App.LastBlockHeight() + 1,
AppHash: c.App.LastCommitID().Hash,
Time: c.CurrentHeader.Time.Add(dt),
ValidatorsHash: c.Vals.Hash(),
NextValidatorsHash: c.NextVals.Hash(),
}
_ = c.App.BeginBlock(abci.RequestBeginBlock{Header: c.CurrentHeader})
}
// EndBlock calls app.EndBlock and executes preCommitCallback BEFORE calling app.Commit
// The callback is useful for testing purposes to execute arbitrary code before the
// chain sdk context is cleared in .Commit().
// For example, app.EndBlock may lead to a new state, which you would like to query
// to check that it is correct. However, the sdk context is cleared after .Commit(),
// so you can query the state inside the callback.
//
// NOTE: this method may be used independently of the rest of simibc.
func EndBlock(
c *ibctesting.TestChain,
preCommitCallback func(),
) (*ibctmtypes.Header, []channeltypes.Packet) {
ebRes := c.App.EndBlock(abci.RequestEndBlock{Height: c.CurrentHeader.Height})
/*
It is useful to call arbitrary code after ending the block but before
committing the block because the sdk.Context is cleared after committing.
*/
c.App.Commit()
c.Vals = c.NextVals
c.NextVals = ibctesting.ApplyValSetChanges(c.T, c.Vals, ebRes.ValidatorUpdates)
c.LastHeader = c.CurrentTMClientHeader()
sdkEvts := ABCIToSDKEvents(ebRes.Events)
packets := ParsePacketsFromEvents(sdkEvts)
return c.LastHeader, packets
}
// ParsePacketsFromEvents returns all packets found in events.
func ParsePacketsFromEvents(events []sdk.Event) (packets []channeltypes.Packet) {
for i, ev := range events {
if ev.Type == channeltypes.EventTypeSendPacket {
packet, err := ibctesting.ParsePacketFromEvents(events[i:])
if err != nil {
panic(err)
}
packets = append(packets, packet)
}
}
return
}
// ABCIToSDKEvents converts a list of ABCI events to Cosmos SDK events.
func ABCIToSDKEvents(abciEvents []abci.Event) sdk.Events {
var events sdk.Events
for _, evt := range abciEvents {
var attributes []sdk.Attribute
for _, attr := range evt.GetAttributes() {
attributes = append(attributes, sdk.NewAttribute(attr.Key, attr.Value))
}
events = events.AppendEvent(sdk.NewEvent(evt.GetType(), attributes...))
}
return events
}