-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscenario.go
More file actions
77 lines (67 loc) · 2 KB
/
scenario.go
File metadata and controls
77 lines (67 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package types
import (
"encoding/json"
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/common"
ethtypes "github.com/ethereum/go-ethereum/core/types"
)
// LoadTx is a wrapper that has pre-encoded json rpc payload and eth transaction.
type LoadTx struct {
EthTx *ethtypes.Transaction
JSONRPCPayload []byte
Payload []byte
Scenario *TxScenario
}
// JSONRPCRequest represents json rpc request.
type JSONRPCRequest struct {
Version string `json:"jsonrpc,omitempty"`
ID json.RawMessage `json:"id,omitempty"`
Method string `json:"method,omitempty"`
Params json.RawMessage `json:"params,omitempty"`
}
func toJSONRequestBytes(rawTx []byte) ([]byte, error) {
req := &JSONRPCRequest{
Version: "2.0",
Method: "eth_sendRawTransaction",
Params: json.RawMessage(fmt.Sprintf(`["0x%x"]`, rawTx)),
ID: json.RawMessage("0"),
}
b, err := json.Marshal(req)
if err != nil {
return nil, err
}
return b, nil
}
// ShardID returns the shard id for the given number of shards.
func (tx *LoadTx) ShardID(n int) int {
addressBigInt := new(big.Int).SetBytes(tx.Scenario.Sender.Address.Bytes())
mod := new(big.Int).Mod(addressBigInt, big.NewInt(int64(n)))
return int(mod.Int64())
}
// TxScenario captures the scenario of this test transaction.
type TxScenario struct {
Name string
Sender *Account
Receiver common.Address
}
// CreateTxFromEthTx creates a LoadTx from an EthTx (pre-marshaled).
func CreateTxFromEthTx(tx *ethtypes.Transaction, scenario *TxScenario) *LoadTx {
// Convert to raw transaction bytes for JSON-RPC payload
rawTx, err := tx.MarshalBinary()
if err != nil {
panic("Failed to marshal transaction: " + err.Error())
}
// Create JSON-RPC payload
jsonRPCPayload, err := toJSONRequestBytes(rawTx)
if err != nil {
panic("Failed to create JSON-RPC payload: " + err.Error())
}
// Return the complete LoadTx object
return &LoadTx{
EthTx: tx,
JSONRPCPayload: jsonRPCPayload,
Payload: rawTx,
Scenario: scenario,
}
}