JNCIA

1

OSI:
7-app
6-present
5-session
4-transport
3-net
2-data-link
1-phy

tcp/ip
4-app
3-transport
2-internet/network
1-link/network access

Please do
not throw
sausage pizza
away

Armadilloes
take in
new ants

MAC= Media Access Contor
LLC= Link Local Control

L2 Header: preamble+sfd src mac, dst.mac, type data FCS

L3 ipv4: Header 20B ipv6: header:

L4: syn, sync-ack, ack

hub=layer1 repeater

qos: classification, marking, mgmt/policy

2 Describe Ethernet and ipv4

binary/decimal/hexadecimal

MAC address (48bits): OUI (24) + Interface ID (24)

ipv4: 32b (8×4)

unicast, multicast, broadcast
01005E
224-239

3 Apply ipv4

subnetting/supernetting
ARP

4 ipv6

128bits – 8xquartet(16b)

broadcast: none!!!!!!!!!
multicast: MAC: 33-33:.. // FF00::/8
link-local: always assigned, no routable: FE80::/10 or /64
unique local: like priv ip, routable internally. FC00::/7 or FD00::/8
global unicast: pub ip, routable internet 2000::/3

2001:1234:5678::/48
2001:1234:5678:9abc::/64
2001:1234:5678:9abc:d:e:f:0/127

NDP = Neighbor Discovery Protocol -> ICMP + link-local + multicast.

  • Duplicate address detection (DAD)
  • link layer address resolution
    — Neighbor Solicitation: src.ip link-local dst.ip: solicited_node_ip (ff02::1:)
    — Neigbor Advertisiemen:

Router Discovery:
router solicitation: RS, request sent by host, dst.ip: FF02::2 (all routers) use link-local as src.ip
router advertisement: RA, reply sent by router, src.ip link-local, dst.ip = FF02::1 (all hosts in link-local). It contains global unicast range

SLAAC: Stateless Address AutoConfiguration
1) obtain prefix through RA
2) host creates its own interface id.
2.1: use EUI-64: use MAC (48b) and filling -> 1st half MAC + FFFE + 2nd half MAC + flip the 7th bit of the MAC.

5 – Intro Juniper Cert and labs

OS fundamentals, user interface, configuration basics, operational monitoring and maintenance
routing fundamentasl, routing policy and firewall filters.

lab: ex2200, vMX, vQFX, vSRX (kvm)

juniper vlabs – are free!

junos genius -> get voucher for jncia, and train for -s and -p.
-> day-one books!

6- Junos Arch

daemons
cp (routing-engine) vs dp (packet-fw-engine). fw-table is created by RE and then copied to the PFE

exception traffic = non-transit traffic -> goes to RE
PFE can handle ICMP responses

major.minor.X = security (srx)
F = feature
R = bug fixes

7 Junos CLI

request system zeroize
mgd = mgmt daemon
/config -> junos config

% -> freebsd cli

-> operationa mode

-> config mode

show interfaces terse

ctr-a -> begining line
-e -> end line

help tip cli
help topic x
help apropos interfaces

show x | save var/tmp/text.txt
file show var/tmp/text/txt
delet
show conf | display x

8 Create Junos Base Config

operator: clear network reset trace view
read-only: view
super-user: all
unauthorized: none

snmp: privacy -> priv+auth
auth -> authNopriv

[] -> list

syslog: timestamp hostname facility:message
set sytesm syslog file x host y
server -> makes the device a syslog server !!!
0 emergency (kernel)
1 alert (user-space)
2 critical
3 error
4 warning
5 notice
6 info
7 any (debug)

set vlans NAME vlan-id x
set interfaces ge-0/0/0 unit 0 family ethernet-switching port-mode access vlan members NAME
trunk vlan members [NAME1 NAME2]
srx -> untrust / trust

9 Configure Junos Interfaces

set vlans NAME vlan-id 25
set interfaces vlan.25 family inet address IP/24
set vlans NAME l3-interface vlan.25

eui-64: use mac 48b
set address 2001:db8:dead:beef::/65 eui-64

10 Junos config datastores

active config
configure -> candidate config global
configure private -> private candidate per user
configure exclusive -> lock candidate for other users. No commited changes are lost after exit
request system configuration rescue save
rollback rescue
edit archival configuration
set archive-sites http/ftp/scp:user:pass@url
set transfer-on commit

encrypt: aes/des

11 Routing

inet.0 ipv4 unicast
inet.1 ipv4 multicasts

RE: creates Routing table, then Forwarding table, then it is copied to the PFE

1 – most specific
2 – lowest AD

0 directed  connect

AD: 5 static routes
10 ospf internal
15 isis
150 ospf ext
160 isis ext
170 bgp

set routing-options static route DST/x next-hop NH1
qualified-next-hop NH2 preference 7

ospf:
224.0.0.5: hello
.6: DR

edit protocols ospf
set area 0.0.0.0 interfaces X.0

edit routing instance
set NAME_INSTANCE interface x.0
routing-options static route
protocols ospf area 0.0.0.2 interface y.0

there are several types of routing-instances: ie:
vrf: for mpls, RD, RT, import/export
virtual-router: no import/expot, no rd,rt

ipv6 static route:
edit routing-options
set rib inet6.0 static route X/Y next-hop Z

edit protocols ospf3
set area 0.0.0.0 iterface X.0

12 multi-vendor ospf

router id – hightest lo.IP or explicit
hello: router-id,
area-id,
timers (hello interval: 10 dead-interval: 40 (ethernet)),
area-types: stub, total-stub, nssa
mtu (stuck in ex-start)

LSA-types:

set routing-options router-id IP-Lo;
edit protocols
set ospf area 0.0.0.0 interface lo0.0;

show ospf database: ID = LSA ID !!!!

ref bw / interface bw = metric

ref bw = 100 mpbs Juniper

set protocols ospf reference-banwidth (bits per second!!!!)

13 Route policies

routing table modification: import/export policies
route distribution: think of the perspective of the routing table

edit policy-options
set policy-statement ISIS2OSPF from protocol isis then accept
from prefix-list NAME then xxx
from route-filter IP/x orlonger

set policy-statement TEST1 term T1 from protocol isis then accept
set policy-statement TEST1 term T2 from prefix-list NAME then tag 1
then accept

edit protocols ospf
export ISIS2OSPF

edit policy-options
set prefix-list PL IP/x

bgp:
edit policy-options
set policy-statement BGP-TEST1 then you can change/add many BGP attributes

default routing policies junos
bgp: default import: accept all
export: readvertise all (exception iBGP for split-horizon)

mpls: default import: accept all
export: readvertise all

isis: default import: accept all
export: reject everything

ldp: default import: accept all
export: reject all

ospf: default import: accept all (you can’t override this by ospf design)
export: reject all

14 Firewall Filter (ACLs) – no stateful !!! -> need input/output !!!

data-plane modification -> accept, drop, QoS, change NH, RPF, logging, counting

edit firewall
edit family inet
set filter NAME term T1 from source-addess IP/32
from destination-port 80
then accept
term then discard (IMPLICIT!!!)

edit interfaces xe-0/0/0 unit 0 family inet
set filter input FILTER-NAME

discard -> drop + no logging (silently)
reject -> drop + sent ICMP

qos:
policer

edit firewall
set policer SSH-BE if-exceeding bandwithd-limit 100m then forwarding-class best-effort

edit firewall family inet filter NAME
set term T1 then forwarding-class assured-forward
then policer SSH-BE

RPF:
strict
loose
edit interfaces ge-0/0/0 unit 0 family inet
set rpf-check fail-filter NAME-FF

15- Operate and Maintain Junos

show system storage
alarms
commit
connections
statistics

show chassis alarms
environment
hardware
routing-engine

show virtual-chassis

show chassis fpc

show route table X.0 IP/X extensive

show interfaces ge-0/0/0.0 extensive
show interfaces ge-0/0/0 extensive -> MAC address, CoS

monitor interface ge-0/0/0 -> statistics live
monitor interface traffic -> all interfaces stats
monitor traffic interface ge-0/0/0.0 extensive -> tcpdump

request system halt -> graceful shutdown

request system storage cleanup dry-run

request system software add /var/tmp/xxxx.tgz reboot

=================

JNCIA – Official training

Note: The process of moving routes between a routing protocol and the routing table is described always from the point of view of the routing table. That is, routes are imported into a routing table from a routing protocol and
routes are exported from a routing table to a routing protocol.

Intro JunOS on-demand

l2mtu: 1514s (ethernet header) _> physical interface

terse, brief, detail, extensive

set cli screen-width 1000

configure exclusive -> lock for one user, but other users can’t commit
configure private -> only commit from top, locks hierachy where you have made a change
commit at
clear system commit

disable (put at the end) -> interfaces and protocols -> “delete …. disable”
deactivate (put at the beginning) -> nearly anything, it is commenting out a piece of config -> use “activate”

rename: rename address 1.1.1.1/31 to address 1.1.1.11/31
replace patten: replace pattern xe-0/1/2 with xe-0/1/4 (hierarchy aware!)
copy / move
insert
annotate
save
load
wildcard delete

emacs short-cuts
ctr-w delete word
ctr-a – beginning
ctr-e – end
ctr-k – delete everything from cursor onwards
esc-b – backwards 1 word
esc-f – forwards 1 word

show route -> Local (ip configured in interface)s

  • = chosen active route between two or more protocols

= best path between two paths from the same protocol

set routing-options static route IP/23 next-hop IP
rib inet6.0 static route …

::/0

show route protocol static

ospf => LSA
isis => LSPs

set protocols ospf[3] area 0 inteface xe0/0/0.0 [passive]

hello: 224.0.0.5, area, hello, dead=4xhello

ospf3 -> ipv6

ex3400

show configuration vlans
set vlans NAME vlan-id X

set interfaces ge-0/0/0 unit 0 family ethernet-swtiching interface-mode [access|trunk]
vlan members [NAME1 NAME2]

set interface ge-0/0/1 vlan-taggig !!!!
unit 10 vlan-id 10
family inet address IP1/24

show ethernet-switching table vlan NAME

MistAI:
port securitu. loop preventio, poe, vc, macsec, evpn-vxlans

missing vlans, wifi, port bounced, etc. onboaring, AI. Marvis (assistant)
wifi assurance, wired assurance, wan assurance

mgmt port: fxp0, em0,

manual
set system time-zone UTC

set date yyyymmddhhmm

ntp
set system ntp server IP
show system uptime
show ntp associations

set system name-server IP

set system login user NAME class CLASS authentication encrypted-password xxxxx
$6$ -> SHA-512

super-user, unauthorized, operator (clear, view, reset, trace, network), read-only (view)
flags: all, clear, configure, network (ping, traceroute), view (no-config-show, system-wide, routing, protocol)

j-web SRX, a few mx.
configure, monitor, status, upgrade

set system service web-management https system-generated-certificate

rs-232

set system login messag “xxx” // before login
set system login annoucement (for after login) “xxxx”

delete system commit factory-settings

request system configuration rescue save

rollbackc rescue

request system reboot/halt [both-routing-engines]

ztp
dhcp (DORA)
discover(broadcast)
offer
request: replies with the same ip info
acknowledge:

dhcp-options:

routing-engine: CPU, monitoring, mgmt, system, chassis, protocol, routing, junos, ping/traceroute
writw forwarding-table and send to data-plane (NH IP, MAC and out interface))

RIB, FIB

PFE: muscle. ASIC (<> CPU) Express – PTX, Trio – MX
look-up, forward traffic, manipulate (ethernet headers, vlans, TTL)
cos: rate-limit, fw filters, priority

exception traffic: traffic to/from RE (loopback), ssh, protocols, ping, ARP, SNMP. TTL, ICMP replies

  • some PFE CPU can handle some exception traffic.

start shell
daemos: routing (rpd), chassis, interfaces, mgmt (mgd), snmp, dcd (device control daemon), ppmd (periodic packet mgmt daemon)

junos EVO
basedon linux, whitebox, integrate with 3rd party
daemons are individual apps, independently of evo, data is stored in distributed database, apps can be restarted without losing state

syslog:
facility:severity:destinatin
show log FILENAME
show log interactive-commands | match USER

monitor start messages -> real time
monotor stop

help syslog CODE_MESSAGE

set system syslog host IP any notice

severity:
0 emergency
1 alert
2 critical
3 error
4 warning
5 notice
6 info
7 any

ping IP size PAYLOAD!!! (it is not MTU, it doesn include icmp header = 8, ip header=20b) so for mtu=1500 you need payload 1472
output shows the size of payload+icmo header only !!!!

traceroute doesnt show return path!!!

show ipv6 neighbors (via NDP)

monitor interfce traffic -> real time stats all interaces
monitor interface xe-0/1/5 -> real time stts interface with errors
monitor traffic interface xe-0/1/5 -> tcpdump

help apropos COMMAD (fnd command)
help topic ospf dead-interval (documentation)
help referece ospf aread (configuration syntax reminders)

statefull ->hw fw — security policies
stateless -> check per packet, no idea about session, lighter load — firewall filters (ACL)

term -> from: 0+ match conditions -> then: 1+ actions

match -> nearlu any field in header ipv4,6 and ethernet

  • same condition -> OR (match source IP1, match source IP2)
  • diff condition -> AND (match source IP, match dst port 343) discard -> drop silent
    reject -> drop + icmp unreachable

enable count, sample, duplicate, policing (rate-limit), cos, redirect, load-balance

default in fw filter: discard -> implicit term

set firewall family inet filter NAME term TERM from CONDITIONS then AcTIONS

show config firewall family inet filter XXX | display set relative

  • count/log has “accept” implicit

trade-offs

set interface xe-0/1/4 unit 0 famiy inet filter output NAME

show fiewall counter COUNTER filter NAME

insert firewall family inter filter NAME term TERMy before/after term TERMx


