Bitcoin

Bitcoin https://www.youtube.com/watch?v=GiU4b7ldKMs

block chain database of bitcoin.

every block added every 10 minutes on average may vary. depending if very easy or hard t create hash sha-256

chain should be longest chain so they get reward. Every block gets rewards 10 minutes 50 bitcoin and halves every 4 years.

same block can be created at the same time. start working on first block that you see. Chk which form longest chain. Then all blocks stop working on other and starts working on the one that was the largest. Forking of chain is also done. Longest one wins.

Transaction in bitcoin:A bitcoin uses elliptic curve(signature is shorter to control length of block chain.). A private key is 32 random bytes.   A public key computed from private key. There is no cryptography in bitcoin. Only signing.

Bitcoin addresses are like bank account numbers they are random and long. Calculated from a public key RIPEMD-160(SHA256(public key)). No one knows who owns which. You can have a many bitcoin addresses. Value can be moved from one account to other via transactions.

Bitcoin mining is the process of adding transaction records to Bitcoin’s public ledger of past transactions or blockchain. This ledger of past transactions is called the block chain as it is a chain of blocks. The block chain serves to confirm transactions to the rest of the network as having taken place.

merkle root is a binary tree of SHA256 hashes: Transaction are unconfirmed. No block in the chain trusts a transaction untill confirmed by miner. Miner pulls a random transaction from the unconfirmed transactions and adds it into a block. Then the transaction is broadcast to all the nodes. But there can be a fork in the block chain where a illegit node can try a transaction.

A hacker can send two transactions and broadcast into the network. Half network will believe that first one is legit and other half will believe in the other transaction.

bitcoin properties 1. you cannot add money at your will you need huge computational power.

Biggest distributed computing in the world. If you combine top 50 supercomputers in the world they will be 200 time slower than bitcoin network.  You can add blocks if you have huge computational power but you cannot keep doing it for a long amount of time because the block chain will readjust the hardness and it will become more difficult for you to keep on adding nodes.

This attack is very expensive around millions.

Block chain concept is new. 

A block can only be one MB large- 1000 transactions in a block. Cannot use brute force attack on it because of SHA 256. When the block rewards gets upto 0 then the miners will live on transaction fees.

Docker Tutum

Tutum is a platform to provide docker container as a service – cloud. Its equivalent docker universal control plane- on premise.

n tutum you BYON and then tutum takes care of plumbing. Available in API,GUI,CLI.

If there is any change in git repo code then tutum will automatically redeploy.

in the node builder all the builds will occur if do not name a node as builder then all the build will occur on a node that has least amount of containers. All the repository build will happen inside tutum builder.

Tutum has its own yaml file tutum.yml similar to docker-compose.yml. You can add a load balancer haproxy which is out-of-box which will automatically scale the application as per the requests. You can add the tags (tags are branches of git where you want to build) only the branches specified in the tag will be built for deployment.

Tutum yaml is used when we create a stack in tutum. autoredploy: true

Docker Part 1

currently with docker you are limited to only linux machines and apps. It is like installing various zip files on your os.

because of the use of base image we can save space.All the containers use the same base image. If you want 100 containers in traditional vm env you would have needed 100GB space considering 1gb per image but with docker you will need only 1gb of space. Suppose if you need emacs and apache then docker will add to separate images for it on top of each other and with the base image it will form a union image. this image is readonly so to write in it docker will place a writable container and the whole set container:image(apache)+image(emacs)+base image will make a whole container in the docker.  But the writable container is not persistent. Docker boots the image/ image layers on top f each other.

apt-cache search docker.io —will display a list of packages containing docker

apt-chache show docker.io —- will display all details of packaage

Check if it is latest version.

Docker guys have provided us with a script get.docker.com. it has the script for installation.

This is to be executed .

wget -qO- https://get.docker.com/ | sh to install docker latest.

If you want to add non root user in docker then execute the following command:

sudo usermod -aG docker your-user

check by id your-user to check in which group  user is present it should be present in docker group.

all images are present in registry. docker own registry at docker hub. first you will need to pull the docker images from the registry with docker pull command. After that you can use docker run. If you have specified which images to use then docker will automatically pull the images from registry while docker run is executed. the registry of docker is hub.docker.com

You can search using docker search mysql

to download use docker pull ubuntu

