mirror of
https://github.com/number571/blockchain.git
synced 2026-09-14 11:05:43 +05:00
update
This commit is contained in:
parent
72f60078bc
commit
eb2cb3a027
Binary file not shown.
@ -125,6 +125,8 @@ func handleClient() {
|
||||
userPurse()
|
||||
case "balance":
|
||||
userBalance()
|
||||
default:
|
||||
fmt.Println("command undefined\n")
|
||||
}
|
||||
case "/chain":
|
||||
if len(splited) < 2 {
|
||||
@ -138,6 +140,12 @@ func handleClient() {
|
||||
chainTX(splited[1:])
|
||||
case "balance":
|
||||
chainBalance(splited[1:])
|
||||
case "block":
|
||||
chainBlock(splited[1:])
|
||||
case "size":
|
||||
chainSize()
|
||||
default:
|
||||
fmt.Println("command undefined\n")
|
||||
}
|
||||
default:
|
||||
fmt.Println("command undefined\n")
|
||||
@ -219,6 +227,38 @@ func chainBalance(splited []string) {
|
||||
printBalance(splited[1])
|
||||
}
|
||||
|
||||
func chainBlock(splited []string) {
|
||||
if len(splited) != 2 {
|
||||
fmt.Println("failed: len(splited) != 2\n")
|
||||
return
|
||||
}
|
||||
num, err := strconv.Atoi(splited[1])
|
||||
if err != nil {
|
||||
fmt.Println("failed: strconv.Atoi(num)\n")
|
||||
return
|
||||
}
|
||||
res := nt.Send(Addresses[0], &nt.Package{
|
||||
Option: GET_BLOCK,
|
||||
Data: fmt.Sprintf("%d", num-1),
|
||||
})
|
||||
if res == nil || res.Data == "" {
|
||||
fmt.Println("failed: getBlock\n")
|
||||
return
|
||||
}
|
||||
fmt.Printf("[%d] => %s\n", num, res.Data)
|
||||
}
|
||||
|
||||
func chainSize() {
|
||||
res := nt.Send(Addresses[0], &nt.Package{
|
||||
Option: GET_CSIZE,
|
||||
})
|
||||
if res == nil || res.Data == "" {
|
||||
fmt.Println("failed: getSize\n")
|
||||
return
|
||||
}
|
||||
fmt.Printf("Size: %s blocks\n\n", res.Data)
|
||||
}
|
||||
|
||||
func printBalance(useraddr string) {
|
||||
for _, addr := range Addresses {
|
||||
res := nt.Send(addr, &nt.Package{
|
||||
@ -226,7 +266,7 @@ func printBalance(useraddr string) {
|
||||
Data: useraddr,
|
||||
})
|
||||
if res == nil {
|
||||
continue
|
||||
continue
|
||||
}
|
||||
fmt.Printf("Balance (%s): %s coins\n", addr, res.Data)
|
||||
}
|
||||
@ -239,6 +279,7 @@ const (
|
||||
GET_BLOCK
|
||||
GET_LHASH
|
||||
GET_BLNCE
|
||||
GET_CSIZE
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
@ -64,6 +64,7 @@ const (
|
||||
GET_BLOCK
|
||||
GET_LHASH
|
||||
GET_BLNCE
|
||||
GET_CSIZE
|
||||
)
|
||||
|
||||
func init() {
|
||||
@ -191,6 +192,7 @@ func handleServer(conn nt.Conn, pack *nt.Package) {
|
||||
nt.Handle(GET_BLOCK, conn, pack, getBlock)
|
||||
nt.Handle(GET_LHASH, conn, pack, getLastHash)
|
||||
nt.Handle(GET_BLNCE, conn, pack, getBalance)
|
||||
nt.Handle(GET_CSIZE, conn, pack, getChainSize)
|
||||
}
|
||||
|
||||
func addBlock(pack *nt.Package) string {
|
||||
@ -273,6 +275,10 @@ func getBalance(pack *nt.Package) string {
|
||||
return fmt.Sprintf("%d", Chain.Balance(pack.Data, Chain.Size()))
|
||||
}
|
||||
|
||||
func getChainSize(pack *nt.Package) string {
|
||||
return fmt.Sprintf("%d", Chain.Size())
|
||||
}
|
||||
|
||||
const (
|
||||
SEPARATOR = "_SEPARATOR_"
|
||||
)
|
||||
|
||||
10
client.go
10
client.go
@ -90,6 +90,8 @@ func handleClient() {
|
||||
userPurse()
|
||||
case "balance":
|
||||
userBalance()
|
||||
default:
|
||||
fmt.Println("command undefined\n")
|
||||
}
|
||||
case "/chain":
|
||||
if len(splited) < 2 {
|
||||
@ -107,6 +109,8 @@ func handleClient() {
|
||||
chainBlock(splited[1:])
|
||||
case "size":
|
||||
chainSize()
|
||||
default:
|
||||
fmt.Println("command undefined\n")
|
||||
}
|
||||
default:
|
||||
fmt.Println("command undefined\n")
|
||||
@ -122,7 +126,7 @@ func chainSize() {
|
||||
fmt.Println("failed: getSize\n")
|
||||
return
|
||||
}
|
||||
fmt.Println("Size:", res.Data, "\n")
|
||||
fmt.Printf("Size: %s blocks\n\n", res.Data)
|
||||
}
|
||||
|
||||
func chainBlock(splited []string) {
|
||||
@ -137,7 +141,7 @@ func chainBlock(splited []string) {
|
||||
}
|
||||
res := nt.Send(Addresses[0], &nt.Package{
|
||||
Option: GET_BLOCK,
|
||||
Data: fmt.Sprintf("%d", num),
|
||||
Data: fmt.Sprintf("%d", num-1),
|
||||
})
|
||||
if res == nil || res.Data == "" {
|
||||
fmt.Println("failed: getBlock\n")
|
||||
@ -231,5 +235,5 @@ func userBalance() {
|
||||
func inputString(begin string) string {
|
||||
fmt.Print(begin)
|
||||
msg, _ := bufio.NewReader(os.Stdin).ReadString('\n')
|
||||
return strings.Replace(msg, "\r\n", "", 1)
|
||||
return strings.Replace(msg, "\n", "", 1)
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user