set firewall family inet filter NAME apply-flags omit -> doesnt show i
set protocols ospf apply-flags omit -> doesnt show (hidden command

show omit -> display set or display omit


protect protocols lldp -> lock config, can’t delete
unprotect protocols lldp

annonate system “comment”


file list /var/log
file show FILE
show commands | save LOG.txt
show config | compare FILE1
file compare files FILE1 FILE2

load override terminal –> replace entirely config via CLI
load merge terminal [relative] -> add your config to the current config

load merge|override FILE.txt

load set terminal

set system archival configuration transfer-on-commit


set groups MTU_9192 interfaces mtu 9192
set interfaces apply-groups MTU_9192
set interfaces xxxx apply-groups-except MTU_9192

show .. | display set | display inheritance [no-comments]

edit interfaces interfaces-range RANGE
set member-range ge-0/0/10 to ge-0/0/12
set unit 0 family ethernet-switching vlan members VLAN

wildcard delete interface xe-0/1/*


loopback:
set interfaces lo0 unit 0 family inet address ip/32
set protocols ospa area 0 interface lo0.0

RE protection -> firewall filter in lo0

router-id: lowest lo0 ip

irb: integrated routing and bridging -> default fw for vlan

set interfaces irb unit 10 family inet address ip1/24
20 …
set vlans NAME l3-interface irb.10
NAME2 20

show vlans NAM detail ->shows irb


Several IPs in interface:
Is-preferred: the source to devices in the same subnet (lowes IP in subnet)
Is-Primary: the source to devices on a different subnet. (lowest IP from all subnets)


ecmp: equal-cost multipath

LAG: link agg groups: LB per flow
set chassis aggregated-devices ethernet device-count X (>=1)

set interfces ae0 unit 0 family inet address x
aggregated-ether-options lacp active

set interfaces ge-0/0/0 gigether-options 802.3ad ae0


automation:
XPATH
netconf


set policy-options prefix-list DIRECT apply-path “interfaces <> unit <> family inet addres <*>”

firewall filter: match-codition+(-except) -> protocol-except udp, destination-port-except 443, et

log + count => implicit “accept” but you can use “next term”

show firewall log

show class-of-service forwarding-class

show route forwarding-table destination IP/x table default

authentication order: if one fails/unreachable, check with the following. If all unreachable, checks local

custom login commands: (command1) | (command2)

order: permissions -> deny-commands/config -> allow-command/config (allow override deny!!!)

JNCIS-SP

JNCIS-SP
Junos Intermediate Routing On-Demand – DONE
Junos Service Provider Switching – DONE
Junos MPLS Fundamentals – DONE

https://jlabs.juniper.net/vlabs

=========================================
Junos Intermediate Routing On-Demand
=========================================

CBT Junos Tunnels


gr-0/0/0 GRE
ip-0/0/0 IPoverIP

set chasis fpc 1 pic 2 tunnel-services [bandwidth X]–> enable tunnels in x-1/2/x

GRE header: 24bytes = 20byes IP header + 4bytes (reserv, version, protocol type) => increase MTU !!! 0x800 = ipv4 / 8x86dd iv6

frames over GRE: set chassis network-services enhanced-ip
set intefaces gr-0/0/0 unit 0 family bridge interface-mode trunk vlan-id-list 100 core-facing

              set routing-instances virtsw instance-type virtual-switch
                                           interface ge-0/0/2.0
                                                     gr-0/0/0.0
                                            bridge-domain C100 vlan-id 100

CBT Chassis HA

vrrp: MAC 0000.5E00.01xx (MC.18) group-id (8bit) identical, priority (8bit 1-254, def=100) or hights IP,
v3: ipv6 and <1s

(inside interface config)
set vrrp-group 1 virtual-address VIP [priority x]

GR: graceful restart
set routing-options graceful-restart (show ospf overview)

GRES: Graceful Routing Engine Switchover, BUT Control Planel needs to reconverge !!!
set chassis redundancy graceful-switchover
request chassis routing-engine master switch check

NSR: Non-stop active routing. NSR helps GRES to get CP syncronize between REs. Incompatible with GR !!!
set routing-options nonstop-routing
set systen commit syschronize
show task replication

ISSU: in-service sw upgrade
Needs GRES+NSR
request system software in-service-upgrade /var/tmp/file.tgz reboot

virtual-chassis

CBT IPv6

multicast FF00::/8
link-local FE80::/10 (no routable), NDP (like ARP) neighbor discovery prot (Neigh Solicitation, Neigh Adver)
unique-local FC00 or FEC8 (no internet routable – like private IP)
global unicst: 2000::/3

dhcp:
slaac (staless): EUI-64: use MAC 48b + FFFE (mac 24b (flip 7th) FFFE mac 24b) -> 64 bits + 64 bits from Route Solication
set interface ge-0/0/2 unit 0 family inet6 address 2012:db8::/64 eui-64

dhcpv6 (stateful)

ospf3: needs a router-id that is ipv4!!!
set routing-options router-id ipv4

CBT LACP

802.3ad
max 8 links per lacp
set chassis aggregated-devices ethernet device-count X

set interfaces ge-0/0/0 ether-options 802.3ad ae0
1
set interfacce ae0 aggregated-ether-options lacp active|passive periodic fast|slow (30s) (def is fast=1sec)
unit 0 family etherent-switching port-mode trunk (EX)
interface-mode (QFX(
bridge interface-mode trunk vlan-id-list 300 (MX)

CBT ISIS

level0 – ES-IS
level1 – IS-IS intra-area
level2 – IS-IS inter-area

 intra-area AD=15  idem

ISIS inter-area AD=18 – metric default 10s

IIH = Hello using MAC address. Router ID, area, Neighbor discovery
CSNP = Like DB descriptor in OSPF. After neighbor discovery
PSNP = request prefix and route info

Border router, establish L2 adj (differnt area) -> 2x LSDB!!!
establish L1 adj (same area)

LSPDU: sent by border router, with Attached bit. The router L1 that receives it install a default route to the originator of LSPDU

NET address: network entity title. In lo0 !!!
49.(16b – area ID).(48b MAC).00

Set adjacencies (no need IPs!)
1) set NET in lo0.0 under family iso
2) set protocols isis interface X.0
3) set interface x.0 family iso (NET not needed!(
4) disable the level you dont need!! (by default all links are l1/l2)

authentication-type simple or md5
authenticaion-key key

Protocol Independent Routing (Static routes)

0 directed  connect

AD: 5 static routes
7 rsvp-lsp
8 sr-te
9 lsdp-lsp
10 ospf internal
15 isis l1
18 isis l2
105 pim
130 agg
150 ospf ext
160 isis ext
170 bgp

prefernce=5
nh = ip directled connected, reject, discard
no recursive loop-up performed by default (like cisco) -> need to use “resolve”
qualified-next-hop IP preference X
no-readvertise (ie for mgmt not advertised into IGP) block exporting via policies
as-path, community, metric, preference

ipv4: edit routing-options staic route …
ipv6: edit routing-options rib inet6.0 static route …

Aggregate routes

preference=130
at least one contributing route active
default nh = reject
as-path, community, metric, policy, preference
show route AGG/22 exact detail

edit routing-optons aggregate ->

*If contributing routes don’t have a next hop (i.e., are not usable), the aggregate route may still appear, but it will be hidden (inactive) — and not advertised unless you use a discard next-hop or similar workaround.

Generated Routes

similar to agg routes. NH = nh of primary (lowest preference) contributing route (aggregate nh=reject)

diff from agg:
-you can assign a next hop.
-And you don’t need the contributing route to be resolvable — it just needs to exist in the routing table (even if unusable).
-It allows more flexibility when the contributing route is unusable or when you want to inject a route into the table regardless of reachability.

ie: advertise default into osfp if you are learning a specific prefix from your ISP.

set policy-option policy-statement match-contributing-prefix term match-bgp from protocol bgp route-filter NET1/16 exact then accept
term else-reject then reject
set policy-option policy-statement export-default term match-default from protocol aggregate route-filter 0.0.0.0/0 exact then accept

set routing-options generate route 0.0.0.0/0 policy match-contributing-prefix
set protocol ospf export export-default

Martia Routes

0.0.0.0/8 orlonger
127.0.0.0/8 orlonger
192.0.0.0/24 orlonger
240.0.0.0/4 orlonger

ipv6: loopback, rfc 2373, link-local

set routing-options martians x/8 orlonger

show route martians table inet.0

Routing Instance (LAB)

show route instance -> default: master -> inet.0 inet6.0

edit routing-instances
set instance-type x=forwarding, l2vpn, no-forwarding (make big network smaller), virtual-router (system virtualization), vpls, vrf (for l3vpn)
set interface ge-0xxxx
set routing-options static route …. next-hop xxx
set protocols ospf area 0.0.0.0 interface ge-xxxxx

show route table INSTANCE.inet.0
show interface terse routing-instance INSTANCE

rib groups: share routes between routing tables

edit routing-options
rib-groups NAME
export-rib T0 (only one! – where routes should be taken from) normally omitted because it is always the primary rib
import-rib T1 T2 (severals tables – where routes should be placed) ????????/
import-policy POLICY

edit routing-options
rib-group test
import-rib [ inet.0 test.inet.0] ===> routes from inet.0 TO test.inet.0 ???? ^^^^ differente from aboves

edit protocols ospf
set rib-group test
area 0.0.0.0 interface ge-0/0/0.0 lo0.0

create a logical-tunnel interface between instances and route between them: lt-0/0/0
*requires service card

edit interfaces lt-0/0/0
unit 0
encapsulation ethernet
peer-unit 1
family inet
uni t
encapsulation ethernet
peer-unit 0
family inet

LOAD BALANCING

per-packet issue -> out-of-order (Junos doesnt do per-packet !!!!)

per-flow LB:
set policy-options policy-statement LB-ALL then load-balance per-packet
set routing-options forwarding-place export LB-ALL

show route forwarding-plane

default flow ipv4: incoming interface, src add, dst add, protocol
ipv6: l3,l4,traffic class

modify: set forwarding-option hash-key family inet layer-3 layer-4

Filter-Based-Forwarding (FBF) (lab)

forwarding based on source IP

1) using RIB-groups
-create match filter and apply to incomming interfaces
set firewall family inet filter F-NAME term TERM from CONDITION then routing-instance INSTANCE

  • create default term because by default, filter discard traffic!
    set interface ge-0/0/0 unit 0 family inet filter input F-NAME

-create routing instance
set routing-instance INSTANCE instance-type forwarding!! routing-option static route 0.0.0/0 next-hop IP
next-table inet.0
-create rib group
set routing-options interface-routes rib-group inet GROUP
rib-groups NAME import-rib [ inet.0 INSTANCE.inet.0 ]

show route table INSTANCE.inet.0

2) using instance-import instead of RIB-group

set policy-options policy-statement ISP-IMPORT from instance master then accept

set routing-options ISP instance-type forwarding!! routing-options static route 0.0.0/0 next-hop IP
instancce-import ISP-IMPORT

Fundamenals OSPF

LSDB, flooding LSA, all routers must have identical LSDB, SPF algo

packets type:
1 hello: 10s default, MC to 224.0.0.5, incluse: netmask, hello interval, dead, options, priority, DR, BDR, neighbor
2 db description: during adj formation, hightest RID is primary for sync and set/maintein seq nu. This relationship is forgotten after transfer. LSDB = ospf header, seq nu, lsa header
3 ls-request: request precise version of db: ospf header, ls-type, ls-id, adv-router (RID of the originator router)
4 ls-update:to 224.0.0.5/6: ospf header, num of adc, ls-adv
5 ls-ack: unicast to originator.

Adj states
down
init: hello packet sent
2way: hell received, bidir achieved
exStart: decide primary router for db sync
exchange: lsdb exchange started
load: transmision finish but still reading from peer
full: lsdb is sync

ethernet: avoid adj all routers. DR only router creates adj to all routers in segment. BDR creates adj to all routers too.
interface-type p2p: no DR/BDR is elected (saves time), no lsa type-2 generated
DR: higher priority (default 128), higher RID, no preemption,
NoDR routers create 2way adj with other NoDR routers.

scalability
reduce lsdb: multiple areas, route summarization between areas.
areas:
area0: all connect to area0
ABR: connect areaX to area0
ASBR: outside ospf to areaX
stub area: LSA3 yes (inter-area), no LSA4/5 (ext) and no ASBR. Inject default route (need configuration)
totally stubby-area: only default, no LSA3-5.
not-so-stubby-area: it gets external routes and can advertise them to are0 but it can’t receive LSA5 from other areas.

lsa1: router links (intra-area)
lsa2: network links (by DR only), describe routers attached to the segment
lsa3: summary, by ABR, inter-are links
lsa4: by ABR, path to ASBR.
lsa5: external, prefix redistributed by ASBR, by-default lsa5-type2 (ext cost not included) (type1 cost to ASBR includded)
lsa7: nssa, by ASBR in NSSA, then the ABR creates a LS5.

Deploy OSPF

ospfv3 works with ipv4/6/
auth, summarization (ABR), external prefix-limit, graceful-restart, BFD

!! cost=ref-bw/bw !!

set protocols osfp reference-bandwidth X (100Mbps default) (lo.0 has always cost 0)

set routing-options router-id LO-IP
set protocols ospf|ospf3 area AREAID interface x.x

set policy-options policy-statement 2OSPF term MATCH from protocol direct route-filter NET/X exact then accept
set protocols ospf export 2OSPF

show ospf neighbor extensive
clear ospf neighbor

show ospf interface [extensive]
show ospf route [abr, asbr, inter, intra, extern, detail,instance]
show ospf database [brief (default), detail, extensive,]
show ospf statistics
show ospf log

troubleshooting:
no neighbor: check link status
exstart: check MTU
2way: normal for DR-other neighbor

set protocols ospf traceoptions file TRACE-ospf flag error detail flag even detail ….
show log TRACE-ospf

edit protocols ospf
save /var/tmp/working-ospf.confg ==> save config to a file from config mode.

Fundamenals BGP

path vector routing protocol.
NLRI: Network Layer Reachability Information
classless, bgpv4, rfc 4271
nonportable (ISP provide IP range), portable (customer has its own prefix).
ebgp -> ttl=1 !!!
ibgp: full-mesh, using lo0 !!!
tcp 179, manually defined neighbors
loop avoidance: as path,

state:
idle: init stte
connect: waiting for tcp to complete
active: trying to establish tcp connection

opensent: tcp completed. wait for open from peer
openconfirm: wait for keep alive from peer.
established: received keepalive from peer. all done

4096 max bgp message size, min 19 bytes

messages:
open: after tcp completed, initiates bgp sesson
update: transport routing info
keepalive:
notificaton: signal when something is wrong.
refresh: soft clearing bgp session to re-advertise route

attributes
wellknown-mandatory: as-path, origin, NH. supported by al bgp implementations. included in each bgp update
wk-discretionary: local-pref, atomic-agg. supported by all bgp implementation. not included in each bgp update
optional-transitive: community, agg. not supported by all bgp implementation. but they need to pass it along unchanged
optional-nontransitive: MED, cluster-list, originator ID. not supported by all bgp implemnetation. If attribure is not identified, it is ignored and not passed along.

NH: ip of peer advertising prfix, must be in RIB-local. ibgp doesnt change it, ebgp changes it.
LP: determine outbound. higher best. used withing individual AS, not redistributed to ebgp (default 100)
as-path: check loop
origin: where was received: 0 – IGP, 1 – EGP, ? – incomplete (redistribute)
MED: multihomed to same external ISP (same ASN!!!). determine inbound from that ISP to you. lower best (default 0)
communities: edit policy-optionss

path-selection:
NH + no loop, highest LP, shortest AS-path, lowest origin, lowest med, ebgp before ibgp, if all ibgp then best exit from AS, if all ebgp, choose current active or one from peer with lowest RID, RR: shortest cluster lenght, routes from peer with lowest RID

nh calculation: checki inet.0 (ipv4) and inet.3 (mpls). If preference equal, inet.3 preferred.

Deploy BGP

ibgp: split-horizon -> full-mesh. NH not changed for routes coming from ebgp (change it with “next-hop self”)

edit policy-options
set policy-statemen NH-self term 1 then next-hop self

edit routing-options
set router-id LO.0
set autonomous-system ASN

edit protocols bgp
set group int-ASN
type internal
local-addess LO.0
neihbor R1.lo0
export NH-self
set group ext-ASN
type external
peer-as ASN
neighbor R2.interface.IP


edit routing-options aggregate
route IP/22

edit policy-options
set policy-statement adv-agg term 1 from protocol aggregate route-filter IP/22 extac then accept

routers from peers -> RIB-in -> import-policy -> RIB-local -> export-policy -> RIB_out -> routes to peers

only for active routes

RIB-in: shw route receved-protocol bgp IP (before routing filtering!!!)
RIB-out: show route advertised-protocol bgp IP (after route filtering!!!)

IP Tunneling

both statelss by default: keepalive config possible or use BFD
set protocols oam gre-tunnel interface gr-x/x/x.1 keepalive-time 10 hold-time 30

define static routes to use tunnel
set routing-options static route LAN2 next-hop gr-x/x/x.0

GRE: ipv4/6, mpls. 24B overhead. TTL decremented. RFC1702
gr-x/x/x

support multiple logical units per interface. as stateless, you need a valid route to the remote endpoints

set interface gr-0/0/0 unit 0 tunnel source IP1 destination IP2
family inet

IP-IP: 20B overhead. TTL decremented. rfc2003. Only for IP
ip-x/x/x

pmtud: set system internet-options gre-path-mtu-discovery
watch out MTU !!!

GR and BFD

uptime <> availability
GR=Graceful Restart: (NSR is mutuel excluent from GR)
BFD: hello bassed
VRRP: vip lan side
ISSU: dual RE, upgrade withouth interruption

GR
rquest grace period to neighbord. fowarding continue during restart. neighbord hide the failure to the rest of the network
supported: ospf, isis,bgp, rsvp, ldp.
requirements: all routers (restarting and helpers) need to support GR and NonStopForwarding !!!!
GR !!helper!! mode is enabled by default, but not for restarter???
set routing-options graceful-restart disable (globla or can do via protocol)

show bgp neighbor IP
set protocols ospf traceoptions flap graceul-restart

BFD ospf, isis, bgp, rsvp, pim, static routes. 3 hellos missed -> down
set protocols bgp group G1 bfd-liveness-detection minimun-interval 300
show bfd session
show bgp neighbor IP

https://www.cisco.com/en/US/technologies/tk648/tk365/tk480/technologies_white_paper0900aecd80244005.html

How are the timers actually negotiated? Each system, upon receiving a BFD control packet will take the “Required Min RX Interval” and compare it to its own “Desired Min TX Interval” and take the greater (slower) of the two values and use it as the transmission rate for its BFD packets. Thus, the slower of the two systems determines the transmission rate.

GRES, NSR, Unified ISSU

GRE switchover: doesnt conserve control plane… if NSR is configured, then it is kept.
without GRES: PFE is restarted, the new RE restart RPD
with GRES: PFE is not restarted, new RP restars RPD.

set groups RE1 system hostname R1-RE1 backup-router IP
interfaces fxp0 …
RE0 system hostname R1-RE0 backup-router IP
interfaces fxp0

commit synchronize

set chassis redudancy gracedful-switchover
show system switchover (only in backup RE)

NSR: uses GRES. RPD runs in backup RE. Mutually exclusive with GR

set routing-options nonstop-routing
chassis redunddancy graceufl-switchover

show task replication

Unified ISSU: unifie in-service software upgrade. ugrade junos withou disruption CP
GRES + NSR

VRRP

rfc 2338, by default master doesnt respond to ICMP to VIP (can be changed), support auth, preempt enabled by default
vrrp master: responds to ARP
224.0.0.18, ttl=255, 1s interval
virtual mac: 00.00.5E.00.01.VRID
hight priority -> best (dfault 100)

set interface ge-0/0/3 unit 0 family inet address IP vrrp-group X virtual-address VIP priority 200

show vrrp summary

INTRO IPv6

QoS, no NAT, end2end ipsec, autoconfig
header 40B. version, traffic class, flow label, payoad length, next header, hop limit, src add, dst add

extension headers
hop-by-hop options
routing
fragment
destination options
auth
enc security payload

8x 16b hex blocs

unicast
multicast
anycast

::/0 = default route
::1 -> loopback

scope:
broadcast: none!!!!!!!!!
multicast: MAC: 33-33:.. // FF00::/8
link-local: always assigned, no routable: FE80::/10 or /64
unique local: like priv ip, routable internally. FC00::/7 or FD00::/8
global unicast: pub ip, routable internet 2000::/3

NDP = Neighbor Discovery Protocol -> ICMP + link-local + multicast.

  • Duplicate address detection (DAD)
  • link layer address resolution
    — Neighbor Solicitation: src.ip=link-local dst.ip=solicited_node_ip (ff02::1:)
    — Neigbor Advertisiement:

Router Discovery:
router solicitation: RS, request sent by host, dst.ip: FF02::2 (all routers) use link-local as src.ip
router advertisement: RA, reply sent by router, src.ip link-local, dst.ip = FF02::1 (all hosts in link-local). It contains global unicast range

SLAAC: Stateless Address AutoConfiguration
1) obtain prefix through RA
2) host creates its own interface id.
2.1: use EUI-64: use MAC (48b) and filling -> 1st half MAC + FFFE + 2nd half MAC + flip the 7th bit of the MAC.

dhcpv6: rfc3315 – it doenst require the MAC to build the ipv6
anycast: rfc2526
set routing-options rib inet.6 static route 0::/0 next-hop IP
ospf3

tunneling ipv6 over ipv4:
set interfaces gr-0/0/0 unit 0 tunnel source IPv4.r1.loopback destination IPv4.r2.loopback
family inet6 address IPV6

INTRO ISIS

CLNP packets originally
PDU – protocol data units. IP reachability include in updates
LSDB. Single AS (IGP)
End-System = host
Intermidiate-System = router
L1: route within the area or towards L2
L2: route between areas and toward other AS

ospf similiraties:
L1L2 router = ABR
L2 = area 0

ISIS PDU:
hello: discover neighbor (IIH) like ospf hello, regular intervals 3sec for DR. broadcast networks => uses MC.14/15.
circuit type (l1,l2,l1l2, source ID (system ID), holding time, pdu lenght, priority (0-127), LAN ID

LS PDU: flood periodically in area. build LSDB

Seq Num PDU: complete: all LS in LSDB, flood periodically. multicast
partial: request missing LS PDU
CSNP: maintain LSDB in sync. sent by DIS only
TLV: encodig Type length Value

Adj:
L1: area ID must be same
L2: area ID can be different
DIS election (like DR in OSPF for multicass networks = ethernet). Use priority (0=never DIS, higher = winner) There is a DIS for L1 and L2.
there is no backup DIS, there is preemption.
metric: max=1023
delay
expense
error
wide metrics: 2^24

config:
by default all links are l1l2
edit protocols
set isis interface ge-0/0/0.0 level 1 disable

set interfaces ge-0/0/0 unit 0 family iso
family inet address IP
lo unit 0 family iso address 49.001.0192.0168.0291.00
inet address IP

show isis interfaces
show isis database
show isis adjacency
show isis spf log
show isis statistics
show isis route
set protocols isis traceoptions file isis-trace flag error detail flag hello detail
monitor start log-file-name
show log log-file-name

issues: physical (l1) or ethernet (l2) issue. Mismatch ares (for level1) and levels, minimum MTU 1492, lack of iso-net, missing lo0


CBT Service Provider Bridging Concepts

802.1q tag 32b -> vlan id: 12b. It doesnt scale ->
802.1ad SP (q-in-q) to overcome 802.1q: C-TAG, S-TAG –> 2x 802.1q header!
-> it must still learn MACs !
-> between SP, you need vlan translation
PEB (PE) customer port is “access port” !!! // IVL – independent VLAN Learning
set interface ge-0/0/2 unit 0 family bridge vlan-id S-TAG interface-mode access

  if needed to filter C-TAG, in PE to P port:
  set interface ge-0/0/2 unit 0 family bridge inteface-mode trunnk 
                                              inner-vlan-id-list x-y [limit the C-TAG vlans from customer]
                                vlan-id S-TAG

S-VLAN Bridge (P device) and PE-P ports
  set interface ge-0/0/2 unit 0 family bridge vlan-id S-TAG interface-mode trunk
                         encapsulation flexible-ethernet-services (aka 802.1ad!)
                         flexible-vlan-tagging

MX: create vlans -> family bridge!!! (created under edit bridge-domains)
    set bridge-domains CUSt1 vlan-id or vlan-id-list 200-204
    show bridge mac-table
CE are trunk ports   

C-VLAN normalization (mainly PEB) = rewrite C-TAG
vlan-id none -> pop C-TAG!

set bridge-domains CUST1 vlan-id none
interface ge-0/0/0.200
.201
interface ge-0/0/2.300

// interface PE-> CE
set interface ge-0/0/0 flexible-vlan-taggin
encapsulation flexible-ethernet-services
unit 200 encapsulation vlan-bridge vlan-id 200
unit 201 201

// interface to PE->P
set interface ge-0/0/2 flexible-vlan-tagging
encapsulation flexible-ethernet-services
unit 300 (S-TAG!) encapsulation vlan-bridge
vlan-tags outer 300 inner 200

S-VLAN translation. In P router, link between SP1 P and SP2 P

set interface ge-0/0/2 flexible-vlan-tagging
encapsulation flexible-ethernet-services
unit 0 family bridge interface-mode trunk vlan-id-list 300
vlan-rewrite translate INCOMING_S_TAG OUR_S_TAG

vpls: mpls, igp, 802.1q (replacement of q-in-q)

=========================================
Junos SP Switching On-Demand
=========================================

Ethernet Switching and L2

physical (show interface terse) vs logical (.x), interface family (inet, inet6, iso, mpls, etc)
ethernet ieee 802.3, single broadcast and collision domain, MAC 48bits, uses CSMA/CD
hub: collisions can occur, no csma/cd

bridgin: 802.1d-2004, segments of a single collision domain, isolates L1, FIB,

learning domain: is a DB, attaches to bridge domain 1:1,

learning: check all frames, learn MAC, src port and timing.
forwarding/flooding(BUM)/filtering/aging

show bridge mac-table

timeout = 300s, max learned MAC 393215

mac-table-size default 5120

l2 firewall filters:
set firewall family bridge filter NAME term 1 from x then y
set interface ge-0/0/0 unit 0 family bridge filter input/output FILTER

  • default: discard

VLANS and IRBs

vlan: broadcast domain
trunk: native-vlan-id
802.1q frame: 4 bytes: tag protocol: 16 bits – 0x8100,
priority: 3 bits, 802.1p
canonical format indicator: CFI = 0 (1 bit)
unique vlan id: 12 bits

vlan-id-list [100 500-505] Really Mean? – This interface accepts or outputs only VLAN IDs 100 and 500 to 505 — after translation!!
It’s the post-rewrite VLAN ID list. This is the range of VLANs that can be present after any translation occurs.
It’s bi-directional (symmetric translation).

set bridge-domains NAME vlan-id [ X y z a-b ]
set interfaces ge-1/0/0 unit 0 family brige interface-mode access vlan-id X
set interfaces ge-2/0/0 native-vlan-id x vlan-tagging unit 0 famyly bridge interface-mode trunk
vlan-id-list [ x y ] or [ x-y z a-b ]
show bridge domain [ NAME detail]

MVRP: multiple vlan registration protocol, l2 messaging protocol to automae creation and mgmt vlans. only on trunk ports.
MRP messages
set protocols mvrp no-dynamic-vlans interface ge-0/0/0.0
show mvrp statistics

IRB: integrated routing and bridging: l3 gw for a vlan.
set interfaces irb unit X description vlan-x family inet address IPx

set brige domains NAMEX vlan-id x
routing-interface irb.x

show bridge mac-table

chatgpt:
Each bridge-domain is a VLAN.
You define bridge-domains explicitly in Junos, and then map interfaces (and VLANs) to them.
Because each bridge-domain has its own MAC table, flood domain, and associated interfaces — it behaves like a mini switch inside the virtual-switch.
You can associating Multiple VLANs to One Bridge-Domain: why? Service Provider bridging where customer traffic uses many VLANs, but you want to transport all of them over a single bridge-domain — maybe because you’re mapping all of them into one L2VPN or EVPN instance

Virtual Switches (lab) ***

routing instances -> virtual router (default) or virtual switch (default-switch)

set routing-instances NAME instance-type virtual-router|virtual-switch
interface ge-0/0/0.0
bridge-domains NAMEv100 vlan-id 100
NAMEv200 vlan-id 200 routing-instance irb.1

show bridge domain
show route instance

interconnecting methods:
internal: loginal tunnel = only supported for VR -> enable in PFE:
set chassis fpc 1 pic 0 tunnel-service bandwidht 1g => that creates le-1/0/x interface!
set interfaces le-1/0/10 unit 0 peer-unit 1
vlan-id 100
….
le-1/0/10 unit 1 peer-unit 0
vlan-id 200
….

external: using physical interfaces, supported for VS and VR

Logical-Systems: LSYS – max 15, offer routing and mgmt separation
set logical-systems LSYS-1 interfaces ge-1/0/5 unit 0 family bridge interface-mode access vlan-id 100

show bridge domain logical-system LSYS-1

interconnect: via logical-tunnel or physical loop

Provider Bridging LAB ***

802.1q
vlan id 12bit= 4094

802.1ad stacking vlans: c-vlan (inner tag) = one customer vlan / s-vlan (outer tag) = service vlan represent customer

issue: mac learning form customers
s-vlan tag: tag prot id (16b 0x88A8), priority (3b), drop eligibility (1 bit, default=0), unique vlan-id (12b)
c-vlan tag: 0x8100 canonical fomat indicator

PBN = Provider Bridged Network
push, pop, swap, pop-pop, push-push, swap-swap, pop-swap, swap-push, rewrite vlan and tag-protocol-id

Learning:
-IVL: independent vlan learning: learning domain for eachc VLAN (included BUM)#
-SVL: single learning domain shared by all vlans in a bridge domain

set interfaces ge-0/0/0 flexible-vlan-tagging unit 0 vlan-id 200 // s-tag
family bridge interface-mode trunk
inner-vlan-id-list 111-114 // c-tag

set bridge-domain NAME vlan-id 200 // s-tag

customer edge port
set interfaces ge-1/0/0 vlan-tagging encapsulation flexible-ethernet-service unit 111 encapsulation vlan-bridge
vlan-id 111
input-vlan-map push vlan-id 200 // s-vlan
output-vlan-map pop
provider network port
set interfaces ge-1/0/4 stacked-vlan-tagging encapsulation flexible-ethernet-service unit 0 encapsulation vlan-bridge
vlan-tags outer 200 inner 111

set bridge-domains NAME1 interface ge-1/0/0
ge-1/0/4

vlan-id none => C-vlan pops before MAC table look-up

VPLS

for customer is just a LAN segment
PE learns MACs, MAC mapped to outbound LSP o interfaces

STP

broadcast storm, duplicated packets
slow convergence, excessive flooding, single tree
rstp: rapid
mstp: rapid and per instance
root bridge: loweest bridge id (priority + mac)
root port: port in a bride closest to the root bridge
default cost = 20k for 1G port
designated port: forwarding port on a LAN segment
BPDU: info about STP, 2sec
config: sent by root bridge
tcn: topology change notificatin: sent by any bridge towards root.

blocking: doesnt sent BPDU, but listen
convergence: 2xforwading-delay (15s) + max-age (20)

RSTP 802.1w / 802.1d-2004

in p2p links: transition to forwarding without waiting for timers to expire
edge port: if unique port in LAN, then always forwarding

new port-roles:
alternate: alternate path to root bridge (backup for root port). Block traffi while receiving superior BPDU
backup: backup of designated port. block traffic while receivng superior BPDU

states: discarding (disabled, blocking, listening // role: alternate, backup, disable), learning, forwarding (role: root, designated, edge)

bpdu: as keepalive (2s)

mx full-duplex -> port is p2p.

format: flags,

bridge id: priority (4b) + extended id (12b) + bridge address (48b)

tcn: only when non-edge (intereconnect switches) port transition to fw state. Transition to discarding doesnt trigger tcn
initiator of tcn, sends out of all designated ports and root port.

 received of tcn: doesnt flush MAC learned from edge-ports, doesnt flush MAC learned on the port receiving the TCN

MSTP 802.1s – 802.1q-2003

extension rstp
stp per vlan. MSTI. Maps 1or+ vlans to one MSTI -> load-balancing

MST region: MST switches with same region name, revision level and vlan-2-instance mapping
max 64 MSTI per region, one regional root bridge per instancce

CST = common ST, interconnects MST regions, one root bridge for CST, each MSTP region appears as a virtual bridge
IST = internal ST, STP inside the region

RSTP is used to interconnect MSTP regions or RSTP-only bridges

VSTP: similat to RSTP, 4094 instances, proprietary

Configuring STP (MSTP Lab)

show spanning-tree interface
bridge
statistics interface

set protocols rstp hello-time X max-age x forward-delay x bridge-priority x
interface ge-1/0/1 priority 128 mode point-to-point|shared cost x
edge (to host)
extended-system-id 0 (default)

set protocols mstp configuration-name REGION1 revision-level x
interface ge-1/0/0

msti 1 bridge-priority 4k vlan 100-199
msti 2 bridge-priority 8k vlan 200-299

show spanning-tree mstp configuration

set protocols vstp interface ge-1/0/1

vlan 100 bridge-priority 60k
interface ge-1/0/1

vlan 200 bridge-priority 8k
interface ge-1/0/1

bpdu protection

show spanning-tree interface

set protocols rstp interface ge-1/0/1 edge
bpdu-block-on-edge

if not rstp:
set protocols layer2-control bpdu-block interface [ ge-1/0/0 ge-1/0/1 ]

show l2-learning interface

clear error bpdu interface

  • loop protection -> on all root and alternate ports -> lack of BPDUs -> transition to “loop inconsistent state” = DIS state = blocking. Returns to origital state when receives BPDUs

set protocols rstp interface ge-1/0/1 bpdu-timeout-action block

  • root protection: on ports shouldn’t be elected as root port (shouldn’t receive supeior BPDUs) -> If receive supeior BPDU -> transition to Inconsisent state. When stops receiving BPDUs, returns to the original state.

set protocols rstp interface ge-1/0/1 no-root-port

set protocols rstp force-version stp

Ethernet OAM

Operation Administration Maintenance – OAM

availability, frame delay, frame delay variation (jitter), frame lost – 802.3-2008.clause – First mile OAM. Link Fault Management LFM

detect defects: use of continuitity check messages (CCM), unidirect and without ack, by intervals

indicators:
node detect failure -> send AIS (Alarm Indicator Signal) and FDI (Forward Defect Indicator) downstream
node received AIS/FDI -> notifies upstream devices when failure occurs in reverse direction (BDI – Backward Defect Indicator)

loopback messages:
nonintrusice loopback: like ping
intrusive loopback: signal a remote node to go into special test mode (where normal traffic can’t flow)

Link Trace Messages LTMs: like traceroute. identify nodes along the path. perform bidir continuity check

LFM: Link Fault Management is limitied to a single Ethernet link (no AIS available)
client needs to support LFM. L2, no IP needed. exchange OAM PDUs, dst MAC = 0180c2-000002 (never flooded). Discovery
Active client start the discovery
OAM PDU
codes:
0x00 information -> discovery, heartbeat (1s), Critical events
0x01 event notification -> signal link events and stats
0x02-03 variable request/response (polling MIBs) – not supported in Junos
0x04 loopback control: signal remote peer to set/unset looped interface
flags
bit 0: link fault
bit 1: dying gasp (external failure: ie power)
bit 2: critical event
bit 3-4: used during discovery

CFM: Connectivity Fault Management

  • fault monitoring using continuity check CC (neighbor discovery and health check)
  • path discovery and fault verifiation using LFM
  • fault isolation using loopbak protocol
  • frame delay measurement dst MAC, src MAC, vlan tag, type/length, CFM header, Data(TLVs), FCS maintenance domains:
    5-7: customer
    3-4: SP
    0-2: operator (subset SP network) quicker fault detection maintenacne point: Port of type:
    MEP: Maintenance End Point: edge port to edge port (protecting E-Line) or EVC (Ethernet Virtual Connect) or
    edge port to multiple edge ports (protecting E-LAN) or multipoint-to-multipoint EVC
    MIP: Maintenance Intermidiate Point: internal to a domain. Optional. Respond to CFM messages from higher level than their own
    Transparent: doesn’t respond to CFM messages task
    initiate CFM message: MEP
    respond to loopback and link trace messages: MEP, MIP
    track CCM: MEP, MIP MEP: forms neighbor exchanging CCMs with other MEPs in same maintenance domain, maintenance association, level and direction
  • Down MEP: MEP interface that faces a neighbor down MEP
  • Up MEP: MEP interface that faces away from a neighboring UP MEP CCM maintenance 0 -> dst MAC 0180C2-000030 (multicast) | 38 (link trace)
    7 7 | 3F (link trace) LBR: LoopBack Reply

Configuring OAM (LAB)

** LFM config
set protocols oam ethernet link-fault-management action-profile NAME event link-adjacency-loss (when PDU are missing)
action link-down
interfacce ge-1/3/6 apply-action-profile NAME
pdu-interval 100 (ms)
link-discovery active
pdu-threshold 10
negotiation-options allow-mode-loopbackcs
remote-loopback -> set a loop on the reote peer

show oam ethenet link-fault-management

test looped circuit

edit interface ge-1/3/5 unit 0 family inet
set address 10.0.0.0/31 arp 10.0.0.1 mac

ping 10.0.0.1 –> It seem TTL exceed that is good!!! loop works

** CFM config: customer bridge
set protocols oam ethernet connectivity-fault-management action-profile NAME event adjacency-loss
action interface-down
maintenance-domain customer leve 5
maintenance-association evc1 continuity-check internval 100ms
mep 101 inteface ge-0/0.115 vlan 115
direction down
auto-discovery
remote-mep 106
action-profile NAME
provider bridge
set protocols oam ethernet connectivity-fault-management
maintenance-domain provider leve 4
maintenance-association evc1 continuity-check internval 100ms
mep 102 inteface ge-0/0.115 vlan 115
direction up
auto-discovery
mip-half-function default

show oam ethernet connecitivty-fault-management interface ge-1/1/5.115 vlan 115 [extensive]

ping ethernet maintenance-domain customer maintenance-association evc1 mep 106

traceroute ethernet maintenance-domain customer maintenance-association evc1 mep 106

monitor ethernet delay-measurement maintenance-domain customer maintenance-association evc1 mep 106 two-way

ERP and LAG

ERP = Ethernet Ring Protection – ITU-T G.8032. Replaces STP, less 50ms recovery for ring.
RPL = Ring protection Link. RPL-owner places RPF in blocking state during normal operation. When failure, RPL-owner puts RPL in forwarding
RPL-owner sents R-APS (Ring-Automatic Protection Switching) eachc 5sec
Normal node generates R-APS whne local link failure occurs. Listen and forward R-APS
APS requires a vlan to deliver R-APS. all vlans affeccted by APS. Uses CFM frame format Opcode = 40. Flags=0. dst MAC = 0119A7-000001
Frame fields: Request/State 4bits 1011 (signal fail 0000 (no request), Reserved, RPL Blocked 1b, Do not flush 1b, Status Reserved 6b, NodeID (MAC(, Reserved

config: must have eas and west-interfacce

set protocols protection-group ethernet-ring PNAME
guar-interval x
node-id MAC
eas-interface ring-protecton-link-end
control-chnnel CHA-NAME vlan X interface
west-interface control-channel CHA-NAMe vlan X interface
ring-protectoin-link-owner

show protection-group ethernet-ring aps [detail]

LAG


802.3ad
duplex, speed, max 8 link. RE generated traffic always sent on lowest member link. IP traffic hashing uses l2-4

LACP: actor, partner (remote). Active or passive (default). you must one active end. Junos doesnt do automatic aggregation.

set chassis aggregated-devices ethernet device-count x

set interface ae0 unit 0 family bridge
aggregated-ether-options lacp active (1sec) / passive (30s)
ge-0/0/0 gigether-options 802.3ad ae0
ge-0/0/7 gigether-options 802.3ad ae0

MC-LAG and Virtual-Chassis

MC-LAG uses ICCP (Inter-Chassis Control Protocol, used TCP similar to BGP) to exchange info between nodes
active/standby or active/active (all links active, MCP cards onluy, must have a ICL link betweend devices)

set switching-options service-id X (idem in both devices)
set protocols iccp local-ip-addre IP
peer IP2 redundancy-group-id-list x
liveness-detection minimym-interval 300
multiplier 3

set interface ae0 aggregated-ether-option lacp active
periodic fast
system-id 00000000000
admin-key 1
mc-ae
mc-ae-id x
redundancy-group 1
chassis-id 0 (the other peer is 1)
status-contorl active (the other peer is standby)
mode activ-active
unit 0 family bridge interface-mode trunk
vlan-id-list XXX
multi-chassis-protection IP2 interface ge-0/0/x // the peer is: IP1 interface ge-0/0/x (only for active/active)

show iccp
show interfaces mc-ae

MX virtual-chassis:

inter chassis redundancy. VCCP, based on ISIS, MPC cards, recommend 10G interfaces for VCP ports.
primary router

Troubleshooting

show system processses

show system core-dumps
file list /var/tmp/core

edit protocols rstp
traceoptions

show chassis routing-engine

edit snmp
set health-monitor

jflowv10 – mpc card

edit services
flow-monitoring
version-ipfix
template NAME
ipv4-template

edit forwarding-options sampling
instance NAME input rate 10
run-lenght 5
max-packet-per-second 30000

edit chassis
tfeb
slot 0
sampling-instance X
inline-servies
flow-table-size
ipv4-flw-table-size 10
ipv6-flow-table-size 5 (requires reboot because by default is onlu ipv4)

show services accounting status inline-jflow

port-mirroring

edit forwarding-options port-mirroring
input rate 1
family inet output inerface ge-0//0.0 next-hop IP

edit firewall family inet
filter port-mirror
term 1
then port-mirror

ARP entry for the monitoring device

show forwarding-options port-mirroring

=========================================
Junos MPLS Fundamentalss On-Demand
=========================================

MPLS Intro

p2mp -> avoid to run multicast

MPLS Mechanics

mpls header: 32b
label: 20 bits – no 0-15: reserved special use
label 3 = implicit null = pop label before sending. this label is sent by egreess PE to neighbor
label 0 (ipv4) / 2 (ipv6 = explicit null = the neihgbor uses label 0/2
label 1 = router alert -> pop label and process packet locally, push label 1 again, it is never on the bottom of the stack
tc (traffic class or EXP): 3b
s: 1b – bottom of stack 1=it is the bottom / 0= is not the bottom and there are labels underneath
ttl: 8b – by default copied from ip ttl

LSP is unidirectional
head-end: ingress router
tail-end: egress router

PHP: penultime hop poping

inet.3 -> all ingress LSP for the router. inet.3 is used to resolve bgp NH. BGP checks inet.0 and inet.3s, the protocol with lower AD wins (RSVP/LDP is lower that ISIS OSPF etc). inet.3 is used for no-labeled traffic

** You use install active when you want regular traffic (not BGP-labeled) to use the LSP directly — for example, in LSP ping tests, or when configuring static routes using LSPs.

set protocols mpls label-switched-path R1-to-R2 to 192.0.2.2
set protocols mpls label-switched-path R1-to-R2 install 192.0.2.2/32 active

set routing-options static route 10.10.10.0/24 next-hop 192.0.2.2

show route table mpls.0 label 16 detail (mpls.0 = LFIB) – mpls.0 is used for labeled traffic. So mainly in P routers

static LSP: rare in prod networks

RSVP:
manual creation. but very powerfull

LDP:
simple. automatically creates a full mesh LSPs. Follows the best path according your IGP -> trade-off !!! for TE mainly

SR:
MPLS SR advertise labels directly in OSPF/ISIS -> no extra protocol needed! It has best-path and TE capabilities.

BGP-LU: BGP can advertise labels using special address-family. Run MPLs VPN between AS

MPLS STATIC LSP and forwarding plane

set interfaces ge-0/0/0 unit 0 family mpls (for data plane)

set protocols mpls interface ge-0/0/0.0 (for control plane) -> show mls interface (says noting about remote routers! only local)

1.000.000 – 1.400.000 statuc labels

!! unidirectioal !!
For ingress:
set protocols mpls static-label-switched-path NAME ingress next-hop PHY-IP to Lo.IP-egrees-PE push

For transit (P)
set protocols mpls static-lable-switch-path NAME transit next-hop PHY-IP2 swap

For PHP (P)
set protocols mpls static-label-switch-path NAME transit pop next-hop PHY_PE_IP

  • you can use LSP as NH for static route
    set routing-options static route NET/2x static-lsp-next-hop LSP_NAME

show mpls static-lsp ingress|transit

show route table mpls.0 (routing based on incoming labels) you may see (S=0) that’t the stack-bottom bit !!!

show route Lo.IP-egrees-PE => will show the static lsp in inet.3 !!

show route NET_advertised_by_egrees_PE [detail]

set protocols mpls icmp-tunneling => show mpls hops in traceroute

set cli logical-system X
clear cli logial-system -> back to main system

RSVP INTRO

create RSVP LSP at ingress router. Every other hop takes care by itself.

feature rich. backup standby LSP from headend (ingress), create local-repair LSP to protect from link/node failure.

ospf/isis used for advertise TE. default by isis.
TE is stored in TED.
RSVP can use TED: ERO = Explicit Route Object, created by ingress PE, and each router in path obeys ERO.
or LSDB: isis/ospf, follows best path hop by hop, no ERO

set interfaces ge-0/0/0 unit 0 family mpls (for data plane)

set protocols mpls interface ge-0/0/0.0 (for control plane) -> show mpls interface (says noting about remote routers! only local)

set protocols rsvp interface ge-0/0/0.0 (for control plane) -> show rsvp interfaces (idem) / show rsvp neighbor

enable firewall if CoP enabled!
set firewall family inet filter NAME term RSVP from protocol rsvp then accept
MPLS_PING from protocol udp port 8503 then accept.

RSVP: config basic LSP

set protocols ospf areo 0.0.0.0 interface X.0 interface-type p2p

interface lo0.0
reference-bandwidth 100g

set protocols mpls (!!!) label-switched-path NAME to lo.IP.egreess_PE no-cspf (turns-off constrained shortest path first -> dont use TED!!)

  • confirm your lsp with “Resv messages” (from destination back to origin)

show mpls lsp [name NAME] [ingress, transit, egress] [extensive]
RRO=Record route object -> each hop adds to this object to indicate the full end-to-end path. avoid loops too!

show rsvp session

show route table inet.3
show route NET/x (advertised by egrees PE)

mpls self-ping: check if lsp is ready to forward traffic because lsp are unidirect!
udp ping sent down the lsp: src: r1 dst: r1
udp ping returned as regular IP traffic (not via LSP!!!)
-needed for backup/local repair paths! if mpls self-ping doesnt work, traffic will never be moved over to these backups

hello message
path message: head-end to tail-end
resv message: tail-end to head-end. confrirm the lsp was successful

messages contain many objects.

RSVP TED

bw visibility, tag links. Every router has almost identical TED. ISIS gives hostname!! OSPF gives router Id number -> difficult!

show ted database [extensive ]
remote: 0.0.0.0 -> pseudonode = LAN

ISIS TLVs.
show isis database extensive

set protocols mpls label-switched-path NAME to lo.IP.egreess_PE

show mpls lsp [name NAME] detail -> shows ERO. Transit router never calculate an alternate path. ERO can be strict or loose
extensive -> shows CSPF

stric: hops must be directly connected.
loose: can be many hops away

set protocols mpls path NAME_PATH lo0.PEx loose
set protocols mpls path NAME_PATH lo0.PEy strict

set protocols mpls label-switched-path NAME_LSP to lo.PEz primary NAME_PATH

set protocols ospf traffic-engineering -> LSA type10 – opaque LSA, not sent outside area
show ospf database opaque-area [extensive lsa-id IP advertising-router Lo0.PE]


you can use TE features in a non-TE network but are limited
-ERO: but hops in between stil decide the best next hop
-bw reservation: if no bw, lsp can’t find another path.

RSVP LSP bw reservation

It uses CSPF
lsp priority: when there is no enough bw, the lsp with higher priority can force low priority lsp to find alternative path.
bw reservation is not a policer! it is just a reservation at control plane

manual bw reservation are hard -> auto-bw, but complicated. not showed in this curse.

set protocols mpls label-switch-path NAME1 to lo0.PE1 bandwidth Xm
show mpls lsp name NAME1 detail
show rsvp interface
show ted database extensive lo0.PE1

oversubscribe vs undersubscribe
set protocols rsvp interface ge-0/0/0.0 subscription 500 -> oversubscribe link by 5x!
1.0 bandwidth 2g -> change the total bw. For example if physical is 1g, now you say it is 2g.

monitor labeled-swith-path NAME -> show traffic stats

  • Juniper Paragon: monitor LSP

RSVP LSP Priorities

solve issue from bw reservation. first lsp gets best path. Or some LSP may not come up

  • the bin packing problem: pack big items first
    priority: 0 = best / 7 = worst. Priority only matters: 1) best path cannot offer enough bw. low-priority may not come up if no bw availabel anywhere.
    2) box has many lsp: high priority signaled first. Equal priority, signalled alphabetical order

Two values: setup priority (default 7): value used to install lsp in a path. It is compared with the hold value of other lsp
hold priority (default 0): value used to keep a lsp. this is compared with the setup priority of a contenden lsp.
=> if setup is better (lower value) than hold -> existing lsp is kicked off

  • lsp setup priority can never be better than its hold priority !!! Only preempt if better (if equal, no changes)

set protocols mpls label-switch-path NAME1 priority SETUP HOLD –> This can trigger LSP flaps !!!!s

show mpls lsp name NAME detail
show ted database Lo0.PE.NAME extensive

set groups RSVP_PRIO protocols mpls labeled-switched-path <*> priority 5 4
set apply-groups RSVP_PRIO
show configuration protocols mpls | display intheritance no-commens

default: lsp rerouting is not graceful!!! because it turns down and signals a new path
-> sol: soft-preemption
set groups RSVP_PRIO protocols mpls labeled-switched-path <*> soft-preemption
–> find a new path first, move traffic, and if good for 30s, delete old path

CSPF and Adming Groups

CSPF = Constrained Shortest Path First. Used TED. like SPF

  • compute LSPs one at a time – Start with high-priority LSP, tie braker is alphabetic order.
  • links are pruned if: not enough bw, dont contain mandatory tag or tagged to be avoided
  • strict and loose hops are considered
  • equal cost paths? -> choose the one with least hops, if still equal, choose random (default) or available bw ratio (most full path or least full path)
    100G with 60G reserved = 40% avail bw ratio
    10G with 1G reserved = 90% avail bw ratio
    least-fill -> highest avail bw ratio
    most-fill -> lowest avail bw ratio -> good to avoid bin packing prob

set protocols mpls label-switched-path NAME to IP [random, most-fill, lest-fill]

Admin Groups = affinity group, link coloring. Group link to be avoided or be used. This is unidirectional !!!

set protocols mpls admin-groups ADGROUP [0-31] -> only number is advertised !!! so you have to map the names to number in each device when writing config!!!
it is a 32-bit value, so an interface can “activate” several groups.
set protocols mpls interface ge-0/0/0.0 admin-group ADGROUP

set protocols mpls lable-switched-path LSP to PE-IP adming-group [include-any | inclide-all | exclude ] [GROUP1 GROUP2]

  • if you tag a link AFTER an LSP is up, nothing happens by default or you can configure self-optimize
    ie: to put a pure P transit router in maintenance, just add all links into “MAintenance” group and have all LSP to exclude Maintenance, and add self-optimize

LSP Failures, errors and session Maintenance

messages: (path->egress(direct path) / resv->ingress(return path))

PathTear: towards egress (direct path: downstream). Tear down LSP
ResvTear: towards ingress (return path: upstream)

PathErr: towards ingrees (upstream). Commumicate errors info
ResvErr: towards egress (downstream)

  • diffent direct from Tear messages!!! This is slow process until the ingress PE received the ResvTear and a new LSP is programmed
    sol: backup local repair lsp: pre-signaled lsp around link or node failures. each hop can generate a local repair path
    secondary path: pre-signaled and with differnt constrains from primary

overload reduction:
initial: soft-state (it was like UDP)
rfc 2961: rsvp refresh overhead reduction extensions
hello are optional: default 9sec in junos (hello-interval)

Primary and secondary path

Primary is used. several secondary paths can be defined. secondary is calculated if primary goes down.
Scondary is used until primary is recovered, after 60s.

  • constraints -> + diversity
  • constraints -> + difficult to scale

set protocols mpls path PATH1 PE1-LO loose

set protocols mpls label-switched-path LSPx to PEx-Lo primary PATH1 secondary PATH2 secondary PATH3

retry-limit: default 0 (unlimiited) – number of times will try to find a new primary
retry-timer: default 30s – time between attempts
revert-timer: default 60s – (0 = never revert) once primary is up, wait x sec before move traffic to primary
or dont define primary, and just define secondary paths for the lsp

set protocols mpls label-switched-path LSP1 secondary PATHx select manual –> secondary path comes up immediately and used for forwarding.
-> this is very manual!!1 ie: used for re-route traffic when node in maintenance -> all LSP going through that node need the “select manual”

defining secondary constraints is manual and tedious
-> sol: secondary standby paths: pre-calculated, pre-signaled and always-up. It adds a temp metric of 8M to each link used by primary path.

set protocols mpls label-switched-path LSP1 to PEx-Lo primary BLANK_PRIMARY secondary BLANK_SECONDARY standby!
set protocols mpls path BLANK_PRIMARY (without constraints!)
set protocols mpls path BLANK_SECONDARY (without constraints!)

show mpls lsp ingress LSP1 detail

trade-off: standby secondary -> double up number RSVP tunnels
if standby is configured with bw constraints -> you may artificially run out of RSVP bw.

show route IP/x detail | match “inet.0|IP|via|Push”
-> you see path for primary and secondary in RIB, but only lowest weigh route is installed in FIB!!
if you want both installed (but only the primary actually used) you need to configure LB (as per JNCIA and below)

show route forwarding-table matching IP/x extensive

enable LB in FIB:
set policy-options policy-statement LB then load-balace per-packet
set routing-options forwardinig-table export LB

Local-Repair P1: 121 backup or FRR (Fast Reroute)

protect agains link and node failure, reduce downtime -> always-on backup LSP at the “point of local repair”. Used short time until headend calculate new LSP
Local repair sens PathErr to head-end to program new lsp. It does only node protection

rfc 4090
-121 backup: 1 backup path (“detour”) for each LSP. At each hop along the path! (scale issues) pointing to the tailend!!! = Fast-Reroute
– node protection, find fastest path to tailend
– as it creates many LSP, some nodes can “merge” detour (1+ ingress) and only generate 1 egress detour

 set protocols mpls label-switched-path LSP1 to PE-IP fast-reroute [hop-limit 6=default | bandwidth 0=default | include-any GROUP]
   + CONFIGURE LB in FIB

 show mpls lsp ingress extensive
 show rsvp session detail
 show mpls lsp transit -> detour lsp uses the same name as the main lsp!!! so you can't figure out if it is a detour!
                          detour number x means the number of detour have been merged to x detour
  -> show rsvp session detail | match "Detour branch from" -> This can help you to figure out if it is a detour
                                                              and if several detour branch have the same "label out" => it is merged!

Local-Repair P2: Facility Backup or Node-Link-Protection

-facility backcup: 1 backup pth (“bypass”) for many LSP. = link-protection or node-link-protectio (better). Scales better in big networks
it is a separate, standalone LSP with its own name. The bypass LSP pushes a second label !! There is PHP to pop the second label. It doesnt signal to the tailend.

bypass to next-hop -> protects link-failure
bypass to next-next-hop -> proteccts link and node failure. trade-off: LSP is longer, may impact delay-sensitive traffic

set protocols rsvp interface ge-0/0/0.0 link-protection [on all links you want link-protection/node-protection]
set protocols mlps label-swtiched-path LSP1 to PEx-lo [link-protection | node-link-protection ]

show route IP/x [detail] -> you can see Bypass!! it has hight weight, the bottom label is the one for identifying the next-next-hop,
and top label for the bypass lsp that is the next node.
show route table inet.3
show mpls lsp
show mpls lsp bypass ingress
show rsvp sesion ingress -> all lsps included bypass

big networks -> facility backup if they support 3 labels at least, if not, use FRR
but one bypass LSP can overwhelm a link! -> setup several bypass or put bandwidht reservation for each bypass

ring topology + 121 -> ech router merges incomeing detour into its outgoing detour
+ node-protection -> traffic has to make a U-turn twice !!! (need drawing)

RSVP LSP Optimization

lsp stays in same path until tear-down or kicked-out by LSP with lower priority value
optimization -> runs CSPF periodically

global: set protocols mpls optimize-timer X (0s=never until 65535s)
indiv: set protocols mpls label-switched-path LSP1 to PE-Lo optmize-timer X

manual: clear mpls lsp name LSP optimize -> It will not clear it!!!

conditions for LSP optimization

  • new CSPF metric must not be higher than old path
  • if metrics are same, new path must no have more hops
  • new path must not cause preemption of other lsps
  • new path must not have worse “available bw ratio” (but only the 4 lowest ratios are compared in the path no matter how long)
    if least-fill used, new path should be at least 10% less than current path

set protocols mpls optimize-aggressive -> optimized purely in IGP metric!
clear mpls lsp name LSP optmize-aggressive

optmize detour and bupass:
set protocols rsvp fast-reroute optimize-timer (0..65535)

set protocols rsvp interface ge-0/0/0 link-protecton optimize-timer (0..65535)

RSVP Make-before-break and adaptive

when link/node down, temporarily, there are two copies same LSP: same name, same tunnel ID but different LSP ids -> show rsvp session

MBB: make before break: traffic is hitlessly moved to an alternative path
show mpls lsp name LSP extensive

outside scope: auto-bw and p2mp lsp (l2vpn course – vpls)

preventing double-counting of bw: this happens with two copies of the same LSP share a link. By default, routers see two copies of same LSP as tehy are different LSPs -> problem

sol: reservation style:

  • fixed filter FF: default, cannot share bw reservations. two of same lsp are treated as separate lsps
  • shared explicit SE: two of same lsps can share a bw reservation -> “adaptive” knob

set protocols mpls label-switched-path LSP1 adaptive // it enabled MBB
show rsvp session extensive name LSPx -> look for “Resv style”

map traffic to rsvp lsp:
set policy-options policy-statement MAP term T1 from route-filer IP/x exact then install-nexthop lsp LSP1 accept
next-hop ingress-PE-Lo
match bgp community list
term T2 from route-filter IP2/y exact then install-nexthop lsp LSP2 accept
term T3 then accept

set routing-options forwarding-table export MAP

show route IP/x
IP2/y

LDP – INTRO

automatic full-mesh lsp to loobacks, follows igp best path -> inet.3
No TE.
FEC = forwarding equivalence class = set of traffic is forwarded through an LSP
PE Lo0 is a FEC

RSVP: ingress router send a Path message = “downstream on deman” –>
LDP: egrees router advertises a FEC for itself unprompted = “downstream unsolicited” <–

Ordered control: junos only advertised a FEC when it has received a label downstream
Liberal label retention: junos keep all labels they receive (speeds up recovery for link/node failures)

LDP LSP are like a tree -> multipoint-to-point LSP (the top is the originating PE.lo=FEC) Every PE has a LSP to the egress = top

LDP – CONFIG

hello msg: dst IP: 224.0.0.2. it containes the lo.0 so then they can start TCP sesion. TCP started by highest Lo IP
tcp: 646

header: version:1 , LSR ID = loopbackc, Label Space ID = 0 (any label can be used)

set protocols ldp interface ge-0/0/0.0 (not needed in the loopback) (CP)
set protocols mpls interfacce ge-0/0/0.0 (CP)
set interfaces ge-0/0/0.0 unit 0 family mpls (DP)

set firewall family inet filter LO term LDP from protocol tcp udp port ldp then accept

show ldp interface [detail | extensive]
show ldp neighbor [extensive] (physical interfaces)
show ldp session [Lo.IP detail] (loopback interfaces) -> two negihbor routers, have as many neighbor as interfaces, but only one session
show ldp databse [session lo.IP]
show route table inet.3
show ldp traffic-statistics
clear ldp session/neighbor [all | lo.IP]

LDP – ENHANCEMENTS

ldp-igp sync: If not ldp in best path, mpls packets are dropped
with “ldp-synchronization”, router advertises a high metric (isis/ospf) until LDP is up. Once it is up for 10s, the metric changes to real value

set protocols isis interface ge-0/0/0.0 ldp-synchronization (only in p2p interface!!!!)

by default ldp metric = 1 -> if used with BGP multipath => LDP can LB with un-equal cost paths.
-> change that
set protocols ldp track-igp-metric

“session-protection” creates an always-up multihop LDP neighborship from loopback to loopback. The router were adjacent, but the link went down.
set protocols ldp interface lo0.0
set protocols ldp session-protection

LDP – EGRESS, IMPORT, EXPORT

egress policies: advertise other FECs apart from PE lo0.

set policy-options policy-statement LDP_EGRESS term export from route-fileter CPE-LO/32 route-filter PE-LO/32 (default term is rejecT!!!)… then accept
set protocols ldp egress-policy LDP_EGRESS

  • this will advertise all new FECs with the same transport label (aggregation) -> load-balancing is not going to be possible
    if you want LB:
    set protocols ldp deaggreate (in all routers!!!)

import/export: act on FECs that already exist
import: tag received FEC as filtered in LDP db. Prevent FEC to be imported into inet.3 and to be readvertised
set policy-options policy-statement LDP_IMPORT term block from route-fileter PEx-LO/32 then reject
term rest then accept
(** default policy for LDP is to accept all in import but make it clear)
set protocols ldp import LDP_IMPORT
show ldp database session PE-Lo
show route table inet.3 PE-Lo

export: Prevent accepted FEC to be readvertised
set policy-options policy-statement LDP_EXPORT term block from route-fileter PEx-LO/32 then reject
term rest then accept (**default ldp export policy for LDP is to reject all!!!)
set protocols ldp export LDP_EXPORT

-ldp tunneling
-ldp local repair
-ldp auth

SEGMENT ROUTING

2010 – SR or SPRING. shortest-path, TE and local-repair. Source-based routing.
SRv6: uses ipv6 headers instead of mpls labels

SR advertises labels using is-is/ospf, so all routers know the labels that every other router has assinged, router can build a stack of labels to specify an exact path: huge reduction in state -> no extra adjecencis (rsvp/ldp), TE lsp dont need to be signaled

segment = link, router, prefix, etc. each segment has SID (segment ID). All advertised by isis/ospf

Adj SID: label allocated to each link running isis/ospf. One label for ipv4 and other for ipv6
Node SID: router. populate inet.3

set interface ge-0/0/0 unit 0 family mpls (DP)
set protocols mpls interface ge-0/0/0.0 (CP)
set chassis network-services enhanced-ip => reboot !!!
set protocols isis source-packet-routing

show isis adjacency R4 detail
show isis databse R3 detail
Flags: F (family) no set = ipv4 / set = ipv6
V value
L local signigicant
P persistent SID across reboots
B backup = local-repair
S belongs to set of interfaces for unequal-cost load balancing

show route table mpls.0 label X

Controller for generating stack of labels: Juniper Paragon Pathfinder.

Replace LDP: no need label stacks, no need external controller. SR can use the same transport label at every hop.
each router allocates a block of labels (advice: configure the same block in each router)
eachc router has a uniquer id = node SID -> mpls label = node SID + starting label
*ldp generates a label for itself and for each received FEC.
*SR advertise an entire block of labels = SRGB = SR Global Block. By default SRGB=4096

set protocols isis source-packet-routing node-segment ipv4-index 405
ipv6-index 605

(same in all routers)
set protocols isis source-packet-routing srgb start-label 800000
index-range 4000 (by default is 4096) -> 50% for ipv4 and 50% for ipv6.

show route table mpls.0 label XXX

when you configure node SIDs on each router, you will find that inet.3 is automatically populated with a full mesh of shortest-paths LSP to each other router, like LDP => show route table inet.3

calculating label = next-hop router starting label + router destination SID

Antifragile

I have read “The Black Swan” some years ago and wanted to read something else from the same author.

I took me a while to read the book, it is dense and wide, but interesting nonetheless.

Antifragility can be used for all aspects of life: economy, health, etc.

As it was an ebook, I didnt take notes, but there are several things I remember. Antifragile is not too flexible (you can’t keep your form) and not too regid (dinosaur). I like a lot the example between Extremistan (our current world) vs Mediocristan (antifragile world). An antifragile world is not perfect, is built to cope with problems so you can be prepared for the big ones or avoid the big ones. It was very funny the example of Switzerland, nobody knows the president of the country, but most of us know the president of USA, UK, Germany, France, etc. Switzerland is an the example of Mediocristan at political level, the country has several cantons, they govern themselves, they deal with local issues locally. The idea, is the “too big” too fail is wrong, and size doesnt assure success or survival. War is a sensitive topic, we have big wars, because we dont have small ones, so the little issues, escalate with time and they blow up big time. I think Italy in the Renaissance is a clear example of that, Italy wasn’t a country, just powerful city-states focus in trading. There was a spike in science, art, architecture without precedent but the cities fought constantly but never to annihilation. So this is a bit curios and a bit difficult to digest. But it “could” be logical, so get stronger through difficult things: weights, problems, study, etc.

I like the references to Stoicism and other religions giving a philosophical touch to the overall argument. And it is funny the attacks in Socrates.

Another thing, that was interesting, it something has been around 100 years (books, religion, etc), it is likely to last that amount of time again. So this is a recommendation for books to read…

La Viña (p2)

I already tried this recipe but I wanted to try again. This time I followed a different video

Ingredients:
900g cream cheese (3×300 packs), room temperature
80g grana padano cheese grated (I didnt have Manchego Cheese)
150g sugar
5 whole eggs, 2 yolks
600g heavy cream (30% fat)
4 tbsp normal flour

Process:

  • Preheat oven at 250C.
  • Prepare the baking mold. User a bit of butter in the bottom. Add two sheets of baking paper, add butter so the cake doesnt stick too much
  • In a bowl, cream together the cream cheese, grated cheese and sugar. I used a hand-mixer at medium speed.
  • Add the eggs and yolks, one by one, mix with the hand-mixer. Dont add the next until fully blended.
  • Add the cream and continue mixing.
  • Add the flour and mix a bit more.
  • Pour the cheesecake into the mold.
  • Bake the cheesecake for 30 aprox, until it jiggles like gentle waves and the top is brown.
  • Let it cool down and then transfer to the fridge. Chill for 6-8 hours or overnight.
  • Take it out of the fridge for 1h or so before eating so it is at room temperature

To be honest, I dont remember how it was my first time. But it doesnt look bad at all.

I think I should have put the temperature a bit lower as the top got brown too fast and then the center too creamy.

So know yourself and your oven!!!!

I will teach you to be rich

I watched some time ago a video about the author and finally decided to read his book.

First of all, being rich has different meanings to each person. I don’t need a mansion, ferraris, 1st class plane tickets, 5* hotels, restaurants, etc.

I just dont want to worry about money in the future when I will not have the edge to work in IT and want to do something else.

I think the books doesn’t discover any magic trick. It is basic, but the funny thing, mostly people dont follow the steps in the book.

I think I have been good at saving money and not having debt, but I was always scared to invest, as I didnt want to pick stocks. But as I discovered by him, you can invest in index funds. I am looking in the long-run. The only good thing I have done regarding investment was to put money in my pension pot.

  1. Study your expensive. Make a conscious spending plan: Fixed costs (rent, utilities, debt, 50-60%), Investment (pension, ISA – 10%), Saving goals (holidays, xmas, house deposit, wedding, emergency, 5-10%), guilt-free spending (dining, party, clothing, 20-35%)
  2. Credit cards: pay your debt. find out why you are in debt. I hate credit cards. I know you can get nice perks from them but it is not for me. My expending habits, doesn’t make sense, apart from buy long-haul plane tickets. If you need done, at least, get the one with less interest.
  3. Open an account with high interest rate. Automatically put money on it.
  4. Put money in your pension pot. Max up as much as you can your tax-free allowance. Use automatic transfers.
  5. Find an investment fund in Vanguard. Dont pay high fees in managed funds!! They are a rip-off. Good source of advice danielsolin and ronlieber. Make automatic transfer into them.
  6. Review your system once a year.

If you have most your savings/investments automatically, the money left is the one you can spend guilt-free. You decide what a rich life means to you.

Honestly, these kind of things should be teach in school or at least in univerisity.

Money represents hard work and luck

1MW Rack, Google Global Network, BGP PIC, Cisco Quantum, Deep Wiki, Gemini Languages, Vielleicht

1MW rack: I had to ask ChatGPT regarding the relationship Power and voltage. Shame on me as a son of a electrician.

Power = Voltage × Current (P = V × I)

So +/-400 VDC is 800V and 1MW power -> we need 1250 A. That is provided by the rectifiers?

Voltage (volts) = water pressure

Current (amps) = flow rate (liters per second)

Power (watts) = total water delivered (pressure × flow)

You can have high water pressure (voltage), but if your pipes are too small (not enough amps), you can’t fill a swimming pool (power).

You don’t “send” amps to the rack — you make amps available, and the rack draws what it needs.

Google global network 2025: (video)

BGP PIC: Prefix Independent Convergence (PIC). In summary, it is calculating a backup path and having installed so when having a fault, the convergence is minimize. I think it is like a MPLS FRR LSPs but for BGP. Cisco and Juniper

Cisco Quantum: Everybody to the wagon. D-Wave was the first company that offered quantum systems several years ago

DeepWiki: free explanation of GitHub repositories

Gemini Little Language Lessons: Usefull, I wish I could connect it to netflix so I can have german/english subtitles at the same time 🙂

Music:

Vielleicht: yeah, vielleicht

Hi Ren: so raw, brutal, respect.

Other:

reMarkable Paper Pro: Jealous my kindle paperwhite is so behind.

CS120: Bitcoin for Developers I

This is a course that a friend recommend me. We were talking about cryptocurrencies and we noticed I was quite clueless about how bitcoin works. I am not after the economy behind, that I can’t manage to fully understand. And when I see people treating crypto investments like fiat, it doesnt help me. Anyway, I really enjoyed he course, I learned a lot of things about the power of bitcoin, the technology and history. The proof-of-work concept so “simple” and beautiful. Double-spend, decentralization, wallets, key, hashes, forth-like reverse polish notation stack-based execution language, etc.

I am not by all means a BTC dev but I think helps you to understand what is going on under the hoods.

BITCOIN DEV COURSE

1 – INTRO TO BITCOIN

1.1 – What is bitcoin

21 million – 2140 – 4y halves production – each 10 minutes, validate transactions
decentralized peer2peer net
public transaction ledger – blockchain
consensus rules
proof-of-work algo (minig) -> avoid central system, global election every 10 min to get consensus about state of transaction -> avoid double-spend

double-spend problem <- digital signatures

2008 creation

solution to distribution computing problem – byzantine generals: trying to agree on a course of action by exchanging infor over an unreliable and potentially compromised network -> proof of work algo

The longest chain not only serves as proof of the sequence of events witnessed, but proof that it came from the largest pool of CPU power.

we propose a solution to the double-spending problem using a peer-to-peer distributed timestamp server to generate computational proof of the chronological order of transactions. The system is secure as long as honest nodes collectively control more CPU power than any cooperating group of attacker nodes <!!!!!!!!!!!!!!!

electronic coin as a chain of digital signatures.

timestamp server

proof-of-work

1.1.1 -> review all points

1.2 P2P networks (the bitcoin network)

stratum protocol
node functions: routing, full blockchain DB, mining and wallet

bitcoin tcp 8333

$ bitcoin-cli getpeerinfo

1.3 Ledger entries (bitcoin transactions)

wallet -> construct transactions

The fundamental building block of a bitcoin transaction is a transaction output. Transaction outputs are indivisible chunks of bitcoin currency

Bitcoin full nodes track all available and spendable outputs, known as unspent transaction outputs, or UTXO.

very transaction represents a change (state transition) in the UTXO set.

each trans increases the UTXO set size by 1.

outputs are discrete and indivisible units of value, denominated in integer satoshis.

As a result of the indivisible nature of transaction outputs, most bitcoin transactions will have to generate change.

special type of transaction called the coinbase transaction, which is the first transaction in each block. This transaction is placed there by the “winning” miner and creates brand-new bitcoin payable to that miner as a reward for mining. This special coinbase transaction does not consume UTXO; instead, it has a special type of input called the “coinbase”. This is how bitcoin’s money supply is created during the mining process.

Transaction outputs is two parts:

  • An amount of bitcoin, denominated in satoshis, the smallest bitcoin unit
  • A cryptographic puzzle that determines the conditions required to spend the output = locking script, a witness script, or a scriptPubKey.

satoshi = 0.00000001

Trans input is four elements:

  • A transaction ID, referencing the transaction that contains the UTXO being spent
  • An output index (vout), identifying which UTXO from that transaction is referenced (first one is zero)
  • A scriptSig, which satisfies the conditions placed on the UTXO, unlocking it for spending
  • A sequence number

1.4 Intro to consensus

video about double spend – the first miner that solves the puzle advertise it and so that transaction is validated, and the second is ignored.

1.5: Decentralization: consensus algo, blockchain tech and bitcoin

video 1h 24m
sw dev
miners
exchange
wallet
merchants

1.6 Open systems

video

1.7 Exercise: loop up a trans on blockchain explorere

check blockchain viewer

study-guide

components and balance:

developers
miners
wallets
exchanges
users
merchants

exam

a transaction considered “confirmed” – When it has been included in a block

Value is transferred via ledger entries called UTXOs

Node operators decide which transactions are valid and will be propagated throughout the network.
Wallet makers decide which features they will include in their apps.

Bitcoin does not use accounts, but instead uses addresses. A transaction consumes one or more outputs from a previous transaction as input, and creates new outputs

Nodes share, network data, their inventory of blockchain data, and transactions that they have validated

=========================

Unit 2: Cryptographic Algorithms

2.1 Cryptography

btc -> hashing (one-way, create identifiiers, identify data tampering,… used in proof-of-work)
digital signatures: sign transactions. Message is signed with private key. Received using the public key of sender can check if the signature matches the message.
-> prevent forging transaction

2.2 Crypto keys

communications and transaction data are not encrypted

Ownership of bitcoin is established through digital keys, bitcoin addresses, and digital signatures.

digital keys are stored in wallet.

Most bitcoin transactions require a valid digital signature to be included in the blockchain,

The digital signature used to spend funds is also referred to as a witness,

In the payment portion of a bitcoin transaction, the recipient’s public key is represented by its digital fingerprint, called a bitcoin address,

In most cases, a bitcoin address is generated from and corresponds to a public key.

Bitcoin uses elliptic curve multiplication as the basis for its cryptography.

Bitcoin uses elliptic curve multiplication as the basis for its cryptography.

When spending bitcoin, the current bitcoin owner presents her public key and a signature (created with priv key)

k=priv key -> elliptic curve mult -> K=public key -> hashing -> A = btc address

priv key = 256-bit number. randomly.

$ bitcoin-cli getnewaddress
19rxWcjug44Xft1T1Ai11ptDZr94wEdRTz
$ bitcoin-cli dumpprivkey 19rxWcjug44Xft1T1Ai11ptDZr94wEdRTz
xxxx

or

$ bx seed | bx ec-new| bx ec-to-wif

dumpprivkey command shows the private key in a Base58 checksum-encoded format called the Wallet Import Format (WIF)

The public key is calculated from the private key using elliptic curve multiplication, which is irreversible: K=k∗G
, where k is the private key,
G is a constant point called the generator point
K is the resulting public key.

2.3 Basics of hashing

btc uses SHA256 (computing efficient, collision resistant, hide information, look random

2.4 keys and transactions signatures

hashing: hash message (static size) + priv key -> signature
verification: message + signature + public key -> yes/no

2.5 keys and btc addresses

btc addresses generated from public key. public key generated using secp256k1 curve

(x ** 3 + 7 – y**2) % p

Starting with a private key in the form of a randomly generated number k, we multiply it by a predetermined point on the curve called the generator point G
to produce another point somewhere else on the curve, which is the corresponding public key K

The generator point is specified as part of the secp256k1 standard and is always the same for all keys in bitcoin:

K = k * G

K = 1E99423A4ED27608A15A2616A2B0E9E52CED330AC530EDCC32C8FFC6A526AEDD * G

The bitcoin address is what appears most commonly in a transaction as the “recipient” of the funds.

public key + double hash: SHA256 and RIPEMD160 –> btc address

A = RIPEMD160(SHA256(K)) -> 160-bit (20-byte) address

Bitcoin addresses are almost always encoded as “Base58Check” -> uses 58 characters (a Base58 number system)

2.6 exercise

python example for enc/dec

pip3 install pycryptodome

>>> from Crypto.Cipher import PKCS1_OAEP
>>> from Crypto.PublicKey import RSA
>>> from binascii import hexlify
>>> 
>>> message = b'I love cryptography!'
>>> 
>>> private_key = RSA.generate(1024)
>>> 
>>> public_key = private_key.publickey()
>>> 
>>> private_pem = private_key.export_key().decode()
>>> public_pem = public_key.export_key().decode()
>>> 
>>> print(type(private_pem), type(public_pem))
<class 'str'> <class 'str'>
>>> 
>>> with open('private.pem', 'w') as pr:
...     pr.write(private_pem)
... 
886
>>> with open('public.pem', 'w') as pu:
...     pu.write(public_pem)
... 
271
>>> 
>>> print('private.pem:')
private.pem:
>>> with open('private.pem', 'r') as f:
...     print(f.read())
... 
-----BEGIN RSA PRIVATE KEY-----
MIIpDblbblalblalballbalblalb=
-----END RSA PRIVATE KEY-----
>>> 
>>> 
>>> print('public.pem:')
public.pem:
>>> with open('public.pem', 'r') as f:
...         print(f.read())
... 
-----BEGIN PUBLIC KEY-----
Mblballbalblablablalbalbllb
-----END PUBLIC KEY-----
>>> 
>>> 
>>> pr_key = RSA.import_key(open('private.pem', 'r').read())
>>> pu_key = RSA.import_key(open('public.pem', 'r').read())
>>> 
>>> print(type(pr_key), type(pu_key))
<class 'Crypto.PublicKey.RSA.RsaKey'> <class 'Crypto.PublicKey.RSA.RsaKey'>
>>> 
>>> cipher = PKCS1_OAEP.new(key=pu_key)
>>> cipher_text = cipher.encrypt(message)
>>> 
>>> print(cipher_text)
b"\xaaok\x1b/\xab\xe8A\xdf\xc9\xb3\xbb\xc4m\xa0\xe7\xc6\xae2\x87\xc9\xa9\x85?\xa27\xe0V\x85\xd9\xa7\x07h\xcb<\xfejm\x8c{\xcf\x94\xdf\x15f\xb6\xc1+\xa9\x90V\xa1\xb6zS\xe3\xf8n\x8d\x83\x95+\x8b\xee3o\x1d\x1d\xc6n\x05\x89\xf9'E\x1c|\xf3\x9c\x8d\x04\xfa{\xe3\x1b\xe2\xeb'q\xd3\xe1\x81\xd6c\x804\xcd\xf8@+\xfeU\xd5Lf0\xac\x1f\xce\x88\x82\x8b4\x03\x0c\xf2gy\x87\x9fy\x9c2\xb4 uGl"
>>> 
>>> 
>>> decrypt = PKCS1_OAEP.new(key=pr_key)
>>> decrypted_message = decrypt.decrypt(cipher_text)
>>> 
>>> print(decrypted_message)
b'I love cryptography!'
>>> 
>>> quit()

video

squiz

=========================

Unit 3: Signatures and Transactions

3.1: What is a Digital Signature?

Elliptic curve cryptography: there is addtion and multiplication, but there is no substraction and division -> you can get recover the original data (1 way function!)s
-> proof you have the priv key without revealing it

3.2: Exercise: Sign and Validate Data

pip3 install ecdsa

from ecdsa import SigningKey, SECP256k1
sk = SigningKey.generate(curve=SECP256k1)
print(sk)

vk = sk.verifying_key
print(vk)

signature = sk.sign(b”Not your keys, not your coins!”)
print(signature)

assert vk.verify(signature, b”Not your keys, not your coins!”)

print(“If your script runs to this point without an error, congrats, you successfully validated the signature!”)

3.3: Introduction to Bitcoin Transactions

command-line interface (getrawtransaction and decoderawtransaction)

The fundamental building block of a bitcoin transaction is a transaction output

Bitcoin full nodes track all available and spendable outputs, known as unspent transaction outputs, or UTXO

Every transaction represents a change (state transition) in the UTXO set.

A user’s wallet has “received” bitcoin, what we mean is that the wallet has detected on the blockchain an UTXO that can be spent with one of the keys controlled by that wallet.

The concept of a balance is created by the wallet application. The wallet calculates the user’s balance by scanning the blockchain and aggregating the value of any UTXO the wallet can spend with the keys it controls.

Notice how each transaction spends an UTXO that was created in a previous transaction, turning it into a spent transaction output, or STXO.

transactions #2 and #3 both create change outputs to the sender, spending a single UTXO and creating two UTXO (the payment and the change output).

bitcoin can be divided down to eight decimal places as satoshis.

outputs are discrete and indivisible units of value, denominated in integer satoshis. An unspent output can only be consumed in its entirety by a transaction.

If an UTXO is larger than the desired value of a transaction, it must still be consumed in its entirety and change must be generated in the transaction. In other words, if you have an UTXO worth 20 bitcoin and want to pay only 1 bitcoin, your transaction must consume the entire 20-bitcoin UTXO and produce two outputs: one paying 1 bitcoin to your desired recipient and another paying 19 bitcoin in change back to your wallet. As a result of the indivisible nature of transaction outputs, most bitcoin transactions will have to generate change.

special type of transaction called the coinbase transaction, which is the first transaction in each block. This transaction is placed there by the “winning” miner and creates brand-new bitcoin. coinbase transaction does not consume UTXO;

example transaction output:

  • the transaction contains two outputs. Each output is defined by a value and a cryptographic puzzle (determines the conditions required to spend the output)

little-endian (least-significant-byte-first)

Transaction inputs identify (by reference) which UTXO will be consumed and provide proof of ownership through an unlocking script.

Sometimes one UTXO is enough, other times more than one is needed.

For each UTXO that will be consumed to make this payment, the wallet creates one input pointing to the UTXO and unlocks it with an unlocking script.

Most often, the unlocking script is a digital signature and public key proving ownership of the bitcoin.

output index (vout), identifying which UTXO from that transaction is referenced (first one is zero)

3.4: How Bitcoin Transactions Use Signatures

Elliptic Curve Digital Signature Algorithm, or ECDSA

ECDSA is used by the script functions OP_CHECKSIG, OP_CHECKSIGVERIFY, OP_CHECKMULTISIG, and OP_CHECKMULTISIGVERIFY

A digital signature serves three purposes in bitcoin:
1- the signature proves that the owner of the private key, who is by implication the owner of the funds, has authorized the spending of those funds
2- the proof of authorization is undeniable (nonrepudiation)
3- the signature proves that the transaction (or specific parts of the transaction) have not and cannot be modified by anyone after it has been signed.

each transaction input is signed independently.

A digital signature is a mathematical scheme that consists of two parts:
1- an algorithm for creating a signature, using a private key (the signing key), from a message (the transaction).
2- an algorithm that allows anyone to verify the signature, given also the message and a public key.

Sig = Fsig(Fhash(message),priv-key)
Sig = (R, S)

The SIGHASH flag is a single byte that is appended to the signature. Every signature has a SIGHASH flag and the flag can be different from input to input.

There are three SIGHASH flags:
ALL: 0x01 signature applies to all inputs and outputs
NONE: 0x02 signature applies to all inputs, none of outputs (bearer check or blank check)
SINGLE: 0x03 signature applies to all inputs but only the one output with the same index number as the signed input

When ANYONECANPAY is set, only one input is signed, leaving the rest (and their sequence numbers) open for modification

ALL|ANYONECANPAY 0x81 Signature applies to one input and all outputs (crowdfunding)
NONE|ANYONECANPAY 0x82 Signature applies to one input, none of the outputs (dust collector = Users who have tiny UTXO in their wallets can’t spend these because the cost in fees exceeds the value of the dust)
SINGLE|ANYONECANPAY 0x83 Signature applies to one input and the output with the same index number

All SIGHASH types sign the transaction nLocktime field. In addition, the SIGHASH type itself is appended to the transaction before it is signed, so that it can’t be modified once signed.

SIGHASH_ALL flag is the most common signature form

the signature generation algorithm uses a random key k, as the basis for an ephemeral private/public key pair. The value of k is not important, as long as it is random.
The industry-standard algorithm for deterministic initialization of k is defined in RFC 6979

-locking scripts and transactions verification (video)

locking (pub key of the receiver and signature) <- sending funds

unlocking: your pub key and a signature created with private <- use funds

Review – view

locking: send funds
unlocking:

input: using uxto (we dont show the value, of 0.5 to pay 0.4) + unlocking script
output: 0.4 for the merchant + locking script with user public key
0.2 for me as the change + locking script with my public key

quiz

Validating a digital signature requires the original message, the signature, and the public key.
A digital signature is created using a private key and the message to be signed

=========================

Unit 4: Hashing and Mining

4.1: What is a Hash? video

hash -> 256bit (compression), one-way (no-reverse), no-collision, deterministic (same entry, same output)s.

4.2: Hashing Algorithms

Applications:

  • verify integrity of messages/files (md5, sha-2, etc)
  • signature generation and verification
  • password verification: only store the hash digest of each password. To authenticate a user, the password presented by the user is hashed and compared with the stored hash. A password hash requires the use of a large random, non-secret salt value which can be stored with the password hash. The salt randomizes the output of the password hash, making it impossible for an adversary to store tables of passwords and precomputed hash values to which the password hash digest can be compared. The output of a password hash function can also be used as a cryptographic key. Password hashes are therefore also known as password-based key derivation functions (PBKDFs).
  • proof-of-work: economic measure to deter DoS attacks. asymmetry: the work must be moderately hard (but feasible) on the requester side but easy to check for the service provider. The sender is required to find a message whose hash value begins with a number of zero bits. The average work that the sender needs to perform in order to find a valid message is exponential in the number of zero bits required in the hash value, while the recipient can verify the validity of the message by executing a single hash function. For instance, in Hashcash, a sender is asked to generate a header whose 160-bit SHA-1 hash value has the first 20 bits as zeros. The sender will, on average, have to try 219 times to find a valid header.
  • file or data identifier

Algos:
md5: 128-b broken
sha-1: 160-b broken
ripemd-160: 160-b not broken
whirlpool: 512-b
sha-2: sha-256 and sha-512 (faster in 64b systems)
sha-3: configurable output size
blake2: no standard, faster than sha-3.
blake3: single algo. merkle tree

4.3: Exercise: Hash Something

import hashlib

mystring = 'Python is fun!'
print('Your string is:', mystring)

myhash = hashlib.sha256(mystring.encode())

print('Your SHA256 hash is:', myhash.hexdigest())

print('The length of your hash is:', len(myhash.hexdigest()))

4.4: Hashing and Bitcoin Mining

mining and consensus

Mining is the mechanism that underpins the decentralized clearinghouse, by which transactions are validated and cleared.
Mining is the invention that makes bitcoin special, a decentralized security mechanism that is the basis for P2P digital cash.

Miners validate new transactions and record them on the global ledger. A new block, containing transactions that occurred since the last block, is “mined” every 10 minutes on average, thereby adding those transactions to the blockchain. Transactions that become part of a block and added to the blockchain are considered “confirmed,” which allows the new owners of bitcoin to spend the bitcoin they received in those transactions.

Miners receive two types of rewards in return for the security provided by mining: new coins created with each new block, also known as a block reward or coinbase reward, and transaction fees from all the transactions included in the block. To earn this reward, miners compete to solve a difficult mathematical problem based on a cryptographic hash algorithm. The solution to the problem, called the Proof-of-Work, is included in the new block and acts as proof that the miner expended significant computing effort. The competition to solve the Proof-of-Work algorithm to earn the reward and the right to record transactions on the blockchain is the basis for bitcoin’s security model.

Deflationary Money: (21m max)

decentralized mechanism for emergent consensus (asynchronous)

Independent verification of each transaction, by every full node, based on a comprehensive list of criteria
Independent aggregation of those transactions into new blocks by mining nodes, coupled with demonstrated computation through a Proof-of-Work algorithm
Independent verification of the new blocks by every node and assembly into a chain
Independent selection, by every node, of the chain with the most cumulative computation demonstrated through Proof-of-Work

transacction validation: conditions can be seen in detail in the functions AcceptToMemoryPool, CheckTransaction, and CheckInputs in Bitcoin Core

$ bitcoin-cli getblockhash 277316
xxxx
$ bitcoin-cli getblock xxxx
yyyy

Mining Fees, Block Data, Block Headers, and Proof-of-Work


The Coinbase Transaction: The first transaction in any block. The coinbase transaction does not consume (spend) UTXO as inputs. Instead, it has only one input, called the coinbase, which creates bitcoin from nothing. The coinbase transaction has one output, payable to the miner’s own bitcoin address.

Total Fees = Sum(Inputs) – Sum(Outputs)

COIN constant (100,000,000 satoshis).

Miner only gets to spend the reward if the block is accepted by everyone.

block header: version, previous block hash, merkle root, timestamp, target, nonce

mining is the process of hashing the block header repeatedly, changing one parameter, until the resulting hash matches a specific target.

In SHA256 output of a script for generating many hashes by iterating on a nonce, the winning “nonce” is 13 and this result can be confirmed by anyone independently. Anyone can add the number 13 as a suffix to the phrase “I am Satoshi Nakamoto” and compute the hash, verifying that it is less than the target. The successful result is also Proof-of-Work, because it proves we did the work to find that nonce

Proof-of-Work must produce a hash that is equal to or less than the target. A higher target means it is less difficult to find a hash that is equal to or below the target. A lower target means it is more difficult to find a hash equal to or below the target. The target and difficulty are inversely related.

The difficulty of mining a bitcoin block is approximately ’10 minutes of processing’ for the entire network, based on the time it took to mine the previous 2016 blocks, adjusted every 2016 blocks. This is achieved by lowering or raising the target.

he primary influence on the mining market is the price of one kilowatt-hour of electricity in bitcoin, because that determines the profitability of mining and therefore the incentives to enter or exit the mining market.

Bitcoin can scale up, achieve broader adoption, and remain secure without any increase in hashing power from today’s level. The increase in hashing power represents market forces as new miners enter the market to compete for the reward.

https://www.youtube.com/watch?v=sdhb1ppD_aU — Bitcoin Q A How Is the Number of Zeros in the Target Hash Determined

4.5 Exercise: Try a Mining Simulator

https://andersbrownworth.com/blockchain/block

4.6: Merkle Trees

Each block in the bitcoin blockchain contains a summary of all the transactions in the block using a merkle tree = binary hash tree

A merkle tree is constructed by recursively hashing pairs of nodes until there is only one hash, called the root, or merkle root.

double-SHA256.

The merkle tree is constructed bottom-up.

Consecutive pairs of leaf nodes are then summarized in a parent node, by concatenating the two hashes and hashing them together

To prove that a specific transaction is included in a block, a node only needs to produce log~2~(N) 32-byte hashes,
??????????????

Video

use of hashges: sw integrity, digital signatures, password verification, data identifier, proof-of-work (email spam origina concept)

how to check hash L is in a block? you get specific hashes to proof to can get to the root hash

========================

Unit 5: Bitcoin Data

5.1: How Bitcoin Transmits Data on the Network

Peer-to-Peer Network Architecture

bitcoin p2p protocol, stratum protocol (mining and wallets)

Node Types and Roles

functions: net routing (N), DB (B), mining (M), wallet (W)

SPV – simplifief payment verification

Fast Internet Bitcoin Relay Engine or FIBRE – udp relay network: minimize the latency in the transmission of blocks between miners.

Network Discovery: tcp 8333
nversion (bitcoin p2p protocol), nlocalservics, ntime, addryou, addrme, subver (sw version satoshixxx), bestheight (block height)

The Bitcoin Core client contains the names of nine different DNS seeds.

Bitcoin Core, also known as the Satoshi client.

SVP nodes

SPV nodes download only the block headers and do not download the transactions included in each block
SPV nodes verify transactions using a slightly different method that relies on peers to provide partial views of relevant parts of the blockchain on demand.

SPV: denial-of-service attack or for a double-spending attack against SPV nodes
vulnerable to network partitioning attacks or Sybil attacks

For infallible security, however, nothing beats running a full blockchain node.

A full blockchain node verifies a transaction by checking the entire chain of thousands of blocks below it in order to guarantee that the UTXO is not spent, whereas an SPV node checks how deep the block is buried by a handful of blocks above it.

Bloom filters allow SPV nodes to receive a subset of the transactions without revealing precisely which addresses they are interested in, through a filtering mechanism that uses probabilities rather than fixed patterns. A bloom filter is a probabilistic search filter that offers an efficient way to express a search pattern while protecting privacy.

Explanation of how bloom filter works

1 patter
M hash functions: each produce a value between 1 and N
N-bits array.

To test if a pattern is part of a bloom filter, the pattern is hashed by each hash function and the resulting bit pattern is tested against the bit array. If all the bits indexed by the hash functions are set to 1, then the pattern is probably recorded in the bloom filter.

On the contrary, if a pattern is tested against the bloom filter and any one of the bits is set to 0, this proves that the pattern was not recorded in the bloom filter.
A negative result is not a probability, it is a certainty. In simple terms, a negative match on a bloom filter is a “Definitely Not!”

An SPV node will initialize a bloom filter as “empty”; in that state the bloom filter will not match any patterns. The SPV node will then make a list of all the addresses, keys, and hashes that it is interested in. It will do this by extracting the public key hash and script hash and transaction IDs from any UTXO controlled by its wallet. The SPV node then adds each of these to the bloom filter, so that the bloom filter will “match” if these patterns are present in a transaction, without revealing the patterns themselves.

The SPV node will then send a filterload message to the peer, containing the bloom filter to use on the connection. On the peer, bloom filters are checked against each incoming transaction. The full node checks several parts of the transaction against the bloom filter, looking for a match including:

The transaction ID
The data components from the locking scripts of each of the transaction outputs (every key and hash in the script)
Each of the transaction inputs
Each of the input signature data components (or witness scripts)

The network protocol and bloom filter mechanism for SPV nodes is defined in BIP-37 (Peer Services).

As a way to increase the privacy and security of the bitcoin P2P network, there are two solutions that provide encryption of the communications: Tor Transport and P2P Authentication and Encryption with BIP-150/151.

$ bitcoind –daemon –debug=tor

BIP-150 (Peer Authentication) and BIP-151 (Peer-to-Peer Communication Encryption).
As of February 2021, BIP-150 and BIP-151 are not implemented in Bitcoin Core

Almost every node on the bitcoin network maintains a temporary list of unconfirmed transactions called the memory pool, mempool, or transaction pool.

the transaction and orphan pools only contain unconfirmed transactions, while the UTXO pool only contains confirmed outputs.

5.2: Bitcoin Addresses and Keys

https://github.com/bitcoinbook/bitcoinbook/blob/develop/ch04_keys.adoc

WIF = wallet import format

base58

Base58 is Base64 without the 0 (number zero), O (capital o), l (lower L), I (capital i), and the symbols: 123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz

Base58Check is a Base58 encoding format: The checksum is an additional four bytes added to the end of the data that is being encoded

version = differnt values depending on the data/payload

checksum = SHA256(SHA256(version+payload))

base58check = BASE58(version+payload+checksum)

From the resulting 32-byte hash (hash-of-a-hash), we take only the first four bytes. These four bytes serve as the error-checking code, or checksum.

              HEX    Base58

btc address -> 0x00 1
pay2script add -> 0x05 3
btc testnet add 0x6F m/n
priv key WIF 0x80 5/K/L
BIP-38 enc priv k 0x0142 6P
BIP-32 ext pub k 0x0488B21E xpub

priv key: raw (32 bytes) hex (64 hex digits), wif, wif-compressed

$ bx base58check-decode xxx
$ bx base58check-encode xxx –version zzz

compress -> append “01” to xxx

uncompressed public keys have a prefix of 04, compressed public keys start with either a 02 (even) or a 03 (odd) prefix (because y point in ellipti graph can have two values)

public key (x,y) -> 04xy uncompressed -> 02x (y even) or 03x (y odd) ==> uncompressed / compressed produces two different btc addresses!!!

priv key:
wif -> 5xxxx
wif-compress -> K/Lxxx

“Compressed private keys” is a misnomer! They are not compressed; rather, WIF-compressed signifies that the keys should only be used to derive compressed public keys and their corresponding bitcoin addresses. Ironically, a “WIF-compressed” encoded private key is one byte longer because it has the added 01 suffix to distinguish it from an “uncompressed” one.

https://github.com/huntrontrakkr/bitcoinbook/blob/master/key-to-address-ecc-example.py
https://github.com/huntrontrakkr/bitcoinbook/blob/master/ec-math.py

Bitcoin addresses that begin with the number “3” are pay-to-script hash (P2SH) addresses –> “joint account”
traditional “1” bitcoin addresses, also known as a pay-to-public-key-hash (P2PKH)

Vanity addresses are valid bitcoin addresses that contain human-readable messages: 1LoveBPzxxxx
Vanity addresses can be used to enhance and to defeat security measures; they are truly a double-edged sword.

Paper wallets are an OBSOLETE technology and are dangerous for most users.

Data Encoding

With Bitcoin, every byte in a transaction incurs a cost.

PEM or Privacy-Enhanced Mail format was developed for storing public and private keys in files. it uses base64

ASN.1 is a standard for data transmission, cross platform. It is used for the exchange of data between systems and is independent of any particular computer or programming language, it is also both human-readable and machine-readable.

DER or Distinguished Encoding Rules is a binary format. DER is intended for applications in which a unique octet(8-bit byte) string encoding is needed, which is the case with cryptographic signatures.

Endianness “A big-endian system stores the most significant byte of a word at the smallest memory address and the least significant byte at the largest.

In DER encoding data is stored in a Tag-Length-Value format.

5.3: Hex Encoding

base 16

5.4: How Bitcoin Stores Data in the Block Structure

The blockchain data structure is an ordered, back-linked list of blocks of transactions
The Bitcoin Core client stores the blockchain metadata using Google’s LevelDB database.
Blocks are linked “back,” each referring to the previous block in the chain.
first block ever created, known as the genesis block.

structure block: 4B block size, 80B block header, 1-9B trans counter, x trans

block header: 4B version, 32B previous block hash, 32B Merkle Root, 4B timestamp, 4B difficulty target, 4B nonce (proof-of-work)

The primary id of a block = hashing the block header twice through the SHA256 algorithm.
Note that the block hash is not actually included inside the block’s data structure,
The block hash might be stored in a separate database table as part of the block’s metadata, to facilitate indexing and faster retrieval of blocks from disk.

A second way to identify a block is by its position in the blockchain, called the block height.
The block height is also not a part of the block’s data structure;

5.5: Exercise: View Block Data

https://bitcoindev.network/bitcoin-cli-sandbox

Mainnet: This is THE Bitcoin network. Transactions on this network have real value. Testing any applications here is highly risky.

Testnet: This is a separate, much smaller network, designed specifically for testing. Transactions on this network exchange testnet coins. You can acquire testnet bitcoins at a testnet faucet such as https://testnet-faucet.mempool.co/. Testnet coins, by design, have no monetary value. They are simply exchanged between developers who are testing Bitcoin applications.

Regtest: This is an entirely local network. You could create a regtest network in your home for you and your roommates. Or set one up at the office, etc. It does not link to mainnet or testnet in any way. It is your own private Bitcoin network.

./bin/bitcoind -regtest -daemon
./bin/bitcoin-cli -regtest stop

./bin/bitcoin-cli -regtest createwallet "wallet1"
./bin/bitcoin-cli -regtest getwalletinfo
./bin/bitcoin-cli -regtest getnewaddress
./bin/bitcoin-cli -regtest generatetoaddress 101 ADDRESS /// create 101 coins/blocks to the ADDRESS
./bin/bitcoin-cli -regtest getblock BLOCKHASH
./bin/bitcoin-cli -regtest getbalance
./bin/bitcoin-cli -regtest -getinfo
./bin/bitcoin-cli -regtest getblockcount

~/btc/bitcoin-27.0$ ./bin/bitcoind
Bitcoin Core starting
go:system|py:btc|tomas@athens:~/btc/bitcoin-27.0$ lsof -i -P -n
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
cli 1720 tomas 10u IPv4 31050 0t0 TCP 127.0.0.1:54150 (LISTEN)
chrome 88292 tomas 460u IPv4 421797 0t0 UDP 224.0.0.251:5353
chrome 88349 tomas 19u IPv4 437188 0t0 TCP 192.168.0.86:48570->34.117.244.60:443 (ESTABLISHED)
chrome 88349 tomas 40u IPv6 417531 0t0 TCP [2a02:8109:b00a:1200:cb1b:f32a:9f9e:48db]:45718->[2a00:1450:4025:c01::bc]:5228 (ESTABLISHED)
bitcoind 96069 tomas 11u IPv6 443969 0t0 TCP [::1]:18443 (LISTEN)
bitcoind 96069 tomas 12u IPv4 443971 0t0 TCP 127.0.0.1:18443 (LISTEN)
bitcoind 96069 tomas 24u IPv4 437239 0t0 TCP 127.0.0.1:18445 (LISTEN)
bitcoind 96069 tomas 25u IPv6 437240 0t0 TCP *:18444 (LISTEN)
bitcoind 96069 tomas 27u IPv4 437241 0t0 TCP *:18444 (LISTEN)
~/btc/bitcoin-27.0$

https://live.blockcypher.com/btc

5.6: Exercise: Convert Data between Decimal, Base58, and Hex

mybase58 = base58.b58encode(mystring)
base58.b58decode()

$ openssl genrsa -out private.pem
$ openssl rsa -in private.pem -out privkey.der -outform der
xxd -p privkey.der

VIDEO

quiz

=====================

Unit 6: Bitcoin Nodes and Wallets

6.1: What a Node Does

node runs btc protocol to advertise blocks and transaction (it is a verification authority) – verify each transaction and block. node sends bad info is isolated
miners create the blocks but nodes validating

6.2: Who Runs a Node and Why?

nodes have diffenrent roles. you run a node, you improve your privacy and participate in the consensus process and validating transactions.

6.3: Exercise: Set Up a Regtest Node

6.4: The Functions of a Bitcoin Wallet (SUPER LONG!!!!)

wallet controls access to a user’s money, managing keys and addresses, tracking the balance, and creating and signing transactions.

the word “wallet” refers to the data structure used to store and manage a user’s keys.

Bitcoin wallets contain keys, not coins. The coins are stored on the blockchain in the form of transaction outputs (often noted as vout or txout).

nondeterministic wallet: JBOK – “Just a Bunch Of Keys”. is discouraged for anything other than simple tests. They are simply too cumbersome to back up and use.
deterministic wallet: are initialized from a random sequence = English words

HD Wallets (BIP-32/BIP-44): tree structure for generating keys + create a sequence of public keys without having access to the corresponding private keys

Seeds and Mnemonic Codes (BIP-39)

BIP-39 defines the creation of a mnemonic code and seed, which we describe here in nine steps. For clarity, the process is split into two parts: steps 1 through 6 are shown in Generating mnemonic words and steps 7 through 9 are shown in From mnemonic to seed.

1 Create a random sequence (entropy) of 128 to 256 bits.
2 Create a checksum of the random sequence by taking the first (entropy-length/32) bits of its SHA256 hash.
3 Add the checksum to the end of the random sequence.
4 Split the result into 11-bit length segments.
5 Map each 11-bit value to a word from the predefined dictionary of 2048 words.
6 The mnemonic code is the sequence of words.

The mnemonic words represent entropy with a length of 128 to 256 bits. The entropy is then used to derive a longer (512-bit) seed through the use of the key-stretching function PBKDF2. The seed produced is then used to build a deterministic wallet and derive its keys.

7 The first parameter to the PBKDF2 key-stretching function is the mnemonic produced from step 6.
8 The second parameter to the PBKDF2 key-stretching function is a salt. The salt is composed of the string constant “mnemonic” concatenated with an optional user-supplied passphrase string.
9 PBKDF2 stretches the mnemonic and salt parameters using 2048 rounds of hashing with the HMAC-SHA512 algorithm, producing a 512-bit value as its final output. That 512-bit value is the seed.

Providing more than 12 words produces additional entropy which is unnecessary, and this unused entropy is not used for the derivation of the seed in the way that one might intially suspect. From a usability perspective, 12 words is also easier to write down, back up, and store.

There are no “wrong” passphrases in BIP-39. Every passphrase leads to some wallet, which unless previously used will be empty.

While passphrases are very useful, they should only be used in combination with a carefully planned process for backup and recovery, considering the possibility of surviving the owner and allowing his or her family to recover the cryptocurrency estate.

Every key in the HD wallet is deterministically derived from this root seed, which makes it possible to re-create the entire HD wallet from that seed in any compatible HD wallet.

The child key derivation functions are based on a one-way hash function that combines:
A parent private or public key (ECDSA compressed key)
A seed called a chain code (256 bits)
An index number (32 bits)

These three items (parent key, chain code, and index) are combined and hashed to generate children keys, as follows

Child private keys are indistinguishable from nondeterministic (random) keys.

So what can the child private key be used for on its own? It can be used to make a public key and a bitcoin address. Then, it can be used to sign transactions to spend anything paid to that address.

: An extended key consists of a private or public key and chain code. An extended key can create children, generating its own branch in the tree structure. Sharing an extended key gives access to the entire branch.

. The Base58Check coding for extended keys uses a special version number that results in the prefix “xprv” and “xpub” when encoded in Base58 characters to make them easily recognizable

As mentioned previously, a very useful characteristic of HD wallets is the ability to derive public child keys from public parent keys, without having the private keys. This gives us two ways to derive a child public key: either from the child private key, or directly from the parent public key.

A single leaked child private key, together with a parent chain code, reveals all the private keys of all the children -> The hardened derivation function looks almost identical to the normal child private key derivation, except that the parent private key is used as input to the hash function, instead of the parent public key

The index number used in the derivation function is a 32-bit integer.
the index number is less than 2^31, the child is normal, whereas if the index number is equal or above 2^31, the child is hardened.

Each parent extended key can have 4 billion children: 2 billion normal children and 2 billion hardened children. Each of those children can have another 4 billion children, and so on.

BIP-44 specifies the structure as consisting of five predefined tree levels:
m / purpose’ / coin_type’ / account’ / change / address_index

Tip: Gap limits explain the phenomenon whereby the importing of a wallet may show an incorrect or zero balance. The funds are not lost, but rather, the wallet importing function has not traversed enough leaves to fully detect funds. Many wallets allow this default gap limit to be changed,

6.5: Exercise: View Your Node Wallet Data

~/btc/bitcoin-27.0$ ./bin/bitcoin-cli -regtest createrawtransaction '[{"txid": "blablatxid", "vout": 0}]' '[{"addresstosent": 0}]'
blablabla

~/btc/bitcoin-27.0$ ./bin/bitcoin-cli -regtest signrawtransactionwithwallet blablabla
{
"hex": "blablablasigned",
"complete": true
}

~/btc/bitcoin-27.0$ ./bin/bitcoin-cli -regtest sendrawtransaction blablablasigned
error code: -26
error message:
dust

$ bitcoin-cli gettransaction [our transaction ID]

6.6 Video

determinustic wallet: just backup the seed to recover the keys
hierarchical determistic HD wallet: 1 seed -> 1 master key -> x child keys -> each child key generates x granchild keys

BIP-39: mnemonic code words + salt -> 2048 rounds of hmac-sha512 -> 512b seed

BIP-44: derivation path: m / purpose (44) / coin_type (0=btc 2=litcoin) / account’ / change (receiving and change address) / address_index

xpub key:

hot wallet: mobile phone (internet access), cold wallet: usb key (no connection to internet)
custodial (coinbase) vs non-custodial (I keep the keys)

wallet:
generate/store keys,
construct transactions
sign transactions with keys
track tr7.1: What’s in a Transaction?
Wansactions

quiz

Unit 7: Transactions and Scripting

7.1: What’s in a Transaction?

Transactions are the most important part of the bitcoin system.
Everything else in bitcoin is designed to ensure that transactions can be created, propagated on the network, validated, and finally added to the global ledger
Transactions are data structures that encode the transfer of value between participants in the bitcoin system.
Each transaction is a public entry in bitcoin’s blockchain, the global double-entry bookkeeping ledger.

Transaction Fees

Most transactions include transaction fees, which compensate the bitcoin miners for securing the network.
Fees also serve as a security mechanism themselves, by making it economically infeasible for attackers to flood the network with transactions.
Transaction fees are calculated based on the size of the transaction in kilobytes,
Transaction fees are not mandatory
Fee estimation algorithms: https://bitcoinfees.earn.com/ (old not working)
The data structure of transactions does not have a field for fees. Instead, fees are implied as the difference between the sum of inputs and the sum of outputs.
Fees=Sum(Inputs)–Sum(Outputs)

Warning: If you forget to add a change output in a manually constructed transaction, you will be paying the change as a transaction fee. Saying “Keep the change!” to the miner might not be what you really intended

7.2: The Transaction Timeline

video

7.3: Transaction Scripting

When a transaction is validated, the unlocking script in each input is executed alongside the corresponding locking script to see if it satisfies the spending condition.
“programmable money” -> because the Script language

there are no loops or complex flow control capabilities other than conditional flow control -> not Turing Complete -> scripts have limited complexity and predictable execution times

The bitcoin transaction script language is stateless -> all the info needed to execute a script is contained within the script -> A script will predictably execute the same way on any system.

Script Construction (Lock + Unlock)

  • A locking script is a spending condition placed on an output: it specifies the conditions that must be met to spend the output in the future (scriptPubKey)
  • An unlocking script is a script that “solves,” or satisfies, the conditions placed on an output by a locking script and allows the output to be spent (scriptSig). Unlocking scripts are part of every transaction input.

Every bitcoin validating node will validate transactions by executing the locking and unlocking scripts together. Each input contains an unlocking script and refers to a previously existing UTXO. The validation software will copy the unlocking script, retrieve the UTXO referenced by the input, and copy the locking script from that UTXO. The unlocking and locking script are then executed in sequence. The input is valid if the unlocking script satisfies the locking script conditions

most common type of bitcoin transaction (a payment to a public key hash) = Pay-to-Public-Key-Hash (P2PKH)

(forth-like reverse polish notetion stack-based execution language)
Bitcoin’s scripting language is called a stack-based language – stack = LIFO (last-in, first-out)
Transactions are invalid if the top value on the stack is FALSE (a zero-length empty value, or if script execution is halted explicitly by an operator, such as OP_VERIFY,
OP_RETURN, or a conditional terminator such as OP_ENDIF.
The scripts are executed separately with the stack transferred between the two executions, as described next.

First, the unlocking script is executed, using the stack execution engine. If the unlocking script is executed without errors (e.g., it has no “dangling” pointers left over), the main stack is copied and the locking script is executed. If the result of executing the locking script with the stack data copied from the unlocking script is “TRUE,” the unlocking script has succeeded in resolving the conditions imposed by the locking script and, therefore, the input is a valid authorization to spend the UTXO.

locking script: OP_DUP OP_HASH160 OP_EQUALVERIFY OP_CHECKSIG
unlocking script:

Advanced Transactions and Scripting

Multisignature

N public keys are recorded in the script and at least M of those must provide signatures to unlock the funds.
M-of-N (M<=N) At the moment max N=3. but P2SH has max N=15

code: M … N CHECKMULTISIG

ie: 0 2 3 CHECKMULTISIG
0 ->it is added due to a bug!!!

Pay-to-Script-Hash (P2SH)

P2SH means “pay to a script matching this hash, a script that will be presented later when this output is spent.”

With P2SH payments, the complex locking script is replaced with its digital fingerprint, a cryptographic hash. When a transaction attempting to spend the UTXO is presented later, it must contain the script that matches the hash, in addition to the unlocking script.

Redeem Script 2 PubKey1 PubKey2 PubKey3 PubKey4 PubKey5 5 CHECKMULTISIG
Locking Script HASH160 <20-byte hash of redeem script> EQUAL
Unlocking Script 0 Sig1 Sig2

<2 PK1 PK2 PK3 PK4 PK5 5 CHECKMULTISIG> EQUAL

P2SH scripts can invalidate transactions by way of their unlocking script only.

P2SH addresses (20byte) use the version prefix “5,” which results in Base58Check-encoded addresses that start with a “3”.

P2SH addresses hide all of the complexity, so that the person making a payment does not see the script.

You are not able to put a P2SH inside a P2SH redeem script, because the P2SH specification is not recursive.

IMPORTANT! Note that because the redeem script is not presented to the network until you attempt to spend a P2SH output, if you lock an output with the hash of an invalid redeem script it will be processed regardless. And then you will not be able unlock the funds

RETURN operator allows developers to add 80 bytes of nonpayment data to a transaction output. However, unlike the use of “fake” UTXO, the RETURN operator creates an explicitly provably unspendable output, which does not need to be stored in the UTXO set -> no bloat!

there is no “unlocking script” that corresponds to RETURN that could possibly be used to “spend” a RETURN output.

Timelocks are useful for postdating transactions and locking funds to a date in the future. More importantly, timelocks extend bitcoin scripting into the dimension of time, opening the door for complex multistep smart contracts.

nLocktime=0 -> immediate propagation and execution.
< 500 million. it is interpreted as a block height, meaning the transaction is not valid and is not relayed or included in the blockchain prior to the specified block height. >=500 million, it is interpreted as a Unix Epoch and the transaction is not valid prior to the specified time.
The use of nLocktime is equivalent to postdating a paper check.

nLocktime + double-spending issue -> timelock restriction must be placed on the UTXO itself and be part of the locking script, rather than on the transaction.

Check Lock Time Verify (CLTV) is a per-output timelock, rather than a per-transaction timelock as is the case with nLocktime.
-By adding the CLTV opcode in the redeem script of an output it restricts the output, so that it can only be spent after the specified time has elapsed.
-CLTV doesn’t replace nLocktime, but rather restricts specific UTXO such that they can only be spent in a future transaction with nLocktime set to a greater or equal value.

CHECKLOCKTIMEVERIFY DROP DUP HASH160 EQUALVERIFY CHECKSIG

Relative Timelocks: they allow a chain of two or more interdependent transactions to be held off chain, while imposing a time constraint on one transaction that is dependent on the elapsed time from the confirmation of a previous transaction. In other words, the clock doesn’t start counting until the UTXO is recorded on the blockchain.

  • For transactions with nLocktime or CHECKLOCKTIMEVERIFY, the nSequence value must be set to less than 2^31 for the timelock guards to have an effect,
  • A transaction can include both timelocked inputs (nSequence < 231) and inputs without a relative timelock (nSequence >= 231).
  • The nSequence value is specified in either blocks or seconds
  • CHECKSEQUENCEVERIFY (CSV) script opcode for relative timelocks that leverages the nSequence value in scripts
  • Relative timelocks with CSV are especially useful when several (chained) transactions are created and signed, but not propagated, when they’re kept “off-chain”. A child transaction cannot be used until the parent transaction has been propagated, mined, and aged by the time specified in the relative timelock.

Median-Time-Past is calculated by taking the timestamps of the last 11 blocks and finding the median. That median time then becomes consensus time and is used for all timelock calculations. By taking the midpoint from approximately two hours in the past, the influence of any one block’s timestamp is reduced. By incorporating 11 blocks, no single miner can influence the timestamps in order to gain fees from transactions with a timelock that hasn’t yet matured.

Fee-sniping is a theoretical attack scenario, where miners attempting to rewrite past blocks “snipe” higher-fee transactions from future blocks to maximize their profitability.
-> Bitcoin Core sets the nLocktime on all new transactions to and sets the nSequence on all the inputs to 0xFFFFFFFE to enable nLocktime.

Complex Scripts

Scripts with Flow Control

opcodes: IF, ELSE, ENDIF, and NOTIF
boolean operators: BOOLAND, BOOLOR, and NOT.
flow control clauses look “backward”: 1 2 ADD, => 1 + 2

VERIFY suffix means that if the condition evaluated is not TRUE, execution of the script terminates immediately and the transaction is deemed invalid.
Opcodes that end in VERIFY do not leave the result on the stack.
ie: A redeem script with an EQUALVERIFY guard clause: HASH160 EQUALVERIFY CHECKSIG

if
script A
else
if
script B
else
scirpt C
endif
endif

-> to execute script B, we need to push to the stack: 1 0 -> 0 will be top of the stack and will fail first if, the next value of the stackc is 1 and it will accept the second if and will execute script B

  • If you use CHECKMULTISIG -> must push a 0 first in the stack

Segregated Witness (segwit) = “separate scriptSig”
every locking script is preceded by a script version number,
The witness data doesn’t need to be transmitted to all nodes and does not need to be stored on disk by all nodes.
Signature Verification Optimization = O(n)
egregated Witness is a change to how individual UTXO are spent and therefore is a per-output feature.

When a transaction spends an UTXO, it must provide a witness. In a traditional UTXO, the locking script requires that witness data be provided inline in the input part of the transaction that spends the UTXO. A Segregated Witness UTXO, however, specifies a locking script that can be satisfied with witness data outside of the input (segregated).

P2WPKH should be constructed by the payee (recipient) by converting a compressed public key to a P2WPKH hash. You should never transform a P2PKH script, bitcoin address, or uncompressed public key to a P2WPKH witness script.

Pay-to-Witness-Script-Hash (P2WSH) = SHA256(script) = 32 bytes script hash
Pay-to-Script-Hash (P2SH) = RIPEMD160(SHA256(script) = 20 bytes script hash

Two types of witness programs:
Pay-to-Witness-Public-Key-Hash (P2WPKH) – public key hash is 20 bytes
Pay-to-Witness-Script-Hash (P2WSH) – script hash is 32 bytes

upgrading to Segregated Witness is a two-step process:
1) wallets must create special segwit type outputs
2) these outputs can be spent by wallets that know how to construct Segregated Witness transactions

For P2WPKH and P2WSH payment types, both the sender and the recipient wallets need to be upgraded to be able to use segwit. Furthermore, the sender’s wallet needs to know that the recipient’s wallet is segwit-aware.

Bob’s wallet can construct a P2SH address that contains a segwit script inside it. Alice’s wallet sees this as a “normal” P2SH address and can make payments to it without any knowledge of segwit.

P2WPKH and P2WSH, can be embedded in a P2SH address.

The native segwit address format is defined in BIP-173: BIP-173 addresses use 32 lower-case-only alphanumeric character set, carefully selected to reduce errors from misreading or mistyping

[bc|tb]1[6-90 chars]

With the introduction of Segregated Witness, transactions have two identifiers, txid and wtxid. The traditional transaction ID txid is the double-SHA256 hash of the serialized transaction, without the witness data. A transaction wtxid is the double-SHA256 hash of the new serialization format of the transaction with witness data.

The traditional txid, since a pure segwit transaction, has empty scriptSigs in every input, there is no part of the transaction that can be modified by a third party.

The wtxid is like an “extended” ID, in that the hash also incorporates the witness data.

signature verification for segwit -> BIP-143: O(n) (instead of O(n2)

The most expensive part of a transaction are the newly created outputs, as they are added to the in-memory UTXO set

Segregated Witness has two main effects on the fees paid by bitcoin users:
1) segwit reduces the overall cost of transactions by discounting witness data and increasing the capacity of the bitcoin blockchain.
2) segwit’s discount on witness data partially mitigates a misalignment of incentives that may have inadvertently created more bloat in the UTXO set.

Transactions and Multisig

https://www.youtube.com/watch?v=Zhd107b8DqA&t=2s
Advanced Bitcoin Scripting Part 1 Transactions Multisig

7.4 Exercise: Bitcoin Scripting

Script Simulator: https://siminchen.github.io/bitcoinIDE/build/editor.html
Opcodes: https://en.bitcoin.it/wiki/Script#Opcodes

7.5 Video

quix

Unit 8: Reaching Consensus

8.1: Elements of Valid Transactions

Honest Nodes and Consensus
difficulty: “hash must be a number less than x”
double-spent (unspent db)
consensus: rules what is valid

8.2: The Mining Process

Mining is the process how we prevent “double spends”, how we determine who has what when, and how we decide on the “truth” on the network without any one entity being in charge of the process.

hash functions, proof of work. Consume electricity!

genesis block: first ever block

8.3: Consensus

validating a new block

block data structure is syntactivally valid, block header hash is equal or less than the targe (proof of work),
block timestamp is less than two hours n the future , block size is within limits, first transaction is a coinbase transaction
all transaction within the block are valid using the transactin checklist

assembling and selection chains of blocks

Nodes maintain three sets of blocks:
-connected to the main blockchain
-branches off the main blockchain (secondary chains)
-orphans: blocks that do not have a known parent in the known chains

The “main chain” at any time is whichever valid chain of blocks has the most cumulative Proof-of-Work associated with it. Under most circumstances this is also the chain with the most blocks in it

When a new block is received, a node will try to slot it into the existing blockchain. The node will look at the block’s “previous block hash” field, which is the reference to the block’s parent. Then, the node will attempt to find that parent in the existing blockchain. Most of the time, the parent will be the “tip” of the main chain, meaning this new block extends the main chain.

Sometimes, as we will see in Blockchain Forks, the new block extends a chain that is not the main chain. In that case, the node will attach the new block to the secondary chain it extends and then compare the work of the secondary chain to the main chain. If the secondary chain has more cumulative work than the main chain, the node will reconverge on the secondary chain, meaning it will select the secondary chain as its new main chain, making the old main chain a secondary chain. If the node is a miner, it will now construct a block extending this new, longer, chain.

If a valid block is received and no parent is found in the existing chains, that block is considered an “orphan.” Orphan blocks are saved in the orphan block pool where they will stay until their parent is received. Once the parent is received and linked into the existing chains, the orphan can be pulled out of the orphan pool and linked to the parent, making it part of a chain. Orphan blocks usually occur when two blocks that were mined within a short time of each other are received in reverse order (child before parent).

*** Blockchain Forks: good explanation

Bitcoin’s block interval of 10 minutes is a design compromise between fast confirmation times (settlement of transactions) and the probability of a fork. A faster block time would make transactions clear faster but lead to more frequent blockchain forks, whereas a slower block time would decrease the number of forks but make settlement slower.

Mining and the Hashing Race: ASICs, terahashes/sec (TH/sec)

Use the coinbase transaction as a source of extra nonce values. Because the coinbase script can store between 2 and 100 bytes of data, miners started using that space as extra nonce space, allowing them to explore a much larger range of block header values to find valid blocks.
The coinbase transaction is included in the merkle tree, which means that any change in the coinbase script causes the merkle root to change

Mining pools: proof-of-work easier than the target, so you can proof you are trying.

Pool miners connect to the pool server using a mining protocol such as Stratum (STM) or GetBlockTemplate (GBT). Both the STM and GBT protocols create block templates that contain a template of a candidate block header. The pool server constructs a candidate block by aggregating transactions, adding a coinbase transaction (with extra nonce space), calculating the merkle root, and linking to the previous block hash. The header of the candidate block is then sent to each of the pool miners as a template. Each pool miner then mines using the block template, at a higher (easier) target than the bitcoin network target, and sends any successful results back to the pool server to earn shares.

P2Pool, a peer-to-peer mining pool without a central operator. P2Pool works by decentralizing the functions of the pool server, implementing a parallel blockchain-like system called a share chain.
P2Pool mining is more complex than pool mining because it requires that the pool miners run a dedicated computer with enough disk space, memory, and internet bandwidth to support a full bitcoin node and the P2Pool node software.

The Consensus Process

video about the above

The Consensus Attacks

Bitcoin’s ledger becomes more and more immutable as time passes. While in theory, a fork can be achieved at any depth, in practice, the computing power needed to force a very deep fork is immense, making old blocks practically immutable.

Consensus attacks also do not affect the security of the private keys and signing algorithm (ECDSA). A consensus attack cannot steal bitcoin, spend bitcoin without signatures, redirect bitcoin, or otherwise change past transactions or ownership records. Consensus attacks can only affect the most recent blocks and cause denial-of-service disruptions on the creation of future blocks.

A double-spend attack can happen in two ways: either before a transaction is confirmed, or if the attacker takes advantage of a blockchain fork to undo several blocks. A 51% attack allows attackers to double-spend their own transactions in the new chain, thus undoing the corresponding transaction in the old chain.

The more confirmations elapse, the harder it becomes to invalidate a transaction with a 51% attack.

The other scenario for a consensus attack is to deny service to specific bitcoin participants (specific bitcoin addresses). An attacker with a majority of the mining power can simply ignore specific transactions.

8.4: Updating Consensus

hard fork: part of the network is operating under a different set of consensus rules (bug or change of consensus rules). not forward compatible
hard fork: sw fork -> net fork -> mining fork -> chain fork

fork -> diverging miner capacity -> blocks take more time to be generated.

Only consensus changes that are forward-incompatible cause a hard fork.
If the change is implemented in such a way that a non-upgraded client still sees the transaction or block as valid under the previous rules, the change can happen without a fork = soft fork is a forward-compatible

Soft forks redefining NOP opcodes:
NOP1 through NOP10. Under the consensus rules, the presence of these opcodes in a script is interpreted as a null-potent operator, meaning they have no effect. Execution continues after the NOP opcode as if it wasn’t there. A soft fork therefore can modify the semantics of a NOP code to give it new meaning.

critics:
Because soft forks create transactions with additional consensus constraints, they become irreversible upgrades in practice.

Since soft forks allow non-upgraded clients to continue to operate within consensus, the mechanism for “activating” a soft fork is through miners signaling readiness:

BIP-34 Signaling and Activation: change block version to 2. When 95% (out 1000) new blocks are version 2, version 1 block are rejected. This was good but it was improved with the next

BIP-9 Signaling and Activation: interprets the block version as a bit field instead of an integer. BIP-9 also sets a maximum time for signaling and activation.

It is important to recognize that there is no perfect solution for consensus development. Both hard forks and soft forks involve tradeoffs.

8.5: Exercise: Create a Valid Transaction via Your Testnet Node

8.6

video

measure amount of proof of work: “difficulty” to generate the block – number of zeros needed to get the block
consensus attack: can’t steal bitcoin, can’t break crypto, etc
can double spend !!! DDOS, etc

bip-34: version = integer value
bip-9: version = bit field (better)

David and Goliath

I wanted to read something from this author for a while and finally got this book.

I enjoyed the beginning with all the analysis about David vs Goliath. The underdog wins 68-70% if follows the underdog rules. And underdog tactics are hard. The story about the female basketball team that just pressed the rivals and won games is hilarious. The summary is the advantage has always disadvantages and the disadvantages has advantages.

Effort can trump ability and that conventions are made to be challenged.

Any fool can spend money. But to earn it and save it and defer gratification, then you learn to value it differently

Another concept is the inverted U that follows nearly everything of consequence. There is nothing such as infinite good. All positive traits, states, experiences have costs that at high level may begin to outweigh the benefits. Examples: School classes with very few students, children from rich families matching their parents (as they dont go through the same problems as the parents)

So being bigger, stronger and richer is not always in our best interest.

Other concept is the “Being big fish in a small pond or being small fish in a big pond”. For this he uses the example of the Impressionism art. And I learned many new details about how they all worked and supported together. For the them, the big pond was the Salon, where only the considered best, were exhibited. But that would kill their meaning as artists. So they created their own pond, it was smaller compared to Salon but they were the big fish there, and time has put them in their place. As well, the inverted-U applies here too. Another example for the big fish/small pond theme, is education. I was surprised about the amount of drop-outs in the top universities compared with the non-top ones. And this touched a fiber. I believe, I am engineer because I studied in the UCM. If I had attended the Polytechnic University, maybe I wouldn’t finish it or I wouldn’t enjoy networks as much. Why that happens? You form your impressions locally, comparing with people on the same boat as you. So you are surrounded by genius, you are going to have a hard time if you are not one of them. The countries with higher suicide rate are in theory, the happiest countries…. This reminds me to Instagram… And there is an inverted U here too. If it is too hard, you fail, but you need some difficulty. That brings me to climbing and work. You want to be the weakest (climbing/engineer) in the room so you work/push harder to be better. I spent 7y in a firma being the dumbest, I learned a lot! I improved a lot, never caught up with the others, and didnt care, I compared with myself, and could see the improvement. Like climbing, I try hard routes, that’s the only way to improve. The obstacle is the way. But again, the inverse-U, too hard, you fail. I can’t solo El Capitan. And somehow, I feel that learning bachata, I feel too clumsy.

Dyslexia is another example of David vs Goliath. The examples of the founder of IKEA and his adventure in (communist) Poland and Gary Cohn. Your learning difficulties make you find alternatives.

Another character, Emil Freireich. He developed the chemotherapy program, while working with dying kids… “There are things that either build you up or put you down” I think we can choose most of the times.

There is another topic that covers Martin Luther King‘s activities, London Blitz , Northern Ireland conflict and Brownsville NY. This is regarding the limits of power. And that applies to school classes.

Most revolutions are not caused by revolutionaries, but by the stupidity and brutality of governments.

When people in authority want the rest of us to behave, it matters the most, how they behave. This is the principle of legitimacy:

  1. The people who are asked to obey authority have to feel like they have a voice.
  2. The law has to be predictable. The rules for tomorrow are going to be roughly the same as today
  3. Authority has to be fair. No groups treated differently.

The near miss

What is right? Most often as not, is simply the way that people in power/privilege close the door on those on the outside.

And many things in life, are actually just “time and chance” although we love to find logic to all. (I am impressesed/scared with so many interpretations of that text…)

The last is a bit harsh. It is about forgiveness and is connected to the power points above. And follows the inverse-U, the three-strikes law didnt do that good. Putting people in jails is expensive. We need a different approach. Not easy at all to do that, even more if your kid is killed cold blood.

The Last Crusade

This is by favourite film as a kid, and after reading the first books about the movie, I got the final one. The book is identical as the movie, and felt like watching it. Good old memories.