Skip to content

Commit

Permalink
fix outpoint tests
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsuo committed Dec 15, 2014
1 parent 6968b19 commit a93271a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 40 deletions.
27 changes: 18 additions & 9 deletions src/messages.jl
Original file line number Diff line number Diff line change
Expand Up @@ -121,25 +121,34 @@ function convert(::Type{Array{Uint8}}, outpoint::OutPoint)
end

type Tx_Input
previous_output::OutPoint # Previous output tx, as OutPoint structure
scriptSig_length::Int # Variable length integer: length of scriptSig
scriptSig::Array{Int8} # Script to confirm tx authorization
sequence::Uint32 # Transaction version as defined by the sender
previous_output::OutPoint # Previous output tx, as OutPoint
scriptSig_length::Int # Variable length int: scriptSig len
scriptSig::Array{Uint8} # Script to confirm tx authorization
sequence::Uint32 # Tx version as defined by the sender

function Tx_Input(previous_output::OutPoint, scriptSig::String; sequence = 0)
scriptSig = hex_string_to_array(scriptSig)
Tx_Input(previous_output, scriptSig, sequence)
end
function Tx_Input(previous_output::OutPoint, scriptSig::Array{Uint8}; sequence = 0)
scriptSig_length = length(scriptSig)
sequence = uint32(sequence)
new(previous_output, scriptSig_length, scriptSig)
end

end

type Tx_Output
# ERROR: does Uint64 exist on 32-bit OS?
value::Uint64 # Transaction value
scriptPubKey_length::Array{Uint8} # Variable length int: scriptPubKey len
scriptPubKey_length::Int # Variable length int: scriptPubKey len
scriptPubKey::Array{Uint8} # Script for claiming tx output

# value: transaction value in Satoshi
# scriptPubKey: script as hex string
function Tx_Output(value, scriptPubKey::String)
value = uint64(value)
scriptPubKey = hex_string_to_array(scriptPubKey)
scriptPubKey_length = to_varint(length(scriptPubKey))
new(value, scriptPubKey_length, scriptPubKey)
Tx_Output(value, scriptPubKey)
end

# value: transaction value in Satoshi
Expand All @@ -158,7 +167,7 @@ function convert(::Type{Array{Uint8}}, tx_out::Tx_Output)
# endian byte array
append!(result, reverse(bytearray(tx_out.value)))

append!(result, reverse(tx_out.scriptPubKey_length))
append!(result, reverse(to_varint(tx_out.scriptPubKey_length)))

append!(result, tx_out.scriptPubKey)

Expand Down
62 changes: 31 additions & 31 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ using Base.Test
base58data = parseint(BigInt, "800c28fca386c7a227600b2fe50b7cae11ec86d3bf1fbe471be89827e19d72aa1d507a5b8d", 16)

# Base 58 encoding
@test Coin.encode58(base58data) == "5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ"
@test encode58(base58data) == "5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ"

# Base 58 decoding
@test Coin.decode58("5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ") == base58data
@test decode58("5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ") == base58data

