-
Notifications
You must be signed in to change notification settings - Fork 669
/
prefixes.go
65 lines (56 loc) · 2.04 KB
/
prefixes.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
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package atomic
import (
"bytes"
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/database/prefixdb"
"github.com/ava-labs/avalanchego/ids"
)
var (
inboundSmallerValuePrefix = []byte{0}
inboundSmallerIndexPrefix = []byte{1}
inboundLargerValuePrefix = []byte{2}
inboundLargerIndexPrefix = []byte{3}
// note that inbound and outbound have their smaller and larger values
// swapped
// inbound specifies the prefixes to use for inbound shared memory
// ie. reading and deleting a message received from another chain.
inbound = prefixes{
smallerValuePrefix: inboundSmallerValuePrefix,
smallerIndexPrefix: inboundSmallerIndexPrefix,
largerValuePrefix: inboundLargerValuePrefix,
largerIndexPrefix: inboundLargerIndexPrefix,
}
// outbound specifies the prefixes to use for outbound shared memory
// ie. writing a message to another chain.
outbound = prefixes{
smallerValuePrefix: inboundLargerValuePrefix,
smallerIndexPrefix: inboundLargerIndexPrefix,
largerValuePrefix: inboundSmallerValuePrefix,
largerIndexPrefix: inboundSmallerIndexPrefix,
}
)
type prefixes struct {
smallerValuePrefix []byte
smallerIndexPrefix []byte
largerValuePrefix []byte
largerIndexPrefix []byte
}
func (p *prefixes) getValueDB(myChainID, peerChainID ids.ID, db database.Database) database.Database {
if bytes.Compare(myChainID[:], peerChainID[:]) == -1 {
return prefixdb.New(p.smallerValuePrefix, db)
}
return prefixdb.New(p.largerValuePrefix, db)
}
func (p *prefixes) getValueAndIndexDB(myChainID, peerChainID ids.ID, db database.Database) (database.Database, database.Database) {
var valueDB, indexDB database.Database
if bytes.Compare(myChainID[:], peerChainID[:]) == -1 {
valueDB = prefixdb.New(p.smallerValuePrefix, db)
indexDB = prefixdb.New(p.smallerIndexPrefix, db)
} else {
valueDB = prefixdb.New(p.largerValuePrefix, db)
indexDB = prefixdb.New(p.largerIndexPrefix, db)
}
return valueDB, indexDB
}