-
Notifications
You must be signed in to change notification settings - Fork 669
/
state.go
97 lines (79 loc) · 2.5 KB
/
state.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
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package avm
import (
"github.com/prometheus/client_golang/prometheus"
"github.com/ava-labs/avalanchego/cache"
"github.com/ava-labs/avalanchego/codec"
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/database/prefixdb"
"github.com/ava-labs/avalanchego/vms/components/avax"
)
const (
txDeduplicatorSize = 8192
)
var (
utxoStatePrefix = []byte("utxo")
statusStatePrefix = []byte("status")
singletonStatePrefix = []byte("singleton")
txStatePrefix = []byte("tx")
)
// State persistently maintains a set of UTXOs, transaction, statuses, and
// singletons.
type State interface {
avax.UTXOState
avax.StatusState
avax.SingletonState
TxState
DeduplicateTx(tx *UniqueTx) *UniqueTx
}
type state struct {
avax.UTXOState
avax.StatusState
avax.SingletonState
TxState
uniqueTxs cache.Deduplicator
}
func NewState(db database.Database, genesisCodec, codec codec.Manager) State {
utxoDB := prefixdb.New(utxoStatePrefix, db)
statusDB := prefixdb.New(statusStatePrefix, db)
singletonDB := prefixdb.New(singletonStatePrefix, db)
txDB := prefixdb.New(txStatePrefix, db)
return &state{
UTXOState: avax.NewUTXOState(utxoDB, codec),
StatusState: avax.NewStatusState(statusDB),
SingletonState: avax.NewSingletonState(singletonDB),
TxState: NewTxState(txDB, genesisCodec),
uniqueTxs: &cache.EvictableLRU{
Size: txDeduplicatorSize,
},
}
}
func NewMeteredState(db database.Database, genesisCodec, codec codec.Manager, namespace string, metrics prometheus.Registerer) (State, error) {
utxoDB := prefixdb.New(utxoStatePrefix, db)
statusDB := prefixdb.New(statusStatePrefix, db)
singletonDB := prefixdb.New(singletonStatePrefix, db)
txDB := prefixdb.New(txStatePrefix, db)
utxoState, err := avax.NewMeteredUTXOState(utxoDB, codec, namespace, metrics)
if err != nil {
return nil, err
}
statusState, err := avax.NewMeteredStatusState(statusDB, namespace, metrics)
if err != nil {
return nil, err
}
txState, err := NewMeteredTxState(txDB, genesisCodec, namespace, metrics)
return &state{
UTXOState: utxoState,
StatusState: statusState,
SingletonState: avax.NewSingletonState(singletonDB),
TxState: txState,
uniqueTxs: &cache.EvictableLRU{
Size: txDeduplicatorSize,
},
}, err
}
// UniqueTx de-duplicates the transaction.
func (s *state) DeduplicateTx(tx *UniqueTx) *UniqueTx {
return s.uniqueTxs.Deduplicate(tx).(*UniqueTx)
}