Skip to content

Commit fd52407

Browse files
author
ScottyPoi
committed
HeadStateNetwork: organize types file and make naming consistent
1 parent 036b574 commit fd52407

File tree

1 file changed

+84
-76
lines changed
  • packages/portalnetwork/src/networks/headState

1 file changed

+84
-76
lines changed

packages/portalnetwork/src/networks/headState/types.ts

Lines changed: 84 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -2,108 +2,115 @@ import { ByteVectorType, ContainerType, ListCompositeType } from '@chainsafe/ssz
22
import { Bytes32Type } from '../types.js'
33
import { Nibbles, TrieNode, TrieProof, type AddressHash, type TNibbles, type TTrieProof } from '../state/types.js'
44

5+
// Network content type definitions
56
export enum HeadStateNetworkContentType {
67
AccountTrieDiff = 0x30,
78
AccountTrieNode = 0x31,
89
ContractTrieDiff = 0x32,
910
ContractTrieNode = 0x33,
1011
}
1112

13+
// Common type aliases
1214
type BlockHash = Uint8Array
15+
type Path = Uint8Array
1316

14-
export type AccountTrieNode = {
15-
path: TNibbles
16-
blockHash: BlockHash
17-
}
18-
17+
// Trie node and diff base types
1918
export const TrieNodeListType = new ListCompositeType(TrieNode, 65536)
2019
export const TrieDiffType = new ContainerType({
2120
before: TrieNodeListType,
2221
after: TrieNodeListType,
2322
})
2423

25-
export const AccountTrieNodeType = new ContainerType({
26-
path: Nibbles,
27-
blockHash: Bytes32Type,
28-
})
24+
// Account Trie types
25+
export type AccountTrieNode = {
26+
path: TNibbles
27+
blockHash: BlockHash
28+
}
2929

