Skip to content

Commit ecd7199

Browse files
Gustav Simonssonkaralabe
authored andcommitted
common/versions, cmd/utils: add geth version contract
1 parent 1c20313 commit ecd7199

4 files changed

Lines changed: 375 additions & 0 deletions

File tree

‎cmd/utils/flags.go‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
"github.com/ethereum/ethash"
3535
"github.com/ethereum/go-ethereum/accounts"
3636
"github.com/ethereum/go-ethereum/common"
37+
"github.com/ethereum/go-ethereum/common/versions"
3738
"github.com/ethereum/go-ethereum/core"
3839
"github.com/ethereum/go-ethereum/core/state"
3940
"github.com/ethereum/go-ethereum/crypto"
@@ -773,6 +774,12 @@ func MakeSystemNode(name, version string, extra []byte, ctx *cli.Context) *node.
773774
}
774775
}
775776

777+
err = stack.Register(func(ctx *node.ServiceContext) (node.Service, error) {
778+
return versions.NewVersionCheck(ctx)
779+
})
780+
if err != nil {
781+
Fatalf("Failed to register the Version Check service: %v", err)
782+
}
776783
return stack
777784
}
778785

‎common/versions/version.sol‎

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
// Copyright 2015 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
// WARNING: WORK IN PROGRESS & UNTESTED
18+
//
19+
// contract tracking versions added by designated signers.
20+
// designed to track versions of geth (go-ethereum) recommended by the
21+
// go-ethereum team. geth client interfaces with contract through ABI by simply
22+
// reading the full state and then deciding on recommended version based on
23+
// some logic (e.g. version date & number of signers).
24+
//
25+
// to keep things simple, the contract does not use FSM for multisig
26+
// but rather allows any designated signer to add a version or vote for an
27+
// existing version. this avoids need to track voting-in-progress states and
28+
// also provides history of all past versions.
29+
//
30+
31+
contract Versions {
32+
struct V {
33+
bytes32 v;
34+
uint64 ts;
35+
address[] signers;
36+
}
37+
38+
address[] public parties; // owners/signers
39+
address[] public deleteAcks; // votes to suicide contract
40+
uint public deleteAcksReq; // number of votes needed
41+
V[] public versions;
42+
43+
modifier canAccess(address addr) {
44+
bool access = false;
45+
for (uint i = 0; i < parties.length; i++) {
46+
if (parties[i] == addr) {
47+
access = true;
48+
break;
49+
}
50+
}
51+
if (access == false) {
52+
throw;
53+
}
54+
_
55+
}
56+
57+
function Versions(address[] addrs) {
58+
if (addrs.length < 2) {
59+
throw;
60+
}
61+
62+
parties = addrs;
63+
deleteAcksReq = (addrs.length / 2) + 1;
64+
}
65+
66+
// TODO: use dynamic array when solidity adds proper support for returning them
67+
function GetVersions() returns (bytes32[10], uint64[10], uint[10]) {
68+
bytes32[10] memory vs;
69+
uint64[10] memory ts;
70+
uint[10] memory ss;
71+
for (uint i = 0; i < versions.length; i++) {
72+
vs[i] = versions[i].v;
73+
ts[i] = versions[i].ts;
74+
ss[i] = versions[i].signers.length;
75+
}
76+
return (vs, ts, ss);
77+
}
78+
79+
// either submit a new version or acknowledge an existing one
80+
function AckVersion(bytes32 ver)
81+
canAccess(msg.sender)
82+
{
83+
for (uint i = 0; i < versions.length; i++) {
84+
if (versions[i].v == ver) {
85+
for (uint j = 0; j < versions[i].signers.length; j++) {
86+
if (versions[i].signers[j] == msg.sender) {
87+
// already signed
88+
throw;
89+
}
90+
}
91+
// add sender as signer of existing version
92+
versions[i].signers.push(msg.sender);
93+
return;
94+
}
95+
}
96+
97+
// version is new, add it
98+
// due to dynamic array, push it first then set values
99+
V memory v;
100+
versions.push(v);
101+
versions[versions.length - 1].v = ver;
102+
// signers is dynamic array; have to extend size manually
103+
versions[versions.length - 1].signers.length++;
104+
versions[versions.length - 1].signers[0] = msg.sender;
105+
versions[versions.length - 1].ts = uint64(block.timestamp);
106+
}
107+
108+
// remove vote for a version, if present
109+
function NackVersion(bytes32 ver)
110+
canAccess(msg.sender)
111+
{
112+
for (uint i = 0; i < versions.length; i++) {
113+
if (versions[i].v == ver) {
114+
for (uint j = 0; j < versions[i].signers.length; j++) {
115+
if (versions[i].signers[j] == msg.sender) {
116+
delete versions[i].signers[j];
117+
}
118+
}
119+
}
120+
}
121+
}
122+
123+
// delete-this-contract vote, suicide if enough votes
124+
function AckDelete()
125+
canAccess(msg.sender)
126+
{
127+
for (uint i = 0; i < deleteAcks.length; i++) {
128+
if (deleteAcks[i] == msg.sender) {
129+
throw; // already acked delete
130+
}
131+
}
132+
deleteAcks.push(msg.sender);
133+
if (deleteAcks.length >= deleteAcksReq) {
134+
suicide(msg.sender);
135+
}
136+
}
137+
138+
// remove sender's delete-this-contract vote, if present
139+
function NackDelete()
140+
canAccess(msg.sender)
141+
{
142+
uint len = deleteAcks.length;
143+
for (uint i = 0; i < len; i++) {
144+
if (deleteAcks[i] == msg.sender) {
145+
if (len > 1) {
146+
deleteAcks[i] = deleteAcks[len-1];
147+
}
148+
deleteAcks.length -= 1;
149+
}
150+
}
151+
}
152+
}