always pull the latest you can also use docker pull ubuntu:latest it is same. :latest is called image tag or the container tag. you can pull using version as well: docker pull ubuntu:14.04

To check what images you have you can issue the docker images command

To start a container use

docker run -ti ubuntu:14.04 /bin/bash -t for terminal i for interactive

to quit container without stopping it press CTRL+P+Q

to check which docker containers are running then command docker ps

to attach to running container use command docker attach <container id>/<container name>

To exit docker and stop the container as well press CTRL+D

To list all the containers even the which that are not running use command docker ps -a

If you exit with ctrl + d and want to start the container again then you can use the command docker start <container-name> 

This will start container in the background

To display docker processes use command docker top / docker top <container-name>

to stop use docker stop <container-name>

in /var/lib there is a folder docker. in that you will have folder container in that you can see folders of running containers with their long ids. In that folders you can see the following files:

<id>-json.log, hosts, config.json, resolv.json, hostconfig.json, resolv.conf.hash, hostname.

In hostname file you will have the short id of container which is shorter version of the long id.

you can also run docker run -ti ubuntu:12.04 /bin/bash it will download automatically.

If you dont want to immediately attach to the container then you can run docker in detached mode:

docker run -d -ti ubuntu:14.04 –name=duck /bin/bash

-d for detached

-ti also because if you dont give you wont be able to login into it.

But it is not persistant if you want the docker container to be persistent then you can use the command:

docker run -ti -v /data –name=duck2 ubuntu:14.04 /bin/bash

-v for volume you have to give it a mount point it will create this mount point inside the container folder.

name should not be same as previous

When in bash inside the container goto /dataand then start working and creat files.

then after quiting the container with ctrl+p+q then in your local machine goto /var/lib/volumes/<container-long-id>/_data you can find all your files here.

If you remove from inside container it will be removed from local machine as well

If you want to attach local directory to continer then

docker run -ti -v /srv/duck3:/data –name=duck3 ubuntu:14.04 /bin/bash

/srv/duck3 local dir

/data container dir

docker run -d -p 3306 -ti mysql /bin/bash

-p for port

this will open port 3306 and nat it to external port.

you check by this command locally iptables -L -t nat

-L list

-t for table nat

it will nat to random port if want to pen specific port

docker run -d -ti -p 3306:3306 mysql /bin/bash

you can also specify the specific ip address if you want with -p <ip>:3306:3306

 

Java

A one-dimensional array is, essentially, a list of like-typed variables. To create an array, you first
must create an array variable of the desired type. The general form of a one-dimensional
array declaration is
type var-name[ ];
Here, type declares the element type (also called the base type) of the array.

Although this declaration establishes the fact that month_days is an array variable, no
array actually exists. To link month_days with an actual, physical array of integers, you must
allocate one using new and assign it to month_days. new is a special operator that allocates
memory.
You will look more closely at new in a later chapter, but you need to use it now to
allocate memory for arrays. The general form of new as it applies to one-dimensional
arrays appears as follows:
array-var = new type [size];

The elements
in the array allocated by new will automatically be initialized to zero (for numeric types), false
(for boolean), or null (for reference types, which are described in a later chapter).

Let’s review: Obtaining an array is a two-step process. First, you must declare a variable
of the desired array type. Second, you must allocate the memory that will hold the array,
using new, and assign it to the array variable. Thus, in Java all arrays are dynamically
allocated.

int month_days[] = new int[12];

int month_days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

int twoD[][] = new int[4][];
twoD[0] = new int[5];
twoD[1] = new int[5];
twoD[2] = new int[5];
twoD[3] = new int[5];

//differing 2nd dimension

int twoD[][] = new int[4][];
twoD[0] = new int[1];
twoD[1] = new int[2];
twoD[2] = new int[3];
twoD[3] = new int[4];

double m[][] = {
{ 0*0, 1*0, 2*0, 3*0 },
{ 0*1, 1*1, 2*1, 3*1 },
{ 0*2, 1*2, 2*2, 3*2 },
{ 0*3, 1*3, 2*3, 3*3 }};

For example, the following two declarations are equivalent:
int al[] = new int[3];
int[] a2 = new int[3];
The following declarations are also equivalent:
char twod1[][] = new char[3][4];
char[][] twod2 = new char[3][4];

