-
Notifications
You must be signed in to change notification settings - Fork 672
/
diff.go
172 lines (140 loc) · 3.86 KB
/
diff.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package states
import (
"errors"
"fmt"
"time"
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/vms/avm/blocks"
"github.com/ava-labs/avalanchego/vms/avm/txs"
"github.com/ava-labs/avalanchego/vms/components/avax"
)
var (
_ Diff = (*diff)(nil)
ErrMissingParentState = errors.New("missing parent state")
)
type Diff interface {
Chain
Apply(Chain)
}
type diff struct {
parentID ids.ID
stateVersions Versions
// map of modified UTXOID -> *UTXO if the UTXO is nil, it has been removed
modifiedUTXOs map[ids.ID]*avax.UTXO
addedTxs map[ids.ID]*txs.Tx // map of txID -> tx
addedBlockIDs map[uint64]ids.ID // map of height -> blockID
addedBlocks map[ids.ID]blocks.Block // map of blockID -> block
lastAccepted ids.ID
timestamp time.Time
}
func NewDiff(
parentID ids.ID,
stateVersions Versions,
) (Diff, error) {
parentState, ok := stateVersions.GetState(parentID)
if !ok {
return nil, fmt.Errorf("%w: %s", ErrMissingParentState, parentID)
}
return &diff{
parentID: parentID,
stateVersions: stateVersions,
modifiedUTXOs: make(map[ids.ID]*avax.UTXO),
addedTxs: make(map[ids.ID]*txs.Tx),
addedBlockIDs: make(map[uint64]ids.ID),
addedBlocks: make(map[ids.ID]blocks.Block),
lastAccepted: parentState.GetLastAccepted(),
timestamp: parentState.GetTimestamp(),
}, nil
}
func (d *diff) GetUTXO(utxoID ids.ID) (*avax.UTXO, error) {
if utxo, modified := d.modifiedUTXOs[utxoID]; modified {
if utxo == nil {
return nil, database.ErrNotFound
}
return utxo, nil
}
parentState, ok := d.stateVersions.GetState(d.parentID)
if !ok {
return nil, fmt.Errorf("%w: %s", ErrMissingParentState, d.parentID)
}
return parentState.GetUTXO(utxoID)
}
func (d *diff) GetUTXOFromID(utxoID *avax.UTXOID) (*avax.UTXO, error) {
return d.GetUTXO(utxoID.InputID())
}
func (d *diff) AddUTXO(utxo *avax.UTXO) {
d.modifiedUTXOs[utxo.InputID()] = utxo
}
func (d *diff) DeleteUTXO(utxoID ids.ID) {
d.modifiedUTXOs[utxoID] = nil
}
func (d *diff) GetTx(txID ids.ID) (*txs.Tx, error) {
if tx, exists := d.addedTxs[txID]; exists {
return tx, nil
}
parentState, ok := d.stateVersions.GetState(d.parentID)
if !ok {
return nil, fmt.Errorf("%w: %s", ErrMissingParentState, d.parentID)
}
return parentState.GetTx(txID)
}
func (d *diff) AddTx(tx *txs.Tx) {
d.addedTxs[tx.ID()] = tx
}
func (d *diff) GetBlockID(height uint64) (ids.ID, error) {
if blkID, exists := d.addedBlockIDs[height]; exists {
return blkID, nil
}
parentState, ok := d.stateVersions.GetState(d.parentID)
if !ok {
return ids.Empty, fmt.Errorf("%w: %s", ErrMissingParentState, d.parentID)
}
return parentState.GetBlockID(height)
}
func (d *diff) GetBlock(blkID ids.ID) (blocks.Block, error) {
if blk, exists := d.addedBlocks[blkID]; exists {
return blk, nil
}
parentState, ok := d.stateVersions.GetState(d.parentID)
if !ok {
return nil, fmt.Errorf("%w: %s", ErrMissingParentState, d.parentID)
}
return parentState.GetBlock(blkID)
}
func (d *diff) AddBlock(blk blocks.Block) {
blkID := blk.ID()
d.addedBlockIDs[blk.Height()] = blkID
d.addedBlocks[blkID] = blk
}
func (d *diff) GetLastAccepted() ids.ID {
return d.lastAccepted
}
func (d *diff) SetLastAccepted(lastAccepted ids.ID) {
d.lastAccepted = lastAccepted
}
func (d *diff) GetTimestamp() time.Time {
return d.timestamp
}
func (d *diff) SetTimestamp(t time.Time) {
d.timestamp = t
}
func (d *diff) Apply(state Chain) {
for utxoID, utxo := range d.modifiedUTXOs {
if utxo != nil {
state.AddUTXO(utxo)
} else {
state.DeleteUTXO(utxoID)
}
}
for _, tx := range d.addedTxs {
state.AddTx(tx)
}
for _, blk := range d.addedBlocks {
state.AddBlock(blk)
}
state.SetLastAccepted(d.lastAccepted)
state.SetTimestamp(d.timestamp)
}