30-
export type TAccountTrieNodeValue = {
30+
export type AccountTrieNodeValue = {
3131
proof: TTrieProof
3232
}
3333

34-
export const AccountTrieNodeValue = new ContainerType({
35-
proof: TrieProof,
36-
})
37-
38-
type Path = Uint8Array
39-
const PathType = new ByteVectorType(1)
40-
4134
export type AccountTrieDiff = {
4235
path: Path
4336
blockHash: BlockHash
4437
}
4538

46-
const AccountTrieDiffType = new ContainerType({
47-
path: PathType,
48-
blockHash: Bytes32Type,
49-
})
50-
51-
export type TAccountTrieDiffValue = {
39+
export type AccountTrieDiffValue = {
5240
subtrieDiff: {
5341
before: TTrieProof
5442
after: TTrieProof
5543
}
5644
}
5745

58-
const AccountTrieDiffValue = new ContainerType({
59-
subtrieDiff: TrieDiffType,
60-
})
61-
46+
// Contract Trie types
6247
export type ContractTrieNode = {
6348
path: TNibbles
6449
addressHash: AddressHash
6550
blockHash: BlockHash
6651
}
6752

68-
export const ContractTrieNodeType = new ContainerType({
69-
path: Nibbles,
70-
addressHash: Bytes32Type,
71-
blockHash: Bytes32Type,
72-
})
73-
74-
export type TContractTrieNodeValue = {
53+
export type ContractTrieNodeValue = {
7554
storageProof: TTrieProof
7655
accountProof: TTrieProof
7756
}
7857

79-
export const ContractTrieNodeValue = new ContainerType({
80-
storageProof: TrieProof,
81-
accountProof: TrieProof,
82-
})
83-
8458
export type ContractTrieDiff = {
8559
path: Path
8660
blockHash: BlockHash
8761
}
8862

89-
export const ContractTrieDiffType = new ContainerType({
90-
path: PathType,
91-
blockHash: Bytes32Type,
92-
})
93-
94-
export type TContractTrieDiffValue = {
63+
export type ContractTrieDiffValue = {
9564
subtrieDiff: {
9665
before: TTrieProof
9766
after: TTrieProof
9867
}
9968
accountProof: TTrieProof
10069
}
10170

102-
const ContractTrieDiffValue = new ContainerType({
71+
// SSZ serialization types
72+
const PathType = new ByteVectorType(1)
73+
74+
export const AccountTrieNodeSszType = new ContainerType({
75+
path: Nibbles,
76+
blockHash: Bytes32Type,
77+
})
78+
79+
export const AccountTrieNodeValueSszType = new ContainerType({
80+
proof: TrieProof,
81+
})
82+
83+
export const AccountTrieDiffSszType = new ContainerType({
84+
path: PathType,
85+
blockHash: Bytes32Type,
86+
})
87+
88+
export const AccountTrieDiffValueSszType = new ContainerType({
89+
subtrieDiff: TrieDiffType,
90+
})
91+
92+
export const ContractTrieNodeSszType = new ContainerType({
93+
path: Nibbles,
94+
addressHash: Bytes32Type,
95+
blockHash: Bytes32Type,
96+
})
97+
98+
export const ContractTrieNodeValueSszType = new ContainerType({
99+
storageProof: TrieProof,
100+
accountProof: TrieProof,
101+
})
102+
103+
export const ContractTrieDiffSszType = new ContainerType({
104+
path: PathType,
105+
blockHash: Bytes32Type,
106+
})
107+
108+
export const ContractTrieDiffValueSszType = new ContainerType({
103109
subtrieDiff: TrieDiffType,
104110
accountProof: TrieProof,
105111
})
106112

113+
// Content type mapping
107114
export type HeadStateNetworkContent<T extends HeadStateNetworkContentType> =
108115
T extends HeadStateNetworkContentType.AccountTrieDiff
109116
? AccountTrieDiff
@@ -115,49 +122,50 @@ export type HeadStateNetworkContent<T extends HeadStateNetworkContentType> =
115122
? ContractTrieNode
116123
: never
117124

118-
const sszSerialize: {
125+
export type HeadStateNetworkContentValue<T extends HeadStateNetworkContentType> =
126+
T extends HeadStateNetworkContentType.AccountTrieDiff
127+
? AccountTrieDiffValue
128+
: T extends HeadStateNetworkContentType.AccountTrieNode
129+
? AccountTrieNodeValue
130+
: T extends HeadStateNetworkContentType.ContractTrieDiff
131+
? ContractTrieDiffValue
132+
: T extends HeadStateNetworkContentType.ContractTrieNode
133+
? ContractTrieNodeValue
134+
: never
135+
136+
// Serialization helpers
137+
const contentKeySerializers: {
119138
[T in HeadStateNetworkContentType]: {
120139
serialize: (content: HeadStateNetworkContent<T>) => Uint8Array
121140
}
122141
} = {
123-
[HeadStateNetworkContentType.AccountTrieDiff]: AccountTrieDiffType,
124-
[HeadStateNetworkContentType.AccountTrieNode]: AccountTrieNodeType,
125-
[HeadStateNetworkContentType.ContractTrieDiff]: ContractTrieDiffType,
126-
[HeadStateNetworkContentType.ContractTrieNode]: ContractTrieNodeType,
142+
[HeadStateNetworkContentType.AccountTrieDiff]: AccountTrieDiffSszType,
143+
[HeadStateNetworkContentType.AccountTrieNode]: AccountTrieNodeSszType,
144+
[HeadStateNetworkContentType.ContractTrieDiff]: ContractTrieDiffSszType,
145+
[HeadStateNetworkContentType.ContractTrieNode]: ContractTrieNodeSszType,
146+
}
147+
148+
const contentValueSerializers: {
149+
[T in HeadStateNetworkContentType]: {
150+
serialize: (content: HeadStateNetworkContentValue<T>) => Uint8Array
151+
}
152+
} = {
153+
[HeadStateNetworkContentType.AccountTrieDiff]: AccountTrieDiffValueSszType,
154+
[HeadStateNetworkContentType.AccountTrieNode]: AccountTrieNodeValueSszType,
155+
[HeadStateNetworkContentType.ContractTrieDiff]: ContractTrieDiffValueSszType,
156+
[HeadStateNetworkContentType.ContractTrieNode]: ContractTrieNodeValueSszType,
127157
}
128158

129159
export function getContentKey<T extends HeadStateNetworkContentType>(
130160
type: T,
131161
content: HeadStateNetworkContent<T>,
132162
) {
133-
return Uint8Array.from([type, ...sszSerialize[type].serialize(content)])
134-
}
135-
136-
type TContentValue<T extends HeadStateNetworkContentType> =
137-
T extends HeadStateNetworkContentType.AccountTrieDiff
138-
? TAccountTrieDiffValue
139-
: T extends HeadStateNetworkContentType.AccountTrieNode
140-
? TAccountTrieNodeValue
141-
: T extends HeadStateNetworkContentType.ContractTrieDiff
142-
? TContractTrieDiffValue
143-
: T extends HeadStateNetworkContentType.ContractTrieNode
144-
? TContractTrieNodeValue
145-
: never
146-
147-
export const serializeContentValue: {
148-
[T in HeadStateNetworkContentType]: {
149-
serialize: (content: TContentValue<T>) => Uint8Array
150-
}
151-
} = {
152-
[HeadStateNetworkContentType.AccountTrieDiff]: AccountTrieDiffValue,
153-
[HeadStateNetworkContentType.AccountTrieNode]: AccountTrieNodeValue,
154-
[HeadStateNetworkContentType.ContractTrieDiff]: ContractTrieDiffValue,
155-
[HeadStateNetworkContentType.ContractTrieNode]: ContractTrieNodeValue,
163+
return Uint8Array.from([type, ...contentKeySerializers[type].serialize(content)])
156164
}
157165

158166
export function getContentValue<T extends HeadStateNetworkContentType>(
159167
type: T,
160-
content: TContentValue<T>,
168+
content: HeadStateNetworkContentValue<T>,
161169
) {
162-
return serializeContentValue[type].serialize(content)
170+
return contentValueSerializers[type].serialize(content)
163171
}

0 commit comments

Comments
 (0)