-
-
Notifications
You must be signed in to change notification settings - Fork 306
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
125 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import {BasicType} from "@chainsafe/ssz"; | ||
import {ValidatorStatus} from "../types.js"; | ||
|
||
// TODO: add spec reference once defined | ||
const statusToByteMapping: Record<ValidatorStatus, number> = { | ||
pending_initialized: 0x01, | ||
pending_queued: 0x02, | ||
active_ongoing: 0x03, | ||
active_exiting: 0x04, | ||
active_slashed: 0x05, | ||
exited_unslashed: 0x06, | ||
exited_slashed: 0x07, | ||
withdrawal_possible: 0x08, | ||
withdrawal_done: 0x09, | ||
}; | ||
|
||
const byteToStatusMapping = Object.fromEntries( | ||
Object.entries(statusToByteMapping).map(([key, value]) => [value, key]) | ||
) as Record<number, ValidatorStatus>; | ||
|
||
export class ValidatorStatusType extends BasicType<ValidatorStatus> { | ||
// TODO: review if those parameters are correct | ||
readonly typeName = "ValidatorStatus"; | ||
readonly byteLength = 1; | ||
readonly fixedSize = 1; | ||
readonly minSize = 1; | ||
readonly maxSize = 1; | ||
|
||
defaultValue(): ValidatorStatus { | ||
return "" as ValidatorStatus; | ||
} | ||
|
||
// Serialization + deserialization | ||
|
||
value_serializeToBytes(output: ByteViews, offset: number, value: ValidatorStatus): number { | ||
output.uint8Array[offset] = statusToByteMapping[value]; | ||
return offset + 1; | ||
} | ||
value_deserializeFromBytes(data: ByteViews, start: number, end: number): ValidatorStatus { | ||
this.assertValidSize(end - start); | ||
|
||
const status = byteToStatusMapping[data.uint8Array[start]]; | ||
|
||
if (status === undefined) { | ||
throw Error(`ValidatorStatus: invalid value: ${data.uint8Array[start]}`); | ||
} | ||
|
||
return status; | ||
} | ||
tree_serializeToBytes(): number { | ||
throw Error("Not supported in ValidatorStatus type"); | ||
} | ||
tree_deserializeFromBytes(): never { | ||
throw Error("Not supported in ValidatorStatus type"); | ||
} | ||
|
||
// Fast tree opts | ||
|
||
tree_getFromNode(): ValidatorStatus { | ||
throw Error("Not supported in ValidatorStatus type"); | ||
} | ||
tree_setToNode(): void { | ||
throw Error("Not supported in ValidatorStatus type"); | ||
} | ||
tree_getFromPackedNode(): ValidatorStatus { | ||
throw Error("Not supported in ValidatorStatus type"); | ||
} | ||
tree_setToPackedNode(): void { | ||
throw Error("Not supported in ValidatorStatus type"); | ||
} | ||
|
||
// JSON | ||
|
||
fromJson(json: unknown): ValidatorStatus { | ||
return json as ValidatorStatus; | ||
} | ||
|
||
toJson(value: ValidatorStatus): ValidatorStatus { | ||
return value; | ||
} | ||
} | ||
|
||
// TODO: export from ssz / or move type to ssz? | ||
type ByteViews = { | ||
uint8Array: Uint8Array; | ||
dataView: DataView; | ||
}; | ||
|
||
export const validatorStatusType = new ValidatorStatusType(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters