Sitelet https://github.com/Fantom-foundation/go-opera/commit/aef2cb3296f69576aac5457168046a7b3c3991bb
Skip to content

Commit aef2cb3

Browse files
committed
typed ethclient.GetEvent()
1 parent 943af23 commit aef2cb3

3 files changed

Lines changed: 77 additions & 5 deletions

File tree

‎ethclient/dag_api.go‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,21 @@ package ethclient
22

33
import (
44
"context"
5-
"encoding/json"
65

76
"github.com/ethereum/go-ethereum"
7+
8+
"github.com/Fantom-foundation/go-opera/inter"
89
)
910

1011
// GetEvent returns Lachesis event by hash or short ID.
11-
func (ec *Client) GetEvent(ctx context.Context, shortEventID string, inclTx bool) (raw json.RawMessage, err error) {
12-
err = ec.c.CallContext(ctx, &raw, "dag_getEvent", shortEventID, inclTx)
12+
func (ec *Client) GetEvent(ctx context.Context, shortEventID string, inclTx bool) (inter.EventI, error) {
13+
var raw map[string]interface{}
14+
err := ec.c.CallContext(ctx, &raw, "dag_getEvent", shortEventID, inclTx)
1315
if err != nil {
1416
return nil, err
1517
} else if len(raw) == 0 {
1618
return nil, ethereum.NotFound
1719
}
1820

19-
return
21+
return inter.RPCUnmarshalEvent(raw), nil
2022
}

‎inter/event_serializer.go‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,57 @@ func RPCMarshalEvent(e EventI) map[string]interface{} {
250250
}
251251
}
252252

253+
// RPCUnmarshalEvent converts the RPC output to the header.
254+
func RPCUnmarshalEvent(data map[string]interface{}) EventI {
255+
mustBeUint64 := func(name string) uint64 {
256+
s := data[name].(string)
257+
return hexutil.MustDecodeUint64(s)
258+
}
259+
mustBeBytes := func(name string) []byte {
260+
s := data[name].(string)
261+
return hexutil.MustDecode(s)
262+
}
263+
mustBeID := func(name string) (id [24]byte) {
264+
s := data[name].(string)
265+
bb := hexutil.MustDecode(s)
266+
copy(id[:], bb)
267+
return
268+
}
269+
mayBeHash := func(name string) *hash.Hash {
270+
s, ok := data[name].(string)
271+
if !ok {
272+
return nil
273+
}
274+
bb := hexutil.MustDecode(s)
275+
h := hash.BytesToHash(bb)
276+
return &h
277+
}
278+
279+
e := MutableEventPayload{}
280+
281+
e.SetEpoch(idx.Epoch(mustBeUint64("epoch")))
282+
e.SetSeq(idx.Event(mustBeUint64("seq")))
283+
e.SetID(mustBeID("id"))
284+
e.SetFrame(idx.Frame(mustBeUint64("frame")))
285+
e.SetCreator(idx.ValidatorID(mustBeUint64("creator")))
286+
e.SetPrevEpochHash(mayBeHash("prevEpochHash"))
287+
e.SetParents(HexToEventIDs(data["parents"].([]interface{})))
288+
e.SetLamport(idx.Lamport(mustBeUint64("lamport")))
289+
e.SetCreationTime(Timestamp(mustBeUint64("creationTime")))
290+
e.SetMedianTime(Timestamp(mustBeUint64("medianTime")))
291+
e.SetExtra(mustBeBytes("extraData"))
292+
e.SetTxHash(*mayBeHash("transactionsRoot"))
293+
e.SetGasPowerUsed(mustBeUint64("gasPowerUsed"))
294+
295+
gas := GasPowerLeft{}
296+
obj := data["gasPowerLeft"].(map[string]interface{})
297+
gas.Gas[ShortTermGas] = hexutil.MustDecodeUint64(obj["shortTerm"].(string))
298+
gas.Gas[LongTermGas] = hexutil.MustDecodeUint64(obj["longTerm"].(string))
299+
e.SetGasPowerLeft(gas)
300+
301+
return e.Build()
302+
}
303+
253304
// RPCMarshalEventPayload converts the given event to the RPC output which depends on fullTx. If inclTx is true transactions are
254305
// returned. When fullTx is true the returned block contains full transaction details, otherwise it will only contain
255306
// transaction hashes.

‎inter/event_serializer_test.go‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package inter
22

33
import (
44
"bytes"
5+
"encoding/json"
56
"math"
67
"math/big"
78
"math/rand"
@@ -214,6 +215,25 @@ func BenchmarkEventPayload_DecodeRLP(b *testing.B) {
214215
}
215216
}
216217

218+
func TestEventRPCMarshaling(t *testing.T) {
219+
require := require.New(t)
220+
221+
for i := 0; i < 10; i++ {
222+
var event0 EventI = FakeEvent(i)
223+
224+
mapping := RPCMarshalEvent(event0)
225+
bb, err := json.Marshal(mapping)
226+
require.NoError(err)
227+
228+
mapping = make(map[string]interface{})
229+
err = json.Unmarshal(bb, &mapping)
230+
require.NoError(err)
231+
232+
event1 := RPCUnmarshalEvent(mapping)
233+
require.Equal(event0, event1)
234+
}
235+
}
236+
217237
func randBig(r *rand.Rand) *big.Int {
218238
b := make([]byte, r.Intn(8))
219239
_, _ = r.Read(b)
@@ -254,7 +274,6 @@ func randAccessList(r *rand.Rand, maxAddrs, maxKeys int) types.AccessList {
254274

255275
// FakeEvent generates random event for testing purpose.
256276
func FakeEvent(txsNum int) *EventPayload {
257-
258277
r := rand.New(rand.NewSource(int64(0)))
259278
random := MutableEventPayload{}
260279
random.SetLamport(1000)

0 commit comments

Comments
 (0)