v6 initial commit, maybe more to follow
This commit is contained in:
parent
7f91be86c0
commit
d6a32870bc
34 changed files with 16875 additions and 0 deletions
55
blockchain/index.test.js
Normal file
55
blockchain/index.test.js
Normal file
|
@ -0,0 +1,55 @@
|
|||
const Blockchain = require('./index');
|
||||
const Block = require('./block');
|
||||
|
||||
describe('Blockchain', () => {
|
||||
let bc, bc2;
|
||||
|
||||
beforeEach(() => {
|
||||
bc = new Blockchain();
|
||||
bc2 = new Blockchain();
|
||||
});
|
||||
|
||||
it('starts with genesis block', () => {
|
||||
expect(bc.chain[0]).toEqual(Block.genesis());
|
||||
});
|
||||
|
||||
it('adds a new block', () => {
|
||||
const data = 'foo';
|
||||
bc.addBlock(data);
|
||||
|
||||
expect(bc.chain[bc.chain.length-1].data).toEqual(data);
|
||||
});
|
||||
|
||||
it('validates a valid chain', () => {
|
||||
bc2.addBlock('foo');
|
||||
|
||||
expect(bc.isValidChain(bc2.chain)).toBe(true);
|
||||
});
|
||||
|
||||
it('invalidates a chain with a corrupt genesis block', () => {
|
||||
bc2.chain[0].data = 'Bad data';
|
||||
|
||||
expect(bc.isValidChain(bc2.chain)).toBe(false);
|
||||
});
|
||||
|
||||
it('invalidates a corrupt chain', () => {
|
||||
bc2.addBlock('foo');
|
||||
bc2.chain[1].data = 'Not foo';
|
||||
|
||||
expect(bc.isValidChain(bc2.chain)).toBe(false);
|
||||
});
|
||||
|
||||
it('replaces the chain with a valid chain', () => {
|
||||
bc2.addBlock('goo');
|
||||
bc.replaceChain(bc2.chain);
|
||||
|
||||
expect(bc.chain).toEqual(bc2.chain);
|
||||
});
|
||||
|
||||
it('does not replace the chain with one of less than or equal to length', () => {
|
||||
bc.addBlock('foo');
|
||||
bc.replaceChain(bc2.chain);
|
||||
|
||||
expect(bc.chain).not.toEqual(bc2.chain);
|
||||
})
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue