-
Notifications
You must be signed in to change notification settings - Fork 651
/
chain_state.go
48 lines (41 loc) · 1.33 KB
/
chain_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
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package avm
import (
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow/choices"
"github.com/ava-labs/avalanchego/vms/avm/states"
"github.com/ava-labs/avalanchego/vms/avm/txs"
)
var _ states.Chain = (*chainState)(nil)
// chainState wraps the disk state and filters non-accepted transactions from
// being returned in GetTx.
type chainState struct {
states.State
}
func (s *chainState) GetTx(txID ids.ID) (*txs.Tx, error) {
tx, err := s.State.GetTx(txID)
if err != nil {
return nil, err
}
// Before the linearization, transactions were persisted before they were
// marked as Accepted. However, this function aims to only return accepted
// transactions.
status, err := s.State.GetStatus(txID)
if err == database.ErrNotFound {
// If the status wasn't persisted, then the transaction was written
// after the linearization, and is accepted.
return tx, nil
}
if err != nil {
return nil, err
}
// If the status was persisted, then the transaction was written before the
// linearization. If it wasn't marked as accepted, then we treat it as if it
// doesn't exist.
if status != choices.Accepted {
return nil, database.ErrNotFound
}
return tx, nil
}