The reason for this is simple: Java does not support or allow pointers. (Or more properly, Java
does not support pointers that can be accessed and/or modified by the programmer.) Java
cannot allow pointers, because doing so would allow Java programs to breach the firewall
between the Java execution environment and the host computer. (Remember, a pointer can
be given any address in memory—even addresses that might be outside the Java run-time
system.) Since C/C++ make extensive use of pointers, you might be thinking that their loss
is a significant disadvantage to Java. However, this is not true. Java is designed in such a way
that as long as you stay within the confines of the execution environment, you will never
need to use a pointer, nor would there be any benefit in using one.

var op= expression;
The compound assignment operators provide two benefits. First, they save you a bit
of typing, because they are “shorthand” for their equivalent long forms. Second, in some
cases they are more efficient than are their equivalent long forms. For these reasons, you
will often see the compound assignment operators used in professionally written Java
programs.

These operators are unique in that they can appear both in postfix form, where they
follow the operand as just shown, and prefix form, where they precede the operand. In the
foregoing examples, there is no difference between the prefix and postfix forms. However,
when the increment and/or decrement operators are part of a larger expression, then a
subtle, yet powerful, difference between these two forms appears. In the prefix form,
the operand is incremented or decremented before the value is obtained for use in the
expression. In postfix form, the previous value is obtained for use in the expression, and
then the operand is modified. For example:
x = 42;
y = ++x;
In this case, y is set to 43 as you would expect, because the increment occurs before x is
assigned to y. Thus, the line y = ++x; is the equivalent of these two statements:
x = x + 1;
y = x;
However, when written like this,
x = 42;
y = x++;
the value of x is obtained before the increment operator is executed, so the value of y is 42.
Of course, in both cases x is set to 43. Here, the line y = x++; is the equivalent of these two
statements:
y = x;
x = x + 1;

Operator Result
~ Bitwise unary NOT
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
>> Shift right
>>> Shift right zero fill
<< Shift left
&= Bitwise AND assignment
|= Bitwise OR assignment
^= Bitwise exclusive OR assignment
>>= Shift right assignment
>>>= Shift right zero fill assignment
<<= Shift left assignment

All of the integer types (except char) are signed integers. This means that they can
represent negative values as well as positive ones. Java uses an encoding known as two’s
complement, which means that negative numbers are represented by inverting (changing 1’s
to 0’s and vice versa) all of the bits in a value, then adding 1 to the result. For example, –42
is represented by inverting all of the bits in 42, or 00101010, which yields 11010101, then
adding 1, which results in 11010110, or –42. To decode a negative number, first invert all of the bits, then add 1. For example, –42, or 11010110 inverted, yields 00101001, or 41, so
when you add 1 you get 42.

Because Java uses two’s complement to store negative numbers—and because all
integers are signed values in Java—applying the bitwise operators can easily produce
unexpected results. For example, turning on the high-order bit will cause the resulting
value to be interpreted as a negative number, whether this is what you intended or not. To
avoid unpleasant surprises, just remember that the high-order bit determines the sign of an
integer no matter how that high-order bit gets set.

The XOR operator, ^, combines bits such that if exactly one operand is 1, then the result
is 1.

The left shift operator, <<, shifts all of the bits in a value to the left a specified number of
times. It has this general form:
value << num
Here, num specifies the number of positions to left-shift the value in value. That is, the
<< moves all of the bits in the specified value to the left by the number of bit positions
specified by num. For each shift left, the high-order bit is shifted out (and lost), and a zero
is brought in on the right. This means that when a left shift is applied to an int operand,
bits are lost once they are shifted past bit position 31. If the operand is a long, then bits are
lost after bit position 63.
Java’s automatic type promotions produce unexpected results when you are shifting
byte and short values. As you know, byte and short values are promoted to int when an
expression is evaluated. Furthermore, the result of such an expression is also an int. This
means that the outcome of a left shift on a byte or short value will be an int, and the bits
shifted left will not be lost until they shift past bit position 31. Furthermore, a negative byte
or short value will be sign-extended when it is promoted to int. Thus, the high-order bits
will be filled with 1’s. For these reasons, to perform a left shift on a byte or short implies
that you must discard the high-order bytes of the int result. For example, if you left-shift a
byte value, that value will first be promoted to int and then shifted. This means that you
must discard the top three bytes of the result if what you want is the result of a shifted byte
value. The easiest way to do this is to simply cast the result back into a byte.

