-
Notifications
You must be signed in to change notification settings - Fork 107
/
block.go
63 lines (52 loc) · 1.72 KB
/
block.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
package api
import (
"time"
"github.com/cometbft/cometbft/abci/types"
"github.com/oasisprotocol/oasis-core/go/consensus/api/events"
"github.com/oasisprotocol/oasis-core/go/consensus/api/transaction"
)
// BlockInfo contains information about a block which is always present in block context.
type BlockInfo struct {
Time time.Time
ProposerAddress []byte
LastCommitInfo types.CommitInfo
ValidatorMisbehavior []types.Misbehavior
GasAccountant GasAccountant
SystemTransactions []*transaction.Transaction
ProvableEvents []events.Provable
}
// BlockContextKey is an interface for a block context key.
type BlockContextKey interface {
// NewDefault returns a new default value for the given key.
NewDefault() interface{}
}
// BlockContext can be used to store arbitrary key/value pairs for state that
// is needed while processing a block.
//
// When a block is committed, this context is automatically reset.
type BlockContext struct {
BlockInfo
storage map[BlockContextKey]interface{}
}
// Get returns the value stored under the given key (if any). If no value
// currently exists, the NewDefault method is called on the key to produce a
// default value and that value is stored.
func (bc *BlockContext) Get(key BlockContextKey) interface{} {
v, ok := bc.storage[key]
if !ok {
v = key.NewDefault()
bc.storage[key] = v
}
return v
}
// Set overwrites the value stored under the given key.
func (bc *BlockContext) Set(key BlockContextKey, value interface{}) {
bc.storage[key] = value
}
// NewBlockContext creates a new block context.
func NewBlockContext(blockInfo BlockInfo) *BlockContext {
return &BlockContext{
BlockInfo: blockInfo,
storage: make(map[BlockContextKey]interface{}),
}
}