##############################################################################
##
Expand All @@ -33,7 +33,7 @@ base58data = parseint(BigInt, "800c28fca386c7a227600b2fe50b7cae11ec86d3bf1fbe471
##############################################################################

secret_key = "18E14A7B6A307F426A94F8114701E7C8E774E7F9A47E2C2035DB29A206321725"
public_key = Coin.get_public_key(secret_key)
public_key = get_public_key(secret_key)
@test public_key == "16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM"

##############################################################################
Expand All @@ -43,13 +43,13 @@ public_key = Coin.get_public_key(secret_key)
##############################################################################

# Private key to WIF
@test Coin.private2wif("0c28fca386c7a227600b2fe50b7cae11ec86d3bf1fbe471be89827e19d72aa1d") == "5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ"
@test private2wif("0c28fca386c7a227600b2fe50b7cae11ec86d3bf1fbe471be89827e19d72aa1d") == "5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ"

# WIF to private key
@test Coin.wif2private("5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ") == "0c28fca386c7a227600b2fe50b7cae11ec86d3bf1fbe471be89827e19d72aa1d"
@test wif2private("5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ") == "0c28fca386c7a227600b2fe50b7cae11ec86d3bf1fbe471be89827e19d72aa1d"

# WIF checksum
@test Coin.wif_check_sum("5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ")
@test wif_check_sum("5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ")

##############################################################################
##
Expand All @@ -59,20 +59,20 @@ public_key = Coin.get_public_key(secret_key)

# Test header generation
payload = "0100000001484d40d45b9ea0d652fca8258ab7caa42541eb52975857f96fb50cd732c8b481000000008a47304402202cb265bf10707bf49346c3515dd3d16fc454618c58ec0a0ff448a676c54ff71302206c6624d762a1fcef4618284ead8f08678ac05b13c84235f1654e6ad168233e8201410414e301b2328f17442c0b8310d787bf3d8a404cfbd0704f135b6ad4b2d3ee751310f981926e53a6e8c39bd7d3fefd576c543cce493cbac06388f2651d1aacbfcdffffffff0162640100000000001976a914c8e90996c7c6080ee06284600c684ed904d14c5c88ac00000000"
# @test Coin.create_header(magic_mainnet, "tx", payload) == "f9beb4d9747800000000000000000000df000000ea0f5494"
# @test create_header(magic_mainnet, "tx", payload) == "f9beb4d9747800000000000000000000df000000ea0f5494"

# Transaction output
tx_out = Coin.Tx_Output(123, "abc")
tx_out = Tx_Output(123, "abc")
@test bytearray(tx_out) == [0x7b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x0a, 0xbc]
tx_out = Coin.Tx_Output(5000000, "76A9141AA0CD1CBEA6E7458A7ABAD512A9D9EA1AFB225E88AC")
tx_out = Tx_Output(5000000, "76A9141AA0CD1CBEA6E7458A7ABAD512A9D9EA1AFB225E88AC")
@test bytearray(tx_out) == [0x40, 0x4b, 0x4c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x19, 0x76, 0xa9, 0x14, 0x1a, 0xa0, 0xcd, 0x1c, 0xbe, 0xa6, 0xe7, 0x45, 0x8a, 0x7a, 0xba, 0xd5, 0x12, 0xa9, 0xd9, 0xea, 0x1a, 0xfb, 0x22, 0x5e, 0x88, 0xac]

# Transaction outpoint (i.e., reference to previous transaction)
@test_throws ErrorException Coin.OutPoint(1, "abc")
@test_throws ErrorException OutPoint(1, "abc")
outpoint = OutPoint(1, "abcaabcaabcaabcaabcaabcaabcaabcaabcaabcaabcaabcaabcaabcaabcaabca")
@test bytearray(outpoint) == [0x01, 0x00, 0x00, 0x00, 0xca, 0xab, 0xca, 0xab, 0xca, 0xab, 0xca, 0xab, 0xca, 0xab, 0xca, 0xab, 0xca, 0xab, 0xca, 0xab, 0xca, 0xab, 0xca, 0xab, 0xca, 0xab, 0xca, 0xab, 0xca, 0xab, 0xca, 0xab, 0xca, 0xab, 0xca, 0xab]
outpoint = OutPoint(0, "81b4c832d70cb56ff957589752eb4125a4cab78a25a8fc52d6a09e5bd4404d48")
@test bytearray(outpoint) == [0x00, 0x00, 0x00, 0x00, 0x81, 0xb4, 0xc8, 0x32, 0xd7, 0x0c, 0xb5, 0x6f, 0xf9, 0x57, 0x58, 0x97, 0x52, 0xeb, 0x41, 0x25, 0xa4, 0xca, 0xb7, 0x8a, 0x25, 0xa8, 0xfc, 0x52, 0xd6, 0xa0, 0x9e, 0x5b, 0xd4, 0x40, 0x4d, 0x48]
@test bytearray(outpoint) == [0x00, 0x00, 0x00, 0x00, 0x48, 0x4d, 0x40, 0xd4, 0x5b, 0x9e, 0xa0, 0xd6, 0x52, 0xfc, 0xa8, 0x25, 0x8a, 0xb7, 0xca, 0xa4, 0x25, 0x41, 0xeb, 0x52, 0x97, 0x58, 0x57, 0xf9, 0x6f, 0xb5, 0x0c, 0xd7, 0x32, 0xc8, 0xb4, 0x81]


##############################################################################
Expand All @@ -82,30 +82,30 @@ outpoint = OutPoint(0, "81b4c832d70cb56ff957589752eb4125a4cab78a25a8fc52d6a09e5b
##############################################################################

# Reverse endian of hex string
@test Coin.reverse_endian("") == ""
@test Coin.reverse_endian("0c28fca386c7a227600b2fe50b7cae11ec86d3bf1fbe471be89827e19d72aa1d") == "1daa729de12798e81b47be1fbfd386ec11ae7c0be52f0b6027a2c786a3fc280c"
@test Coin.reverse_endian(0) == 0
@test Coin.reverse_endian(0x22) == 0x22
@test Coin.reverse_endian(0x333) == 0x3303
@test Coin.reverse_endian(0x2234) == 0x3422
@test Coin.reverse_endian(0xf9beb4d9) == 0xd9b4bef9
@test Coin.reverse_endian(BigInt(19238471923847192837419283749128374912837491823742198374)) == 9830438508025927557749821611753759704769006686345681864
@test reverse_endian("") == ""
@test reverse_endian("0c28fca386c7a227600b2fe50b7cae11ec86d3bf1fbe471be89827e19d72aa1d") == "1daa729de12798e81b47be1fbfd386ec11ae7c0be52f0b6027a2c786a3fc280c"
@test reverse_endian(0) == 0
@test reverse_endian(0x22) == 0x22
@test reverse_endian(0x333) == 0x3303
@test reverse_endian(0x2234) == 0x3422
@test reverse_endian(0xf9beb4d9) == 0xd9b4bef9
@test reverse_endian(BigInt(19238471923847192837419283749128374912837491823742198374)) == 9830438508025927557749821611753759704769006686345681864
# reverse_endian assumes that BigInt is at least 16 bytes and Int is system-dependent
@test Coin.reverse_endian(BigInt(24)) == 31901471898837980949691369446728269824
@test Coin.reverse_endian(23) == 1657324662872342528
@test Coin.reverse_endian(-23) == -1585267068834414593
@test reverse_endian(BigInt(24)) == 31901471898837980949691369446728269824
@test reverse_endian(23) == 1657324662872342528
@test reverse_endian(-23) == -1585267068834414593

# hex_string_to_array: empty string
@test Coin.hex_string_to_array("") == []
@test hex_string_to_array("") == []
# hex_string_to_array: odd- and even-length strings
@test Coin.hex_string_to_array("adfcef981") == [0x0a, 0xdf, 0xce, 0xf9, 0x81]
@test Coin.hex_string_to_array("aadfcef981") == [0xaa, 0xdf, 0xce, 0xf9, 0x81]
@test hex_string_to_array("adfcef981") == [0x0a, 0xdf, 0xce, 0xf9, 0x81]
@test hex_string_to_array("aadfcef981") == [0xaa, 0xdf, 0xce, 0xf9, 0x81]

# Test VarInt conversion
@test Coin.to_varint(1) == [0x01]
@test Coin.to_varint(252) == [0xfc]
@test Coin.to_varint(253) == [0xfd, 0x00, 0xfd]
@test Coin.to_varint(0xffff) == [0xfd, 0xff, 0xff]
@test Coin.to_varint(0x10000) == [0xfe, 0x00, 0x01, 0x00, 0x00]
@test Coin.to_varint(0x100000000) == [0xff, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00]
@test to_varint(1) == [0x01]
@test to_varint(252) == [0xfc]
@test to_varint(253) == [0xfd, 0x00, 0xfd]
@test to_varint(0xffff) == [0xfd, 0xff, 0xff]
@test to_varint(0x10000) == [0xfe, 0x00, 0x01, 0x00, 0x00]
@test to_varint(0x100000000) == [0xff, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00]

0 comments on commit a93271a

Please sign in to comment.