‎common/versions/versions.go‎

Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
// Copyright 2015 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package versions
18+
19+
import (
20+
"fmt"
21+
"math/big"
22+
"strconv"
23+
"time"
24+
25+
"github.com/ethereum/go-ethereum/common"
26+
"github.com/ethereum/go-ethereum/core"
27+
"github.com/ethereum/go-ethereum/core/state"
28+
"github.com/ethereum/go-ethereum/crypto"
29+
"github.com/ethereum/go-ethereum/eth"
30+
"github.com/ethereum/go-ethereum/logger"
31+
"github.com/ethereum/go-ethereum/logger/glog"
32+
"github.com/ethereum/go-ethereum/node"
33+
"github.com/ethereum/go-ethereum/p2p"
34+
"github.com/ethereum/go-ethereum/rpc"
35+
)
36+
37+
var (
38+
jsonlogger = logger.NewJsonLogger()
39+
// TODO: add Frontier address
40+
GlobalVersionsAddr = common.HexToAddress("0x40bebcadbb4456db23fda39f261f3b2509096e9e") // test
41+
dummySender = common.HexToAddress("0x16db48070243bc37a1c59cd5bb977ad7047618be") // test
42+
getVersionsSignature = "GetVersions()"
43+
firstCheckTime = time.Second * 4
44+
continousCheckTime = time.Second * 600
45+
)
46+
47+
type VersionCheck struct {
48+
serverName string
49+
timer *time.Timer
50+
e *eth.Ethereum
51+
stop chan bool
52+
}
53+
54+
// Boilerplate to satisfy node.Service interface
55+
func (v *VersionCheck) Protocols() []p2p.Protocol {
56+
return []p2p.Protocol{}
57+
}
58+
59+
func (v *VersionCheck) APIs() []rpc.API {
60+
return []rpc.API{}
61+
}
62+
63+
func (v *VersionCheck) Start(server *p2p.Server) error {
64+
v.serverName = server.Name
65+
// Check version first time after a few seconds so it shows after
66+
// other startup messages
67+
t := time.NewTimer(firstCheckTime)
68+
v.timer = t
69+
v.stop = make(chan bool)
70+
versionCheck := func() {
71+
for {
72+
select {
73+
case <-v.stop:
74+
close(v.stop)
75+
return
76+
case <-v.timer.C:
77+
_, err := get(v.e, v.serverName)
78+
if err != nil {
79+
glog.V(logger.Error).Infof("Could not query geth version contract: %s", err)
80+
}
81+
v.timer.Reset(continousCheckTime)
82+
}
83+
}
84+
}
85+
go versionCheck()
86+
return nil
87+
}
88+
89+
func (v *VersionCheck) Stop() error {
90+
v.stop <- true
91+
select {
92+
case <-v.stop:
93+
}
94+
return nil
95+
}
96+
97+
func NewVersionCheck(ctx *node.ServiceContext) (node.Service, error) {
98+
var v VersionCheck
99+
var e *eth.Ethereum
100+
// sets e to the Ethereum instance previously started
101+
// expects double pointer
102+
ctx.Service(&e)
103+
v.e = e
104+
return &v, nil
105+
}
106+
107+
// query versions list from the (custom) accessor in the versions contract
108+
func get(e *eth.Ethereum, clientVersion string) (string, error) {
109+
// TODO: move common/registrar abiSignature to some util package
110+
abi := crypto.Sha3([]byte(getVersionsSignature))[:4]
111+
res, _, err := simulateCall(
112+
e,
113+
&dummySender,
114+
&GlobalVersionsAddr,
115+
big.NewInt(3000000), // gasLimit
116+
big.NewInt(1), // gasPrice
117+
big.NewInt(0), // value
118+
abi)
119+
if err != nil {
120+
return "", err
121+
}
122+
123+
// TODO: we use static arrays of size versionCount as workaround
124+
// until solidity has proper support for returning dynamic arrays
125+
versionCount := 10
126+
127+
if len(res) != 2+(64*versionCount*3) { // 0x + three 32-byte fields per version
128+
return "", fmt.Errorf("unexpected result length from GetVersions")
129+
}
130+
131+
// TODO: use ABI (after solidity supports returning arrays of arrays and/or structs)
132+
var versions []string
133+
var timestamps []uint64
134+
var signerCounts []uint64
135+
136+
// trim 0x
137+
res = res[2:]
138+
139+
// parse res
140+
for i := 0; i < versionCount; i++ {
141+
bytes := common.FromHex(res[:64])
142+
versions = append(versions, string(bytes))
143+
res = res[64:]
144+
}
145+
146+
for i := 0; i < versionCount; i++ {
147+
ts, err := strconv.ParseUint(res[:64], 16, 64)
148+
if err != nil {
149+
return "", err
150+
}
151+
timestamps = append(timestamps, ts)
152+
res = res[64:]
153+
}
154+
155+
for i := 0; i < versionCount; i++ {
156+
sc, err := strconv.ParseUint(res[:64], 16, 64)
157+
if err != nil {
158+
return "", err
159+
}
160+
signerCounts = append(signerCounts, sc)
161+
res = res[64:]
162+
}
163+
164+
// TODO: version matching logic (e.g. most votes / most recent)
165+
if versions[0] != clientVersion {
166+
glog.V(logger.Info).Infof("geth version %s does not match recommended version %s", clientVersion, versions[0])
167+
}
168+
169+
return res, nil
170+
}
171+
172+
func simulateCall(e *eth.Ethereum, from0, to *common.Address, gas, gasPrice, value *big.Int, data []byte) (string, *big.Int, error) {
173+
stateCopy, err := e.BlockChain().State()
174+
if err != nil {
175+
return "", nil, err
176+
}
177+
from := stateCopy.GetOrNewStateObject(*from0)
178+
from.SetBalance(common.MaxBig)
179+
180+
msg := callmsg{
181+
from: from,
182+
to: to,
183+
gas: gas,
184+
gasPrice: gasPrice,
185+
value: value,
186+
data: data,
187+
}
188+
189+
// Execute the call and return
190+
vmenv := core.NewEnv(stateCopy, e.BlockChain(), msg, e.BlockChain().CurrentHeader())
191+
gp := new(core.GasPool).AddGas(common.MaxBig)
192+
193+
res, gas, err := core.ApplyMessage(vmenv, msg, gp)
194+
return common.ToHex(res), gas, err
195+
196+
}
197+
198+
// TODO: consider moving to package common or accounts/abi as it's useful for anyone
199+
// simulating EVM CALL
200+
type callmsg struct {
201+
from *state.StateObject
202+
to *common.Address
203+
gas, gasPrice *big.Int
204+
value *big.Int
205+
data []byte
206+
}
207+
208+
// accessor boilerplate to implement core.Message
209+
func (m callmsg) From() (common.Address, error) { return m.from.Address(), nil }
210+
func (m callmsg) Nonce() uint64 { return m.from.Nonce() }
211+
func (m callmsg) To() *common.Address { return m.to }
212+
func (m callmsg) GasPrice() *big.Int { return m.gasPrice }
213+
func (m callmsg) Gas() *big.Int { return m.gas }
214+
func (m callmsg) Value() *big.Int { return m.value }
215+
func (m callmsg) Data() []byte { return m.data }

‎node/node.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,7 @@ func (n *Node) Server() *p2p.Server {
530530
}
531531

532532
// Service retrieves a currently running service registered of a specific type.
533+
// NOTE: must be called with double pointer to service
533534
func (n *Node) Service(service interface{}) error {
534535
n.lock.RLock()
535536
defer n.lock.RUnlock()

0 commit comments

Comments
 (0)