Skip to content

Commit

Permalink
1.fix some bugs and test functions
Browse files Browse the repository at this point in the history
2.make bliss interface compatible with both struct and pointer
  • Loading branch information
SeienK committed Jan 23, 2018
1 parent 52150fc commit 8e7c7a6
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 60 deletions.
52 changes: 29 additions & 23 deletions blockchain/stakeversion_test.go
Expand Up @@ -473,7 +473,7 @@ func DNWTestCalcStakeVersionByNode(t *testing.T) {
}

// TestIsStakeMajorityVersion doesn't work yet
func DNWTestIsStakeMajorityVersion(t *testing.T) {
func TestIsStakeMajorityVersion(t *testing.T) {
params := &chaincfg.MainNetParams

// Calculate super majority for 5 and 3 ticket maxes.
Expand All @@ -497,15 +497,15 @@ func DNWTestIsStakeMajorityVersion(t *testing.T) {
}{
{
name: "too shallow",
numNodes: params.StakeValidationHeight + params.StakeVersionInterval - 1,
numNodes: params.StakeValidationHeight + params.StakeVersionInterval ,
startStakeVersion: 1,
expectedStakeVersion: 1,
expectedCalcVersion: 0,
result: true,
},
{
name: "just enough",
numNodes: params.StakeValidationHeight + params.StakeVersionInterval,
numNodes: params.StakeValidationHeight + params.StakeVersionInterval + 1,
startStakeVersion: 1,
expectedStakeVersion: 1,
expectedCalcVersion: 0,
Expand All @@ -523,7 +523,7 @@ func DNWTestIsStakeMajorityVersion(t *testing.T) {
name: "100%",
numNodes: params.StakeValidationHeight + params.StakeVersionInterval,
set: func(b *blockNode) {
if int64(b.header.Height) > params.StakeValidationHeight {
if int64(b.header.KeyHeight + 1) >= params.StakeValidationHeight{
for x := 0; x < int(params.TicketsPerBlock); x++ {
b.votes = append(b.votes,
VoteVersionTuple{Version: 2})
Expand All @@ -539,11 +539,11 @@ func DNWTestIsStakeMajorityVersion(t *testing.T) {
name: "50%",
numNodes: params.StakeValidationHeight + (params.StakeVersionInterval * 2),
set: func(b *blockNode) {
if int64(b.header.Height) <= params.StakeValidationHeight {
if int64(b.header.KeyHeight + 1) < params.StakeValidationHeight{
return
}

if int64(b.header.Height) < params.StakeValidationHeight+params.StakeVersionInterval {
if int64(b.header.KeyHeight + 1) < params.StakeValidationHeight + params.StakeVersionInterval{
for x := 0; x < int(params.TicketsPerBlock); x++ {
b.votes = append(b.votes,
VoteVersionTuple{Version: 1})
Expand Down Expand Up @@ -572,11 +572,11 @@ func DNWTestIsStakeMajorityVersion(t *testing.T) {
name: "75%-1",
numNodes: params.StakeValidationHeight + (params.StakeVersionInterval * 2),
set: func(b *blockNode) {
if int64(b.header.Height) < params.StakeValidationHeight {
if int64(b.header.KeyHeight + 1) < params.StakeValidationHeight {
return
}

if int64(b.header.Height) < params.StakeValidationHeight+params.StakeVersionInterval {
if int64(b.header.KeyHeight + 1) < params.StakeValidationHeight+params.StakeVersionInterval {
for x := 0; x < int(params.TicketsPerBlock); x++ {
b.votes = append(b.votes,
VoteVersionTuple{Version: 1})
Expand Down Expand Up @@ -605,11 +605,11 @@ func DNWTestIsStakeMajorityVersion(t *testing.T) {
name: "75%",
numNodes: params.StakeValidationHeight + (params.StakeVersionInterval * 2),
set: func(b *blockNode) {
if int64(b.header.Height) <= params.StakeValidationHeight {
if int64(b.header.KeyHeight + 1) < params.StakeValidationHeight {
return
}

if int64(b.header.Height) < params.StakeValidationHeight+params.StakeVersionInterval {
if int64(b.header.KeyHeight + 1) < params.StakeValidationHeight+params.StakeVersionInterval {
for x := 0; x < int(params.TicketsPerBlock); x++ {
b.votes = append(b.votes,
VoteVersionTuple{Version: 1})
Expand Down Expand Up @@ -638,11 +638,11 @@ func DNWTestIsStakeMajorityVersion(t *testing.T) {
name: "100% after several non majority intervals",
numNodes: params.StakeValidationHeight + (params.StakeVersionInterval * 222),
set: func(b *blockNode) {
if int64(b.header.Height) <= params.StakeValidationHeight {
if int64(b.header.KeyHeight + 1) < params.StakeValidationHeight {
return
}

if int64(b.header.Height) < params.StakeValidationHeight+params.StakeVersionInterval {
if int64(b.header.KeyHeight + 1) < params.StakeValidationHeight+params.StakeVersionInterval {
for x := 0; x < int(params.TicketsPerBlock); x++ {
b.votes = append(b.votes,
VoteVersionTuple{Version: 1})
Expand All @@ -664,7 +664,7 @@ func DNWTestIsStakeMajorityVersion(t *testing.T) {
name: "no majority ever",
numNodes: params.StakeValidationHeight + (params.StakeVersionInterval * 8),
set: func(b *blockNode) {
if int64(b.header.Height) <= params.StakeValidationHeight {
if int64(b.header.KeyHeight + 1) < params.StakeValidationHeight {
return
}

Expand All @@ -682,11 +682,11 @@ func DNWTestIsStakeMajorityVersion(t *testing.T) {
name: "75%-1 with 3 votes",
numNodes: params.StakeValidationHeight + (params.StakeVersionInterval * 2),
set: func(b *blockNode) {
if int64(b.header.Height) < params.StakeValidationHeight {
if int64(b.header.KeyHeight + 1) < params.StakeValidationHeight {
return
}

if int64(b.header.Height) < params.StakeValidationHeight+params.StakeVersionInterval {
if int64(b.header.KeyHeight + 1) < params.StakeValidationHeight+params.StakeVersionInterval {
for x := 0; x < int(params.TicketsPerBlock-2); x++ {
b.votes = append(b.votes,
VoteVersionTuple{Version: 1})
Expand Down Expand Up @@ -715,11 +715,11 @@ func DNWTestIsStakeMajorityVersion(t *testing.T) {
name: "75% with 3 votes",
numNodes: params.StakeValidationHeight + (params.StakeVersionInterval * 2),
set: func(b *blockNode) {
if int64(b.header.Height) <= params.StakeValidationHeight {
if int64(b.header.KeyHeight + 1) < params.StakeValidationHeight {
return
}

if int64(b.header.Height) < params.StakeValidationHeight+params.StakeVersionInterval {
if int64(b.header.KeyHeight + 1) < params.StakeValidationHeight+params.StakeVersionInterval {
for x := 0; x < int(params.TicketsPerBlock-2); x++ {
b.votes = append(b.votes,
VoteVersionTuple{Version: 1})
Expand Down Expand Up @@ -748,11 +748,11 @@ func DNWTestIsStakeMajorityVersion(t *testing.T) {
name: "75% with 3 votes blockversion 3",
numNodes: params.StakeValidationHeight + (params.StakeVersionInterval * 2),
set: func(b *blockNode) {
if int64(b.header.Height) <= params.StakeValidationHeight {
if int64(b.header.KeyHeight + 1) < params.StakeValidationHeight {
return
}

if int64(b.header.Height) < params.StakeValidationHeight+params.StakeVersionInterval {
if int64(b.header.KeyHeight + 1) < params.StakeValidationHeight+params.StakeVersionInterval {
for x := 0; x < int(params.TicketsPerBlock-2); x++ {
b.votes = append(b.votes,
VoteVersionTuple{Version: 1})
Expand Down Expand Up @@ -782,11 +782,11 @@ func DNWTestIsStakeMajorityVersion(t *testing.T) {
name: "75%-1 with 3 votes blockversion 3",
numNodes: params.StakeValidationHeight + (params.StakeVersionInterval * 2),
set: func(b *blockNode) {
if int64(b.header.Height) < params.StakeValidationHeight {
if int64(b.header.KeyHeight + 1) < params.StakeValidationHeight {
return
}

if int64(b.header.Height) < params.StakeValidationHeight+params.StakeVersionInterval {
if int64(b.header.KeyHeight + 1) < params.StakeValidationHeight+params.StakeVersionInterval {
for x := 0; x < int(params.TicketsPerBlock-2); x++ {
b.votes = append(b.votes,
VoteVersionTuple{Version: 1})
Expand Down Expand Up @@ -826,6 +826,7 @@ func DNWTestIsStakeMajorityVersion(t *testing.T) {
t.Logf("running: %v\n", test.name)
var currentNode *blockNode
currentNode = genesisNode
lastKeyBlock := chainhash.Hash{}
for i := int64(1); i <= test.numNodes; i++ {
/*
// Make up a header.
Expand All @@ -837,10 +838,15 @@ func DNWTestIsStakeMajorityVersion(t *testing.T) {
}
node := newBlockNode(header, nil, nil, nil)
*/

node := newBlockNode(FakeBlock(test.blockVersion, i, test.startStakeVersion), nil, nil, nil)
fakeBlock := FakeBlock(test.blockVersion, i, test.startStakeVersion)
node := newBlockNode(fakeBlock, nil, nil, nil)
node.height = i
node.keyHeight = i - 1
node.isKeyBlock = true
node.parent = currentNode
node.header.PrevKeyBlock = lastKeyBlock
node.header.KeyHeight = uint32(i - 1)
lastKeyBlock = *(fakeBlock.Hash())

// Override version.
if test.set != nil {
Expand Down
65 changes: 34 additions & 31 deletions chaincfg/genesis_test.go
Expand Up @@ -15,18 +15,19 @@ import (

// TestGenesisBlock tests the genesis block of the main network for validity by
// checking the encoded bytes and hashes.
func DNWTestGenesisBlock(t *testing.T) {
func TestGenesisBlock(t *testing.T) {

genesisBlockBytes, _ := hex.DecodeString("0100000000000000000000000000" +
genesisBlockBytes, _ := hex.DecodeString("010000000000000000000000000" +
"00000000000000000000000000000000000000000000000000000000000000000" +
"000000000000000000000000000000000000000000000dc101dfc3c6a2eb10ca0" +
"c5374e10d28feb53f7eabcc850511ceadb99174aa660000000000000000000000" +
"00000000000000000000000000000000000000000000000000000000000000000" +
"000000000ffff011b00c2eb0b000000000000000000000000a0d7b85600000000" +
"000000000ffff0f1d00c2eb0b0000000000000000000000000000000066764c5a" +
"00000000000000000000000000000000000000000000000000000000000000000" +
"00000000101000000010000000000000000000000000000000000000000000000" +
"000000000000000000ffffffff00ffffffff01000000000000000000002080167" +
"9e98561ada96caec2949a5d41c4cab3851eb740d951c10ecbcf265c1fd9000000" +
"000000000001ffffffffffffffff00000000ffffffff02000000")
"00000000000000001010000000100000000000000000000000000000000000000" +
"00000000000000000000000000ffffffff00ffffffff010000000000000000000" +
"020801679e98561ada96caec2949a5d41c4cab3851eb740d951c10ecbcf265c1f" +
"d9000000000000000001ffffffffffffffff00000000ffffffff02000000")

// Encode the genesis block to raw bytes.
var buf bytes.Buffer
Expand All @@ -53,28 +54,29 @@ func DNWTestGenesisBlock(t *testing.T) {

// TestTestNetGenesisBlock tests the genesis block of the test network (version
// 9) for validity by checking the encoded bytes and hashes.
func DNWTestTestNetGenesisBlock(t *testing.T) {
func TestTestNetGenesisBlock(t *testing.T) {
// Encode the genesis block to raw bytes.
var buf bytes.Buffer
err := TestNet2Params.GenesisBlock.Serialize(&buf)
if err != nil {
t.Fatalf("TestTestNetGenesisBlock: %v", err)
}

testNetGenesisBlockBytes, _ := hex.DecodeString("0400000000000000000" +
testNetGenesisBlockBytes, _ := hex.DecodeString("04000000000000000000" +
"000000000000000000000000000000000000000000000000000000000000000" +
"000000000000000000000000000000000000000000000000000000571a3f8bb" +
"1903091a18edd3efbc324c79876764af2424071a480d3f04ea16a2000000000" +
"000000000000000000000000000000000000000000000000000000000000000" +
"000000000000000000000000ffff001e002d310100000000000000000000000" +
"040bcc8581aa4ae180000000000000000000000000000000000000000000000" +
"000000000000000000000000000101000000010000000000000000000000000" +
"000000000000000000000000000000000000000ffffffff00ffffffff010000" +
"0000000000000000434104678afdb0fe5548271967f1a67130b7105cd6a828e" +
"03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7" +
"ba0b8d578a4c702b6bf11d5fac0000000000000000010000000000000000000" +
"00000000000004d04ffff001d0104455468652054696d65732030332f4a616e" +
"2f32303039204368616e63656c6c6f72206f6e206272696e6b206f662073656" +
"36f6e64206261696c6f757420666f722062616e6b7300")
"00000000040bcc8580000000000000000000000000000000000000000000000" +
"0000000000000000001aa4ae180000000001010000000100000000000000000" +
"00000000000000000000000000000000000000000000000ffffffff00ffffff" +
"ff0100000000000000000000434104678afdb0fe5548271967f1a67130b7105" +
"cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de" +
"5c384df7ba0b8d578a4c702b6bf11d5fac00000000000000000100000000000" +
"0000000000000000000004d04ffff001d0104455468652054696d6573203033" +
"2f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f6" +
"6207365636f6e64206261696c6f757420666f722062616e6b7300")

// Ensure the encoded block matches the expected bytes.
if !bytes.Equal(buf.Bytes(), testNetGenesisBlockBytes) {
Expand All @@ -95,7 +97,7 @@ func DNWTestTestNetGenesisBlock(t *testing.T) {

// TestSimNetGenesisBlock tests the genesis block of the simulation test network
// for validity by checking the encoded bytes and hashes.
func DNWTestSimNetGenesisBlock(t *testing.T) {
func TestSimNetGenesisBlock(t *testing.T) {
// Encode the genesis block to raw bytes.
var buf bytes.Buffer
err := SimNetParams.GenesisBlock.Serialize(&buf)
Expand All @@ -104,19 +106,20 @@ func DNWTestSimNetGenesisBlock(t *testing.T) {
}

simNetGenesisBlockBytes, _ := hex.DecodeString("0100000000000000000" +
"000000000000000000000000000000000000000000000000000000dc101dfc" +
"3c6a2eb10ca0c5374e10d28feb53f7eabcc850511ceadb99174aa660000000" +
"00000000000000000000000000000000000000000000000000000000000000" +
"000000000000000000000000000ffff7f20000000000000000000000000000" +
"00000450686530000000000000000000000000000000000000000000000000" +
"00000000000000000000000000000000101000000010000000000000000000" +
"000000000000000000000000000000000000000000000ffffffff00fffffff" +
"f0100000000000000000000434104678afdb0fe5548271967f1a67130b7105" +
"cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112d" +
"e5c384df7ba0b8d578a4c702b6bf11d5fac000000000000000001000000000" +
"000000000000000000000004d04ffff001d0104455468652054696d6573203" +
"0332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b2" +
"06f66207365636f6e64206261696c6f757420666f722062616e6b7300")
"00000000000000000000000000000000000000000000000000000000dc101d" +
"fc3c6a2eb10ca0c5374e10d28feb53f7eabcc850511ceadb99174aa6600000" +
"00000000000000000000000000000000000000000000000000000000000000" +
"00000000000000000000000000000ffff7f200000000000000000000000000" +
"00000000000000045068653000000000000000000000000000000000000000" +
"00000000000000000000000000000000000000000010100000001000000000" +
"0000000000000000000000000000000000000000000000000000000fffffff" +
"f00ffffffff0100000000000000000000434104678afdb0fe5548271967f1a" +
"67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f3550" +
"4e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000000000000" +
"1000000000000000000000000000000004d04ffff001d01044554686520546" +
"96d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206" +
"272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b7300")

// Ensure the encoded block matches the expected bytes.
if !bytes.Equal(buf.Bytes(), simNetGenesisBlockBytes) {
Expand Down
2 changes: 1 addition & 1 deletion chaincfg/params.go
Expand Up @@ -624,7 +624,7 @@ var MainNetParams = Params{
StakeVersionInterval: 144 * 2 * 7, // ~1 week
MaxFreshStakePerBlock: 20, // 4*TicketsPerBlock
StakeEnabledHeight: 16 + 16/*256 + 256*/, // CoinbaseMaturity + TicketMaturity
StakeValidationHeight: 768, // ~14 days
StakeValidationHeight: 1024, // ~14 days
StakeBaseSigScript: []byte{0x00, 0x00},
StakeMajorityMultiplier: 3,
StakeMajorityDivisor: 4,
Expand Down
12 changes: 10 additions & 2 deletions crypto/bliss/bliss.go
Expand Up @@ -289,8 +289,16 @@ func newBlissDSA() DSA {
if err != nil{
return nil, err
}
priv1 := priv.(PrivateKey)
sig, err := priv1.Sign(hash, entropy)
var sig *bliss.Signature
switch priv.(type){
case PrivateKey:
pv := priv.(PrivateKey)
sig, err = pv.Sign(hash, entropy)
case *PrivateKey:
pvptr := priv.(*PrivateKey)
sig, err = pvptr.Sign(hash, entropy)
}

if err != nil{
return nil, err
}
Expand Down
8 changes: 6 additions & 2 deletions txscript/reference_test.go
Expand Up @@ -181,7 +181,7 @@ func createSpendingTx(sigScript, pkScript []byte) *wire.MsgTx {

// TestScriptInvalidTests ensures all of the tests in script_invalid.json fail
// as expected.
func DNWTestScriptInvalidTests(t *testing.T) {
func TestScriptInvalidTests(t *testing.T) {
file, err := ioutil.ReadFile("data/script_invalid.json")
if err != nil {
t.Errorf("TestScriptInvalidTests: %v\n", err)
Expand Down Expand Up @@ -249,7 +249,7 @@ func DNWTestScriptInvalidTests(t *testing.T) {

// TestScriptValidTests ensures all of the tests in script_valid.json pass as
// expected.
func DNWTestScriptValidTests(t *testing.T) {
func TestScriptValidTests(t *testing.T) {
file, err := ioutil.ReadFile("data/script_valid.json")
if err != nil {
t.Errorf("TestScriptValidTests: %v\n", err)
Expand Down Expand Up @@ -284,16 +284,19 @@ func DNWTestScriptValidTests(t *testing.T) {
t.Errorf("%s: can't parse scriptSig; %v", name, err)
continue
}

scriptPubKey, err := parseShortForm(test[1])
if err != nil {
t.Errorf("%s: can't parse scriptPubkey; %v", name, err)
continue
}

flags, err := parseScriptFlags(test[2])
if err != nil {
t.Errorf("%s: %v", name, err)
continue
}

tx := createSpendingTx(scriptSig, scriptPubKey)

var vm *Engine
Expand All @@ -309,6 +312,7 @@ func DNWTestScriptValidTests(t *testing.T) {
t.Errorf("%s failed to create script: %v", name, err)
continue
}

err = vm.Execute()
if err != nil {
t.Errorf("%s failed to execute: %v", name, err)
Expand Down
2 changes: 1 addition & 1 deletion txscript/script.go
Expand Up @@ -134,7 +134,7 @@ func parseAltScriptTemplate(script []byte, opcodes *[256]opcode) ([]parsedOpcode
instr := script[i]

// TODO for read
if i == 24 && script[i] < (OP_1) {
if i == 24 && script[i] >= (OP_DATA_1) && script[i] <= (OP_DATA_16) {
instr = instr + 80
}
op := &opcodes[instr]
Expand Down

0 comments on commit 8e7c7a6

Please sign in to comment.