Created
July 19, 2019 12:56
-
-
Save tenuki/66eb5875635b59aae91c9ced65f75404 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*----------------------------------------------------------------------------------- | |
SIMPLE REVERSI | |
---------------------------------------------------------------------------------- */ | |
const os = require("os"); | |
const EventEmitter = require('events'); | |
const { | |
Channel, | |
Crypto, | |
Universal | |
} = require('@aeternity/aepp-sdk'); | |
const fs = require('fs'); | |
const bs58 = require('bs58'); | |
(async function () { | |
/*----------------------------------------------------------------------------------- | |
IMPORTS | |
---------------------------------------------------------------------------------- */ | |
let SignerFunc; | |
const compilerURL = 'https://compiler.aepps.com'; | |
/*----------------------------------------------------------------------------------- | |
GLOBAL VARIABLES | |
---------------------------------------------------------------------------------- */ | |
const NETWORK_ID = 'ae_devnet'; | |
let playerPair; | |
let i_addr; | |
let r_addr; | |
let player; | |
let opposite; | |
let channel; | |
let is_initiator = !(process.argv[2][0].toLowerCase() === "i"); | |
let role; | |
let STATUS = ""; | |
/*----------------------------------------------------------------------------------- | |
UTIL FUNCTIONS | |
---------------------------------------------------------------------------------- */ | |
const null_func = () => {}; | |
function call_async(f, ...args) { | |
f(...args).then(null_func).catch(console.error); | |
} | |
async function sleep(ms) { | |
return new Promise((resolve, reject) => { | |
try { | |
setTimeout(resolve, ms) | |
} catch (err) { | |
reject(err) | |
} | |
}); | |
} | |
async function wait_state(expected) { | |
return await wait_for(() => STATUS === expected); | |
} | |
async function wait_for(func) { | |
while (!func()) { | |
await sleep(100); | |
} | |
} | |
/*----------------------------------------------------------------------------------- | |
DEFINITIONS | |
---------------------------------------------------------------------------------- */ | |
const same_port = process.env["SAMEPORT"]?true : false; | |
if (is_initiator) { | |
playerPair = { | |
publicKey: 'ak_2mwRmUeYmfuW93ti9HMSUJzCk1EYcQEfikVSzgo6k2VghsWhgU', | |
secretKey: 'bb9f0b01c8c9553cfbaf7ef81a50f977b1326801ebf7294d1c2cbccdedf27476e9bbf604e611b5460a3b3999e9771b6f60417d73ce7c5519e12f7e127a1225ca' | |
}; | |
opposite = 'ak_fUq2NesPXcYZ1CcqBcGC3StpdnQw3iVxMA3YSeCNAwfN4myQk'; | |
role = 'initiator'; | |
port = 3001; | |
oport = same_port? 3001 : 3002; | |
} else { | |
playerPair = { | |
publicKey: 'ak_fUq2NesPXcYZ1CcqBcGC3StpdnQw3iVxMA3YSeCNAwfN4myQk', | |
secretKey: '7c6e602a94f30e4ea7edabe4376314f69ba7eaa2f355ecedb339df847b6f0d80575f81ffb0a297b7725dc671da0b1769b1fc5cbe45385c7b5ad1fc2eaf1d609d' | |
}; | |
opposite = 'ak_2mwRmUeYmfuW93ti9HMSUJzCk1EYcQEfikVSzgo6k2VghsWhgU'; | |
role = 'responder'; | |
port = same_port? 3001 : 3002; | |
oport = 3001; | |
} | |
let URL = 'localhost:'+port; | |
URL = process.env["NODE"]?process.env["NODE"] : URL; | |
console.log("Node> ", URL) | |
const API_URL = "http://" + URL; | |
const WS_URL = "ws://" + URL; | |
const INTERNAL_API_URL = API_URL; | |
i_addr = 'ak_2mwRmUeYmfuW93ti9HMSUJzCk1EYcQEfikVSzgo6k2VghsWhgU'; | |
r_addr = 'ak_fUq2NesPXcYZ1CcqBcGC3StpdnQw3iVxMA3YSeCNAwfN4myQk'; | |
player = await Universal({ | |
networkId: NETWORK_ID, | |
url: API_URL, | |
internalUrl: INTERNAL_API_URL, | |
keypair: playerPair, | |
compilerUrl: compilerURL | |
}); | |
SignerFunc = async (tag, tx) => { | |
if (tag === "shutdown_sign_ack") { | |
console.log("TX:", tx) | |
} | |
return player.signTransaction(tx) | |
}; | |
if (is_initiator) { | |
console.log("SPENT:", (await player.spend('6000000000000000', opposite)).tx.amount); | |
console.log(); | |
} | |
let options = { | |
url: WS_URL+'/channel', //'ws://localhost:3001' | |
pushAmount: 3, | |
initiatorAmount: 1000000000000000, | |
responderAmount: 1000000000000000, | |
channelReserve: 20000000000, | |
ttl: 10000, | |
host: URL.substr(0, URL.indexOf(":")), | |
port: oport, | |
lockPeriod: 1, | |
initiatorId: i_addr, | |
responderId: r_addr, | |
role: role, | |
}; | |
console.log(options); | |
options["sign"] = SignerFunc; | |
channel = await Channel(options); | |
channel.on('statusChanged', (status) => { | |
STATUS = status.toUpperCase(); | |
console.log(`[${STATUS}]`); | |
console.log(); | |
if (STATUS === "DISCONNECTED") { | |
process.exit(0); | |
} | |
}); | |
channel.on('message', (msg) => { | |
if (msg.info.split(" ")[0] === "contract_address") { | |
contract_address = msg.info.split(" ")[1]; | |
} | |
}); | |
/*----------------------------------------------------------------------------------- | |
DEPLOY SMART CONTRACT | |
---------------------------------------------------------------------------------- */ | |
await wait_state("OPEN"); | |
state.emit("init"); | |
/*----------------------------------------------------------------------------------- | |
CLOSING CHANNEL | |
---------------------------------------------------------------------------------- */ | |
async function end() { | |
console.log("game finished!!!"); | |
if (is_initiator) { | |
await sleep(200); | |
channel.shutdown(tx => SignerFunc('tag_SHUTDOWN', tx)) | |
.then((tx) => { | |
console.log('==> State channel has been closed. You can track this transaction on chain', tx) | |
}).catch(e => { | |
console.log('==> Error:', e); | |
}); | |
} | |
} | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2019-07-18 22:46:47.043 [debug] <0.23024.264>@sc_ws_handler:websocket_init:40 Starting Channel WS with params #{<<"channel_reserve">> => <<"20000000000">>,<<"host">> => <<"10.10.0.79">>,<<"initiator_amount">> => <<"1000000000000000">>,<<"initiator_id">> => <<"ak_2mwRmUeYmfuW93ti9HMSUJzCk1EYcQEfikVSzgo6k2VghsWhgU">>,<<"lock_period">> => <<"1">>,<<"port">> => <<"3001">>,<<"protocol">> => <<"json-rpc">>,<<"push_amount">> => <<"3">>,<<"responder_amount">> => <<"1000000000000000">>,<<"responder_id">> => <<"ak_fUq2NesPXcYZ1CcqBcGC3StpdnQw3iVxMA3YSeCNAwfN4myQk">>,<<"role">> => <<"responder">>,<<"sign">> => <<"async (tag, tx) => {\n if (tag === \"shutdown_sign_ack\") {\n console.log(\"TX:\", tx)\n }\n return player.signTransaction(tx)\n }">>,<<"ttl">> => <<"10000">>,<<"url">> => <<"ws://10.10.0.79:3001/channel">>} | |
2019-07-18 22:46:47.064 [info] <0.23024.264>@sc_ws_handler:websocket_init:46 Channel WS failed to start because of {{badmatch,{error,eaddrinuse}},[{aesc_session_noise,establish,2,[{file,"/app/apps/aechannel/src/aesc_session_noise.erl"},{line,131}]},{aesc_session_noise,init,1,[{file,"/app/apps/aechannel/src/aesc_session_noise.erl"},{line,124}]},{gen_server,init_it,2,[{file,"gen_server.erl"},{line,365}]},{gen_server,init_it,6,[{file,"gen_server.erl"},{line,333}]},{proc_lib,init_p_do_apply,3,[{file,"proc_lib.erl"},{line,247}]}]}; params #{<<"channel_reserve">> => <<"20000000000">>,<<"host">> => <<"10.10.0.79">>,<<"initiator_amount">> => <<"1000000000000000">>,<<"initiator_id">> => <<"ak_2mwRmUeYmfuW93ti9HMSUJzCk1EYcQEfikVSzgo6k2VghsWhgU">>,<<"lock_period">> => <<"1">>,<<"port">> => <<"3001">>,<<"protocol">> => <<"json-rpc">>,<<"push_amount">> => <<"3">>,<<"responder_amount">> => <<"1000000000000000">>,<<"responder_id">> => <<"ak_fUq2NesPXcYZ1CcqBcGC3StpdnQw3iVxMA3YSeCNAwfN4myQk">>,<<"role">> => <<"responder">>,<<"sign">> => <<"async (tag, tx) => {\n if (tag === \"shutdown_sign_ack\") {\n console.log(\"TX:\", tx)\n }\n return player.signTransaction(tx)\n }">>,<<"ttl">> => <<"10000">>,<<"url">> => <<"ws://10.10.0.79:3001/channel">>} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2019-07-18 22:11:17.364 [debug] <0.17034.1>@sc_ws_handler:websocket_init:40 Starting Channel WS with params #{<<"channel_reserve">> => <<"20000000000">>,<<"host">> => <<"10.10.0.79">>,<<"initiator_amount">> => <<"1000000000000000">>,<<"initiator_id">> => <<"ak_2mwRmUeYmfuW93ti9HMSUJzCk1EYcQEfikVSzgo6k2VghsWhgU">>,<<"lock_period">> => <<"1">>,<<"port">> => <<"3001">>,<<"protocol">> => <<"json-rpc">>,<<"push_amount">> => <<"3">>,<<"responder_amount">> => <<"1000000000000000">>,<<"responder_id">> => <<"ak_fUq2NesPXcYZ1CcqBcGC3StpdnQw3iVxMA3YSeCNAwfN4myQk">>,<<"role">> => <<"responder">>,<<"sign">> => <<"async (tag, tx) => {\n // console.log(\"************ TAG:\", tag);\n if (tag === \"shutdown_sign_ack\") {\n console.log(\"TX:\", tx)\n }\n if (tag === \"update_ack\") {\n ResponderRecvdContract = true;\n state.on_update();\n }\n if (tag === PLACE_DISC_TAG) {\n state.on_update();\n }\n return player.signTransaction(tx)\n }">>,<<"ttl">> => <<"10000">>,<<"url">> => <<"ws://10.10.0.79:3002/channel">>} | |
and error here: | |
2019-07-18 22:11:17.393 [info] <0.17034.1>@sc_ws_handler:websocket_init:46 Channel WS failed to start because of not_found; params #{<<"channel_reserve">> => <<"20000000000">>,<<"host">> => <<"10.10.0.79">>,<<"initiator_amount">> => <<"1000000000000000">>,<<"initiator_id">> => <<"ak_2mwRmUeYmfuW93ti9HMSUJzCk1EYcQEfikVSzgo6k2VghsWhgU">>,<<"lock_period">> => <<"1">>,<<"port">> => <<"3001">>,<<"protocol">> => <<"json-rpc">>,<<"push_amount">> => <<"3">>,<<"responder_amount">> => <<"1000000000000000">>,<<"responder_id">> => <<"ak_fUq2NesPXcYZ1CcqBcGC3StpdnQw3iVxMA3YSeCNAwfN4myQk">>,<<"role">> => <<"responder">>,<<"sign">> => <<"async (tag, tx) => {\n // console.log(\"************ TAG:\", tag);\n if (tag === \"shutdown_sign_ack\") {\n console.log(\"TX:\", tx)\n }\n if (tag === \"update_ack\") {\n ResponderRecvdContract = true;\n state.on_update();\n }\n if (tag === PLACE_DISC_TAG) {\n state.on_update();\n }\n return player.signTransaction(tx)\n }">>,<<"ttl">> => <<"10000">>,<<"url">> => <<"ws://10.10.0.79:3002/channel">>} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As original code, connect to same port:
changed to use different: