-
Notifications
You must be signed in to change notification settings - Fork 2
/
abci.go
64 lines (51 loc) · 1.72 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
package testutil
import (
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
abci "github.com/tendermint/tendermint/abci/types"
tmtypes "github.com/tendermint/tendermint/types"
"github.com/brcchain/brcchain/app"
)
// Commit commits a block at a given time. Reminder: At the end of each
// Tendermint Consensus round the following methods are run
// 1. BeginBlock
// 2. DeliverTx
// 3. EndBlock
// 4. Commit
func Commit(ctx sdk.Context, app *app.EthermintApp, t time.Duration, vs *tmtypes.ValidatorSet) (sdk.Context, error) {
header := ctx.BlockHeader()
if vs != nil {
res := app.EndBlock(abci.RequestEndBlock{Height: header.Height})
nextVals, err := applyValSetChanges(vs, res.ValidatorUpdates)
if err != nil {
return ctx, err
}
header.ValidatorsHash = vs.Hash()
header.NextValidatorsHash = nextVals.Hash()
} else {
app.EndBlocker(ctx, abci.RequestEndBlock{Height: header.Height})
}
_ = app.Commit()
header.Height++
header.Time = header.Time.Add(t)
header.AppHash = app.LastCommitID().Hash
app.BeginBlock(abci.RequestBeginBlock{
Header: header,
})
return ctx.WithBlockHeader(header), nil
}
// applyValSetChanges takes in tmtypes.ValidatorSet and []abci.ValidatorUpdate and will return a new tmtypes.ValidatorSet which has the
// provided validator updates applied to the provided validator set.
func applyValSetChanges(valSet *tmtypes.ValidatorSet, valUpdates []abci.ValidatorUpdate) (*tmtypes.ValidatorSet, error) {
updates, err := tmtypes.PB2TM.ValidatorUpdates(valUpdates)
if err != nil {
return nil, err
}
// must copy since validator set will mutate with UpdateWithChangeSet
newVals := valSet.Copy()
err = newVals.UpdateWithChangeSet(updates)
if err != nil {
return nil, err
}
return newVals, nil
}