2018-03-22 10:47:13 -07:00
|
|
|
package pow
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
)
|
|
|
|
|
2018-04-18 21:49:24 -07:00
|
|
|
func TestNewMsgMine(t *testing.T) {
|
2018-03-22 10:47:13 -07:00
|
|
|
addr := sdk.Address([]byte("sender"))
|
2018-04-18 21:49:24 -07:00
|
|
|
msg := MsgMine{addr, 0, 0, 0, []byte("")}
|
|
|
|
equiv := NewMsgMine(addr, 0, 0, 0, []byte(""))
|
2018-03-22 10:47:13 -07:00
|
|
|
assert.Equal(t, msg, equiv, "%s != %s", msg, equiv)
|
|
|
|
}
|
|
|
|
|
2018-04-18 21:49:24 -07:00
|
|
|
func TestMsgMineType(t *testing.T) {
|
2018-03-22 10:47:13 -07:00
|
|
|
addr := sdk.Address([]byte("sender"))
|
2018-04-18 21:49:24 -07:00
|
|
|
msg := MsgMine{addr, 0, 0, 0, []byte("")}
|
2018-04-05 05:13:11 -07:00
|
|
|
assert.Equal(t, msg.Type(), "pow")
|
2018-03-22 10:47:13 -07:00
|
|
|
}
|
|
|
|
|
2018-04-18 21:49:24 -07:00
|
|
|
func TestMsgMineValidation(t *testing.T) {
|
2018-03-22 10:47:13 -07:00
|
|
|
addr := sdk.Address([]byte("sender"))
|
|
|
|
otherAddr := sdk.Address([]byte("another"))
|
|
|
|
count := uint64(0)
|
2018-04-18 21:49:24 -07:00
|
|
|
|
2018-03-22 10:47:13 -07:00
|
|
|
for difficulty := uint64(1); difficulty < 1000; difficulty += 100 {
|
2018-04-18 21:49:24 -07:00
|
|
|
|
|
|
|
count++
|
2018-03-22 10:47:13 -07:00
|
|
|
nonce, proof := mine(addr, count, difficulty)
|
2018-04-18 21:49:24 -07:00
|
|
|
msg := MsgMine{addr, difficulty, count, nonce, proof}
|
2018-03-22 10:47:13 -07:00
|
|
|
err := msg.ValidateBasic()
|
|
|
|
assert.Nil(t, err, "error with difficulty %d - %+v", difficulty, err)
|
|
|
|
|
2018-04-18 21:49:24 -07:00
|
|
|
msg.Count++
|
2018-03-22 10:47:13 -07:00
|
|
|
err = msg.ValidateBasic()
|
|
|
|
assert.NotNil(t, err, "count was wrong, should have thrown error with msg %s", msg)
|
|
|
|
|
2018-04-18 21:49:24 -07:00
|
|
|
msg.Count--
|
|
|
|
msg.Nonce++
|
2018-03-22 10:47:13 -07:00
|
|
|
err = msg.ValidateBasic()
|
|
|
|
assert.NotNil(t, err, "nonce was wrong, should have thrown error with msg %s", msg)
|
|
|
|
|
2018-04-18 21:49:24 -07:00
|
|
|
msg.Nonce--
|
2018-03-22 10:47:13 -07:00
|
|
|
msg.Sender = otherAddr
|
|
|
|
err = msg.ValidateBasic()
|
|
|
|
assert.NotNil(t, err, "sender was wrong, should have thrown error with msg %s", msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-18 21:49:24 -07:00
|
|
|
func TestMsgMineString(t *testing.T) {
|
2018-03-22 10:47:13 -07:00
|
|
|
addr := sdk.Address([]byte("sender"))
|
2018-04-18 21:49:24 -07:00
|
|
|
msg := MsgMine{addr, 0, 0, 0, []byte("abc")}
|
2018-03-22 10:47:13 -07:00
|
|
|
res := msg.String()
|
2018-04-18 21:49:24 -07:00
|
|
|
assert.Equal(t, res, "MsgMine{Sender: 73656E646572, Difficulty: 0, Count: 0, Nonce: 0, Proof: abc}")
|
2018-03-22 10:47:13 -07:00
|
|
|
}
|
|
|
|
|
2018-04-18 21:49:24 -07:00
|
|
|
func TestMsgMineGet(t *testing.T) {
|
2018-03-22 10:47:13 -07:00
|
|
|
addr := sdk.Address([]byte("sender"))
|
2018-04-18 21:49:24 -07:00
|
|
|
msg := MsgMine{addr, 0, 0, 0, []byte("")}
|
2018-03-22 10:47:13 -07:00
|
|
|
res := msg.Get(nil)
|
|
|
|
assert.Nil(t, res)
|
|
|
|
}
|
|
|
|
|
2018-04-18 21:49:24 -07:00
|
|
|
func TestMsgMineGetSignBytes(t *testing.T) {
|
2018-03-22 10:47:13 -07:00
|
|
|
addr := sdk.Address([]byte("sender"))
|
2018-04-18 21:49:24 -07:00
|
|
|
msg := MsgMine{addr, 1, 1, 1, []byte("abc")}
|
2018-03-22 10:47:13 -07:00
|
|
|
res := msg.GetSignBytes()
|
|
|
|
assert.Equal(t, string(res), `{"sender":"73656E646572","difficulty":1,"count":1,"nonce":1,"proof":"YWJj"}`)
|
|
|
|
}
|
|
|
|
|
2018-04-18 21:49:24 -07:00
|
|
|
func TestMsgMineGetSigners(t *testing.T) {
|
2018-03-22 10:47:13 -07:00
|
|
|
addr := sdk.Address([]byte("sender"))
|
2018-04-18 21:49:24 -07:00
|
|
|
msg := MsgMine{addr, 1, 1, 1, []byte("abc")}
|
2018-03-22 10:47:13 -07:00
|
|
|
res := msg.GetSigners()
|
|
|
|
assert.Equal(t, fmt.Sprintf("%v", res), "[73656E646572]")
|
|
|
|
}
|