The right shift operator, >>, shifts all of the bits in a value to the right a specified number of
times. Its general form is shown here:
value >> num
Here, num specifies the number of positions to right-shift the value in value. That is, the >>
moves all of the bits in the specified value to the right the number of bit positions specified
by num.

Layer and tier

difference between a layer and a tier:

q1: web server architecture is a pipelined architecture.

q2: cloud archi require end teir design architecture.

q3: SOA architecture: component architecture

q4: GoF book “design patterns”

q5: plugin: type of factory pattern implemented to submit a inversion of control for object.

q6: piece of code that runs on its own

q7: GoF

q8: all hidden

q9: encapsulation ensures that the code does not have direct dependencies outside that can be changed at will.

q10: Cohesion determines layes

q11: cannot be scled easily

q12: no of things in piece of code.

q13: Favor component design over inheritnce

q14: Persistence,caching and optimistic locking cohesive code.

q15: Instance separately and consumed has connection with business ligic and comes as a service.

q16: sprint methodology (iterative)

q17: how implementation is done vs how it was told.

Coding Standards

  1.  Schemaless:

Nosql

Variable state has different instances that have different values that need to be stored inside them.  Common state when most of the instances have the same value so it is easier to reteive.

the above is a method in which we can make our RDBMS system more flexible like NoSQL but make them more rigid then the NOSQL.

Then in our traditional schema we need the data that fits the schema. But in the predicate one we can handle different types of data and decide how we are going to handle that data.

We can use xml for this pupose. To keep a check on xml data we can use xml schema.. but now a days there is relax compact notation.

RELAX NG is a simple schema language for XML, based on [RELAX] and [TREX]. A RELAX NG schema specifies a pattern for the structure and content of an XML document. A RELAX NG schema thus identifies a class of XML documents consisting of those documents that match the pattern.

Consider a simple XML representation of an email address book:

<addressBook>  <card>    <name>John Smith</name>    <email>js@example.com</email>  </card>  <card>    <name>Fred Bloggs</name>    <email>fb@example.net</email>  </card></addressBook>

The DTD (as an internal subset) would be as follows:

<!DOCTYPE addressBook [<!ELEMENT addressBook (card*)><!ELEMENT card (name, email)><!ELEMENT name (#PCDATA)><!ELEMENT email (#PCDATA)>]>

A RELAX NG pattern for this could be written as follows:

element addressBook {  element card {    element name { text },    element email { text }  }*}

If the addressBook is required to be non-empty, then we can use + instead of *:

element addressBook {  element card {    element name { text },    element email { text }  }+}

Now let’s change it to allow each card to have an optional note element:

element addressBook {  element card {    element name { text },    element email { text },    element note { text }?  }*}

Please refer to http://www.relaxng.org/compact-tutorial-20030326.html for more details.

You can have multiple schemas in it. You can extend one schema into the other.

2. Contextual Validation

http://martinfowler.com/bliki/ContextualValidation.html

Some recent readings made me think about saying a few preliminary things on the topic. One common thing I see people do is to develop validation routines for objects. These routines come in various ways, they may be in the object or external, they may return a boolean or throw an exception to indicate failure. But one thing that I think constantly trips people up is when they think object validity on a context independent way such as an isValid method implies.

I think it’s much more useful to think of validation as something that’s bound to a context – typically an action that you want to do. Is this order valid to be filled, is this customer valid to check in to the hotel. So rather than have methods like isValid have methods like isValidForCheckIn.

One of the consequences of this is that saving an object to a database is itself an action. Thinking about it that way raises some important questions. Often when people talk about a context-free validity, they mean it in terms of saving to a database. But the various validity checks that make this up should be interrogated with the question “should failing this test prevent saving?”

In About Face Alan Cooper advocated that we shouldn’t let our ideas of valid states prevent a user from entering (and saving) incomplete information. I was reminded by this a few days ago when reading a draft of a book that Jimmy Nilsson is working on. He stated a principle that you should always be able to save an object, even if it has errors in it. While I’m not convinced that this should be an absolute rule, I do think people tend to prevent saving more than they ought. Thinking about the context for validation may help prevent that.

Implicit schema == bad

Prefer explicit schema…..

can refer to everything in details in http://martinfowler.com/articles/schemaless/