Skip to content

Instantly share code, notes, and snippets.

View zeroFruit's full-sized avatar
🎯
Focusing

JooHyung Kim zeroFruit

🎯
Focusing
View GitHub Profile
@zeroFruit
zeroFruit / handler.go
Last active July 5, 2022 00:56
Cosmos Dev Series: Cosmos-SDK-based Blockchain Upgrade - UpgradeHandler
// UpgradeHandler specifies the type of function that is called when an upgrade
// is applied.
//
// `fromVM` is a VersionMap of moduleName to fromVersion (unit64), where
// fromVersion denotes the version from which we should migrate the module, the
// target version being the module's latest version in the return VersionMap,
// let's call it `toVM`.
//
// `fromVM` is retrieved from x/upgrade's store, whereas `toVM` is chosen
// arbitrarily by the app developer (and persisted to x/upgrade's store right
@zeroFruit
zeroFruit / app.go
Created July 5, 2022 00:40
Cosmos Dev Series: Cosmos-SDK-based Blockchain Upgrade - NewSimApp x/gov handler
func NewSimApp(...) *SimApp {
...
// register the proposal types
govRouter := govtypes.NewRouter()
govRouter.AddRoute(govtypes.RouterKey, govtypes.ProposalHandler).
AddRoute(upgradetypes.RouterKey, upgrade.NewSoftwareUpgradeProposalHandler(app.UpgradeKeeper))
govKeeper := govkeeper.NewKeeper(
appCodec, keys[govtypes.StoreKey], app.GetSubspace(govtypes.ModuleName), app.AccountKeeper, app.BankKeeper,
&stakingKeeper, govRouter,
@zeroFruit
zeroFruit / abci.go
Created July 5, 2022 00:35
Cosmos Dev Series: Cosmos-SDK-based Blockchain Upgrade - x/gov
// EndBlocker called every block, process inflation, update validator set.
func EndBlocker(ctx sdk.Context, keeper keeper.Keeper) {
keeper.IterateActiveProposalsQueue(ctx, ctx.BlockHeader().Time, func(proposal types.Proposal) bool {
passes, burnDeposits, tallyResults := keeper.Tally(ctx, proposal)
...
if passes {
handler := keeper.Router().GetRoute(proposal.ProposalRoute())
cacheCtx, writeCache := ctx.CacheContext()
err := handler(cacheCtx, proposal.GetContent())
@zeroFruit
zeroFruit / upgrade.proto
Created July 5, 2022 00:31
Cosmos Dev Series: Cosmos-SDK-based Blockchain Upgrade
// SoftwareUpgradeProposal is a gov Content type for initiating a software
// upgrade.
message SoftwareUpgradeProposal {
string title = 1;
string description = 2;
Plan plan = 3 [(gogoproto.nullable) = false];
}
// Plan specifies information about a planned upgrade and when it should occur.
message Plan {
@zeroFruit
zeroFruit / Cosmovisor directory structure.txt
Created July 5, 2022 00:27
Cosmos Dev Series: Cosmos-SDK-based Blockchain Upgrade
. # $DAEMON_HOME
β”œβ”€β”€ config
β”œβ”€β”€ cosmovisor
β”‚ β”œβ”€β”€ current # symbolic link
β”‚ β”œβ”€β”€ genesis
β”‚ β”‚ └── bin
β”‚ β”‚ └── simappd # $DAEMON_NAME
β”‚ └── upgrades
β”‚ └── v2.0.0 # upgrade name
β”‚ └── bin
@zeroFruit
zeroFruit / network.go
Created October 31, 2020 12:33
Modeling the Internet from the scratch: Link-layer, LAN, Switch - Code snippet: Build network-3
func Build() (host1 *link.Host, host2 *link.Host, host3 *link.Host,
swch1 *link.Switch, swch2 *link.Switch) {
// setup node
// setup interface
...
// setup link
link1 := link.NewLink(1)
attachLink(intf1, link1)
attachLink(sp11, link1)
@zeroFruit
zeroFruit / network.go
Created October 31, 2020 12:31
Modeling the Internet from the scratch: Link-layer, LAN, Switch - Code snippet: Build network-2
func Build() (host1 *link.Host, host2 *link.Host, host3 *link.Host,
swch1 *link.Switch, swch2 *link.Switch) {
// setup node
...
// setup interface
intf1 := link.NewInterface(40001, link.AddrFromStr("11-11-11-11-11-11"), host1)
attachInterface(host1, intf1)
intf2 := link.NewInterface(40002, link.AddrFromStr("22-22-22-22-22-22"), host2)
attachInterface(host2, intf2)
@zeroFruit
zeroFruit / network.go
Created October 31, 2020 12:30
Modeling the Internet from the scratch: Link-layer, LAN, Switch - Code snippet: Build network-1
func Build() (host1 *link.Host, host2 *link.Host, host3 *link.Host,
swch1 *link.Switch, swch2 *link.Switch) {
// setup node
host1 = link.NewHost()
host2 = link.NewHost()
host3 = link.NewHost()
swch1 = link.NewSwitch()
swch2 = link.NewSwitch()
}
@zeroFruit
zeroFruit / swch.go
Created October 31, 2020 12:28
Modeling the Internet from the scratch: Link-layer, LAN, Switch - Code snippet: Switch Forwarding
// Forward receives id of port it receives frame, address of sender
// and frame to send to receiver. Based on id and address it determines whether to
// broadcast frame or forward it to others, otherwise just discard frame.
func (s *Switch) Forward(incoming Id, frame na.Frame) error {
s.Table.Update(incoming, frame.Src)
frm, err := s.frmEnc.Encode(frame)
if err != nil {
return err
}
@zeroFruit
zeroFruit / port.go
Created October 31, 2020 12:25
Modeling the Internet from the scratch: Link-layer, LAN, Switch - Code snippet: Port
// Port can transmit data and can be point of link. But it has no hardware
// address. Before using Port, it must register its own Id
type Port interface {
Transmitter
EndPoint
Register(id Id)
Registered() bool
}